Skip to content

Commit 0a1d1ed

Browse files
committed
Added compareJson() for comparing to objects of the same type.
1 parent 9997604 commit 0a1d1ed

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/test/java/org/gitlab4j/api/JsonUtils.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ static <T> Map<String, T> unmarshalMap(Class<T> returnType, String json) throws
6565
return (jacksonJson.unmarshalMap(returnType, json));
6666
}
6767

68-
6968

7069
static <T> boolean compareJson(T apiObject, String filename) throws IOException {
7170
InputStreamReader reader = new InputStreamReader(TestGitLabApiBeans.class.getResourceAsStream(filename));
@@ -88,7 +87,23 @@ static <T> boolean compareJson(T apiObject, InputStreamReader reader) throws IOE
8887
return (sameJson);
8988
}
9089

91-
90+
static <T> boolean compareJson(T apiObject, T apiObject1) throws IOException {
91+
92+
String objectJson = jacksonJson.marshal(apiObject);
93+
String object1Json = jacksonJson.marshal(apiObject1);
94+
JsonNode tree1 = jacksonJson.getObjectMapper().readTree(objectJson.getBytes());
95+
JsonNode tree2 = jacksonJson.getObjectMapper().readTree(object1Json.getBytes());
96+
97+
boolean sameJson = tree1.equals(tree2);
98+
if (!sameJson) {
99+
System.err.println("JSON did not match:");
100+
sortedDump(tree1);
101+
sortedDump(tree2);
102+
}
103+
104+
return (sameJson);
105+
}
106+
92107
static void sortedDump(final JsonNode node) throws JsonProcessingException {
93108
final Object obj = jacksonJson.getObjectMapper().treeToValue(node, Object.class);
94109
System.err.println(jacksonJson.getObjectMapper().writeValueAsString(obj));

0 commit comments

Comments
 (0)