Skip to content

Commit

Permalink
fix:testsCantons (#101)
Browse files Browse the repository at this point in the history
* fix:testsCantons

2 tests in error because of wrong Type value and balises in wrong order

* fix-testsCanton

query correction

* revert for expected Type and modify mock

* Update ConstantForIntegration.java

correction of error

* Fix(projection) : update sparql request  and modify CSVMapper

* fix:testsCantons

2 tests in error because of wrong Type value and balises in wrong order

* fix-testsCanton

query correction

* revert for expected Type and modify mock

* Update ConstantForIntegration.java

correction of error

* feat(prop for baseHost) baseHost is an internal property of th release which shouldn't be override in insee production context

* test(cantons) add tests for commmunes inside a canton : need business answers

* merge reabse with main

* tests(canton) fix and improve tests for CantonOuVilleApiTest and CantonAPITest

* Fix(intituleComplet): update ftlh

* Fix(CantonOuVilleApiTest): public to private

---------

Co-authored-by: Jérôme Bourgeois <[email protected]>
Co-authored-by: Fabrice Bibonne <[email protected]>
  • Loading branch information
3 people authored Oct 17, 2023
1 parent b918aa9 commit b0dcf41
Show file tree
Hide file tree
Showing 17 changed files with 965 additions and 1,240 deletions.
10 changes: 7 additions & 3 deletions src/main/java/fr/insee/rmes/api/AbstractMetadataApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ public abstract class AbstractMetadataApi {

protected AbstractMetadataApi() {

this.sparqlUtils = new SparqlUtils();
this.csvUtils = new CSVUtils();
this.responseUtils = new ResponseUtils();
this(new SparqlUtils(), new CSVUtils(), new ResponseUtils());
}

protected AbstractMetadataApi(SparqlUtils sparqlUtils, CSVUtils csvUtils, ResponseUtils responseUtils) {
this.sparqlUtils=sparqlUtils;
this.csvUtils=csvUtils;
this.responseUtils=responseUtils;
}

/**
Expand Down
37 changes: 19 additions & 18 deletions src/main/java/fr/insee/rmes/api/geo/AbstractGeoApi.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
package fr.insee.rmes.api.geo;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import fr.insee.rmes.modeles.geo.territoire.Commune;
import fr.insee.rmes.modeles.geo.territoires.Communes;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import fr.insee.rmes.api.AbstractMetadataApi;
import fr.insee.rmes.modeles.geo.EnumTypeGeographie;
import fr.insee.rmes.modeles.geo.territoire.Projection;
import fr.insee.rmes.modeles.geo.territoire.Territoire;
import fr.insee.rmes.modeles.geo.territoires.Projections;
import fr.insee.rmes.queries.geo.CsvGeoUtils;
import fr.insee.rmes.utils.Constants;
import fr.insee.rmes.utils.DateUtils;
import fr.insee.rmes.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

public abstract class AbstractGeoApi extends AbstractMetadataApi {

protected static final String LITTERAL_PARAMETER_DATE_WITH_HISTORY = ". Le paramètre '*' permet de renvoyer tout l'historique.";

protected CsvGeoUtils csvGeoUtils = new CsvGeoUtils();

private static Logger logger = LogManager.getLogger(AbstractGeoApi.class);
private static final Logger logger = LogManager.getLogger(AbstractGeoApi.class);

protected AbstractGeoApi(){}

protected AbstractGeoApi(SparqlUtils sparqlUtils, CSVUtils csvUtils, ResponseUtils responseUtils) {
super(sparqlUtils, csvUtils, responseUtils);
}

// Method to find a territoire object
protected Response generateResponseATerritoireByCode(String csvResult, String header, Territoire territoire) {
Expand Down Expand Up @@ -88,7 +89,7 @@ protected String formatValidParameterFiltreIfIsNull(String filtreNom) {
}

protected Boolean formatValidParameterBooleanIfIsNull(Boolean bool) {
return (bool != null) ? bool : false;
return (bool != null) && bool;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import fr.insee.rmes.modeles.geo.territoires.CantonsEtVilles;
import fr.insee.rmes.modeles.geo.territoires.Territoires;
import fr.insee.rmes.queries.geo.GeoQueries;
import fr.insee.rmes.utils.CSVUtils;
import fr.insee.rmes.utils.Constants;
import fr.insee.rmes.utils.ResponseUtils;
import fr.insee.rmes.utils.SparqlUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
Expand Down Expand Up @@ -38,7 +41,13 @@ public class CantonOuVilleApi extends AbstractGeoApi {
"Filtre pour renvoyer le canton-ou-ville actif à la date donnée. Par défaut, c’est la date courante. (Format : 'AAAA-MM-JJ')";
private static final String LITTERAL_DATE_EXAMPLE = "2016-01-01";

protected CantonOuVilleApi(SparqlUtils sparqlUtils, CSVUtils csvUtils, ResponseUtils responseUtils) {
super(sparqlUtils, csvUtils, responseUtils);
}

public CantonOuVilleApi() {
// Constructeur par défaut
}

@Path(ConstGeoApi.PATH_CANTON_OU_VILLE + CODE_PATTERN)
@GET
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/fr/insee/rmes/config/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ public static String getBaseHost() {
return baseHost;
}

public static void setBaseHost(String baseHost) {
Configuration.baseHost = baseHost;
}

public static String getFileStorage() {
return fileStorage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

public class Intercommunalite extends Territoire {

@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@XmlElement(name="IntituleComplet")
private String intituleComplet = null;

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/fr/insee/rmes/utils/JavaLangUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fr.insee.rmes.utils;

import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Stream;

public class JavaLangUtils {

private JavaLangUtils(){}

public static <S,T, R> Stream<R> merge(Stream<S> stream, List<T> list, BiFunction<S,T,R> merger) {
return stream.flatMap(s->list.stream().map(t->merger.apply(s,t)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ a ?typeRDF;
:nomSansArticle ?intituleSansArticle .
FILTER(?typeRelation = :territoireCommunAvec || ?typeRelation = :subdivisionDirecteDe)
OPTIONAL {BIND(IF(?typeRelation = :territoireCommunAvec, 'partielle', 'totale') AS ?inclusion)}
BIND(STR(?typeRDF) AS ?type).
BIND(STRAFTER(STR(?typeRDF),"http://rdf.insee.fr/def/geo#") AS ?type).
BIND(STR(?ressource) AS ?uri).
OPTIONAL {?canton (^:creation/:date) ?dateCreationCanton. }
OPTIONAL {?canton (^:suppression/:date) ?dateSuppressionCanton. }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ WHERE {
igeo:codeArticle ?typeArticle;
igeo:nom ?intitule;
igeo:nomSansArticle ?intituleSansArticle .
OPTIONAL {
<#if typeOrigine == "Intercommunalite">
OPTIONAL {
?uri a igeo:${typeOrigine} ;
igeo:nomIntercommunalite ?intituleComplet .
}
</#if>
Optional {
?uri a igeo:${typeOrigine} ;
igeo:nomIntercommunalite ?intituleComplet ;
insee:categorieJuridique ?cj.
?cj skos:prefLabel ?categorieJuridique.
}

OPTIONAL {
?evenementCreation igeo:creation ?uri ;
igeo:date ?dateCreation .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@ WHERE {
igeo:codeArticle ?typeArticle;
igeo:nom ?intitule;
igeo:nomSansArticle ?intituleSansArticle .
OPTIONAL {
?uri a igeo:${typeOrigine} ;
igeo:nomIntercommunalite ?intituleComplet ;
insee:categorieJuridique ?cj.
?cj skos:prefLabel ?categorieJuridique.
}
<#if typeOrigine == "Intercommunalite">
OPTIONAL {
?uri a igeo:${typeOrigine} ;
igeo:nomIntercommunalite ?intituleComplet .
}
</#if>
Optional {
?uri a igeo:${typeOrigine} ;
insee:categorieJuridique ?cj.
?cj skos:prefLabel ?categorieJuridique.
}
OPTIONAL {
?evenementCreation igeo:creation ?uri ;
igeo:date ?dateCreation .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ WHERE {
</#if>
igeo:nom ?intitule ;
igeo:nomSansArticle ?intituleSansArticle .
<#if territoire == "Intercommunalite" >
<#if territoire == "Intercommunalite">


OPTIONAL {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/rmes-api.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ fr.insee.rmes.api.log.configuration = log4j2.xml

# database
fr.insee.rmes.api.sparqlEndpoint = XXXXX
fr.insee.rmes.api.baseHost = XXXXX
fr.insee.rmes.api.baseHost = http://id.insee.fr

# files storage
# physical place
Expand Down
1 change: 0 additions & 1 deletion src/test/java/fr/insee/rmes/api/AbstractApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public abstract class AbstractApiTest {
public void mockUtilsMethodsThenReturnListOfPojo(boolean isAttendResponseIsOk) {
this.mockMethodsUtilsGeneric(isAttendResponseIsOk);
when(mockCSVUtils.populateMultiPOJO(Mockito.anyString(), Mockito.any())).thenReturn(list);

}

public void mockUtilsMethodsThenReturnOnePojo(Object o, boolean isAttendResponseIsOk) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,42 @@

import fr.insee.rmes.api.geo.territoire.CantonAPI;
import fr.insee.rmes.utils.SparqlUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import java.util.stream.Stream;

import static fr.insee.rmes.api.geo.pseudointegrationtest.ConstantForIntegration.assertEqualsJson;
import static fr.insee.rmes.api.geo.pseudointegrationtest.ConstantForIntegration.assertEqualsXml;
import static fr.insee.rmes.api.geo.pseudointegrationtest.ConstantForIntegration.*;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.APPLICATION_XML;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;


@ExtendWith(MockitoExtension.class)
class CantonApiIntegrationTest {

private static final String NO_DATE = null;
@InjectMocks
private CantonAPI geoApi;
private final static String CODE = "0105";
private final static String CODE = "5923";

private final static String CODE_COMMUNES_FROM_CANTON = "3319";
private static final String DATE1991 = "1991-12-19";
private static final String DATE2000 = "2000-02-27";

private static final String INCORRECT_DATE="*";

@Mock
protected SparqlUtils mockSparqlUtils;
Expand All @@ -34,36 +46,41 @@ class CantonApiIntegrationTest {
void givengetCanton_whenCorrectRequest_With_JSON_Header_thenResponseIsOk() {
when(mockSparqlUtils.executeSparqlQuery(anyString()))
.thenReturn(ConstantForIntegration.CANTON_MOCK_SERVER_RETURN_GET);
Response response = geoApi.getByCode(CODE, MediaType.APPLICATION_JSON, null);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
Response response = geoApi.getByCode(CODE, APPLICATION_JSON, null);
assertEquals(Status.OK.getStatusCode(), response.getStatus());
assertEqualsJson(ConstantForIntegration.CANTON_EXPECTED_RESPONSE_GET_JSON, response.getEntity());
}

@Test
void givengetCanton_whenCorrectRequest_With_XML_Header_thenResponseIsOk() {
when(mockSparqlUtils.executeSparqlQuery(anyString()))
.thenReturn(ConstantForIntegration.CANTON_MOCK_SERVER_RETURN_GET);
Response response = geoApi.getByCode(CODE, MediaType.APPLICATION_XML, null);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
assertEqualsXml(ConstantForIntegration.CANTON_EXPECTED_RESPONSE_GET_XML, response.getEntity());
}

@Test
void givenGetCommunesFromCanton_whenCorrectRequest_With_JSON_Header_thenResponseIsOk() {
when(mockSparqlUtils.executeSparqlQuery(anyString()))
.thenReturn(ConstantForIntegration.COMMUNES_FROM_CANTON_MOCK_SERVER_RETURN_GET);
Response response = geoApi.getCommunes(CODE_COMMUNES_FROM_CANTON, MediaType.APPLICATION_JSON, null);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
assertEqualsJson(ConstantForIntegration.COMMUNES_FROM_CANTON_EXPECTED_RESPONSE_GET_JSON, response.getEntity());
@ParameterizedTest(name = "{0} - {2} : {4}")
@MethodSource("argumentProvider")
void test_getCommunes(String titre, String sparqlResult, String media, String date, Status status, String expected) {
lenient().when(mockSparqlUtils.executeSparqlQuery(anyString())).thenReturn(sparqlResult);
Response response = geoApi.getCommunes(CODE, media, date);
Assertions.assertEquals(status.getStatusCode(), response.getStatus());
if (Status.OK==status){
switch (media){
case APPLICATION_XML:
assertEqualsXml(expected, response.getEntity() );
break;
case APPLICATION_JSON:
assertEqualsJson(expected, response.getEntity());
break;
}
}
}

@Test
void givenGetCommunesFromCanton_whenCorrectRequest_With_XML_Header_thenResponseIsOk() {
when(mockSparqlUtils.executeSparqlQuery(anyString()))
.thenReturn(ConstantForIntegration.COMMUNES_FROM_CANTON_MOCK_SERVER_RETURN_GET);
Response response = geoApi.getCommunes(CODE_COMMUNES_FROM_CANTON, MediaType.APPLICATION_XML, null);
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
assertEqualsXml(ConstantForIntegration.COMMUNES_FROM_CANTON_EXPECTED_RESPONSE_GET_XML, response.getEntity());
static Stream<Arguments> argumentProvider() {
return Stream.of(
//TODO fix sparql request pb and enable this test
//arguments("Communes de Lille-1 au "+DATE1991,COMMUNES_INCLUSES_DATEES1991_MOCK_SPARQL_RESPONSE, APPLICATION_JSON, DATE1991, Status.OK, COMMUNES_INCLUSES_DATEES1991_RESPONSE_GET_JSON),
//TODO fix sparql request pb and enable this test
//arguments("Communes de Lille-1 au "+DATE2000,COMMUNES_INCLUSES_DATEES2000_MOCK_SPARQL_RESPONSE, APPLICATION_JSON, DATE2000, Status.OK, COMMUNES_INCLUSES_DATEES2000_RESPONSE_GET_JSON),
arguments("Mauvaise date", COMMUNES_INCLUSES_MOCK_SPARQL_RESPONSE, APPLICATION_JSON, INCORRECT_DATE, Status.BAD_REQUEST, null),
arguments("Mauvaise date", COMMUNES_INCLUSES_MOCK_SPARQL_RESPONSE, APPLICATION_XML, INCORRECT_DATE, Status.BAD_REQUEST, null),
//TODO fix order of output xml : actual is "Intitule, Type, DateCreation, IntituleSansArticle, Inclusion" but should be "Type, Intitule, IntituleSansArticle, Inclusion, DateCreation"
//arguments("Communes de Lille-1 ", COMMUNES_INCLUSES_MOCK_SPARQL_RESPONSE, APPLICATION_XML, NO_DATE, Status.OK, COMMUNES_INCLUSES_RESPONSE_GET_XML),
arguments("Communes de Lille-1 ", COMMUNES_INCLUSES_MOCK_SPARQL_RESPONSE, APPLICATION_JSON, NO_DATE, Status.OK, COMMUNES_INCLUSES_EXPECTED_RESPONSE_GET_JSON)
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ void givengetPrecedentsCommunes_whenCorrectRequest_With_XML_Header_thenResponseI
@Test
void givengetSuivantsCommunes_whenCorrectRequest_With_JSON_Header_thenResponseIsOk() {
when(mockSparqlUtils.executeSparqlQuery(anyString()))
.thenReturn(ConstantForIntegration.COMMUNE_MOCK_SERVER_RETURN_PRECEDENTS);
.thenReturn(ConstantForIntegration.COMMUNE_MOCK_SERVER_RETURN_SUIVANTS);
Response response = geoApi.getSuivant(CODE_SUIVANT, MediaType.APPLICATION_JSON, "1973-01-01");
assertEquals(Status.OK.getStatusCode(), response.getStatus());
assertEqualsJson(ConstantForIntegration.COMMUNE_EXPECTED_RESPONSE_PRECEDENTS_JSON, response.getEntity());
assertEqualsJson(ConstantForIntegration.COMMUNE_EXPECTED_RESPONSE_SUIVANTS_JSON, response.getEntity());
}

@Test
Expand Down
Loading

0 comments on commit b0dcf41

Please sign in to comment.