Search in sources :

Example 26 with MediaType

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

the class EnvelopeValidatorTest method shouldHandleAJsonProcessingException.

@Test
public void shouldHandleAJsonProcessingException() throws Exception {
    final JsonProcessingException jsonProcessingException = new JsonGenerationException("Ooops");
    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);
    final JsonValue payload = mock(JsonValue.class);
    when(jsonEnvelope.payload()).thenReturn(payload);
    when(objectMapper.writeValueAsString(payload)).thenThrow(jsonProcessingException);
    envelopeValidator.validate(jsonEnvelope, actionName, mediaType);
    verify(envelopeValidationExceptionHandler).handle(exceptionArgumentCaptor.capture());
    final EnvelopeValidationException envelopeValidationException = exceptionArgumentCaptor.getValue();
    assertThat(envelopeValidationException.getMessage(), is("Error serialising json."));
    assertThat(envelopeValidationException.getCause(), is(jsonProcessingException));
    verifyZeroInteractions(jsonSchemaValidator);
}
Also used : JsonValue(javax.json.JsonValue) MediaType(uk.gov.justice.services.core.mapping.MediaType) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test)

Example 27 with MediaType

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

the class MediaTypeProviderTest method shouldGetTheRequestMediaTypeForTheActionName.

@Test
public void shouldGetTheRequestMediaTypeForTheActionName() throws Exception {
    final String actionName = "example.add-recipe";
    final MediaType requestMediaType = new MediaType("application/vnd.example.add-recipe+json");
    final MediaTypes mediaTypes = mock(MediaTypes.class);
    when(mediaTypesMappingCache.mediaTypesFor(actionName)).thenReturn(of(mediaTypes));
    when(mediaTypes.getRequestMediaType()).thenReturn(of(requestMediaType));
    assertThat(mediaTypeProvider.getRequestMediaType(actionName), is(of(requestMediaType)));
}
Also used : MediaType(uk.gov.justice.services.core.mapping.MediaType) MediaTypes(uk.gov.justice.services.core.mapping.MediaTypes) Test(org.junit.Test)

Example 28 with MediaType

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

the class ActionNameToMediaTypesParserTest method shouldReturnActionNameMappingsForPostResourceSameActionMappedToTwoMediaTypes.

@Test
public void shouldReturnActionNameMappingsForPostResourceSameActionMappedToTwoMediaTypes() throws Exception {
    final String name_1 = "contextC.someAction";
    final String requestType_1 = "application/vnd.ctx.command.somemediatype1+json";
    final String requestType_2 = "application/vnd.ctx.command.somemediatype2+json";
    final String mappingDescription = mappingDescriptionWith(mapping().withName(name_1).withRequestType(requestType_1), mapping().withName(name_1).withRequestType(requestType_2)).build();
    final Raml raml = restRamlWithCommandApiDefaults().with(resource("/case").with(httpActionWithDefaultMapping(POST).withDescription(mappingDescription).withMediaTypeWithDefaultSchema(requestType_1).withMediaTypeWithDefaultSchema(requestType_2))).build();
    final List<ActionNameMapping> actionNameMappings = actionNameToMediaTypesParser.parseFrom(raml);
    assertThat(actionNameMappings.size(), is(2));
    assertThat(actionNameMappings, hasItems(new ActionNameMapping(name_1, new MediaType(requestType_1), null), new ActionNameMapping(name_1, new MediaType(requestType_2), null)));
}
Also used : Raml(org.raml.model.Raml) MediaType(uk.gov.justice.services.core.mapping.MediaType) Test(org.junit.Test)

Example 29 with MediaType

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

the class ActionNameToMediaTypesParserTest method shouldReturnActionNameMappingsForPostPutPatchAndDeleteResources.

@Test
public void shouldReturnActionNameMappingsForPostPutPatchAndDeleteResources() throws Exception {
    final String name_1 = "contextA.someAction-1";
    final String requestType_1 = "application/vnd.ctx.query.request-1+json";
    final String responseType_1 = "application/vnd.ctx.query.response-1+json";
    final String name_2 = "contextA.someAction-2";
    final String requestType_2 = "application/vnd.ctx.query.request-2+json";
    final String responseType_2 = "application/vnd.ctx.query.response-2+json";
    final String name_3 = "contextA.someAction-3";
    final String requestType_3 = "application/vnd.ctx.query.request-3+json";
    final String responseType_3 = "application/vnd.ctx.query.response-3+json";
    final String name_4 = "contextA.someAction-4";
    final String requestType_4 = "application/vnd.ctx.query.request-4+json";
    final String responseType_4 = "application/vnd.ctx.query.response-4+json";
    final String mappingDescription_1 = mappingDescriptionWith(mapping().withName(name_1).withRequestType(requestType_1).withResponseType(responseType_1)).build();
    final String mappingDescription_2 = mappingDescriptionWith(mapping().withName(name_2).withRequestType(requestType_2).withResponseType(responseType_2)).build();
    final String mappingDescription_3 = mappingDescriptionWith(mapping().withName(name_3).withRequestType(requestType_3).withResponseType(responseType_3)).build();
    final String mappingDescription_4 = mappingDescriptionWith(mapping().withName(name_4).withRequestType(requestType_4).withResponseType(responseType_4)).build();
    final Raml raml = restRamlWithQueryApiDefaults().with(resource("/user").with(httpActionWithDefaultMapping(POST).withDescription(mappingDescription_1).withMediaTypeWithDefaultSchema(requestType_1).withResponseTypes(responseType_1)).with(httpActionWithDefaultMapping(PUT).withDescription(mappingDescription_2).withMediaTypeWithDefaultSchema(requestType_2).withResponseTypes(responseType_3)).with(httpActionWithDefaultMapping(PATCH).withDescription(mappingDescription_3).withMediaTypeWithDefaultSchema(requestType_3).withResponseTypes(responseType_4)).with(httpActionWithDefaultMapping(DELETE).withDescription(mappingDescription_4).withMediaTypeWithDefaultSchema(requestType_4).withResponseTypes(responseType_4))).build();
    final List<ActionNameMapping> actionNameMappings = actionNameToMediaTypesParser.parseFrom(raml);
    assertThat(actionNameMappings.size(), is(4));
    assertThat(actionNameMappings, hasItems(new ActionNameMapping(name_1, new MediaType(requestType_1), new MediaType(responseType_1)), new ActionNameMapping(name_2, new MediaType(requestType_2), new MediaType(responseType_2)), new ActionNameMapping(name_3, new MediaType(requestType_3), new MediaType(responseType_3)), new ActionNameMapping(name_4, new MediaType(requestType_4), new MediaType(responseType_4))));
}
Also used : Raml(org.raml.model.Raml) MediaType(uk.gov.justice.services.core.mapping.MediaType) Test(org.junit.Test)

Example 30 with MediaType

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

the class MediaTypeToSchemaIdTest method shouldHaveCorrectEqualsAndHashcode.

@Test
public void shouldHaveCorrectEqualsAndHashcode() {
    final MediaType mediaType_1 = new MediaType("application/vnd.test.command.test+json");
    final MediaType mediaType_2 = new MediaType("application/vnd.other.command.test+json");
    final String schemaId_1 = "schemaId";
    final String schemaId_2 = "otherId";
    final MediaTypeToSchemaId mediaTypeToSchemaId_1 = new MediaTypeToSchemaId(mediaType_1, schemaId_1);
    final MediaTypeToSchemaId mediaTypeToSchemaId_2 = new MediaTypeToSchemaId(mediaType_1, schemaId_1);
    final MediaTypeToSchemaId mediaTypeToSchemaId_3 = new MediaTypeToSchemaId(mediaType_1, schemaId_2);
    final MediaTypeToSchemaId mediaTypeToSchemaId_4 = new MediaTypeToSchemaId(mediaType_2, schemaId_1);
    final MediaTypeToSchemaId mediaTypeToSchemaId_5 = new MediaTypeToSchemaId(mediaType_2, schemaId_2);
    new EqualsTester().addEqualityGroup(mediaTypeToSchemaId_1, mediaTypeToSchemaId_2).addEqualityGroup(mediaTypeToSchemaId_3).addEqualityGroup(mediaTypeToSchemaId_4).addEqualityGroup(mediaTypeToSchemaId_5).testEquals();
}
Also used : EqualsTester(com.google.common.testing.EqualsTester) MediaType(uk.gov.justice.services.core.mapping.MediaType) 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