Skip to content

Commit

Permalink
test: add search contact cucumber test
Browse files Browse the repository at this point in the history
  • Loading branch information
BettyB979 committed Aug 8, 2024
1 parent fa5a321 commit b038190
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fr.insee.survey.datacollectionmanagement.contact.dto;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class SearchContactDtoImpl implements SearchContactDto{

private String identifier;
private String email;
private String firstName;
private String lastName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void createSurveyUnit(String idSu, String label) {
surveyUnitRepository.save(su);
}

@Given("the contact {string} with firstname {string} and lastanme {string} and gender {string} and the streetnumber {string}")
@Given("the contact {string} with firstname {string} and lastname {string} and gender {string} and the streetnumber {string}")
public void createContact(String contactId, String firstName, String lastName, String gender, String streetNumber) {
Contact c = new Contact();
c.setIdentifier(contactId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package fr.insee.survey.datacollectionmanagement.query;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.insee.survey.datacollectionmanagement.config.AuthenticationUserProvider;
import fr.insee.survey.datacollectionmanagement.config.auth.user.AuthorityRoleEnum;
import fr.insee.survey.datacollectionmanagement.constants.Constants;
import fr.insee.survey.datacollectionmanagement.contact.domain.Contact;
import fr.insee.survey.datacollectionmanagement.contact.dto.SearchContactDtoImpl;
import fr.insee.survey.datacollectionmanagement.contact.repository.ContactRepository;
import io.cucumber.datatable.DataTable;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@AutoConfigureMockMvc
@SpringBootTest
@ActiveProfiles("test")
public class SearchContactSteps {

@Autowired
private MockMvc mockMvc;

@Autowired
private ObjectMapper objectMapper;

@Autowired
private ContactRepository contactRepository;

private MvcResult mvcResult;
private Page<SearchContactDtoImpl> pageSearchContact;
private String role;

@Transactional
@Given("the following contacts exist:")
public void createContacts(DataTable dataTable) {
List<Map<String, String>> rows = dataTable.asMaps(String.class, String.class);
for (Map<String, String> row : rows) {
Contact contact = new Contact();
contact.setIdentifier(row.get("idep"));
contact.setFirstName(row.get("firstname"));
contact.setLastName(row.get("lastname"));
contact.setEmail(row.get("email"));
contactRepository.save(contact);
}
}

@Given("I am a survey manager")
public void setRole() {
role = AuthorityRoleEnum.INTERNAL_USER.name();
SecurityContextHolder.getContext()
.setAuthentication(AuthenticationUserProvider.getAuthenticatedUser("USER", AuthorityRoleEnum.valueOf(role)));
}

@When("I type {string} in the searching area")
public void searchContact(String param) throws Exception {
mvcResult = mockMvc.perform(get(Constants.API_CONTACTS_SEARCH)
.param("param", param))
.andExpect(status().isOk())
.andReturn();

String content = mvcResult.getResponse().getContentAsString();

Map<String, Object> result = objectMapper.readValue(content, new TypeReference<>() {
});
List<SearchContactDtoImpl> contentList = objectMapper.convertValue(result.get("content"), new TypeReference<>() {
});

pageSearchContact = new PageImpl<>(contentList);
}

@Then("I found the following contacts:")
public void iFoundTheFollowingContacts(DataTable expectedTable) {
List<Map<String, String>> expectedRows = expectedTable.asMaps(String.class, String.class);

for (Map<String, String> expectedRow : expectedRows) {
String expectedIdentifier = expectedRow.get("idep");
boolean found = pageSearchContact.getContent().stream()
.anyMatch(contact -> contact.getIdentifier().equalsIgnoreCase(expectedIdentifier));
assertTrue(found, "Expected to find contact with identifier: " + expectedIdentifier);
}
}

@Then("I found nothing")
public void iFoundNothing() {
assertTrue(pageSearchContact.isEmpty(), "Expected to find no contacts");
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Feature: Get Questioning Informations
Given the campaign "EAP2023T01" related to survey "EAP2023"
Given the partitioning "EAP2023T0100" related to campaign "EAP2023T01"
Given the survey unit "TESTCASE" with label "entreprise"
Given the contact "USER01" with firstname "Nom" and lastanme "Prenom" and gender "Male" and the streetnumber "17"
Given the contact "USER02" with firstname "Nom2" and lastanme "Prenom2" and gender "Female" and the streetnumber "17"
Given the contact "USER01" with firstname "Nom" and lastname "Prenom" and gender "Male" and the streetnumber "17"
Given the contact "USER02" with firstname "Nom2" and lastname "Prenom2" and gender "Female" and the streetnumber "17"
Given the questioning for partitioning "EAP2023T0100" survey unit id "TESTCASE" and model "model" and main contact "USER01"
Given the questioning for partitioning "EAP2023T0100" survey unit id "TESTCASE" and model "model" and contact "USER02"

Expand Down
47 changes: 47 additions & 0 deletions src/test/resources/integration/query/search_contact.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Feature: Search for a contact

Background:
Given the following contacts exist:
| idep | lastname | firstname | email |
| JD2024 | Doe | John | john.doe@gmail.com |
| DD1234 | Durant | Doeris | dd1995@orange.fr |
| ABCD12 | DOEDOE | johnny | jojodu94@yahoo.fr |
| DOE203 | | | |
| COCO54 | BOOP | Betty | betty.boop@free.fr |

Scenario: search for John Doe
Given I am a survey manager
When I type "Joh" in the searching area
Then I found the following contacts:
| idep |
| ABCD12 |
| JD2024 |

Scenario: search for a contact who has a name or surname beginning by Doe
Given I am a survey manager
When I type "Doe" in the searching area
Then I found the following contacts:
| idep |
| JD2024 |
| DD1234 |
| ABCD12 |
| DOE203 |

Scenario: search for a contact who does not exist
Given I am a survey manager
When I type "Cam" in the searching area
Then I found nothing

Scenario: search for betty boop
Given I am a survey manager
When I type "bet" in the searching area
Then I found the following contacts:
| idep |
| COCO54 |

Scenario: search for John Doe by his "idep"
Given I am a survey manager
When I type "JD2" in the searching area
Then I found the following contacts:
| idep |
| JD2024 |

0 comments on commit b038190

Please sign in to comment.