Search in sources :

Example 6 with MediaType

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

the class ActionNameToMediaTypesGeneratorTest method shouldCreateMediaTypeToSchemaIdMapperForGivenRamlWithGet.

@Test
public void shouldCreateMediaTypeToSchemaIdMapperForGivenRamlWithGet() throws Exception {
    final String actionName_1 = "contextA.someAction";
    final String responseType_1 = "application/vnd.ctx.query.somemediatype1+json";
    final String actionName_2 = "contextA.someOtherAction";
    final String responseType_2 = "application/vnd.ctx.query.somemediatype2+json";
    new ActionNameToMediaTypesGenerator().generateActionNameToMediaTypes(restRamlWithQueryApiDefaults().with(resource("/user").with(httpAction(GET).withDescription(mappingDescriptionWith(mapping().withName(actionName_1).withResponseType(responseType_1), mapping().withName(actionName_2).withResponseType(responseType_2)).build()).withResponseTypes(responseType_1, responseType_2))).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> mediaTypesMapperClass = compiler.compiledClassOf(BASE_PACKAGE, "mapper", "WarnameActionNameToMediaTypesMapper");
    final ActionNameToMediaTypesMapper instance = (ActionNameToMediaTypesMapper) mediaTypesMapperClass.newInstance();
    final Map<String, MediaTypes> actionNameToMediaTypesMap = instance.getActionNameToMediaTypesMap();
    assertThat(actionNameToMediaTypesMap.size(), is(2));
    assertThat(actionNameToMediaTypesMap.get(actionName_1).getResponseMediaType(), is(Optional.of(new MediaType(responseType_1))));
    assertThat(actionNameToMediaTypesMap.get(actionName_1).getRequestMediaType(), is(Optional.empty()));
    assertThat(actionNameToMediaTypesMap.get(actionName_2).getResponseMediaType(), is(Optional.of(new MediaType(responseType_2))));
    assertThat(actionNameToMediaTypesMap.get(actionName_2).getRequestMediaType(), is(Optional.empty()));
}
Also used : ActionNameToMediaTypesMapper(uk.gov.justice.services.core.mapping.ActionNameToMediaTypesMapper) MediaType(uk.gov.justice.services.core.mapping.MediaType) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) MediaTypes(uk.gov.justice.services.core.mapping.MediaTypes) Test(org.junit.Test)

Example 7 with MediaType

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

the class SchemaCatalogAwareJsonSchemaValidatorTest method shouldFallBackToFileBasedSchemaValidationIfNoSchemaFoundInTheCatalogCache.

@Test
public void shouldFallBackToFileBasedSchemaValidationIfNoSchemaFoundInTheCatalogCache() 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\"}";
    when(schemaIdMappingCache.schemaIdFor(mediaType)).thenReturn(schemaId);
    when(schemaCatalogService.findSchema(uri)).thenReturn(empty());
    schemaCatalogAwareJsonSchemaValidator.validate(envelopeJson, actionName, of(mediaType));
    verify(fileBasedJsonSchemaValidator).validateWithoutSchemaCatalog(envelopeJson, actionName);
    verifyZeroInteractions(logger);
    verifyZeroInteractions(payloadExtractor);
}
Also used : MediaType(uk.gov.justice.services.core.mapping.MediaType) Test(org.junit.Test)

Example 8 with MediaType

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

the class EnvelopeValidatorTest method shouldHandleASchemaLoadingException.

@Test
public void shouldHandleASchemaLoadingException() throws Exception {
    final SchemaLoadingException schemaLoadingException = new SchemaLoadingException("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);
    doThrow(schemaLoadingException).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("Could not load json schema that matches message type exaple.action-name."));
    assertThat(envelopeValidationException.getCause(), is(schemaLoadingException));
}
Also used : JsonValue(javax.json.JsonValue) SchemaLoadingException(uk.gov.justice.services.core.json.SchemaLoadingException) MediaType(uk.gov.justice.services.core.mapping.MediaType) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 9 with MediaType

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

the class EnvelopeValidatorTest method shouldHandleAEnvelopeValidationException.

@Test
public void shouldHandleAEnvelopeValidationException() throws Exception {
    final EnvelopeValidationException envelopeValidationException = new EnvelopeValidationException("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(envelopeValidationException).when(jsonSchemaValidator).validate(payloadJson, actionName, mediaType);
    envelopeValidator.validate(jsonEnvelope, actionName, mediaType);
    verify(envelopeValidationExceptionHandler).handle(exceptionArgumentCaptor.capture());
    assertThat(exceptionArgumentCaptor.getValue(), is(envelopeValidationException));
}
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)

Example 10 with MediaType

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

the class EnvelopeValidatorTest method shouldDoNothingIfTheEnvelopePayloadIsNull.

@Test
public void shouldDoNothingIfTheEnvelopePayloadIsNull() throws Exception {
    final String actionName = "example.action-name";
    final Optional<MediaType> mediaType = of(new MediaType("application/vnd.example.action-name+json"));
    final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class);
    when(jsonEnvelope.payload()).thenReturn(NULL);
    envelopeValidator.validate(jsonEnvelope, actionName, mediaType);
    verifyZeroInteractions(objectMapper);
    verifyZeroInteractions(jsonSchemaValidator);
}
Also used : 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