JsonUtil { public static boolean equals(final String expectedJson, final String actualJson) { try { final Map expectedSchemaMap = null != expectedJson ? OBJECT_MAPPER.readValue(expectedJson, Map.class) : Collections.emptyMap(); final Map actualSchemaMap = null != actualJson ? OBJECT_MAPPER.readValue(actualJson, Map.class) : Collections.emptyMap(); return Objects.equals(expectedSchemaMap, actualSchemaMap); } catch (final IOException e) { } try { final List expectedSchemaMap = null != expectedJson ? OBJECT_MAPPER.readValue(expectedJson, List.class) : Collections.emptyList(); final List actualSchemaMap = null != actualJson ? OBJECT_MAPPER.readValue(actualJson, List.class) : Collections.emptyList(); return Objects.equals(expectedSchemaMap, actualSchemaMap); } catch (final IOException e) { logError(expectedJson, actualJson, e); return false; } } private  JsonUtil(); static boolean equals(final String expectedJson, final String actualJson); static boolean equals(final byte[] expectedJson, final byte[] actualJson);  }
@Test public void shouldReturnTrueWhenJsonObjectsAreEqualButInADifferentOrder() { final String json1 = "{\"a\": 1, \"b\": 2}"; final String json2 = "{\"b\": 2, \"a\": 1}"; assertTrue(JsonUtil.equals(json1, json2)); assertTrue(JsonUtil.equals(json1.getBytes(), json2.getBytes())); JsonAssert.assertEquals(json1, json2); JsonAssert.assertEquals(json1.getBytes(), json2.getBytes()); }
@Test public void shouldReturnFalseWhenJsonObjectsAreDifferentSizes() { final String json1 = "{\"a\": 1, \"b\": 2}"; final String json2 = "{\"a\": 1, \"b\": 2, \"c\": 3}"; assertFalse(JsonUtil.equals(json1, json2)); assertFalse(JsonUtil.equals(json1.getBytes(), json2.getBytes())); JsonAssert.assertNotEqual(json1, json2); JsonAssert.assertNotEqual(json1.getBytes(), json2.getBytes()); }
@Test public void shouldReturnFalseWhenJsonObjectsAreNotEqual() { final String json1 = "{\"a\": 1, \"b\": 2}"; final String json2 = "{\"a\": 1, \"b\": 3}"; assertFalse(JsonUtil.equals(json1, json2)); assertFalse(JsonUtil.equals(json1.getBytes(), json2.getBytes())); JsonAssert.assertNotEqual(json1, json2); JsonAssert.assertNotEqual(json1.getBytes(), json2.getBytes()); }
@Test public void shouldReturnTrueWhenJsonArraysAreEqual() { final String json1 = "[1,2,3]"; final String json2 = "[1,2,3]"; assertTrue(JsonUtil.equals(json1, json2)); assertTrue(JsonUtil.equals(json1.getBytes(), json2.getBytes())); JsonAssert.assertEquals(json1, json2); JsonAssert.assertEquals(json1.getBytes(), json2.getBytes()); }
@Test public void shouldReturnFalseWhenJsonArraysAreNotEqual() { final String json1 = "[1,2,3]"; final String json2 = "[1,2,4]"; assertFalse(JsonUtil.equals(json1, json2)); assertFalse(JsonUtil.equals(json1.getBytes(), json2.getBytes())); JsonAssert.assertNotEqual(json1, json2); JsonAssert.assertNotEqual(json1.getBytes(), json2.getBytes()); }
@Test public void shouldReturnFalseWhenJsonArraysAreDifferentSizes() { final String json1 = "[1,2,3]"; final String json2 = "[1,2,3,4]"; assertFalse(JsonUtil.equals(json1, json2)); assertFalse(JsonUtil.equals(json1.getBytes(), json2.getBytes())); JsonAssert.assertNotEqual(json1, json2); JsonAssert.assertNotEqual(json1.getBytes(), json2.getBytes()); }
@Test public void shouldReturnFalseWhenActualObjectIsNull() { final String json1 = "{\"a\": 1, \"b\": 2}"; assertFalse(JsonUtil.equals(json1, null)); assertFalse(JsonUtil.equals(json1.getBytes(), null)); JsonAssert.assertNotEqual(json1, null); JsonAssert.assertNotEqual(json1.getBytes(), null); }