@@ -36,6 +36,69 @@ public class JsonContentHandlerTests {
36
36
@ Rule
37
37
public ExpectedException thrown = ExpectedException .none ();
38
38
39
+ @ Test
40
+ public void typeForFieldWithNullValueMustMatch () {
41
+ this .thrown .expect (FieldTypesDoNotMatchException .class );
42
+ new JsonContentHandler ("{\" a\" : null}" .getBytes ())
43
+ .resolveFieldType (new FieldDescriptor ("a" ).type (JsonFieldType .STRING ));
44
+ }
45
+
46
+ @ Test
47
+ public void typeForFieldWithNotNullAndThenNullValueMustMatch () {
48
+ this .thrown .expect (FieldTypesDoNotMatchException .class );
49
+ new JsonContentHandler ("{\" a\" :[{\" id\" :1},{\" id\" :null}]}" .getBytes ())
50
+ .resolveFieldType (
51
+ new FieldDescriptor ("a[].id" ).type (JsonFieldType .STRING ));
52
+ }
53
+
54
+ @ Test
55
+ public void typeForFieldWithNullAndThenNotNullValueMustMatch () {
56
+ this .thrown .expect (FieldTypesDoNotMatchException .class );
57
+ new JsonContentHandler ("{\" a\" :[{\" id\" :null},{\" id\" :1}]}" .getBytes ())
58
+ .resolveFieldType (
59
+ new FieldDescriptor ("a.[].id" ).type (JsonFieldType .STRING ));
60
+ }
61
+
62
+ @ Test
63
+ public void typeForOptionalFieldWithNumberAndThenNullValueIsNumber () {
64
+ Object fieldType = new JsonContentHandler (
65
+ "{\" a\" :[{\" id\" :1},{\" id\" :null}]}\" " .getBytes ())
66
+ .resolveFieldType (new FieldDescriptor ("a[].id" ).optional ());
67
+ assertThat ((JsonFieldType ) fieldType ).isEqualTo (JsonFieldType .NUMBER );
68
+ }
69
+
70
+ @ Test
71
+ public void typeForOptionalFieldWithNullAndThenNumberIsNumber () {
72
+ Object fieldType = new JsonContentHandler (
73
+ "{\" a\" :[{\" id\" :null},{\" id\" :1}]}" .getBytes ())
74
+ .resolveFieldType (new FieldDescriptor ("a[].id" ).optional ());
75
+ assertThat ((JsonFieldType ) fieldType ).isEqualTo (JsonFieldType .NUMBER );
76
+ }
77
+
78
+ @ Test
79
+ public void typeForFieldWithNumberAndThenNullValueIsVaries () {
80
+ Object fieldType = new JsonContentHandler (
81
+ "{\" a\" :[{\" id\" :1},{\" id\" :null}]}\" " .getBytes ())
82
+ .resolveFieldType (new FieldDescriptor ("a[].id" ));
83
+ assertThat ((JsonFieldType ) fieldType ).isEqualTo (JsonFieldType .VARIES );
84
+ }
85
+
86
+ @ Test
87
+ public void typeForFieldWithNullAndThenNumberIsVaries () {
88
+ Object fieldType = new JsonContentHandler (
89
+ "{\" a\" :[{\" id\" :null},{\" id\" :1}]}" .getBytes ())
90
+ .resolveFieldType (new FieldDescriptor ("a[].id" ));
91
+ assertThat ((JsonFieldType ) fieldType ).isEqualTo (JsonFieldType .VARIES );
92
+ }
93
+
94
+ @ Test
95
+ public void typeForOptionalFieldWithNullValueCanBeProvidedExplicitly () {
96
+ Object fieldType = new JsonContentHandler ("{\" a\" : null}" .getBytes ())
97
+ .resolveFieldType (
98
+ new FieldDescriptor ("a" ).type (JsonFieldType .STRING ).optional ());
99
+ assertThat ((JsonFieldType ) fieldType ).isEqualTo (JsonFieldType .STRING );
100
+ }
101
+
39
102
@ Test
40
103
public void failsFastWithNonJsonContent () {
41
104
this .thrown .expect (PayloadHandlingException .class );
0 commit comments