Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davdarras committed Mar 29, 2024
1 parent f913c76 commit 6c387eb
Showing 1 changed file with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fr.insee.queen.application.utils.AuthenticatedUserTestHelper;
import fr.insee.queen.application.utils.JsonTestHelper;
import io.zonky.test.db.AutoConfigureEmbeddedDatabase;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
Expand All @@ -17,6 +18,7 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.authentication;
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
Expand Down Expand Up @@ -153,8 +155,9 @@ void on_update_data_when_anonymous_user_return_401() throws Exception {
}

@Test
@DisplayName("Given survey unit data with collected data, when inserting partial collected data, then merge collected datas")
@Sql(value = ScriptConstants.REINIT_SQL_SCRIPT, executionPhase = AFTER_TEST_METHOD)
void on_update_collected_data_data_is_updated() throws Exception {
void updateCollectedData01() throws Exception {
String surveyUnitId = "11";
String collectedDataJson = """
{
Expand All @@ -167,15 +170,6 @@ void on_update_collected_data_data_is_updated() throws Exception {
},
"COMMENT": null
}""";
MvcResult result = mockMvc.perform(get("/api/survey-unit/" + surveyUnitId + "/data")
.accept(MediaType.APPLICATION_JSON)
.with(authentication(authenticatedUserTestHelper.getSurveyUnitUser()))
)
.andExpect(status().isOk())
.andReturn();

String content = result.getResponse().getContentAsString();
JSONAssert.assertNotEquals(collectedDataJson, content, JSONCompareMode.NON_EXTENSIBLE);

mockMvc.perform(patch("/api/survey-unit/" + surveyUnitId + "/data")
.content(collectedDataJson)
Expand All @@ -185,14 +179,14 @@ void on_update_collected_data_data_is_updated() throws Exception {
)
.andExpect(status().isOk());

result = mockMvc.perform(get("/api/survey-unit/" + surveyUnitId + "/data")
MvcResult result = mockMvc.perform(get("/api/survey-unit/" + surveyUnitId + "/data")
.accept(MediaType.APPLICATION_JSON)
.with(authentication(authenticatedUserTestHelper.getSurveyUnitUser()))
)
.andExpect(status().isOk())
.andReturn();

content = result.getResponse().getContentAsString();
String content = result.getResponse().getContentAsString();
String expectedResult = """
{
"EXTERNAL":{
Expand Down Expand Up @@ -223,7 +217,38 @@ void on_update_collected_data_data_is_updated() throws Exception {
}

@Test
void on_update_collected_data_when_su_id_invalid_return_400() throws Exception {
@DisplayName("Given survey unit with no collected json data, when updating data then insert partial data as collected data")
@Sql(value = ScriptConstants.REINIT_SQL_SCRIPT, executionPhase = AFTER_TEST_METHOD)
void updateCollectedData02() throws Exception {
String surveyUnitId = "21";
String collectedDataJson = """
{
"COMMENT": null
}""";

mockMvc.perform(patch("/api/survey-unit/" + surveyUnitId + "/data")
.content(collectedDataJson)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.with(authentication(authenticatedUserTestHelper.getSurveyUnitUser()))
)
.andExpect(status().isOk());

MvcResult result = mockMvc.perform(get("/api/survey-unit/" + surveyUnitId + "/data")
.accept(MediaType.APPLICATION_JSON)
.with(authentication(authenticatedUserTestHelper.getSurveyUnitUser()))
)
.andExpect(status().isOk())
.andReturn();

String content = result.getResponse().getContentAsString();
String expectedResult = "{\"COLLECTED\":" + collectedDataJson + "}";
JSONAssert.assertEquals(expectedResult, content, JSONCompareMode.STRICT);
}

@Test
@DisplayName("Given invalid survey unit id, when updating collected data then throw bad request")
void updateCollectedDataError02() throws Exception {
mockMvc.perform(patch("/api/survey-unit/invalid$identifier/data")
.content("{}")
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -234,7 +259,8 @@ void on_update_collected_data_when_su_id_invalid_return_400() throws Exception {
}

@Test
void on_update_collected_data_when_data_not_json_object_node_return_400() throws Exception {
@DisplayName("Given invalid json collected input data, when updating collected data then throw bad request")
void updateCollectedDataError03() throws Exception {
mockMvc.perform(patch("/api/survey-unit/12/data")
.content("[]")
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -245,7 +271,8 @@ void on_update_collected_data_when_data_not_json_object_node_return_400() throws
}

@Test
void on_update_collected_data_when_anonymous_user_return_401() throws Exception {
@DisplayName("Given an anoymous user, when updating collected data then return unauthenticated error")
void updateCollectedDataError04() throws Exception {
mockMvc.perform(patch("/api/survey-unit/12/data")
.content("{}")
.contentType(MediaType.APPLICATION_JSON)
Expand Down

0 comments on commit 6c387eb

Please sign in to comment.