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)));
}
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)));
}
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);
}
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;
}
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);
}
Aggregations