Search in sources :

Example 1 with ActionNameToMediaTypesMapper

use of uk.gov.justice.services.core.mapping.ActionNameToMediaTypesMapper 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)

Example 2 with ActionNameToMediaTypesMapper

use of uk.gov.justice.services.core.mapping.ActionNameToMediaTypesMapper 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 3 with ActionNameToMediaTypesMapper

use of uk.gov.justice.services.core.mapping.ActionNameToMediaTypesMapper 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 4 with ActionNameToMediaTypesMapper

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

the class MediaTypesMappingCacheMock method initialize.

public MediaTypesMappingCacheMock initialize(final String mapperPackage) {
    final Reflections reflections = new Reflections(mapperPackage);
    final Set<Class<? extends ActionNameToMediaTypesMapper>> classes = reflections.getSubTypesOf(ActionNameToMediaTypesMapper.class);
    final List<ActionNameToMediaTypesMapper> actionNameToMediaTypesMappers = classes.stream().map(this::instantiate).collect(toList());
    actionNameToMediaTypesMappers.forEach(mapper -> nameToMediaTypes.putAll(mapper.getActionNameToMediaTypesMap()));
    return this;
}
Also used : ActionNameToMediaTypesMapper(uk.gov.justice.services.core.mapping.ActionNameToMediaTypesMapper) Reflections(org.reflections.Reflections)

Example 5 with ActionNameToMediaTypesMapper

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

the class RestAdapterGenerator_ActionNameToMediaTypesMapperTest method shouldGenerateMediaTypeToSchemaIdMapper.

@Test
public void shouldGenerateMediaTypeToSchemaIdMapper() throws Exception {
    final String actionName = "contextA.someAction";
    generator.run(restRamlWithQueryApiDefaults().with(resource("/user").with(httpAction(GET).with(mapping().withName(actionName).withResponseType(MEDIA_TYPE_1.toString())).withResponseTypes(MEDIA_TYPE_1.toString()))).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).getResponseMediaType(), is(Optional.of(MEDIA_TYPE_1)));
    assertThat(actionNameToMediaTypesMap.get(actionName).getRequestMediaType(), is(Optional.empty()));
}
Also used : ActionNameToMediaTypesMapper(uk.gov.justice.services.core.mapping.ActionNameToMediaTypesMapper) CommonGeneratorProperties(uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties) MediaTypes(uk.gov.justice.services.core.mapping.MediaTypes) Test(org.junit.Test)

Aggregations

ActionNameToMediaTypesMapper (uk.gov.justice.services.core.mapping.ActionNameToMediaTypesMapper)5 Test (org.junit.Test)4 MediaTypes (uk.gov.justice.services.core.mapping.MediaTypes)4 MediaType (uk.gov.justice.services.core.mapping.MediaType)3 CommonGeneratorProperties (uk.gov.justice.services.generators.commons.config.CommonGeneratorProperties)3 TypeSpec (com.squareup.javapoet.TypeSpec)1 Reflections (org.reflections.Reflections)1 MediaTypesMapper (uk.gov.justice.services.core.annotation.MediaTypesMapper)1