Search in sources :

Example 1 with MediaTypes

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

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

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

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

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

Aggregations

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