Skip to content

Commit

Permalink
feat: create a method to convert JSONArray to stream (#807)
Browse files Browse the repository at this point in the history
* feat: create a method to convert JSONArray to stream

* feat: review
  • Loading branch information
EmmanuelDemey authored Nov 19, 2024
1 parent aec0cdb commit a6334da
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
27 changes: 17 additions & 10 deletions src/main/java/fr/insee/rmes/utils/JSONUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
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){
res.put(((JSONObject) o).getString(field));
}
stream(jsonA).forEach(object -> res.put(object.getString(field)));
return res;
}

Expand Down Expand Up @@ -45,16 +45,23 @@ public static boolean isEmpty(JSONObject obj) {
return true;
}


private static IntStream generateIntStreamBasedOnJsonArray(JSONArray array) {
return IntStream.range(0, array.length());
}

public static Stream<JSONObject> stream(JSONArray array) {
return generateIntStreamBasedOnJsonArray(array).mapToObj(array::getJSONObject);
}

/**
* Transform an array to a list of strings
* @param jsonArray
* @return
*/
public static List<String> jsonArrayToList(JSONArray jsonArray) {
return IntStream.range(0, jsonArray.length())
.mapToObj(jsonArray::get)
public static List<String> jsonArrayToList(JSONArray array) {
return generateIntStreamBasedOnJsonArray(array)
.mapToObj(array::get)
.map(Object::toString)
.collect(Collectors.toList());
.toList();
}

private JSONUtils() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}";


Expand Down
95 changes: 95 additions & 0 deletions src/test/java/fr/insee/rmes/utils/JSONUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
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.assertj.core.api.AssertionsForClassTypes.assertThat;
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();
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
void testJsonArrayToList() {
JSONArray jsonArray = new JSONArray();
jsonArray.put("Alice");
jsonArray.put("Bob");
jsonArray.put("Charlie");

assertThat(JSONUtils.jsonArrayToList(jsonArray)).isEqualTo(List.of("Alice", "Bob", "Charlie"));
}
}

0 comments on commit a6334da

Please sign in to comment.