use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.
the class EnvelopeInspectorTest method shouldGetTheActionNameOfAnEnvelope.
@Test
public void shouldGetTheActionNameOfAnEnvelope() throws Exception {
final String actionName = "example.an-action";
final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class);
final Metadata metadata = mock(Metadata.class);
when(jsonEnvelope.metadata()).thenReturn(metadata);
when(metadata.name()).thenReturn(actionName);
assertThat(envelopeInspector.getActionNameFor(jsonEnvelope), is(actionName));
}
use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.
the class EnvelopeInspectorTest method shouldThrowValidationExceptionIfTheJsonEnvelopeContainsNoMetadata.
@Test
public void shouldThrowValidationExceptionIfTheJsonEnvelopeContainsNoMetadata() throws Exception {
final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class);
when(jsonEnvelope.metadata()).thenReturn(null);
try {
envelopeInspector.getMetadataFor(jsonEnvelope);
fail();
} catch (final EnvelopeValidationException expected) {
assertThat(expected.getMessage(), is("Metadata not set in the envelope."));
}
}
use of uk.gov.justice.services.messaging.JsonEnvelope 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.messaging.JsonEnvelope 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.messaging.JsonEnvelope 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