Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create a method to convert JSONArray to stream #807

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 15 additions & 6 deletions src/main/java/fr/insee/rmes/utils/JSONUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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){
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -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<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());
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
}
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
113 changes: 113 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,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() {
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved
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<JSONObject> stream = JSONUtils.stream(jsonArray);

List<JSONObject> 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<String> result = JSONUtils.jsonArrayToList(jsonArray);
EmmanuelDemey marked this conversation as resolved.
Show resolved Hide resolved

// 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));
}
}