use of org.everit.json.schema.ValidationException in project microservice_framework by CJSCommonPlatform.
the class JsonSchemaValidationMatcher method isValidJsonEnvelopeForSchema.
/**
* Validates a JsonEnvelope against the correct schema for the action name provided in the
* metadata. Expects to find the schema on the class path in package
* 'json/schema/{action.name}.json' or 'raml/json/schema/{action.name}.json'.
*
* @return matcher
*/
public static Matcher<JsonEnvelope> isValidJsonEnvelopeForSchema() {
return new TypeSafeDiagnosingMatcher<JsonEnvelope>() {
private ValidationException validationException = null;
@Override
protected boolean matchesSafely(final JsonEnvelope jsonEnvelope, final Description description) {
if (null == validationException) {
try {
final String pathToJsonSchema = format(JSON_SCHEMA_TEMPLATE, jsonEnvelope.metadata().name());
getJsonSchemaFor(pathToJsonSchema).validate(new JSONObject(jsonEnvelope.payloadAsJsonObject().toString()));
} catch (final IllegalArgumentException | IOException e) {
try {
final String pathToJsonSchema = format(RAML_JSON_SCHEMA_TEMPLATE, jsonEnvelope.metadata().name());
getJsonSchemaFor(pathToJsonSchema).validate(new JSONObject(jsonEnvelope.payloadAsJsonObject().toString()));
} catch (final IOException ioe) {
throw new IllegalArgumentException(ioe);
}
} catch (final ValidationException e) {
validationException = e;
return false;
}
return true;
} else {
description.appendText("Schema validation failed with message: ").appendValue(validationException.getMessage());
return false;
}
}
@Override
public void describeTo(final Description description) {
description.appendText("JsonEnvelope validated against schema found on classpath at 'raml/json/schema/' ");
}
};
}
use of org.everit.json.schema.ValidationException in project microservice_framework by CJSCommonPlatform.
the class BadRequestExceptionMapperTest method shouldAddJsonValidationErrorsToResponse.
@Test
public void shouldAddJsonValidationErrorsToResponse() {
final ValidationException validationException = new ValidationException(schema, "Test Json");
final JsonSchemaValidationException jsonSchemaValidationException = new JsonSchemaValidationException(validationException.getMessage(), validationException);
final BadRequestException badRequestException = new BadRequestException(TEST_ERROR_MESSAGE, jsonSchemaValidationException);
final Response response = exceptionMapper.toResponse(badRequestException);
final String body = response.getEntity().toString();
assertThat(body, hasJsonPath("$.validationErrors.message", equalTo("#: Test Json")));
}
Aggregations