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 .JsonParserException ;
6+ import com .relogiclabs .json .schema .exception .JsonSchemaException ;
7+ import org .junit .jupiter .api .Test ;
8+
9+ import static com .relogiclabs .json .schema .message .ErrorCode .DTYP04 ;
10+ import static com .relogiclabs .json .schema .message .ErrorCode .DTYP06 ;
11+ import static com .relogiclabs .json .schema .message .ErrorCode .ELEM01 ;
12+ import static com .relogiclabs .json .schema .message .ErrorCode .ENUM02 ;
13+ import static com .relogiclabs .json .schema .message .ErrorCode .JPRS01 ;
14+ import static com .relogiclabs .json .schema .message .ErrorCode .NEMT02 ;
15+ import static org .junit .jupiter .api .Assertions .assertEquals ;
16+ import static org .junit .jupiter .api .Assertions .assertThrows ;
17+
18+ public class ArrayTests {
19+ @ Test
20+ public void When_JsonNotArray_ExceptionThrown () {
21+ var schema = "#array" ;
22+ var json = "10" ;
23+
24+ JsonSchema .isValid (schema , json );
25+ var exception = assertThrows (JsonSchemaException .class ,
26+ () -> JsonAssert .isValid (schema , json ));
27+ assertEquals (DTYP04 , exception .getCode ());
28+ exception .printStackTrace ();
29+ }
30+
31+ @ Test
32+ public void When_JsonNotArrayInObject_ExceptionThrown () {
33+ var schema =
34+ """
35+ {
36+ "key1": #array,
37+ "key2": #array,
38+ "key3": #array
39+ }
40+ """ ;
41+ var json =
42+ """
43+ {
44+ "key1": "value1",
45+ "key2": {"key": "value"},
46+ "key3": 100000
47+ }
48+ """ ;
49+ JsonSchema .isValid (schema , json );
50+ var exception = assertThrows (JsonSchemaException .class ,
51+ () -> JsonAssert .isValid (schema , json ));
52+ assertEquals (DTYP04 , exception .getCode ());
53+ exception .printStackTrace ();
54+ }
55+
56+ @ Test
57+ public void When_JsonNotArrayInArray_ExceptionThrown () {
58+ var schema =
59+ """
60+ [#array, #array, #array]
61+ """ ;
62+ var json =
63+ """
64+ [{}, "value1", 10.5]
65+ """ ;
66+ JsonSchema .isValid (schema , json );
67+ var exception = assertThrows (JsonSchemaException .class ,
68+ () -> JsonAssert .isValid (schema , json ));
69+ assertEquals (DTYP04 , exception .getCode ());
70+ exception .printStackTrace ();
71+ }
72+
73+ @ Test
74+ public void When_NestedJsonNotArrayInArray_ExceptionThrown () {
75+ var schema =
76+ """
77+ #array*
78+ """ ;
79+ var json =
80+ """
81+ [true, "value1", false]
82+ """ ;
83+ JsonSchema .isValid (schema , json );
84+ var exception = assertThrows (JsonSchemaException .class ,
85+ () -> JsonAssert .isValid (schema , json ));
86+ assertEquals (DTYP06 , exception .getCode ());
87+ exception .printStackTrace ();
88+ }
89+
90+ @ Test
91+ public void When_NestedJsonNotArrayInObject_ExceptionThrown () {
92+ var schema =
93+ """
94+ #array*
95+ """ ;
96+ var json =
97+ """
98+ {
99+ "key1": 10.11,
100+ "key2": true,
101+ "key3": "value1"
102+ }
103+ """ ;
104+ JsonSchema .isValid (schema , json );
105+ var exception = assertThrows (JsonSchemaException .class ,
106+ () -> JsonAssert .isValid (schema , json ));
107+ assertEquals (DTYP06 , exception .getCode ());
108+ exception .printStackTrace ();
109+ }
110+
111+ @ Test
112+ public void When_ElementsWithWrongArray_ExceptionThrown () {
113+ var schema = "@elements(10, 20, 30, 40) #array" ;
114+ var json = "[5, 10, 15, 20, 25]" ;
115+
116+ JsonSchema .isValid (schema , json );
117+ var exception = assertThrows (JsonSchemaException .class ,
118+ () -> JsonAssert .isValid (schema , json ));
119+ assertEquals (ELEM01 , exception .getCode ());
120+ exception .printStackTrace ();
121+ }
122+
123+ @ Test
124+ public void When_NestedElementsWithWrongArrayInArray_ExceptionThrown () {
125+ var schema = "@elements*(5, 10) #array" ;
126+ var json = "[[5, 10], [], [5, 10, 15, 20]]" ;
127+
128+ JsonSchema .isValid (schema , json );
129+ var exception = assertThrows (JsonSchemaException .class ,
130+ () -> JsonAssert .isValid (schema , json ));
131+ assertEquals (ELEM01 , exception .getCode ());
132+ exception .printStackTrace ();
133+ }
134+
135+ @ Test
136+ public void When_EnumWithWrongValueInArray_ExceptionThrown () {
137+ var schema =
138+ """
139+ [
140+ @enum(5, 10, 15),
141+ @enum(100, 150, 200),
142+ @enum("abc", "pqr", "xyz")
143+ ] #array
144+ """ ;
145+ var json =
146+ """
147+ [11, 102, "efg"]
148+ """ ;
149+ JsonSchema .isValid (schema , json );
150+ var exception = assertThrows (JsonSchemaException .class ,
151+ () -> JsonAssert .isValid (schema , json ));
152+ assertEquals (ENUM02 , exception .getCode ());
153+ exception .printStackTrace ();
154+ }
155+
156+ @ Test
157+ public void When_InvalidJsonInArray_ExceptionThrown () {
158+ var schema = "#array" ;
159+ var json = "[,,]" ;
160+
161+ //JsonSchema.IsValid(schema, json);
162+ var exception = assertThrows (JsonParserException .class ,
163+ () -> JsonAssert .isValid (schema , json ));
164+ assertEquals (JPRS01 , exception .getCode ());
165+ exception .printStackTrace ();
166+ }
167+
168+ @ Test
169+ public void When_EmptyArrayInObject_ExceptionThrown () {
170+ var schema =
171+ """
172+ {
173+ "key1": @nonempty #array,
174+ "key2": @nonempty
175+ }
176+ """ ;
177+ var json =
178+ """
179+ {
180+ "key1": [],
181+ "key2": []
182+ }
183+ """ ;
184+ JsonSchema .isValid (schema , json );
185+ var exception = assertThrows (JsonSchemaException .class ,
186+ () -> JsonAssert .isValid (schema , json ));
187+ assertEquals (NEMT02 , exception .getCode ());
188+ exception .printStackTrace ();
189+ }
190+ }
0 commit comments