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+ }
0 commit comments