Search in sources :

Example 21 with MediaType

use of uk.gov.justice.services.core.mapping.MediaType in project microservice_framework by CJSCommonPlatform.

the class JsonSchemaValidationInterceptor method aroundReadFrom.

@Override
public Object aroundReadFrom(final ReaderInterceptorContext context) throws IOException, WebApplicationException {
    final MediaType mediaType = new MediaType(context.getMediaType().toString());
    if (mediaType.getSubtype().endsWith(JSON_MEDIA_TYPE_SUFFIX)) {
        final String charset = charsetFrom(context.getMediaType());
        final String payload = IOUtils.toString(context.getInputStream(), charset);
        try {
            restJsonSchemaValidator.validate(payload, nameToMediaTypeConverter.convert(mediaType), of(mediaType));
        } catch (final JsonSchemaValidationException jsonSchemaValidationException) {
            final String message = format("JSON schema validation has failed on %s due to %s ", httpTraceLoggerHelper.toHttpHeaderTrace(context.getHeaders()), jsonValidationLoggerHelper.toValidationTrace(jsonSchemaValidationException));
            logger.debug(message);
            throw new BadRequestException(message, jsonSchemaValidationException);
        } catch (InvalidMediaTypeException ex) {
            throw new BadRequestException(ex.getMessage(), ex);
        }
        context.setInputStream(new ByteArrayInputStream(payload.getBytes(charset)));
    }
    return context.proceed();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JsonSchemaValidationException(uk.gov.justice.services.core.json.JsonSchemaValidationException) MediaType(uk.gov.justice.services.core.mapping.MediaType) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException) InvalidMediaTypeException(uk.gov.justice.services.messaging.exception.InvalidMediaTypeException)

Example 22 with MediaType

use of uk.gov.justice.services.core.mapping.MediaType in project microservice_framework by CJSCommonPlatform.

the class SchemaCatalogAwareJsonSchemaValidatorTest method shouldThrowExceptionIfSchemaValidationFails.

@Test
public void shouldThrowExceptionIfSchemaValidationFails() {
    final String uri = "http://space.time.gov.uk/mind/command/api/initiate-warp-speed.json";
    final String actionName = "command.api.initiate-warp-speed";
    final Optional<String> schemaId = of(uri);
    final MediaType mediaType = new MediaType("application", "vnd.mind.command.initiate-warp-speed+json");
    final String envelopeJson = "{\"envelope\": \"json\"}";
    final Schema schema = mock(Schema.class);
    final JSONObject payload = mock(JSONObject.class);
    when(schemaIdMappingCache.schemaIdFor(mediaType)).thenReturn(schemaId);
    when(schemaCatalogService.findSchema(uri)).thenReturn(of(schema));
    when(payloadExtractor.extractPayloadFrom(envelopeJson)).thenReturn(payload);
    doThrow(new ValidationException(schema, "", "", "")).when(schema).validate(payload);
    try {
        schemaCatalogAwareJsonSchemaValidator.validate(envelopeJson, actionName, of(mediaType));
        fail();
    } catch (final JsonSchemaValidationException e) {
        assertThat(e.getCause(), is(instanceOf(ValidationException.class)));
    }
}
Also used : ValidationException(org.everit.json.schema.ValidationException) JSONObject(org.json.JSONObject) Schema(org.everit.json.schema.Schema) MediaType(uk.gov.justice.services.core.mapping.MediaType) Test(org.junit.Test)

Example 23 with MediaType

use of uk.gov.justice.services.core.mapping.MediaType in project microservice_framework by CJSCommonPlatform.

the class SchemaCatalogAwareJsonSchemaValidatorTest method shouldLoadTheCorrectSchemaUsingTheCatalogServiceAndValidate.

@Test
public void shouldLoadTheCorrectSchemaUsingTheCatalogServiceAndValidate() throws Exception {
    final String uri = "http://space.time.gov.uk/mind/command/api/initiate-warp-speed.json";
    final String actionName = "command.api.initiate-warp-speed";
    final Optional<String> schemaId = of(uri);
    final MediaType mediaType = new MediaType("application", "vnd.mind.command.initiate-warp-speed+json");
    final String envelopeJson = "{\"envelope\": \"json\"}";
    final Schema schema = mock(Schema.class);
    final JSONObject payload = mock(JSONObject.class);
    when(schemaIdMappingCache.schemaIdFor(mediaType)).thenReturn(schemaId);
    when(schemaCatalogService.findSchema(uri)).thenReturn(of(schema));
    when(payloadExtractor.extractPayloadFrom(envelopeJson)).thenReturn(payload);
    schemaCatalogAwareJsonSchemaValidator.validate(envelopeJson, actionName, of(mediaType));
    verify(logger).info("Performing schema validation with catalog schema for action 'command.api.initiate-warp-speed' and mediaType 'application/vnd.mind.command.initiate-warp-speed+json");
    verify(schema).validate(payload);
    verifyZeroInteractions(fileBasedJsonSchemaValidator);
}
Also used : JSONObject(org.json.JSONObject) Schema(org.everit.json.schema.Schema) MediaType(uk.gov.justice.services.core.mapping.MediaType) Test(org.junit.Test)

Example 24 with MediaType

use of uk.gov.justice.services.core.mapping.MediaType in project microservice_framework by CJSCommonPlatform.

the class EnvelopeValidatorTest method shouldHandleAValidationException.

@SuppressWarnings("deprecation")
@Test
public void shouldHandleAValidationException() throws Exception {
    final ValidationException validationException = new ValidationException("Ooops");
    final String actionName = "exaple.action-name";
    final String payloadJson = "{\"some\": \"json\"}";
    final Optional<MediaType> mediaType = of(new MediaType("application/vnd.example.action-name+json"));
    final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class);
    final JsonValue payload = mock(JsonValue.class);
    when(jsonEnvelope.payload()).thenReturn(payload);
    when(objectMapper.writeValueAsString(payload)).thenReturn(payloadJson);
    when(jsonEnvelope.toObfuscatedDebugString()).thenReturn("debug-json");
    doThrow(validationException).when(jsonSchemaValidator).validate(payloadJson, actionName, mediaType);
    envelopeValidator.validate(jsonEnvelope, actionName, mediaType);
    verify(envelopeValidationExceptionHandler).handle(exceptionArgumentCaptor.capture());
    final EnvelopeValidationException envelopeValidationException = exceptionArgumentCaptor.getValue();
    assertThat(envelopeValidationException.getMessage(), is("Message not valid against schema: \ndebug-json"));
    assertThat(envelopeValidationException.getCause(), is(validationException));
}
Also used : ValidationException(org.everit.json.schema.ValidationException) JsonValue(javax.json.JsonValue) MediaType(uk.gov.justice.services.core.mapping.MediaType) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 25 with MediaType

use of uk.gov.justice.services.core.mapping.MediaType in project microservice_framework by CJSCommonPlatform.

the class EnvelopeValidatorTest method shouldValidateThePayloadOfAJsonEnvelope.

@Test
public void shouldValidateThePayloadOfAJsonEnvelope() throws Exception {
    final String actionName = "example.action-name";
    final Optional<MediaType> mediaType = of(new MediaType("application/vnd.example.action-name+json"));
    final String payloadJson = "{\"some\": \"json\"}";
    final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class);
    final JsonValue payload = mock(JsonValue.class);
    when(jsonEnvelope.payload()).thenReturn(payload);
    when(objectMapper.writeValueAsString(payload)).thenReturn(payloadJson);
    envelopeValidator.validate(jsonEnvelope, actionName, mediaType);
    verify(jsonSchemaValidator).validate(payloadJson, actionName, mediaType);
}
Also used : JsonValue(javax.json.JsonValue) MediaType(uk.gov.justice.services.core.mapping.MediaType) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Aggregations

MediaType (uk.gov.justice.services.core.mapping.MediaType)33 Test (org.junit.Test)30 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)8 MediaTypeToSchemaIdMapper (uk.gov.justice.services.core.mapping.MediaTypeToSchemaIdMapper)6 CommonGeneratorProperties (uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties)6 JsonValue (javax.json.JsonValue)5 MediaTypes (uk.gov.justice.services.core.mapping.MediaTypes)5 TypeSpec (com.squareup.javapoet.TypeSpec)3 TextMessage (javax.jms.TextMessage)3 MimeType (org.raml.model.MimeType)3 Raml (org.raml.model.Raml)3 JsonSchemaValidationException (uk.gov.justice.services.core.json.JsonSchemaValidationException)3 ActionNameToMediaTypesMapper (uk.gov.justice.services.core.mapping.ActionNameToMediaTypesMapper)3 Method (java.lang.reflect.Method)2 Map (java.util.Map)2 Schema (org.everit.json.schema.Schema)2 ValidationException (org.everit.json.schema.ValidationException)2 JSONObject (org.json.JSONObject)2 SchemaIdMapper (uk.gov.justice.services.core.annotation.SchemaIdMapper)2 Event (uk.gov.justice.subscription.domain.Event)2