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 .DTYP04 ;
9+ import static com .relogiclabs .json .schema .message .ErrorCode .DTYP06 ;
10+ import static com .relogiclabs .json .schema .message .ErrorCode .FUNC06 ;
11+ import static com .relogiclabs .json .schema .message .ErrorCode .INTE01 ;
12+ import static com .relogiclabs .json .schema .message .ErrorCode .NEGI01 ;
13+ import static com .relogiclabs .json .schema .message .ErrorCode .POSI01 ;
14+ import static com .relogiclabs .json .schema .message .ErrorCode .RANG01 ;
15+ import static com .relogiclabs .json .schema .message .ErrorCode .RANG04 ;
16+ import static org .junit .jupiter .api .Assertions .assertEquals ;
17+ import static org .junit .jupiter .api .Assertions .assertThrows ;
18+
19+ public class IntegerTests {
20+ @ Test
21+ public void When_JsonNotInteger_ExceptionThrown () {
22+ var schema = "#integer" ;
23+ var json = "10.5" ;
24+
25+ JsonSchema .isValid (schema , json );
26+ var exception = assertThrows (JsonSchemaException .class ,
27+ () -> JsonAssert .isValid (schema , json ));
28+ assertEquals (DTYP04 , exception .getCode ());
29+ exception .printStackTrace ();
30+ }
31+
32+ @ Test
33+ public void When_JsonValueNotEqualForInteger_ExceptionThrown () {
34+ var schema = "10 #integer" ;
35+ var json = "9" ;
36+
37+ JsonSchema .isValid (schema , json );
38+ var exception = assertThrows (JsonSchemaException .class ,
39+ () -> JsonAssert .isValid (schema , json ));
40+ assertEquals (INTE01 , exception .getCode ());
41+ exception .printStackTrace ();
42+ }
43+
44+ @ Test
45+ public void When_JsonNotIntegerInObject_ExceptionThrown () {
46+ var schema =
47+ """
48+ {
49+ "key1": #integer,
50+ "key2": #integer,
51+ "key3": #integer
52+ }
53+ """ ;
54+ var json =
55+ """
56+ {
57+ "key1": null,
58+ "key2": "value1",
59+ "key3": 4000.45
60+ }
61+ """ ;
62+ JsonSchema .isValid (schema , json );
63+ var exception = assertThrows (JsonSchemaException .class ,
64+ () -> JsonAssert .isValid (schema , json ));
65+ assertEquals (DTYP04 , exception .getCode ());
66+ exception .printStackTrace ();
67+ }
68+
69+ @ Test
70+ public void When_JsonNotIntegerInArray_ExceptionThrown () {
71+ var schema =
72+ """
73+ [#integer, #integer, #integer]
74+ """ ;
75+ var json =
76+ """
77+ [true, -4568.57, 100]
78+ """ ;
79+ JsonSchema .isValid (schema , json );
80+ var exception = assertThrows (JsonSchemaException .class ,
81+ () -> JsonAssert .isValid (schema , json ));
82+ assertEquals (DTYP04 , exception .getCode ());
83+ exception .printStackTrace ();
84+ }
85+
86+ @ Test
87+ public void When_NestedJsonNotIntegerInArray_ExceptionThrown () {
88+ var schema =
89+ """
90+ #integer*
91+ """ ;
92+ var json =
93+ """
94+ [null, 2.2, "40000000"]
95+ """ ;
96+ JsonSchema .isValid (schema , json );
97+ var exception = assertThrows (JsonSchemaException .class ,
98+ () -> JsonAssert .isValid (schema , json ));
99+ assertEquals (DTYP06 , exception .getCode ());
100+ exception .printStackTrace ();
101+ }
102+
103+ @ Test
104+ public void When_NestedJsonNotIntegerInObject_ExceptionThrown () {
105+ var schema =
106+ """
107+ #integer*
108+ """ ;
109+ var json =
110+ """
111+ {
112+ "key1": "value1",
113+ "key2": false,
114+ "key3": "-50000"
115+ }
116+ """ ;
117+ JsonSchema .isValid (schema , json );
118+ var exception = assertThrows (JsonSchemaException .class ,
119+ () -> JsonAssert .isValid (schema , json ));
120+ assertEquals (DTYP06 , exception .getCode ());
121+ exception .printStackTrace ();
122+ }
123+
124+ @ Test
125+ public void When_NestedRangeWithJsonNotIntegerInObject_ExceptionThrown () {
126+ var schema =
127+ """
128+ @range*(-100, 200) #integer*
129+ """ ;
130+ var json =
131+ """
132+ {
133+ "key1": "value1",
134+ "key2": false,
135+ "key3": "-50000"
136+ }
137+ """ ;
138+ JsonSchema .isValid (schema , json );
139+ var exception = assertThrows (JsonSchemaException .class ,
140+ () -> JsonAssert .isValid (schema , json ));
141+ assertEquals (DTYP06 , exception .getCode ());
142+ exception .printStackTrace ();
143+ }
144+
145+ @ Test
146+ public void When_NestedRangeWithNonCompositeJsonInObject_ExceptionThrown () {
147+ var schema =
148+ """
149+ @range*(100, !)
150+ """ ;
151+ var json =
152+ """
153+ "value1"
154+ """ ;
155+ JsonSchema .isValid (schema , json );
156+ var exception = assertThrows (JsonSchemaException .class ,
157+ () -> JsonAssert .isValid (schema , json ));
158+ assertEquals (FUNC06 , exception .getCode ());
159+ exception .printStackTrace ();
160+ }
161+
162+ @ Test
163+ public void When_NestedRangeWithJsonWrongIntegerInObject_ExceptionThrown () {
164+ var schema =
165+ """
166+ @range*(-100, 100) #integer*
167+ """ ;
168+ var json =
169+ """
170+ {
171+ "key1": -100,
172+ "key2": 100,
173+ "key3": -500
174+ }
175+ """ ;
176+ JsonSchema .isValid (schema , json );
177+ var exception = assertThrows (JsonSchemaException .class ,
178+ () -> JsonAssert .isValid (schema , json ));
179+ assertEquals (RANG01 , exception .getCode ());
180+ exception .printStackTrace ();
181+ }
182+
183+ @ Test
184+ public void When_NestedRangeWithUndefinedAndWrongIntegerInArray_ExceptionThrown () {
185+ var schema =
186+ """
187+ @range*(!, 400) #integer*
188+ """ ;
189+ var json =
190+ """
191+ [100, 500, 900]
192+ """ ;
193+ JsonSchema .isValid (schema , json );
194+ var exception = assertThrows (JsonSchemaException .class ,
195+ () -> JsonAssert .isValid (schema , json ));
196+ assertEquals (RANG04 , exception .getCode ());
197+ exception .printStackTrace ();
198+ }
199+
200+ @ Test
201+ public void When_NestedPositiveWithWrongIntegerInArray_ExceptionThrown () {
202+ var schema =
203+ """
204+ @positive* #integer*
205+ """ ;
206+ var json =
207+ """
208+ [100, -500, 900]
209+ """ ;
210+ JsonSchema .isValid (schema , json );
211+ var exception = assertThrows (JsonSchemaException .class ,
212+ () -> JsonAssert .isValid (schema , json ));
213+ assertEquals (POSI01 , exception .getCode ());
214+ exception .printStackTrace ();
215+ }
216+
217+ @ Test
218+ public void When_NestedNegativeWithWrongIntegerInArray_ExceptionThrown () {
219+ var schema =
220+ """
221+ @negative* #integer*
222+ """ ;
223+ var json =
224+ """
225+ [-100, -500, 900]
226+ """ ;
227+ JsonSchema .isValid (schema , json );
228+ var exception = assertThrows (JsonSchemaException .class ,
229+ () -> JsonAssert .isValid (schema , json ));
230+ assertEquals (NEGI01 , exception .getCode ());
231+ exception .printStackTrace ();
232+ }
233+ }
0 commit comments