Skip to content

Commit 4a1742c

Browse files
committed
Add test cases for the array data type
1 parent 4528518 commit 4a1742c

File tree

2 files changed

+382
-0
lines changed

2 files changed

+382
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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.JsonParserException;
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.ELEM01;
12+
import static com.relogiclabs.json.schema.message.ErrorCode.ENUM02;
13+
import static com.relogiclabs.json.schema.message.ErrorCode.JPRS01;
14+
import static com.relogiclabs.json.schema.message.ErrorCode.NEMT02;
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
import static org.junit.jupiter.api.Assertions.assertThrows;
17+
18+
public class ArrayTests {
19+
@Test
20+
public void When_JsonNotArray_ExceptionThrown() {
21+
var schema = "#array";
22+
var json = "10";
23+
24+
JsonSchema.isValid(schema, json);
25+
var exception = assertThrows(JsonSchemaException.class,
26+
() -> JsonAssert.isValid(schema, json));
27+
assertEquals(DTYP04, exception.getCode());
28+
exception.printStackTrace();
29+
}
30+
31+
@Test
32+
public void When_JsonNotArrayInObject_ExceptionThrown() {
33+
var schema =
34+
"""
35+
{
36+
"key1": #array,
37+
"key2": #array,
38+
"key3": #array
39+
}
40+
""";
41+
var json =
42+
"""
43+
{
44+
"key1": "value1",
45+
"key2": {"key": "value"},
46+
"key3": 100000
47+
}
48+
""";
49+
JsonSchema.isValid(schema, json);
50+
var exception = assertThrows(JsonSchemaException.class,
51+
() -> JsonAssert.isValid(schema, json));
52+
assertEquals(DTYP04, exception.getCode());
53+
exception.printStackTrace();
54+
}
55+
56+
@Test
57+
public void When_JsonNotArrayInArray_ExceptionThrown() {
58+
var schema =
59+
"""
60+
[#array, #array, #array]
61+
""";
62+
var json =
63+
"""
64+
[{}, "value1", 10.5]
65+
""";
66+
JsonSchema.isValid(schema, json);
67+
var exception = assertThrows(JsonSchemaException.class,
68+
() -> JsonAssert.isValid(schema, json));
69+
assertEquals(DTYP04, exception.getCode());
70+
exception.printStackTrace();
71+
}
72+
73+
@Test
74+
public void When_NestedJsonNotArrayInArray_ExceptionThrown() {
75+
var schema =
76+
"""
77+
#array*
78+
""";
79+
var json =
80+
"""
81+
[true, "value1", false]
82+
""";
83+
JsonSchema.isValid(schema, json);
84+
var exception = assertThrows(JsonSchemaException.class,
85+
() -> JsonAssert.isValid(schema, json));
86+
assertEquals(DTYP06, exception.getCode());
87+
exception.printStackTrace();
88+
}
89+
90+
@Test
91+
public void When_NestedJsonNotArrayInObject_ExceptionThrown() {
92+
var schema =
93+
"""
94+
#array*
95+
""";
96+
var json =
97+
"""
98+
{
99+
"key1": 10.11,
100+
"key2": true,
101+
"key3": "value1"
102+
}
103+
""";
104+
JsonSchema.isValid(schema, json);
105+
var exception = assertThrows(JsonSchemaException.class,
106+
() -> JsonAssert.isValid(schema, json));
107+
assertEquals(DTYP06, exception.getCode());
108+
exception.printStackTrace();
109+
}
110+
111+
@Test
112+
public void When_ElementsWithWrongArray_ExceptionThrown() {
113+
var schema = "@elements(10, 20, 30, 40) #array";
114+
var json = "[5, 10, 15, 20, 25]";
115+
116+
JsonSchema.isValid(schema, json);
117+
var exception = assertThrows(JsonSchemaException.class,
118+
() -> JsonAssert.isValid(schema, json));
119+
assertEquals(ELEM01, exception.getCode());
120+
exception.printStackTrace();
121+
}
122+
123+
@Test
124+
public void When_NestedElementsWithWrongArrayInArray_ExceptionThrown() {
125+
var schema = "@elements*(5, 10) #array";
126+
var json = "[[5, 10], [], [5, 10, 15, 20]]";
127+
128+
JsonSchema.isValid(schema, json);
129+
var exception = assertThrows(JsonSchemaException.class,
130+
() -> JsonAssert.isValid(schema, json));
131+
assertEquals(ELEM01, exception.getCode());
132+
exception.printStackTrace();
133+
}
134+
135+
@Test
136+
public void When_EnumWithWrongValueInArray_ExceptionThrown() {
137+
var schema =
138+
"""
139+
[
140+
@enum(5, 10, 15),
141+
@enum(100, 150, 200),
142+
@enum("abc", "pqr", "xyz")
143+
] #array
144+
""";
145+
var json =
146+
"""
147+
[11, 102, "efg"]
148+
""";
149+
JsonSchema.isValid(schema, json);
150+
var exception = assertThrows(JsonSchemaException.class,
151+
() -> JsonAssert.isValid(schema, json));
152+
assertEquals(ENUM02, exception.getCode());
153+
exception.printStackTrace();
154+
}
155+
156+
@Test
157+
public void When_InvalidJsonInArray_ExceptionThrown() {
158+
var schema = "#array";
159+
var json = "[,,]";
160+
161+
//JsonSchema.IsValid(schema, json);
162+
var exception = assertThrows(JsonParserException.class,
163+
() -> JsonAssert.isValid(schema, json));
164+
assertEquals(JPRS01, exception.getCode());
165+
exception.printStackTrace();
166+
}
167+
168+
@Test
169+
public void When_EmptyArrayInObject_ExceptionThrown() {
170+
var schema =
171+
"""
172+
{
173+
"key1": @nonempty #array,
174+
"key2": @nonempty
175+
}
176+
""";
177+
var json =
178+
"""
179+
{
180+
"key1": [],
181+
"key2": []
182+
}
183+
""";
184+
JsonSchema.isValid(schema, json);
185+
var exception = assertThrows(JsonSchemaException.class,
186+
() -> JsonAssert.isValid(schema, json));
187+
assertEquals(NEMT02, exception.getCode());
188+
exception.printStackTrace();
189+
}
190+
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
package com.relogiclabs.json.schema.positive;
2+
3+
import com.relogiclabs.json.schema.JsonAssert;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class ArrayTests
7+
{
8+
@Test
9+
public void When_DataTypeArray_ValidTrue() {
10+
var schema = "#array";
11+
var json = "[10, 20, 30]";
12+
JsonAssert.isValid(schema, json);
13+
}
14+
15+
@Test
16+
public void When_DataTypeArrayInObject_ValidTrue() {
17+
var schema =
18+
"""
19+
{
20+
"key1": #array,
21+
"key2": #array,
22+
"key3": #array
23+
}
24+
""";
25+
var json =
26+
"""
27+
{
28+
"key1": [1, 10, 100],
29+
"key2": [100, 1000, [10, 10000]],
30+
"key3": [900000, 9000000, 9000000]
31+
}
32+
""";
33+
JsonAssert.isValid(schema, json);
34+
}
35+
36+
@Test
37+
public void When_DataTypeArrayInArray_ValidTrue() {
38+
var schema =
39+
"""
40+
[#array, #array, #array]
41+
""";
42+
var json =
43+
"""
44+
[[], ["value1"], [0, 100, "value3", ["value4"]]]
45+
""";
46+
JsonAssert.isValid(schema, json);
47+
}
48+
49+
@Test
50+
public void When_NestedDataTypeArrayInArray_ValidTrue() {
51+
var schema =
52+
"""
53+
#array*
54+
""";
55+
var json =
56+
"""
57+
[[], [10, 100], [20000, 40000, 6000000]]
58+
""";
59+
JsonAssert.isValid(schema, json);
60+
}
61+
62+
@Test
63+
public void When_NestedDataTypeArrayInObject_ValidTrue() {
64+
var schema =
65+
"""
66+
#array*
67+
""";
68+
var json =
69+
"""
70+
{
71+
"key1": [1, 10, 100],
72+
"key2": [100, 1000, [10, 10000]],
73+
"key3": [900000, 9000000, 9000000]
74+
}
75+
""";
76+
JsonAssert.isValid(schema, json);
77+
}
78+
79+
@Test
80+
public void When_ElementsWithArray_ValidTrue() {
81+
var schema = "@elements(10, 20, 30) #array";
82+
var json = "[5, 10, 15, 20, 25, 30]";
83+
JsonAssert.isValid(schema, json);
84+
}
85+
86+
@Test
87+
public void When_NestedElementsWithArrayInArray_ValidTrue() {
88+
var schema = "@elements*(5, 10) #array";
89+
var json = "[[5, 10], [5, 10, 15], [5, 10, 15, 20]]";
90+
JsonAssert.isValid(schema, json);
91+
}
92+
93+
@Test
94+
public void When_EnumInArray_ValidTrue() {
95+
var schema =
96+
"""
97+
[
98+
@enum(5, 10, 15),
99+
@enum(100, 150, 200),
100+
@enum("abc", "pqr", "xyz")
101+
] #array
102+
""";
103+
var json =
104+
"""
105+
[10, 100, "xyz"]
106+
""";
107+
JsonAssert.isValid(schema, json);
108+
}
109+
110+
@Test
111+
public void When_FixedLengthWithArray_ValidTrue() {
112+
var schema =
113+
"""
114+
@length(3) #array
115+
""";
116+
var json =
117+
"""
118+
[
119+
"value1",
120+
"value2",
121+
"value3"
122+
]
123+
""";
124+
JsonAssert.isValid(schema, json);
125+
}
126+
127+
@Test
128+
public void When_RangeLengthWithArray_ValidTrue() {
129+
var schema =
130+
"""
131+
@length(2, 5) #array
132+
""";
133+
var json =
134+
"""
135+
[
136+
"value1",
137+
"value2",
138+
"value3"
139+
]
140+
""";
141+
JsonAssert.isValid(schema, json);
142+
}
143+
144+
@Test
145+
public void When_NestedLengthWithUndefinedArrayInObject_ValidTrue() {
146+
var schema =
147+
"""
148+
@length*(3, !) #array*
149+
""";
150+
var json =
151+
"""
152+
{
153+
"key1": [1, 2, 3],
154+
"key2": [10, 20, 30, 40],
155+
"key3": [100, 200, 300, 400, 500]
156+
}
157+
""";
158+
JsonAssert.isValid(schema, json);
159+
}
160+
161+
@Test
162+
public void When_NestedLengthWithUndefinedArrayInArray_ValidTrue() {
163+
var schema =
164+
"""
165+
@length*(!, 5) #array*
166+
""";
167+
var json =
168+
"""
169+
[[1, 2, 3], [10, 20, 30, 40], [100, 200, 300, 400, 500]]
170+
""";
171+
JsonAssert.isValid(schema, json);
172+
}
173+
174+
@Test
175+
public void When_NonEmptyArrayInObject_ValidTrue() {
176+
var schema =
177+
"""
178+
{
179+
"key1": @nonempty #array,
180+
"key2": @nonempty #array
181+
}
182+
""";
183+
var json =
184+
"""
185+
{
186+
"key1": [10, 20, 30],
187+
"key2": ["val1", "val2"]
188+
}
189+
""";
190+
JsonAssert.isValid(schema, json);
191+
}
192+
}

0 commit comments

Comments
 (0)