/** * Parse the expected and actual strings as JSON and assert the two * are "similar" - i.e. they contain the same attribute-value pairs * regardless of formatting. * <p>Can compare in two modes, depending on {@code strict} parameter value: * <ul> * <li>{@code true}: strict checking. Not extensible, and strict array ordering.</li> * <li>{@code false}: lenient checking. Extensible, and non-strict array ordering.</li> * </ul> * @param expected the expected JSON content * @param actual the actual JSON content * @param strict enables strict checking * @since 4.2 */ public void assertJsonEqual(String expected, String actual, boolean strict) throws Exception { JSONAssert.assertEquals(expected, actual, strict); }
/** * Parse the expected and actual strings as JSON and assert the two * are "not similar" - i.e. they contain different attribute-value pairs * regardless of formatting. * <p>Can compare in two modes, depending on {@code strict} parameter value: * <ul> * <li>{@code true}: strict checking. Not extensible, and strict array ordering.</li> * <li>{@code false}: lenient checking. Extensible, and non-strict array ordering.</li> * </ul> * @param expected the expected JSON content * @param actual the actual JSON content * @param strict enables strict checking * @since 4.2 */ public void assertJsonNotEqual(String expected, String actual, boolean strict) throws Exception { JSONAssert.assertNotEquals(expected, actual, strict); }
/** * Identify that the comparison failed * @param field Which field failed * @param exception exception containing details of match failure * @return result of comparision */ public JSONCompareResult fail(String field, ValueMatcherException exception) { fail(field + ": " + exception.getMessage(), exception.getExpected(), exception.getActual()); return this; }
/** * Compares JSONArray provided to the expected JSONArray, and returns the results of the comparison. * * @param expected Expected JSONArray * @param actual JSONArray to compare * @param mode Defines comparison behavior * @return result of the comparison * @throws JSONException JSON parsing error */ public static JSONCompareResult compareJSON(JSONArray expected, JSONArray actual, JSONCompareMode mode) throws JSONException { return compareJSON(expected, actual, getComparatorForMode(mode)); }
/** * Identify unexpected field * @param field unexpected field * @param actual actual result * @return result of comparison */ public JSONCompareResult unexpected(String field, Object actual) { _fieldUnexpected.add(new FieldComparisonFailure(field, null, actual)); fail(formatUnexpected(field, actual)); return this; }
/** * Identify the missing field * @param field missing field * @param expected expected result * @return result of comparison */ public JSONCompareResult missing(String field, Object expected) { _fieldMissing.add(new FieldComparisonFailure(field, expected, null)); fail(formatMissing(field, expected)); return this; }
private String formatMissing(String field, Object expected) { return field + "\nExpected: " + describe(expected) + "\n but none found\n"; }
private String buildPatternLevel1(String path) { String regex = "\\*\\*\\."; String replacement = "(?:.+\\.)?"; return buildPattern(path, regex, replacement, 1); }
private Customization getCustomization(String path) { for (Customization c : customizations) if (c.appliesToPath(path)) return c; return null; } }
/** * Creates a new {@link Customization} instance for {@code path} and {@code comparator}. * * @param path the json path * @param comparator the comparator * @return a new Customization */ public static Customization customization(String path, ValueMatcher<Object> comparator) { return new Customization(path, comparator); }
/** * Asserts that the JSONArray provided matches the expected string. If it isn't it throws an * {@link AssertionError}. * * @param expectedStr Expected JSON string * @param actualStr String to compare * @param strict Enables strict checking * @throws JSONException JSON parsing error */ public static void assertEquals(String expectedStr, String actualStr, boolean strict) throws JSONException { assertEquals(expectedStr, actualStr, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); }
/** * Compares JSONObject provided to the expected JSONObject, and returns the results of the comparison. * * @param expected Expected JSONObject * @param actual JSONObject to compare * @param mode Defines comparison behavior * @return result of the comparison * @throws JSONException JSON parsing error */ public static JSONCompareResult compareJSON(JSONObject expected, JSONObject actual, JSONCompareMode mode) throws JSONException { return compareJSON(expected, actual, getComparatorForMode(mode)); }
/** * Asserts that the JSONArray provided does not match the expected string. If it is it throws an * {@link AssertionError}. * * @param expectedStr Expected JSON string * @param actual JSONArray to compare * @param strict Enables strict checking * @throws JSONException JSON parsing error */ public static void assertNotEquals(String expectedStr, JSONArray actual, boolean strict) throws JSONException { assertNotEquals(expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); }
/** * Asserts that the JSONArray provided matches the expected JSONArray. If it isn't it throws an * {@link AssertionError}. * * @param expected Expected JSONArray * @param actual JSONArray to compare * @param strict Enables strict checking * @throws JSONException JSON parsing error */ public static void assertEquals(JSONArray expected, JSONArray actual, boolean strict) throws JSONException { assertEquals("", expected, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); }
private String formatUnexpected(String field, Object actual) { return field + "\nUnexpected: " + describe(actual) + "\n"; }
/** * Asserts that the JSONArray provided matches the expected JSONArray. If it isn't it throws an * {@link AssertionError}. * * @param expected Expected JSONArray * @param actual JSONArray to compare * @param compareMode Specifies which comparison mode to use * @throws JSONException JSON parsing error */ public static void assertEquals(JSONArray expected, JSONArray actual, JSONCompareMode compareMode) throws JSONException { assertEquals("", expected, actual, compareMode); }
/** * Asserts that the JSONObject provided matches the expected string. If it isn't it throws an * {@link AssertionError}. * * @param expectedStr Expected JSON string * @param actual JSONObject to compare * @param strict Enables strict checking * @throws JSONException JSON parsing error */ public static void assertEquals(String expectedStr, JSONObject actual, boolean strict) throws JSONException { assertEquals(expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); }
/** * Asserts that the JSONArray provided matches the expected string. If it isn't it throws an * {@link AssertionError}. * * @param expectedStr Expected JSON string * @param actual JSONArray to compare * @param strict Enables strict checking * @throws JSONException JSON parsing error */ public static void assertEquals(String expectedStr, JSONArray actual, boolean strict) throws JSONException { assertEquals(expectedStr, actual, strict ? JSONCompareMode.STRICT : JSONCompareMode.LENIENT); }
@Override public void accept(String s) { try { JSONAssert.assertEquals(this.expected, s, true); } catch (JSONException ex) { throw new RuntimeException(ex); } } }