-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add search contact cucumber test
- Loading branch information
Showing
5 changed files
with
171 additions
and
3 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/main/java/fr/insee/survey/datacollectionmanagement/contact/dto/SearchContactDtoImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/test/java/fr/insee/survey/datacollectionmanagement/query/SearchContactSteps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/test/resources/integration/query/search_contact.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |