Skip to content

Commit 4b4f579

Browse files
committed
Add test cases for the pragmas
1 parent a3c1bf6 commit 4b4f579

File tree

2 files changed

+289
-0
lines changed

2 files changed

+289
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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.DuplicatePragmaException;
6+
import com.relogiclabs.json.schema.exception.InvalidPragmaValueException;
7+
import com.relogiclabs.json.schema.exception.JsonSchemaException;
8+
import com.relogiclabs.json.schema.exception.PragmaNotFoundException;
9+
import com.relogiclabs.json.schema.exception.SchemaParserException;
10+
import org.junit.jupiter.api.Test;
11+
12+
import static com.relogiclabs.json.schema.message.ErrorCode.FLOT01;
13+
import static com.relogiclabs.json.schema.message.ErrorCode.PRAG01;
14+
import static com.relogiclabs.json.schema.message.ErrorCode.PRAG02;
15+
import static com.relogiclabs.json.schema.message.ErrorCode.PRAG03;
16+
import static com.relogiclabs.json.schema.message.ErrorCode.PROP06;
17+
import static com.relogiclabs.json.schema.message.ErrorCode.PROP07;
18+
import static com.relogiclabs.json.schema.message.ErrorCode.SPRS01;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
21+
22+
public class PragmaTests {
23+
@Test
24+
public void When_UndefinedPropertyOfObject_ExceptionThrown() {
25+
var schema =
26+
"""
27+
%pragma IgnoreUndefinedProperties: false
28+
%schema:
29+
{
30+
"key1": #integer
31+
}
32+
""";
33+
var json =
34+
"""
35+
{
36+
"key1": 10,
37+
"key2": "value1"
38+
}
39+
""";
40+
JsonSchema.isValid(schema, json);
41+
var exception = assertThrows(JsonSchemaException.class,
42+
() -> JsonAssert.isValid(schema, json));
43+
assertEquals(PROP06, exception.getCode());
44+
exception.printStackTrace();
45+
}
46+
47+
@Test
48+
public void When_InvalidUndefinedPropertyValueMissing_ExceptionThrown() {
49+
var schema =
50+
"""
51+
%pragma IgnoreUndefinedProperties:
52+
%schema:
53+
{
54+
"key1": #integer
55+
}
56+
""";
57+
var json =
58+
"""
59+
{
60+
"key1": 10,
61+
"key2": "value1"
62+
}
63+
""";
64+
//JsonSchema.IsValid(schema, json);
65+
var exception = assertThrows(SchemaParserException.class,
66+
() -> JsonAssert.isValid(schema, json));
67+
assertEquals(SPRS01, exception.getCode());
68+
exception.printStackTrace();
69+
}
70+
71+
@Test
72+
public void When_IgnoreUndefinedPropertiesMalformed_ExceptionThrown() {
73+
var schema =
74+
"""
75+
%pragma IgnoreUndefinedProperty: true
76+
%schema:
77+
{
78+
"key1": #integer
79+
}
80+
""";
81+
var json =
82+
"""
83+
{
84+
"key1": 10,
85+
"key2": "value1"
86+
}
87+
""";
88+
//JsonSchema.IsValid(schema, json);
89+
var exception = assertThrows(PragmaNotFoundException.class,
90+
() -> JsonAssert.isValid(schema, json));
91+
assertEquals(PRAG01, exception.getCode());
92+
exception.printStackTrace();
93+
}
94+
95+
@Test
96+
public void When_InvalidUndefinedPropertyValue_ExceptionThrown() {
97+
var schema =
98+
"""
99+
%pragma IgnoreUndefinedProperties: 1
100+
%schema:
101+
{
102+
"key1": #integer
103+
}
104+
""";
105+
var json =
106+
"""
107+
{
108+
"key1": 10,
109+
"key2": "value1"
110+
}
111+
""";
112+
//JsonSchema.IsValid(schema, json);
113+
var exception = assertThrows(InvalidPragmaValueException.class,
114+
() -> JsonAssert.isValid(schema, json));
115+
assertEquals(PRAG02, exception.getCode());
116+
exception.printStackTrace();
117+
}
118+
119+
@Test
120+
public void When_IgnorePropertyOrderOfObject_ExceptionThrown() {
121+
var schema =
122+
"""
123+
%pragma IgnoreObjectPropertyOrder: false
124+
%schema:
125+
{
126+
"key1": #integer,
127+
"key2": #string,
128+
"key3": #float
129+
}
130+
""";
131+
var json =
132+
"""
133+
{
134+
"key1": 10,
135+
"key3": 2.1,
136+
"key2": "value1"
137+
}
138+
""";
139+
JsonSchema.isValid(schema, json);
140+
var exception = assertThrows(JsonSchemaException.class,
141+
() -> JsonAssert.isValid(schema, json));
142+
assertEquals(PROP07, exception.getCode());
143+
exception.printStackTrace();
144+
}
145+
146+
@Test
147+
public void When_FloatingPointToleranceOfNumber_ExceptionThrown() {
148+
var schema =
149+
"""
150+
%pragma FloatingPointTolerance: 0.00001
151+
%schema:
152+
{
153+
"key1": 5.00 #float,
154+
"key2": 10.00E+0 #double
155+
}
156+
""";
157+
var json =
158+
"""
159+
{
160+
"key1": 5.00002,
161+
"key2": 10.0002E+0
162+
}
163+
""";
164+
JsonSchema.isValid(schema, json);
165+
var exception = assertThrows(JsonSchemaException.class,
166+
() -> JsonAssert.isValid(schema, json));
167+
assertEquals(FLOT01, exception.getCode());
168+
exception.printStackTrace();
169+
}
170+
171+
@Test
172+
public void When_DuplicatePragmaAssign_ExceptionThrown() {
173+
var schema =
174+
"""
175+
%pragma IgnoreUndefinedProperties: false
176+
%pragma IgnoreUndefinedProperties: false
177+
178+
%schema:
179+
{
180+
"key1": #integer
181+
}
182+
""";
183+
var json =
184+
"""
185+
{
186+
"key1": 10,
187+
"key2": "value1"
188+
}
189+
""";
190+
//JsonSchema.IsValid(schema, json);
191+
var exception = assertThrows(DuplicatePragmaException.class,
192+
() -> JsonAssert.isValid(schema, json));
193+
assertEquals(PRAG03, exception.getCode());
194+
exception.printStackTrace();
195+
}
196+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 PragmaTests {
7+
@Test
8+
public void When_UndefinedPropertyInObject_ValidTrue() {
9+
var schema =
10+
"""
11+
%pragma IgnoreUndefinedProperties: true
12+
%schema:
13+
{
14+
"key1": #integer
15+
}
16+
""";
17+
var json =
18+
"""
19+
{
20+
"key1": 10,
21+
"key2": "value1"
22+
}
23+
""";
24+
JsonAssert.isValid(schema, json);
25+
}
26+
27+
@Test
28+
public void When_IgnorePropertyOrderFalseOfObject_ValidTrue() {
29+
var schema =
30+
"""
31+
%pragma IgnoreObjectPropertyOrder: false
32+
%schema:
33+
{
34+
"key1": #integer,
35+
"key2": #string,
36+
"key3": #float
37+
}
38+
""";
39+
var json =
40+
"""
41+
{
42+
"key1": 10,
43+
"key2": "value1",
44+
"key3": 2.1
45+
}
46+
""";
47+
JsonAssert.isValid(schema, json);
48+
}
49+
50+
@Test
51+
public void When_IgnorePropertyOrderTrueOfObject_ValidTrue() {
52+
var schema =
53+
"""
54+
%pragma IgnoreObjectPropertyOrder: true
55+
%schema:
56+
{
57+
"key1": #integer,
58+
"key2": #string,
59+
"key3": #float
60+
}
61+
""";
62+
var json =
63+
"""
64+
{
65+
"key1": 10,
66+
"key3": 2.1,
67+
"key2": "value1"
68+
}
69+
""";
70+
JsonAssert.isValid(schema, json);
71+
}
72+
73+
@Test
74+
public void When_FloatingPointToleranceOfNumber_ValidTrue() {
75+
var schema =
76+
"""
77+
%pragma FloatingPointTolerance: 0.00001
78+
%schema:
79+
{
80+
"key1": 5.00 #float,
81+
"key2": 10.00E+0 #double
82+
}
83+
""";
84+
var json =
85+
"""
86+
{
87+
"key1": 5.000001,
88+
"key2": 10.000001E+0
89+
}
90+
""";
91+
JsonAssert.isValid(schema, json);
92+
}
93+
}

0 commit comments

Comments
 (0)