use of uk.gov.justice.services.core.json.JsonSchemaValidationException in project microservice_framework by CJSCommonPlatform.
the class JsonSchemaValidationInterceptor method validate.
private void validate(final TextMessage message) throws JMSException {
try {
final String name = message.getStringProperty(JMS_HEADER_CPPNAME);
final MediaType mediaType = nameToMediaTypeConverter.convert(name);
jsonSchemaValidator.validate(message.getText(), name, of(mediaType));
} catch (final JsonSchemaValidationException jsonSchemaValidationException) {
logger.debug(format("JSON schema validation has failed for %s due to %s", jmsMessageLoggerHelper.toJmsTraceString(message), jsonValidationLoggerHelper.toValidationTrace(jsonSchemaValidationException)));
throw jsonSchemaValidationException;
}
}
use of uk.gov.justice.services.core.json.JsonSchemaValidationException in project microservice_framework by CJSCommonPlatform.
the class JsonSchemaValidationInterceptorTest method shouldThrowExceptionIfValidatorFails.
@Test
public void shouldThrowExceptionIfValidatorFails() throws Exception {
final TextMessage message = mock(TextMessage.class);
final String payload = "test payload";
final String name = "test-name";
final MediaType mediaType = mock(MediaType.class);
final JsonSchemaValidationException jsonSchemaValidationException = mock(JsonSchemaValidationException.class);
when(message.getText()).thenReturn(payload);
when(message.getStringProperty(JMS_HEADER_CPPNAME)).thenReturn(name);
when(invocationContext.getParameters()).thenReturn(new Object[] { message });
when(nameToMediaTypeConverter.convert(name)).thenReturn(mediaType);
doThrow(jsonSchemaValidationException).when(jsonSchemaValidator).validate(payload, name, of(mediaType));
when(jmsMessageLoggerHelper.toJmsTraceString(message)).thenReturn("message");
when(jsonValidationLoggerHelper.toValidationTrace(jsonSchemaValidationException)).thenReturn("jsonSchemaValidationException");
try {
jsonSchemaValidationInterceptor.validate(invocationContext);
fail();
} catch (final JsonSchemaValidationException e) {
verify(logger).debug("JSON schema validation has failed for message due to jsonSchemaValidationException");
}
}
use of uk.gov.justice.services.core.json.JsonSchemaValidationException in project microservice_framework by CJSCommonPlatform.
the class JsonSchemaValidationInterceptorTest method shouldThrowBadRequestExceptionIfValidatorFailsWithValidationException.
@Test(expected = BadRequestException.class)
@SuppressWarnings("unchecked")
public void shouldThrowBadRequestExceptionIfValidatorFailsWithValidationException() throws Exception {
final MultivaluedMap<String, String> headers = new MultivaluedHashMap();
final String actionName = "example.action-name";
when(nameToMediaTypeConverter.convert(CONVERTED_MEDIA_TYPE)).thenReturn(actionName);
doThrow(new JsonSchemaValidationException("")).when(jsonSchemaValidator).validate(PAYLOAD, actionName, of(CONVERTED_MEDIA_TYPE));
when(context.getHeaders()).thenReturn(headers);
jsonSchemaValidationInterceptor.aroundReadFrom(context);
}
use of uk.gov.justice.services.core.json.JsonSchemaValidationException 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.json.JsonSchemaValidationException in project microservice_framework by CJSCommonPlatform.
the class BadRequestExceptionMapper method toResponse.
@Override
public Response toResponse(final BadRequestException exception) {
logger.debug("Bad Request", exception);
final JsonObjectBuilder builder = createObjectBuilder().add("error", exception.getMessage());
if (exception.getCause() instanceof JsonSchemaValidationException) {
builder.add("validationErrors", jsonValidationLoggerHelper.toJsonObject((JsonSchemaValidationException) exception.getCause()));
}
return status(BAD_REQUEST).entity(builder.build().toString()).build();
}
Aggregations