Skip to content

Commit 07fcfc0

Browse files
committed
Add other positive and negative test cases
1 parent acbe7f0 commit 07fcfc0

File tree

6 files changed

+679
-0
lines changed

6 files changed

+679
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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.InvalidDataTypeException;
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.DTYP01;
10+
import static com.relogiclabs.json.schema.message.ErrorCode.DTYP04;
11+
import static com.relogiclabs.json.schema.message.ErrorCode.DUBL01;
12+
import static com.relogiclabs.json.schema.message.ErrorCode.FLOT01;
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertThrows;
15+
16+
public class OtherTests {
17+
@Test
18+
public void When_DataTypeNotValid_ExceptionThrown() {
19+
var schema = "#abcd";
20+
var json = "0";
21+
22+
//JsonSchema.IsValid(schema, json);
23+
var exception = assertThrows(InvalidDataTypeException.class,
24+
() -> JsonAssert.isValid(schema, json));
25+
assertEquals(DTYP01, exception.getCode());
26+
exception.printStackTrace();
27+
}
28+
29+
@Test
30+
public void When_JsonNotFloat_ExceptionThrown() {
31+
var schema = "#float";
32+
var json = "2.5E10";
33+
34+
JsonSchema.isValid(schema, json);
35+
var exception = assertThrows(JsonSchemaException.class,
36+
() -> JsonAssert.isValid(schema, json));
37+
assertEquals(DTYP04, exception.getCode());
38+
exception.printStackTrace();
39+
}
40+
41+
@Test
42+
public void When_JsonNotDouble_ExceptionThrown() {
43+
var schema = "#double";
44+
var json = "\"string\"";
45+
46+
JsonSchema.isValid(schema, json);
47+
var exception = assertThrows(JsonSchemaException.class,
48+
() -> JsonAssert.isValid(schema, json));
49+
assertEquals(DTYP04, exception.getCode());
50+
exception.printStackTrace();
51+
}
52+
53+
@Test
54+
public void When_JsonNotNull_ExceptionThrown() {
55+
var schema = "#null";
56+
var json = "0";
57+
58+
JsonSchema.isValid(schema, json);
59+
var exception = assertThrows(JsonSchemaException.class,
60+
() -> JsonAssert.isValid(schema, json));
61+
assertEquals(DTYP04, exception.getCode());
62+
exception.printStackTrace();
63+
}
64+
65+
@Test
66+
public void When_JsonValueNotEqualForFloat_ExceptionThrown() {
67+
var schema = "3.5 #float";
68+
var json = "2.5";
69+
70+
JsonSchema.isValid(schema, json);
71+
var exception = assertThrows(JsonSchemaException.class,
72+
() -> JsonAssert.isValid(schema, json));
73+
assertEquals(FLOT01, exception.getCode());
74+
exception.printStackTrace();
75+
}
76+
77+
@Test
78+
public void When_JsonValueNotEqualForDouble_ExceptionThrown() {
79+
var schema = "2.5E0 #double";
80+
var json = "2.5E1";
81+
82+
JsonSchema.isValid(schema, json);
83+
var exception = assertThrows(JsonSchemaException.class,
84+
() -> JsonAssert.isValid(schema, json));
85+
assertEquals(DUBL01, exception.getCode());
86+
exception.printStackTrace();
87+
}
88+
89+
@Test
90+
public void When_NonStaticValidMethodWithWrongJson_ExceptionThrown() {
91+
var schema =
92+
"""
93+
{
94+
"key1": #array,
95+
"key2": #array
96+
}
97+
""";
98+
var json1 =
99+
"""
100+
{
101+
"key1": [1, 10, 100],
102+
"key2": [100, 1000, [10, 10000]]
103+
}
104+
""";
105+
var json2 =
106+
"""
107+
{
108+
"key1": [1, 10, 100],
109+
"key2": "string"
110+
}
111+
""";
112+
var jsonAssert = new JsonAssert(schema);
113+
jsonAssert.isValid(json1);
114+
var exception = assertThrows(JsonSchemaException.class,
115+
() -> jsonAssert.isValid(json2));
116+
assertEquals(DTYP04, exception.getCode());
117+
exception.printStackTrace();
118+
}
119+
}

0 commit comments

Comments
 (0)