forked from skyscreamer/JSONassert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONCompareTest.java
More file actions
152 lines (128 loc) · 6.84 KB
/
JSONCompareTest.java
File metadata and controls
152 lines (128 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package org.skyscreamer.jsonassert;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.skyscreamer.jsonassert.JSONCompare.compareJSON;
import static org.skyscreamer.jsonassert.JSONCompareMode.LENIENT;
import static org.skyscreamer.jsonassert.JSONCompareMode.NON_EXTENSIBLE;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.json.JSONException;
import org.junit.Test;
import org.junit.internal.matchers.TypeSafeMatcher;
/**
* Unit tests for {@code JSONCompare}.
*/
public class JSONCompareTest {
@Test
public void succeedsWithEmptyArrays() throws JSONException {
assertTrue(compareJSON("[]", "[]", LENIENT).passed());
}
@Test
public void reportsArraysOfUnequalLength() throws JSONException {
JSONCompareResult result = compareJSON("[4]", "[]", LENIENT);
assertThat(result, failsWithMessage(equalTo("[]: Expected 1 values but got 0")));
}
@Test
public void reportsArrayMissingExpectedElement() throws JSONException {
JSONCompareResult result = compareJSON("[4]", "[7]", LENIENT);
assertThat(result, failsWithMessage(equalTo("[]\nExpected: 4\n but none found\n ; []\nUnexpected: 7\n")));
}
@Test
public void reportsMismatchedFieldValues() throws JSONException {
JSONCompareResult result = compareJSON("{\"id\": 3}", "{\"id\": 5}", LENIENT);
assertThat(result, failsWithMessage(equalTo("id\nExpected: 3\n got: 5\n")));
}
@Test
public void reportsMissingField() throws JSONException {
JSONCompareResult result = compareJSON("{\"obj\": {\"id\": 3}}", "{\"obj\": {}}", LENIENT);
assertThat(result, failsWithMessage(equalTo("obj\nExpected: id\n but none found\n")));
}
@Test
public void reportsUnexpectedArrayWhenExpectingObject() throws JSONException {
JSONCompareResult result = compareJSON("{}", "[]", LENIENT);
assertThat(result, failsWithMessage(equalTo("\nExpected: a JSON object\n got: a JSON array\n")));
}
@Test
public void reportsUnexpectedObjectWhenExpectingArray() throws JSONException {
JSONCompareResult result = compareJSON("[]", "{}", LENIENT);
assertThat(result, failsWithMessage(equalTo("\nExpected: a JSON array\n got: a JSON object\n")));
}
@Test
public void reportsUnexpectedNull() throws JSONException {
JSONCompareResult result = compareJSON("{\"id\": 3}", "{\"id\": null}", LENIENT);
assertThat(result, failsWithMessage(equalTo("id\nExpected: 3\n got: null\n")));
}
@Test
public void reportsUnexpectedNonNull() throws JSONException {
JSONCompareResult result = compareJSON("{\"id\": null}", "{\"id\": \"abc\"}", LENIENT);
assertThat(result, failsWithMessage(equalTo("id\nExpected: null\n got: abc\n")));
}
@Test
public void reportsUnexpectedFieldInNonExtensibleMode() throws JSONException {
JSONCompareResult result = compareJSON("{\"obj\": {}}", "{\"obj\": {\"id\": 3}}", NON_EXTENSIBLE);
assertThat(result, failsWithMessage(equalTo("obj\nUnexpected: id\n")));
}
@Test
public void reportsMismatchedTypes() throws JSONException {
JSONCompareResult result = compareJSON("{\"arr\":[]}", "{\"arr\":{}}", LENIENT);
assertThat(result, failsWithMessage(equalTo("arr\nExpected: a JSON array\n got: a JSON object\n")));
}
@Test
public void reportsWrongSimpleValueCountInUnorderedArray() throws JSONException {
JSONCompareResult result = compareJSON("[5, 5]", "[5, 7]", LENIENT);
assertThat(result, failsWithMessage(equalTo("[]: Expected 2 occurrence(s) of 5 but got 1 occurrence(s) ; []\nUnexpected: 7\n")));
}
@Test
public void reportsMissingJSONObjectWithUniqueKeyInUnorderedArray() throws JSONException {
JSONCompareResult result = compareJSON("[{\"id\" : 3}]", "[{\"id\" : 5}]", LENIENT);
assertThat(result, failsWithMessage(equalTo("[id=3]\nExpected: a JSON object\n but none found\n ; " +
"[id=5]\nUnexpected: a JSON object\n")));
}
@Test
public void reportsUnmatchedJSONObjectInUnorderedArray() throws JSONException {
JSONCompareResult result = compareJSON("[{\"address\" : {\"street\" : \"Acacia Avenue\"}}]", "[{\"age\" : 23}]", LENIENT);
assertThat(result, failsWithMessage(equalTo("[0] Could not find match for element {\"address\":{\"street\":\"Acacia Avenue\"}}")));
}
@Test
public void succeedsWithNestedJSONObjectsInUnorderedArray() throws JSONException {
assertTrue(compareJSON("[{\"address\" : {\"street\" : \"Acacia Avenue\"}}, 5]", "[5, {\"address\" : {\"street\" : \"Acacia Avenue\"}}]", LENIENT).passed());
}
@Test
public void succeedsWithJSONObjectsWithNonUniqueKeyInUnorderedArray() throws JSONException {
String jsonDocument = "[{\"age\" : 43}, {\"age\" : 43}]";
assertTrue(compareJSON(jsonDocument, jsonDocument, LENIENT).passed());
}
@Test
public void succeedsWithSomeNestedJSONObjectsInUnorderedArray() throws JSONException {
String jsonDocument = "[{\"age\" : 43}, {\"age\" : {\"years\" : 43}}]";
assertTrue(compareJSON(jsonDocument, jsonDocument, LENIENT).passed());
}
@Test
public void reportsUnmatchesIntegerValueInUnorderedArrayContainingJSONObject() throws JSONException {
JSONCompareResult result = compareJSON("[{\"address\" : {\"street\" : \"Acacia Avenue\"}}, 5]", "[{\"address\" : {\"street\" : \"Acacia Avenue\"}}, 2]", LENIENT);
assertThat(result, failsWithMessage(equalTo("[1] Could not find match for element 5")));
}
@Test
public void reportsUnmatchedJSONArrayWhereOnlyExpectedContainsJSONObjectWithUniqueKey() throws JSONException {
JSONCompareResult result = compareJSON("[{\"id\": 3}]", "[{}]", LENIENT);
assertThat(result, failsWithMessage(equalTo("[0] Could not find match for element {\"id\":3}")));
}
@Test
public void reportsUnmatchedJSONArrayWhereExpectedContainsJSONObjectWithUniqueKeyButActualContainsElementOfOtherType() throws JSONException {
JSONCompareResult result = compareJSON("[{\"id\": 3}]", "[5]", LENIENT);
assertThat(result, failsWithMessage(equalTo("[0] Could not find match for element {\"id\":3}")));
}
private Matcher<JSONCompareResult> failsWithMessage(final Matcher<String> expectedMessage) {
return new TypeSafeMatcher<JSONCompareResult>() {
@Override
public void describeTo(Description description) {
description.appendText("a failed comparison with message ").appendDescriptionOf(expectedMessage);
}
@Override
public boolean matchesSafely(JSONCompareResult item) {
return item.failed() && expectedMessage.matches(item.getMessage());
}
};
}
}