Skip to content

Commit 596cf9e

Browse files
committed
Add new test cases for data types
1 parent a86d031 commit 596cf9e

File tree

3 files changed

+149
-14
lines changed

3 files changed

+149
-14
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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.DefinitionNotFoundException;
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.*;
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertThrows;
12+
13+
public class DataTypeTests {
14+
@Test
15+
public void When_JsonWithWrongMainDataType_ExceptionThrown() {
16+
var schema =
17+
"""
18+
#string* #array
19+
""";
20+
var json =
21+
"""
22+
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_JsonWithWrongNestedDataType_ExceptionThrown() {
33+
var schema =
34+
"""
35+
#string* #array
36+
""";
37+
var json =
38+
"""
39+
[10, 20]
40+
""";
41+
JsonSchema.isValid(schema, json);
42+
var exception = assertThrows(JsonSchemaException.class,
43+
() -> JsonAssert.isValid(schema, json));
44+
assertEquals(DTYP06, exception.getCode());
45+
exception.printStackTrace();
46+
}
47+
48+
@Test
49+
public void When_NestedTypeWithNonCompositeJson_ExceptionThrown() {
50+
var schema =
51+
"""
52+
#string*
53+
""";
54+
var json =
55+
"""
56+
10
57+
""";
58+
JsonSchema.isValid(schema, json);
59+
var exception = assertThrows(JsonSchemaException.class,
60+
() -> JsonAssert.isValid(schema, json));
61+
assertEquals(DTYP03, exception.getCode());
62+
exception.printStackTrace();
63+
}
64+
65+
@Test
66+
public void When_UndefinedDataTypeArgument_ExceptionThrown() {
67+
var schema =
68+
"""
69+
#array($undefined)
70+
""";
71+
var json =
72+
"""
73+
[10, 20]
74+
""";
75+
JsonSchema.isValid(schema, json);
76+
var exception = assertThrows(DefinitionNotFoundException.class,
77+
() -> JsonAssert.isValid(schema, json));
78+
assertEquals(DEFI03, exception.getCode());
79+
exception.printStackTrace();
80+
}
81+
82+
@Test
83+
public void When_UndefinedNestedDataTypeArgument_ExceptionThrown() {
84+
var schema =
85+
"""
86+
#integer*($undefined) #array
87+
""";
88+
var json =
89+
"""
90+
[10, 20]
91+
""";
92+
JsonSchema.isValid(schema, json);
93+
var exception = assertThrows(DefinitionNotFoundException.class,
94+
() -> JsonAssert.isValid(schema, json));
95+
assertEquals(DEFI04, exception.getCode());
96+
exception.printStackTrace();
97+
}
98+
99+
@Test
100+
public void When_DataTypeArgumentWithValidationFailed_ExceptionThrown()
101+
{
102+
var schema =
103+
"""
104+
%define $test: {"k1": #string}
105+
%schema: #object($test)
106+
""";
107+
var json =
108+
"""
109+
{"k1": 10}
110+
""";
111+
JsonSchema.isValid(schema, json);
112+
var exception = assertThrows(JsonSchemaException.class,
113+
() -> JsonAssert.isValid(schema, json));
114+
assertEquals(DTYP04, exception.getCode());
115+
exception.printStackTrace();
116+
}
117+
118+
@Test
119+
public void When_NestedDataTypeArgumentWithValidationFailed_ExceptionThrown()
120+
{
121+
var schema =
122+
"""
123+
%define $test: {"k1": #string}
124+
%schema: #object*($test) #array
125+
""";
126+
var json =
127+
"""
128+
[{"k1": 10}]
129+
""";
130+
JsonSchema.isValid(schema, json);
131+
var exception = assertThrows(JsonSchemaException.class,
132+
() -> JsonAssert.isValid(schema, json));
133+
assertEquals(DTYP04, exception.getCode());
134+
exception.printStackTrace();
135+
}
136+
}

src/test/java/com/relogiclabs/json/schema/negative/PragmaTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import static com.relogiclabs.json.schema.message.ErrorCode.PRAG01;
1414
import static com.relogiclabs.json.schema.message.ErrorCode.PRAG02;
1515
import static com.relogiclabs.json.schema.message.ErrorCode.PRAG03;
16+
import static com.relogiclabs.json.schema.message.ErrorCode.PROP05;
1617
import static com.relogiclabs.json.schema.message.ErrorCode.PROP06;
17-
import static com.relogiclabs.json.schema.message.ErrorCode.PROP07;
1818
import static com.relogiclabs.json.schema.message.ErrorCode.SPRS01;
1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -40,7 +40,7 @@ public void When_UndefinedPropertyOfObject_ExceptionThrown() {
4040
JsonSchema.isValid(schema, json);
4141
var exception = assertThrows(JsonSchemaException.class,
4242
() -> JsonAssert.isValid(schema, json));
43-
assertEquals(PROP06, exception.getCode());
43+
assertEquals(PROP05, exception.getCode());
4444
exception.printStackTrace();
4545
}
4646

@@ -139,7 +139,7 @@ public void When_IgnorePropertyOrderOfObject_ExceptionThrown() {
139139
JsonSchema.isValid(schema, json);
140140
var exception = assertThrows(JsonSchemaException.class,
141141
() -> JsonAssert.isValid(schema, json));
142-
assertEquals(PROP07, exception.getCode());
142+
assertEquals(PROP06, exception.getCode());
143143
exception.printStackTrace();
144144
}
145145

src/test/java/com/relogiclabs/json/schema/positive/AggregatedTests.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,17 @@ public void When_ExtendedJsonSchemaAggregatedTest_ValidTrue() {
178178
"storage": @regex("[0-9]{1,4}GB (SSD|HDD)") #string
179179
} #object #null
180180
}
181-
%define $tags: @length(1, 10) #array($tag)
181+
%define $tags: @length(1, 10) #string*($tag) #array
182182
%define $tag: @length(3, 20) @regex("[A-Za-z_]+") #string
183-
%schema:
183+
%schema:
184184
{
185185
"user": {
186186
"id": @range(1, 10000) #integer,
187187
/*username does not allow special characters*/
188188
"username": @regex("[a-z_]{3,30}") #string,
189-
/*currently only one role is allowed by system*/
190-
"role": "user" #string,
189+
"role": @enum("user", "admin") #string,
191190
"isActive": #boolean, //user account current status
192-
"registeredAt": #time,
191+
"registeredAt": @time("DD-MM-YYYY hh:mm:ss") #string,
193192
"profile": {
194193
"firstName": @regex("[A-Za-z]{3,50}") #string,
195194
"lastName": @regex("[A-Za-z]{3,50}") #string,
@@ -223,9 +222,9 @@ public void When_ExtendedJsonSchemaAggregatedTest_ValidTrue() {
223222
"user": {
224223
"id": 1234,
225224
"username": "johndoe",
226-
"role": "user",
225+
"role": "admin",
227226
"isActive": true,
228-
"registeredAt": "2023-09-06T15:10:30.639Z",
227+
"registeredAt": "06-09-2023 15:10:30",
229228
"profile": {
230229
"firstName": "John",
231230
"lastName": "Doe",
@@ -290,12 +289,12 @@ public void When_ExtendedJsonSchemaAggregatedTest_ValidTrue() {
290289
"id": "p2",
291290
"name": "Laptop",
292291
"brand": "SuperTech",
293-
"price": 159999.99,
292+
"price": 1299.99,
294293
"inStock": false,
295294
"specs": {
296-
"cpu": "Ryzen 01",
297-
"ram": "8GB",
298-
"storage": "256GB SSD"
295+
"cpu": "Ryzen 11",
296+
"ram": "11GB",
297+
"storage": "11GB SSD"
299298
}
300299
}
301300
],

0 commit comments

Comments
 (0)