Search in sources :

Example 51 with JsonEnvelope

use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.

the class DefaultContextPayloadTest method shouldCreateContextPayloadWithEnvelope.

@Test
public void shouldCreateContextPayloadWithEnvelope() throws Exception {
    final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class);
    final ContextPayload contextPayload = contextPayloadWith(jsonEnvelope);
    assertThat(contextPayload.getEnvelope(), is(Optional.of(jsonEnvelope)));
}
Also used : JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 52 with JsonEnvelope

use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.

the class DefaultContextPayloadTest method shouldCopyAllParametersAndAddEnvelope.

@Test
public void shouldCopyAllParametersAndAddEnvelope() throws Exception {
    final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class);
    final Object parameter = mock(Object.class);
    final ContextPayload initialContext = contextPayloadWithNoEnvelope();
    initialContext.setParameter("test", parameter);
    final ContextPayload result = copyWithEnvelope(initialContext, jsonEnvelope);
    assertThat(result.getEnvelope(), is(Optional.of(jsonEnvelope)));
    assertThat(result.getParameter("test"), is(Optional.of(parameter)));
}
Also used : JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 53 with JsonEnvelope

use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.

the class AccessControlFailureMessageGeneratorTest method shouldGenerateAnErrorMessageWithUserIdEnvelopeNameAndFailureReason.

@Test
public void shouldGenerateAnErrorMessageWithUserIdEnvelopeNameAndFailureReason() throws Exception {
    final String jsonEnvelopeString = "the: jsonEnvelope";
    final String reason = "reason";
    final AccessControlViolation accessControlViolation = new AccessControlViolation(reason);
    final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class);
    when(jsonEnvelope.toString()).thenReturn(jsonEnvelopeString);
    final String errorMessage = accessControlFailureMessageGenerator.errorMessageFrom(jsonEnvelope, accessControlViolation);
    assertThat(errorMessage, is("Access Control failed for json envelope 'the: jsonEnvelope'. Reason: reason"));
}
Also used : JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 54 with JsonEnvelope

use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.

the class SimpleAuditClientTest method shouldPrependTheAppNameToTheEnvelopeJsonAndLog.

@Test
public void shouldPrependTheAppNameToTheEnvelopeJsonAndLog() throws Exception {
    final String serviceContextName = "the-service-context-name";
    final String propertyValue = "value";
    final String envelopeJson = new JSONObject().put("propertyName", propertyValue).toString();
    final JsonEnvelope envelope = mock(JsonEnvelope.class);
    when(envelope.toString()).thenReturn(envelopeJson);
    when(serviceContextNameProvider.getServiceContextName()).thenReturn(serviceContextName);
    simpleAuditClient.auditEntry(envelope, COMPONENT);
    final ArgumentCaptor<String> argumentCaptor = forClass(String.class);
    verify(logger).info(argumentCaptor.capture());
    final String json = argumentCaptor.getValue();
    with(json).assertEquals("serviceContext", serviceContextName).assertEquals("component", COMPONENT).assertEquals("envelope.propertyName", propertyValue);
}
Also used : JSONObject(org.json.JSONObject) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 55 with JsonEnvelope

use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.

the class EventsServiceTest method shouldReturnFirstEvents.

@Test
public void shouldReturnFirstEvents() throws Exception {
    final UUID streamId = randomUUID();
    final ZonedDateTime event1CreatedAt = now();
    final ZonedDateTime event2CreatedAt = now();
    final long pageSize = 2L;
    final JsonObject payload1 = createObjectBuilder().add("field1", "value1").build();
    final JsonObject payload2 = createObjectBuilder().add("field2", "value2").build();
    final JsonEnvelope event1 = envelope().withPayloadOf("value1", "field1").with(metadataOf(streamId, "Test Name1").withVersion(1L).withStreamId(streamId).createdAt(event1CreatedAt)).build();
    final JsonEnvelope event2 = envelope().withPayloadOf("value2", "field2").with(metadataOf(streamId, "Test Name2").withVersion(2L).withStreamId(streamId).createdAt(event2CreatedAt)).build();
    final EventStream eventStream = mock(EventStream.class);
    when(eventSource.getStreamById(streamId)).thenReturn(eventStream);
    when(eventStream.size()).thenReturn(2L);
    when(eventStream.readFrom(first().getPosition())).thenReturn(Stream.of(event1, event2));
    final List<EventEntry> eventEntries = service.events(streamId, first(), FORWARD, pageSize);
    assertThat(eventEntries, hasSize(2));
    assertThat(eventEntries.get(0).getStreamId(), is(streamId.toString()));
    assertThat(eventEntries.get(0).getName(), is("Test Name2"));
    assertThat(eventEntries.get(0).getPosition(), is(2L));
    assertThat(eventEntries.get(0).getCreatedAt(), is(ZonedDateTimes.toString(event2CreatedAt)));
    assertThat(eventEntries.get(0).getPayload(), is(notNullValue()));
    assertThat(eventEntries.get(0).getPayload(), is(payload2));
    assertThat(eventEntries.get(1).getStreamId(), is(streamId.toString()));
    assertThat(eventEntries.get(1).getName(), is("Test Name1"));
    assertThat(eventEntries.get(1).getPosition(), is(1L));
    assertThat(eventEntries.get(1).getPayload(), is(payload1));
    assertThat(eventEntries.get(1).getCreatedAt(), is(ZonedDateTimes.toString(event1CreatedAt)));
}
Also used : ZonedDateTime(java.time.ZonedDateTime) EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) JsonObject(javax.json.JsonObject) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) 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