Skip to content

Commit

Permalink
Reserialization utilities for loaded data
Browse files Browse the repository at this point in the history
  • Loading branch information
javiertuya committed Jul 3, 2024
1 parent 8549c6b commit 2fdaa7f
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package giis.tdrules.store.loader.oa;

import java.util.Iterator;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import giis.tdrules.store.loader.LoaderException;

/**
* Utilities to reorganize json strings containing serialized test data to
* facilitate the display and test result comparison. Note that the resulting
* strings are not valid json, only for display.
*/
public class Reserializer {
/**
* Returns a string containing the whole content of the data stored, suitable
* for display or test data comparison. Given a json string representing an
* object, where each property has a key with the entity name and a value with
* an array of the objects stored in this entity, produces a string where each
* line includes an entity name and a single object.
*/
public String reserializeData(String payload) {
StringBuilder sb = new StringBuilder();
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
try {
JsonNode entities = mapper.readTree(payload);
Iterator<String> entitiesIt = entities.fieldNames();
while (entitiesIt.hasNext()) {
String entity = entitiesIt.next();
JsonNode objects = entities.get(entity);
Iterator<JsonNode> objectsIt = objects.iterator();
while (objectsIt.hasNext()) { // a line for each object
JsonNode object = (JsonNode) objectsIt.next();
sb.append("\"" + entity + "\":" + object + "\n");
}
}
} catch (JsonProcessingException e) {
throw new LoaderException(e);
}
return sb.toString();
}

/**
* Returns a string containing a list of objects, suitable for display or test
* data comparison. Given a json string representing a list of objects, produces
* a string where each object is in a line
*/
public String reserializeList(String payload) {
StringBuilder sb = new StringBuilder();
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
try {
JsonNode objects = mapper.readTree(payload);
Iterator<JsonNode> objectsIt = objects.elements();
while (objectsIt.hasNext()) {
JsonNode object = objectsIt.next();
sb.append(object + "\n");
}
} catch (JsonProcessingException e) {
throw new LoaderException(e);
}
return sb.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package test4giis.tdrules.store.loader.oa;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import giis.tdrules.store.loader.oa.Reserializer;

public class TestReserialize {

@Test
public void testReserializeData() {
String output="{'ent1':[{'attr111':111, 'attr112':112}, {'attr121':121, 'attr122':122}], "
+ "'ent2':[], "
+ "'ent3':[{'attr311':311}, {}]"
+ "}";
String expected="'ent1':{'attr111':111,'attr112':112}\n"
+ "'ent1':{'attr121':121,'attr122':122}\n"
+ "'ent3':{'attr311':311}\n"
+ "'ent3':{}\n";
output = output.replace("'", "\"");
expected=expected.replace("'", "\"");
String actual=new Reserializer().reserializeData(output);
assertEquals(expected, actual);
}

@Test
public void testReserialize() {
String output="[{'attr111':111, 'attr112':112}, {}, {'attr121':121}]";
String expected="{'attr111':111,'attr112':112}\n"
+ "{}\n"
+ "{'attr121':121}\n";
output = output.replace("'", "\"");
expected=expected.replace("'", "\"");
String actual=new Reserializer().reserializeList(output);
assertEquals(expected, actual);
}

}

0 comments on commit 2fdaa7f

Please sign in to comment.