From 0e793de8abfa2f4a32b1bb7631cca9ab6cd4a002 Mon Sep 17 00:00:00 2001 From: Laurent Caouissin Date: Tue, 30 Jun 2026 15:51:17 +0200 Subject: [PATCH 1/2] feat: expose suggesterParams processor for SuggesterParameters type --- pom.xml | 2 +- .../conversion/JSONSynonymsPreProcessor.java | 46 ++++++++++++++++ .../test/JSONSynonymsProcessorTest.java | 54 +++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 63c1e0d..63ef83b 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ fr.insee.pogues pogues-model - 1.16.0 + 1.17.0 jar Pogues Model diff --git a/src/main/java/fr/insee/pogues/conversion/JSONSynonymsPreProcessor.java b/src/main/java/fr/insee/pogues/conversion/JSONSynonymsPreProcessor.java index 2b6dd9b..4374fb2 100644 --- a/src/main/java/fr/insee/pogues/conversion/JSONSynonymsPreProcessor.java +++ b/src/main/java/fr/insee/pogues/conversion/JSONSynonymsPreProcessor.java @@ -57,6 +57,52 @@ public String transform(InputStream jsonQuestionnaireInputStream) { return result; } + public String transformSuggesterParameters(String jsonSuggesterParams){ + // Should throw an exception, yet it may have impacts in Pogues-Back-Office + if (jsonSuggesterParams == null) { + logger.warn("null string given in JSON synonyms pre-processing method."); + return null; + } + + return transformSuggesterParameters(new ByteArrayInputStream(jsonSuggesterParams.getBytes())); + } + + public String transformSuggesterParameters(InputStream jsonSuggesterParamsIS){ + // Should throw an exception, yet it may have impacts in Pogues-Back-Office + if (jsonSuggesterParamsIS == null) { + logger.warn("null input stream given in JSON synonyms pre-processing method."); + return null; + } + + logger.debug("Pre-processing json suggesterParams stream source..."); + + OutputStream outputStream = new ByteArrayOutputStream(); + + try (JsonReader jsonReader = Json.createReader(jsonSuggesterParamsIS); + JsonWriter jsonWriter = Json.createWriter(outputStream)) { + + JsonObject jsonSuggesterParams = jsonReader.readObject(); + + JsonObjectBuilder jsonSuggesterParamsBuilder = Json.createObjectBuilder(); + jsonSuggesterParams.forEach((key, jsonValue)-> { + if (! "fields".equals(key)) { + jsonSuggesterParamsBuilder.add(key, jsonValue); + } else { + editFieldsArray(jsonSuggesterParamsBuilder, (JsonArray) jsonValue); + } + }); + jsonWriter.writeObject(jsonSuggesterParamsBuilder.build()); + } + + String result = outputStream.toString(); + try { + outputStream.close(); + } catch (IOException e) { + throw new PreProcessingException("IO exception occurred when trying to close pre processing output.", e); + } + + return result; + } private static void editQuestionnaire(JsonObject jsonQuestionnaire, JsonObjectBuilder jsonQuestionnaireBuilder) { jsonQuestionnaire.forEach((key, jsonValue) -> { diff --git a/src/test/java/fr/insee/pogues/test/JSONSynonymsProcessorTest.java b/src/test/java/fr/insee/pogues/test/JSONSynonymsProcessorTest.java index aeedd21..1ab6052 100644 --- a/src/test/java/fr/insee/pogues/test/JSONSynonymsProcessorTest.java +++ b/src/test/java/fr/insee/pogues/test/JSONSynonymsProcessorTest.java @@ -74,6 +74,53 @@ class JSONSynonymsProcessorTest { } }"""; + private String suggesterParametersSynonymsObject = """ + { + "fields": [ + { + "name": "foo", + "synonyms": { + "foo": [ + "BAR" + ], + "example": [ + "INSTANCE", + "SAMPLE", + "ILLUSTRATION" + ] + } + } + ] + } + """; + + + private String suggesterParametersSynonymsArray = """ + { + "fields": [ + { + "name": "foo", + "synonyms": [ + { + "source": "foo", + "target": [ + "BAR" + ] + }, + { + "source": "example", + "target": [ + "INSTANCE", + "SAMPLE", + "ILLUSTRATION" + ] + } + ] + } + ] + } + """; + @Test void preProcessingTransformSynonyms() throws JSONException { JSONSynonymsPreProcessor jsonSynonymsPreProcessor = new JSONSynonymsPreProcessor(); @@ -104,4 +151,11 @@ void identityPostPrePocessingTransformSynonyms() throws JSONException { JSONAssert.assertEquals(questionnaireSynonymsArray, result, JSONCompareMode.STRICT); } + @Test + void preProcessingTransformSuggesterParameters() throws JSONException { + JSONSynonymsPreProcessor jsonSynonymsPreProcessor = new JSONSynonymsPreProcessor(); + String result = jsonSynonymsPreProcessor.transformSuggesterParameters(suggesterParametersSynonymsObject); + JSONAssert.assertEquals(suggesterParametersSynonymsArray, result, JSONCompareMode.STRICT); + } + } From 9fb127122143582bb95f47ab3e0be9459f74c954 Mon Sep 17 00:00:00 2001 From: Laurent Caouissin Date: Tue, 30 Jun 2026 15:54:17 +0200 Subject: [PATCH 2/2] ref: sonar issue --- .../insee/pogues/conversion/JSONSynonymsPreProcessor.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/fr/insee/pogues/conversion/JSONSynonymsPreProcessor.java b/src/main/java/fr/insee/pogues/conversion/JSONSynonymsPreProcessor.java index 4374fb2..e5fddeb 100644 --- a/src/main/java/fr/insee/pogues/conversion/JSONSynonymsPreProcessor.java +++ b/src/main/java/fr/insee/pogues/conversion/JSONSynonymsPreProcessor.java @@ -15,6 +15,8 @@ public class JSONSynonymsPreProcessor { private static final Logger logger = LoggerFactory.getLogger(JSONSynonymsPreProcessor.class); + private static final String FIELDS_KEY = "fields"; + public String transform(String jsonQuestionnaireString) { // Should throw an exception, yet it may have impacts in Pogues-Back-Office if (jsonQuestionnaireString == null) { @@ -85,7 +87,7 @@ public String transformSuggesterParameters(InputStream jsonSuggesterParamsIS){ JsonObjectBuilder jsonSuggesterParamsBuilder = Json.createObjectBuilder(); jsonSuggesterParams.forEach((key, jsonValue)-> { - if (! "fields".equals(key)) { + if (! FIELDS_KEY.equals(key)) { jsonSuggesterParamsBuilder.add(key, jsonValue); } else { editFieldsArray(jsonSuggesterParamsBuilder, (JsonArray) jsonValue); @@ -145,7 +147,7 @@ private static void editCodeList(JsonArrayBuilder jsonCodeListArrayBuilder, Json private static void editSuggesterParameters(JsonObjectBuilder jsonCodeListBuilder, JsonObject jsonSuggesterParameters) { JsonObjectBuilder jsonSuggesterParametersBuilder = Json.createObjectBuilder(); jsonSuggesterParameters.forEach((key, jsonValue) -> { - if (! "fields".equals(key)) { + if (! FIELDS_KEY.equals(key)) { jsonSuggesterParametersBuilder.add(key, jsonValue); } else { editFieldsArray(jsonSuggesterParametersBuilder, (JsonArray) jsonValue); @@ -160,7 +162,7 @@ private static void editFieldsArray(JsonObjectBuilder jsonSuggesterParametersBui JsonObject jsonFields = (JsonObject) jsonValue; editFields(jsonFieldsArrayBuilder, jsonFields); } - jsonSuggesterParametersBuilder.add("fields", jsonFieldsArrayBuilder.build()); + jsonSuggesterParametersBuilder.add(FIELDS_KEY, jsonFieldsArrayBuilder.build()); } private static void editFields(JsonArrayBuilder jsonFieldsArrayBuilder, JsonObject jsonFields) {