|
19 | 19 | import nl.jqno.equalsverifier.Warning;
|
20 | 20 | import org.everit.json.schema.loader.SchemaLoader;
|
21 | 21 | import org.json.JSONObject;
|
22 |
| -import org.json.JSONTokener; |
23 | 22 | import org.junit.Assert;
|
24 |
| -import org.junit.Ignore; |
25 | 23 | import org.junit.Test;
|
26 | 24 |
|
| 25 | +import java.util.List; |
| 26 | +import java.util.concurrent.Callable; |
| 27 | + |
27 | 28 | import static org.junit.Assert.assertTrue;
|
28 | 29 |
|
29 | 30 | public class ObjectSchemaTest {
|
@@ -136,9 +137,74 @@ public void multipleViolations() {
|
136 | 137 | Assert.fail("did not throw exception for 3 schema violations");
|
137 | 138 | } catch (ValidationException e) {
|
138 | 139 | Assert.assertEquals(3, e.getCausingExceptions().size());
|
139 |
| - Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(e, "#/numberProp")); |
140 | 140 | Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(e, "#"));
|
| 141 | + Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(e, "#/numberProp")); |
141 | 142 | Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(e, "#/stringPatternMatch"));
|
| 143 | + |
| 144 | + List<String> messages = e.getAllMessages(); |
| 145 | + Assert.assertEquals(3, messages.size()); |
| 146 | + Assert.assertEquals(1, TestSupport.countMatchingMessage(messages, "#:")); |
| 147 | + Assert.assertEquals(1, TestSupport.countMatchingMessage(messages, "#/numberProp:")); |
| 148 | + Assert.assertEquals(1, TestSupport.countMatchingMessage(messages, "#/stringPatternMatch:")); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @SuppressWarnings("OptionalGetWithoutIsPresent") |
| 153 | + @Test |
| 154 | + public void multipleViolationsNested() throws Exception { |
| 155 | + Callable<ObjectSchema.Builder> newBuilder = () -> ObjectSchema.builder() |
| 156 | + .addPropertySchema("numberProp", new NumberSchema()) |
| 157 | + .patternProperty("^string.*", new StringSchema()) |
| 158 | + .addPropertySchema("boolProp", BooleanSchema.INSTANCE) |
| 159 | + .addRequiredProperty("boolProp"); |
| 160 | + |
| 161 | + Schema nested2 = newBuilder.call().build(); |
| 162 | + Schema nested1 = newBuilder.call().addPropertySchema("nested", nested2).build(); |
| 163 | + Schema subject = newBuilder.call().addPropertySchema("nested", nested1).build(); |
| 164 | + |
| 165 | + try { |
| 166 | + subject.validate(OBJECTS.get("multipleViolationsNested")); |
| 167 | + Assert.fail("did not throw exception for 9 schema violations"); |
| 168 | + } catch (ValidationException subjectException) { |
| 169 | + Assert.assertEquals("#: 9 schema violations found", subjectException.getMessage()); |
| 170 | + Assert.assertEquals(4, subjectException.getCausingExceptions().size()); |
| 171 | + Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(subjectException, "#")); |
| 172 | + Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(subjectException, "#/numberProp")); |
| 173 | + Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(subjectException, "#/stringPatternMatch")); |
| 174 | + Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(subjectException, "#/nested")); |
| 175 | + |
| 176 | + ValidationException nested1Exception = subjectException.getCausingExceptions().stream() |
| 177 | + .filter(ex -> ex.getPointerToViolation().equals("#/nested")) |
| 178 | + .findFirst() |
| 179 | + .get(); |
| 180 | + Assert.assertEquals("#/nested: 6 schema violations found", nested1Exception.getMessage()); |
| 181 | + Assert.assertEquals(4, nested1Exception.getCausingExceptions().size()); |
| 182 | + Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(nested1Exception, "#/nested")); |
| 183 | + Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(nested1Exception, "#/nested/numberProp")); |
| 184 | + Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(nested1Exception, "#/nested/stringPatternMatch")); |
| 185 | + Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(nested1Exception, "#/nested/nested")); |
| 186 | + |
| 187 | + ValidationException nested2Exception = nested1Exception.getCausingExceptions().stream() |
| 188 | + .filter(ex -> ex.getPointerToViolation().equals("#/nested/nested")) |
| 189 | + .findFirst() |
| 190 | + .get(); |
| 191 | + Assert.assertEquals("#/nested/nested: 3 schema violations found", nested2Exception.getMessage()); |
| 192 | + Assert.assertEquals(3, nested2Exception.getCausingExceptions().size()); |
| 193 | + Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(nested2Exception, "#/nested/nested")); |
| 194 | + Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(nested2Exception, "#/nested/nested/numberProp")); |
| 195 | + Assert.assertEquals(1, TestSupport.countCauseByJsonPointer(nested2Exception, "#/nested/nested/stringPatternMatch")); |
| 196 | + |
| 197 | + List<String> messages = subjectException.getAllMessages(); |
| 198 | + Assert.assertEquals(9, messages.size()); |
| 199 | + Assert.assertEquals(1, TestSupport.countMatchingMessage(messages, "#:")); |
| 200 | + Assert.assertEquals(1, TestSupport.countMatchingMessage(messages, "#/numberProp:")); |
| 201 | + Assert.assertEquals(1, TestSupport.countMatchingMessage(messages, "#/stringPatternMatch:")); |
| 202 | + Assert.assertEquals(1, TestSupport.countMatchingMessage(messages, "#/nested:")); |
| 203 | + Assert.assertEquals(1, TestSupport.countMatchingMessage(messages, "#/nested/numberProp:")); |
| 204 | + Assert.assertEquals(1, TestSupport.countMatchingMessage(messages, "#/nested/stringPatternMatch:")); |
| 205 | + Assert.assertEquals(1, TestSupport.countMatchingMessage(messages, "#/nested/nested:")); |
| 206 | + Assert.assertEquals(1, TestSupport.countMatchingMessage(messages, "#/nested/nested/numberProp:")); |
| 207 | + Assert.assertEquals(1, TestSupport.countMatchingMessage(messages, "#/nested/nested/stringPatternMatch:")); |
142 | 208 | }
|
143 | 209 | }
|
144 | 210 |
|
|
0 commit comments