Search in sources :

Example 11 with ValidationException

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/' ");
        }
    };
}
Also used : ValidationException(org.everit.json.schema.ValidationException) Description(org.hamcrest.Description) JSONObject(org.json.JSONObject) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) IOException(java.io.IOException) TypeSafeDiagnosingMatcher(org.hamcrest.TypeSafeDiagnosingMatcher)

Example 12 with ValidationException

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")));
}
Also used : Response(javax.ws.rs.core.Response) ValidationException(org.everit.json.schema.ValidationException) JsonSchemaValidationException(uk.gov.justice.services.core.json.JsonSchemaValidationException) JsonSchemaValidationException(uk.gov.justice.services.core.json.JsonSchemaValidationException) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException) Test(org.junit.Test)

Aggregations

ValidationException (org.everit.json.schema.ValidationException)12 JSONObject (org.json.JSONObject)7 Test (org.junit.Test)6 Schema (org.everit.json.schema.Schema)5 IOException (java.io.IOException)3 MediaType (uk.gov.justice.services.core.mapping.MediaType)2 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)2 SubscriptionDescriptorFileValidator (uk.gov.justice.subscription.file.read.SubscriptionDescriptorFileValidator)2 YamlFileToJsonObjectConverter (uk.gov.justice.subscription.file.read.YamlFileToJsonObjectConverter)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)1 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)1 ParameterNamesModule (com.fasterxml.jackson.module.paramnames.ParameterNamesModule)1 Config (com.typesafe.config.Config)1 InputStream (java.io.InputStream)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 JsonValue (javax.json.JsonValue)1 Response (javax.ws.rs.core.Response)1 ModelId (org.eclipse.vorto.repository.api.ModelId)1 Description (org.hamcrest.Description)1