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