added additional examples
This commit is contained in:
parent
f13b1e78ef
commit
0eeb8a37f5
|
@ -18,10 +18,39 @@ public class JsonCompareUnitTest {
|
||||||
public void givenTwoSameJsonDataObjects_whenCompared_thenAreEqual() throws IOException {
|
public void givenTwoSameJsonDataObjects_whenCompared_thenAreEqual() throws IOException {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
String jsonString1 = "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\",\"k4\":\"v4\"}";
|
String s1 = "{\"employee\": {\"id\": \"1212\",\"fullName\": \"John Miles\",\"age\": 34 }}";
|
||||||
String jsonString2 = "{\"k1\":\"v1\",\"k2\":\"v2\",\"k3\":\"v3\",\"k4\":\"v4\"}";
|
String s2 = "{\"employee\": {\"id\": \"1212\",\"fullName\": \"John Miles\",\"age\": 34 }}";
|
||||||
JsonNode actualObj1 = mapper.readTree(jsonString1);
|
|
||||||
JsonNode actualObj2 = mapper.readTree(jsonString2);
|
JsonNode actualObj1 = mapper.readTree(s1);
|
||||||
|
JsonNode actualObj2 = mapper.readTree(s2);
|
||||||
|
|
||||||
|
assertTrue(actualObj1.equals(actualObj2));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoSameNestedJsonDataObjects_whenCompared_thenAreEqual() throws IOException {
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
String s1 = "{\"employee\": {\"id\": \"1212\",\"fullName\": \"John Miles\",\"age\": 34,\n" + "\"contact\":{\"email\": \"john@xyz.com\",\"phone\": \"9999999999\"} }}";
|
||||||
|
String s2 = "{\"employee\": {\"id\": \"1212\",\"fullName\": \"John Miles\",\"age\": 34,\n" + "\"contact\":{\"email\": \"john@xyz.com\",\"phone\": \"9999999999\"} }}";
|
||||||
|
|
||||||
|
JsonNode actualObj1 = mapper.readTree(s1);
|
||||||
|
JsonNode actualObj2 = mapper.readTree(s2);
|
||||||
|
|
||||||
|
assertTrue(actualObj1.equals(actualObj2));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTwoSameListJsonDataObjects_whenCompared_thenAreEqual() throws IOException {
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
String s1 = "{\"employee\": {\"id\": \"1212\",\"fullName\": \"John Miles\",\"age\": 34, \"skills\":[\"Java\", \"C++\", \"Python\"] }}";
|
||||||
|
String s2 = "{\"employee\": {\"id\": \"1212\",\"fullName\": \"John Miles\",\"age\": 34, \"skills\":[\"Java\", \"C++\", \"Python\"] }}";
|
||||||
|
|
||||||
|
JsonNode actualObj1 = mapper.readTree(s1);
|
||||||
|
JsonNode actualObj2 = mapper.readTree(s2);
|
||||||
|
|
||||||
assertTrue(actualObj1.equals(actualObj2));
|
assertTrue(actualObj1.equals(actualObj2));
|
||||||
|
|
||||||
|
@ -31,12 +60,11 @@ public class JsonCompareUnitTest {
|
||||||
public void givenTwoJsonDataObjects_whenComparedUsingCustomComparator_thenEqual() throws Exception {
|
public void givenTwoJsonDataObjects_whenComparedUsingCustomComparator_thenEqual() throws Exception {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
String jsonString1 = "{\"k1\": 5,\"k2\":9191}";
|
String s1 = "{\"name\": \"John\",\"score\":5.0}";
|
||||||
String jsonString2 = "{\"k1\": 5.0,\"k2\":9191}";
|
String s2 = "{\"name\": \"John\",\"score\":5}";
|
||||||
JsonNode actualObj1 = mapper.readTree(jsonString1);
|
JsonNode actualObj1 = mapper.readTree(s1);
|
||||||
JsonNode actualObj2 = mapper.readTree(jsonString2);
|
JsonNode actualObj2 = mapper.readTree(s2);
|
||||||
|
|
||||||
assertFalse(actualObj1.equals(actualObj2));
|
|
||||||
|
|
||||||
Comparator<JsonNode> cmp = new Comparator<JsonNode>() {
|
Comparator<JsonNode> cmp = new Comparator<JsonNode>() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -55,6 +83,7 @@ public class JsonCompareUnitTest {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
assertFalse(actualObj1.equals(actualObj2));
|
||||||
assertTrue(actualObj1.equals(cmp, actualObj2));
|
assertTrue(actualObj1.equals(cmp, actualObj2));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue