/** * Compares JSON string provided to the expected JSON string, and returns the results of the comparison. * * @param expectedStr Expected JSON string * @param actualStr JSON string to compare * @param mode Defines comparison behavior * @return result of the comparison * @throws JSONException JSON parsing error */ public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONCompareMode mode) throws JSONException { return compareJSON(expectedStr, actualStr, getComparatorForMode(mode)); }
/** * 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)); }
/** * 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)); }
@Override public boolean matches(Object item) { if (!(item instanceof String)) { ObjectMapper om = new ObjectMapper(); try { item = om.writeValueAsString(item); } catch (JsonProcessingException e) { failureMessage = e.getMessage(); return false; } } JSONCompareResult result; try { result = JSONCompare.compareJSON(expectedJson, item.toString(), JSONCompareMode.LENIENT); } catch (JSONException jex) { failureMessage = jex.getMessage(); return false; } if (result != null && result.failed()) { failureMessage = result.getMessage(); } return result != null && result.passed(); }
/** * Asserts that the JSONArray provided matches the expected string. If it isn't it throws an * {@link AssertionError}. * * @param message Error message to be displayed in case of assertion failure * @param expectedStr Expected JSON string * @param actualStr String to compare * @param compareMode Specifies which comparison mode to use * @throws JSONException JSON parsing error */ public static void assertEquals(String message, String expectedStr, String actualStr, JSONCompareMode compareMode) throws JSONException { if (expectedStr==actualStr) return; if (expectedStr==null){ throw new AssertionError("Expected string is null."); }else if (actualStr==null){ throw new AssertionError("Actual string is null."); } JSONCompareResult result = JSONCompare.compareJSON(expectedStr, actualStr, compareMode); if (result.failed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); } }
/** * Asserts that the JSONArray provided does not match the expected string. If it is it throws an * {@link AssertionError}. * * @param message Error message to be displayed in case of assertion failure * @param expectedStr Expected JSON string * @param actualStr String to compare * @param compareMode Specifies which comparison mode to use * @throws JSONException JSON parsing error */ public static void assertNotEquals(String message, String expectedStr, String actualStr, JSONCompareMode compareMode) throws JSONException { JSONCompareResult result = JSONCompare.compareJSON(expectedStr, actualStr, compareMode); if (result.passed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); } }
/** * Asserts that the json string provided matches the expected string. If it isn't it throws an * {@link AssertionError}. * * @param message Error message to be displayed in case of assertion failure * @param expectedStr Expected JSON string * @param actualStr String to compare * @param comparator Comparator * @throws JSONException JSON parsing error */ public static void assertEquals(String message, String expectedStr, String actualStr, JSONComparator comparator) throws JSONException { JSONCompareResult result = JSONCompare.compareJSON(expectedStr, actualStr, comparator); if (result.failed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); } }
/** * Asserts that the JSONObject provided does not match the expected JSONObject. If it is it throws an * {@link AssertionError}. * * @param message Error message to be displayed in case of assertion failure * @param expected Expected JSONObject * @param actual JSONObject to compare * @param compareMode Specifies which comparison mode to use * @throws JSONException JSON parsing error */ public static void assertNotEquals(String message, JSONObject expected, JSONObject actual, JSONCompareMode compareMode) throws JSONException { JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode); if (result.passed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); } }
/** * Asserts that the json string provided does not match the expected string. If it is it throws an * {@link AssertionError}. * * @param message Error message to be displayed in case of assertion failure * @param expectedStr Expected JSON string * @param actualStr String to compare * @param comparator Comparator * @throws JSONException JSON parsing error */ public static void assertNotEquals(String message, String expectedStr, String actualStr, JSONComparator comparator) throws JSONException { JSONCompareResult result = JSONCompare.compareJSON(expectedStr, actualStr, comparator); if (result.passed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); } }
/** * Asserts that the JSONArray provided does not match the expected JSONArray. If it is it throws an * {@link AssertionError}. * * @param message Error message to be displayed in case of assertion failure * @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 assertNotEquals(String message, JSONArray expected, JSONArray actual, JSONCompareMode compareMode) throws JSONException { JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode); if (result.passed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); } }
/** * Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws an * {@link AssertionError}. * * @param message Error message to be displayed in case of assertion failure * @param expected Expected JSONObject * @param actual JSONObject to compare * @param compareMode Specifies which comparison mode to use * @throws JSONException JSON parsing error */ public static void assertEquals(String message, JSONObject expected, JSONObject actual, JSONCompareMode compareMode) throws JSONException { JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode); if (result.failed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); } }
/** * Asserts that the JSONArray provided matches the expected JSONArray. If it isn't it throws an * {@link AssertionError}. * * @param message Error message to be displayed in case of assertion failure * @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(String message, JSONArray expected, JSONArray actual, JSONCompareMode compareMode) throws JSONException { JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode); if (result.failed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); } }
public boolean matches(final HttpRequest context, String matched) { boolean result = false; JSONCompareResult jsonCompareResult; try { if (Strings.isNullOrEmpty(matcher)) { result = true; } else { JSONCompareMode jsonCompareMode = JSONCompareMode.LENIENT; if (matchType == MatchType.STRICT) { jsonCompareMode = JSONCompareMode.STRICT; } jsonCompareResult = compareJSON(matcher, matched, jsonCompareMode); if (jsonCompareResult.passed()) { result = true; } if (!result) { mockServerLogger.trace(context, "Failed to perform JSON match \"{}\" with \"{}\" because {}", matched, this.matcher, jsonCompareResult.getMessage()); } } } catch (Exception e) { mockServerLogger.trace(context, "Failed to perform JSON match \"{}\" with \"{}\" because {}", matched, this.matcher, e.getMessage()); } return not != result; }
/** * Compares the item (which represents the actual result) against the expected value to see if they are the same JSON. These two * values * may not be equal as Strings, but the JSON comparison should ignore unnecessary whitespace and the differences between " and '. * * @param item a String containing valid JSON to compare against the expected value * @return true if the expected and actual values are equivalent JSON values */ @Override public boolean matchesSafely(final String item) { try { return JSONCompare.compareJSON(expectedJson, item, JSONCompareMode.STRICT).passed(); } catch (final JSONException e) { Assert.fail(String.format("JSON compare threw exception when trying to compare %n %s %n with %n %s%n Exception was: %n%s ", expectedJson, item, e)); return false; } }
/** * Compares JSON string provided to the expected JSON string using provided comparator, and returns the results of * the comparison. * @param expectedStr Expected JSON string * @param actualStr JSON string to compare * @param comparator Comparator to use * @return result of the comparison * @throws JSONException JSON parsing error * @throws IllegalArgumentException when type of expectedStr doesn't match the type of actualStr */ public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONComparator comparator) throws JSONException { Object expected = JSONParser.parseJSON(expectedStr); Object actual = JSONParser.parseJSON(actualStr); if ((expected instanceof JSONObject) && (actual instanceof JSONObject)) { return compareJSON((JSONObject) expected, (JSONObject) actual, comparator); } else if ((expected instanceof JSONArray) && (actual instanceof JSONArray)) { return compareJSON((JSONArray)expected, (JSONArray)actual, comparator); } else if (expected instanceof JSONString && actual instanceof JSONString) { return compareJson((JSONString) expected, (JSONString) actual); } else if (expected instanceof JSONObject) { return new JSONCompareResult().fail("", expected, actual); } else { return new JSONCompareResult().fail("", expected, actual); } }
/** * 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)); }
/** * 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)); }
/** * Compares JSON string provided to the expected JSON string, and returns the results of the comparison. * * @param expectedStr Expected JSON string * @param actualStr JSON string to compare * @param mode Defines comparison behavior * @return result of the comparison * @throws JSONException JSON parsing error */ public static JSONCompareResult compareJSON(String expectedStr, String actualStr, JSONCompareMode mode) throws JSONException { return compareJSON(expectedStr, actualStr, getComparatorForMode(mode)); }
/** * Asserts that the json string provided matches the expected string. If it isn't it throws an * {@link AssertionError}. * * @param message Error message to be displayed in case of assertion failure * @param expectedStr Expected JSON string * @param actualStr String to compare * @param comparator Comparator * @throws JSONException JSON parsing error */ public static void assertEquals(String message, String expectedStr, String actualStr, JSONComparator comparator) throws JSONException { JSONCompareResult result = JSONCompare.compareJSON(expectedStr, actualStr, comparator); if (result.failed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); } }
/** * Asserts that the JSONObject provided matches the expected JSONObject. If it isn't it throws an * {@link AssertionError}. * * @param message Error message to be displayed in case of assertion failure * @param expected Expected JSONObject * @param actual JSONObject to compare * @param compareMode Specifies which comparison mode to use * @throws JSONException JSON parsing error */ public static void assertEquals(String message, JSONObject expected, JSONObject actual, JSONCompareMode compareMode) throws JSONException { JSONCompareResult result = JSONCompare.compareJSON(expected, actual, compareMode); if (result.failed()) { throw new AssertionError(getCombinedMessage(message, result.getMessage())); } }