Skip to content

Commit 7be9177

Browse files
committed
Add and update test cases
1 parent 014d247 commit 7be9177

File tree

3 files changed

+168
-11
lines changed

3 files changed

+168
-11
lines changed

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

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
public class DataTypeTests {
1818
@Test
19-
public void When_JsonWithWrongMainDataType_ExceptionThrown() {
19+
public void When_WrongJsonWithDirectDataType_ExceptionThrown() {
2020
var schema =
2121
"""
2222
#string* #array
@@ -33,7 +33,7 @@ public void When_JsonWithWrongMainDataType_ExceptionThrown() {
3333
}
3434

3535
@Test
36-
public void When_JsonWithWrongNestedDataType_ExceptionThrown() {
36+
public void When_WrongJsonWithNestedDataType_ExceptionThrown() {
3737
var schema =
3838
"""
3939
#string* #array
@@ -137,4 +137,58 @@ public void When_NestedDataTypeArgumentWithValidationFailed_ExceptionThrown()
137137
assertEquals(DTYP04, exception.getCode());
138138
exception.printStackTrace();
139139
}
140+
141+
@Test
142+
public void When_MultipleNestedDataTypeWithWrongValueInObject_ExceptionThrown() {
143+
var schema =
144+
"""
145+
{
146+
"key1": #date* #time* #null* #array,
147+
"key2": #string* #null* #array,
148+
"key3": #integer* #float* #array
149+
}
150+
""";
151+
var json =
152+
"""
153+
{
154+
"key1": ["2021-08-01", "2021-08-01T15:50:30.300Z", "test"],
155+
"key2": ["test", null, "text", 10],
156+
"key3": [10, 100, 200.5, "text"]
157+
}
158+
""";
159+
JsonSchema.isValid(schema, json);
160+
var exception = assertThrows(JsonSchemaException.class,
161+
() -> JsonAssert.isValid(schema, json));
162+
assertEquals(DTYP06, exception.getCode());
163+
exception.printStackTrace();
164+
}
165+
166+
@Test
167+
public void When_MultipleDataTypeWithWrongValueInObject_ExceptionThrown() {
168+
var schema =
169+
"""
170+
{
171+
"key1": #date #time #null,
172+
"key2": #array #object #null,
173+
"key3": #integer #float,
174+
"key4": #string #null,
175+
"key5": #number #string
176+
}
177+
""";
178+
var json =
179+
"""
180+
{
181+
"key1": "2021-08-01",
182+
"key2": null,
183+
"key3": 100,
184+
"key4": "test",
185+
"key5": false
186+
}
187+
""";
188+
JsonSchema.isValid(schema, json);
189+
var exception = assertThrows(JsonSchemaException.class,
190+
() -> JsonAssert.isValid(schema, json));
191+
assertEquals(DTYP04, exception.getCode());
192+
exception.printStackTrace();
193+
}
140194
}

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

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,69 @@
44
import org.junit.jupiter.api.Test;
55

66
public class ComponentTests {
7+
@Test
8+
public void When_ComponentExampleInArray_ValidTrue() {
9+
var schema =
10+
"""
11+
%define $article: {
12+
"id": @range(1, 100) #integer,
13+
"title": @length(10, 100) #string,
14+
"preview": @length(30, 1000) #string,
15+
"tags": @length*(3, 30) @length(1, 5) #string* #array
16+
} #object
17+
%schema: @length(1, 10) #object*($article) #array
18+
""";
19+
var json =
20+
"""
21+
[
22+
{
23+
"id": 1,
24+
"title": "Getting Started",
25+
"preview": "This guide will show you through the essential steps to quickly...",
26+
"tags": ["JSON", "Json Schema", "Quick Start"]
27+
},
28+
{
29+
"id": 2,
30+
"title": "Validation Syntax",
31+
"preview": "A JSON document is a structured data format used for the exchange...",
32+
"tags": ["JSON", "Json Schema", "Validation Syntax"]
33+
},
34+
{
35+
"id": 3,
36+
"title": "Constraint Functions",
37+
"preview": "This document serves as a brief overview, providing key insights into...",
38+
"tags": ["JSON", "Json Schema", "Constraint Functions"]
39+
}
40+
]
41+
""";
42+
JsonAssert.isValid(schema, json);
43+
}
44+
45+
@Test
46+
public void When_ComponentAlternativeFormInArray_ValidTrue() {
47+
var schema1 =
48+
"""
49+
%define $cmp: @range*(1, 10) #integer*
50+
%schema: @length(5) #array($cmp)
51+
""";
52+
var schema2 =
53+
"""
54+
%define $cmp: @range(1, 10)
55+
%schema: @length(5) #integer*($cmp) #array
56+
""";
57+
var schema3 =
58+
"""
59+
@range*(1, 10) @length(5) #integer* #array
60+
""";
61+
var json =
62+
"""
63+
[1, 3, 5, 8, 10]
64+
""";
65+
JsonAssert.isValid(schema1, json);
66+
JsonAssert.isValid(schema2, json);
67+
JsonAssert.isValid(schema3, json);
68+
}
69+
770
@Test
871
public void When_ComponentObjectInObject_ValidTrue() {
972
var schema =
@@ -108,8 +171,8 @@ public void When_NestedComponentObjectWithObjectInArray_ValidTrue() {
108171
"""
109172
%define $cmp1:
110173
{
111-
"key1": @range(10, 30) #integer,
112-
"key2": @enum("val1", "val2", "val3") #string
174+
"key1": @range(10, 30) #integer,
175+
"key2": @enum("val1", "val2", "val3") #string
113176
}
114177
%schema: #object*($cmp1) #array
115178
""";

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

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.relogiclabs.json.schema.positive;
22

33
import com.relogiclabs.json.schema.JsonAssert;
4+
import com.relogiclabs.json.schema.JsonSchema;
45
import org.junit.jupiter.api.Test;
56

67
public class DataTypeTests {
@@ -12,26 +13,65 @@ public void When_DataTypeMultiple_ValidTrue() {
1213
}
1314

1415
@Test
15-
public void When_DataTypeMultipleInObject_ValidTrue() {
16+
public void When_MultipleNestedDataTypeInObject_ValidTrue() {
1617
var schema =
1718
"""
1819
{
19-
"key1": #string #null,
20-
"key2": #string #boolean,
21-
"key3": #string #number
20+
"key1": #date* #time* #array,
21+
"key2": #string* #integer* #null* #array,
22+
"key3": #integer* #float* #array
2223
}
2324
""";
2425
var json =
2526
"""
2627
{
27-
"key1": null,
28-
"key2": false,
29-
"key3": 500000
28+
"key1": ["2021-08-01", "2021-08-01T15:50:30.300Z"],
29+
"key2": ["test", null, 10],
30+
"key3": [10, 100, 200.5]
3031
}
3132
""";
33+
JsonSchema.isValid(schema, json);
3234
JsonAssert.isValid(schema, json);
3335
}
3436

37+
@Test
38+
public void When_MultipleDirectDataTypeInObject_ValidTrue() {
39+
var schema =
40+
"""
41+
{
42+
"key1": #date #time,
43+
"key2": #array #null,
44+
"key3": #integer #float,
45+
"key4": #string #null,
46+
"key5": #number #string
47+
}
48+
""";
49+
var json1 =
50+
"""
51+
{
52+
"key1": "2021-08-01",
53+
"key2": [10, 20, 30],
54+
"key3": 100,
55+
"key4": "test",
56+
"key5": 10.10
57+
}
58+
""";
59+
var json2 =
60+
"""
61+
{
62+
"key1": "2021-08-01T15:50:30.300Z",
63+
"key2": null,
64+
"key3": 200.5,
65+
"key4": null,
66+
"key5": "10.10"
67+
}
68+
""";
69+
JsonSchema.isValid(schema, json1);
70+
JsonSchema.isValid(schema, json2);
71+
JsonAssert.isValid(schema, json1);
72+
JsonAssert.isValid(schema, json2);
73+
}
74+
3575
@Test
3676
public void When_DataTypeMultipleInArray_ValidTrue() {
3777
var schema =

0 commit comments

Comments
 (0)