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))));
}
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));
}
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"));
}
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));
}
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))));
}
Aggregations