23
23
import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
24
24
import lombok .Data ;
25
25
import lombok .NoArgsConstructor ;
26
+ import org .assertj .core .api .InstanceOfAssertFactories ;
26
27
import org .junit .jupiter .api .BeforeEach ;
27
28
import org .junit .jupiter .api .DisplayName ;
28
29
import org .junit .jupiter .api .Nested ;
31
32
import static com .fasterxml .jackson .annotation .JsonTypeInfo .Id .DEDUCTION ;
32
33
import static com .fasterxml .jackson .annotation .JsonTypeInfo .Id .NONE ;
33
34
import static org .assertj .core .api .Assertions .assertThat ;
34
- import static org .junit .jupiter .api .Assertions .assertEquals ;
35
- import static org .junit .jupiter .api .Assertions .assertInstanceOf ;
36
- import static org .junit .jupiter .api .Assertions .assertNotNull ;
37
35
38
36
class JsonUnwrappedDeserializerTest {
39
37
40
- private static final String EXPECTED_VALUE_A = "Value A" ;
41
- private static final String EXPECTED_VALUE_B = "Value B" ;
42
- private static final String EXPECTED_VALUE_C = "Value C" ;
43
-
44
38
private ObjectMapper mapper ;
45
39
46
40
@ BeforeEach
@@ -53,22 +47,39 @@ class Deserialize {
53
47
54
48
@ Test
55
49
@ DisplayName ("Single @JsonUnwrapped polymorphic type" )
56
- void singleInterfaceWithJsonWrapped () throws JsonProcessingException {
57
- RootClass instance = mapper .readValue ("{ \" stringField\" : \" " + EXPECTED_VALUE_A + "\" , "
58
- + "\" extendedField\" : \" " + EXPECTED_VALUE_B + "\" , "
59
- + "\" nestedField\" : \" " + EXPECTED_VALUE_C + "\" }" , RootClass .class );
60
- // Verify normal fields works along to the json-wrapped fields
61
- assertEquals (EXPECTED_VALUE_A , instance .stringField );
62
-
63
- // Verify interfaces are supported at root level
64
- assertNotNull (instance .rootInterface , "Interface was not deserialized!" );
65
- assertInstanceOf (RootImplementation .class , instance .rootInterface );
66
- RootImplementation rootImplementation = ((RootImplementation ) instance .rootInterface );
67
- assertEquals (EXPECTED_VALUE_B , rootImplementation .extendedField );
68
-
69
- // Verify nested interfaces are also supported
70
- assertInstanceOf (NestedImplementation .class , rootImplementation .nestedInterface );
71
- assertEquals (EXPECTED_VALUE_C , ((NestedImplementation ) rootImplementation .nestedInterface ).nestedField );
50
+ void singleJsonWrappedPolymorphicField () throws JsonProcessingException {
51
+ final RootClass result = mapper .readValue ("{" +
52
+ "\" stringField\" : \" string-field-value\" , " +
53
+ "\" extendedField\" : \" extended-field-value\" , " +
54
+ "\" nestedField\" : \" nested-field-value\" " +
55
+ "}" , RootClass .class );
56
+ assertThat (result )
57
+ // Verify normal fields works along to the json-wrapped fields
58
+ .hasFieldOrPropertyWithValue ("stringField" , "string-field-value" )
59
+ // Verify interfaces are supported at root level
60
+ .extracting (RootClass ::getRootInterface )
61
+ .isNotNull ()
62
+ .asInstanceOf (InstanceOfAssertFactories .type (RootImplementation .class ))
63
+ .hasFieldOrPropertyWithValue ("extendedField" , "extended-field-value" )
64
+ // Verify nested interfaces are also supported
65
+ .extracting (RootImplementation ::getNestedInterface )
66
+ .isNotNull ()
67
+ .asInstanceOf (InstanceOfAssertFactories .type (NestedImplementation .class ))
68
+ .hasFieldOrPropertyWithValue ("nestedField" , "nested-field-value" );
69
+ }
70
+
71
+ @ Test
72
+ @ DisplayName ("Single @JsonUnwrapped polymorphic field with missing data" )
73
+ void singleJsonWrappedPolymorphicFieldWithMissingDataForUnwrapped () throws JsonProcessingException {
74
+ final RootClass result = mapper .readValue ("{" +
75
+ "\" stringField\" : \" string-field-value\" " +
76
+ "}" , RootClass .class );
77
+ assertThat (result )
78
+ // Verify normal fields works along to the json-wrapped fields
79
+ .hasFieldOrPropertyWithValue ("stringField" , "string-field-value" )
80
+ // Verify interfaces are supported at root level
81
+ .extracting (RootClass ::getRootInterface )
82
+ .isNull ();
72
83
}
73
84
74
85
@ Test
@@ -167,6 +178,8 @@ public static class BarImpl implements Bar {
167
178
private String bar ;
168
179
}
169
180
181
+ @ Data
182
+ @ NoArgsConstructor
170
183
@ JsonDeserialize (using = io .fabric8 .kubernetes .model .jackson .JsonUnwrappedDeserializer .class )
171
184
public static class RootClass {
172
185
@@ -175,25 +188,6 @@ public static class RootClass {
175
188
@ JsonUnwrapped
176
189
private RootInterface rootInterface ;
177
190
178
- public RootClass () {
179
-
180
- }
181
-
182
- public String getStringField () {
183
- return stringField ;
184
- }
185
-
186
- public void setStringField (String stringField ) {
187
- this .stringField = stringField ;
188
- }
189
-
190
- public RootInterface getRootInterface () {
191
- return rootInterface ;
192
- }
193
-
194
- public void setRootInterface (RootInterface rootInterface ) {
195
- this .rootInterface = rootInterface ;
196
- }
197
191
}
198
192
199
193
@ JsonSubTypes (@ JsonSubTypes .Type (RootImplementation .class ))
@@ -202,24 +196,14 @@ interface RootInterface {
202
196
203
197
}
204
198
199
+ @ Data
200
+ @ NoArgsConstructor
205
201
@ JsonDeserialize (using = io .fabric8 .kubernetes .model .jackson .JsonUnwrappedDeserializer .class )
206
202
public static class RootImplementation implements RootInterface {
207
203
208
204
private String extendedField ;
209
205
@ JsonUnwrapped
210
206
private NestedInterface nestedInterface ;
211
-
212
- public RootImplementation () {
213
-
214
- }
215
-
216
- public String getExtendedField () {
217
- return extendedField ;
218
- }
219
-
220
- public void setExtendedField (String extendedField ) {
221
- this .extendedField = extendedField ;
222
- }
223
207
}
224
208
225
209
@ JsonSubTypes (@ JsonSubTypes .Type (NestedImplementation .class ))
@@ -228,19 +212,9 @@ interface NestedInterface {
228
212
229
213
}
230
214
215
+ @ Data
216
+ @ NoArgsConstructor
231
217
public static class NestedImplementation implements NestedInterface {
232
218
private String nestedField ;
233
-
234
- public NestedImplementation () {
235
-
236
- }
237
-
238
- public String getNestedField () {
239
- return nestedField ;
240
- }
241
-
242
- public void setNestedField (String nestedField ) {
243
- this .nestedField = nestedField ;
244
- }
245
219
}
246
220
}
0 commit comments