1
1
/*
2
- * Copyright 2014-2015 the original author or authors.
2
+ * Copyright 2014-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
32
32
* Tests for {@link JsonFieldTypeResolver}.
33
33
*
34
34
* @author Andy Wilkinson
35
- *
36
35
*/
37
36
public class JsonFieldTypeResolverTests {
38
37
@@ -49,15 +48,15 @@ public void arrayField() throws IOException {
49
48
@ Test
50
49
public void topLevelArray () throws IOException {
51
50
assertThat (
52
- this .fieldTypeResolver .resolveFieldType ("[]" ,
51
+ this .fieldTypeResolver .resolveFieldType (new FieldDescriptor ( "[]" ) ,
53
52
new ObjectMapper ().readValue ("[{\" a\" :\" alpha\" }]" , List .class )),
54
53
equalTo (JsonFieldType .ARRAY ));
55
54
}
56
55
57
56
@ Test
58
57
public void nestedArray () throws IOException {
59
58
assertThat (
60
- this .fieldTypeResolver .resolveFieldType ("a[]" ,
59
+ this .fieldTypeResolver .resolveFieldType (new FieldDescriptor ( "a[]" ) ,
61
60
createPayload ("{\" a\" : [{\" b\" :\" bravo\" }]}" )),
62
61
equalTo (JsonFieldType .ARRAY ));
63
62
}
@@ -90,72 +89,112 @@ public void stringField() throws IOException {
90
89
@ Test
91
90
public void nestedField () throws IOException {
92
91
assertThat (
93
- this .fieldTypeResolver .resolveFieldType ("a.b.c" ,
92
+ this .fieldTypeResolver .resolveFieldType (new FieldDescriptor ( "a.b.c" ) ,
94
93
createPayload ("{\" a\" :{\" b\" :{\" c\" :{}}}}" )),
95
94
equalTo (JsonFieldType .OBJECT ));
96
95
}
97
96
98
97
@ Test
99
98
public void multipleFieldsWithSameType () throws IOException {
100
99
assertThat (
101
- this .fieldTypeResolver .resolveFieldType ("a[].id" ,
100
+ this .fieldTypeResolver .resolveFieldType (new FieldDescriptor ( "a[].id" ) ,
102
101
createPayload ("{\" a\" :[{\" id\" :1},{\" id\" :2}]}" )),
103
102
equalTo (JsonFieldType .NUMBER ));
104
103
}
105
104
106
105
@ Test
107
106
public void multipleFieldsWithDifferentTypes () throws IOException {
108
107
assertThat (
109
- this .fieldTypeResolver .resolveFieldType ("a[].id" ,
108
+ this .fieldTypeResolver .resolveFieldType (new FieldDescriptor ( "a[].id" ) ,
110
109
createPayload ("{\" a\" :[{\" id\" :1},{\" id\" :true}]}" )),
111
110
equalTo (JsonFieldType .VARIES ));
112
111
}
113
112
114
113
@ Test
115
114
public void multipleFieldsWithDifferentTypesAndSometimesAbsent () throws IOException {
116
115
assertThat (
117
- this .fieldTypeResolver .resolveFieldType ("a[].id" ,
116
+ this .fieldTypeResolver .resolveFieldType (new FieldDescriptor ("a[].id" ),
117
+ createPayload ("{\" a\" :[{\" id\" :1},{\" id\" :true}, { }]}" )),
118
+ equalTo (JsonFieldType .VARIES ));
119
+ }
120
+
121
+ @ Test
122
+ public void multipleFieldsWithDifferentTypesAndSometimesAbsentWhenOptionalResolvesToVaries ()
123
+ throws IOException {
124
+ assertThat (
125
+ this .fieldTypeResolver .resolveFieldType (
126
+ new FieldDescriptor ("a[].id" ).optional (),
118
127
createPayload ("{\" a\" :[{\" id\" :1},{\" id\" :true}, { }]}" )),
119
128
equalTo (JsonFieldType .VARIES ));
120
129
}
121
130
122
131
@ Test
123
132
public void multipleFieldsWhenSometimesAbsent () throws IOException {
124
133
assertThat (
125
- this .fieldTypeResolver .resolveFieldType ("a[].id" ,
134
+ this .fieldTypeResolver .resolveFieldType (new FieldDescriptor ( "a[].id" ) ,
126
135
createPayload ("{\" a\" :[{\" id\" :1},{ }]}" )),
127
136
equalTo (JsonFieldType .NUMBER ));
128
137
}
129
138
130
139
@ Test
131
140
public void multipleFieldsWithDifferentTypesAndSometimesNull () throws IOException {
132
141
assertThat (
133
- this .fieldTypeResolver .resolveFieldType ("a[].id" ,
142
+ this .fieldTypeResolver .resolveFieldType (new FieldDescriptor ( "a[].id" ) ,
134
143
createPayload (
135
144
"{\" a\" :[{\" id\" :1},{\" id\" :true}, {\" id\" :null}]}" )),
136
145
equalTo (JsonFieldType .VARIES ));
137
146
}
138
147
139
148
@ Test
140
- public void multipleFieldsWhenSometimesNull () throws IOException {
149
+ public void multipleFieldsWhenNotNullThenNullWhenRequiredHasVariesType ()
150
+ throws IOException {
151
+ assertThat (
152
+ this .fieldTypeResolver .resolveFieldType (new FieldDescriptor ("a[].id" ),
153
+ createPayload ("{\" a\" :[{\" id\" :1},{\" id\" :null}]}" )),
154
+ equalTo (JsonFieldType .VARIES ));
155
+ }
156
+
157
+ @ Test
158
+ public void multipleFieldsWhenNotNullThenNullWhenOptionalHasSpecificType ()
159
+ throws IOException {
141
160
assertThat (
142
- this .fieldTypeResolver .resolveFieldType ("a[].id" ,
161
+ this .fieldTypeResolver .resolveFieldType (
162
+ new FieldDescriptor ("a[].id" ).optional (),
143
163
createPayload ("{\" a\" :[{\" id\" :1},{\" id\" :null}]}" )),
144
164
equalTo (JsonFieldType .NUMBER ));
145
165
}
146
166
167
+ @ Test
168
+ public void multipleFieldsWhenNullThenNotNullWhenRequiredHasVariesType ()
169
+ throws IOException {
170
+ assertThat (
171
+ this .fieldTypeResolver .resolveFieldType (new FieldDescriptor ("a[].id" ),
172
+ createPayload ("{\" a\" :[{\" id\" :null},{\" id\" :1}]}" )),
173
+ equalTo (JsonFieldType .VARIES ));
174
+ }
175
+
176
+ @ Test
177
+ public void multipleFieldsWhenNullThenNotNullWhenOptionalHasSpecificType ()
178
+ throws IOException {
179
+ assertThat (
180
+ this .fieldTypeResolver .resolveFieldType (
181
+ new FieldDescriptor ("a[].id" ).optional (),
182
+ createPayload ("{\" a\" :[{\" id\" :null},{\" id\" :1}]}" )),
183
+ equalTo (JsonFieldType .NUMBER ));
184
+ }
185
+
147
186
@ Test
148
187
public void multipleFieldsWhenEitherNullOrAbsent () throws IOException {
149
188
assertThat (
150
- this .fieldTypeResolver .resolveFieldType ("a[].id" ,
189
+ this .fieldTypeResolver .resolveFieldType (new FieldDescriptor ( "a[].id" ) ,
151
190
createPayload ("{\" a\" :[{},{\" id\" :null}]}" )),
152
191
equalTo (JsonFieldType .NULL ));
153
192
}
154
193
155
194
@ Test
156
195
public void multipleFieldsThatAreAllNull () throws IOException {
157
196
assertThat (
158
- this .fieldTypeResolver .resolveFieldType ("a[].id" ,
197
+ this .fieldTypeResolver .resolveFieldType (new FieldDescriptor ( "a[].id" ) ,
159
198
createPayload ("{\" a\" :[{\" id\" :null},{\" id\" :null}]}" )),
160
199
equalTo (JsonFieldType .NULL ));
161
200
}
@@ -166,7 +205,8 @@ public void nonExistentSingleFieldProducesFieldDoesNotExistException()
166
205
this .thrownException .expect (FieldDoesNotExistException .class );
167
206
this .thrownException .expectMessage (
168
207
"The payload does not contain a field with the path 'a.b'" );
169
- this .fieldTypeResolver .resolveFieldType ("a.b" , createPayload ("{\" a\" :{}}" ));
208
+ this .fieldTypeResolver .resolveFieldType (new FieldDescriptor ("a.b" ),
209
+ createPayload ("{\" a\" :{}}" ));
170
210
}
171
211
172
212
@ Test
@@ -175,13 +215,13 @@ public void nonExistentMultipleFieldsProducesFieldDoesNotExistException()
175
215
this .thrownException .expect (FieldDoesNotExistException .class );
176
216
this .thrownException .expectMessage (
177
217
"The payload does not contain a field with the path 'a[].b'" );
178
- this .fieldTypeResolver .resolveFieldType ("a[].b" ,
218
+ this .fieldTypeResolver .resolveFieldType (new FieldDescriptor ( "a[].b" ) ,
179
219
createPayload ("{\" a\" :[{\" c\" :1},{\" c\" :2}]}" ));
180
220
}
181
221
182
222
private void assertFieldType (JsonFieldType expectedType , String jsonValue )
183
223
throws IOException {
184
- assertThat (this .fieldTypeResolver .resolveFieldType ("field" ,
224
+ assertThat (this .fieldTypeResolver .resolveFieldType (new FieldDescriptor ( "field" ) ,
185
225
createSimplePayload (jsonValue )), equalTo (expectedType ));
186
226
}
187
227
0 commit comments