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 .DuplicatePragmaException ;
6
+ import com .relogiclabs .json .schema .exception .InvalidPragmaValueException ;
7
+ import com .relogiclabs .json .schema .exception .JsonSchemaException ;
8
+ import com .relogiclabs .json .schema .exception .PragmaNotFoundException ;
9
+ import com .relogiclabs .json .schema .exception .SchemaParserException ;
10
+ import org .junit .jupiter .api .Test ;
11
+
12
+ import static com .relogiclabs .json .schema .message .ErrorCode .FLOT01 ;
13
+ import static com .relogiclabs .json .schema .message .ErrorCode .PRAG01 ;
14
+ import static com .relogiclabs .json .schema .message .ErrorCode .PRAG02 ;
15
+ import static com .relogiclabs .json .schema .message .ErrorCode .PRAG03 ;
16
+ import static com .relogiclabs .json .schema .message .ErrorCode .PROP06 ;
17
+ import static com .relogiclabs .json .schema .message .ErrorCode .PROP07 ;
18
+ import static com .relogiclabs .json .schema .message .ErrorCode .SPRS01 ;
19
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
20
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
21
+
22
+ public class PragmaTests {
23
+ @ Test
24
+ public void When_UndefinedPropertyOfObject_ExceptionThrown () {
25
+ var schema =
26
+ """
27
+ %pragma IgnoreUndefinedProperties: false
28
+ %schema:
29
+ {
30
+ "key1": #integer
31
+ }
32
+ """ ;
33
+ var json =
34
+ """
35
+ {
36
+ "key1": 10,
37
+ "key2": "value1"
38
+ }
39
+ """ ;
40
+ JsonSchema .isValid (schema , json );
41
+ var exception = assertThrows (JsonSchemaException .class ,
42
+ () -> JsonAssert .isValid (schema , json ));
43
+ assertEquals (PROP06 , exception .getCode ());
44
+ exception .printStackTrace ();
45
+ }
46
+
47
+ @ Test
48
+ public void When_InvalidUndefinedPropertyValueMissing_ExceptionThrown () {
49
+ var schema =
50
+ """
51
+ %pragma IgnoreUndefinedProperties:
52
+ %schema:
53
+ {
54
+ "key1": #integer
55
+ }
56
+ """ ;
57
+ var json =
58
+ """
59
+ {
60
+ "key1": 10,
61
+ "key2": "value1"
62
+ }
63
+ """ ;
64
+ //JsonSchema.IsValid(schema, json);
65
+ var exception = assertThrows (SchemaParserException .class ,
66
+ () -> JsonAssert .isValid (schema , json ));
67
+ assertEquals (SPRS01 , exception .getCode ());
68
+ exception .printStackTrace ();
69
+ }
70
+
71
+ @ Test
72
+ public void When_IgnoreUndefinedPropertiesMalformed_ExceptionThrown () {
73
+ var schema =
74
+ """
75
+ %pragma IgnoreUndefinedProperty: true
76
+ %schema:
77
+ {
78
+ "key1": #integer
79
+ }
80
+ """ ;
81
+ var json =
82
+ """
83
+ {
84
+ "key1": 10,
85
+ "key2": "value1"
86
+ }
87
+ """ ;
88
+ //JsonSchema.IsValid(schema, json);
89
+ var exception = assertThrows (PragmaNotFoundException .class ,
90
+ () -> JsonAssert .isValid (schema , json ));
91
+ assertEquals (PRAG01 , exception .getCode ());
92
+ exception .printStackTrace ();
93
+ }
94
+
95
+ @ Test
96
+ public void When_InvalidUndefinedPropertyValue_ExceptionThrown () {
97
+ var schema =
98
+ """
99
+ %pragma IgnoreUndefinedProperties: 1
100
+ %schema:
101
+ {
102
+ "key1": #integer
103
+ }
104
+ """ ;
105
+ var json =
106
+ """
107
+ {
108
+ "key1": 10,
109
+ "key2": "value1"
110
+ }
111
+ """ ;
112
+ //JsonSchema.IsValid(schema, json);
113
+ var exception = assertThrows (InvalidPragmaValueException .class ,
114
+ () -> JsonAssert .isValid (schema , json ));
115
+ assertEquals (PRAG02 , exception .getCode ());
116
+ exception .printStackTrace ();
117
+ }
118
+
119
+ @ Test
120
+ public void When_IgnorePropertyOrderOfObject_ExceptionThrown () {
121
+ var schema =
122
+ """
123
+ %pragma IgnoreObjectPropertyOrder: false
124
+ %schema:
125
+ {
126
+ "key1": #integer,
127
+ "key2": #string,
128
+ "key3": #float
129
+ }
130
+ """ ;
131
+ var json =
132
+ """
133
+ {
134
+ "key1": 10,
135
+ "key3": 2.1,
136
+ "key2": "value1"
137
+ }
138
+ """ ;
139
+ JsonSchema .isValid (schema , json );
140
+ var exception = assertThrows (JsonSchemaException .class ,
141
+ () -> JsonAssert .isValid (schema , json ));
142
+ assertEquals (PROP07 , exception .getCode ());
143
+ exception .printStackTrace ();
144
+ }
145
+
146
+ @ Test
147
+ public void When_FloatingPointToleranceOfNumber_ExceptionThrown () {
148
+ var schema =
149
+ """
150
+ %pragma FloatingPointTolerance: 0.00001
151
+ %schema:
152
+ {
153
+ "key1": 5.00 #float,
154
+ "key2": 10.00E+0 #double
155
+ }
156
+ """ ;
157
+ var json =
158
+ """
159
+ {
160
+ "key1": 5.00002,
161
+ "key2": 10.0002E+0
162
+ }
163
+ """ ;
164
+ JsonSchema .isValid (schema , json );
165
+ var exception = assertThrows (JsonSchemaException .class ,
166
+ () -> JsonAssert .isValid (schema , json ));
167
+ assertEquals (FLOT01 , exception .getCode ());
168
+ exception .printStackTrace ();
169
+ }
170
+
171
+ @ Test
172
+ public void When_DuplicatePragmaAssign_ExceptionThrown () {
173
+ var schema =
174
+ """
175
+ %pragma IgnoreUndefinedProperties: false
176
+ %pragma IgnoreUndefinedProperties: false
177
+
178
+ %schema:
179
+ {
180
+ "key1": #integer
181
+ }
182
+ """ ;
183
+ var json =
184
+ """
185
+ {
186
+ "key1": 10,
187
+ "key2": "value1"
188
+ }
189
+ """ ;
190
+ //JsonSchema.IsValid(schema, json);
191
+ var exception = assertThrows (DuplicatePragmaException .class ,
192
+ () -> JsonAssert .isValid (schema , json ));
193
+ assertEquals (PRAG03 , exception .getCode ());
194
+ exception .printStackTrace ();
195
+ }
196
+ }
0 commit comments