24
24
25
25
import com .jayway .jsonpath .JsonPath ;
26
26
import com .jayway .jsonpath .PathNotFoundException ;
27
- import org .assertj .core .api .AbstractStringAssert ;
27
+ import org .assertj .core .api .AbstractObjectAssert ;
28
28
import org .assertj .core .api .AssertProvider ;
29
+ import org .assertj .core .api .Assertions ;
29
30
import org .assertj .core .error .BasicErrorMessageFactory ;
30
31
import org .assertj .core .internal .Failures ;
31
32
61
62
* @param <SELF> the type of assertions
62
63
*/
63
64
public abstract class AbstractJsonContentAssert <SELF extends AbstractJsonContentAssert <SELF >>
64
- extends AbstractStringAssert <SELF > {
65
+ extends AbstractObjectAssert <SELF , JsonContent > {
65
66
66
67
private static final Failures failures = Failures .instance ();
67
68
@@ -79,16 +80,12 @@ public abstract class AbstractJsonContentAssert<SELF extends AbstractJsonContent
79
80
80
81
/**
81
82
* Create an assert for the given JSON document.
82
- * <p>Path can be converted to a value object using the given
83
- * {@linkplain GenericHttpMessageConverter JSON message converter}.
84
- * @param json the JSON document to assert
85
- * @param jsonMessageConverter the converter to use
83
+ * @param actual the JSON document to assert
86
84
* @param selfType the implementation type of this assert
87
85
*/
88
- protected AbstractJsonContentAssert (@ Nullable String json ,
89
- @ Nullable GenericHttpMessageConverter <Object > jsonMessageConverter , Class <?> selfType ) {
90
- super (json , selfType );
91
- this .jsonMessageConverter = jsonMessageConverter ;
86
+ protected AbstractJsonContentAssert (@ Nullable JsonContent actual , Class <?> selfType ) {
87
+ super (actual , selfType );
88
+ this .jsonMessageConverter = (actual != null ? actual .getJsonMessageConverter () : null );
92
89
this .jsonLoader = new JsonLoader (null , null );
93
90
as ("JSON content" );
94
91
}
@@ -141,6 +138,19 @@ public SELF doesNotHavePath(String path) {
141
138
142
139
// JsonAssert support
143
140
141
+ /**
142
+ * Verify that the actual value is {@linkplain JsonCompareMode#STRICT strictly}
143
+ * equal to the given JSON. The {@code expected} value can contain the JSON
144
+ * itself or, if it ends with {@code .json}, the name of a resource to be
145
+ * loaded from the classpath.
146
+ * @param expected the expected JSON or the name of a resource containing
147
+ * the expected JSON
148
+ * @see #isEqualTo(CharSequence, JsonCompareMode)
149
+ */
150
+ public SELF isEqualTo (@ Nullable CharSequence expected ) {
151
+ return isEqualTo (expected , JsonCompareMode .STRICT );
152
+ }
153
+
144
154
/**
145
155
* Verify that the actual value is equal to the given JSON. The
146
156
* {@code expected} value can contain the JSON itself or, if it ends with
@@ -257,6 +267,19 @@ public SELF isStrictlyEqualTo(Resource expected) {
257
267
return isEqualTo (expected , JsonCompareMode .STRICT );
258
268
}
259
269
270
+ /**
271
+ * Verify that the actual value is {@linkplain JsonCompareMode#STRICT strictly}
272
+ * not equal to the given JSON. The {@code expected} value can contain the
273
+ * JSON itself or, if it ends with {@code .json}, the name of a resource to
274
+ * be loaded from the classpath.
275
+ * @param expected the expected JSON or the name of a resource containing
276
+ * the expected JSON
277
+ * @see #isNotEqualTo(CharSequence, JsonCompareMode)
278
+ */
279
+ public SELF isNotEqualTo (@ Nullable CharSequence expected ) {
280
+ return isNotEqualTo (expected , JsonCompareMode .STRICT );
281
+ }
282
+
260
283
/**
261
284
* Verify that the actual value is not equal to the given JSON. The
262
285
* {@code expected} value can contain the JSON itself or, if it ends with
@@ -399,13 +422,24 @@ public SELF withCharset(@Nullable Charset charset) {
399
422
return this .myself ;
400
423
}
401
424
425
+ @ Nullable
426
+ private String toJsonString () {
427
+ return (this .actual != null ? this .actual .getJson () : null );
428
+ }
429
+
430
+ @ SuppressWarnings ("NullAway" )
431
+ private String toNonNullJsonString () {
432
+ String jsonString = toJsonString ();
433
+ Assertions .assertThat (jsonString ).as ("JSON content" ).isNotNull ();
434
+ return jsonString ;
435
+ }
402
436
403
437
private JsonComparison compare (@ Nullable CharSequence expectedJson , JsonCompareMode compareMode ) {
404
438
return compare (expectedJson , JsonAssert .comparator (compareMode ));
405
439
}
406
440
407
441
private JsonComparison compare (@ Nullable CharSequence expectedJson , JsonComparator comparator ) {
408
- return comparator .compare ((expectedJson != null ) ? expectedJson .toString () : null , this . actual );
442
+ return comparator .compare ((expectedJson != null ) ? expectedJson .toString () : null , toJsonString () );
409
443
}
410
444
411
445
private SELF assertIsMatch (JsonComparison result ) {
@@ -435,16 +469,15 @@ private class JsonPathValue {
435
469
436
470
private final String path ;
437
471
438
- private final JsonPath jsonPath ;
439
-
440
472
private final String json ;
441
473
474
+ private final JsonPath jsonPath ;
475
+
442
476
JsonPathValue (String path ) {
443
477
Assert .hasText (path , "'path' must not be null or empty" );
444
- isNotNull ();
445
478
this .path = path ;
479
+ this .json = toNonNullJsonString ();
446
480
this .jsonPath = JsonPath .compile (this .path );
447
- this .json = AbstractJsonContentAssert .this .actual ;
448
481
}
449
482
450
483
@ Nullable
0 commit comments