Skip to content

Commit b588cc8

Browse files
committed
Add new test cases for updates
1 parent d833653 commit b588cc8

File tree

6 files changed

+413
-4
lines changed

6 files changed

+413
-4
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.JsonSchemaException;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP04;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertThrows;
11+
12+
public class AggregatedTests {
13+
14+
@Test
15+
public void When_JsonSchemaAggregatedTestWithWrongData_ExceptionThrown() {
16+
var schema =
17+
"""
18+
%title: "User Profile Response"
19+
%version: 1.0.0
20+
%schema:
21+
{
22+
"user": {
23+
"id": @range(1, 10000) #integer,
24+
/*username does not allow special characters*/
25+
"username": @regex("[a-z_]{3,30}") #string,
26+
/*currently only one role is allowed by system*/
27+
"role": "user" #string,
28+
"isActive": #boolean, //user account current status
29+
"profile": {
30+
"firstName": @regex("[A-Za-z ]{3,50}") #string,
31+
"lastName": @regex("[A-Za-z ]{3,50}") #string,
32+
"age": @range(18, 130) #integer,
33+
"email": @email #string,
34+
"pictureURL": @url #string,
35+
"address": {
36+
"street": @length(10, 200) #string,
37+
"city": @length(3, 50) #string,
38+
"country": @regex("[A-Za-z ]{3,50}") #string
39+
} #object #null
40+
}
41+
}
42+
}
43+
""";
44+
var json =
45+
"""
46+
{
47+
"user": {
48+
"id": "not number",
49+
"username": "john doe",
50+
"role": "user",
51+
"isActive": true,
52+
"profile": {
53+
"firstName": "John",
54+
"lastName": "Doe",
55+
"age": 30,
56+
"email": "[email protected]",
57+
"pictureURL": "https://example.com/picture.jpg",
58+
"address": {
59+
"street": "123 Some St",
60+
"city": "Some town",
61+
"country": "Some Country"
62+
}
63+
}
64+
}
65+
}
66+
""";
67+
JsonSchema.isValid(schema, json);
68+
var exception = assertThrows(JsonSchemaException.class,
69+
() -> JsonAssert.isValid(schema, json));
70+
assertEquals(DTYP04, exception.getCode());
71+
exception.printStackTrace();
72+
}
73+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.relogiclabs.json.schema.JsonAssert;
44
import com.relogiclabs.json.schema.exception.ClassInstantiationException;
55
import com.relogiclabs.json.schema.exception.DuplicateIncludeException;
6+
import com.relogiclabs.json.schema.exception.FunctionMismatchException;
67
import com.relogiclabs.json.schema.exception.FunctionNotFoundException;
78
import com.relogiclabs.json.schema.exception.InvalidFunctionException;
89
import com.relogiclabs.json.schema.exception.InvalidIncludeException;
@@ -15,12 +16,31 @@
1516
import static com.relogiclabs.json.schema.message.ErrorCode.CLAS04;
1617
import static com.relogiclabs.json.schema.message.ErrorCode.FUNC01;
1718
import static com.relogiclabs.json.schema.message.ErrorCode.FUNC02;
19+
import static com.relogiclabs.json.schema.message.ErrorCode.FUNC03;
1820
import static com.relogiclabs.json.schema.message.ErrorCode.FUNC05;
1921
import static org.junit.jupiter.api.Assertions.assertEquals;
2022
import static org.junit.jupiter.api.Assertions.assertThrows;
2123

2224
public class FunctionTests {
2325

26+
@Test
27+
public void When_FunctionAppliedOnWrongType_ExceptionThrown() {
28+
var schema =
29+
"""
30+
%schema: @range(10, 20)
31+
""";
32+
var json =
33+
"""
34+
"test"
35+
""";
36+
37+
//JsonSchema.IsValid(schema, json);
38+
var exception = assertThrows(FunctionMismatchException.class,
39+
() -> JsonAssert.isValid(schema, json));
40+
assertEquals(FUNC03, exception.getCode());
41+
exception.printStackTrace();
42+
}
43+
2444
@Test
2545
public void When_ExternalIncludeNotInheritBaseClass_ExceptionThrown() {
2646
var schema =
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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.JsonSchemaException;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static com.relogiclabs.json.schema.message.ErrorCode.MAXI01;
9+
import static com.relogiclabs.json.schema.message.ErrorCode.MAXI03;
10+
import static com.relogiclabs.json.schema.message.ErrorCode.MINI01;
11+
import static com.relogiclabs.json.schema.message.ErrorCode.MINI03;
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.assertThrows;
14+
15+
public class NumberTests {
16+
@Test
17+
public void When_JsonLessThanMinimumFloat_ExceptionThrown() {
18+
var schema =
19+
"""
20+
@minimum(10) #float
21+
""";
22+
var json =
23+
"""
24+
9.999999
25+
""";
26+
JsonSchema.isValid(schema, json);
27+
var exception = assertThrows(JsonSchemaException.class,
28+
() -> JsonAssert.isValid(schema, json));
29+
assertEquals(MINI01, exception.getCode());
30+
exception.printStackTrace();
31+
}
32+
33+
@Test
34+
public void When_JsonGreaterThanMaximumInteger_ExceptionThrown() {
35+
var schema =
36+
"""
37+
@maximum(10) #integer
38+
""";
39+
var json =
40+
"""
41+
1000
42+
""";
43+
JsonSchema.isValid(schema, json);
44+
var exception = assertThrows(JsonSchemaException.class,
45+
() -> JsonAssert.isValid(schema, json));
46+
assertEquals(MAXI01, exception.getCode());
47+
exception.printStackTrace();
48+
}
49+
50+
@Test
51+
public void When_WrongJsonWithNestedMinimumIntegerInArray_ExceptionThrown() {
52+
var schema =
53+
"""
54+
@minimum*(10.5) #integer*
55+
""";
56+
var json =
57+
"""
58+
[
59+
1111,
60+
100,
61+
10
62+
]
63+
""";
64+
JsonSchema.isValid(schema, json);
65+
var exception = assertThrows(JsonSchemaException.class,
66+
() -> JsonAssert.isValid(schema, json));
67+
assertEquals(MINI01, exception.getCode());
68+
exception.printStackTrace();
69+
}
70+
71+
@Test
72+
public void When_WrongJsonWithNestedMinimumFloatInObject_ExceptionThrown() {
73+
var schema =
74+
"""
75+
@minimum*(100) #number*
76+
""";
77+
var json =
78+
"""
79+
{
80+
"key1": 100.000,
81+
"key2": 99.99,
82+
"key3": 200.884
83+
}
84+
""";
85+
JsonSchema.isValid(schema, json);
86+
var exception = assertThrows(JsonSchemaException.class,
87+
() -> JsonAssert.isValid(schema, json));
88+
assertEquals(MINI01, exception.getCode());
89+
exception.printStackTrace();
90+
}
91+
92+
@Test
93+
public void When_WrongJsonWithNestedMaximumNumberInArray_ExceptionThrown() {
94+
var schema =
95+
"""
96+
@maximum*(1000.05) #number*
97+
""";
98+
var json =
99+
"""
100+
[
101+
-1000.05,
102+
1000.05,
103+
1001
104+
]
105+
""";
106+
JsonSchema.isValid(schema, json);
107+
var exception = assertThrows(JsonSchemaException.class,
108+
() -> JsonAssert.isValid(schema, json));
109+
assertEquals(MAXI01, exception.getCode());
110+
exception.printStackTrace();
111+
}
112+
113+
@Test
114+
public void When_NestedMaximumFloatInObject_ValidTrue() {
115+
var schema =
116+
"""
117+
@maximum*(100) #float*
118+
""";
119+
var json =
120+
"""
121+
{
122+
"key1": 100.000,
123+
"key2": -150.407,
124+
"key3": 100.00001
125+
}
126+
""";
127+
JsonSchema.isValid(schema, json);
128+
var exception = assertThrows(JsonSchemaException.class,
129+
() -> JsonAssert.isValid(schema, json));
130+
assertEquals(MAXI01, exception.getCode());
131+
exception.printStackTrace();
132+
}
133+
134+
@Test
135+
public void When_NestedMinimumExclusiveFloatInObject_ValidTrue() {
136+
var schema =
137+
"""
138+
@minimum*(100, true) #float*
139+
""";
140+
var json =
141+
"""
142+
{
143+
"key1": 500.999,
144+
"key2": 100.001,
145+
"key3": 100.000
146+
}
147+
""";
148+
JsonSchema.isValid(schema, json);
149+
var exception = assertThrows(JsonSchemaException.class,
150+
() -> JsonAssert.isValid(schema, json));
151+
assertEquals(MINI03, exception.getCode());
152+
exception.printStackTrace();
153+
}
154+
155+
@Test
156+
public void When_NestedMaximumExclusiveFloatInObject_ValidTrue() {
157+
var schema =
158+
"""
159+
@maximum*(100, true) #float*
160+
""";
161+
var json =
162+
"""
163+
{
164+
"key1": 99.999,
165+
"key2": 10.407,
166+
"key3": 100.000
167+
}
168+
""";
169+
JsonSchema.isValid(schema, json);
170+
var exception = assertThrows(JsonSchemaException.class,
171+
() -> JsonAssert.isValid(schema, json));
172+
assertEquals(MAXI03, exception.getCode());
173+
exception.printStackTrace();
174+
}
175+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class AggregatedTests {
88
public void When_SchemaAggregatedTest_ValidTrue() {
99
var schema = """
1010
%title: "Example Schema For Some Json HTTP Request or Response"
11-
%version: 1.0.1
11+
%version: 2023.09.11
1212
%include: com.relogiclabs.json.schema.positive.ExternalFunctions
1313
%pragma IgnoreUndefinedProperties: true
1414

0 commit comments

Comments
 (0)