16
16
17
17
package com .networknt .schema ;
18
18
19
- import com .fasterxml .jackson .databind .node .DecimalNode ;
20
- import org .junit .jupiter .api .Test ;
19
+ import static com .networknt .schema .TypeFactory .getSchemaNodeType ;
20
+ import static com .networknt .schema .TypeFactory .getValueNodeType ;
21
+ import static org .junit .jupiter .api .Assertions .assertSame ;
21
22
22
23
import java .math .BigDecimal ;
24
+ import java .nio .charset .StandardCharsets ;
23
25
24
- import static com .networknt .schema .TypeFactory .getValueNodeType ;
25
- import static org .junit .jupiter .api .Assertions .assertSame ;
26
+ import org .junit .jupiter .api .Test ;
27
+
28
+ import com .fasterxml .jackson .databind .node .DecimalNode ;
29
+ import com .fasterxml .jackson .databind .node .MissingNode ;
30
+ import com .fasterxml .jackson .databind .node .TextNode ;
31
+ import com .networknt .schema .serialization .JsonMapperFactory ;
26
32
33
+ /**
34
+ * Test for TypeFactory.
35
+ */
27
36
public class TypeFactoryTest {
28
37
29
- private static final String [] validIntegralValues = {
30
- "1" , "-1" , "0E+1" , "0E1" , "-0E+1" , "-0E1" , "10.1E+1" , "10.1E1" , "-10.1E+1" , "-10.1E1" , "1E+0" , "1E-0" ,
31
- "1E0" , "1E18" , "9223372036854775807" , "-9223372036854775808" , "1.0" , "1.00" , "-1.0" , "-1.00"
32
- };
38
+ private static final String [] validIntegralValues = { "1" , "-1" , "0E+1" , "0E1" , "-0E+1" , "-0E1" , "10.1E+1" ,
39
+ "10.1E1" , "-10.1E+1" , "-10.1E1" , "1E+0" , "1E-0" , "1E0" , "1E18" , "9223372036854775807" ,
40
+ "-9223372036854775808" , "1.0" , "1.00" , "-1.0" , "-1.00" };
33
41
34
- private static final String [] validNonIntegralNumberValues = {
35
- "1.1" , "-1.1" , "1.10"
36
- };
42
+ private static final String [] validNonIntegralNumberValues = { "1.1" , "-1.1" , "1.10" };
37
43
38
44
private final SchemaValidatorsConfig schemaValidatorsConfig = new SchemaValidatorsConfig ();
39
45
40
46
@ Test
41
- public void testIntegralValuesWithJavaSemantics () {
47
+ void testIntegralValuesWithJavaSemantics () {
42
48
schemaValidatorsConfig .setJavaSemantics (true );
43
49
for (String validValue : validIntegralValues ) {
44
50
assertSame (JsonType .INTEGER ,
@@ -53,7 +59,7 @@ public void testIntegralValuesWithJavaSemantics() {
53
59
}
54
60
55
61
@ Test
56
- public void testIntegralValuesWithoutJavaSemantics () {
62
+ void testIntegralValuesWithoutJavaSemantics () {
57
63
schemaValidatorsConfig .setJavaSemantics (false );
58
64
for (String validValue : validIntegralValues ) {
59
65
assertSame (JsonType .NUMBER ,
@@ -67,33 +73,126 @@ public void testIntegralValuesWithoutJavaSemantics() {
67
73
}
68
74
}
69
75
70
-
71
76
@ Test
72
- public void testWithLosslessNarrowing () {
77
+ void testWithLosslessNarrowing () {
73
78
schemaValidatorsConfig .setLosslessNarrowing (true );
74
79
for (String validValue : validIntegralValues ) {
75
80
assertSame (JsonType .INTEGER ,
76
- getValueNodeType (DecimalNode .valueOf (new BigDecimal ("1.0" )), schemaValidatorsConfig ),
77
- validValue );
81
+ getValueNodeType (DecimalNode .valueOf (new BigDecimal ("1.0" )), schemaValidatorsConfig ), validValue );
78
82
79
83
assertSame (JsonType .NUMBER ,
80
- getValueNodeType (DecimalNode .valueOf (new BigDecimal ("1.5" )), schemaValidatorsConfig ),
81
- validValue );
84
+ getValueNodeType (DecimalNode .valueOf (new BigDecimal ("1.5" )), schemaValidatorsConfig ), validValue );
82
85
}
83
86
}
84
87
85
88
@ Test
86
- public void testWithoutLosslessNarrowing () {
89
+ void testWithoutLosslessNarrowing () {
87
90
schemaValidatorsConfig .setLosslessNarrowing (false );
88
91
for (String validValue : validIntegralValues ) {
89
92
assertSame (JsonType .NUMBER ,
90
- getValueNodeType (DecimalNode .valueOf (new BigDecimal ("1.0" )), schemaValidatorsConfig ),
91
- validValue );
93
+ getValueNodeType (DecimalNode .valueOf (new BigDecimal ("1.0" )), schemaValidatorsConfig ), validValue );
92
94
93
95
assertSame (JsonType .NUMBER ,
94
- getValueNodeType (DecimalNode .valueOf (new BigDecimal ("1.5" )), schemaValidatorsConfig ),
95
- validValue );
96
+ getValueNodeType (DecimalNode .valueOf (new BigDecimal ("1.5" )), schemaValidatorsConfig ), validValue );
96
97
}
97
98
98
99
}
100
+
101
+ @ Test
102
+ void testObjectValue () {
103
+ assertSame (JsonType .OBJECT , getValueNodeType (JsonMapperFactory .getInstance ().getNodeFactory ().objectNode (),
104
+ schemaValidatorsConfig ));
105
+ }
106
+
107
+ @ Test
108
+ void testArrayValue () {
109
+ assertSame (JsonType .ARRAY ,
110
+ getValueNodeType (JsonMapperFactory .getInstance ().getNodeFactory ().arrayNode (), schemaValidatorsConfig ));
111
+ }
112
+
113
+ @ Test
114
+ void testBooleanValue () {
115
+ assertSame (JsonType .BOOLEAN , getValueNodeType (
116
+ JsonMapperFactory .getInstance ().getNodeFactory ().booleanNode (true ), schemaValidatorsConfig ));
117
+ }
118
+
119
+ @ Test
120
+ public void testNullValue () {
121
+ assertSame (JsonType .NULL ,
122
+ getValueNodeType (JsonMapperFactory .getInstance ().getNodeFactory ().nullNode (), schemaValidatorsConfig ));
123
+ }
124
+
125
+ @ Test
126
+ void testMissingValue () {
127
+ assertSame (JsonType .UNKNOWN , getValueNodeType (JsonMapperFactory .getInstance ().getNodeFactory ().missingNode (),
128
+ schemaValidatorsConfig ));
129
+ }
130
+
131
+ @ Test
132
+ void testIntegerValue () {
133
+ assertSame (JsonType .INTEGER , getValueNodeType (JsonMapperFactory .getInstance ().getNodeFactory ().numberNode (10 ),
134
+ schemaValidatorsConfig ));
135
+ }
136
+
137
+ @ Test
138
+ void testBinaryValue () {
139
+ assertSame (JsonType .STRING , getValueNodeType (
140
+ JsonMapperFactory .getInstance ().getNodeFactory ().binaryNode ("test" .getBytes (StandardCharsets .UTF_8 )),
141
+ schemaValidatorsConfig ));
142
+ }
143
+
144
+ @ Test
145
+ void testUnknownSchema () {
146
+ assertSame (JsonType .UNKNOWN , getSchemaNodeType (TextNode .valueOf ("unexpected" )));
147
+ }
148
+
149
+ @ Test
150
+ void testMissingSchema () {
151
+ assertSame (JsonType .UNKNOWN , getSchemaNodeType (MissingNode .getInstance ()));
152
+ }
153
+
154
+ @ Test
155
+ void testStringSchema () {
156
+ assertSame (JsonType .STRING , getSchemaNodeType (TextNode .valueOf (JsonType .STRING .toString ())));
157
+ }
158
+
159
+ @ Test
160
+ void testObjectSchema () {
161
+ assertSame (JsonType .OBJECT , getSchemaNodeType (TextNode .valueOf (JsonType .OBJECT .toString ())));
162
+ }
163
+
164
+ @ Test
165
+ void testArraySchema () {
166
+ assertSame (JsonType .ARRAY , getSchemaNodeType (TextNode .valueOf (JsonType .ARRAY .toString ())));
167
+ }
168
+
169
+ @ Test
170
+ void testBooleanSchema () {
171
+ assertSame (JsonType .BOOLEAN , getSchemaNodeType (TextNode .valueOf (JsonType .BOOLEAN .toString ())));
172
+ }
173
+
174
+ @ Test
175
+ void testNumberSchema () {
176
+ assertSame (JsonType .NUMBER , getSchemaNodeType (TextNode .valueOf (JsonType .NUMBER .toString ())));
177
+ }
178
+
179
+ @ Test
180
+ void testIntegerSchema () {
181
+ assertSame (JsonType .INTEGER , getSchemaNodeType (TextNode .valueOf (JsonType .INTEGER .toString ())));
182
+ }
183
+
184
+ @ Test
185
+ void testAnySchema () {
186
+ assertSame (JsonType .ANY , getSchemaNodeType (TextNode .valueOf (JsonType .ANY .toString ())));
187
+ }
188
+
189
+ @ Test
190
+ void testNullSchema () {
191
+ assertSame (JsonType .NULL , getSchemaNodeType (TextNode .valueOf (JsonType .NULL .toString ())));
192
+ }
193
+
194
+ @ Test
195
+ void testUnionSchema () {
196
+ assertSame (JsonType .UNION , getSchemaNodeType (JsonMapperFactory .getInstance ().getNodeFactory ().arrayNode ()));
197
+ }
99
198
}
0 commit comments