Skip to content

Commit d1fa3d5

Browse files
committed
Add test cases for the string data type
1 parent ab5f98c commit d1fa3d5

File tree

2 files changed

+610
-0
lines changed

2 files changed

+610
-0
lines changed
Lines changed: 364 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,364 @@
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.JsonLexerException;
6+
import com.relogiclabs.json.schema.exception.JsonSchemaException;
7+
import com.relogiclabs.json.schema.exception.SchemaLexerException;
8+
import org.junit.jupiter.api.Test;
9+
10+
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP04;
11+
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP06;
12+
import static com.relogiclabs.json.schema.message.ErrorCode.EMAL01;
13+
import static com.relogiclabs.json.schema.message.ErrorCode.ENUM01;
14+
import static com.relogiclabs.json.schema.message.ErrorCode.JLEX01;
15+
import static com.relogiclabs.json.schema.message.ErrorCode.NEMT01;
16+
import static com.relogiclabs.json.schema.message.ErrorCode.PHON01;
17+
import static com.relogiclabs.json.schema.message.ErrorCode.REGX01;
18+
import static com.relogiclabs.json.schema.message.ErrorCode.SLEN03;
19+
import static com.relogiclabs.json.schema.message.ErrorCode.SLEN04;
20+
import static com.relogiclabs.json.schema.message.ErrorCode.SLEN05;
21+
import static com.relogiclabs.json.schema.message.ErrorCode.SLEX01;
22+
import static com.relogiclabs.json.schema.message.ErrorCode.URLA01;
23+
import static com.relogiclabs.json.schema.message.ErrorCode.URLA04;
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertThrows;
26+
27+
public class StringTests {
28+
@Test
29+
public void When_JsonNotString_ExceptionThrown() {
30+
var schema = "#string";
31+
var json = "10";
32+
33+
JsonSchema.isValid(schema, json);
34+
var exception = assertThrows(JsonSchemaException.class,
35+
() -> JsonAssert.isValid(schema, json));
36+
assertEquals(DTYP04, exception.getCode());
37+
exception.printStackTrace();
38+
}
39+
40+
@Test
41+
public void When_InvalidUnicodeStringInSchema_ExceptionThrown() {
42+
var schema = "\"\\uX0485\\uY486\\r\\n\\t\" #string";
43+
var json = "\"\\u0485\\u0486\\r\\n\\t\"";
44+
45+
//JsonSchema.IsValid(schema, json);
46+
var exception = assertThrows(SchemaLexerException.class,
47+
() -> JsonAssert.isValid(schema, json));
48+
assertEquals(SLEX01, exception.getCode());
49+
exception.printStackTrace();
50+
}
51+
52+
@Test
53+
public void When_InvalidUnicodeStringInJson_ExceptionThrown() {
54+
var schema = "\"\\u0485\\u0486\\r\\n\\t\" #string";
55+
var json = "\"\\uX0485\\uY486\\r\\n\\t\"";
56+
57+
//JsonSchema.IsValid(schema, json);
58+
var exception = assertThrows(JsonLexerException.class,
59+
() -> JsonAssert.isValid(schema, json));
60+
assertEquals(JLEX01, exception.getCode());
61+
exception.printStackTrace();
62+
}
63+
64+
@Test
65+
public void When_JsonNotStringInObject_ExceptionThrown() {
66+
var schema =
67+
"""
68+
{
69+
"key1": #string,
70+
"key2": #string,
71+
"key3": #string
72+
}
73+
""";
74+
var json =
75+
"""
76+
{
77+
"key1": "",
78+
"key2": 10,
79+
"key3": null
80+
}
81+
""";
82+
JsonSchema.isValid(schema, json);
83+
var exception = assertThrows(JsonSchemaException.class,
84+
() -> JsonAssert.isValid(schema, json));
85+
assertEquals(DTYP04, exception.getCode());
86+
exception.printStackTrace();
87+
}
88+
89+
@Test
90+
public void When_JsonNotStringInArray_ExceptionThrown() {
91+
var schema =
92+
"""
93+
[#string, #string, #string]
94+
""";
95+
var json =
96+
"""
97+
["value1", null,
98+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit"]
99+
""";
100+
JsonSchema.isValid(schema, json);
101+
var exception = assertThrows(JsonSchemaException.class,
102+
() -> JsonAssert.isValid(schema, json));
103+
assertEquals(DTYP04, exception.getCode());
104+
exception.printStackTrace();
105+
}
106+
107+
@Test
108+
public void When_NestedJsonNotStringInArray_ExceptionThrown() {
109+
var schema =
110+
"""
111+
#string*
112+
""";
113+
var json =
114+
"""
115+
["value1", 10, "value3"]
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_NestedJsonNotStringInObject_ExceptionThrown() {
126+
var schema =
127+
"""
128+
#string*
129+
""";
130+
var json =
131+
"""
132+
{
133+
"key1": "value1",
134+
"key2": null,
135+
"key3": 100.5
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_NestedWrongLengthWithStringInObject_ExceptionThrown() {
147+
var schema =
148+
"""
149+
@length*(5, 15) #string*
150+
""";
151+
var json =
152+
"""
153+
{
154+
"key1": "value1",
155+
"key2": "Lorem ipsum dolor sit amet",
156+
"key3": "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
157+
}
158+
""";
159+
JsonSchema.isValid(schema, json);
160+
var exception = assertThrows(JsonSchemaException.class,
161+
() -> JsonAssert.isValid(schema, json));
162+
assertEquals(SLEN03, exception.getCode());
163+
exception.printStackTrace();
164+
}
165+
166+
@Test
167+
public void When_NestedWrongLengthWithUndefinedStringInObject_ExceptionThrown() {
168+
var schema =
169+
"""
170+
@length*(100, !) #string*
171+
""";
172+
var json =
173+
"""
174+
{
175+
"key1": "Lorem ipsum dolor sit amet",
176+
"key2": "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
177+
"key3": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"
178+
}
179+
""";
180+
JsonSchema.isValid(schema, json);
181+
var exception = assertThrows(JsonSchemaException.class,
182+
() -> JsonAssert.isValid(schema, json));
183+
assertEquals(SLEN04, exception.getCode());
184+
exception.printStackTrace();
185+
}
186+
187+
@Test
188+
public void When_NestedWrongLengthWithUndefinedStringInArray_ExceptionThrown() {
189+
var schema =
190+
"""
191+
@length*(!, 10) #string*
192+
""";
193+
var json =
194+
"""
195+
["Lorem ipsum dolor sit amet", "value11", "value111"]
196+
""";
197+
JsonSchema.isValid(schema, json);
198+
var exception = assertThrows(JsonSchemaException.class,
199+
() -> JsonAssert.isValid(schema, json));
200+
assertEquals(SLEN05, exception.getCode());
201+
exception.printStackTrace();
202+
}
203+
204+
@Test
205+
public void When_RegexWithWrongStringInObject_ExceptionThrown() {
206+
var schema =
207+
"""
208+
{ "key1": @regex("[A-Za-z0-9]+@gmail\\.com") }
209+
""".replace("\\", "\\\\");
210+
var json =
211+
"""
212+
{
213+
"key1": "new example@gmail.com"
214+
}
215+
""";
216+
JsonSchema.isValid(schema, json);
217+
var exception = assertThrows(JsonSchemaException.class,
218+
() -> JsonAssert.isValid(schema, json));
219+
assertEquals(REGX01, exception.getCode());
220+
exception.printStackTrace();
221+
}
222+
223+
@Test
224+
public void When_EnumWithWrongStringInObject_ExceptionThrown() {
225+
var schema =
226+
"""
227+
{
228+
"key1": #string,
229+
"key2": @enum("val1", "val2") #string
230+
}
231+
""";
232+
var json =
233+
"""
234+
{
235+
"key1": "",
236+
"key2": "val4"
237+
}
238+
""";
239+
JsonSchema.isValid(schema, json);
240+
var exception = assertThrows(JsonSchemaException.class,
241+
() -> JsonAssert.isValid(schema, json));
242+
assertEquals(ENUM01, exception.getCode());
243+
exception.printStackTrace();
244+
}
245+
246+
@Test
247+
public void When_EmailWithWrongStringInObject_ExceptionThrown() {
248+
var schema =
249+
"""
250+
{
251+
"key1": @email #string,
252+
"key2": @email #string
253+
}
254+
""";
255+
var json =
256+
"""
257+
{
258+
"key1": "email@test@example.com",
259+
"key2": "Email_Test@example.org"
260+
}
261+
""";
262+
JsonSchema.isValid(schema, json);
263+
var exception = assertThrows(JsonSchemaException.class,
264+
() -> JsonAssert.isValid(schema, json));
265+
assertEquals(EMAL01, exception.getCode());
266+
exception.printStackTrace();
267+
}
268+
269+
@Test
270+
public void When_UrlWithWrongStringAddressInObject_ExceptionThrown() {
271+
var schema =
272+
"""
273+
{
274+
"key1": @url #string,
275+
"key2": @url #string,
276+
"key3": @url("ftps") #string
277+
}
278+
""";
279+
var json =
280+
"""
281+
{
282+
"key1": "https:// www.example.com/",
283+
"key2": "https://www.<example>.com/test/",
284+
"key3": "ftps://subdomain.`example`.com/test#section?query=string"
285+
}
286+
""";
287+
JsonSchema.isValid(schema, json);
288+
var exception = assertThrows(JsonSchemaException.class,
289+
() -> JsonAssert.isValid(schema, json));
290+
assertEquals(URLA01, exception.getCode());
291+
exception.printStackTrace();
292+
}
293+
294+
@Test
295+
public void When_UrlWithSchemeAndWrongStringAddressInObject_ExceptionThrown() {
296+
var schema =
297+
"""
298+
{
299+
"key1": @url("ftps") #string,
300+
"key2": @url #string
301+
}
302+
""";
303+
var json =
304+
"""
305+
{
306+
"key1": "ssh://www.example.com/test/",
307+
"key2": "ftp://www.example.com/"
308+
}
309+
""";
310+
JsonSchema.isValid(schema, json);
311+
var exception = assertThrows(JsonSchemaException.class,
312+
() -> JsonAssert.isValid(schema, json));
313+
assertEquals(URLA04, exception.getCode());
314+
exception.printStackTrace();
315+
}
316+
317+
@Test
318+
public void When_PhoneWithWrongStringInObject_ExceptionThrown() {
319+
var schema =
320+
"""
321+
{
322+
"key1": @phone #string,
323+
"key2": @phone #string,
324+
"key3": @phone #string
325+
}
326+
""";
327+
var json =
328+
"""
329+
{
330+
"key1": "Phone 01737048177",
331+
"key2": "+880:1737048177",
332+
"key3": "0088/01737048177"
333+
}
334+
""";
335+
JsonSchema.isValid(schema, json);
336+
var exception = assertThrows(JsonSchemaException.class,
337+
() -> JsonAssert.isValid(schema, json));
338+
assertEquals(PHON01, exception.getCode());
339+
exception.printStackTrace();
340+
}
341+
342+
@Test
343+
public void When_EmptyStringInObject_ExceptionThrown() {
344+
var schema =
345+
"""
346+
{
347+
"key1": @nonempty #string,
348+
"key2": @nonempty #string #null
349+
}
350+
""";
351+
var json =
352+
"""
353+
{
354+
"key1": "",
355+
"key2": null
356+
}
357+
""";
358+
JsonSchema.isValid(schema, json);
359+
var exception = assertThrows(JsonSchemaException.class,
360+
() -> JsonAssert.isValid(schema, json));
361+
assertEquals(NEMT01, exception.getCode());
362+
exception.printStackTrace();
363+
}
364+
}

0 commit comments

Comments
 (0)