28
28
import org .assertj .core .api .AssertProvider ;
29
29
import org .assertj .core .error .BasicErrorMessageFactory ;
30
30
import org .assertj .core .internal .Failures ;
31
- import org .skyscreamer .jsonassert .JSONCompare ;
32
- import org .skyscreamer .jsonassert .JSONCompareMode ;
33
- import org .skyscreamer .jsonassert .JSONCompareResult ;
34
- import org .skyscreamer .jsonassert .comparator .JSONComparator ;
35
31
36
32
import org .springframework .core .io .ByteArrayResource ;
37
33
import org .springframework .core .io .ClassPathResource ;
41
37
import org .springframework .http .converter .GenericHttpMessageConverter ;
42
38
import org .springframework .lang .Nullable ;
43
39
import org .springframework .util .Assert ;
44
- import org .springframework .util .function .ThrowingBiFunction ;
45
40
46
41
/**
47
42
* Base AssertJ {@linkplain org.assertj.core.api.Assert assertions} that can be
51
46
* extracting a part of the document for further {@linkplain JsonPathValueAssert
52
47
* assertions} on the value.
53
48
*
54
- * <p>Also supports comparing the JSON document against a target, using
55
- * {@linkplain JSONCompare JSON Assert }. Resources that are loaded from
49
+ * <p>Also supports comparing the JSON document against a target, using a
50
+ * {@linkplain JsonComparator JSON Comparator }. Resources that are loaded from
56
51
* the classpath can be relative if a {@linkplain #withResourceLoadClass(Class)
57
52
* class} is provided. By default, {@code UTF-8} is used to load resources,
58
53
* but this can be overridden using {@link #withCharset(Charset)}.
@@ -154,9 +149,9 @@ public SELF doesNotHavePath(String path) {
154
149
* the expected JSON
155
150
* @param compareMode the compare mode used when checking
156
151
*/
157
- public SELF isEqualTo (@ Nullable CharSequence expected , JSONCompareMode compareMode ) {
152
+ public SELF isEqualTo (@ Nullable CharSequence expected , JsonCompareMode compareMode ) {
158
153
String expectedJson = this .jsonLoader .getJson (expected );
159
- return assertNotFailed (compare (expectedJson , compareMode ));
154
+ return assertIsMatch (compare (expectedJson , compareMode ));
160
155
}
161
156
162
157
/**
@@ -171,9 +166,9 @@ public SELF isEqualTo(@Nullable CharSequence expected, JSONCompareMode compareMo
171
166
* @param expected a resource containing the expected JSON
172
167
* @param compareMode the compare mode used when checking
173
168
*/
174
- public SELF isEqualTo (Resource expected , JSONCompareMode compareMode ) {
169
+ public SELF isEqualTo (Resource expected , JsonCompareMode compareMode ) {
175
170
String expectedJson = this .jsonLoader .getJson (expected );
176
- return assertNotFailed (compare (expectedJson , compareMode ));
171
+ return assertIsMatch (compare (expectedJson , compareMode ));
177
172
}
178
173
179
174
/**
@@ -184,9 +179,9 @@ public SELF isEqualTo(Resource expected, JSONCompareMode compareMode) {
184
179
* the expected JSON
185
180
* @param comparator the comparator used when checking
186
181
*/
187
- public SELF isEqualTo (@ Nullable CharSequence expected , JSONComparator comparator ) {
182
+ public SELF isEqualTo (@ Nullable CharSequence expected , JsonComparator comparator ) {
188
183
String expectedJson = this .jsonLoader .getJson (expected );
189
- return assertNotFailed (compare (expectedJson , comparator ));
184
+ return assertIsMatch (compare (expectedJson , comparator ));
190
185
}
191
186
192
187
/**
@@ -201,25 +196,25 @@ public SELF isEqualTo(@Nullable CharSequence expected, JSONComparator comparator
201
196
* @param expected a resource containing the expected JSON
202
197
* @param comparator the comparator used when checking
203
198
*/
204
- public SELF isEqualTo (Resource expected , JSONComparator comparator ) {
199
+ public SELF isEqualTo (Resource expected , JsonComparator comparator ) {
205
200
String expectedJson = this .jsonLoader .getJson (expected );
206
- return assertNotFailed (compare (expectedJson , comparator ));
201
+ return assertIsMatch (compare (expectedJson , comparator ));
207
202
}
208
203
209
204
/**
210
- * Verify that the actual value is {@link JSONCompareMode #LENIENT leniently}
205
+ * Verify that the actual value is {@link JsonCompareMode #LENIENT leniently}
211
206
* equal to the given JSON. The {@code expected} value can contain the JSON
212
207
* itself or, if it ends with {@code .json}, the name of a resource to be
213
208
* loaded from the classpath.
214
209
* @param expected the expected JSON or the name of a resource containing
215
210
* the expected JSON
216
211
*/
217
212
public SELF isLenientlyEqualTo (@ Nullable CharSequence expected ) {
218
- return isEqualTo (expected , JSONCompareMode .LENIENT );
213
+ return isEqualTo (expected , JsonCompareMode .LENIENT );
219
214
}
220
215
221
216
/**
222
- * Verify that the actual value is {@link JSONCompareMode #LENIENT leniently}
217
+ * Verify that the actual value is {@link JsonCompareMode #LENIENT leniently}
223
218
* equal to the given JSON {@link Resource}.
224
219
* <p>The resource abstraction allows to provide several input types:
225
220
* <ul>
@@ -231,23 +226,23 @@ public SELF isLenientlyEqualTo(@Nullable CharSequence expected) {
231
226
* @param expected a resource containing the expected JSON
232
227
*/
233
228
public SELF isLenientlyEqualTo (Resource expected ) {
234
- return isEqualTo (expected , JSONCompareMode .LENIENT );
229
+ return isEqualTo (expected , JsonCompareMode .LENIENT );
235
230
}
236
231
237
232
/**
238
- * Verify that the actual value is {@link JSONCompareMode #STRICT strictly}
233
+ * Verify that the actual value is {@link JsonCompareMode #STRICT strictly}
239
234
* equal to the given JSON. The {@code expected} value can contain the JSON
240
235
* itself or, if it ends with {@code .json}, the name of a resource to be
241
236
* loaded from the classpath.
242
237
* @param expected the expected JSON or the name of a resource containing
243
238
* the expected JSON
244
239
*/
245
240
public SELF isStrictlyEqualTo (@ Nullable CharSequence expected ) {
246
- return isEqualTo (expected , JSONCompareMode .STRICT );
241
+ return isEqualTo (expected , JsonCompareMode .STRICT );
247
242
}
248
243
249
244
/**
250
- * Verify that the actual value is {@link JSONCompareMode #STRICT strictly}
245
+ * Verify that the actual value is {@link JsonCompareMode #STRICT strictly}
251
246
* equal to the given JSON {@link Resource}.
252
247
* <p>The resource abstraction allows to provide several input types:
253
248
* <ul>
@@ -259,7 +254,7 @@ public SELF isStrictlyEqualTo(@Nullable CharSequence expected) {
259
254
* @param expected a resource containing the expected JSON
260
255
*/
261
256
public SELF isStrictlyEqualTo (Resource expected ) {
262
- return isEqualTo (expected , JSONCompareMode .STRICT );
257
+ return isEqualTo (expected , JsonCompareMode .STRICT );
263
258
}
264
259
265
260
/**
@@ -270,9 +265,9 @@ public SELF isStrictlyEqualTo(Resource expected) {
270
265
* the expected JSON
271
266
* @param compareMode the compare mode used when checking
272
267
*/
273
- public SELF isNotEqualTo (@ Nullable CharSequence expected , JSONCompareMode compareMode ) {
268
+ public SELF isNotEqualTo (@ Nullable CharSequence expected , JsonCompareMode compareMode ) {
274
269
String expectedJson = this .jsonLoader .getJson (expected );
275
- return assertNotPassed (compare (expectedJson , compareMode ));
270
+ return assertIsMismatch (compare (expectedJson , compareMode ));
276
271
}
277
272
278
273
/**
@@ -287,9 +282,9 @@ public SELF isNotEqualTo(@Nullable CharSequence expected, JSONCompareMode compar
287
282
* @param expected a resource containing the expected JSON
288
283
* @param compareMode the compare mode used when checking
289
284
*/
290
- public SELF isNotEqualTo (Resource expected , JSONCompareMode compareMode ) {
285
+ public SELF isNotEqualTo (Resource expected , JsonCompareMode compareMode ) {
291
286
String expectedJson = this .jsonLoader .getJson (expected );
292
- return assertNotPassed (compare (expectedJson , compareMode ));
287
+ return assertIsMismatch (compare (expectedJson , compareMode ));
293
288
}
294
289
295
290
/**
@@ -300,9 +295,9 @@ public SELF isNotEqualTo(Resource expected, JSONCompareMode compareMode) {
300
295
* the expected JSON
301
296
* @param comparator the comparator used when checking
302
297
*/
303
- public SELF isNotEqualTo (@ Nullable CharSequence expected , JSONComparator comparator ) {
298
+ public SELF isNotEqualTo (@ Nullable CharSequence expected , JsonComparator comparator ) {
304
299
String expectedJson = this .jsonLoader .getJson (expected );
305
- return assertNotPassed (compare (expectedJson , comparator ));
300
+ return assertIsMismatch (compare (expectedJson , comparator ));
306
301
}
307
302
308
303
/**
@@ -317,25 +312,25 @@ public SELF isNotEqualTo(@Nullable CharSequence expected, JSONComparator compara
317
312
* @param expected a resource containing the expected JSON
318
313
* @param comparator the comparator used when checking
319
314
*/
320
- public SELF isNotEqualTo (Resource expected , JSONComparator comparator ) {
315
+ public SELF isNotEqualTo (Resource expected , JsonComparator comparator ) {
321
316
String expectedJson = this .jsonLoader .getJson (expected );
322
- return assertNotPassed (compare (expectedJson , comparator ));
317
+ return assertIsMismatch (compare (expectedJson , comparator ));
323
318
}
324
319
325
320
/**
326
- * Verify that the actual value is not {@link JSONCompareMode #LENIENT
321
+ * Verify that the actual value is not {@link JsonCompareMode #LENIENT
327
322
* leniently} equal to the given JSON. The {@code expected} value can
328
323
* contain the JSON itself or, if it ends with {@code .json}, the name of a
329
324
* resource to be loaded from the classpath.
330
325
* @param expected the expected JSON or the name of a resource containing
331
326
* the expected JSON
332
327
*/
333
328
public SELF isNotLenientlyEqualTo (@ Nullable CharSequence expected ) {
334
- return isNotEqualTo (expected , JSONCompareMode .LENIENT );
329
+ return isNotEqualTo (expected , JsonCompareMode .LENIENT );
335
330
}
336
331
337
332
/**
338
- * Verify that the actual value is not {@link JSONCompareMode #LENIENT
333
+ * Verify that the actual value is not {@link JsonCompareMode #LENIENT
339
334
* leniently} equal to the given JSON {@link Resource}.
340
335
* <p>The resource abstraction allows to provide several input types:
341
336
* <ul>
@@ -347,23 +342,23 @@ public SELF isNotLenientlyEqualTo(@Nullable CharSequence expected) {
347
342
* @param expected a resource containing the expected JSON
348
343
*/
349
344
public SELF isNotLenientlyEqualTo (Resource expected ) {
350
- return isNotEqualTo (expected , JSONCompareMode .LENIENT );
345
+ return isNotEqualTo (expected , JsonCompareMode .LENIENT );
351
346
}
352
347
353
348
/**
354
- * Verify that the actual value is not {@link JSONCompareMode #STRICT
349
+ * Verify that the actual value is not {@link JsonCompareMode #STRICT
355
350
* strictly} equal to the given JSON. The {@code expected} value can
356
351
* contain the JSON itself or, if it ends with {@code .json}, the name of a
357
352
* resource to be loaded from the classpath.
358
353
* @param expected the expected JSON or the name of a resource containing
359
354
* the expected JSON
360
355
*/
361
356
public SELF isNotStrictlyEqualTo (@ Nullable CharSequence expected ) {
362
- return isNotEqualTo (expected , JSONCompareMode .STRICT );
357
+ return isNotEqualTo (expected , JsonCompareMode .STRICT );
363
358
}
364
359
365
360
/**
366
- * Verify that the actual value is not {@link JSONCompareMode #STRICT
361
+ * Verify that the actual value is not {@link JsonCompareMode #STRICT
367
362
* strictly} equal to the given JSON {@link Resource}.
368
363
* <p>The resource abstraction allows to provide several input types:
369
364
* <ul>
@@ -375,7 +370,7 @@ public SELF isNotStrictlyEqualTo(@Nullable CharSequence expected) {
375
370
* @param expected a resource containing the expected JSON
376
371
*/
377
372
public SELF isNotStrictlyEqualTo (Resource expected ) {
378
- return isNotEqualTo (expected , JSONCompareMode .STRICT );
373
+ return isNotEqualTo (expected , JsonCompareMode .STRICT );
379
374
}
380
375
381
376
/**
@@ -405,54 +400,25 @@ public SELF withCharset(@Nullable Charset charset) {
405
400
}
406
401
407
402
408
- private JSONCompareResult compare (@ Nullable CharSequence expectedJson , JSONCompareMode compareMode ) {
409
- return compare (this .actual , expectedJson , (actualJsonString , expectedJsonString ) ->
410
- JSONCompare .compareJSON (expectedJsonString , actualJsonString , compareMode ));
403
+ private JsonComparison compare (@ Nullable CharSequence expectedJson , JsonCompareMode compareMode ) {
404
+ return compare (expectedJson , JsonAssert .comparator (compareMode ));
411
405
}
412
406
413
- private JSONCompareResult compare (@ Nullable CharSequence expectedJson , JSONComparator comparator ) {
414
- return compare (this .actual , expectedJson , (actualJsonString , expectedJsonString ) ->
415
- JSONCompare .compareJSON (expectedJsonString , actualJsonString , comparator ));
407
+ private JsonComparison compare (@ Nullable CharSequence expectedJson , JsonComparator comparator ) {
408
+ return comparator .compare ((expectedJson != null ) ? expectedJson .toString () : null , this .actual );
416
409
}
417
410
418
- private JSONCompareResult compare (@ Nullable CharSequence actualJson , @ Nullable CharSequence expectedJson ,
419
- ThrowingBiFunction <String , String , JSONCompareResult > comparator ) {
420
-
421
- if (actualJson == null ) {
422
- return compareForNull (expectedJson );
423
- }
424
- if (expectedJson == null ) {
425
- return compareForNull (actualJson .toString ());
426
- }
427
- try {
428
- return comparator .applyWithException (actualJson .toString (), expectedJson .toString ());
429
- }
430
- catch (Exception ex ) {
431
- if (ex instanceof RuntimeException runtimeException ) {
432
- throw runtimeException ;
433
- }
434
- throw new IllegalStateException (ex );
435
- }
411
+ private SELF assertIsMatch (JsonComparison result ) {
412
+ return assertComparison (result , JsonComparison .Result .MATCH );
436
413
}
437
414
438
- private JSONCompareResult compareForNull (@ Nullable CharSequence expectedJson ) {
439
- JSONCompareResult result = new JSONCompareResult ();
440
- if (expectedJson != null ) {
441
- result .fail ("Expected null JSON" );
442
- }
443
- return result ;
444
- }
445
-
446
- private SELF assertNotFailed (JSONCompareResult result ) {
447
- if (result .failed ()) {
448
- failWithMessage ("JSON comparison failure: %s" , result .getMessage ());
449
- }
450
- return this .myself ;
415
+ private SELF assertIsMismatch (JsonComparison result ) {
416
+ return assertComparison (result , JsonComparison .Result .MISMATCH );
451
417
}
452
418
453
- private SELF assertNotPassed ( JSONCompareResult result ) {
454
- if (result . passed () ) {
455
- failWithMessage ("JSON comparison failure: %s" , result .getMessage ());
419
+ private SELF assertComparison ( JsonComparison jsonComparison , JsonComparison . Result requiredResult ) {
420
+ if (jsonComparison . getResult () != requiredResult ) {
421
+ failWithMessage ("JSON comparison failure: %s" , jsonComparison .getMessage ());
456
422
}
457
423
return this .myself ;
458
424
}
0 commit comments