Skip to content

Commit 2f37b9e

Browse files
author
Anderson Ferreira
committed
Adding Unit Tests for IfValidator
1 parent 01dfc0b commit 2f37b9e

File tree

2 files changed

+281
-0
lines changed

2 files changed

+281
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2016 Network New Technologies Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.networknt.schema;
18+
19+
import com.fasterxml.jackson.databind.JsonNode;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import java.util.*;
24+
25+
public class IfValidator extends BaseJsonValidator implements JsonValidator {
26+
private static final Logger logger = LoggerFactory.getLogger(IfValidator.class);
27+
28+
private static final ArrayList<String> KEYWORDS = new ArrayList<String>(Arrays.asList("if", "then", "else"));
29+
30+
private JsonSchema ifSchema;
31+
private JsonSchema thenSchema;
32+
private JsonSchema elseSchema;
33+
34+
public IfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
35+
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.IF_THEN_ELSE, validationContext);
36+
37+
for (final String keyword : KEYWORDS) {
38+
final JsonNode node = schemaNode.get(keyword);
39+
if (keyword.equals("if")) {
40+
ifSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema);
41+
} else if (keyword.equals("then") && node != null) {
42+
thenSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema);
43+
} else if (keyword.equals("else") && node != null) {
44+
elseSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), node, parentSchema);
45+
}
46+
}
47+
}
48+
49+
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
50+
debug(logger, node, rootNode, at);
51+
52+
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
53+
54+
Set<ValidationMessage> ifErrors = ifSchema.validate(node, rootNode, at);
55+
if (ifErrors.isEmpty() && thenSchema != null) {
56+
errors.addAll(thenSchema.validate(node, rootNode, at));
57+
} else if (thenSchema != null && elseSchema != null) {
58+
errors.addAll(elseSchema.validate(node, rootNode, at));
59+
}
60+
61+
return Collections.unmodifiableSet(errors);
62+
}
63+
64+
}

src/test/resources/tests/if.json

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
[
2+
{
3+
"description": "Simple if (incomplete structure, without THEN and ELSE, no validation is performed)",
4+
"schema": {
5+
"then": {
6+
"properties": {
7+
"foo": { "maximum": 3.0 }
8+
}
9+
},
10+
"else": {
11+
"properties": {
12+
"foo": { "maximum": 3.0 }
13+
}
14+
}
15+
},
16+
"tests": [
17+
{
18+
"description": "if valid",
19+
"data": { "foo": 2.9 },
20+
"valid": true
21+
},
22+
{
23+
"description": "if invalid",
24+
"data": { "foo": 3.1 },
25+
"valid": true
26+
}
27+
]
28+
},
29+
{
30+
"description": "Simple if (incomplete structure, without THEN and ELSE, no validation is performed)",
31+
"schema": {
32+
"if": {
33+
"properties": {
34+
"foo": { "maximum": 3.0 }
35+
}
36+
}
37+
},
38+
"tests": [
39+
{
40+
"description": "if valid",
41+
"data": { "foo": 2.9 },
42+
"valid": true
43+
},
44+
{
45+
"description": "if invalid",
46+
"data": { "foo": 3.1 },
47+
"valid": true
48+
}
49+
]
50+
},
51+
{
52+
"description": "Simple if-else (incomplete structure, without THEN, no validation is performed)",
53+
"schema": {
54+
"if": {
55+
"properties": {
56+
"foo": { "minimum": 3.0 }
57+
}
58+
},
59+
"else": {
60+
"required": [ "bar" ]
61+
}
62+
},
63+
"tests": [
64+
{
65+
"description": "if valid, no validation (THEN is not executed because it doesn't exist)",
66+
"data": { "foo": 3.1 },
67+
"valid": true
68+
},
69+
{
70+
"description": "if invalid, no validation (ELSE is not executed because THEN doesn't exist)",
71+
"data": { "foo": 2.9 },
72+
"valid": true
73+
}
74+
]
75+
},
76+
{
77+
"description": "Simple if-then (complete structure, without ELSE)",
78+
"schema": {
79+
"if": {
80+
"properties": {
81+
"foo": { "pattern": "^[AB]$" }
82+
}
83+
},
84+
"then": {
85+
"required": [ "bar" ]
86+
}
87+
},
88+
"tests": [
89+
{
90+
"description": "Simple if-then valid",
91+
"data": { "foo": "A", "bar": 1 },
92+
"valid": true
93+
},
94+
{
95+
"description": "Simple if-then invalid",
96+
"data": { "foo": "A" },
97+
"valid": false
98+
}
99+
]
100+
},
101+
{
102+
"description": "Simple if-then-else (complete structure)",
103+
"schema": {
104+
"if": {
105+
"properties": {
106+
"foo": { "pattern": "^[AB]$" }
107+
}
108+
},
109+
"then": {
110+
"required": [ "bar" ]
111+
},
112+
"else": {
113+
"required": [ "baz" ]
114+
}
115+
},
116+
"tests": [
117+
{
118+
"description": "Simple if-then-else, then valid",
119+
"data": { "foo": "A", "bar": 1 },
120+
"valid": true
121+
},
122+
{
123+
"description": "Simple if-then-else, then invalid",
124+
"data": { "foo": "A" },
125+
"valid": false
126+
},
127+
{
128+
"description": "Simple if-then-else, else valid",
129+
"data": { "foo": "C", "baz": 1 },
130+
"valid": true
131+
},
132+
{
133+
"description": "Simple if-then-else, else invalid",
134+
"data": { "foo": "C" },
135+
"valid": false
136+
}
137+
]
138+
},
139+
{
140+
"description": "Nested if-then-else (complete structure)",
141+
"schema": {
142+
"if": {
143+
"properties": {
144+
"foo": { "pattern": "^[AB]$" }
145+
}
146+
},
147+
"then": {
148+
"if": {
149+
"properties": {
150+
"bar": { "pattern": "^[CD]$" }
151+
}
152+
},
153+
"then": {
154+
"required": [ "baz" ]
155+
},
156+
"else": {
157+
"required": [ "qux" ]
158+
}
159+
},
160+
"else": {
161+
"if": {
162+
"properties": {
163+
"bar": { "pattern": "^[XY]$" }
164+
}
165+
},
166+
"then": {
167+
"required": [ "baz" ]
168+
},
169+
"else": {
170+
"required": [ "qux" ]
171+
}
172+
}
173+
},
174+
"tests": [
175+
{
176+
"description": "if-then-if-then valid",
177+
"data": { "foo": "A", "bar": "C", "baz": 1 },
178+
"valid": true
179+
},
180+
{
181+
"description": "if-then-if-then invalid",
182+
"data": { "foo": "A", "bar": "C" },
183+
"valid": false
184+
},
185+
{
186+
"description": "if-then-if-else valid",
187+
"data": { "foo": "A", "bar": "E", "qux": 1 },
188+
"valid": true
189+
},
190+
{
191+
"description": "if-then-if-else invalid",
192+
"data": { "foo": "A", "bar": "E" },
193+
"valid": false
194+
},
195+
{
196+
"description": "if-else-if-then valid",
197+
"data": { "foo": "C", "bar": "X", "baz": 1 },
198+
"valid": true
199+
},
200+
{
201+
"description": "if-else-if-then invalid",
202+
"data": { "foo": "C", "bar": "X" },
203+
"valid": false
204+
},
205+
{
206+
"description": "if-else-if-else valid",
207+
"data": { "foo": "C", "bar": "Z", "qux": 1 },
208+
"valid": true
209+
},
210+
{
211+
"description": "if-else-if-else invalid",
212+
"data": { "foo": "C", "bar": "Z" },
213+
"valid": false
214+
}
215+
]
216+
}
217+
]

0 commit comments

Comments
 (0)