use of uk.gov.justice.services.core.mapping.MediaType 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()));
}
use of uk.gov.justice.services.core.mapping.MediaType in project microservice_framework by CJSCommonPlatform.
the class SchemaCatalogAwareJsonSchemaValidatorTest method shouldFallBackToFileBasedSchemaValidationIfNoSchemaFoundInTheCatalogCache.
@Test
public void shouldFallBackToFileBasedSchemaValidationIfNoSchemaFoundInTheCatalogCache() throws Exception {
final String uri = "http://space.time.gov.uk/mind/command/api/initiate-warp-speed.json";
final String actionName = "command.api.initiate-warp-speed";
final Optional<String> schemaId = of(uri);
final MediaType mediaType = new MediaType("application", "vnd.mind.command.initiate-warp-speed+json");
final String envelopeJson = "{\"envelope\": \"json\"}";
when(schemaIdMappingCache.schemaIdFor(mediaType)).thenReturn(schemaId);
when(schemaCatalogService.findSchema(uri)).thenReturn(empty());
schemaCatalogAwareJsonSchemaValidator.validate(envelopeJson, actionName, of(mediaType));
verify(fileBasedJsonSchemaValidator).validateWithoutSchemaCatalog(envelopeJson, actionName);
verifyZeroInteractions(logger);
verifyZeroInteractions(payloadExtractor);
}
use of uk.gov.justice.services.core.mapping.MediaType in project microservice_framework by CJSCommonPlatform.
the class EnvelopeValidatorTest method shouldHandleASchemaLoadingException.
@Test
public void shouldHandleASchemaLoadingException() throws Exception {
final SchemaLoadingException schemaLoadingException = new SchemaLoadingException("Ooops");
final String actionName = "exaple.action-name";
final String payloadJson = "{\"some\": \"json\"}";
final Optional<MediaType> mediaType = of(new MediaType("application/vnd.example.action-name+json"));
final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class);
final JsonValue payload = mock(JsonValue.class);
when(jsonEnvelope.payload()).thenReturn(payload);
when(objectMapper.writeValueAsString(payload)).thenReturn(payloadJson);
doThrow(schemaLoadingException).when(jsonSchemaValidator).validate(payloadJson, actionName, mediaType);
envelopeValidator.validate(jsonEnvelope, actionName, mediaType);
verify(envelopeValidationExceptionHandler).handle(exceptionArgumentCaptor.capture());
final EnvelopeValidationException envelopeValidationException = exceptionArgumentCaptor.getValue();
assertThat(envelopeValidationException.getMessage(), is("Could not load json schema that matches message type exaple.action-name."));
assertThat(envelopeValidationException.getCause(), is(schemaLoadingException));
}
use of uk.gov.justice.services.core.mapping.MediaType in project microservice_framework by CJSCommonPlatform.
the class EnvelopeValidatorTest method shouldHandleAEnvelopeValidationException.
@Test
public void shouldHandleAEnvelopeValidationException() throws Exception {
final EnvelopeValidationException envelopeValidationException = new EnvelopeValidationException("Ooops");
final String actionName = "exaple.action-name";
final String payloadJson = "{\"some\": \"json\"}";
final Optional<MediaType> mediaType = of(new MediaType("application/vnd.example.action-name+json"));
final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class);
final JsonValue payload = mock(JsonValue.class);
when(jsonEnvelope.payload()).thenReturn(payload);
when(objectMapper.writeValueAsString(payload)).thenReturn(payloadJson);
when(jsonEnvelope.toObfuscatedDebugString()).thenReturn("debug-json");
doThrow(envelopeValidationException).when(jsonSchemaValidator).validate(payloadJson, actionName, mediaType);
envelopeValidator.validate(jsonEnvelope, actionName, mediaType);
verify(envelopeValidationExceptionHandler).handle(exceptionArgumentCaptor.capture());
assertThat(exceptionArgumentCaptor.getValue(), is(envelopeValidationException));
}
use of uk.gov.justice.services.core.mapping.MediaType in project microservice_framework by CJSCommonPlatform.
the class EnvelopeValidatorTest method shouldDoNothingIfTheEnvelopePayloadIsNull.
@Test
public void shouldDoNothingIfTheEnvelopePayloadIsNull() throws Exception {
final String actionName = "example.action-name";
final Optional<MediaType> mediaType = of(new MediaType("application/vnd.example.action-name+json"));
final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class);
when(jsonEnvelope.payload()).thenReturn(NULL);
envelopeValidator.validate(jsonEnvelope, actionName, mediaType);
verifyZeroInteractions(objectMapper);
verifyZeroInteractions(jsonSchemaValidator);
}
Aggregations