Skip to content

Commit 7b3a5d6

Browse files
committed
Add test for multiple nested violations
1 parent d5847b0 commit 7b3a5d6

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

core/src/test/java/org/everit/json/schema/ObjectSchemaTest.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import nl.jqno.equalsverifier.Warning;
2020
import org.everit.json.schema.loader.SchemaLoader;
2121
import org.json.JSONObject;
22-
import org.json.JSONTokener;
2322
import org.junit.Assert;
24-
import org.junit.Ignore;
2523
import org.junit.Test;
2624

25+
import java.util.concurrent.Callable;
26+
2727
import static org.junit.Assert.assertTrue;
2828

2929
public class ObjectSchemaTest {
@@ -142,6 +142,53 @@ public void multipleViolations() {
142142
}
143143
}
144144

145+
@SuppressWarnings("OptionalGetWithoutIsPresent")
146+
@Test
147+
public void multipleViolationsNested() throws Exception {
148+
Callable<ObjectSchema.Builder> newBuilder = () -> ObjectSchema.builder()
149+
.addPropertySchema("numberProp", new NumberSchema())
150+
.patternProperty("^string.*", new StringSchema())
151+
.addPropertySchema("boolProp", BooleanSchema.INSTANCE)
152+
.addRequiredProperty("boolProp");
153+
154+
Schema nested2 = newBuilder.call().build();
155+
Schema nested1 = newBuilder.call().addPropertySchema("nested", nested2).build();
156+
Schema subject = newBuilder.call().addPropertySchema("nested", nested1).build();
157+
158+
try {
159+
subject.validate(OBJECTS.get("multipleViolationsNested"));
160+
Assert.fail("did not throw exception for 9 schema violations");
161+
} catch (ValidationException subjectException) {
162+
Assert.assertEquals("#: 9 schema violations found", subjectException.getMessage());
163+
Assert.assertEquals(4, subjectException.getCausingExceptions().size());
164+
Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(subjectException, "#"));
165+
Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(subjectException, "#/numberProp"));
166+
Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(subjectException, "#/stringPatternMatch"));
167+
Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(subjectException, "#/nested"));
168+
169+
ValidationException nested1Exception = subjectException.getCausingExceptions().stream()
170+
.filter(ex -> ex.getPointerToViolation().equals("#/nested"))
171+
.findFirst()
172+
.get();
173+
Assert.assertEquals("#/nested: 6 schema violations found", nested1Exception.getMessage());
174+
Assert.assertEquals(4, nested1Exception.getCausingExceptions().size());
175+
Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(nested1Exception, "#/nested"));
176+
Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(nested1Exception, "#/nested/numberProp"));
177+
Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(nested1Exception, "#/nested/stringPatternMatch"));
178+
Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(nested1Exception, "#/nested/nested"));
179+
180+
ValidationException nested2Exception = nested1Exception.getCausingExceptions().stream()
181+
.filter(ex -> ex.getPointerToViolation().equals("#/nested/nested"))
182+
.findFirst()
183+
.get();
184+
Assert.assertEquals("#/nested/nested: 3 schema violations found", nested2Exception.getMessage());
185+
Assert.assertEquals(3, nested2Exception.getCausingExceptions().size());
186+
Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(nested2Exception, "#/nested/nested"));
187+
Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(nested2Exception, "#/nested/nested/numberProp"));
188+
Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(nested2Exception, "#/nested/nested/stringPatternMatch"));
189+
}
190+
}
191+
145192
@Test
146193
public void noAdditionalProperties() {
147194
ObjectSchema subject = ObjectSchema.builder().additionalProperties(false).build();

core/src/test/resources/org/everit/jsonvalidator/objecttestcases.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@
4040
"numberProp": "not number",
4141
"stringPatternMatch": 2
4242
},
43+
"multipleViolationsNested": {
44+
"numberProp": "not number",
45+
"stringPatternMatch": 2,
46+
"nested": {
47+
"numberProp": "not number 1",
48+
"stringPatternMatch": 11,
49+
"nested": {
50+
"numberProp": "not number 2",
51+
"stringPatternMatch": 22
52+
}
53+
}
54+
},
4355
"rectangleSingleFailure": {
4456
"rectangle": {
4557
"a": -5,

0 commit comments

Comments
 (0)