Skip to content

Commit

Permalink
test: add coverage for find survey endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
davdarras committed Jan 23, 2025
1 parent 4f526f5 commit c00676a
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package fr.insee.survey.datacollectionmanagement.integration;

import fr.insee.survey.datacollectionmanagement.configuration.AuthenticationUserProvider;
import fr.insee.survey.datacollectionmanagement.constants.AuthorityRoleEnum;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Source;
import fr.insee.survey.datacollectionmanagement.metadata.domain.Survey;
import fr.insee.survey.datacollectionmanagement.metadata.repository.SourceRepository;
import fr.insee.survey.datacollectionmanagement.metadata.repository.SurveyRepository;
import fr.insee.survey.datacollectionmanagement.questioning.dto.SearchSurveyUnitDtoImpl;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import jakarta.transaction.Transactional;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class SurveySteps {
@Autowired
private MockMvc mockMvc;

@Autowired
private SurveyRepository surveyRepository;

@Autowired
private SourceRepository sourceRepository;

private Page<SearchSurveyUnitDtoImpl> pageSearchSurveyUnit;
private String surveyId = "SURVEY-ID";
private String role;
private ResultActions resultActions;

@Transactional
@Given("a survey exists")
public void createSurvey() {
Survey survey = new Survey();
survey.setId(surveyId);
survey.setCnisUrl("cnisUrl");
survey.setCommunication("communication");
survey.setLongWording("longWording");
survey.setShortWording("shortWording");
survey.setSampleSize(10);
survey.setYear(2024);
survey.setDiffusionUrl("diffusionUrl");
survey.setLongObjectives("longObjectives");
survey.setShortObjectives("shortObjectives");
survey.setSpecimenUrl("specimenUrl");
survey.setNoticeUrl("noticeUrl");
survey.setVisaNumber("visa");
Source source = new Source();
source.setId("SOURCE-ID");
source = sourceRepository.save(source);

survey.setSource(source);
surveyRepository.save(survey);
}

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

@When("I'm searching the existing survey")
public void searchSurveyById() throws Exception {
resultActions = mockMvc.perform(get("/api/surveys/" + surveyId));
}

@Then("the survey is returned")
public void surveyReturned() throws Exception {
MvcResult mvcResult = resultActions.andExpect(status().isOk())
.andReturn();
String content = mvcResult.getResponse().getContentAsString();
String expectedContent = """
{
"id": "SURVEY-ID",
"cnisUrl": "cnisUrl",
"communication": "communication",
"longWording": "longWording",
"shortWording": "shortWording",
"sampleSize": 10,
"year": 2024,
"diffusionUrl": "diffusionUrl",
"longObjectives": "longObjectives",
"shortObjectives": "shortObjectives",
"specimenUrl": "specimenUrl",
"noticeUrl": "noticeUrl",
"visaNumber": "visa",
"sourceId": "SOURCE-ID"
}
""";
JSONAssert.assertEquals(expectedContent, content, JSONCompareMode.NON_EXTENSIBLE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Feature: search for survey

Background:
Given a survey exists

Scenario: search for a survey
Given I am an authenticated user
When I'm searching the existing survey
Then the survey is returned

0 comments on commit c00676a

Please sign in to comment.