use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.
the class EventStreamManagerTest method shouldAppendNonConsecutively.
@Test
public void shouldAppendNonConsecutively() throws Exception {
when(eventRepository.getStreamSize(STREAM_ID)).thenReturn(CURRENT_VERSION);
final JsonEnvelope event1 = envelope().with(metadataWithDefaults()).build();
final JsonEnvelope event2 = envelope().with(metadataWithDefaults()).build();
eventStreamManager.appendNonConsecutively(STREAM_ID, Stream.of(event1, event2));
verify(eventAppender).append(event1, STREAM_ID, CURRENT_VERSION + 1);
verify(eventAppender).append(event2, STREAM_ID, CURRENT_VERSION + 2);
}
use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.
the class EventStreamManagerTest method shouldRetryWithNextVersionIdInCaseOfOptimisticLockException.
@Test
public void shouldRetryWithNextVersionIdInCaseOfOptimisticLockException() throws Exception {
eventStreamManager.maxRetry = 20L;
final long currentVersion = 6L;
final long currentVersionAfterException = 11L;
when(eventRepository.getStreamSize(STREAM_ID)).thenReturn(currentVersion).thenReturn(currentVersionAfterException);
final JsonEnvelope event1 = envelope().with(metadataWithDefaults()).build();
final JsonEnvelope event2 = envelope().with(metadataWithDefaults()).build();
final JsonEnvelope event3 = envelope().with(metadataWithDefaults()).build();
doThrow(OptimisticLockingRetryException.class).when(eventAppender).append(event2, STREAM_ID, currentVersion + 2);
eventStreamManager.appendNonConsecutively(STREAM_ID, Stream.of(event1, event2, event3));
verify(eventAppender).append(event1, STREAM_ID, currentVersion + 1);
verify(eventAppender).append(event2, STREAM_ID, currentVersion + 2);
verify(eventAppender).append(event2, STREAM_ID, currentVersionAfterException + 1);
verify(eventAppender).append(event3, STREAM_ID, currentVersionAfterException + 2);
}
use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.
the class EventStreamManagerTest method shouldReturnCurrentVersionWhenAppendingNonConsecutively.
@Test
public void shouldReturnCurrentVersionWhenAppendingNonConsecutively() throws Exception {
when(eventRepository.getStreamSize(STREAM_ID)).thenReturn(CURRENT_VERSION);
final JsonEnvelope event1 = envelope().with(metadataWithDefaults()).build();
final JsonEnvelope event2 = envelope().with(metadataWithDefaults()).build();
long returnedVersion = eventStreamManager.appendNonConsecutively(STREAM_ID, Stream.of(event1, event2));
assertThat(returnedVersion, is(CURRENT_VERSION + 2));
}
use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.
the class EventStreamManagerTest method shouldTraceLogAnAttemptedRetry.
@Test
public void shouldTraceLogAnAttemptedRetry() throws Exception {
eventStreamManager.maxRetry = 20L;
final long currentVersion = 6L;
final long currentVersionAfterException = 11L;
when(eventRepository.getStreamSize(STREAM_ID)).thenReturn(currentVersion).thenReturn(currentVersionAfterException);
final JsonEnvelope event1 = envelope().with(metadataWithDefaults()).build();
final JsonEnvelope event2 = envelope().with(metadataWithDefaults()).build();
final JsonEnvelope event3 = envelope().with(metadataWithDefaults()).build();
doThrow(OptimisticLockingRetryException.class).when(eventAppender).append(event2, STREAM_ID, currentVersion + 2);
eventStreamManager.appendNonConsecutively(STREAM_ID, Stream.of(event1, event2, event3));
verify(logger).trace("Retrying appending to stream {}, with version {}", STREAM_ID, currentVersionAfterException + 1);
}
use of uk.gov.justice.services.messaging.JsonEnvelope in project microservice_framework by CJSCommonPlatform.
the class EventStreamManagerTest method shouldCloneStreamWithBlankVersions.
@Test
public void shouldCloneStreamWithBlankVersions() throws EventStreamException {
final JsonEnvelope event = buildEnvelope("test.events.event1");
final JsonEnvelope systemEvent = buildEnvelope("system.events.cloned");
when(eventRepository.getEventsByStreamId(STREAM_ID)).thenReturn(Stream.of(event));
when(eventRepository.getStreamPosition(STREAM_ID)).thenReturn(0L);
when(systemEventService.clonedEventFor(STREAM_ID)).thenReturn(systemEvent);
final UUID clonedId = eventStreamManager.cloneAsAncestor(STREAM_ID);
assertThat(clonedId, is(notNullValue()));
assertThat(clonedId, is(not(STREAM_ID)));
verify(eventAppender, times(2)).append(eventCaptor.capture(), eq(clonedId), versionCaptor.capture());
final List<JsonEnvelope> clonedEvents = eventCaptor.getAllValues();
assertThat(versionCaptor.getAllValues(), hasItems(1L, 2L));
assertThat(clonedEvents, hasItems(systemEvent));
final JsonEnvelope clonedEvent = clonedEvents.get(0);
assertThat(clonedEvent.metadata().name(), is("test.events.event1"));
assertThat(clonedEvent.metadata().version(), is(empty()));
verify(eventRepository).markEventStreamActive(clonedId, false);
}
Aggregations