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+ }
0 commit comments