use of uk.gov.justice.services.core.mapping.MediaType in project microservice_framework by CJSCommonPlatform.
the class SubscriptionMediaTypeToSchemaIdGeneratorTest method shouldCreateMediaTypeToSchemaIdMapper.
@Test
public void shouldCreateMediaTypeToSchemaIdMapper() throws Exception {
final String eventName_1 = "ctx.command.command1";
final String mediaType_1 = "application/vnd." + eventName_1 + "+json";
final String schemaId_1 = "http://justice.gov.uk/test/schema1.json";
final String eventName_2 = "ctx.command.command2";
final String mediaType_2 = "application/vnd." + eventName_2 + "+json";
final String schemaId_2 = "http://justice.gov.uk/test/schema2.json";
final String contextName = "my-context";
final String componentName = "EVENT_LISTENER";
final String generatedClassName = "MyContextEventListenerMediaTypeToSchemaIdMapper";
final List<Event> events = asList(new Event(eventName_1, schemaId_1), new Event(eventName_2, schemaId_2));
new SubscriptionMediaTypeToSchemaIdGenerator().generateMediaTypeToSchemaIdMapper(contextName, componentName, events, configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
final Class<?> schemaIdMapperClass = compiler.compiledClassOf(BASE_PACKAGE, "mapper", generatedClassName);
final MediaTypeToSchemaIdMapper instance = (MediaTypeToSchemaIdMapper) schemaIdMapperClass.newInstance();
final Map<MediaType, String> mediaTypeToSchemaIdMap = instance.getMediaTypeToSchemaIdMap();
assertThat(mediaTypeToSchemaIdMap.size(), is(2));
assertThat(mediaTypeToSchemaIdMap.get(new MediaType(mediaType_1)), is(schemaId_1));
assertThat(mediaTypeToSchemaIdMap.get(new MediaType(mediaType_2)), is(schemaId_2));
}
use of uk.gov.justice.services.core.mapping.MediaType 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.mapping.MediaType in project microservice_framework by CJSCommonPlatform.
the class JsonSchemaValidationInterceptorTest method shouldReturnContextProceed.
@Test
public void shouldReturnContextProceed() throws Exception {
final String payload = "{\"the\": \"payload\"}";
final Object proceed = new Object();
final TextMessage message = mock(TextMessage.class);
final MediaType mediaType = mock(MediaType.class);
final String actionName = message.getStringProperty(JMS_HEADER_CPPNAME);
when(invocationContext.proceed()).thenReturn(proceed);
when(invocationContext.getParameters()).thenReturn(new Object[] { message });
when(message.getText()).thenReturn(payload);
when(nameToMediaTypeConverter.convert(actionName)).thenReturn(mediaType);
assertThat(jsonSchemaValidationInterceptor.validate(invocationContext), sameInstance(proceed));
}
use of uk.gov.justice.services.core.mapping.MediaType in project microservice_framework by CJSCommonPlatform.
the class JsonSchemaValidationInterceptorTest method shouldValidateMessage.
@Test
public void shouldValidateMessage() throws Exception {
final TextMessage message = mock(TextMessage.class);
final String payload = "test payload";
final String name = "test-name";
final MediaType mediaType = mock(MediaType.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);
jsonSchemaValidationInterceptor.validate(invocationContext);
verify(jsonSchemaValidator).validate(payload, name, of(mediaType));
}
use of uk.gov.justice.services.core.mapping.MediaType 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");
}
}
Aggregations