Skip to content

Commit ca3eca5

Browse files
committed
Add test cases for new features
1 parent 08e6f7d commit ca3eca5

19 files changed

+1834
-104
lines changed

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

Lines changed: 165 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
import org.junit.jupiter.api.Test;
77

88
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP04;
9+
import static com.relogiclabs.json.schema.positive.ExternalFunctions.ERRACCESS01;
910
import static org.junit.jupiter.api.Assertions.assertEquals;
1011
import static org.junit.jupiter.api.Assertions.assertThrows;
1112

1213
public class AggregatedTests {
13-
1414
@Test
1515
public void When_JsonSchemaAggregatedTestWithWrongData_ExceptionThrown() {
16-
var schema =
17-
"""
16+
var schema = """
1817
%title: "User Profile Response"
1918
%version: 1.0.0
2019
%schema:
@@ -41,8 +40,7 @@ public void When_JsonSchemaAggregatedTestWithWrongData_ExceptionThrown() {
4140
}
4241
}
4342
""";
44-
var json =
45-
"""
43+
var json = """
4644
{
4745
"user": {
4846
"id": "not number",
@@ -70,4 +68,166 @@ public void When_JsonSchemaAggregatedTestWithWrongData_ExceptionThrown() {
7068
assertEquals(DTYP04, exception.getCode());
7169
exception.printStackTrace();
7270
}
71+
72+
@Test
73+
public void When_ExtendedAggregatedTestWithInvalidAccess_ExceptionThrown() {
74+
var schema = """
75+
%title: "Extended User Profile Dashboard API Response"
76+
%version: 2.0.0
77+
%include: com.relogiclabs.json.schema.positive.ExternalFunctions
78+
%pragma IgnoreUndefinedProperties: true
79+
80+
%define $post: {
81+
"id": @range(1, 1000) #integer,
82+
"title": @length(10, 100) #string,
83+
"content": @length(30, 1000) #string,
84+
"tags": $tags
85+
} #object
86+
%define $product: {
87+
"id": @length(2, 10) @regex("[a-z][a-z0-9]+") #string,
88+
"name": @length(5, 30) #string,
89+
"brand": @length(5, 30) #string,
90+
"price": @range(0.1, 1000000),
91+
"inStock": #boolean,
92+
"specs": {
93+
"cpu": @length(5, 30) #string,
94+
"ram": @regex("[0-9]{1,2}GB") #string,
95+
"storage": @regex("[0-9]{1,4}GB (SSD|HDD)") #string
96+
} #object #null
97+
}
98+
%define $tags: @length(1, 10) #string*($tag) #array
99+
%define $tag: @length(3, 20) @regex("[A-Za-z_]+") #string
100+
%schema:
101+
{
102+
"user": {
103+
"id": @range(1, 10000) #integer,
104+
/*username does not allow special characters*/
105+
"username": @regex("[a-z_]{3,30}") #string,
106+
"role": @enum("user", "admin") #string &role,
107+
"isActive": #boolean, //user account current status
108+
"registeredAt": @time("DD-MM-YYYY hh:mm:ss") #string,
109+
"dataAccess": @checkAccess(&role) #integer,
110+
"profile": {
111+
"firstName": @regex("[A-Za-z]{3,50}") #string,
112+
"lastName": @regex("[A-Za-z]{3,50}") #string,
113+
"dateOfBirth": @date("DD-MM-YYYY") #string,
114+
"age": @range(18, 128) #integer,
115+
"email": @email #string,
116+
"pictureURL": @url #string,
117+
"address": {
118+
"street": @length(10, 200) #string,
119+
"city": @length(3, 50) #string,
120+
"country": @regex("[A-Za-z ]{3,50}") #string
121+
} #object #null,
122+
"hobbies": !?
123+
},
124+
"posts": @length(0, 1000) #object*($post) #array,
125+
"preferences": {
126+
"theme": @enum("light", "dark") #string,
127+
"fontSize": @range(9, 24) #integer,
128+
"autoSave": #boolean
129+
}
130+
},
131+
"products": #object*($product) #array,
132+
"weather": {
133+
"temperature": @range(-50, 60) #integer #float,
134+
"isCloudy": #boolean
135+
}
136+
}
137+
""";
138+
var json = """
139+
{
140+
"user": {
141+
"id": 1234,
142+
"username": "johndoe",
143+
"role": "user",
144+
"isActive": true,
145+
"registeredAt": "06-09-2023 15:10:30",
146+
"dataAccess": 6,
147+
"profile": {
148+
"firstName": "John",
149+
"lastName": "Doe",
150+
"dateOfBirth": "17-06-1993",
151+
"age": 30,
152+
"email": "[email protected]",
153+
"pictureURL": "https://example.com/picture.jpg",
154+
"address": {
155+
"street": "123 Some St",
156+
"city": "Some town",
157+
"country": "Some Country"
158+
}
159+
},
160+
"posts": [
161+
{
162+
"id": 1,
163+
"title": "Introduction to JSON",
164+
"content": "JSON (JavaScript Object Notation) is a lightweight data interchange format...",
165+
"tags": [
166+
"JSON",
167+
"tutorial",
168+
"data"
169+
]
170+
},
171+
{
172+
"id": 2,
173+
"title": "Working with JSON in C#",
174+
"content": "C# provides built-in support for working with JSON...",
175+
"tags": [
176+
"CSharp",
177+
"JSON",
178+
"tutorial"
179+
]
180+
},
181+
{
182+
"id": 3,
183+
"title": "Introduction to JSON Schema",
184+
"content": "A JSON schema defines the structure and data types of JSON objects...",
185+
"tags": [
186+
"Schema",
187+
"JSON",
188+
"tutorial"
189+
]
190+
}
191+
],
192+
"preferences": {
193+
"theme": "dark",
194+
"fontSize": 14,
195+
"autoSave": true
196+
}
197+
},
198+
"products": [
199+
{
200+
"id": "p1",
201+
"name": "Smartphone",
202+
"brand": "TechGiant",
203+
"price": 599.99,
204+
"inStock": true,
205+
"specs": null
206+
},
207+
{
208+
"id": "p2",
209+
"name": "Laptop",
210+
"brand": "SuperTech",
211+
"price": 1299.99,
212+
"inStock": false,
213+
"specs": {
214+
"cpu": "Intel i11",
215+
"ram": "11GB",
216+
"storage": "11GB SSD"
217+
}
218+
}
219+
],
220+
"weather": {
221+
"temperature": 25.5,
222+
"isCloudy": false,
223+
"conditions": null
224+
}
225+
}
226+
""";
227+
JsonSchema.isValid(schema, json);
228+
var exception = assertThrows(JsonSchemaException.class,
229+
() -> JsonAssert.isValid(schema, json));
230+
assertEquals(ERRACCESS01, exception.getCode());
231+
exception.printStackTrace();
232+
}
73233
}

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

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
import com.relogiclabs.json.schema.exception.JsonSchemaException;
77
import org.junit.jupiter.api.Test;
88

9+
import static com.relogiclabs.json.schema.message.ErrorCode.ALEN01;
10+
import static com.relogiclabs.json.schema.message.ErrorCode.ALEN02;
11+
import static com.relogiclabs.json.schema.message.ErrorCode.ALEN03;
12+
import static com.relogiclabs.json.schema.message.ErrorCode.ALEN04;
13+
import static com.relogiclabs.json.schema.message.ErrorCode.ALEN05;
914
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP04;
1015
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP06;
1116
import static com.relogiclabs.json.schema.message.ErrorCode.ELEM01;
@@ -158,7 +163,7 @@ public void When_InvalidJsonInArray_ExceptionThrown() {
158163
var schema = "#array";
159164
var json = "[,,]";
160165

161-
//JsonSchema.IsValid(schema, json);
166+
//JsonSchema.isValid(schema, json);
162167
var exception = assertThrows(JsonParserException.class,
163168
() -> JsonAssert.isValid(schema, json));
164169
assertEquals(JPRS01, exception.getCode());
@@ -187,4 +192,104 @@ public void When_EmptyArrayInObject_ExceptionThrown() {
187192
assertEquals(NEMT02, exception.getCode());
188193
exception.printStackTrace();
189194
}
195+
196+
@Test
197+
public void When_JsonWrongLengthInObject_ExceptionThrown() {
198+
var schema =
199+
"""
200+
@length*(2) #array* #object
201+
""";
202+
var json =
203+
"""
204+
{
205+
"key1": [10, 20],
206+
"key2": [10]
207+
}
208+
""";
209+
JsonSchema.isValid(schema, json);
210+
var exception = assertThrows(JsonSchemaException.class,
211+
() -> JsonAssert.isValid(schema, json));
212+
assertEquals(ALEN01, exception.getCode());
213+
exception.printStackTrace();
214+
}
215+
216+
@Test
217+
public void When_JsonWrongMinimumLengthInObject_ExceptionThrown() {
218+
var schema =
219+
"""
220+
@length*(2, 4) #array* #object
221+
""";
222+
var json =
223+
"""
224+
{
225+
"key1": [10, 20],
226+
"key2": [10]
227+
}
228+
""";
229+
JsonSchema.isValid(schema, json);
230+
var exception = assertThrows(JsonSchemaException.class,
231+
() -> JsonAssert.isValid(schema, json));
232+
assertEquals(ALEN02, exception.getCode());
233+
exception.printStackTrace();
234+
}
235+
236+
@Test
237+
public void When_JsonWrongMaximumLengthInObject_ExceptionThrown() {
238+
var schema =
239+
"""
240+
@length*(2, 4) #array* #object
241+
""";
242+
var json =
243+
"""
244+
{
245+
"key1": [10, 20],
246+
"key2": [10, 20, 30, 40, 50]
247+
}
248+
""";
249+
JsonSchema.isValid(schema, json);
250+
var exception = assertThrows(JsonSchemaException.class,
251+
() -> JsonAssert.isValid(schema, json));
252+
assertEquals(ALEN03, exception.getCode());
253+
exception.printStackTrace();
254+
}
255+
256+
@Test
257+
public void When_JsonWrongMinimumLengthWithUndefinedInObject_ExceptionThrown() {
258+
var schema =
259+
"""
260+
@length*(2, !) #array* #object
261+
""";
262+
var json =
263+
"""
264+
{
265+
"key1": [10, 20],
266+
"key2": [10]
267+
}
268+
""";
269+
JsonSchema.isValid(schema, json);
270+
var exception = assertThrows(JsonSchemaException.class,
271+
() -> JsonAssert.isValid(schema, json));
272+
assertEquals(ALEN04, exception.getCode());
273+
exception.printStackTrace();
274+
}
275+
276+
@Test
277+
public void When_JsonWrongMaximumLengthWithUndefinedInObject_ExceptionThrown() {
278+
var schema =
279+
"""
280+
@length*(!, 4) #array* #object
281+
""";
282+
var json =
283+
"""
284+
{
285+
"key1": [10, 20],
286+
"key2": [10, 20, 30, 40, 50]
287+
}
288+
""";
289+
JsonSchema.isValid(schema, json);
290+
var exception = assertThrows(JsonSchemaException.class,
291+
() -> JsonAssert.isValid(schema, json));
292+
assertEquals(ALEN05, exception.getCode());
293+
exception.printStackTrace();
294+
}
190295
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void When_ComponentNotDefined_ExceptionThrown() {
2727
"key2": { "key1": 20, "key2": "value22" }
2828
}
2929
""";
30-
//JsonSchema.IsValid(schema, json);
30+
//JsonSchema.isValid(schema, json);
3131
var exception = assertThrows(DefinitionNotFoundException.class,
3232
() -> JsonAssert.isValid(schema, json));
3333
assertEquals(DEFI02, exception.getCode());
@@ -52,7 +52,7 @@ public void When_ComponentWithDuplicateDefinition_ExceptionThrown() {
5252
"key2": { "key1": 20, "key2": "value22" }
5353
}
5454
""";
55-
//JsonSchema.IsValid(schema, json);
55+
//JsonSchema.isValid(schema, json);
5656
var exception = assertThrows(DuplicateDefinitionException.class,
5757
() -> JsonAssert.isValid(schema, json));
5858
assertEquals(DEFI01, exception.getCode());

0 commit comments

Comments
 (0)