From 8a4f15f1dcad0e667cc5fdc3e0ac80f08b60a084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20S=C3=A9nave?= <59770457+nsenave@users.noreply.github.com> Date: Wed, 22 May 2024 15:34:03 +0200 Subject: [PATCH] Next (#114) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * build: update dependencies (#113) * ci: bump softprops/action-gh-release from 1 to 2 (#97) * build: bump actions/setup-java from 3 to 4 (#98) * build: bump org.apache.logging.log4j:log4j-slf4j2-impl from 2.22.1 to 2.23.1 (#99) * ci: bump mukunku/tag-exists-action from 1.2.0 to 1.6.0 (#100) * ci: bump actions/github-script from 6 to 7 (#103) * ci: bump actions/checkout from 3 to 4 (#104) * build: bump org.apache.maven.plugins:maven-source-plugin from 3.2.1 to 3.3.1 (#108) * build: bump org.apache.maven.plugins:maven-gpg-plugin from 3.1.0 to 3.2.4 (#111) --------- Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * feat: Allow Control/@scope="line" (#107) * Allow Control/@scope="line" * bump: 1.3.4 * chore: snapshot version --------- Co-authored-by: nsenave * feat: enrich uncollected cells (#112) * Fix/json serializer (#119) * fix: serialize to string without root attribute * test: add test for new use case * bump: version to 1.3.6-SNAPSHOT * chore: remove blanck * Fix/serialize synonyms (#120) * Fix serialize synonyms issues * build: remove useless maven repo (exist-db) * bump: version to 1.3.7-SNAPSHOT * bump to 1.3.7 --------- Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: François Bulot Co-authored-by: Laurent Caouissin <38245508+laurentC35@users.noreply.github.com> --- .github/workflows/create-release.yml | 12 +- .github/workflows/create-snapshot.yml | 18 +- .github/workflows/tests.yml | 4 +- pom.xml | 16 +- .../insee/pogues/PostProcessingException.java | 9 + .../pogues/conversion/JSONSerializer.java | 20 ++- .../conversion/JSONSynonymsPostProcessor.java | 158 ++++++++++++++++++ .../pogues/mock/QuestionnaireFactory.java | 37 +++- src/main/resources/xsd/Questionnaire.xsd | 17 +- .../insee/pogues/test/JSONSerializerTest.java | 78 +++++++++ ...st.java => JSONSynonymsProcessorTest.java} | 45 +++-- 11 files changed, 361 insertions(+), 53 deletions(-) create mode 100644 src/main/java/fr/insee/pogues/PostProcessingException.java create mode 100644 src/main/java/fr/insee/pogues/conversion/JSONSynonymsPostProcessor.java rename src/test/java/fr/insee/pogues/test/{JSONSynonymsPreProcessorTest.java => JSONSynonymsProcessorTest.java} (58%) diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml index 160c5d2..107939f 100644 --- a/.github/workflows/create-release.yml +++ b/.github/workflows/create-release.yml @@ -12,7 +12,7 @@ jobs: release-version: ${{ steps.version.outputs.version }} steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Get version id: version @@ -21,7 +21,7 @@ jobs: - name: Print version run: echo ${{ steps.version.outputs.version }} - - uses: mukunku/tag-exists-action@v1.2.0 + - uses: mukunku/tag-exists-action@v1.6.0 name: Check tag existence id: check-tag-exists with: @@ -52,7 +52,7 @@ jobs: needs: check-version runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: ref: ${{ github.ref }} fetch-depth: 0 @@ -70,7 +70,7 @@ jobs: token: ${{ secrets.GITHUB_TOKEN }} writeToFile: false - - uses: softprops/action-gh-release@v1 + - uses: softprops/action-gh-release@v2 with: tag_name: ${{ needs.check-version.outputs.release-version }} target_commitish: ${{ github.head_ref || github.ref }} @@ -83,9 +83,9 @@ jobs: needs: create-release runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Maven Central Repository - uses: actions/setup-java@v3 + uses: actions/setup-java@v4 with: java-version: '21' distribution: 'temurin' diff --git a/.github/workflows/create-snapshot.yml b/.github/workflows/create-snapshot.yml index 5866b72..6456066 100644 --- a/.github/workflows/create-snapshot.yml +++ b/.github/workflows/create-snapshot.yml @@ -13,7 +13,7 @@ jobs: tag-version: ${{ steps.version.outputs.version }} steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Get version id: version @@ -22,7 +22,7 @@ jobs: - name: Print version run: echo ${{ steps.version.outputs.version }} - - uses: mukunku/tag-exists-action@v1.2.0 + - uses: mukunku/tag-exists-action@v1.6.0 name: Check tag existence id: check-tag-exists with: @@ -45,10 +45,10 @@ jobs: needs: check-version runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up JDK - uses: actions/setup-java@v3 + uses: actions/setup-java@v4 with: java-version: '21' distribution: 'temurin' @@ -61,7 +61,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Create tag - uses: actions/github-script@v6 + uses: actions/github-script@v7 with: script: | github.rest.git.createRef({ @@ -75,9 +75,9 @@ jobs: needs: create-tag runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Maven Central Repository - uses: actions/setup-java@v3 + uses: actions/setup-java@v4 with: java-version: '21' distribution: 'temurin' @@ -97,7 +97,7 @@ jobs: needs: [ deploy-maven-repo, check-version ] runs-on: ubuntu-latest steps: - - uses: actions/github-script@v6 + - uses: actions/github-script@v7 with: script: | github.rest.issues.createComment({ @@ -111,7 +111,7 @@ jobs: needs: deploy-comment runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - uses: actions-ecosystem/action-remove-labels@v1 with: labels: deploy-snapshot \ No newline at end of file diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8d96c25..6e1d3ef 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -9,10 +9,10 @@ jobs: if: ${{ ! contains(github.event.pull_request.labels.*.name, 'deploy-snapshot') }} runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up JDK - uses: actions/setup-java@v3 + uses: actions/setup-java@v4 with: java-version: '21' distribution: 'temurin' diff --git a/pom.xml b/pom.xml index e5fc9b8..bf844bd 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ fr.insee.pogues pogues-model jar - 1.3.3 + 1.3.7 Pogues Model Classes and converters for the Pogues model https://inseefr.github.io/Pogues-Model/ @@ -106,7 +106,7 @@ org.apache.logging.log4j log4j-slf4j2-impl - 2.22.1 + 2.23.1 test @@ -219,7 +219,7 @@ org.apache.maven.plugins maven-source-plugin - 3.2.1 + 3.3.1 attach-sources @@ -256,7 +256,7 @@ org.apache.maven.plugins maven-gpg-plugin - 3.1.0 + 3.2.4 sign-artifacts @@ -277,12 +277,4 @@ - - - - exist - https://raw.github.com/eXist-db/mvn-repo/master/ - - - diff --git a/src/main/java/fr/insee/pogues/PostProcessingException.java b/src/main/java/fr/insee/pogues/PostProcessingException.java new file mode 100644 index 0000000..5dab2f5 --- /dev/null +++ b/src/main/java/fr/insee/pogues/PostProcessingException.java @@ -0,0 +1,9 @@ +package fr.insee.pogues; + +public class PostProcessingException extends RuntimeException { + + public PostProcessingException(String message, Exception e) { + super(message, e); + } + +} diff --git a/src/main/java/fr/insee/pogues/conversion/JSONSerializer.java b/src/main/java/fr/insee/pogues/conversion/JSONSerializer.java index 7182577..ae9dd99 100644 --- a/src/main/java/fr/insee/pogues/conversion/JSONSerializer.java +++ b/src/main/java/fr/insee/pogues/conversion/JSONSerializer.java @@ -21,6 +21,12 @@ public class JSONSerializer { public JSONSerializer() { } + public JSONSerializer(boolean withoutJsonRoot){ + this.withoutJsonRoot = withoutJsonRoot; + } + + private boolean withoutJsonRoot; + private static final Logger logger = LoggerFactory.getLogger(JSONSerializer.class); public String serialize(Questionnaire questionnaire) throws JAXBException, UnsupportedEncodingException { @@ -34,15 +40,15 @@ public String serialize(Questionnaire questionnaire) throws JAXBException, Unsup marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); // Set it to true if you need to include the JSON root element in the JSON output - marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); + marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, !withoutJsonRoot); // Set it to true if you need the JSON output to formatted marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // Marshal the questionnaire object to JSON and put the output in a string ByteArrayOutputStream baos = new ByteArrayOutputStream(); marshaller.marshal(questionnaire, baos); - - return baos.toString("UTF-8"); + String questionnaireString = baos.toString("UTF-8"); + return new JSONSynonymsPostProcessor().transform(questionnaireString); } @@ -57,7 +63,7 @@ public String serialize(SequenceType sequence) throws JAXBException, Unsupported marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); // Set it to true if you need to include the JSON root element in the JSON output - marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); + marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, !withoutJsonRoot); // Set it to true if you need the JSON output to formatted marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // Marshal the sequence object to JSON and put the output in a string @@ -79,7 +85,7 @@ public String serialize(CodeList codeList) throws JAXBException, UnsupportedEnco marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); // Set it to true if you need to include the JSON root element in the JSON output - marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); + marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, !withoutJsonRoot); // Set it to true if you need the JSON output to formatted marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // Marshal the code list object to JSON and put the output in a string @@ -101,7 +107,7 @@ public String serialize(QuestionType question) throws JAXBException, Unsupported marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); // Set it to true if you need to include the JSON root element in the JSON output - marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); + marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, !withoutJsonRoot); // Set it to true if you need the JSON output to formatted marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // Marshal the code list object to JSON and put the output in a string @@ -123,7 +129,7 @@ public String serialize(ResponseType response) throws JAXBException, Unsupported marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); // Set it to true if you need to include the JSON root element in the JSON output - marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); + marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, !withoutJsonRoot); // Set it to true if you need the JSON output to formatted marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // Marshal the code list object to JSON and put the output in a string diff --git a/src/main/java/fr/insee/pogues/conversion/JSONSynonymsPostProcessor.java b/src/main/java/fr/insee/pogues/conversion/JSONSynonymsPostProcessor.java new file mode 100644 index 0000000..5df6f64 --- /dev/null +++ b/src/main/java/fr/insee/pogues/conversion/JSONSynonymsPostProcessor.java @@ -0,0 +1,158 @@ +package fr.insee.pogues.conversion; + +import fr.insee.pogues.PostProcessingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.json.*; +import java.io.*; + +/** Pogues application generates suggester "synonyms" parameter as key/value pairs. + * This modeling is not adapted for XSD generation (at least without a hack). + * This class is intended to transform key/value pairs generated by Pogues into source/target fields that are defined + * in the xml schema. */ +public class JSONSynonymsPostProcessor { + + private static final Logger logger = LoggerFactory.getLogger(JSONSynonymsPostProcessor.class); + + public String transform(String jsonQuestionnaireString) { + // Should throw an exception, yet it may have impacts in Pogues-Back-Office + if (jsonQuestionnaireString == null) { + logger.warn("null string given in JSON synonyms post-processing method."); + return null; + } + return transform(new ByteArrayInputStream(jsonQuestionnaireString.getBytes())); + } + + public String transform(InputStream jsonQuestionnaireInputStream) { + // Should throw an exception, yet it may have impacts in Pogues-Back-Office + if (jsonQuestionnaireInputStream == null) { + logger.warn("null input stream given in JSON synonyms post-processing method."); + return null; + } + + logger.info("Post-processing json questionnaire stream source..."); + + OutputStream outputStream = new ByteArrayOutputStream(); + + try (JsonReader jsonReader = Json.createReader(jsonQuestionnaireInputStream); + JsonWriter jsonWriter = Json.createWriter(outputStream)) { + + JsonObject jsonQuestionnaire = jsonReader.readObject(); + + // We will copy the entire input json object, except the "symLinks" attribute in PairwiseLinks components + JsonObjectBuilder jsonQuestionnaireBuilder = Json.createObjectBuilder(); + + if(jsonQuestionnaire.containsKey("Questionnaire")) { + editWrapperQuestionnaire(jsonQuestionnaire, jsonQuestionnaireBuilder) +; } else { + editQuestionnaire(jsonQuestionnaire, jsonQuestionnaireBuilder); + } + + jsonWriter.writeObject(jsonQuestionnaireBuilder.build()); + } + + String result = outputStream.toString(); + try { + outputStream.close(); + } catch (IOException e) { + throw new PostProcessingException("IO exception occurred when trying to close post processing output.", e); + } + + return result; + } + + private void editWrapperQuestionnaire(JsonObject jsonWrapperQuestionnaire, JsonObjectBuilder jsonQuestionnaireBuilder) { + jsonWrapperQuestionnaire.forEach((key, jsonValue) -> { + if (! "Questionnaire".equals(key)) { + jsonQuestionnaireBuilder.add(key, jsonValue); + } else { + editQuestionnaire((JsonObject) jsonValue, jsonQuestionnaireBuilder); + } + }); + jsonQuestionnaireBuilder.add("Questionnaire", jsonQuestionnaireBuilder.build()); + } + + + private void editQuestionnaire(JsonObject jsonQuestionnaire, JsonObjectBuilder jsonQuestionnaireBuilder) { + jsonQuestionnaire.forEach((key, jsonValue) -> { + if (! "CodeLists".equals(key)) { + jsonQuestionnaireBuilder.add(key, jsonValue); + } else { + editCodeLists(jsonQuestionnaireBuilder, (JsonObject) jsonValue); + } + }); + } + + private void editCodeLists(JsonObjectBuilder jsonQuestionnaireBuilder, JsonObject jsonCodeLists) { + JsonObjectBuilder jsonCodeListsBuilder = Json.createObjectBuilder(); + JsonArray jsonCodeList = jsonCodeLists.getJsonArray("CodeList"); + editCodeListArray(jsonCodeListsBuilder, jsonCodeList); + jsonQuestionnaireBuilder.add("CodeLists", jsonCodeListsBuilder.build()); + } + + private void editCodeListArray(JsonObjectBuilder jsonCodeLists, JsonArray jsonCodeListArray) { + JsonArrayBuilder jsonCodeListArrayBuilder = Json.createArrayBuilder(); + for (JsonValue jsonValue : jsonCodeListArray) { + JsonObject jsonCodeList = (JsonObject) jsonValue; + editCodeList(jsonCodeListArrayBuilder, jsonCodeList); + } + jsonCodeLists.add("CodeList", jsonCodeListArrayBuilder.build()); + } + + private void editCodeList(JsonArrayBuilder jsonCodeListArrayBuilder, JsonObject jsonCodeList) { + JsonObjectBuilder jsonCodeListBuilder = Json.createObjectBuilder(); + jsonCodeList.forEach((key, jsonValue) -> { + if (! "SuggesterParameters".equals(key)) { + jsonCodeListBuilder.add(key, jsonValue); + } else { + editSuggesterParameters(jsonCodeListBuilder, (JsonObject) jsonValue); + } + }); + jsonCodeListArrayBuilder.add(jsonCodeListBuilder.build()); + } + + private void editSuggesterParameters(JsonObjectBuilder jsonCodeListBuilder, JsonObject jsonSuggesterParameters) { + JsonObjectBuilder jsonSuggesterParametersBuilder = Json.createObjectBuilder(); + jsonSuggesterParameters.forEach((key, jsonValue) -> { + if (! "fields".equals(key)) { + jsonSuggesterParametersBuilder.add(key, jsonValue); + } else { + editFieldsArray(jsonSuggesterParametersBuilder, (JsonArray) jsonValue); + } + }); + jsonCodeListBuilder.add("SuggesterParameters", jsonSuggesterParametersBuilder.build()); + } + + private void editFieldsArray(JsonObjectBuilder jsonSuggesterParametersBuilder, JsonArray jsonFieldsArray) { + JsonArrayBuilder jsonFieldsArrayBuilder = Json.createArrayBuilder(); + for (JsonValue jsonValue : jsonFieldsArray) { + JsonObject jsonFields = (JsonObject) jsonValue; + editFields(jsonFieldsArrayBuilder, jsonFields); + } + jsonSuggesterParametersBuilder.add("fields", jsonFieldsArrayBuilder.build()); + } + + private void editFields(JsonArrayBuilder jsonFieldsArrayBuilder, JsonObject jsonFields) { + JsonObjectBuilder jsonFieldsBuilder = Json.createObjectBuilder(); + jsonFields.forEach((key, jsonValue) -> { + if (! "synonyms".equals(key)) { + jsonFieldsBuilder.add(key, jsonValue); + } else { + editSynonyms(jsonFieldsBuilder, (JsonArray) jsonValue); + } + }); + jsonFieldsArrayBuilder.add(jsonFieldsBuilder.build()); + } + + private void editSynonyms(JsonObjectBuilder jsonFieldsBuilder, JsonArray jsonSynonyms) { + JsonObjectBuilder jsonSynonymsBuilder = Json.createObjectBuilder(); + for (JsonValue synonymField : jsonSynonyms) { + String synonymName = ((JsonObject) synonymField).getString("source"); + JsonArray synonymTarget = ((JsonObject) synonymField).getJsonArray("target"); + jsonSynonymsBuilder.add(synonymName,synonymTarget); + } + jsonFieldsBuilder.add("synonyms", jsonSynonymsBuilder.build()); + } + +} diff --git a/src/main/java/fr/insee/pogues/mock/QuestionnaireFactory.java b/src/main/java/fr/insee/pogues/mock/QuestionnaireFactory.java index 6541052..c8d26e0 100644 --- a/src/main/java/fr/insee/pogues/mock/QuestionnaireFactory.java +++ b/src/main/java/fr/insee/pogues/mock/QuestionnaireFactory.java @@ -1,13 +1,9 @@ package fr.insee.pogues.mock; +import fr.insee.pogues.model.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import fr.insee.pogues.model.ComponentGroup; -import fr.insee.pogues.model.DataCollection; -import fr.insee.pogues.model.Questionnaire; -import fr.insee.pogues.model.SequenceType; - public class QuestionnaireFactory { private static final Logger logger = LoggerFactory.getLogger(QuestionnaireFactory.class); @@ -59,6 +55,37 @@ public Questionnaire createQuestionnaire() { logger.debug("Code list container added to questionnaire number " + questionnaireNumber); + return questionnaire; + } + + public Questionnaire createMinimalQuestionnaire(){ + Questionnaire questionnaire = new Questionnaire(); + questionnaire.setId("test"); + questionnaire.setAgency("Insee"); + questionnaire.setFinal(true); + questionnaire.setName("Without root questionnaire"); + questionnaire.setFlowLogic(FlowLogicEnum.FILTER); + return questionnaire; + } + + public Questionnaire createQuestionnaireWithSynonyms(){ + Questionnaire questionnaire = new Questionnaire(); + questionnaire.setId("test"); + CodeLists codeLists = new CodeLists(); + CodeList codeList = new CodeList(); + SuggesterParametersType suggesterParametersType = new SuggesterParametersType(); + SuggesterField suggesterField = new SuggesterField(); + FieldSynonym fieldSynonym = new FieldSynonym(); + fieldSynonym.setSource("foo"); + fieldSynonym.getTarget().add("one"); + fieldSynonym.getTarget().add("two"); + suggesterField.getSynonyms().add(fieldSynonym); + suggesterParametersType.getFields().add(suggesterField); + codeList.setSuggesterParameters(suggesterParametersType); + codeLists.getCodeList().add(codeList); + questionnaire.setCodeLists(codeLists); + + return questionnaire; } } diff --git a/src/main/resources/xsd/Questionnaire.xsd b/src/main/resources/xsd/Questionnaire.xsd index ac6c03f..001e70c 100644 --- a/src/main/resources/xsd/Questionnaire.xsd +++ b/src/main/resources/xsd/Questionnaire.xsd @@ -350,6 +350,11 @@ the dimensions, format "i" (one dimension) or "i j" (two dimensions) + + + optional Label for noDataByDefintion cells + + @@ -366,7 +371,16 @@ the dimensions, format "i" (one dimension) or "i j" (two dimensions) - + + + Specifies a condition for filter for displaying the Response and not the AlternativeLabel + + + + + Alternative Label when the filter is false + + @@ -557,6 +571,7 @@ + diff --git a/src/test/java/fr/insee/pogues/test/JSONSerializerTest.java b/src/test/java/fr/insee/pogues/test/JSONSerializerTest.java index 61826a3..029d6a1 100644 --- a/src/test/java/fr/insee/pogues/test/JSONSerializerTest.java +++ b/src/test/java/fr/insee/pogues/test/JSONSerializerTest.java @@ -11,6 +11,7 @@ import javax.xml.bind.JAXBException; import java.io.File; +import java.io.IOException; import java.io.UnsupportedEncodingException; @@ -33,6 +34,40 @@ void testQuestionnaire() throws Exception { System.out.println("Serialization time: " + elapsedTime); } + @Test + void testQuestionnaireWithOrWithoutRoot() throws Exception { + + QuestionnaireFactory factory = new QuestionnaireFactory(); + Questionnaire fakeQuestionnaire = factory.createMinimalQuestionnaire(); + + JSONSerializer serializer1 = new JSONSerializer(); + JSONSerializer serializer2 = new JSONSerializer(true); + String withRoot = serializer1.serialize(fakeQuestionnaire); + String withoutRoot = serializer2.serialize(fakeQuestionnaire); + + String expectedJsonWithRoot = """ + { + "Questionnaire": { + "id" : "test", + "agency" : "Insee", + "final" : true, + "flowLogic" : "FILTER", + "Name" : "Without root questionnaire" + } + }""";; + + String expectedJsonWithoutRoot = """ + { + "id" : "test", + "agency" : "Insee", + "final" : true, + "flowLogic" : "FILTER", + "Name" : "Without root questionnaire" + }"""; + JSONAssert.assertEquals(expectedJsonWithRoot, withRoot, JSONCompareMode.STRICT); + JSONAssert.assertEquals(expectedJsonWithoutRoot, withoutRoot, JSONCompareMode.STRICT); + } + @Test void testSequence() throws Exception { @@ -158,4 +193,47 @@ void serializeCodeList() throws JAXBException, UnsupportedEncodingException, JSO JSONAssert.assertEquals(expectedJson, result, JSONCompareMode.STRICT); } + @Test + void testSynonyms() throws JAXBException, IOException, JSONException { + QuestionnaireFactory factory = new QuestionnaireFactory(); + Questionnaire questionnaire = factory.createQuestionnaireWithSynonyms(); + + JSONSerializer serializer = new JSONSerializer(); + JSONSerializer serializerWithoutRoot = new JSONSerializer(true); + String result = serializer.serialize(questionnaire); + String resultWithoutRoot = serializerWithoutRoot.serialize(questionnaire); + String expectedJson = """ + { + "Questionnaire": { + "id": "test", + "CodeLists": { + "CodeList": [ + { + "SuggesterParameters": { + "fields": [{ "synonyms": { "foo": ["one", "two"] } }] + } + } + ] + } + } + }"""; + String expectedJsonWithoutRoot = """ + { + "id": "test", + "CodeLists": { + "CodeList": [ + { + "SuggesterParameters": { + "fields": [{ "synonyms": { "foo": ["one", "two"] } }] + } + } + ] + } + }"""; + + JSONAssert.assertEquals(expectedJson, result, JSONCompareMode.STRICT); + JSONAssert.assertEquals(expectedJsonWithoutRoot, resultWithoutRoot, JSONCompareMode.STRICT); + + } + } diff --git a/src/test/java/fr/insee/pogues/test/JSONSynonymsPreProcessorTest.java b/src/test/java/fr/insee/pogues/test/JSONSynonymsProcessorTest.java similarity index 58% rename from src/test/java/fr/insee/pogues/test/JSONSynonymsPreProcessorTest.java rename to src/test/java/fr/insee/pogues/test/JSONSynonymsProcessorTest.java index 5ffba8c..aeedd21 100644 --- a/src/test/java/fr/insee/pogues/test/JSONSynonymsPreProcessorTest.java +++ b/src/test/java/fr/insee/pogues/test/JSONSynonymsProcessorTest.java @@ -1,18 +1,16 @@ package fr.insee.pogues.test; +import fr.insee.pogues.conversion.JSONSynonymsPostProcessor; import fr.insee.pogues.conversion.JSONSynonymsPreProcessor; import org.json.JSONException; import org.junit.jupiter.api.Test; import org.skyscreamer.jsonassert.JSONAssert; import org.skyscreamer.jsonassert.JSONCompareMode; -class JSONSynonymsPreProcessorTest { +class JSONSynonymsProcessorTest { - @Test - void transformSynonyms() throws JSONException { - // - String jsonInput = """ + private String questionnaireSynonymsObject = """ { "id": "foo-id", "CodeLists": { @@ -40,11 +38,8 @@ void transformSynonyms() throws JSONException { ] } }"""; - // - JSONSynonymsPreProcessor jsonSynonymsPreProcessor = new JSONSynonymsPreProcessor(); - String result = jsonSynonymsPreProcessor.transform(jsonInput); - // - String expected = """ + + private String questionnaireSynonymsArray = """ { "id": "foo-id", "CodeLists": { @@ -78,7 +73,35 @@ void transformSynonyms() throws JSONException { ] } }"""; - JSONAssert.assertEquals(expected, result, JSONCompareMode.STRICT); + + @Test + void preProcessingTransformSynonyms() throws JSONException { + JSONSynonymsPreProcessor jsonSynonymsPreProcessor = new JSONSynonymsPreProcessor(); + String result = jsonSynonymsPreProcessor.transform(questionnaireSynonymsObject); + JSONAssert.assertEquals(questionnaireSynonymsArray, result, JSONCompareMode.STRICT); + } + + @Test + void postProcessingTransformSynonyms() throws JSONException { + JSONSynonymsPostProcessor jsonSynonymsPostProcessor = new JSONSynonymsPostProcessor(); + String result = jsonSynonymsPostProcessor.transform(questionnaireSynonymsArray); + JSONAssert.assertEquals(questionnaireSynonymsObject, result, JSONCompareMode.STRICT); + } + + @Test + void identityPrePostPocessingTransformSynonyms() throws JSONException { + JSONSynonymsPreProcessor jsonSynonymsPreProcessor = new JSONSynonymsPreProcessor(); + JSONSynonymsPostProcessor jsonSynonymsPostProcessor = new JSONSynonymsPostProcessor(); + String result = jsonSynonymsPostProcessor.transform(jsonSynonymsPreProcessor.transform(questionnaireSynonymsObject)); + JSONAssert.assertEquals(questionnaireSynonymsObject, result, JSONCompareMode.STRICT); + } + + @Test + void identityPostPrePocessingTransformSynonyms() throws JSONException { + JSONSynonymsPostProcessor jsonSynonymsPostProcessor = new JSONSynonymsPostProcessor(); + JSONSynonymsPreProcessor jsonSynonymsPreProcessor = new JSONSynonymsPreProcessor(); + String result = jsonSynonymsPreProcessor.transform(jsonSynonymsPostProcessor.transform(questionnaireSynonymsArray)); + JSONAssert.assertEquals(questionnaireSynonymsArray, result, JSONCompareMode.STRICT); } }