ValidationException extends RuntimeException { public JSONObject toJSON() { JSONObject rval = new JSONObject(); rval.put("keyword", keyword); if (pointerToViolation == null) { rval.put("pointerToViolation", JSONObject.NULL); } else { rval.put("pointerToViolation", getPointerToViolation()); } rval.put("message", super.getMessage()); List<JSONObject> causeJsons = causingExceptions.stream() .map(ValidationException::toJSON) .collect(Collectors.toList()); rval.put("causingExceptions", new JSONArray(causeJsons)); if (schemaLocation != null) { rval.put("schemaLocation", schemaLocation); } return rval; } @Deprecated  ValidationException(Class<?> expectedType, Object actualValue);  ValidationException(Schema violatedSchema, Class<?> expectedType,
            Object actualValue); @Deprecated  ValidationException(Schema violatedSchema, Class<?> expectedType,
            Object actualValue, String keyword);  ValidationException(Schema violatedSchema, Class<?> expectedType,
            Object actualValue, String keyword, String schemaLocation); @Deprecated  ValidationException(Schema violatedSchema, String message); @Deprecated  ValidationException(Schema violatedSchema,
            String message,
            String keyword);  ValidationException(Schema violatedSchema,
            String message,
            String keyword,
            String schemaLocation); @Deprecated  ValidationException(Schema violatedSchema, StringBuilder pointerToViolation,
            String message,
            List<ValidationException> causingExceptions);   ValidationException(Schema violatedSchema, StringBuilder pointerToViolation,
            String message,
            List<ValidationException> causingExceptions,
            String keyword,
            String schemaLocation); @Deprecated  ValidationException(String message); private  ValidationException(StringBuilder pointerToViolation,
            Schema violatedSchema,
            String message,
            List<ValidationException> causingExceptions,
            String keyword, String schemaLocation); @Deprecated  ValidationException(Schema violatedSchema, String message,
            List<ValidationException> causingExceptions); static void throwFor(Schema rootFailingSchema,
            List<ValidationException> failures); List<ValidationException> getCausingExceptions(); List<String> getAllMessages(); @Override String getMessage(); String getErrorMessage(); String getPointerToViolation(); Schema getViolatedSchema(); ValidationException prepend(String fragment); ValidationException prepend(String fragment, Schema violatedSchema); int getViolationCount(); String getKeyword(); JSONObject toJSON(); String getSchemaLocation(); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void testToJSON() { ValidationException subject = new ValidationException(BooleanSchema.INSTANCE, new StringBuilder("#/a/b"), "exception message", emptyList(), "type", null); JSONObject expected = loader.readObj("exception-to-json.json"); JSONObject actual = subject.toJSON(); assertThat(actual, sameJsonAs(expected)); }
@Test public void testToJSONWithSchemaLocation() { ValidationException subject = new ValidationException(BooleanSchema.INSTANCE, new StringBuilder("#/a/b"), "exception message", emptyList(), "type", "#/schema/location"); JSONObject expected = loader.readObj("exception-to-json-with-schema-location.json"); JSONObject actual = subject.toJSON(); assertThat(actual, sameJsonAs(expected)); }
@Test public void toJSONNullPointerToViolation() { ValidationException subject = new ValidationException(BooleanSchema.INSTANCE, null, "exception message", emptyList(), "type", null); JSONObject actual = subject.toJSON(); assertEquals(JSONObject.NULL, actual.get("pointerToViolation")); }
@Test public void toJSONWithCauses() { ValidationException cause = new ValidationException(NullSchema.INSTANCE, new StringBuilder("#/a/0"), "cause msg", emptyList(), "type", null); ValidationException subject = new ValidationException(BooleanSchema.INSTANCE, new StringBuilder("#/a"), "exception message", Arrays.asList(cause), "type", null); JSONObject expected = ResourceLoader.DEFAULT.readObj("exception-to-json-with-causes.json"); JSONObject actual = subject.toJSON(); assertThat(actual, sameJsonAs(expected)); }