|
| 1 | +package org.skyscreamer.jsonassert; |
| 2 | + |
| 3 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 4 | +import static org.junit.Assert.assertThat; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | +import static org.skyscreamer.jsonassert.JSONCompare.compareJSON; |
| 7 | +import static org.skyscreamer.jsonassert.JSONCompareMode.LENIENT; |
| 8 | +import static org.skyscreamer.jsonassert.JSONCompareMode.NON_EXTENSIBLE; |
| 9 | + |
| 10 | +import org.hamcrest.Description; |
| 11 | +import org.hamcrest.Matcher; |
| 12 | +import org.json.JSONException; |
| 13 | +import org.junit.Ignore; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.internal.matchers.TypeSafeMatcher; |
| 16 | + |
| 17 | +/** |
| 18 | + * Unit tests for {@code JSONCompare}. |
| 19 | + */ |
| 20 | +public class JSONCompareTest { |
| 21 | + @Test |
| 22 | + public void reportsWrongSimpleValueCountInUnorderedArray() throws JSONException { |
| 23 | + JSONCompareResult result = compareJSON("[5, 5]", "[5, 7]", LENIENT); |
| 24 | + assertThat(result, failsWithMessage(equalTo("[]: Expected contains 2 5 actual contains 1 ; []: Contains 7, but not expected"))); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void reportsMissingJSONObjectWithUniqueKeyInUnorderedArray() throws JSONException { |
| 29 | + JSONCompareResult result = compareJSON("[{\"id\" : 3}]", "[{\"id\" : 5}]", LENIENT); |
| 30 | + assertThat(result, failsWithMessage(equalTo("[]: Expected but did not find object where id=3 ; " + |
| 31 | + "[]: Contains object where \" + uniqueKey + \"=\" + id + \", but not expected"))); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void reportsUnmatchedJSONObjectInUnorderedArray() throws JSONException { |
| 36 | + JSONCompareResult result = compareJSON("[{\"address\" : {\"street\" : \"Acacia Avenue\"}}]", "[{\"age\" : 23}]", LENIENT); |
| 37 | + assertThat(result, failsWithMessage(equalTo("Could not find match for element {\"age\":23}"))); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void succeedsWithNestedJSONObjectsInUnorderedArray() throws JSONException { |
| 42 | + assertTrue(compareJSON("[{\"address\" : {\"street\" : \"Acacia Avenue\"}}, 5]", "[5, {\"address\" : {\"street\" : \"Acacia Avenue\"}}]", LENIENT).passed()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void succeedsWithJSONObjectsWithNonUniqueKeyInUnorderedArray() throws JSONException { |
| 47 | + String jsonDocument = "[{\"age\" : 43}, {\"age\" : 43}]"; |
| 48 | + assertTrue(compareJSON(jsonDocument, jsonDocument, LENIENT).passed()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void succeedsWithSomeNestedJSONObjectsInUnorderedArray() throws JSONException { |
| 53 | + String jsonDocument = "[{\"age\" : 43}, {\"age\" : {\"years\" : 43}}]"; |
| 54 | + assertTrue(compareJSON(jsonDocument, jsonDocument, LENIENT).passed()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void reportsUnmatchesIntegerValueInUnorderedArrayContainingJSONObject() throws JSONException { |
| 59 | + JSONCompareResult result = compareJSON("[{\"address\" : {\"street\" : \"Acacia Avenue\"}}, 5]", "[{\"address\" : {\"street\" : \"Acacia Avenue\"}}, 2]", LENIENT); |
| 60 | + assertThat(result, failsWithMessage(equalTo("Could not find match for element 2"))); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @Ignore // currently failing |
| 65 | + public void failing() throws JSONException { |
| 66 | + compareJSON("[{\"id\": 3}]", "[{}]", NON_EXTENSIBLE); |
| 67 | + } |
| 68 | + |
| 69 | + private Matcher<JSONCompareResult> failsWithMessage(final Matcher<String> expectedMessage) { |
| 70 | + return new TypeSafeMatcher<JSONCompareResult>() { |
| 71 | + @Override |
| 72 | + public void describeTo(Description description) { |
| 73 | + description.appendText("a failed comparison with message ").appendDescriptionOf(expectedMessage); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean matchesSafely(JSONCompareResult item) { |
| 78 | + return item.failed() && expectedMessage.matches(item.getMessage()); |
| 79 | + } |
| 80 | + }; |
| 81 | + } |
| 82 | +} |
0 commit comments