use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.
the class EventStreamManagerTest method shouldLogWarningAfterMaxNumberOfRetriesReached.
@Test
public void shouldLogWarningAfterMaxNumberOfRetriesReached() throws Exception {
eventStreamManager.maxRetry = 2L;
final long currentVersion = 6L;
final long currentVersionAfterException1 = 11L;
final long currentVersionAfterException2 = 12L;
when(eventRepository.getStreamSize(STREAM_ID)).thenReturn(currentVersion).thenReturn(currentVersionAfterException1).thenReturn(currentVersionAfterException2);
final JsonEnvelope event = envelope().with(metadataWithDefaults()).build();
doThrow(OptimisticLockingRetryException.class).when(eventAppender).append(event, STREAM_ID, currentVersion + 1);
doThrow(OptimisticLockingRetryException.class).when(eventAppender).append(event, STREAM_ID, currentVersionAfterException1 + 1);
doThrow(OptimisticLockingRetryException.class).when(eventAppender).append(event, STREAM_ID, currentVersionAfterException2 + 1);
try {
eventStreamManager.appendNonConsecutively(STREAM_ID, Stream.of(event));
} catch (OptimisticLockingRetryException e) {
}
verify(logger).warn("Failed to append to stream {} due to concurrency issues, returning to handler.", STREAM_ID);
}
use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.
the class EnvelopeEventStreamTest method shouldAllowTwoAppendsAfterRead.
@Test
public void shouldAllowTwoAppendsAfterRead() throws Exception {
final JsonEnvelope event5 = envelope().with(metadataWithDefaults()).build();
final JsonEnvelope event6 = envelope().with(metadataWithDefaults()).build();
eventStream.read().forEach(e -> {
});
eventStream.append(Stream.of(event5));
verify(eventStreamManager).appendAfter(eq(STREAM_ID), streamCaptor.capture(), versionCaptor.capture());
final List<JsonEnvelope> appendedEvents1 = streamCaptor.getValue().collect(toList());
assertThat(appendedEvents1, hasSize(1));
assertThat(appendedEvents1.get(0), is(event5));
assertThat(versionCaptor.getValue(), equalTo(CURRENT_POSITION));
eventStream.append(Stream.of(event6));
verify(eventStreamManager, times(2)).appendAfter(eq(STREAM_ID), streamCaptor.capture(), versionCaptor.capture());
final List<JsonEnvelope> appendedEvents2 = streamCaptor.getValue().collect(toList());
assertThat(appendedEvents2, hasSize(1));
assertThat(appendedEvents2.get(0), is(event6));
assertThat(versionCaptor.getValue(), equalTo(CURRENT_POSITION + 1));
}
use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.
the class EnvelopeEventStreamTest method shouldAppendToLastReadVersion.
@Test
public void shouldAppendToLastReadVersion() throws Exception {
final JsonEnvelope event = envelope().with(metadataWithDefaults()).build();
final Stream<JsonEnvelope> events = Stream.of(event);
eventStream.read().forEach(e -> {
});
eventStream.append(events);
verify(eventStreamManager).appendAfter(eq(STREAM_ID), streamCaptor.capture(), eq(CURRENT_POSITION));
final List<JsonEnvelope> appendedEvents = streamCaptor.getValue().collect(toList());
assertThat(appendedEvents, hasSize(1));
assertThat(appendedEvents.get(0), is(event));
}
use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.
the class OrderCakeCommandApiTest method shouldHandleOrderCakeRequest.
@Test
public void shouldHandleOrderCakeRequest() {
commandApi = new OrderCakeCommandApi();
commandApi.enveloper = enveloper;
commandApi.sender = sender;
final JsonEnvelope envelope = envelope().with(metadataWithDefaults().withName("example.order-cake")).withPayloadOf("Field", "Value").build();
commandApi.orderCake(envelope);
verify(sender).send(envelopeCaptor.capture());
assertThat(envelopeCaptor.getValue().metadata().name(), is("example.command.order-cake"));
}
use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.
the class EventsService method events.
public List<EventEntry> events(final UUID streamId, final Position position, final Direction direction, final long pageSize) {
final EventStream eventStream = eventSource.getStreamById(streamId);
if (position.isHead()) {
final long versionHead = eventStream.size() - pageSize + 1;
final Stream<JsonEnvelope> events = eventStream.readFrom(versionHead).limit(pageSize);
return reverse(eventEntries(events));
}
if (position.isFirst()) {
final int versionFirst = 1;
final Stream<JsonEnvelope> events = eventStream.readFrom(versionFirst).limit(pageSize);
return reverse(eventEntries(events));
}
if (FORWARD.equals(direction)) {
final long version = position.getPosition();
final Stream<JsonEnvelope> events = eventStream.readFrom(version).limit(pageSize);
return reverse(eventEntries(events));
}
if (BACKWARD.equals(direction)) {
final long version = position.getPosition() - pageSize + 1;
final Stream<JsonEnvelope> events = eventStream.readFrom(version).limit(pageSize);
return reverse(eventEntries(events));
}
return emptyList();
}
Aggregations