Skip to content

Commit acbe7f0

Browse files
committed
Add test cases for defining components
1 parent ff78fb7 commit acbe7f0

File tree

2 files changed

+232
-0
lines changed

2 files changed

+232
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.relogiclabs.json.schema.negative;
2+
3+
import com.relogiclabs.json.schema.JsonAssert;
4+
import com.relogiclabs.json.schema.exception.DefinitionNotFoundException;
5+
import com.relogiclabs.json.schema.exception.DuplicateDefinitionException;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static com.relogiclabs.json.schema.message.ErrorCode.DEFI01;
9+
import static com.relogiclabs.json.schema.message.ErrorCode.DEFI02;
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
12+
13+
public class ComponentTests {
14+
@Test
15+
public void When_ComponentNotDefined_ExceptionThrown() {
16+
var schema =
17+
"""
18+
%schema: {
19+
"key1": $cmp1,
20+
"key2": $cmp1
21+
}
22+
""";
23+
var json =
24+
"""
25+
{
26+
"key1": { "key1": 10, "key2": "value11" },
27+
"key2": { "key1": 20, "key2": "value22" }
28+
}
29+
""";
30+
//JsonSchema.IsValid(schema, json);
31+
var exception = assertThrows(DefinitionNotFoundException.class,
32+
() -> JsonAssert.isValid(schema, json));
33+
assertEquals(DEFI02, exception.getCode());
34+
exception.printStackTrace();
35+
}
36+
37+
@Test
38+
public void When_ComponentWithDuplicateDefinition_ExceptionThrown() {
39+
var schema =
40+
"""
41+
%define $cmp1: { "key1": #integer, "key2": #string }
42+
%schema: {
43+
"key1": $cmp1,
44+
"key2": $cmp1
45+
}
46+
%define $cmp1: [#string, #string]
47+
""";
48+
var json =
49+
"""
50+
{
51+
"key1": { "key1": 10, "key2": "value11" },
52+
"key2": { "key1": 20, "key2": "value22" }
53+
}
54+
""";
55+
//JsonSchema.IsValid(schema, json);
56+
var exception = assertThrows(DuplicateDefinitionException.class,
57+
() -> JsonAssert.isValid(schema, json));
58+
assertEquals(DEFI01, exception.getCode());
59+
exception.printStackTrace();
60+
}
61+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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 ComponentTests {
7+
@Test
8+
public void When_ComponentObjectInObject_ValidTrue() {
9+
var schema =
10+
"""
11+
%define $cmp1: { "key1": #integer, "key2": #string }
12+
%schema: {
13+
"key1": $cmp1,
14+
"key2": $cmp1,
15+
"key3": $cmp1
16+
}
17+
""";
18+
var json =
19+
"""
20+
{
21+
"key1": { "key1": 10, "key2": "value11" },
22+
"key2": { "key1": 20, "key2": "value22" },
23+
"key3": { "key1": 30, "key2": "value33" }
24+
}
25+
""";
26+
JsonAssert.isValid(schema, json);
27+
}
28+
29+
@Test
30+
public void When_ComponentObjectInArray_ValidTrue() {
31+
var schema =
32+
"""
33+
%define $cmp1: { "key1": #integer, "key2": #float }
34+
%schema: [$cmp1, $cmp1, $cmp1]
35+
""";
36+
var json =
37+
"""
38+
[
39+
{ "key1": 10, "key2": 2.5 },
40+
{ "key1": 20, "key2": 3.5 },
41+
{ "key1": 30, "key2": 4.5 }
42+
]
43+
""";
44+
JsonAssert.isValid(schema, json);
45+
}
46+
47+
@Test
48+
public void When_ComponentArrayInArray_ValidTrue() {
49+
var schema =
50+
"""
51+
%define $cmp1: [#number, #number]
52+
%schema: [$cmp1, $cmp1, $cmp1]
53+
""";
54+
var json =
55+
"""
56+
[
57+
[10, 2.5],
58+
[20, 3.5],
59+
[30, 4.5]
60+
]
61+
""";
62+
JsonAssert.isValid(schema, json);
63+
}
64+
65+
@Test
66+
public void When_ComponentArrayInObject_ValidTrue() {
67+
var schema =
68+
"""
69+
%define $cmp1: [#number, #number]
70+
%schema: {
71+
"key1": $cmp1,
72+
"key2": $cmp1,
73+
"key3": $cmp1
74+
}
75+
""";
76+
var json =
77+
"""
78+
{
79+
"key1": [10, 2.5],
80+
"key2": [20, 3.5],
81+
"key3": [30, 4.5]
82+
}
83+
""";
84+
JsonAssert.isValid(schema, json);
85+
}
86+
87+
@Test
88+
public void When_RangeWithComponentObjectInArray_ValidTrue() {
89+
var schema =
90+
"""
91+
%define $cmp1: { "key1": @range(10, 30) #integer, "key2": @range(2.5, 4.5) #float }
92+
%schema: [$cmp1, $cmp1, $cmp1]
93+
""";
94+
var json =
95+
"""
96+
[
97+
{ "key1": 10, "key2": 2.5 },
98+
{ "key1": 20, "key2": 3.5 },
99+
{ "key1": 30, "key2": 4.5 }
100+
]
101+
""";
102+
JsonAssert.isValid(schema, json);
103+
}
104+
105+
@Test
106+
public void When_NestedComponentObjectWithObjectInArray_ValidTrue() {
107+
var schema =
108+
"""
109+
%define $cmp1:
110+
{
111+
"key1": @range(10, 30) #integer,
112+
"key2": @enum("val1", "val2", "val3") #string
113+
}
114+
%schema: #object*($cmp1) #array
115+
""";
116+
var json =
117+
"""
118+
[
119+
{ "key1": 10, "key2": "val1" },
120+
{ "key1": 20, "key2": "val2" },
121+
{ "key1": 30, "key2": "val3" }
122+
]
123+
""";
124+
JsonAssert.isValid(schema, json);
125+
}
126+
127+
@Test
128+
public void When_ComponentInComponentWithArrayInObject_ValidTrue() {
129+
var schema =
130+
"""
131+
%define $cmp1: {
132+
"key": @range(10, 100) #number
133+
}
134+
%define $cmp2: [ $cmp1, $cmp1 ]
135+
%schema: {
136+
"key1": $cmp2,
137+
"key2": $cmp2,
138+
"key3": $cmp2
139+
}
140+
""";
141+
var json =
142+
"""
143+
{
144+
"key1": [{"key": 10}, {"key": 11}],
145+
"key2": [{"key": 20}, {"key": 21}],
146+
"key3": [{"key": 30}, {"key": 31}]
147+
}
148+
""";
149+
JsonAssert.isValid(schema, json);
150+
}
151+
152+
@Test
153+
public void When_CompositeAndPrimitiveDataTypeWithPrimitiveInput_ValidTrue() {
154+
var schema = "@range*(5, 10) #array #string";
155+
var json =
156+
"""
157+
"value1"
158+
""";
159+
JsonAssert.isValid(schema, json);
160+
}
161+
162+
@Test
163+
public void When_CompositeAndPrimitiveDataTypeWithCompositeInput_ValidTrue() {
164+
var schema = "@range*(5, 10) #array #string";
165+
var json =
166+
"""
167+
[5, 6, 7]
168+
""";
169+
JsonAssert.isValid(schema, json);
170+
}
171+
}

0 commit comments

Comments
 (0)