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();
}
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)));
}
}
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);
}
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));
}
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);
}
Aggregations