26
26
import java .util .function .Consumer ;
27
27
import java .util .stream .Stream ;
28
28
29
+ import com .fasterxml .jackson .annotation .JsonIgnoreProperties ;
29
30
import com .fasterxml .jackson .databind .ObjectMapper ;
31
+ import org .assertj .core .api .AbstractObjectAssert ;
30
32
import org .assertj .core .api .AssertProvider ;
31
33
import org .assertj .core .api .InstanceOfAssertFactories ;
34
+ import org .assertj .core .api .InstanceOfAssertFactory ;
32
35
import org .json .JSONException ;
33
36
import org .json .JSONObject ;
34
37
import org .junit .jupiter .api .Nested ;
@@ -100,6 +103,56 @@ void satisfiesAllowFurtherAssertions() {
100
103
});
101
104
}
102
105
106
+ @ Nested
107
+ class ConversionTests {
108
+
109
+ @ Test
110
+ void convertToTargetType () {
111
+ assertThat (forJson (SIMPSONS , jsonHttpMessageConverter ))
112
+ .convertTo (Family .class )
113
+ .satisfies (family -> assertThat (family .familyMembers ()).hasSize (5 ));
114
+ }
115
+
116
+ @ Test
117
+ void convertToIncompatibleTargetTypeShouldFail () {
118
+ AbstractJsonContentAssert <?> jsonAssert = assertThat (forJson (SIMPSONS , jsonHttpMessageConverter ));
119
+ assertThatExceptionOfType (AssertionError .class )
120
+ .isThrownBy (() -> jsonAssert .convertTo (Member .class ))
121
+ .withMessageContainingAll ("To convert successfully to:" ,
122
+ Member .class .getName (), "But it failed:" );
123
+ }
124
+
125
+ @ Test
126
+ void convertUsingAssertFactory () {
127
+ assertThat (forJson (SIMPSONS , jsonHttpMessageConverter ))
128
+ .convertTo (new FamilyAssertFactory ())
129
+ .hasFamilyMember ("Homer" );
130
+ }
131
+
132
+ private AssertProvider <AbstractJsonContentAssert <?>> forJson (@ Nullable String json ,
133
+ @ Nullable GenericHttpMessageConverter <Object > jsonHttpMessageConverter ) {
134
+
135
+ return () -> new TestJsonContentAssert (json , jsonHttpMessageConverter );
136
+ }
137
+
138
+ private static class FamilyAssertFactory extends InstanceOfAssertFactory <Family , FamilyAssert > {
139
+ public FamilyAssertFactory () {
140
+ super (Family .class , FamilyAssert ::new );
141
+ }
142
+ }
143
+
144
+ private static class FamilyAssert extends AbstractObjectAssert <FamilyAssert , Family > {
145
+ public FamilyAssert (Family family ) {
146
+ super (family , FamilyAssert .class );
147
+ }
148
+
149
+ public FamilyAssert hasFamilyMember (String name ) {
150
+ assertThat (this .actual .familyMembers ).anySatisfy (m -> assertThat (m .name ()).isEqualTo (name ));
151
+ return this .myself ;
152
+ }
153
+ }
154
+ }
155
+
103
156
@ Nested
104
157
class HasPathTests {
105
158
@@ -261,14 +314,14 @@ void asMapIsEmpty() {
261
314
void convertToWithoutHttpMessageConverterShouldFail () {
262
315
JsonPathValueAssert path = assertThat (forJson (SIMPSONS )).extractingPath ("$.familyMembers[0]" );
263
316
assertThatIllegalStateException ()
264
- .isThrownBy (() -> path .convertTo (ExtractingPathTests . Member .class ))
317
+ .isThrownBy (() -> path .convertTo (Member .class ))
265
318
.withMessage ("No JSON message converter available to convert {name=Homer}" );
266
319
}
267
320
268
321
@ Test
269
322
void convertToTargetType () {
270
323
assertThat (forJson (SIMPSONS , jsonHttpMessageConverter ))
271
- .extractingPath ("$.familyMembers[0]" ).convertTo (ExtractingPathTests . Member .class )
324
+ .extractingPath ("$.familyMembers[0]" ).convertTo (Member .class )
272
325
.satisfies (member -> assertThat (member .name ).isEqualTo ("Homer" ));
273
326
}
274
327
@@ -283,7 +336,7 @@ void convertToIncompatibleTargetTypeShouldFail() {
283
336
}
284
337
285
338
@ Test
286
- void convertArrayToParameterizedType () {
339
+ void convertArrayUsingAssertFactory () {
287
340
assertThat (forJson (SIMPSONS , jsonHttpMessageConverter ))
288
341
.extractingPath ("$.familyMembers" )
289
342
.convertTo (InstanceOfAssertFactories .list (Member .class ))
@@ -336,8 +389,6 @@ void isNotEmptyForPathWithFilterNotMatching() {
336
389
}
337
390
338
391
339
- private record Member (String name ) {}
340
-
341
392
private record Customer (long id , String username ) {}
342
393
343
394
private AssertProvider <AbstractJsonContentAssert <?>> forJson (@ Nullable String json ) {
@@ -836,6 +887,12 @@ private AssertProvider<AbstractJsonContentAssert<?>> forJson(@Nullable String js
836
887
return () -> new TestJsonContentAssert (json , null );
837
888
}
838
889
890
+
891
+ record Member (String name ) {}
892
+
893
+ @ JsonIgnoreProperties (ignoreUnknown = true )
894
+ record Family (List <Member > familyMembers ) {}
895
+
839
896
private static class TestJsonContentAssert extends AbstractJsonContentAssert <TestJsonContentAssert > {
840
897
841
898
public TestJsonContentAssert (@ Nullable String json , @ Nullable GenericHttpMessageConverter <Object > jsonMessageConverter ) {
0 commit comments