Search in sources :

Example 41 with JsonEnvelope

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));
}
Also used : Metadata(uk.gov.justice.services.messaging.Metadata) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 42 with JsonEnvelope

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."));
    }
}
Also used : JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 43 with JsonEnvelope

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));
}
Also used : JsonValue(javax.json.JsonValue) SchemaLoadingException(uk.gov.justice.services.core.json.SchemaLoadingException) MediaType(uk.gov.justice.services.core.mapping.MediaType) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 44 with JsonEnvelope

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));
}
Also used : JsonValue(javax.json.JsonValue) MediaType(uk.gov.justice.services.core.mapping.MediaType) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 45 with JsonEnvelope

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);
}
Also used : MediaType(uk.gov.justice.services.core.mapping.MediaType) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Aggregations

JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)276 Test (org.junit.Test)249 UUID (java.util.UUID)69 UUID.randomUUID (java.util.UUID.randomUUID)64 JsonObject (javax.json.JsonObject)30 JsonValue (javax.json.JsonValue)26 InterceptorContext (uk.gov.justice.services.core.interceptor.InterceptorContext)24 Metadata (uk.gov.justice.services.messaging.Metadata)18 Method (java.lang.reflect.Method)17 Function (java.util.function.Function)14 StreamBufferEvent (uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferEvent)10 EventStream (uk.gov.justice.services.eventsourcing.source.core.EventStream)9 JsonObjects.getJsonObject (uk.gov.justice.services.messaging.JsonObjects.getJsonObject)9 EndpointDefinition (uk.gov.justice.services.clients.core.EndpointDefinition)8 MediaType (uk.gov.justice.services.core.mapping.MediaType)8 StreamStatus (uk.gov.justice.services.event.buffer.core.repository.streamstatus.StreamStatus)7 ZonedDateTime (java.time.ZonedDateTime)6 Collection (java.util.Collection)6 Optional (java.util.Optional)6 HttpHeaders (javax.ws.rs.core.HttpHeaders)6