From eddbbfc5af305cf070cc307fddae31e6f6da8dcd Mon Sep 17 00:00:00 2001 From: Fabrice Bibonne Date: Wed, 29 Mar 2023 09:10:22 +0200 Subject: [PATCH 1/2] feat(test getStructure) add tests for the methods of StructuresImpl called by getStructure --- .../persistence/RepositoryGestionTest.java | 14 +++++ ...ucturesImplIntegrationIntegrationTest.java | 52 +++++++++++++++++++ .../stub/StructuresImplMockBuildRequest.java | 26 ++++++++++ src/test/resources/application-test.yml | 8 +++ 4 files changed, 100 insertions(+) create mode 100644 src/test/java/fr/insee/rmes/persistence/RepositoryGestionTest.java create mode 100644 src/test/java/fr/insee/rmes/services/StructuresImplIntegrationIntegrationTest.java create mode 100644 src/test/java/fr/insee/rmes/services/structures/stub/StructuresImplMockBuildRequest.java create mode 100644 src/test/resources/application-test.yml diff --git a/src/test/java/fr/insee/rmes/persistence/RepositoryGestionTest.java b/src/test/java/fr/insee/rmes/persistence/RepositoryGestionTest.java new file mode 100644 index 00000000..018cae47 --- /dev/null +++ b/src/test/java/fr/insee/rmes/persistence/RepositoryGestionTest.java @@ -0,0 +1,14 @@ +package fr.insee.rmes.persistence; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class RepositoryGestionTest { + + @Test + void getResponseAsArrayTest(){ + + } + +} diff --git a/src/test/java/fr/insee/rmes/services/StructuresImplIntegrationIntegrationTest.java b/src/test/java/fr/insee/rmes/services/StructuresImplIntegrationIntegrationTest.java new file mode 100644 index 00000000..61326343 --- /dev/null +++ b/src/test/java/fr/insee/rmes/services/StructuresImplIntegrationIntegrationTest.java @@ -0,0 +1,52 @@ +package fr.insee.rmes.services; + +import com.fasterxml.jackson.core.JsonProcessingException; +import fr.insee.rmes.persistence.RepositoryGestion; +import fr.insee.rmes.services.structures.StructuresImpl; +import fr.insee.rmes.services.structures.stub.StructuresImplMockBuildRequest; +import fr.insee.rmes.utils.config.Config; +import fr.insee.rmes.utils.exceptions.RmesException; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.json.JsonTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; + +import static org.skyscreamer.jsonassert.JSONAssert.assertEquals; + +@JsonTest(properties = {"spring.profiles.active=test"}) +@Import({Config.class, StructuresImplMockBuildRequest.class, RepositoryGestion.class}) +class StructuresImplIntegrationIntegrationTest { + + @Autowired + StructuresImpl structuresImplMockBuildRequest; + + @MockBean + RepositoryGestion repositoryGestion; + + @Test + void getStructureTest() throws RmesException, JsonProcessingException { + + Mockito.when(repositoryGestion.getResponseAsArray("getStructure.ftlh")).thenReturn(new JSONArray("[{\"dateMiseAJour\":\"2023-02-10T14:37:14.691014\",\"dateCreation\":\"2022-10-28T13:53:59.228157\",\"prefLabelLg1\":\"set\",\"notation\":\"est\",\"prefLabelLg2\":\"set\",\"statutValidation\":\"Unpublished\",\"id\":\"dsd1001\",\"uri\":\"http://bauhaus/structuresDeDonnees/structure/dsd1001\"}]")); + Mockito.when(repositoryGestion.getResponseAsArray("getStructureComponents.ftlh")).thenReturn(new JSONArray("[{\"ordre\":\"1\",\"id\":\"d1001\"}]")); + Mockito.when(repositoryGestion.getResponseAsObject("getComponent.ftlh")).thenReturn(new JSONObject("{\"dateMiseAJour\":\"2022-10-28T13:53:24.204248\",\"dateCreation\":\"2022-10-28T13:53:24.204248\",\"prefLabelLg1\":\"test_list_part-fr\",\"notation\":\"test_list_part\",\"prefLabelLg2\":\"test_list_part-en\",\"statutValidation\":\"Unpublished\",\"id\":\"d1001\",\"type\":\"Dimension\",\"uri\":\"http://bauhaus/structuresDeDonnees/composants/dimension/d1001\",\"representation\":\"http://bauhaus/codes/concept/CodeTestSuppressionCasAvecListePartielle\"}")); + Mockito.when(repositoryGestion.getResponseAsArray("getComponentChildren.ftlh")).thenReturn(new JSONArray("[]")); + + String expected = "{\"dateMiseAJour\":\"2023-02-10T14:37:14.691014\",\"dateCreation\":\"2022-10-28T13:53:59.228157\",\"notation\":\"est\",\"statutValidation\":\"Provisoire, jamais publiée\",\"mesures\":[],\"id\":\"dsd1001\",\"label\":[{\"langue\":\"fr\",\"contenu\":\"set\"},{\"langue\":\"en\",\"contenu\":\"set\"}],\"uri\":\"http://bauhaus/structuresDeDonnees/structure/dsd1001\",\"attributs\":[],\"dimensions\":[{\"ordre\":\"1\",\"notation\":\"test_list_part\",\"label\":[{\"langue\":\"fr\",\"contenu\":\"test_list_part-fr\"},{\"langue\":\"en\",\"contenu\":\"test_list_part-en\"}],\"uri\":\"http://bauhaus/structuresDeDonnees/composants/dimension/d1001\",\"representation\":\"http://bauhaus/codes/concept/CodeTestSuppressionCasAvecListePartielle\",\"dateMiseAJour\":\"2022-10-28T13:53:24.204248\",\"dateCreation\":\"2022-10-28T13:53:24.204248\",\"statutValidation\":\"Provisoire, jamais publiée\",\"id\":\"d1001\",\"type\":\"Dimension\"}]}"; + assertEquals(expected,structuresImplMockBuildRequest.getStructure("dsd1001"),true); + } + + + @Test + void getStructureComponentsTest(){ + String structure="{\"dateMiseAJour\":\"2023-02-10T14:37:14.691014\",\"dateCreation\":\"2022-10-28T13:53:59.228157\",\"notation\":\"est\",\"statutValidation\":\"Provisoire, jamais publiée\",\"id\":\"dsd1001\",\"label\":[{\"langue\":\"fr\",\"contenu\":\"set\"},{\"langue\":\"en\",\"contenu\":\"set\"}],\"uri\":\"http://bauhaus/structuresDeDonnees/structure/dsd1001\"}"; + String id="dsd1001"; + String result="{\"dateMiseAJour\":\"2023-02-10T14:37:14.691014\",\"dateCreation\":\"2022-10-28T13:53:59.228157\",\"notation\":\"est\",\"statutValidation\":\"Provisoire, jamais publiée\",\"mesures\":[],\"id\":\"dsd1001\",\"label\":[{\"langue\":\"fr\",\"contenu\":\"set\"},{\"langue\":\"en\",\"contenu\":\"set\"}],\"uri\":\"http://bauhaus/structuresDeDonnees/structure/dsd1001\",\"attributs\":[],\"dimensions\":[{\"dateMiseAJour\":\"2022-10-28T13:53:24.204248\",\"dateCreation\":\"2022-10-28T13:53:24.204248\",\"ordre\":\"1\",\"notation\":\"test_list_part\",\"statutValidation\":\"Provisoire, jamais publiée\",\"id\":\"d1001\",\"label\":[{\"langue\":\"fr\",\"contenu\":\"test_list_part-fr\"},{\"langue\":\"en\",\"contenu\":\"test_list_part-en\"}],\"type\":\"Dimension\",\"uri\":\"http://bauhaus/structuresDeDonnees/composants/dimension/d1001\",\"representation\":\"http://bauhaus/codes/concept/CodeTestSuppressionCasAvecListePartielle\"}]}"; + } + + + +} diff --git a/src/test/java/fr/insee/rmes/services/structures/stub/StructuresImplMockBuildRequest.java b/src/test/java/fr/insee/rmes/services/structures/stub/StructuresImplMockBuildRequest.java new file mode 100644 index 00000000..da555e86 --- /dev/null +++ b/src/test/java/fr/insee/rmes/services/structures/stub/StructuresImplMockBuildRequest.java @@ -0,0 +1,26 @@ +package fr.insee.rmes.services.structures.stub; + +import fr.insee.rmes.services.structures.StructuresImpl; +import fr.insee.rmes.utils.exceptions.RmesException; +import org.springframework.stereotype.Service; + +import java.util.Map; + +@Service +public class StructuresImplMockBuildRequest extends StructuresImpl { + + @Override + protected String buildRequest(String path, String fileName, Map params) throws RmesException { + switch (fileName) { + case "getStructure.ftlh": + case "getStructureComponents.ftlh": + case "getComponent.ftlh": + case "getComponentChildren.ftlh": + return fileName; + + default: + throw new RuntimeException("Not implemented " + fileName); + } + + } +} diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml new file mode 100644 index 00000000..09b2e62f --- /dev/null +++ b/src/test/resources/application-test.yml @@ -0,0 +1,8 @@ +fr: + insee: + rmes: + magma: + envir: QF + force: + ssl: false + From be05d633185c8f2454a1d844cdfe7c6cf49b7122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Bourgeois?= Date: Thu, 4 May 2023 12:11:58 +0200 Subject: [PATCH 2/2] Update StructuresImpl --- .../services/structures/StructuresImpl.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/fr/insee/rmes/services/structures/StructuresImpl.java b/src/main/java/fr/insee/rmes/services/structures/StructuresImpl.java index a7d23580..fe1ac7d2 100644 --- a/src/main/java/fr/insee/rmes/services/structures/StructuresImpl.java +++ b/src/main/java/fr/insee/rmes/services/structures/StructuresImpl.java @@ -13,6 +13,7 @@ import org.json.JSONArray; import org.json.JSONObject; import org.springframework.stereotype.Service; +import com.fasterxml.jackson.databind.JsonNode; import javax.xml.soap.SAAJResult; import java.util.HashMap; @@ -176,20 +177,20 @@ private void getStructureComponents(String id, JSONObject structure) throws Rmes component.put(ORDRE, componentSpecification.getString(ORDRE)); } if(componentSpecification.has("attachement")){ - component.put("attachement", componentSpecification.getString("attachment").replace(QB.NAMESPACE, "")); + component.put("attachement", componentSpecification.getString("attachement").replace(QB.NAMESPACE, "")); } if(componentSpecification.has(OBLIGATOIRE)){ component.put(OBLIGATOIRE, componentSpecification.getString(OBLIGATOIRE).equalsIgnoreCase("true") ? "oui": "non"); } if(idComponent.startsWith("a")){ - attributes.put(component); + extracted(attributes, component); } if(idComponent.startsWith("m")){ - measures.put(component); + extracted(measures, component); } if(idComponent.startsWith("d")){ - dimensions.put(component); + extracted(dimensions, component); } } structure.put("attributs", attributes); @@ -197,6 +198,14 @@ private void getStructureComponents(String id, JSONObject structure) throws Rmes structure.put("dimensions", dimensions); } + private void extracted(JSONArray sortie, JSONObject component) { + JSONObject entree = component; + entree.remove("label"); + entree.remove("uri"); + entree.remove("concept"); + sortie.put(entree); + } + private static final String STATUT_VALIDATION = "statutValidation"; @Override @@ -226,6 +235,7 @@ public JSONObject getComponent(String id) throws RmesException { JSONObject component = repoGestion.getResponseAsObject(buildRequest("getComponent.ftlh", params)); + component.put(LABEL, this.formatLabel(component)); component.put(NOM,this.formatNom(component)); component.remove("altLabelLg1");