Search in sources :

Example 56 with JsonEnvelope

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

the class EventsServiceTest method shouldReturnNextEvents.

@Test
public void shouldReturnNextEvents() throws Exception {
    final UUID streamId = randomUUID();
    final UUID firstEventId = randomUUID();
    final UUID secondEventId = randomUUID();
    final ZonedDateTime event3CreatedAt = now();
    final ZonedDateTime event4CreatedAt = now();
    final long pageSize = 2L;
    final JsonObject payload4 = createObjectBuilder().add("field4", "value4").build();
    final JsonObject payload3 = createObjectBuilder().add("field3", "value3").build();
    final JsonEnvelope event4 = envelope().withPayloadOf("value4", "field4").with(metadataOf(secondEventId, "Test Name4").withVersion(4L).withStreamId(streamId).createdAt(event4CreatedAt)).build();
    final JsonEnvelope event3 = envelope().withPayloadOf("value3", "field3").with(metadataOf(firstEventId, "Test Name3").withVersion(3L).withStreamId(streamId).createdAt(event3CreatedAt)).build();
    final EventStream eventStream = mock(EventStream.class);
    final long positionId = 3L;
    when(eventSource.getStreamById(streamId)).thenReturn(eventStream);
    when(eventStream.readFrom(positionId)).thenReturn(Stream.of(event3, event4));
    final List<EventEntry> eventEntries = service.events(streamId, position(positionId), FORWARD, pageSize);
    assertThat(eventEntries, hasSize(2));
    assertThat(eventEntries.get(0).getStreamId(), is(streamId.toString()));
    assertThat(eventEntries.get(0).getName(), is("Test Name4"));
    assertThat(eventEntries.get(0).getPosition(), is(4L));
    assertThat(eventEntries.get(0).getCreatedAt(), is(ZonedDateTimes.toString(event4CreatedAt)));
    assertThat(eventEntries.get(0).getPayload(), is(notNullValue()));
    assertThat(eventEntries.get(0).getPayload(), is(payload4));
    assertThat(eventEntries.get(1).getStreamId(), is(streamId.toString()));
    assertThat(eventEntries.get(1).getName(), is("Test Name3"));
    assertThat(eventEntries.get(1).getPosition(), is(3L));
    assertThat(eventEntries.get(1).getPayload(), is(payload3));
    assertThat(eventEntries.get(1).getCreatedAt(), is(ZonedDateTimes.toString(event3CreatedAt)));
}
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)

Example 57 with JsonEnvelope

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

the class EventsServiceTest method shouldReturnPreviousEvents.

@Test
public void shouldReturnPreviousEvents() throws Exception {
    final UUID streamId = randomUUID();
    final UUID firstEventId = randomUUID();
    final UUID secondEventId = randomUUID();
    final ZonedDateTime event2CreatedAt = now();
    final ZonedDateTime event3CreatedAt = now();
    final ZonedDateTime event4CreatedAt = now();
    final long pageSize = 2L;
    final JsonObject payload3 = createObjectBuilder().add("field3", "value3").build();
    final JsonObject payload2 = createObjectBuilder().add("field2", "value2").build();
    final JsonEnvelope event2 = envelope().withPayloadOf("value2", "field2").with(metadataOf(secondEventId, "Test Name2").withVersion(2L).withStreamId(streamId).createdAt(event2CreatedAt)).build();
    final JsonEnvelope event3 = envelope().withPayloadOf("value3", "field3").with(metadataOf(firstEventId, "Test Name3").withVersion(3L).withStreamId(streamId).createdAt(event3CreatedAt)).build();
    final JsonEnvelope event4 = envelope().withPayloadOf("value4", "field4").with(metadataOf(secondEventId, "Test Name4").withVersion(4L).withStreamId(streamId).createdAt(event4CreatedAt)).build();
    final EventStream eventStream = mock(EventStream.class);
    when(eventSource.getStreamById(streamId)).thenReturn(eventStream);
    when(eventStream.readFrom(2L)).thenReturn(Stream.of(event2, event3, event4));
    final List<EventEntry> eventEntries = service.events(streamId, position(3L), BACKWARD, pageSize);
    assertThat(eventEntries, hasSize(2));
    assertThat(eventEntries.get(0).getStreamId(), is(streamId.toString()));
    assertThat(eventEntries.get(0).getName(), is("Test Name3"));
    assertThat(eventEntries.get(0).getPosition(), is(3L));
    assertThat(eventEntries.get(0).getCreatedAt(), is(ZonedDateTimes.toString(event3CreatedAt)));
    assertThat(eventEntries.get(0).getPayload(), is(notNullValue()));
    assertThat(eventEntries.get(0).getPayload(), is(payload3));
    assertThat(eventEntries.get(1).getStreamId(), is(streamId.toString()));
    assertThat(eventEntries.get(1).getName(), is("Test Name2"));
    assertThat(eventEntries.get(1).getPosition(), is(2L));
    assertThat(eventEntries.get(1).getPayload(), is(payload2));
    assertThat(eventEntries.get(1).getCreatedAt(), is(ZonedDateTimes.toString(event2CreatedAt)));
}
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)

Example 58 with JsonEnvelope

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

the class EventsServiceTest method shouldReturnEventExist.

@Test
public void shouldReturnEventExist() {
    final UUID streamId = randomUUID();
    final long position = 1L;
    final ZonedDateTime createdAt = now();
    final JsonEnvelope event = envelope().withPayloadOf("value1", "field1").with(metadataOf(streamId, "Test Name1").withVersion(position).withStreamId(streamId).createdAt(createdAt)).build();
    final EventStream eventStream = mock(EventStream.class);
    when(eventSource.getStreamById(streamId)).thenReturn(eventStream);
    when(eventStream.readFrom(1L)).thenReturn(Stream.of(event));
    assertTrue(service.eventExists(streamId, position));
    verify(eventSource).getStreamById(streamId);
    verify(eventStream).readFrom(position);
}
Also used : ZonedDateTime(java.time.ZonedDateTime) EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 59 with JsonEnvelope

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

the class EventStreamManager method appendNonConsecutively.

/**
 * Store a stream of events without enforcing consecutive version ids. Reduces risk of throwing
 * optimistic lock error. To be use instead of the append method, when it's acceptable to
 * store events with non consecutive version ids
 *
 * @param streamId - id of the stream to append to
 * @param events   the stream of events to store
 * @return the current stream version
 * @throws EventStreamException if an event could not be appended
 */
@Transactional(dontRollbackOn = OptimisticLockingRetryException.class)
public long appendNonConsecutively(final UUID streamId, final Stream<JsonEnvelope> events) throws EventStreamException {
    final List<JsonEnvelope> envelopeList = events.collect(toList());
    long currentVersion = eventRepository.getStreamSize(streamId);
    validateEvents(streamId, envelopeList);
    for (final JsonEnvelope event : envelopeList) {
        boolean appendedSuccessfully = false;
        long retryCount = 0L;
        while (!appendedSuccessfully) {
            try {
                eventAppender.append(event, streamId, ++currentVersion);
                appendedSuccessfully = true;
            } catch (OptimisticLockingRetryException e) {
                retryCount++;
                if (retryCount > maxRetry) {
                    logger.warn("Failed to append to stream {} due to concurrency issues, returning to handler.", streamId);
                    throw e;
                }
                currentVersion = eventRepository.getStreamSize(streamId);
                logger.trace("Retrying appending to stream {}, with version {}", streamId, currentVersion + 1);
            }
        }
    }
    return currentVersion;
}
Also used : JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) OptimisticLockingRetryException(uk.gov.justice.services.eventsourcing.repository.jdbc.exception.OptimisticLockingRetryException) Transactional(javax.transaction.Transactional)

Example 60 with JsonEnvelope

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

the class EventStreamManagerTest method shouldAppendToStreamFromVersion.

@Test
public void shouldAppendToStreamFromVersion() throws Exception {
    when(eventRepository.getStreamSize(STREAM_ID)).thenReturn(CURRENT_VERSION);
    final long expectedVersion = CURRENT_VERSION + 1;
    final JsonEnvelope event = envelope().with(metadataWithRandomUUIDAndName()).build();
    eventStreamManager.appendAfter(STREAM_ID, Stream.of(event), CURRENT_VERSION);
    verify(eventAppender).append(event, STREAM_ID, expectedVersion);
}
Also used : 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