From 2fdaa7f441538382a7ad8195820013623d20e9a8 Mon Sep 17 00:00:00 2001 From: Javier <10879637+javiertuya@users.noreply.github.com> Date: Wed, 3 Jul 2024 11:52:44 +0200 Subject: [PATCH] Reserialization utilities for loaded data --- .../tdrules/store/loader/oa/Reserializer.java | 67 +++++++++++++++++++ .../store/loader/oa/TestReserialize.java | 39 +++++++++++ 2 files changed, 106 insertions(+) create mode 100644 tdrules-store-loader/src/main/java/giis/tdrules/store/loader/oa/Reserializer.java create mode 100644 tdrules-store-loader/src/test/java/test4giis/tdrules/store/loader/oa/TestReserialize.java diff --git a/tdrules-store-loader/src/main/java/giis/tdrules/store/loader/oa/Reserializer.java b/tdrules-store-loader/src/main/java/giis/tdrules/store/loader/oa/Reserializer.java new file mode 100644 index 00000000..62e63739 --- /dev/null +++ b/tdrules-store-loader/src/main/java/giis/tdrules/store/loader/oa/Reserializer.java @@ -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 entitiesIt = entities.fieldNames(); + while (entitiesIt.hasNext()) { + String entity = entitiesIt.next(); + JsonNode objects = entities.get(entity); + Iterator 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 objectsIt = objects.elements(); + while (objectsIt.hasNext()) { + JsonNode object = objectsIt.next(); + sb.append(object + "\n"); + } + } catch (JsonProcessingException e) { + throw new LoaderException(e); + } + return sb.toString(); + } + +} diff --git a/tdrules-store-loader/src/test/java/test4giis/tdrules/store/loader/oa/TestReserialize.java b/tdrules-store-loader/src/test/java/test4giis/tdrules/store/loader/oa/TestReserialize.java new file mode 100644 index 00000000..0d9b5e7a --- /dev/null +++ b/tdrules-store-loader/src/test/java/test4giis/tdrules/store/loader/oa/TestReserialize.java @@ -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); + } + +}