Search in sources :

Example 66 with JsonEnvelope

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

Example 67 with JsonEnvelope

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

Example 68 with JsonEnvelope

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

Example 69 with JsonEnvelope

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

Example 70 with JsonEnvelope

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();
}
Also used : EventStream(uk.gov.justice.services.eventsourcing.source.core.EventStream) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope)

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