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 .MAXI01 ;
9+ import static com .relogiclabs .json .schema .message .ErrorCode .MAXI03 ;
10+ import static com .relogiclabs .json .schema .message .ErrorCode .MINI01 ;
11+ import static com .relogiclabs .json .schema .message .ErrorCode .MINI03 ;
12+ import static org .junit .jupiter .api .Assertions .assertEquals ;
13+ import static org .junit .jupiter .api .Assertions .assertThrows ;
14+
15+ public class NumberTests {
16+ @ Test
17+ public void When_JsonLessThanMinimumFloat_ExceptionThrown () {
18+ var schema =
19+ """
20+ @minimum(10) #float
21+ """ ;
22+ var json =
23+ """
24+ 9.999999
25+ """ ;
26+ JsonSchema .isValid (schema , json );
27+ var exception = assertThrows (JsonSchemaException .class ,
28+ () -> JsonAssert .isValid (schema , json ));
29+ assertEquals (MINI01 , exception .getCode ());
30+ exception .printStackTrace ();
31+ }
32+
33+ @ Test
34+ public void When_JsonGreaterThanMaximumInteger_ExceptionThrown () {
35+ var schema =
36+ """
37+ @maximum(10) #integer
38+ """ ;
39+ var json =
40+ """
41+ 1000
42+ """ ;
43+ JsonSchema .isValid (schema , json );
44+ var exception = assertThrows (JsonSchemaException .class ,
45+ () -> JsonAssert .isValid (schema , json ));
46+ assertEquals (MAXI01 , exception .getCode ());
47+ exception .printStackTrace ();
48+ }
49+
50+ @ Test
51+ public void When_WrongJsonWithNestedMinimumIntegerInArray_ExceptionThrown () {
52+ var schema =
53+ """
54+ @minimum*(10.5) #integer*
55+ """ ;
56+ var json =
57+ """
58+ [
59+ 1111,
60+ 100,
61+ 10
62+ ]
63+ """ ;
64+ JsonSchema .isValid (schema , json );
65+ var exception = assertThrows (JsonSchemaException .class ,
66+ () -> JsonAssert .isValid (schema , json ));
67+ assertEquals (MINI01 , exception .getCode ());
68+ exception .printStackTrace ();
69+ }
70+
71+ @ Test
72+ public void When_WrongJsonWithNestedMinimumFloatInObject_ExceptionThrown () {
73+ var schema =
74+ """
75+ @minimum*(100) #number*
76+ """ ;
77+ var json =
78+ """
79+ {
80+ "key1": 100.000,
81+ "key2": 99.99,
82+ "key3": 200.884
83+ }
84+ """ ;
85+ JsonSchema .isValid (schema , json );
86+ var exception = assertThrows (JsonSchemaException .class ,
87+ () -> JsonAssert .isValid (schema , json ));
88+ assertEquals (MINI01 , exception .getCode ());
89+ exception .printStackTrace ();
90+ }
91+
92+ @ Test
93+ public void When_WrongJsonWithNestedMaximumNumberInArray_ExceptionThrown () {
94+ var schema =
95+ """
96+ @maximum*(1000.05) #number*
97+ """ ;
98+ var json =
99+ """
100+ [
101+ -1000.05,
102+ 1000.05,
103+ 1001
104+ ]
105+ """ ;
106+ JsonSchema .isValid (schema , json );
107+ var exception = assertThrows (JsonSchemaException .class ,
108+ () -> JsonAssert .isValid (schema , json ));
109+ assertEquals (MAXI01 , exception .getCode ());
110+ exception .printStackTrace ();
111+ }
112+
113+ @ Test
114+ public void When_NestedMaximumFloatInObject_ValidTrue () {
115+ var schema =
116+ """
117+ @maximum*(100) #float*
118+ """ ;
119+ var json =
120+ """
121+ {
122+ "key1": 100.000,
123+ "key2": -150.407,
124+ "key3": 100.00001
125+ }
126+ """ ;
127+ JsonSchema .isValid (schema , json );
128+ var exception = assertThrows (JsonSchemaException .class ,
129+ () -> JsonAssert .isValid (schema , json ));
130+ assertEquals (MAXI01 , exception .getCode ());
131+ exception .printStackTrace ();
132+ }
133+
134+ @ Test
135+ public void When_NestedMinimumExclusiveFloatInObject_ValidTrue () {
136+ var schema =
137+ """
138+ @minimum*(100, true) #float*
139+ """ ;
140+ var json =
141+ """
142+ {
143+ "key1": 500.999,
144+ "key2": 100.001,
145+ "key3": 100.000
146+ }
147+ """ ;
148+ JsonSchema .isValid (schema , json );
149+ var exception = assertThrows (JsonSchemaException .class ,
150+ () -> JsonAssert .isValid (schema , json ));
151+ assertEquals (MINI03 , exception .getCode ());
152+ exception .printStackTrace ();
153+ }
154+
155+ @ Test
156+ public void When_NestedMaximumExclusiveFloatInObject_ValidTrue () {
157+ var schema =
158+ """
159+ @maximum*(100, true) #float*
160+ """ ;
161+ var json =
162+ """
163+ {
164+ "key1": 99.999,
165+ "key2": 10.407,
166+ "key3": 100.000
167+ }
168+ """ ;
169+ JsonSchema .isValid (schema , json );
170+ var exception = assertThrows (JsonSchemaException .class ,
171+ () -> JsonAssert .isValid (schema , json ));
172+ assertEquals (MAXI03 , exception .getCode ());
173+ exception .printStackTrace ();
174+ }
175+ }
0 commit comments