Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add a test and refactor #218

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,76 +35,91 @@ class CodeListImplTest {
@Mock
Config config;
@InjectMocks
CodeListImpl codeListImpl=new CodeListImpl(new FreeMarkerUtilsStub());
@InjectMocks
CodeListImpl codeList = new CodeListImpl();
CodeListImpl codeListImpl = new CodeListImpl(new FreeMarkerUtilsStub());


@BeforeAll
static void setUp(){
Config.LG1="fr";
Config.LG2="en";
Config.BASE_GRAPH="http://rdf.insee.fr/graphes/";
Config.CODELIST_GRAPH="codes";
static void setUp() {
Config.LG1 = "fr";
Config.LG2 = "en";
Config.BASE_GRAPH = "http://rdf.insee.fr/graphes/";
Config.CODELIST_GRAPH = "codes";
}


@Test
void getAllCodesLists_shouldReturnList() throws RmesException, JsonProcessingException {
JSONArray mockJSON = new JSONArray(CodeListUtilsTest.RDF_OUTPUT);

Mockito.when(repoGestion.getResponseAsArray(Mockito.anyString())).thenReturn(mockJSON);
assertThat(MAPPER.readTree(codeList.getAllCodesLists(""))).isEqualTo(MAPPER.readTree(CodeListUtilsTest.RDF_OUTPUT_EXPECTED));
assertThat(MAPPER.readTree(codeListImpl.getAllCodesLists(""))).isEqualTo(MAPPER.readTree(CodeListUtilsTest.RDF_OUTPUT_EXPECTED));

}


@Test
void getCodesListWithCodeWithStatutValidation_shouldReturnExpectedCodeList() throws RmesException, JsonProcessingException {
JSONObject mockJSON_requete1 = new JSONObject(CodeListUtilsTest.CODELIST_WITH_STATUT_VALIDATION);
JSONArray mockJSON_requete2 = new JSONArray(CodeListUtilsTest.CODES);
JSONArray mockJSON_requete3 = new JSONArray(ResponseUtilsTest.EMPTY_JSON_ARRAY);
String mockString = new String (CodeListUtilsTest.CODELIST_GRAPH);
JSONArray mockJSON_requete4 = new JSONArray(ResponseUtilsTest.EMPTY_JSON_ARRAY);
when(repoGestion.getResponseAsObject("getCodesList.ftlh")).thenReturn(mockJSON_requete1);
when(repoGestion.getResponseAsArray("getCodes.ftlh")).thenReturn(mockJSON_requete2);
when(repoGestion.getResponseAsArray("getCodeLevel.ftlh")).thenReturn(mockJSON_requete3);
JSONObject mockJSON1 = new JSONObject(CodeListUtilsTest.CODELIST_WITH_STATUT_VALIDATION);
JSONArray mockJSON2 = new JSONArray(CodeListUtilsTest.CODES);
JSONArray mockJSON3 = new JSONArray(ResponseUtilsTest.EMPTY_JSON_ARRAY);
String mockString = new String(CodeListUtilsTest.CODELIST_GRAPH);
JSONArray mockJSON4 = new JSONArray(ResponseUtilsTest.EMPTY_JSON_ARRAY);
when(repoGestion.getResponseAsObject("getCodesList.ftlh")).thenReturn(mockJSON1);
when(repoGestion.getResponseAsArray("getCodes.ftlh")).thenReturn(mockJSON2);
when(repoGestion.getResponseAsArray("getCodeLevel.ftlh")).thenReturn(mockJSON3);
when(config.getCodelistGraph()).thenReturn(mockString);
when(repoGestion.getResponseAsArray("getMatch.ftlh")).thenReturn(mockJSON_requete4);
assertThat(MAPPER.readTree(codeListImpl.getCodesList("1"))).isEqualTo(MAPPER.readTree(CodeListUtilsTest.CODELIST_WITH_STATUT_VALIDATION_EXPECTED.toString()));
when(repoGestion.getResponseAsArray("getMatch.ftlh")).thenReturn(mockJSON4);
assertThat(MAPPER.readTree(codeListImpl.getCodesList("1"))).isEqualTo(MAPPER.readTree(CodeListUtilsTest.CODELIST_WITH_STATUT_VALIDATION_EXPECTED));
}

@Test
void getCodesListWithCodeWithoutStatutValidation_shouldReturnExpectedCodeList() throws RmesException, JsonProcessingException {
JSONObject mockJSON_requete1 = new JSONObject(CodeListUtilsTest.CODELIST_WITHOUT_STATUT_VALIDATION);
JSONArray mockJSON_requete2 = new JSONArray(CodeListUtilsTest.CODES);
JSONArray mockJSON_requete3 = new JSONArray(ResponseUtilsTest.EMPTY_JSON_ARRAY);
String mockString = new String (CodeListUtilsTest.CODELIST_GRAPH);
JSONArray mockJSON_requete4 = new JSONArray(ResponseUtilsTest.EMPTY_JSON_ARRAY);
when(repoGestion.getResponseAsObject("getCodesList.ftlh")).thenReturn(mockJSON_requete1);
when(repoGestion.getResponseAsArray("getCodes.ftlh")).thenReturn(mockJSON_requete2);
when(repoGestion.getResponseAsArray("getCodeLevel.ftlh")).thenReturn(mockJSON_requete3);
JSONObject mockJSON1 = new JSONObject(CodeListUtilsTest.CODELIST_WITHOUT_STATUT_VALIDATION_WITHOUT_CODE);
JSONArray mockJSON2 = new JSONArray(CodeListUtilsTest.CODES);
JSONArray mockJSON3 = new JSONArray(ResponseUtilsTest.EMPTY_JSON_ARRAY);
String mockString = new String(CodeListUtilsTest.CODELIST_GRAPH);
JSONArray mockJSON4 = new JSONArray(ResponseUtilsTest.EMPTY_JSON_ARRAY);
when(repoGestion.getResponseAsObject("getCodesList.ftlh")).thenReturn(mockJSON1);
when(repoGestion.getResponseAsArray("getCodes.ftlh")).thenReturn(mockJSON2);
when(repoGestion.getResponseAsArray("getCodeLevel.ftlh")).thenReturn(mockJSON3);
when(config.getCodelistGraph()).thenReturn(mockString);
when(repoGestion.getResponseAsArray("getMatch.ftlh")).thenReturn(mockJSON_requete4);
assertThat(MAPPER.readTree(codeListImpl.getCodesList("1"))).isEqualTo(MAPPER.readTree(CodeListUtilsTest.CODELIST_WITHOUT_STATUT_VALIDATION_EXPECTED.toString()));
when(repoGestion.getResponseAsArray("getMatch.ftlh")).thenReturn(mockJSON4);
assertThat(MAPPER.readTree(codeListImpl.getCodesList("1"))).isEqualTo(MAPPER.readTree(CodeListUtilsTest.CODELIST_WITHOUT_STATUT_VALIDATION_EXPECTED));
}

@Test
void getCodesListWithCode_shouldReturn404IfInexistentId() throws RmesException, JsonProcessingException {
void getCodesList_shouldReturn404IfInexistentId() throws RmesException {
JSONObject mockJSON = new JSONObject(ResponseUtilsTest.EMPTY_JSON_OBJECT);
when(repoGestion.getResponseAsObject(Mockito.anyString())).thenReturn(mockJSON);

assertThatThrownBy(()->codeListImpl.getCodesList("1")).isInstanceOf(RmesException.class)
.matches(rmesException->((RmesException)rmesException).getStatus()==404)
assertThatThrownBy(() -> codeListImpl.getCodesList("1")).isInstanceOf(RmesException.class)
.matches(rmesException -> ((RmesException) rmesException).getStatus() == 404)
.hasMessageContaining("Non existent codes list identifier");
}

@Test
void getCodesListPagination_shouldReturn404IfInexistentId() throws RmesException, JsonProcessingException {
void getCodesListPagination_shouldReturn404IfInexistentId() throws RmesException {
JSONObject mockJSON = new JSONObject(ResponseUtilsTest.EMPTY_JSON_OBJECT);
when(repoGestion.getResponseAsObject(Mockito.anyString())).thenReturn(mockJSON);

assertThatThrownBy(()->codeListImpl.getMaxpage("1")).isInstanceOf(RmesException.class)
.matches(rmesException->((RmesException)rmesException).getStatus()==404)
assertThatThrownBy(() -> codeListImpl.getMaxpage("1")).isInstanceOf(RmesException.class)
.matches(rmesException -> ((RmesException) rmesException).getStatus() == 404)
.hasMessageContaining("Non existent codes list identifier");
}

@Test
void getCodesListWithoutCodes_shouldReturnExpectedCodeList() throws RmesException, JsonProcessingException {
JSONObject mockJSON = new JSONObject(CodeListUtilsTest.CODELIST_WITHOUT_STATUT_VALIDATION_WITHOUT_CODE);
when(repoGestion.getResponseAsObject("getCodesList.ftlh")).thenReturn(mockJSON);
assertThat(MAPPER.readTree(codeListImpl.getCodesListWithoutCodes("1"))).isEqualTo(MAPPER.readTree(CodeListUtilsTest.CODELIST_WITHOUT_CODE_EXPECTED));
}

@Test
void getCodesListForDatasetTest() throws RmesException, JsonProcessingException {
JSONObject mockJSON1 = new JSONObject(CodeListUtilsTest.CODELIST_WITH_STATUT_VALIDATION);
JSONArray mockJSON2 = new JSONArray(CodeListUtilsTest.EMPTY_ARRAY);
when(repoGestion.getResponseAsObject("getCodesListForDataset.ftlh")).thenReturn(mockJSON1);
when(repoGestion.getResponseAsArray("getCodes.ftlh")).thenReturn(mockJSON2);
when(repoGestion.getResponseAsArray("getCodeLevel.ftlh")).thenReturn(mockJSON2);
assertThat(MAPPER.readTree(codeListImpl.getCodesListForDataset("1"))).isEqualTo(MAPPER.readTree(CodeListUtilsTest.CODES_FOR_DATASET_EXPECTED));
}
}
Loading
Loading