Skip to content

Commit 28ac3e7

Browse files
committed
Add test cases for the boolean data type
1 parent d1fa3d5 commit 28ac3e7

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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.JsonSchemaException;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static com.relogiclabs.json.schema.message.ErrorCode.BOOL01;
9+
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP04;
10+
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP06;
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertThrows;
13+
14+
public class BooleanTests {
15+
@Test
16+
public void When_JsonNotBoolean_ExceptionThrown() {
17+
var schema = "#boolean";
18+
var json = "5";
19+
20+
JsonSchema.isValid(schema, json);
21+
var exception = assertThrows(JsonSchemaException.class,
22+
() -> JsonAssert.isValid(schema, json));
23+
assertEquals(DTYP04, exception.getCode());
24+
exception.printStackTrace();
25+
}
26+
27+
@Test
28+
public void When_JsonValueNotEqualForBoolean_ExceptionThrown() {
29+
var schema = "true #boolean";
30+
var json = "false";
31+
32+
JsonSchema.isValid(schema, json);
33+
var exception = assertThrows(JsonSchemaException.class,
34+
() -> JsonAssert.isValid(schema, json));
35+
assertEquals(BOOL01, exception.getCode());
36+
exception.printStackTrace();
37+
}
38+
39+
@Test
40+
public void When_JsonNotBooleanInObject_ExceptionThrown() {
41+
var schema =
42+
"""
43+
{
44+
"key1": #boolean,
45+
"key2": #boolean,
46+
"key3": #boolean
47+
}
48+
""";
49+
var json =
50+
"""
51+
{
52+
"key1": null,
53+
"key2": 10.5,
54+
"key3": true
55+
}
56+
""";
57+
JsonSchema.isValid(schema, json);
58+
var exception = assertThrows(JsonSchemaException.class,
59+
() -> JsonAssert.isValid(schema, json));
60+
assertEquals(DTYP04, exception.getCode());
61+
exception.printStackTrace();
62+
}
63+
64+
@Test
65+
public void When_JsonNotBooleanInArray_ExceptionThrown() {
66+
var schema =
67+
"""
68+
[#boolean, #boolean, #boolean]
69+
""";
70+
var json =
71+
"""
72+
[[], 11.5, "false"]
73+
""";
74+
JsonSchema.isValid(schema, json);
75+
var exception = assertThrows(JsonSchemaException.class,
76+
() -> JsonAssert.isValid(schema, json));
77+
assertEquals(DTYP04, exception.getCode());
78+
exception.printStackTrace();
79+
}
80+
81+
@Test
82+
public void When_NestedJsonNotBooleanInArray_ExceptionThrown() {
83+
var schema =
84+
"""
85+
#boolean*
86+
""";
87+
var json =
88+
"""
89+
["true", {}, [true]]
90+
""";
91+
JsonSchema.isValid(schema, json);
92+
var exception = assertThrows(JsonSchemaException.class,
93+
() -> JsonAssert.isValid(schema, json));
94+
assertEquals(DTYP06, exception.getCode());
95+
exception.printStackTrace();
96+
}
97+
98+
@Test
99+
public void When_NestedJsonNotBooleanInObject_ExceptionThrown() {
100+
var schema =
101+
"""
102+
#boolean*
103+
""";
104+
var json =
105+
"""
106+
{
107+
"key1": {"key": true},
108+
"key2": null,
109+
"key3": false
110+
}
111+
""";
112+
JsonSchema.isValid(schema, json);
113+
var exception = assertThrows(JsonSchemaException.class,
114+
() -> JsonAssert.isValid(schema, json));
115+
assertEquals(DTYP06, exception.getCode());
116+
exception.printStackTrace();
117+
}
118+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 BooleanTests {
7+
8+
@Test
9+
public void When_DataTypeBoolean_ValidTrue() {
10+
var schema = "#boolean";
11+
var json = "true";
12+
JsonAssert.isValid(schema, json);
13+
}
14+
15+
@Test
16+
public void When_DataTypeBooleanInObject_ValidTrue() {
17+
var schema =
18+
"""
19+
{
20+
"key1": #boolean,
21+
"key2": #boolean,
22+
"key3": #boolean
23+
}
24+
""";
25+
var json =
26+
"""
27+
{
28+
"key1": true,
29+
"key2": false,
30+
"key3": true
31+
}
32+
""";
33+
JsonAssert.isValid(schema, json);
34+
}
35+
36+
@Test
37+
public void When_DataTypeBooleanInArray_ValidTrue() {
38+
var schema =
39+
"""
40+
[#boolean, #boolean, #boolean]
41+
""";
42+
var json =
43+
"""
44+
[true, true, false]
45+
""";
46+
JsonAssert.isValid(schema, json);
47+
}
48+
49+
@Test
50+
public void When_NestedDataTypeBooleanInArray_ValidTrue() {
51+
var schema =
52+
"""
53+
#boolean*
54+
""";
55+
var json =
56+
"""
57+
[false, true, false]
58+
""";
59+
JsonAssert.isValid(schema, json);
60+
}
61+
62+
@Test
63+
public void When_NestedDataTypeBooleanInObject_ValidTrue() {
64+
var schema =
65+
"""
66+
#boolean*
67+
""";
68+
var json =
69+
"""
70+
{
71+
"key1": true,
72+
"key2": true,
73+
"key3": false
74+
}
75+
""";
76+
JsonAssert.isValid(schema, json);
77+
}
78+
}

0 commit comments

Comments
 (0)