Search in sources :

Example 11 with MediaType

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

the class RequestResponseEnvelopeValidatorTest method shouldValidateARequestEnvelope.

@Test
public void shouldValidateARequestEnvelope() throws Exception {
    final String actionName = "example.action-name";
    final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class);
    final MediaType mediaType = mock(MediaType.class);
    when(envelopeInspector.getActionNameFor(jsonEnvelope)).thenReturn(actionName);
    when(nameToMediaTypeConverter.convert(actionName)).thenReturn(mediaType);
    requestResponseEnvelopeValidator.validateRequest(jsonEnvelope);
    verify(envelopeValidator).validate(jsonEnvelope, actionName, of(mediaType));
}
Also used : MediaType(uk.gov.justice.services.core.mapping.MediaType) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 12 with MediaType

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

the class RequestResponseEnvelopeValidatorTest method shouldValidateAResponsetEnvelope.

@Test
public void shouldValidateAResponsetEnvelope() throws Exception {
    final String actionName = "example.action-name";
    final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class);
    final Optional<MediaType> mediaType = of(mock(MediaType.class));
    when(envelopeInspector.getActionNameFor(jsonEnvelope)).thenReturn(actionName);
    when(mediaTypeProvider.getResponseMediaType(actionName)).thenReturn(mediaType);
    requestResponseEnvelopeValidator.validateResponse(jsonEnvelope);
    verify(envelopeValidator).validate(jsonEnvelope, actionName, mediaType);
}
Also used : MediaType(uk.gov.justice.services.core.mapping.MediaType) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 13 with MediaType

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

the class MediaTypeProviderTest method shouldGetTheResponseMediaTypeForTheActionName.

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

Example 14 with MediaType

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

the class ActionNameToMediaTypesMapperClassBuilderTest method shouldGenerateAMediaTypeToNameMapper.

@Test
public void shouldGenerateAMediaTypeToNameMapper() throws Exception {
    final String baseUri = "http://localhost:8080/test-command-api/command/api/rest/test";
    final MediaType requestType_1 = new MediaType("application/vnd.requestMediaType_1+json");
    final MediaType requestType_2 = new MediaType("application/vnd.requestMediaType_2+json");
    final MediaType responseType_1 = new MediaType("application/vnd.responseMediaType_1+json");
    final ActionNameMapping mapping_1 = new ActionNameMapping("mapping_1", requestType_1, responseType_1);
    final ActionNameMapping mapping_2 = new ActionNameMapping("mapping_2", requestType_2, null);
    final TypeSpec typeSpec = actionNameToMediaTypesMapperClassBuilder.generate(asList(mapping_1, mapping_2), baseUri);
    final Class<?> nameToMediaTypesMapperClass = writeSourceFileAndCompile(PACKAGE_NAME, typeSpec);
    assertThat(nameToMediaTypesMapperClass.getSimpleName(), is("TestCommandApiActionNameToMediaTypesMapper"));
    assertThat(nameToMediaTypesMapperClass.getAnnotation(MediaTypesMapper.class), is(notNullValue()));
    final ActionNameToMediaTypesMapper actionNameToMediaTypesMapper = (ActionNameToMediaTypesMapper) nameToMediaTypesMapperClass.newInstance();
    final Map<String, MediaTypes> actionNameToMediaTypesMap = actionNameToMediaTypesMapper.getActionNameToMediaTypesMap();
    assertThat(actionNameToMediaTypesMap.size(), is(2));
    final MediaTypes mediaTypes_1 = actionNameToMediaTypesMap.get("mapping_1");
    assertThat(mediaTypes_1.getRequestMediaType(), is(of(requestType_1)));
    assertThat(mediaTypes_1.getResponseMediaType(), is(of(responseType_1)));
    final MediaTypes mediaTypes_2 = actionNameToMediaTypesMap.get("mapping_2");
    assertThat(mediaTypes_2.getRequestMediaType(), is(of(requestType_2)));
    assertThat(mediaTypes_2.getResponseMediaType(), is(empty()));
}
Also used : ActionNameToMediaTypesMapper(uk.gov.justice.services.core.mapping.ActionNameToMediaTypesMapper) MediaTypesMapper(uk.gov.justice.services.core.annotation.MediaTypesMapper) ActionNameToMediaTypesMapper(uk.gov.justice.services.core.mapping.ActionNameToMediaTypesMapper) MediaType(uk.gov.justice.services.core.mapping.MediaType) MediaTypes(uk.gov.justice.services.core.mapping.MediaTypes) TypeSpec(com.squareup.javapoet.TypeSpec) Test(org.junit.Test)

Example 15 with MediaType

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

the class RamlMediaTypeToSchemaIdGeneratorTest method shouldCreateMediaTypeToSchemaIdMapperForGivenRamlWithGet.

@Test
public void shouldCreateMediaTypeToSchemaIdMapperForGivenRamlWithGet() throws Exception {
    final String schemaId = "http://justice.gov.uk/test/schema.json";
    final MimeType mimeType_1 = createMimeTypeWith(MEDIA_TYPE_1.toString(), schemaId);
    new RamlMediaTypeToSchemaIdGenerator().generateMediaTypeToSchemaIdMapper(restRamlWithQueryApiDefaults().with(resource("/user").with(httpAction(GET).withResponseTypes(mimeType_1))).build(), configurationWithBasePackage(BASE_PACKAGE, outputFolder, new CommonGeneratorProperties()));
    final Class<?> schemaIdMapperClass = compiler.compiledClassOf(BASE_PACKAGE, "mapper", "WarnameMediaTypeToSchemaIdMapper");
    final MediaTypeToSchemaIdMapper instance = (MediaTypeToSchemaIdMapper) schemaIdMapperClass.newInstance();
    final Map<MediaType, String> mediaTypeToSchemaIdMap = instance.getMediaTypeToSchemaIdMap();
    assertThat(mediaTypeToSchemaIdMap.size(), is(1));
    assertThat(mediaTypeToSchemaIdMap.get(MEDIA_TYPE_1), is(schemaId));
}
Also used : 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) MimeType(org.raml.model.MimeType) 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