From b0381902c2dc668f9c95c97bab77fd4ba1398bca Mon Sep 17 00:00:00 2001 From: y72wvh Date: Thu, 8 Aug 2024 15:49:29 +0200 Subject: [PATCH] test: add search contact cucumber test --- .../contact/dto/SearchContactDtoImpl.java | 14 +++ .../query/QuestioningInformationsSteps.java | 2 +- .../query/SearchContactSteps.java | 107 ++++++++++++++++++ .../get_questioning_informations.feature | 4 +- .../integration/query/search_contact.feature | 47 ++++++++ 5 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 src/main/java/fr/insee/survey/datacollectionmanagement/contact/dto/SearchContactDtoImpl.java create mode 100644 src/test/java/fr/insee/survey/datacollectionmanagement/query/SearchContactSteps.java create mode 100644 src/test/resources/integration/query/search_contact.feature diff --git a/src/main/java/fr/insee/survey/datacollectionmanagement/contact/dto/SearchContactDtoImpl.java b/src/main/java/fr/insee/survey/datacollectionmanagement/contact/dto/SearchContactDtoImpl.java new file mode 100644 index 00000000..b70d7101 --- /dev/null +++ b/src/main/java/fr/insee/survey/datacollectionmanagement/contact/dto/SearchContactDtoImpl.java @@ -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; +} diff --git a/src/test/java/fr/insee/survey/datacollectionmanagement/query/QuestioningInformationsSteps.java b/src/test/java/fr/insee/survey/datacollectionmanagement/query/QuestioningInformationsSteps.java index 3b6dd509..2f7420ba 100644 --- a/src/test/java/fr/insee/survey/datacollectionmanagement/query/QuestioningInformationsSteps.java +++ b/src/test/java/fr/insee/survey/datacollectionmanagement/query/QuestioningInformationsSteps.java @@ -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); diff --git a/src/test/java/fr/insee/survey/datacollectionmanagement/query/SearchContactSteps.java b/src/test/java/fr/insee/survey/datacollectionmanagement/query/SearchContactSteps.java new file mode 100644 index 00000000..ba4f1f2f --- /dev/null +++ b/src/test/java/fr/insee/survey/datacollectionmanagement/query/SearchContactSteps.java @@ -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 pageSearchContact; + private String role; + + @Transactional + @Given("the following contacts exist:") + public void createContacts(DataTable dataTable) { + List> rows = dataTable.asMaps(String.class, String.class); + for (Map 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 result = objectMapper.readValue(content, new TypeReference<>() { + }); + List contentList = objectMapper.convertValue(result.get("content"), new TypeReference<>() { + }); + + pageSearchContact = new PageImpl<>(contentList); + } + + @Then("I found the following contacts:") + public void iFoundTheFollowingContacts(DataTable expectedTable) { + List> expectedRows = expectedTable.asMaps(String.class, String.class); + + for (Map 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"); + } + +} + diff --git a/src/test/resources/integration/query/get_questioning_informations.feature b/src/test/resources/integration/query/get_questioning_informations.feature index f0faf88d..6643d6c0 100644 --- a/src/test/resources/integration/query/get_questioning_informations.feature +++ b/src/test/resources/integration/query/get_questioning_informations.feature @@ -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" diff --git a/src/test/resources/integration/query/search_contact.feature b/src/test/resources/integration/query/search_contact.feature new file mode 100644 index 00000000..ec1d8334 --- /dev/null +++ b/src/test/resources/integration/query/search_contact.feature @@ -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 | \ No newline at end of file