Search in sources :

Example 61 with JsonEnvelope

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);
}
Also used : JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 62 with JsonEnvelope

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);
}
Also used : JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 63 with JsonEnvelope

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));
}
Also used : JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 64 with JsonEnvelope

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);
}
Also used : JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Test(org.junit.Test)

Example 65 with JsonEnvelope

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);
}
Also used : 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