1
1
package org .everit .json .schema ;
2
2
3
+ import static org .everit .json .schema .JSONMatcher .sameJsonAs ;
4
+ import static org .junit .Assert .assertThat ;
5
+
6
+ import org .json .JSONObject ;
3
7
import org .junit .Test ;
4
8
5
9
public class ConditionalSchemaTest {
6
10
7
11
private static final StringSchema MAX_LENGTH_STRING_SCHEMA = StringSchema .builder ().maxLength (4 ).build ();
12
+
8
13
private static final StringSchema MIN_LENGTH_STRING_SCHEMA = StringSchema .builder ().minLength (6 ).build ();
14
+
9
15
private static final StringSchema PATTERN_STRING_SCHEMA = StringSchema .builder ().pattern ("f.*o" ).build ();
10
16
17
+ private static final ResourceLoader LOADER = new ResourceLoader ("/org/everit/jsonvalidator/tostring/" );
18
+
19
+ private static ConditionalSchema .Builder initCompleteSchema () {
20
+ return ConditionalSchema .builder ()
21
+ .ifSchema (TrueSchema .builder ().build ())
22
+ .thenSchema (ObjectSchema .builder ()
23
+ .requiresObject (true )
24
+ .addRequiredProperty ("prop" ).build ())
25
+ .elseSchema (EmptySchema .builder ().build ());
26
+ }
27
+
11
28
// only if
12
29
13
30
@ Test
@@ -48,7 +65,8 @@ public void onlyElseSuccess() {
48
65
49
66
@ Test
50
67
public void ifSubschemaSuccessThenSubschemaFailure () {
51
- ConditionalSchema .Builder subject = ConditionalSchema .builder ().ifSchema (MAX_LENGTH_STRING_SCHEMA ).thenSchema (PATTERN_STRING_SCHEMA );
68
+ ConditionalSchema .Builder subject = ConditionalSchema .builder ().ifSchema (MAX_LENGTH_STRING_SCHEMA )
69
+ .thenSchema (PATTERN_STRING_SCHEMA );
52
70
TestSupport .failureOf (subject )
53
71
.expectedKeyword ("then" )
54
72
.expectedPointer ("#" )
@@ -80,7 +98,8 @@ public void ifSubschemaSuccessElseSubschemaFailure() {
80
98
81
99
@ Test
82
100
public void ifSubschemaFailureElseSubschemaFailure () {
83
- ConditionalSchema .Builder subject = ConditionalSchema .builder ().ifSchema (PATTERN_STRING_SCHEMA ).elseSchema (MAX_LENGTH_STRING_SCHEMA );
101
+ ConditionalSchema .Builder subject = ConditionalSchema .builder ().ifSchema (PATTERN_STRING_SCHEMA )
102
+ .elseSchema (MAX_LENGTH_STRING_SCHEMA );
84
103
TestSupport .failureOf (subject )
85
104
.expectedKeyword ("else" )
86
105
.expectedPointer ("#" )
@@ -124,17 +143,20 @@ public void thenSubschemaFailureElseSubschemaSuccess() {
124
143
125
144
@ Test
126
145
public void ifSubschemaSuccessThenSubschemaSuccessElseSubSchemaSuccess () {
127
- ConditionalSchema .builder ().ifSchema (PATTERN_STRING_SCHEMA ).thenSchema (MAX_LENGTH_STRING_SCHEMA ).elseSchema (MAX_LENGTH_STRING_SCHEMA ).build ().validate ("foo" );
146
+ ConditionalSchema .builder ().ifSchema (PATTERN_STRING_SCHEMA ).thenSchema (MAX_LENGTH_STRING_SCHEMA )
147
+ .elseSchema (MAX_LENGTH_STRING_SCHEMA ).build ().validate ("foo" );
128
148
}
129
149
130
150
@ Test
131
151
public void ifSubschemaSuccessThenSubschemaSuccessElseSubSchemaFailure () {
132
- ConditionalSchema .builder ().ifSchema (PATTERN_STRING_SCHEMA ).thenSchema (MAX_LENGTH_STRING_SCHEMA ).elseSchema (MIN_LENGTH_STRING_SCHEMA ).build ().validate ("foo" );
152
+ ConditionalSchema .builder ().ifSchema (PATTERN_STRING_SCHEMA ).thenSchema (MAX_LENGTH_STRING_SCHEMA )
153
+ .elseSchema (MIN_LENGTH_STRING_SCHEMA ).build ().validate ("foo" );
133
154
}
134
155
135
156
@ Test
136
157
public void ifSubschemaSuccessThenSubschemaFailureElseSubSchemaSuccess () {
137
- ConditionalSchema .Builder subject = ConditionalSchema .builder ().ifSchema (PATTERN_STRING_SCHEMA ).thenSchema (MAX_LENGTH_STRING_SCHEMA ).elseSchema (MIN_LENGTH_STRING_SCHEMA );
158
+ ConditionalSchema .Builder subject = ConditionalSchema .builder ().ifSchema (PATTERN_STRING_SCHEMA ).thenSchema (MAX_LENGTH_STRING_SCHEMA )
159
+ .elseSchema (MIN_LENGTH_STRING_SCHEMA );
138
160
TestSupport .failureOf (subject )
139
161
.expectedKeyword ("then" )
140
162
.expectedPointer ("#" )
@@ -144,7 +166,8 @@ public void ifSubschemaSuccessThenSubschemaFailureElseSubSchemaSuccess() {
144
166
145
167
@ Test
146
168
public void ifSubschemaSuccessThenSubschemaFailureElseSubSchemaFailure () {
147
- ConditionalSchema .Builder subject = ConditionalSchema .builder ().ifSchema (PATTERN_STRING_SCHEMA ).thenSchema (MAX_LENGTH_STRING_SCHEMA ).elseSchema (MIN_LENGTH_STRING_SCHEMA );
169
+ ConditionalSchema .Builder subject = ConditionalSchema .builder ().ifSchema (PATTERN_STRING_SCHEMA ).thenSchema (MAX_LENGTH_STRING_SCHEMA )
170
+ .elseSchema (MIN_LENGTH_STRING_SCHEMA );
148
171
TestSupport .failureOf (subject )
149
172
.expectedKeyword ("then" )
150
173
.expectedPointer ("#" )
@@ -154,12 +177,14 @@ public void ifSubschemaSuccessThenSubschemaFailureElseSubSchemaFailure() {
154
177
155
178
@ Test
156
179
public void ifSubschemaFailureThenSubschemaSuccessElseSubSchemaSuccess () {
157
- ConditionalSchema .builder ().ifSchema (MAX_LENGTH_STRING_SCHEMA ).thenSchema (PATTERN_STRING_SCHEMA ).elseSchema (MIN_LENGTH_STRING_SCHEMA ).build ().validate ("foobar" );
180
+ ConditionalSchema .builder ().ifSchema (MAX_LENGTH_STRING_SCHEMA ).thenSchema (PATTERN_STRING_SCHEMA )
181
+ .elseSchema (MIN_LENGTH_STRING_SCHEMA ).build ().validate ("foobar" );
158
182
}
159
183
160
184
@ Test
161
185
public void ifSubschemaFailureThenSubschemaSuccessElseSubSchemaFailure () {
162
- ConditionalSchema .Builder subject = ConditionalSchema .builder ().ifSchema (PATTERN_STRING_SCHEMA ).thenSchema (MAX_LENGTH_STRING_SCHEMA ).elseSchema (MIN_LENGTH_STRING_SCHEMA );
186
+ ConditionalSchema .Builder subject = ConditionalSchema .builder ().ifSchema (PATTERN_STRING_SCHEMA ).thenSchema (MAX_LENGTH_STRING_SCHEMA )
187
+ .elseSchema (MIN_LENGTH_STRING_SCHEMA );
163
188
TestSupport .failureOf (subject )
164
189
.expectedKeyword ("else" )
165
190
.expectedPointer ("#" )
@@ -169,17 +194,62 @@ public void ifSubschemaFailureThenSubschemaSuccessElseSubSchemaFailure() {
169
194
170
195
@ Test
171
196
public void ifSubschemaFailureThenSubschemaFailureElseSubSchemaSuccess () {
172
- ConditionalSchema .builder ().ifSchema (PATTERN_STRING_SCHEMA ).thenSchema (MIN_LENGTH_STRING_SCHEMA ).elseSchema (MAX_LENGTH_STRING_SCHEMA ).build ().validate ("bar" );
197
+ ConditionalSchema .builder ().ifSchema (PATTERN_STRING_SCHEMA ).thenSchema (MIN_LENGTH_STRING_SCHEMA )
198
+ .elseSchema (MAX_LENGTH_STRING_SCHEMA ).build ().validate ("bar" );
173
199
}
174
200
175
201
@ Test
176
202
public void ifSubschemaFailureThenSubschemaFailureElseSubSchemaFailure () {
177
- ConditionalSchema .Builder subject = ConditionalSchema .builder ().ifSchema (PATTERN_STRING_SCHEMA ).thenSchema (MIN_LENGTH_STRING_SCHEMA ).elseSchema (MAX_LENGTH_STRING_SCHEMA );
203
+ ConditionalSchema .Builder subject = ConditionalSchema .builder ().ifSchema (PATTERN_STRING_SCHEMA ).thenSchema (MIN_LENGTH_STRING_SCHEMA )
204
+ .elseSchema (MAX_LENGTH_STRING_SCHEMA );
178
205
TestSupport .failureOf (subject )
179
206
.expectedKeyword ("else" )
180
207
.expectedPointer ("#" )
181
208
.input ("barbarbar" )
182
209
.expect ();
183
210
}
184
211
185
- }
212
+ @ Test
213
+ public void toStringTest () {
214
+ ConditionalSchema subject = initCompleteSchema ().build ();
215
+
216
+ JSONObject actual = new JSONObject (subject .toString ());
217
+
218
+ assertThat (actual , sameJsonAs (LOADER .readObj ("conditionalschema.json" )));
219
+ }
220
+
221
+ @ Test
222
+ public void toString_noIf () {
223
+ ConditionalSchema subject = initCompleteSchema ().ifSchema (null ).build ();
224
+ JSONObject expectedSchemaJson = LOADER .readObj ("conditionalschema.json" );
225
+ expectedSchemaJson .remove ("if" );
226
+
227
+ JSONObject actual = new JSONObject (subject .toString ());
228
+
229
+ assertThat (actual , sameJsonAs (expectedSchemaJson ));
230
+ }
231
+
232
+ @ Test
233
+ public void toString_noThen () {
234
+ ConditionalSchema subject = initCompleteSchema ().thenSchema (null ).build ();
235
+ JSONObject expectedSchemaJson = LOADER .readObj ("conditionalschema.json" );
236
+ expectedSchemaJson .remove ("then" );
237
+
238
+ JSONObject actual = new JSONObject (subject .toString ());
239
+
240
+ assertThat (actual , sameJsonAs (expectedSchemaJson ));
241
+ }
242
+
243
+ @ Test
244
+ public void toString_noElse () {
245
+ ConditionalSchema subject = initCompleteSchema ().thenSchema (null ).elseSchema (null ).build ();
246
+ JSONObject expectedSchemaJson = LOADER .readObj ("conditionalschema.json" );
247
+ expectedSchemaJson .remove ("then" );
248
+ expectedSchemaJson .remove ("else" );
249
+
250
+ JSONObject actual = new JSONObject (subject .toString ());
251
+
252
+ assertThat (actual , sameJsonAs (expectedSchemaJson ));
253
+ }
254
+
255
+ }
0 commit comments