From 60d28456cd35d98ada1137b7214c43ed2b860dba Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Mon, 18 Nov 2024 21:14:00 +0000 Subject: [PATCH 1/2] feat: create a method to convert JSONArray to stream --- .../datasets/DatasetServiceImpl.java | 18 ++- .../java/fr/insee/rmes/utils/JSONUtils.java | 21 +++- .../structures/utils/StructureUtilsTest.java | 3 +- .../fr/insee/rmes/utils/JSONUtilsTest.java | 113 ++++++++++++++++++ 4 files changed, 138 insertions(+), 17 deletions(-) create mode 100644 src/test/java/fr/insee/rmes/utils/JSONUtilsTest.java diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/datasets/DatasetServiceImpl.java b/src/main/java/fr/insee/rmes/bauhaus_services/datasets/DatasetServiceImpl.java index c4cfa4a5a..84332ab93 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/datasets/DatasetServiceImpl.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/datasets/DatasetServiceImpl.java @@ -16,6 +16,7 @@ import fr.insee.rmes.persistance.ontologies.INSEE; import fr.insee.rmes.utils.DateUtils; import fr.insee.rmes.utils.Deserializer; +import fr.insee.rmes.utils.JSONUtils; import org.eclipse.rdf4j.model.BNode; import org.eclipse.rdf4j.model.IRI; import org.eclipse.rdf4j.model.Model; @@ -515,16 +516,13 @@ private void persistDataset(Dataset dataset) throws RmesException { Optional.ofNullable(dataset.getKeywords().lg2()).ifPresent(list -> list.forEach(keyword -> RdfUtils.addTripleString(datasetIri, DCAT.KEYWORD, keyword, config.getLg2(), model, graph))); } - JSONArray distributions = new JSONArray(this.getDistributions(dataset.getId())); - - for(int i = 0; i < distributions.length(); i++) { - JSONObject distribution = distributions.getJSONObject(i); - if (distribution.has("id")) { - String id = distribution.getString("id"); - IRI distributionIRI = RdfUtils.createIRI(getDistributionBaseUri() + "/" + id); - RdfUtils.addTripleUri(datasetIri, DCAT.HAS_DISTRIBUTION, distributionIRI, model, graph); - } - } + JSONUtils.stream(new JSONArray(this.getDistributions(dataset.getId()))) + .filter(distribution -> distribution.has("id")) + .map(distribution -> { + String id = distribution.getString("id"); + return RdfUtils.createIRI(getDistributionBaseUri() + "/" + id); + }) + .forEach(distributionIRI -> RdfUtils.addTripleUri(datasetIri, DCAT.HAS_DISTRIBUTION, distributionIRI, model, graph)); repoGestion.loadSimpleObject(datasetIri, model, null); } diff --git a/src/main/java/fr/insee/rmes/utils/JSONUtils.java b/src/main/java/fr/insee/rmes/utils/JSONUtils.java index 9af847590..9e7008023 100644 --- a/src/main/java/fr/insee/rmes/utils/JSONUtils.java +++ b/src/main/java/fr/insee/rmes/utils/JSONUtils.java @@ -7,9 +7,11 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; +import java.util.stream.Stream; public class JSONUtils { - + + public static JSONArray extractFieldToArray(JSONArray jsonA, String field) { JSONArray res = new JSONArray(); for(Object o: jsonA){ @@ -45,14 +47,21 @@ public static boolean isEmpty(JSONObject obj) { return true; } + + private static IntStream generateIntStreamBasedOnJsonArray(JSONArray array) { + return IntStream.range(0, array.length()); + } + + public static Stream stream(JSONArray array) { + return generateIntStreamBasedOnJsonArray(array).mapToObj(array::getJSONObject); + } + /** * Transform an array to a list of strings - * @param jsonArray - * @return */ - public static List jsonArrayToList(JSONArray jsonArray) { - return IntStream.range(0, jsonArray.length()) - .mapToObj(jsonArray::get) + public static List jsonArrayToList(JSONArray array) { + return generateIntStreamBasedOnJsonArray(array) + .mapToObj(array::get) .map(Object::toString) .collect(Collectors.toList()); } diff --git a/src/test/java/fr/insee/rmes/bauhaus_services/structures/utils/StructureUtilsTest.java b/src/test/java/fr/insee/rmes/bauhaus_services/structures/utils/StructureUtilsTest.java index 9561be896..0856318a9 100644 --- a/src/test/java/fr/insee/rmes/bauhaus_services/structures/utils/StructureUtilsTest.java +++ b/src/test/java/fr/insee/rmes/bauhaus_services/structures/utils/StructureUtilsTest.java @@ -29,12 +29,13 @@ class StructureUtilsTest { StructureUtils structureUtils = new StructureUtils(); @Mock StructureUtils mockStructureUtils; + @MockBean RepositoryGestion repositoryGestion; + @Autowired Config config; - public static final String VALIDATION_STATUS = "{\"state\":\"Published\"}"; diff --git a/src/test/java/fr/insee/rmes/utils/JSONUtilsTest.java b/src/test/java/fr/insee/rmes/utils/JSONUtilsTest.java new file mode 100644 index 000000000..1a8ca5fa9 --- /dev/null +++ b/src/test/java/fr/insee/rmes/utils/JSONUtilsTest.java @@ -0,0 +1,113 @@ +package fr.insee.rmes.utils; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.*; + +class JSONUtilsTest { + + @Test + void testExtractFieldToArray() { + JSONArray inputArray = new JSONArray(); + inputArray.put(new JSONObject().put("name", "Alice").put("age", 25)); + inputArray.put(new JSONObject().put("name", "Bob").put("age", 30)); + inputArray.put(new JSONObject().put("name", "Charlie").put("age", 35)); + + JSONArray result = JSONUtils.extractFieldToArray(inputArray, "name"); + + assertEquals(3, result.length()); + assertEquals("Alice", result.getString(0)); + assertEquals("Bob", result.getString(1)); + assertEquals("Charlie", result.getString(2)); + + // Cas 2 : JSONArray vide + JSONArray emptyArray = new JSONArray(); + result = JSONUtils.extractFieldToArray(emptyArray, "name"); + + assertEquals(0, result.length()); + } + + @Test + void testJsonArrayOfStringToString() { + JSONArray singleElementArray = new JSONArray(); + singleElementArray.put("Alice"); + String result = JSONUtils.jsonArrayOfStringToString(singleElementArray); + assertEquals("Alice", result); + + JSONArray multipleElementsArray = new JSONArray(); + multipleElementsArray.put("Alice"); + multipleElementsArray.put("Bob"); + multipleElementsArray.put("Charlie"); + result = JSONUtils.jsonArrayOfStringToString(multipleElementsArray); + assertEquals("Alice - Bob - Charlie", result); + } + + @Test + void testIsEmpty() { + JSONObject emptyObject = new JSONObject(); + assertTrue(JSONUtils.isEmpty(emptyObject)); + + JSONObject objWithEmptyValues = new JSONObject(); + objWithEmptyValues.put("key1", ""); + objWithEmptyValues.put("key2", ""); + assertTrue(JSONUtils.isEmpty(objWithEmptyValues)); + + JSONObject objWithNonEmptyValue = new JSONObject(); + objWithNonEmptyValue.put("key1", ""); + objWithNonEmptyValue.put("key2", "value"); + assertFalse(JSONUtils.isEmpty(objWithNonEmptyValue)); + + JSONObject objWithNonEmptyValues = new JSONObject(); + objWithNonEmptyValues.put("key1", "value1"); + objWithNonEmptyValues.put("key2", "value2"); + assertFalse(JSONUtils.isEmpty(objWithNonEmptyValues)); + } + + @Test + void testStreamConversion() { + JSONArray jsonArray = new JSONArray(); + jsonArray.put(new JSONObject().put("id", 1).put("name", "Alice")); + jsonArray.put(new JSONObject().put("id", 2).put("name", "Bob")); + jsonArray.put(new JSONObject().put("id", 3).put("name", "Charlie")); + + Stream stream = JSONUtils.stream(jsonArray); + + List result = stream.collect(Collectors.toList()); + + assertEquals(3, result.size()); + assertEquals("Alice", result.get(0).getString("name")); + assertEquals(1, result.get(0).getInt("id")); + + assertEquals("Bob", result.get(1).getString("name")); + assertEquals(2, result.get(1).getInt("id")); + + assertEquals("Charlie", result.get(2).getString("name")); + assertEquals(3, result.get(2).getInt("id")); + } + + @Test + void testJsonArrayToList() { + JSONArray jsonArray = new JSONArray(); + jsonArray.put("Alice"); + jsonArray.put("Bob"); + jsonArray.put("Charlie"); + + List result = JSONUtils.jsonArrayToList(jsonArray); + + // Vérifications + assertEquals(3, result.size()); + assertTrue(result.contains("Alice")); + assertTrue(result.contains("Bob")); + assertTrue(result.contains("Charlie")); + + assertEquals("Alice", result.get(0)); + assertEquals("Bob", result.get(1)); + assertEquals("Charlie", result.get(2)); + } +} \ No newline at end of file From 047a966577858b9f9117811e250552da4c5810d5 Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Tue, 19 Nov 2024 08:09:22 +0000 Subject: [PATCH 2/2] feat: review --- .../java/fr/insee/rmes/utils/JSONUtils.java | 6 +-- .../fr/insee/rmes/utils/JSONUtilsTest.java | 38 +++++-------------- 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/src/main/java/fr/insee/rmes/utils/JSONUtils.java b/src/main/java/fr/insee/rmes/utils/JSONUtils.java index 9e7008023..d4343fa4c 100644 --- a/src/main/java/fr/insee/rmes/utils/JSONUtils.java +++ b/src/main/java/fr/insee/rmes/utils/JSONUtils.java @@ -14,9 +14,7 @@ public class JSONUtils { public static JSONArray extractFieldToArray(JSONArray jsonA, String field) { JSONArray res = new JSONArray(); - for(Object o: jsonA){ - res.put(((JSONObject) o).getString(field)); - } + stream(jsonA).forEach(object -> res.put(object.getString(field))); return res; } @@ -63,7 +61,7 @@ public static List jsonArrayToList(JSONArray array) { return generateIntStreamBasedOnJsonArray(array) .mapToObj(array::get) .map(Object::toString) - .collect(Collectors.toList()); + .toList(); } private JSONUtils() { diff --git a/src/test/java/fr/insee/rmes/utils/JSONUtilsTest.java b/src/test/java/fr/insee/rmes/utils/JSONUtilsTest.java index 1a8ca5fa9..a890195d6 100644 --- a/src/test/java/fr/insee/rmes/utils/JSONUtilsTest.java +++ b/src/test/java/fr/insee/rmes/utils/JSONUtilsTest.java @@ -8,6 +8,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.junit.jupiter.api.Assertions.*; class JSONUtilsTest { @@ -72,23 +73,14 @@ void testIsEmpty() { @Test void testStreamConversion() { JSONArray jsonArray = new JSONArray(); - jsonArray.put(new JSONObject().put("id", 1).put("name", "Alice")); - jsonArray.put(new JSONObject().put("id", 2).put("name", "Bob")); - jsonArray.put(new JSONObject().put("id", 3).put("name", "Charlie")); - - Stream stream = JSONUtils.stream(jsonArray); - - List result = stream.collect(Collectors.toList()); - - assertEquals(3, result.size()); - assertEquals("Alice", result.get(0).getString("name")); - assertEquals(1, result.get(0).getInt("id")); - - assertEquals("Bob", result.get(1).getString("name")); - assertEquals(2, result.get(1).getInt("id")); - - assertEquals("Charlie", result.get(2).getString("name")); - assertEquals(3, result.get(2).getInt("id")); + JSONObject alice = new JSONObject().put("id", 1).put("name", "Alice"); + jsonArray.put(alice); + JSONObject bob = new JSONObject().put("id", 2).put("name", "Bob"); + jsonArray.put(bob); + JSONObject charlie = new JSONObject().put("id", 3).put("name", "Charlie"); + jsonArray.put(charlie); + + assertThat(JSONUtils.stream(jsonArray).toList()).isEqualTo(List.of(alice, bob, charlie)); } @Test @@ -98,16 +90,6 @@ void testJsonArrayToList() { jsonArray.put("Bob"); jsonArray.put("Charlie"); - List result = JSONUtils.jsonArrayToList(jsonArray); - - // Vérifications - assertEquals(3, result.size()); - assertTrue(result.contains("Alice")); - assertTrue(result.contains("Bob")); - assertTrue(result.contains("Charlie")); - - assertEquals("Alice", result.get(0)); - assertEquals("Bob", result.get(1)); - assertEquals("Charlie", result.get(2)); + assertThat(JSONUtils.jsonArrayToList(jsonArray)).isEqualTo(List.of("Alice", "Bob", "Charlie")); } } \ No newline at end of file