Search in sources :

Example 1 with MediaType

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

the class ActionNameToMediaTypesParserTest method shouldReturnActionNameMappingsForGetResource.

@Test
public void shouldReturnActionNameMappingsForGetResource() throws Exception {
    final String name_1 = "contextA.someAction";
    final String responseType_1 = "application/vnd.ctx.query.somemediatype1+json";
    final String responseType_2 = "application/vnd.ctx.query.somemediatype2+json";
    final String name_3 = "contextA.someOtherAction";
    final String responseType_3 = "application/vnd.ctx.query.somemediatype3+json";
    final String mappingDescription = mappingDescriptionWith(mapping().withName(name_1).withResponseType(responseType_1), mapping().withName(name_1).withResponseType(responseType_2), mapping().withName(name_3).withResponseType(responseType_3)).build();
    final Raml raml = restRamlWithQueryApiDefaults().with(resource("/user").with(httpActionWithDefaultMapping(GET).withDescription(mappingDescription).withResponseTypes(responseType_1, responseType_2, responseType_3))).build();
    final List<ActionMapping> actionMappings = asList(new ActionMapping(name_1, null, responseType_1), new ActionMapping(name_1, null, responseType_2), new ActionMapping(name_3, null, responseType_3));
    final List<ActionNameMapping> actionNameMappings = actionNameToMediaTypesParser.parseFrom(raml);
    assertThat(actionNameMappings.size(), is(3));
    assertThat(actionNameMappings, hasItems(new ActionNameMapping(name_1, null, new MediaType(responseType_1)), new ActionNameMapping(name_1, null, new MediaType(responseType_2)), new ActionNameMapping(name_3, null, new MediaType(responseType_3))));
}
Also used : Raml(org.raml.model.Raml) MediaType(uk.gov.justice.services.core.mapping.MediaType) Test(org.junit.Test)

Example 2 with MediaType

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

the class MediaTypeToSchemaIdTest method shouldCreateMediaTypeToSchemaId.

@Test
public void shouldCreateMediaTypeToSchemaId() {
    final MediaType mediaType = new MediaType("application/vnd.test.command.test+json");
    final String schemaId = "schemaId";
    final MediaTypeToSchemaId mediaTypeToSchemaId = new MediaTypeToSchemaId(mediaType, schemaId);
    assertThat(mediaTypeToSchemaId.getMediaType(), is(mediaType));
    assertThat(mediaTypeToSchemaId.getSchemaId(), is(schemaId));
}
Also used : MediaType(uk.gov.justice.services.core.mapping.MediaType) Test(org.junit.Test)

Example 3 with MediaType

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

the class RamlMediaTypeToSchemaIdMapperClassBuilderTest method shouldGenerateACorrectMapperClass.

@SuppressWarnings("unchecked")
@Test
public void shouldGenerateACorrectMapperClass() throws Exception {
    final String baseUri = "http://localhost:8080/people-command-api/command/api/rest/people";
    final String simpleName = "PeopleCommandApiMediaTypeToSchemaIdMapper";
    final String packageName = "uk.gov.justice.generation.mapper.example.generated.test";
    final MediaType mediaType_1 = new MediaType("application/vnd.ctx.command.mediaType_1+json");
    final MediaType mediaType_2 = new MediaType("application/vnd.ctx.command.mediaType_2+json");
    final MediaType mediaType_3 = new MediaType("application/vnd.ctx.command.mediaType_3+json");
    final MediaType mediaType_4 = new MediaType("application/vnd.ctx.command.mediaType_4+json");
    final List<MediaTypeToSchemaId> mediaTypeToSchemaIds = asList(new MediaTypeToSchemaId(mediaType_1, "schemaId_1"), new MediaTypeToSchemaId(mediaType_2, "schemaId_2"), new MediaTypeToSchemaId(mediaType_3, "schemaId_3"), new MediaTypeToSchemaId(mediaType_4, "schemaId_4"));
    when(schemaMappingClassNameGenerator.createMappingClassNameFrom(baseUri, MediaTypeToSchemaIdMapper.class)).thenReturn(simpleName);
    final TypeSpec typeSpec = ramlMediaTypeToSchemaIdMapperClassBuilder.typeSpecWith(baseUri, mediaTypeToSchemaIds);
    final Class<?> mapperClass = writeSourceFileAndCompile(packageName, typeSpec);
    assertThat(mapperClass.getAnnotation(SchemaIdMapper.class), is(notNullValue()));
    assertThat(mapperClass.getPackage().getName(), is(packageName));
    assertThat(mapperClass.getSimpleName(), is(simpleName));
    final Constructor<?> constructor = mapperClass.getConstructor();
    final Object instance = constructor.newInstance();
    final Method getSchemaIds = mapperClass.getMethod("getMediaTypeToSchemaIdMap");
    final Map<MediaType, String> schemaIds = (Map<MediaType, String>) getSchemaIds.invoke(instance);
    assertThat(schemaIds.get(mediaType_1), is("schemaId_1"));
    assertThat(schemaIds.get(mediaType_2), is("schemaId_2"));
    assertThat(schemaIds.get(mediaType_3), is("schemaId_3"));
    assertThat(schemaIds.get(mediaType_4), is("schemaId_4"));
}
Also used : MediaType(uk.gov.justice.services.core.mapping.MediaType) Method(java.lang.reflect.Method) Map(java.util.Map) TypeSpec(com.squareup.javapoet.TypeSpec) MediaTypeToSchemaIdMapper(uk.gov.justice.services.core.mapping.MediaTypeToSchemaIdMapper) SchemaIdMapper(uk.gov.justice.services.core.annotation.SchemaIdMapper) Test(org.junit.Test)

Example 4 with MediaType

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

the class SubscriptionMediaTypeToSchemaIdMapperClassBuilderTest method shouldGenerateACorrectMapperClass.

@SuppressWarnings("unchecked")
@Test
public void shouldGenerateACorrectMapperClass() throws Exception {
    final String baseUri = "http://localhost:8080/people-command-api/command/api/rest/people";
    final String contextName = "my-context";
    final String componentName = "EVENT_LISTENER";
    final String simpleName = "PeopleCommandApiMediaTypeToSchemaIdMapper";
    final String packageName = "uk.gov.justice.generation.mapper.example.generated.test";
    final String eventName_1 = "ctx.command.mediaType_1";
    final String eventName_2 = "ctx.command.mediaType_2";
    final String eventName_3 = "ctx.command.mediaType_3";
    final String eventName_4 = "ctx.command.mediaType_4";
    final String schemaId_1 = "schemaId_1";
    final String schemaId_2 = "schemaId_2";
    final String schemaId_3 = "schemaId_3";
    final String schemaId_4 = "schemaId_4";
    final MediaType mediaType_1 = new MediaType("application/vnd." + eventName_1 + "+json");
    final MediaType mediaType_2 = new MediaType("application/vnd." + eventName_2 + "+json");
    final MediaType mediaType_3 = new MediaType("application/vnd." + eventName_3 + "+json");
    final MediaType mediaType_4 = new MediaType("application/vnd." + eventName_4 + "+json");
    final List<Event> events = asList(new Event(eventName_1, schemaId_1), new Event(eventName_2, schemaId_2), new Event(eventName_3, schemaId_3), new Event(eventName_4, schemaId_4));
    when(subscriptionSchemaMappingClassNameGenerator.createMappingClassNameFrom(contextName, componentName, MediaTypeToSchemaIdMapper.class)).thenReturn(simpleName);
    final TypeSpec typeSpec = subscriptionMediaTypeToSchemaIdMapperClassBuilder.typeSpecWith(contextName, componentName, events);
    final Class<?> mapperClass = writeSourceFileAndCompile(packageName, typeSpec);
    assertThat(mapperClass.getAnnotation(SchemaIdMapper.class), is(notNullValue()));
    assertThat(mapperClass.getPackage().getName(), is(packageName));
    assertThat(mapperClass.getSimpleName(), is(simpleName));
    final Constructor<?> constructor = mapperClass.getConstructor();
    final Object instance = constructor.newInstance();
    final Method getSchemaIds = mapperClass.getMethod("getMediaTypeToSchemaIdMap");
    final Map<MediaType, String> schemaIds = (Map<MediaType, String>) getSchemaIds.invoke(instance);
    assertThat(schemaIds.get(mediaType_1), is(schemaId_1));
    assertThat(schemaIds.get(mediaType_2), is(schemaId_2));
    assertThat(schemaIds.get(mediaType_3), is(schemaId_3));
    assertThat(schemaIds.get(mediaType_4), is(schemaId_4));
}
Also used : MediaType(uk.gov.justice.services.core.mapping.MediaType) Event(uk.gov.justice.subscription.domain.Event) Method(java.lang.reflect.Method) Map(java.util.Map) TypeSpec(com.squareup.javapoet.TypeSpec) SchemaIdMapper(uk.gov.justice.services.core.annotation.SchemaIdMapper) MediaTypeToSchemaIdMapper(uk.gov.justice.services.core.mapping.MediaTypeToSchemaIdMapper) Test(org.junit.Test)

Example 5 with MediaType

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

the class ActionNameToMediaTypesGeneratorTest method shouldCreateMediaTypeToSchemaIdMapperForGivenRamlWithPost.

@Test
public void shouldCreateMediaTypeToSchemaIdMapperForGivenRamlWithPost() throws Exception {
    final String actionName = "contextA.someAction";
    final String requestType = "application/vnd.ctx.query.somemediatype1+json";
    final String responseType = "application/vnd.ctx.query.somemediatype2+json";
    final String description = mappingDescriptionWith(mapping().withName(actionName).withRequestType(requestType).withResponseType(responseType)).build();
    new ActionNameToMediaTypesGenerator().generateActionNameToMediaTypes(restRamlWithQueryApiDefaults().with(resource("/user").with(httpAction(POST).withDescription(description).withMediaTypeWithDefaultSchema(requestType).withResponseTypes(responseType))).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(1));
    assertThat(actionNameToMediaTypesMap.get(actionName).getRequestMediaType(), is(Optional.of(new MediaType(requestType))));
    assertThat(actionNameToMediaTypesMap.get(actionName).getResponseMediaType(), is(Optional.of(new MediaType(responseType))));
}
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)

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