private boolean isSimilar_strict_timezone(String expected, String actual) { return new JsonComparison().withTimezone().areSimilar(expected, actual); }
private boolean isSimilar_strict_array_order(String expected, String actual) { return new JsonComparison().withStrictArrayOrder().areSimilar(expected, actual); } }
private boolean compareArrays(JSONArray expected, JSONArray actual) { if (strictArrayOrder) { return compareArraysByStrictOrder(expected, actual); } return compareArraysByLenientOrder(expected, actual); }
private boolean compare(@Nullable Object expectedObject, @Nullable Object actualObject) { if (expectedObject == null) { return actualObject == null; } if (actualObject == null) { // expected non-null, got null return false; } if (expectedObject.getClass() != actualObject.getClass()) { return false; } if (expectedObject instanceof JSONArray) { return compareArrays((JSONArray) expectedObject, (JSONArray) actualObject); } if (expectedObject instanceof JSONObject) { return compareObjects((JSONObject) expectedObject, (JSONObject) actualObject); } if (expectedObject instanceof String) { return compareStrings((String) expectedObject, (String) actualObject); } if (expectedObject instanceof Number) { return compareNumbers((Number) expectedObject, (Number) actualObject); } return compareBooleans((Boolean) expectedObject, (Boolean) actualObject); }
@Test public void strict_timezone() { assertThat(new JsonComparison().withTimezone().isStrictTimezone()).isTrue(); // same instant, same timezone assertThat(isSimilar_strict_timezone("{\"foo\": \"2010-05-18T15:50:45+0100\"}", "{\"foo\": \"2010-05-18T15:50:45+0100\"}")).isTrue(); assertThat(isSimilar_strict_timezone("[\"2010-05-18T15:50:45+0100\"]", "[\"2010-05-18T15:50:45+0100\"]")).isTrue(); // same instant, but different timezone assertThat(isSimilar_strict_timezone("{\"foo\": \"2010-05-18T15:50:45+0100\"}", "{\"foo\": \"2010-05-18T18:50:45+0400\"}")).isFalse(); // different time assertThat(isSimilar_strict_timezone("{\"foo\": \"2010-05-18T15:50:45+0100\"}", "{\"foo\": \"2010-05-18T15:51:45+0100\"}")).isFalse(); }
private boolean compareObjects(JSONObject expectedMap, JSONObject actualMap) { // each key-value of expected map must exist in actual map for (Map.Entry<Object, Object> expectedEntry : (Set<Map.Entry<Object, Object>>) expectedMap.entrySet()) { Object key = expectedEntry.getKey(); if (shouldIgnoreField(key)) { continue; } if (!actualMap.containsKey(key)) { return false; } if (!compare(expectedEntry.getValue(), actualMap.get(key))) { return false; } } return true; }
private boolean areSimilar(String expected, String actual) { return new JsonComparison().areSimilar(expected, actual); }
public JsonAssert isSimilarTo(String expected) { boolean similar = comparison.areSimilar(expected, actualJson); if (!similar) { throw new ComparisonFailure("Not a super-set of expected JSON -", pretty(expected), pretty(actualJson)); } return this; }
public JsonAssert withStrictTimezone() { comparison.withTimezone(); return this; }
public JsonAssert withStrictArrayOrder() { comparison.withStrictArrayOrder(); return this; }
private boolean compareArraysByLenientOrder(JSONArray expected, JSONArray actual) { if (expected.size() > actual.size()) { return false; } List remainingActual = new ArrayList(actual); for (Object expectedElement : expected) { // element can be null boolean found = false; for (Object actualElement : remainingActual) { if (compare(expectedElement, actualElement)) { found = true; remainingActual.remove(actualElement); break; } } if (!found) { return false; } } return remainingActual.isEmpty(); }
private boolean compare(@Nullable Object expectedObject, @Nullable Object actualObject) { if (expectedObject == null) { return actualObject == null; } if (actualObject == null) { // expected non-null, got null return false; } if (expectedObject.getClass() != actualObject.getClass()) { return false; } if (expectedObject instanceof JSONArray) { return compareArrays((JSONArray) expectedObject, (JSONArray) actualObject); } if (expectedObject instanceof JSONObject) { return compareObjects((JSONObject) expectedObject, (JSONObject) actualObject); } if (expectedObject instanceof String) { return compareStrings((String) expectedObject, (String) actualObject); } if (expectedObject instanceof Number) { return compareNumbers((Number) expectedObject, (Number) actualObject); } return compareBooleans((Boolean) expectedObject, (Boolean) actualObject); }
private boolean compareArrays(JSONArray expected, JSONArray actual) { if (strictArrayOrder) { return compareArraysByStrictOrder(expected, actual); } return compareArraysByLenientOrder(expected, actual); }
private boolean compareObjects(JSONObject expectedMap, JSONObject actualMap) { // each key-value of expected map must exist in actual map for (Map.Entry<Object, Object> expectedEntry : (Set<Map.Entry<Object, Object>>) expectedMap.entrySet()) { Object key = expectedEntry.getKey(); if (shouldIgnoreField(key)) { continue; } if (!actualMap.containsKey(key)) { return false; } if (!compare(expectedEntry.getValue(), actualMap.get(key))) { return false; } } return true; }
public JsonAssert isSimilarTo(String expected) { boolean similar = comparison.areSimilar(expected, actualJson); if (!similar) { throw new ComparisonFailure("Not a super-set of expected JSON -", pretty(expected), pretty(actualJson)); } return this; }
public JsonAssert withStrictTimezone() { comparison.withTimezone(); return this; }
public JsonAssert withStrictArrayOrder() { comparison.withStrictArrayOrder(); return this; }
private boolean compareArraysByStrictOrder(JSONArray expected, JSONArray actual) { if (expected.size() != actual.size()) { return false; } for (int index = 0; index < expected.size(); index++) { Object expectedElt = expected.get(index); Object actualElt = actual.get(index); if (!compare(expectedElt, actualElt)) { return false; } } return true; }