diff --git a/src/main/java/fr/insee/rmes/modelSwagger/dataset/DataSetModelSwagger.java b/src/main/java/fr/insee/rmes/modelSwagger/dataset/DataSetModelSwagger.java index de5d909c8..e69119ef1 100644 --- a/src/main/java/fr/insee/rmes/modelSwagger/dataset/DataSetModelSwagger.java +++ b/src/main/java/fr/insee/rmes/modelSwagger/dataset/DataSetModelSwagger.java @@ -196,4 +196,15 @@ public DataSetModelSwagger(Id id, List title, Uri uri, Modified mod this.validationState = validationState; this.themeModelSwaggerS = themeModelSwaggerS; } + + public DataSetModelSwagger(Id id, List title, Uri uri, String validationState, CatalogRecordCreated catalogRecordCreated, CatalogRecordModified catalogRecordModified, CatalogRecordCreator catalogRecordCreator, CatalogRecordContributor catalogRecordContributor) { + this.id = id.toString(); + this.title = title; + this.uri = uri.toString(); + this.validationState = validationState; + this.catalogRecordContributor = catalogRecordContributor; + this.catalogRecordCreator = catalogRecordCreator; + this.catalogRecordCreated = catalogRecordCreated; + this.catalogRecordModified = catalogRecordModified; + } } diff --git a/src/main/java/fr/insee/rmes/services/datasets/DataSetsImpl.java b/src/main/java/fr/insee/rmes/services/datasets/DataSetsImpl.java index 2c987fe18..61185df0a 100644 --- a/src/main/java/fr/insee/rmes/services/datasets/DataSetsImpl.java +++ b/src/main/java/fr/insee/rmes/services/datasets/DataSetsImpl.java @@ -150,13 +150,22 @@ protected DataSetModelSwagger findDataSetModelSwagger(String id) throws RmesExce List title = constructLangContent(catalogue_result.getString("titleLg1"), catalogue_result.getString("titleLg2")); Id id1=new Id(catalogue_result.getString("id")); Uri uri = new Uri(catalogue_result.getString("uri")); - DisseminationStatus disseminationStatus = new DisseminationStatus(ontologies_result.getString("labeldisseminationStatusLg1")); + CatalogRecordCreated catalogRecordCreated = new CatalogRecordCreated(catalogue_result.getString("catalogRecordCreated")); CatalogRecordModified catalogRecordModified = new CatalogRecordModified(catalogue_result.getString("catalogRecordModified")); CatalogRecordCreator catalogRecordCreator = new CatalogRecordCreator(catalogue_result.getString("catalogRecordCreator")); CatalogRecordContributor catalogRecordContributor = new CatalogRecordContributor(catalogue_result.getString("catalogRecordContributor")); String validationState = catalogue_result.getString("statutValidation"); - DataSetModelSwagger response = new DataSetModelSwagger(id1, title, uri, validationState, disseminationStatus, catalogRecordCreated,catalogRecordModified,catalogRecordCreator,catalogRecordContributor); + + DisseminationStatus disseminationStatus = null; + DataSetModelSwagger response = null; + if (ontologies_result.has("labeldisseminationStatusLg1")){ + disseminationStatus = new DisseminationStatus(ontologies_result.getString("labeldisseminationStatusLg1")); + response = new DataSetModelSwagger(id1, title, uri, validationState, disseminationStatus, catalogRecordCreated, catalogRecordModified, catalogRecordCreator, catalogRecordContributor); + } else { + response = new DataSetModelSwagger(id1, title, uri, validationState, catalogRecordCreated, catalogRecordModified, catalogRecordCreator, catalogRecordContributor); + } + testPresenceVariablePuisAjout(response,catalogue_result,adms_result,codes_result,organisations_result,structures_result); return response; } else { @@ -522,10 +531,14 @@ private List getWasGeneratedBy(List operationStat) throws RmesE params.put("URI", s.replace(" ", "")); JSONObject wasGeneratedByQuery = repoGestion.getResponseAsObject(buildRequest(Constants.DATASETS_QUERIES_PATH+DATASET_BY_ID_PATH, "getDataSetByIdWasGeneratedBy.ftlh", params)); - List wasGeneratedByTitles = constructLangContent(wasGeneratedByQuery.getString("labelwasGeneratedByLg1"),wasGeneratedByQuery.getString("labelwasGeneratedByLg2")); - IdLabel wasGeneratedByIdLabel = new IdLabel(wasGeneratedByQuery.getString("wasGeneratedById"),wasGeneratedByTitles); - wasGeneratedByIdLabel.setType(wasGeneratedByQuery.getString("typeWasGeneratedBy")); - wasGeneratedBy.add(wasGeneratedByIdLabel); + List wasGeneratedByTitles = null; + IdLabel wasGeneratedByIdLabel = null; + if (wasGeneratedByQuery.has("wasGeneratedById")) { + wasGeneratedByTitles = constructLangContent(wasGeneratedByQuery.getString("labelwasGeneratedByLg1"), wasGeneratedByQuery.getString("labelwasGeneratedByLg2")); + wasGeneratedByIdLabel = new IdLabel(wasGeneratedByQuery.getString("wasGeneratedById"), wasGeneratedByTitles); + wasGeneratedByIdLabel.setType(wasGeneratedByQuery.getString("typeWasGeneratedBy")); + wasGeneratedBy.add(wasGeneratedByIdLabel); + } } return wasGeneratedBy; } diff --git a/src/main/resources/queries/dataSets/getDatasetById/getDataSetByIDSummary.ftlh b/src/main/resources/queries/dataSets/getDatasetById/getDataSetByIDSummary.ftlh index 161eead31..d3e4b213e 100644 --- a/src/main/resources/queries/dataSets/getDatasetById/getDataSetByIDSummary.ftlh +++ b/src/main/resources/queries/dataSets/getDatasetById/getDataSetByIDSummary.ftlh @@ -2,9 +2,13 @@ select distinct ?id ?dateModification FROM where { - ?uri a dcat:Dataset ; - dcterms:identifier "${ID}" ; - dcterms:identifier ?id ; - OPTIONAL {?uri dcterms:modified ?dateModification . } + ?uri a dcat:Dataset ; + dcterms:identifier "${ID}" ; + dcterms:identifier ?id ; + OPTIONAL { + ?record a dcat:CatalogRecord ; + foaf:primaryTopic ?uri ; + dcterms:modified ?dateModification . + } -} group by ?id ?dateModification \ No newline at end of file +} \ No newline at end of file diff --git a/src/test/java/fr/insee/rmes/modelSwagger/dataset/DataSetModelSwaggerTest.java b/src/test/java/fr/insee/rmes/modelSwagger/dataset/DataSetModelSwaggerTest.java index 08bac2df1..e71d343a1 100644 --- a/src/test/java/fr/insee/rmes/modelSwagger/dataset/DataSetModelSwaggerTest.java +++ b/src/test/java/fr/insee/rmes/modelSwagger/dataset/DataSetModelSwaggerTest.java @@ -3,14 +3,13 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.json.JsonMapper; -import fr.insee.rmes.model.datasets.CatalogRecordContributor; -import fr.insee.rmes.model.datasets.CatalogRecordCreated; -import fr.insee.rmes.model.datasets.CatalogRecordCreator; -import fr.insee.rmes.model.datasets.CatalogRecordModified; +import fr.insee.rmes.model.datasets.*; import org.json.JSONException; import org.json.JSONObject; import org.junit.jupiter.api.Test; +import java.util.List; + import static org.assertj.core.api.Assertions.assertThat; class DataSetModelSwaggerTest { @@ -34,4 +33,53 @@ void verify_jacksonAnnotation_test() throws JsonProcessingException, JSONExcepti "\"catalogRecordModified\":\"modified\""); } + @Test + void constructor_withIdTitleUriValidationStateAndCatalogFields_shouldSetAllFields() { + Id id = new Id("jd1000"); + Uri uri = new Uri("http://bauhaus/catalogues/jeuDeDonnees/jd1000"); + List title = List.of(new LangContent("fr", "Titre FR"), new LangContent("en", "Title EN")); + String validationState = "Published"; + CatalogRecordCreated created = new CatalogRecordCreated("2024-01-01T00:00:00"); + CatalogRecordModified modified = new CatalogRecordModified("2024-06-15T10:00:00"); + CatalogRecordCreator creator = new CatalogRecordCreator("creator1"); + CatalogRecordContributor contributor = new CatalogRecordContributor("contributor1"); + + DataSetModelSwagger result = new DataSetModelSwagger(id, title, uri, validationState, created, modified, creator, contributor); + + assertThat(result.getId()).isEqualTo("jd1000"); + assertThat(result.getUri()).isEqualTo("http://bauhaus/catalogues/jeuDeDonnees/jd1000"); + assertThat(result.getTitle()).isEqualTo(title); + assertThat(result.getValidationState()).isEqualTo("Published"); + assertThat(result.getCatalogRecordCreated()).isEqualTo(created); + assertThat(result.getCatalogRecordModified()).isEqualTo(modified); + assertThat(result.getCatalogRecordCreator()).isEqualTo(creator); + assertThat(result.getCatalogRecordContributor()).isEqualTo(contributor); + } + + @Test + void constructor_withIdTitleUriValidationStateAndCatalogFields_shouldSerializeToJson() throws JsonProcessingException, JSONException { + Id id = new Id("jd1000"); + Uri uri = new Uri("http://bauhaus/catalogues/jeuDeDonnees/jd1000"); + List title = List.of(new LangContent("fr", "Titre FR"), new LangContent("en", "Title EN")); + CatalogRecordCreated created = new CatalogRecordCreated("2024-01-01T00:00:00"); + CatalogRecordModified modified = new CatalogRecordModified("2024-06-15T10:00:00"); + CatalogRecordCreator creator = new CatalogRecordCreator("creator1"); + CatalogRecordContributor contributor = new CatalogRecordContributor("contributor1"); + + DataSetModelSwagger result = new DataSetModelSwagger(id, title, uri, "Published", created, modified, creator, contributor); + + ObjectMapper mapper = new JsonMapper(); + String json = mapper.writeValueAsString(result); + JSONObject jsonObject = new JSONObject(json); + + assertThat(jsonObject.getString("id")).isEqualTo("jd1000"); + assertThat(jsonObject.getString("uri")).isEqualTo("http://bauhaus/catalogues/jeuDeDonnees/jd1000"); + assertThat(jsonObject.getString("validationState")).isEqualTo("Published"); + assertThat(jsonObject.getString("catalogRecordCreated")).isEqualTo("2024-01-01T00:00:00"); + assertThat(jsonObject.getString("catalogRecordModified")).isEqualTo("2024-06-15T10:00:00"); + assertThat(jsonObject.getString("catalogRecordCreator")).isEqualTo("creator1"); + assertThat(jsonObject.getString("catalogRecordContributor")).isEqualTo("contributor1"); + assertThat(jsonObject.getJSONArray("title")).hasSize(2); + } + } \ No newline at end of file diff --git a/src/test/java/fr/insee/rmes/services/datasets/DataSetsImplTest.java b/src/test/java/fr/insee/rmes/services/datasets/DataSetsImplTest.java index d63230717..8351e5e3a 100644 --- a/src/test/java/fr/insee/rmes/services/datasets/DataSetsImplTest.java +++ b/src/test/java/fr/insee/rmes/services/datasets/DataSetsImplTest.java @@ -4,7 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.json.JsonMapper; import fr.insee.rmes.dto.datasets.PatchDatasetDTO; -import fr.insee.rmes.model.datasets.Theme; +import fr.insee.rmes.model.datasets.*; import fr.insee.rmes.modelSwagger.dataset.*; import fr.insee.rmes.persistence.RepositoryGestion; import fr.insee.rmes.services.utils.DataSetsUtilsTest; @@ -25,6 +25,8 @@ import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.HttpStatus; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.*; import java.util.function.Supplier; import java.util.stream.Stream; @@ -48,7 +50,7 @@ class DataSetsImplTest { void getListDataSetsTest() throws RmesException, JsonProcessingException { JSONArray mockJSON = new JSONArray(DataSetsUtilsTest.DATA_SET_LIST); when(repoGestion.getResponseAsArray(Mockito.anyString())).thenReturn(mockJSON); - assertThat(MAPPER.readTree(dataSetsImpl.getListDataSets(""))).isEqualTo(MAPPER.readTree(DataSetsUtilsTest.EXPECTED_GET_DATA_SET_LIST.toString())); + assertThat(MAPPER.readTree(dataSetsImpl.getListDataSets(""))).isEqualTo(MAPPER.readTree(DataSetsUtilsTest.EXPECTED_GET_DATA_SET_LIST)); } @Test @@ -171,9 +173,6 @@ void getThemeModelSwaggerSTest() { } @ParameterizedTest(name = "{0}") - /*@CsvSource({ - "subtitleLg1, subtitleLg2" - })*/ @MethodSource(value = "argumentsProvider") void testPresenceVariablePuisAjoutTest_checkFieldIsAdded(String key1, String key2, Supplier> getListLangContent) throws RmesException, JsonProcessingException { var catalogue_result = new JSONObject(Map.of(key1, "l1", key2, "l2")); @@ -220,6 +219,22 @@ void getDataSetByIDDateMiseAJour_shouldReturn404IfInexistentId() throws RmesExce .hasMessageContaining("Non existent dataset identifier"); } + @Test + void getDataSetByIDSummary_shouldReturnIdUriAndModified_whenDataSetExists() throws RmesException, JsonProcessingException { + JSONObject mockJSON = new JSONObject(); + mockJSON.put("id", "jd1000"); + mockJSON.put("uri", "http://bauhaus/catalogues/jeuDeDonnees/jd1000"); + mockJSON.put("dateMiseAJour", "2024-06-15T10:00:00"); + when(repoGestion.getResponseAsObject(Mockito.anyString())).thenReturn(mockJSON); + + String result = dataSetsImpl.getDataSetByIDSummary("jd1000"); + + var resultNode = MAPPER.readTree(result); + assertThat(resultNode.get("id").asText()).isEqualTo("jd1000"); + assertThat(resultNode.get("uri").asText()).isEqualTo("http://bauhaus/catalogues/jeuDeDonnees/jd1000"); + assertThat(resultNode.get("modified").asText()).isEqualTo("2024-06-15T10:00:00"); + } + @Test void patchDataset_shouldReturn400() { @@ -247,9 +262,221 @@ void testConstructLangContentList(){ String expected_2 = "[LangContent(lang=lg2, content=contentLg2)]"; assertThat(actual_1).isEqualTo(expected_1); assertThat(actual_2).isEqualTo(expected_2); + } + + // ///////////////////////////////////// + // Tests for findDataSetModelSwagger // + // ///////////////////////////////////// + + + private void mockSubQueries(JSONObject adms, JSONObject codes, JSONObject ontologies, JSONObject organisations, JSONObject structures) throws RmesException { + when(repoGestion.getResponseAsObject("getDataSetById_catalogueAdms.ftlh")).thenReturn(adms); + when(repoGestion.getResponseAsObject("getDataSetById_catalogueCodes.ftlh")).thenReturn(codes); + when(repoGestion.getResponseAsObject("getDataSetById_catalogueOntologies.ftlh")).thenReturn(ontologies); + when(repoGestion.getResponseAsObject("getDataSetById_catalogueOrganisations.ftlh")).thenReturn(organisations); + when(repoGestion.getResponseAsObject("getDataSetById_catalogueStructures.ftlh")).thenReturn(structures); + } + + @Test + void findDataSetModelSwagger_shouldReturnMinimalDataSet_withoutDisseminationStatus() throws RmesException, JsonProcessingException { + var catalogueResult = new JSONObject(DataSetsUtilsTest.CATALOGUE_RESULT_MINIMAL); + JSONObject emptyJson = new JSONObject(); + + when(repoGestion.getResponseAsObject("getDataSetById_catalogue.ftlh")).thenReturn(catalogueResult); + mockSubQueries(emptyJson, emptyJson, emptyJson, emptyJson, emptyJson); + + DataSetModelSwagger result = dataSetsImpl.findDataSetModelSwagger("jd1000"); + + assertThat(result.getId()).isEqualTo("jd1000"); + assertThat(result.getUri()).isEqualTo("http://bauhaus/catalogues/jeuDeDonnees/jd1000"); + assertThat(result.getValidationState()).isEqualTo("Modified"); + assertThat(result.getTitle()).isEqualTo(dataSetsImpl.constructLangContent("Titre FR", "Title EN")); + assertThat(result.getCatalogRecordCreated()).isEqualTo(new CatalogRecordCreated("2024-01-01T00:00:00")); + assertThat(result.getCatalogRecordModified()).isEqualTo(new CatalogRecordModified("2024-01-02T00:00:00")); + assertThat(result.getCatalogRecordCreator()).isEqualTo(new CatalogRecordCreator("creator1")); + assertThat(result.getCatalogRecordContributor()).isEqualTo(new CatalogRecordContributor("contributor1")); + assertThat(result.getDisseminationStatus()).isNull(); + assertThat(result.getKeyword()).isEmpty(); + } + + @Test + void findDataSetModelSwagger_shouldReturnDataSet_withDisseminationStatus() throws RmesException, JsonProcessingException { + var catalogueResult = new JSONObject(DataSetsUtilsTest.CATALOGUE_RESULT_MINIMAL); + JSONObject emptyJson = new JSONObject(); + JSONObject ontologiesResult = new JSONObject(); + ontologiesResult.put("labeldisseminationStatusLg1", "Public"); + + when(repoGestion.getResponseAsObject("getDataSetById_catalogue.ftlh")).thenReturn(catalogueResult); + mockSubQueries(emptyJson, emptyJson, ontologiesResult, emptyJson, emptyJson); + + DataSetModelSwagger result = dataSetsImpl.findDataSetModelSwagger("jd1000"); + + assertThat(result.getId()).isEqualTo("jd1000"); + assertThat(result.getDisseminationStatus()).isEqualTo("Public"); + assertThat(result.getTitle()).isEqualTo(dataSetsImpl.constructLangContent("Titre FR", "Title EN")); + } + + @Test + void findDataSetModelSwagger_shouldSetOptionalFields_whenPresentInCatalogue() throws RmesException, JsonProcessingException { + var catalogueResult = new JSONObject(DataSetsUtilsTest.CATALOGUE_RESULT_MINIMAL); + catalogueResult.put("dateModification", "2024-06-15T10:00:00"); + catalogueResult.put("subtitleLg1", "Sous-titre FR"); + catalogueResult.put("subtitleLg2", "Subtitle EN"); + catalogueResult.put("abstractLg1", "Resume FR"); + catalogueResult.put("abstractLg2", "Abstract EN"); + catalogueResult.put("version", "1.0"); + catalogueResult.put("dateEmission", "2024-01-15"); + + JSONObject emptyJson = new JSONObject(); + + when(repoGestion.getResponseAsObject("getDataSetById_catalogue.ftlh")).thenReturn(catalogueResult); + mockSubQueries(emptyJson, emptyJson, emptyJson, emptyJson, emptyJson); + + DataSetModelSwagger result = dataSetsImpl.findDataSetModelSwagger("jd1000"); + + assertThat(result.getModified()).isEqualTo("2024-06-15T10:00:00"); + assertThat(result.getSubtitle()).isEqualTo(dataSetsImpl.constructLangContent("Sous-titre FR", "Subtitle EN")); + assertThat(result.getAbstractDataset()).isEqualTo(dataSetsImpl.constructLangContent("Resume FR", "Abstract EN")); + assertThat(result.getVersion()).isEqualTo("1.0"); + assertThat(result.getIssued()).isEqualTo("2024-01-15"); + } + + @Test + void findDataSetModelSwagger_shouldSetIdentifier_whenPresentInAdms() throws RmesException, JsonProcessingException { + var catalogueResult = new JSONObject(DataSetsUtilsTest.CATALOGUE_RESULT_MINIMAL); + JSONObject admsResult = new JSONObject(); + admsResult.put("identifier", "INSEE-JD1000"); + JSONObject emptyJson = new JSONObject(); + + when(repoGestion.getResponseAsObject("getDataSetById_catalogue.ftlh")).thenReturn(catalogueResult); + mockSubQueries(admsResult, emptyJson, emptyJson, emptyJson, emptyJson); + + DataSetModelSwagger result = dataSetsImpl.findDataSetModelSwagger("jd1000"); + + assertThat(result.getIdentifier()).isEqualTo("INSEE-JD1000"); + } + + @Test + void findDataSetModelSwagger_shouldSetPublisher_whenPresentInOrganisations() throws RmesException, JsonProcessingException { + var catalogueResult = new JSONObject(DataSetsUtilsTest.CATALOGUE_RESULT_MINIMAL); + JSONObject organisationsResult = new JSONObject(); + organisationsResult.put("idPublisher", "INSEE"); + organisationsResult.put("labelPublisherLg1", "Institut national de la statistique"); + organisationsResult.put("labelPublisherLg2", "National Institute of Statistics"); + JSONObject emptyJson = new JSONObject(); + + when(repoGestion.getResponseAsObject("getDataSetById_catalogue.ftlh")).thenReturn(catalogueResult); + mockSubQueries(emptyJson, emptyJson, emptyJson, organisationsResult, emptyJson); + + DataSetModelSwagger result = dataSetsImpl.findDataSetModelSwagger("jd1000"); + + assertThat(result.getPublisher()).isNotNull(); + assertThat(result.getPublisher().getId()).isEqualTo("INSEE"); + } + + @Test + void findDataSetModelSwagger_shouldSetTypeAndAccessRights_whenPresentInCodes() throws RmesException, JsonProcessingException { + var catalogueResult = new JSONObject(DataSetsUtilsTest.CATALOGUE_RESULT_MINIMAL); + JSONObject codesResult = new JSONObject(); + codesResult.put("labeltypeLg1", "Fichier detail"); + codesResult.put("labeltypeLg2", "Detail file"); + codesResult.put("labelaccessRightsLg1", "Acces libre"); + codesResult.put("labelaccessRightsLg2", "Open access"); + JSONObject emptyJson = new JSONObject(); + + when(repoGestion.getResponseAsObject("getDataSetById_catalogue.ftlh")).thenReturn(catalogueResult); + mockSubQueries(emptyJson, codesResult, emptyJson, emptyJson, emptyJson); + + DataSetModelSwagger result = dataSetsImpl.findDataSetModelSwagger("jd1000"); + + assertThat(result.getType()).isEqualTo(dataSetsImpl.constructLangContent("Fichier detail", "Detail file")); + assertThat(result.getAccessRights()).isEqualTo(dataSetsImpl.constructLangContent("Acces libre", "Open access")); + } + + @Test + void findDataSetModelSwagger_shouldSetTemporal_whenPresentInCatalogue() throws RmesException, JsonProcessingException { + var catalogueResult = new JSONObject(DataSetsUtilsTest.CATALOGUE_RESULT_MINIMAL); + catalogueResult.put("startPeriod", "2020-01-01"); + catalogueResult.put("endPeriod", "2023-12-31"); + JSONObject emptyJson = new JSONObject(); + + when(repoGestion.getResponseAsObject("getDataSetById_catalogue.ftlh")).thenReturn(catalogueResult); + mockSubQueries(emptyJson, emptyJson, emptyJson, emptyJson, emptyJson); + + DataSetModelSwagger result = dataSetsImpl.findDataSetModelSwagger("jd1000"); + + assertThat(result.getTemporal()).isNotNull(); + } + + @Test + void findDataSetModelSwagger_shouldSetStructure_whenDsdPresentInStructures() throws RmesException, JsonProcessingException { + var catalogueResult = new JSONObject(DataSetsUtilsTest.CATALOGUE_RESULT_MINIMAL); + JSONObject structuresResult = new JSONObject(); + structuresResult.put("uri", "http://structures/dsd1"); + structuresResult.put("structureId", "dsd1"); + structuresResult.put("dsd", "DSD_CHAMP"); + structuresResult.put("DataStructureDefinition", "true"); + JSONObject emptyJson = new JSONObject(); + + when(repoGestion.getResponseAsObject("getDataSetById_catalogue.ftlh")).thenReturn(catalogueResult); + mockSubQueries(emptyJson, emptyJson, emptyJson, emptyJson, structuresResult); + + DataSetModelSwagger result = dataSetsImpl.findDataSetModelSwagger("jd1000"); + + assertThat(result.getStructure()).isNotNull(); + } + + @Test + void findDataSetModelSwagger_shouldSetStructureFromUri_whenNoDsd() throws RmesException, JsonProcessingException { + var catalogueResult = new JSONObject(DataSetsUtilsTest.CATALOGUE_RESULT_MINIMAL); + JSONObject structuresResult = new JSONObject(); + structuresResult.put("uri", "http://structures/struct1"); + JSONObject emptyJson = new JSONObject(); + + when(repoGestion.getResponseAsObject("getDataSetById_catalogue.ftlh")).thenReturn(catalogueResult); + mockSubQueries(emptyJson, emptyJson, emptyJson, emptyJson, structuresResult); + DataSetModelSwagger result = dataSetsImpl.findDataSetModelSwagger("jd1000"); + assertThat(result.getStructure()).isNotNull(); } + @Test + void getStatisticalUnit_shouldReturnIdLabelsForEachUri() throws Exception { + JSONObject response1 = new JSONObject(); + response1.put("labelUriStatisticalLg1", "Ménages"); + response1.put("labelUriStatisticalLg2", "Households"); + + JSONObject response2 = new JSONObject(); + response2.put("labelUriStatisticalLg1", "Individus"); + response2.put("labelUriStatisticalLg2", "Individuals"); + + when(repoGestion.getResponseAsObject(Mockito.anyString())) + .thenReturn(response1) + .thenReturn(response2); + + Method method = DataSetsImpl.class.getDeclaredMethod("getStatisticalUnit", List.class, String.class); + method.setAccessible(true); + + @SuppressWarnings("unchecked") + List result = (List) method.invoke(dataSetsImpl, List.of("http://uri1", "http://uri2"), "jd1000"); + + assertThat(result).hasSize(2); + assertThat(result.get(0).getId()).isEqualTo("http://uri1"); + assertThat(result.get(0).getLabel()).isEqualTo(dataSetsImpl.constructLangContent("Ménages", "Households")); + assertThat(result.get(1).getId()).isEqualTo("http://uri2"); + assertThat(result.get(1).getLabel()).isEqualTo(dataSetsImpl.constructLangContent("Individus", "Individuals")); + } + + @Test + void getStatisticalUnit_shouldReturnEmptyList_whenNoUris() throws Exception { + Method method = DataSetsImpl.class.getDeclaredMethod("getStatisticalUnit", List.class, String.class); + method.setAccessible(true); + + @SuppressWarnings("unchecked") + List result = (List) method.invoke(dataSetsImpl, List.of(), "jd1000"); + + assertThat(result).isEmpty(); + } } \ No newline at end of file diff --git a/src/test/java/fr/insee/rmes/services/utils/DataSetsUtilsTest.java b/src/test/java/fr/insee/rmes/services/utils/DataSetsUtilsTest.java index 1c225b976..46511cdfe 100644 --- a/src/test/java/fr/insee/rmes/services/utils/DataSetsUtilsTest.java +++ b/src/test/java/fr/insee/rmes/services/utils/DataSetsUtilsTest.java @@ -6,7 +6,6 @@ public class DataSetsUtilsTest { public static final String EXPECTED_THEMELIST_GET_THEME_MODEL_SWAGGERS = "[ThemeModelSwagger(uri=uri0, labelDataSet=[LabelDataSet(lang=fr, content=labelThemeLg10), LabelDataSet(lang=en, content=labelThemeLg20)], themeTaxonomy=themeTaxonomy0), ThemeModelSwagger(uri=uri1, labelDataSet=[LabelDataSet(lang=fr, content=labelThemeLg11), LabelDataSet(lang=en, content=labelThemeLg21)], themeTaxonomy=themeTaxonomy1), ThemeModelSwagger(uri=uri2, labelDataSet=[LabelDataSet(lang=fr, content=labelThemeLg12), LabelDataSet(lang=en, content=labelThemeLg22)], themeTaxonomy=themeTaxonomy2), ThemeModelSwagger(uri=uri3, labelDataSet=[LabelDataSet(lang=fr, content=labelThemeLg13), LabelDataSet(lang=en, content=labelThemeLg23)], themeTaxonomy=themeTaxonomy3)]"; public static final String EXPECTED_LANGCONTENTLIST_SET_ID_LABEL = "IdLabel(id=id, label=[LangContent(lang=fr, content=content1), LangContent(lang=en, content=content2)], type=null)"; public static final String EXPECTED_LANGCONTENTLIST_SET_ID_LABEL_WITH_TYPE = "IdLabel(id=id, label=[LangContent(lang=fr, content=content1), LangContent(lang=en, content=content2)], type=type)"; - public static final String URI_TEST = "http://uri.test"; public static final String DATA_SET_LIST= """ [ { @@ -70,4 +69,6 @@ public class DataSetsUtilsTest { public static final String CATALOGUE_RESULT_RELATIONS = "{\"relations\":\"http://www.insee.fr,http://www.rdf.insee.fr\"}"; public static final String EXPECTED_RELATIONS = "[\"http://www.insee.fr\", \"http://www.rdf.insee.fr\"]"; + public static final String CATALOGUE_RESULT_MINIMAL = "{\"id\":\"jd1000\", \"titleLg1\":\"Titre FR\", \"titleLg2\":\"Title EN\", \"uri\":\"http://bauhaus/catalogues/jeuDeDonnees/jd1000\", \"catalogRecordCreated\":\"2024-01-01T00:00:00\", \"catalogRecordModified\":\"2024-01-02T00:00:00\", \"catalogRecordCreator\":\"creator1\", \"catalogRecordContributor\":\"contributor1\", \"statutValidation\":\"Modified\"}"; + }