Search in sources :

Example 16 with MediaType

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));
}
Also used : Event(uk.gov.justice.subscription.domain.Event) MediaTypeToSchemaIdMapper(uk.gov.justice.services.core.mapping.MediaTypeToSchemaIdMapper) MediaType(uk.gov.justice.services.core.mapping.MediaType) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) Test(org.junit.Test)

Example 17 with MediaType

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;
    }
}
Also used : JsonSchemaValidationException(uk.gov.justice.services.core.json.JsonSchemaValidationException) MediaType(uk.gov.justice.services.core.mapping.MediaType)

Example 18 with MediaType

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));
}
Also used : MediaType(uk.gov.justice.services.core.mapping.MediaType) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 19 with MediaType

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));
}
Also used : MediaType(uk.gov.justice.services.core.mapping.MediaType) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 20 with 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");
    }
}
Also used : JsonSchemaValidationException(uk.gov.justice.services.core.json.JsonSchemaValidationException) MediaType(uk.gov.justice.services.core.mapping.MediaType) TextMessage(javax.jms.TextMessage) 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