forked from skyscreamer/JSONassert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayValueMatcherTest.java
More file actions
284 lines (246 loc) · 13.6 KB
/
ArrayValueMatcherTest.java
File metadata and controls
284 lines (246 loc) · 13.6 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.skyscreamer.jsonassert;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.text.MessageFormat;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.skyscreamer.jsonassert.comparator.ArraySizeComparator;
import org.skyscreamer.jsonassert.comparator.CustomComparator;
import org.skyscreamer.jsonassert.comparator.DefaultComparator;
import org.skyscreamer.jsonassert.comparator.JSONComparator;
/**
* Unit tests for ArrayValueMatcher
*
* @author Duncan Mackinder
*
*/
public class ArrayValueMatcherTest {
private static final String ARRAY_OF_JSONOBJECTS = "{a:[{background:white,id:1,type:row},{background:grey,id:2,type:row},{background:white,id:3,type:row},{background:grey,id:4,type:row}]}";
private static final String ARRAY_OF_INTEGERS = "{a:[1,2,3,4,5]}";
private static final String ARRAY_OF_JSONARRAYS = "{a:[[6,7,8],[9,10,11],[12,13,14],[19,20,21,22]]}";
private static final JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT);
private void doTest(String jsonPath, ArrayValueMatcher<Object> arrayValueMatcher, String expectedJSON,
String actualJSON) throws JSONException {
Customization customization = new Customization(jsonPath, arrayValueMatcher);
JSONAssert.assertEquals(expectedJSON, actualJSON, new CustomComparator(JSONCompareMode.LENIENT, customization));
}
private void doFailingMatchTest(String jsonPath, ArrayValueMatcher<Object> arrayValueMatcher, String expectedJSON, String actualJSON, String expectedMessagePattern) throws JSONException {
try {
doTest(jsonPath, arrayValueMatcher, expectedJSON, actualJSON);
}
catch (AssertionError e) {
String failureMessage = MessageFormat.format("Exception message ''{0}'', does not match expected pattern ''{1}''", e.getMessage(), expectedMessagePattern);
assertTrue(failureMessage, e.getMessage().matches(expectedMessagePattern));
return;
}
fail("AssertionError not thrown");
}
@Test
public void matchesSecondElementOfJSONObjectArray() throws JSONException {
doTest("a", new ArrayValueMatcher<Object>(comparator, 1), "{a:[{background:grey,id:2,type:row}]}", ARRAY_OF_JSONOBJECTS);
}
@Test
public void failsWhenSecondElementOfJSONObjectArrayDoesNotMatch() throws JSONException {
doFailingMatchTest("a",
new ArrayValueMatcher<Object>(comparator, 1),
"{a:[{background:DOES_NOT_MATCH,id:2,type:row}]}",
ARRAY_OF_JSONOBJECTS,
"a\\[1\\]\\.background\\s*Expected:\\s*DOES_NOT_MATCH\\s*got:\\s*grey\\s*");
}
@Test
public void failsWhenThirdElementOfJSONObjectArrayDoesNotMatchInMultiplePlaces() throws JSONException {
doFailingMatchTest("a",
new ArrayValueMatcher<Object>(comparator, 2),
"{a:[{background:DOES_NOT_MATCH,id:3,type:WRONG_TYPE}]}",
ARRAY_OF_JSONOBJECTS,
"a\\[2\\]\\.background\\s*Expected:\\s*DOES_NOT_MATCH\\s*got:\\s*white\\s*;\\s*a\\[2\\]\\.type\\s*Expected:\\s*WRONG_TYPE\\s*got:\\s*row\\s*");
}
@Test
public void failsWhenTwoElementsOfJSONObjectArrayDoNotMatch() throws JSONException {
doFailingMatchTest("a",
new ArrayValueMatcher<Object>(comparator, 1, 2),
"{a:[{background:DOES_NOT_MATCH,id:2,type:row},{background:white,id:3,type:WRONG_TYPE}]}",
ARRAY_OF_JSONOBJECTS,
"a\\[1\\]\\.background\\s*Expected:\\s*DOES_NOT_MATCH\\s*got:\\s*grey\\s*;\\s*a\\[2\\]\\.type\\s*Expected:\\s*WRONG_TYPE\\s*got:\\s*row\\s*");
}
@Test
public void matchesThirdElementOfSimpleValueArray() throws JSONException {
doTest("a", new ArrayValueMatcher<Object>(comparator, 2), "{a:[3]}", ARRAY_OF_INTEGERS);
}
@Test
public void failsWhenTwoElementOfSimpleValueArrayDoNotMatch() throws JSONException {
doFailingMatchTest("a", new ArrayValueMatcher<Object>(comparator, 3, 4), "{a:[3,4]}", ARRAY_OF_INTEGERS,
"a\\[3\\]\\s*Expected:\\s3\\s*got:\\s*4\\s*;\\s*a\\[4\\]\\s*Expected:\\s*4\\s*got:\\s*5\\s*");
}
@Test
public void matchesFirstElementOfArrayOfJSONArrays() throws JSONException {
doTest("a", new ArrayValueMatcher<Object>(comparator, 0), "{a:[[6,7,8]]}", ARRAY_OF_JSONARRAYS);
}
@Test
public void matchesSizeOfFirstThreeInnerArrays() throws JSONException {
JSONComparator innerArraySizeComparator = new ArraySizeComparator(JSONCompareMode.STRICT_ORDER);
doTest("a", new ArrayValueMatcher<Object>(innerArraySizeComparator, 0, 2), "{a:[[3]]}", ARRAY_OF_JSONARRAYS);
}
@Test
public void failsWhenInnerArraySizeDoesNotMatch() throws JSONException {
JSONComparator innerArraySizeComparator = new ArraySizeComparator(JSONCompareMode.STRICT_ORDER);
doFailingMatchTest("a",
new ArrayValueMatcher<Object>(innerArraySizeComparator),
"{a:[[3]]}",
ARRAY_OF_JSONARRAYS,
"a\\[3\\]\\[\\]\\s*Expected:\\s*array size of 3 elements\\s*got:\\s*4 elements\\s*");
}
@Test
public void failsWhenInnerJSONObjectArrayElementDoesNotMatch() throws JSONException {
ArrayValueMatcher<Object> innerArrayValueMatcher = new ArrayValueMatcher<Object>(comparator, 1);
JSONComparator innerArrayComparator = new CustomComparator(
JSONCompareMode.LENIENT, new Customization("a[2]", innerArrayValueMatcher));
doFailingMatchTest("a",
new ArrayValueMatcher<Object>(innerArrayComparator, 2), // tests inner array i.e. [12,13,14]
"{a:[[99]]}",
ARRAY_OF_JSONARRAYS,
"a\\[2\\]\\[1\\]\\s*Expected:\\s*99\\s*got:\\s*13\\s*");
}
@Test
public void matchesEveryElementOfJSONObjectArray() throws JSONException {
doTest("a", new ArrayValueMatcher<Object>(comparator), "{a:[{type:row}]}", ARRAY_OF_JSONOBJECTS);
}
@Test
public void failsWhenNotEveryElementOfJSONObjectArrayMatches() throws JSONException {
doFailingMatchTest("a",
new ArrayValueMatcher<Object>(comparator),
"{a:[{background:white}]}",
ARRAY_OF_JSONOBJECTS,
"a\\[1\\]\\.background\\s*Expected:\\s*white\\s*got:\\s*grey\\s*;\\s*a\\[3\\]\\.background\\s*Expected:\\s*white\\s*got:\\s*grey\\s*");
}
@Test
public void matchesEveryElementOfJSONObjectArrayWhenRangeTooLarge() throws JSONException {
doTest("a", new ArrayValueMatcher<Object>(comparator, 0, 500), "{a:[{type:row}]}", ARRAY_OF_JSONOBJECTS);
}
@Test
public void matchesElementPairsStartingFromElement1OfJSONObjectArrayWhenRangeTooLarge() throws JSONException {
doTest("a", new ArrayValueMatcher<Object>(comparator, 1, 500), "{a:[{background:grey},{background:white}]}", ARRAY_OF_JSONOBJECTS);
}
@Test
public void matchesElementPairsStartingFromElement0OfJSONObjectArrayWhenRangeTooLarge() throws JSONException {
doTest("a", new ArrayValueMatcher<Object>(comparator), "{a:[{background:white},{background:grey}]}", ARRAY_OF_JSONOBJECTS);
}
@Test
public void failsWhenAppliedToNonArray() throws JSONException {
try {
doTest("a", new ArrayValueMatcher<Object>(comparator), "{a:[{background:white}]}", "{a:{attr1:value1,attr2:value2}}");
}
catch (IllegalArgumentException e) {
assertEquals("Exception message", "ArrayValueMatcher applied to non-array actual value", e.getMessage());
return;
}
fail("Did not throw IllegalArgumentException");
}
/*
* Following tests verify the ability to match an element containing either
* a simple value or a JSON object against simple value or JSON object
* without requiring expected value to be wrapped in an array reducing
* slightly the syntactic load on teh test author & reader.
*/
@Test
public void simpleValueMatchesSecondElementOfJSONObjectArray() throws JSONException {
doTest("a", new ArrayValueMatcher<Object>(comparator, 3), "{a:4}", ARRAY_OF_INTEGERS);
}
@Test
public void jsonObjectMatchesSecondElementOfJSONObjectArray() throws JSONException {
doTest("a", new ArrayValueMatcher<Object>(comparator, 1), "{a:{background:grey,id:2,type:row}}", ARRAY_OF_JSONOBJECTS);
}
/*
* Following tests contain copies of code quoted in ArrayValueMatcher JavaDoc and are included to verify that the exact code documented works as expected.
*/
@Test
public void verifyIdAttributeOfFirstArrayElementMatches() throws JSONException {
JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT);
Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator, 0));
JSONAssert.assertEquals("{a:[{id:1}]}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization));
}
@Test
public void verifyIdAttributeOfFirstArrayElementMatchesSimplifiedExpectedSyntax() throws JSONException {
JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT);
Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator, 0));
JSONAssert.assertEquals("{a:{id:1}}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization));
}
@Test
public void verifyTypeAttributeOfSecondAndThirdElementMatchesRow() throws JSONException {
JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT);
Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator, 1, 2));
JSONAssert.assertEquals("{a:[{type:row}]}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization));
}
@Test
public void verifyTypeAttributeOfEveryArrayElementMatchesRow() throws JSONException {
JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT);
Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator));
JSONAssert.assertEquals("{a:[{type:row}]}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization));
}
@Test
public void verifyEveryArrayElementWithCustomComparator() throws JSONException {
// get length of array we will verify
int aLength = ((JSONArray)((JSONObject)JSONParser.parseJSON(ARRAY_OF_JSONOBJECTS)).get("a")).length();
// create array of customizations one for each array element
RegularExpressionValueMatcher<Object> regExValueMatcher = new RegularExpressionValueMatcher<Object>("\\d+"); // matches one or more digits
Customization[] customizations = new Customization[aLength];
for (int i=0; i<aLength; i++) {
String contextPath = "a["+i+"].id";
customizations[i] = new Customization(contextPath, regExValueMatcher);
}
CustomComparator regExComparator = new CustomComparator(JSONCompareMode.STRICT_ORDER, customizations);
ArrayValueMatcher<Object> regExArrayValueMatcher = new ArrayValueMatcher<Object>(regExComparator);
Customization regExArrayValueCustomization = new Customization("a", regExArrayValueMatcher);
CustomComparator regExCustomArrayValueComparator = new CustomComparator(JSONCompareMode.STRICT_ORDER, new Customization[] { regExArrayValueCustomization });
JSONAssert.assertEquals("{a:[{id:X}]}", ARRAY_OF_JSONOBJECTS, regExCustomArrayValueComparator);
}
@Test
public void verifyBackgroundAttributesOfEveryArrayElementAlternateBetweenWhiteAndGrey() throws JSONException {
JSONComparator comparator = new DefaultComparator(JSONCompareMode.LENIENT);
Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator));
JSONAssert.assertEquals("{a:[{background:white},{background:grey}]}", ARRAY_OF_JSONOBJECTS, new CustomComparator(JSONCompareMode.LENIENT, customization));
}
@Test
public void verifyEveryElementOfArrayIsJSONArrayOfLength3() throws JSONException {
JSONComparator comparator = new ArraySizeComparator(JSONCompareMode.STRICT_ORDER);
Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator, 0, 2));
JSONAssert.assertEquals("{a:[[3]]}", ARRAY_OF_JSONARRAYS, new CustomComparator(JSONCompareMode.LENIENT, customization));
}
@Test
public void verifySecondElementOfArrayIsJSONArrayWhoseFirstElementIs9() throws JSONException {
Customization innerCustomization = new Customization("a[1]", new ArrayValueMatcher<Object>(comparator, 0));
JSONComparator comparator = new CustomComparator(JSONCompareMode.LENIENT, innerCustomization);
Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator, 1));
JSONAssert.assertEquals("{a:[[9]]}", ARRAY_OF_JSONARRAYS, new CustomComparator(JSONCompareMode.LENIENT, customization));
}
@Test
public void verifySecondElementOfArrayIsJSONArrayWhoseFirstElementIs9WithSimpliedExpectedString() throws JSONException {
Customization innerCustomization = new Customization("a[1]", new ArrayValueMatcher<Object>(comparator, 0));
JSONComparator comparator = new CustomComparator(JSONCompareMode.LENIENT, innerCustomization);
Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator, 1));
JSONAssert.assertEquals("{a:[9]}", ARRAY_OF_JSONARRAYS, new CustomComparator(JSONCompareMode.LENIENT, customization));
}
@Test
public void verifySecondElementOfArrayIsJSONArrayWhoseFirstElementIs9WithEvenMoreSimpliedExpectedString() throws JSONException {
Customization innerCustomization = new Customization("a[1]", new ArrayValueMatcher<Object>(comparator, 0));
JSONComparator comparator = new CustomComparator(JSONCompareMode.LENIENT, innerCustomization);
Customization customization = new Customization("a", new ArrayValueMatcher<Object>(comparator, 1));
JSONAssert.assertEquals("{a:9}", ARRAY_OF_JSONARRAYS, new CustomComparator(JSONCompareMode.LENIENT, customization));
}
}