generated from giis-uniovi/samples-giis-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reserialization utilities for loaded data
- Loading branch information
1 parent
8549c6b
commit 2fdaa7f
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
tdrules-store-loader/src/main/java/giis/tdrules/store/loader/oa/Reserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
tdrules-store-loader/src/test/java/test4giis/tdrules/store/loader/oa/TestReserialize.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |