Skip to content

Commit 8643964

Browse files
committed
Add test cases for the object data type
1 parent 4a1742c commit 8643964

File tree

2 files changed

+593
-0
lines changed

2 files changed

+593
-0
lines changed
Lines changed: 377 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,377 @@
1+
package com.relogiclabs.json.schema.negative;
2+
3+
import com.relogiclabs.json.schema.JsonAssert;
4+
import com.relogiclabs.json.schema.JsonSchema;
5+
import com.relogiclabs.json.schema.exception.DuplicatePropertyKeyException;
6+
import com.relogiclabs.json.schema.exception.JsonSchemaException;
7+
import org.junit.jupiter.api.Test;
8+
9+
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP04;
10+
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP06;
11+
import static com.relogiclabs.json.schema.message.ErrorCode.ENUM02;
12+
import static com.relogiclabs.json.schema.message.ErrorCode.KEYS01;
13+
import static com.relogiclabs.json.schema.message.ErrorCode.NEMT03;
14+
import static com.relogiclabs.json.schema.message.ErrorCode.OLEN01;
15+
import static com.relogiclabs.json.schema.message.ErrorCode.OLEN02;
16+
import static com.relogiclabs.json.schema.message.ErrorCode.OLEN04;
17+
import static com.relogiclabs.json.schema.message.ErrorCode.OLEN05;
18+
import static com.relogiclabs.json.schema.message.ErrorCode.PROP03;
19+
import static com.relogiclabs.json.schema.message.ErrorCode.VALU01;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
22+
23+
public class ObjectTests {
24+
@Test
25+
public void When_JsonNotObject_ExceptionThrown() {
26+
var schema = "#object";
27+
var json = "100";
28+
29+
JsonSchema.isValid(schema, json);
30+
var exception = assertThrows(JsonSchemaException.class,
31+
() -> JsonAssert.isValid(schema, json));
32+
assertEquals(DTYP04, exception.getCode());
33+
exception.printStackTrace();
34+
}
35+
36+
@Test
37+
public void When_JsonNotObjectInObject_ExceptionThrown() {
38+
var schema =
39+
"""
40+
{
41+
"key1": #object,
42+
"key2": #object,
43+
"key3": #object
44+
}
45+
""";
46+
var json =
47+
"""
48+
{
49+
"key1": [],
50+
"key2": "value1",
51+
"key3": [10, 20, 30]
52+
}
53+
""";
54+
JsonSchema.isValid(schema, json);
55+
var exception = assertThrows(JsonSchemaException.class,
56+
() -> JsonAssert.isValid(schema, json));
57+
assertEquals(DTYP04, exception.getCode());
58+
exception.printStackTrace();
59+
}
60+
61+
@Test
62+
public void When_JsonNotObjectInArray_ExceptionThrown() {
63+
var schema =
64+
"""
65+
[#object, #object, #object]
66+
""";
67+
var json =
68+
"""
69+
[null, "value1", true]
70+
""";
71+
JsonSchema.isValid(schema, json);
72+
var exception = assertThrows(JsonSchemaException.class,
73+
() -> JsonAssert.isValid(schema, json));
74+
assertEquals(DTYP04, exception.getCode());
75+
exception.printStackTrace();
76+
}
77+
78+
@Test
79+
public void When_NestedJsonNotObjectInArray_ExceptionThrown() {
80+
var schema =
81+
"""
82+
#object*
83+
""";
84+
var json =
85+
"""
86+
[ 100, true, false ]
87+
""";
88+
JsonSchema.isValid(schema, json);
89+
var exception = assertThrows(JsonSchemaException.class,
90+
() -> JsonAssert.isValid(schema, json));
91+
assertEquals(DTYP06, exception.getCode());
92+
exception.printStackTrace();
93+
}
94+
95+
@Test
96+
public void When_NestedJsonNotObjectInObject_ExceptionThrown() {
97+
var schema =
98+
"""
99+
#object*
100+
""";
101+
var json =
102+
"""
103+
{
104+
"key1": 15.4,
105+
"key2": 0,
106+
"key3": [10]
107+
}
108+
""";
109+
JsonSchema.isValid(schema, json);
110+
var exception = assertThrows(JsonSchemaException.class,
111+
() -> JsonAssert.isValid(schema, json));
112+
assertEquals(DTYP06, exception.getCode());
113+
exception.printStackTrace();
114+
}
115+
116+
@Test
117+
public void When_KeysWithWrongObject_ExceptionThrown() {
118+
var schema =
119+
"""
120+
@keys("key1", "key2") #integer*
121+
""";
122+
var json =
123+
"""
124+
{
125+
"key4": 100,
126+
"key5": 150,
127+
"key6": 200
128+
}
129+
""";
130+
JsonSchema.isValid(schema, json);
131+
var exception = assertThrows(JsonSchemaException.class,
132+
() -> JsonAssert.isValid(schema, json));
133+
assertEquals(KEYS01, exception.getCode());
134+
exception.printStackTrace();
135+
}
136+
137+
@Test
138+
public void When_ValuesWithWrongObject_ExceptionThrown() {
139+
var schema =
140+
"""
141+
@values(100, 200) #integer*
142+
""";
143+
var json =
144+
"""
145+
{
146+
"key1": 1,
147+
"key2": 2,
148+
"key3": 3
149+
}
150+
""";
151+
JsonSchema.isValid(schema, json);
152+
var exception = assertThrows(JsonSchemaException.class,
153+
() -> JsonAssert.isValid(schema, json));
154+
assertEquals(VALU01, exception.getCode());
155+
exception.printStackTrace();
156+
}
157+
158+
@Test
159+
public void When_NestedKeysWithWrongObjectInObject_ExceptionThrown() {
160+
var schema =
161+
"""
162+
@keys*("key") #object*
163+
""";
164+
var json =
165+
"""
166+
{
167+
"key1": {"value": 10},
168+
"key2": {"value": 150},
169+
"key3": {"value": 1000}
170+
}
171+
""";
172+
JsonSchema.isValid(schema, json);
173+
var exception = assertThrows(JsonSchemaException.class,
174+
() -> JsonAssert.isValid(schema, json));
175+
assertEquals(KEYS01, exception.getCode());
176+
exception.printStackTrace();
177+
}
178+
179+
@Test
180+
public void When_NestedKeysAndValuesWithWrongObjectInArray_ExceptionThrown() {
181+
var schema =
182+
"""
183+
@keys*("key1", "key2") @values*(100, 200) #object*
184+
""";
185+
var json =
186+
"""
187+
[{"value": 10}, {"value": 20}, {"value": 30}]
188+
""";
189+
JsonSchema.isValid(schema, json);
190+
var exception = assertThrows(JsonSchemaException.class,
191+
() -> JsonAssert.isValid(schema, json));
192+
assertEquals(KEYS01, exception.getCode());
193+
exception.printStackTrace();
194+
}
195+
196+
@Test
197+
public void When_EnumWithWrongObject_ExceptionThrown() {
198+
var schema =
199+
"""
200+
{
201+
"key1": @enum(5, 10, 15),
202+
"key2": @enum(100, 150, 200),
203+
"key3": @enum("abc", "pqr", "xyz")
204+
} #object
205+
""";
206+
var json =
207+
"""
208+
{
209+
"key1": 1,
210+
"key2": 10,
211+
"key3": "efg"
212+
}
213+
""";
214+
JsonSchema.isValid(schema, json);
215+
var exception = assertThrows(JsonSchemaException.class,
216+
() -> JsonAssert.isValid(schema, json));
217+
assertEquals(ENUM02, exception.getCode());
218+
exception.printStackTrace();
219+
}
220+
221+
@Test
222+
public void When_DuplicateJsonPropertyInObject_ExceptionThrown() {
223+
var schema =
224+
"""
225+
{
226+
"key1": #object,
227+
"key2": #object,
228+
"key3": #object
229+
}
230+
""";
231+
var json =
232+
"""
233+
{
234+
"key1": [],
235+
"key2": "value1",
236+
"key2": [10, 20, 30]
237+
}
238+
""";
239+
//JsonSchema.IsValid(schema, json);
240+
var exception = assertThrows(DuplicatePropertyKeyException.class,
241+
() -> JsonAssert.isValid(schema, json));
242+
assertEquals(PROP03, exception.getCode());
243+
exception.printStackTrace();
244+
}
245+
246+
@Test
247+
public void When_DuplicateSchemaPropertyInObject_ExceptionThrown() {
248+
var schema =
249+
"""
250+
{
251+
"key1": #object,
252+
"key2": #object,
253+
"key2": #object
254+
}
255+
""";
256+
var json =
257+
"""
258+
{
259+
"key1": [],
260+
"key2": "value1",
261+
"key3": [10, 20, 30]
262+
}
263+
""";
264+
//JsonSchema.IsValid(schema, json);
265+
var exception = assertThrows(DuplicatePropertyKeyException.class,
266+
() -> JsonAssert.isValid(schema, json));
267+
assertEquals(PROP03, exception.getCode());
268+
exception.printStackTrace();
269+
}
270+
271+
@Test
272+
public void When_EmptyObjectInArray_ExceptionThrown() {
273+
var schema =
274+
"""
275+
[
276+
@nonempty #object,
277+
@nonempty #object
278+
]
279+
""";
280+
var json =
281+
"""
282+
[
283+
{ },
284+
{ "key1": ["val1", "val2"] }
285+
]
286+
""";
287+
JsonSchema.isValid(schema, json);
288+
var exception = assertThrows(JsonSchemaException.class,
289+
() -> JsonAssert.isValid(schema, json));
290+
assertEquals(NEMT03, exception.getCode());
291+
exception.printStackTrace();
292+
}
293+
294+
@Test
295+
public void When_WrongLengthOfObjectInArray1_ExceptionThrown() {
296+
var schema =
297+
"""
298+
[
299+
@length(1) #object
300+
]
301+
""";
302+
var json =
303+
"""
304+
[
305+
{ "key1": 10, "key2": 20 }
306+
]
307+
""";
308+
JsonSchema.isValid(schema, json);
309+
var exception = assertThrows(JsonSchemaException.class,
310+
() -> JsonAssert.isValid(schema, json));
311+
assertEquals(OLEN01, exception.getCode());
312+
exception.printStackTrace();
313+
}
314+
315+
@Test
316+
public void When_WrongLengthOfObjectInArray2_ExceptionThrown() {
317+
var schema =
318+
"""
319+
[
320+
@length(1, 4) #object
321+
]
322+
""";
323+
var json =
324+
"""
325+
[
326+
{ }
327+
]
328+
""";
329+
JsonSchema.isValid(schema, json);
330+
var exception = assertThrows(JsonSchemaException.class,
331+
() -> JsonAssert.isValid(schema, json));
332+
assertEquals(OLEN02, exception.getCode());
333+
exception.printStackTrace();
334+
}
335+
336+
@Test
337+
public void When_WrongLengthOfObjectInArray3_ExceptionThrown() {
338+
var schema =
339+
"""
340+
[
341+
@length(!, 4) #object
342+
]
343+
""";
344+
var json =
345+
"""
346+
[
347+
{ "key1": 10, "key2": 20, "key3": 30, "key4": 40, "key5": 50 }
348+
]
349+
""";
350+
JsonSchema.isValid(schema, json);
351+
var exception = assertThrows(JsonSchemaException.class,
352+
() -> JsonAssert.isValid(schema, json));
353+
assertEquals(OLEN05, exception.getCode());
354+
exception.printStackTrace();
355+
}
356+
357+
@Test
358+
public void When_WrongLengthOfObjectInArray4_ExceptionThrown() {
359+
var schema =
360+
"""
361+
[
362+
@length(2, !) #object
363+
]
364+
""";
365+
var json =
366+
"""
367+
[
368+
{ "key1": 10 }
369+
]
370+
""";
371+
JsonSchema.isValid(schema, json);
372+
var exception = assertThrows(JsonSchemaException.class,
373+
() -> JsonAssert.isValid(schema, json));
374+
assertEquals(OLEN04, exception.getCode());
375+
exception.printStackTrace();
376+
}
377+
}

0 commit comments

Comments
 (0)