Search in sources :

Example 1 with StreamBufferEvent

use of uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferEvent in project microservice_framework by CJSCommonPlatform.

the class ConsecutiveEventBufferServiceTest method shoulCloseSourceStreamOnConsecutiveStreamClose.

@Test
public void shoulCloseSourceStreamOnConsecutiveStreamClose() {
    final UUID streamId = randomUUID();
    final String source = "source";
    final String eventName = "source.event.name";
    when(bufferInitialisationStrategy.initialiseBuffer(eq(streamId), eq(source))).thenReturn(2L);
    final StreamCloseSpy sourceStreamSpy = new StreamCloseSpy();
    when(streamBufferRepository.findStreamByIdAndSource(streamId, source)).thenReturn(Stream.of(new StreamBufferEvent(streamId, 4L, "someEventContent4", source), new StreamBufferEvent(streamId, 8L, "someEventContent8", source)).onClose(sourceStreamSpy));
    final JsonEnvelope bufferedEvent4 = mock(JsonEnvelope.class);
    when(jsonObjectEnvelopeConverter.asEnvelope("someEventContent4")).thenReturn(bufferedEvent4);
    final JsonEnvelope incomingEvent = envelope().with(metadataWithDefaults().withName(eventName).withStreamId(streamId).withVersion(3L)).build();
    final Stream<JsonEnvelope> returnedEvents = bufferService.currentOrderedEventsWith(incomingEvent);
    returnedEvents.close();
    assertThat(sourceStreamSpy.streamClosed(), is(true));
}
Also used : StreamBufferEvent(uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferEvent) StreamCloseSpy(uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 2 with StreamBufferEvent

use of uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferEvent in project microservice_framework by CJSCommonPlatform.

the class ConsecutiveEventBufferServiceTest method shouldReturnConsecutiveBufferedEventsIfIncomingEventFillsTheVersionGap.

@Test
public void shouldReturnConsecutiveBufferedEventsIfIncomingEventFillsTheVersionGap() {
    final UUID streamId = randomUUID();
    final String source = "source";
    final String eventName = "source.event.name";
    when(bufferInitialisationStrategy.initialiseBuffer(eq(streamId), eq(source))).thenReturn(2L);
    when(streamBufferRepository.findStreamByIdAndSource(streamId, source)).thenReturn(Stream.of(new StreamBufferEvent(streamId, 4L, "someEventContent4", "source_4"), new StreamBufferEvent(streamId, 5L, "someEventContent5", "source_5"), new StreamBufferEvent(streamId, 6L, "someEventContent6", "source_6"), new StreamBufferEvent(streamId, 8L, "someEventContent8", "source_8"), new StreamBufferEvent(streamId, 9L, "someEventContent9", "source_9"), new StreamBufferEvent(streamId, 10L, "someEventContent10", "source_10"), new StreamBufferEvent(streamId, 11L, "someEventContent11", "source_11")));
    final JsonEnvelope bufferedEvent4 = envelope().build();
    final JsonEnvelope bufferedEvent5 = envelope().build();
    final JsonEnvelope bufferedEvent6 = envelope().build();
    when(jsonObjectEnvelopeConverter.asEnvelope("someEventContent4")).thenReturn(bufferedEvent4);
    when(jsonObjectEnvelopeConverter.asEnvelope("someEventContent5")).thenReturn(bufferedEvent5);
    when(jsonObjectEnvelopeConverter.asEnvelope("someEventContent6")).thenReturn(bufferedEvent6);
    final JsonEnvelope incomingEvent = envelope().with(metadataWithDefaults().withName(eventName).withStreamId(streamId).withVersion(3L)).build();
    final Stream<JsonEnvelope> returnedEvents = bufferService.currentOrderedEventsWith(incomingEvent);
    assertThat(returnedEvents, contains(incomingEvent, bufferedEvent4, bufferedEvent5, bufferedEvent6));
}
Also used : StreamBufferEvent(uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferEvent) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 3 with StreamBufferEvent

use of uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferEvent in project microservice_framework by CJSCommonPlatform.

the class ConsecutiveEventBufferServiceTest method shouldRemoveEventsFromBufferOnceStreamed.

@Test
public void shouldRemoveEventsFromBufferOnceStreamed() {
    final UUID streamId = randomUUID();
    final String source = "source";
    final String eventName = "source.event.name";
    when(bufferInitialisationStrategy.initialiseBuffer(eq(streamId), eq(source))).thenReturn(2L);
    final StreamBufferEvent event4 = new StreamBufferEvent(streamId, 4L, "someEventContent4", "source_1");
    final StreamBufferEvent event5 = new StreamBufferEvent(streamId, 5L, "someEventContent5", "source_2");
    final StreamBufferEvent event6 = new StreamBufferEvent(streamId, 6L, "someEventContent6", "source_3");
    when(streamBufferRepository.findStreamByIdAndSource(streamId, source)).thenReturn(Stream.of(event4, event5, event6));
    final JsonEnvelope bufferedEvent4 = envelope().build();
    final JsonEnvelope bufferedEvent5 = envelope().build();
    final JsonEnvelope bufferedEvent6 = envelope().build();
    when(jsonObjectEnvelopeConverter.asEnvelope("someEventContent4")).thenReturn(bufferedEvent4);
    when(jsonObjectEnvelopeConverter.asEnvelope("someEventContent5")).thenReturn(bufferedEvent5);
    when(jsonObjectEnvelopeConverter.asEnvelope("someEventContent6")).thenReturn(bufferedEvent6);
    final JsonEnvelope incomingEvent = envelope().with(metadataWithDefaults().withName(eventName).withStreamId(streamId).withVersion(3L)).build();
    final Stream<JsonEnvelope> returnedEvents = bufferService.currentOrderedEventsWith(incomingEvent);
    assertThat(returnedEvents, contains(incomingEvent, bufferedEvent4, bufferedEvent5, bufferedEvent6));
    verify(streamBufferRepository).remove(event4);
    verify(streamBufferRepository).remove(event5);
    verify(streamBufferRepository).remove(event6);
}
Also used : StreamBufferEvent(uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferEvent) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 4 with StreamBufferEvent

use of uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferEvent in project microservice_framework by CJSCommonPlatform.

the class EventBufferIT method shouldAddEventToBufferIfVersionNotOne.

// Uncomment below to test when deplpoyed to the vagrant vm
/*

    @Configuration
    public Properties postgresqlConfiguration() {
        return OpenEjbConfigurationBuilder.createOpenEjbConfigurationBuilder()
                .addInitialContext()
                .addPostgresqlViewStore()
                .build();
    }

    */
@Test
public void shouldAddEventToBufferIfVersionNotOne() {
    final UUID metadataId = randomUUID();
    final UUID streamId = randomUUID();
    final JsonEnvelope envelope = envelope().with(metadataOf(metadataId, EVENT_ABC).withStreamId(streamId).withVersion(2L)).build();
    interceptorChainProcessor.process(interceptorContextWithInput(envelope));
    final List<StreamBufferEvent> streamBufferEvents = jdbcStreamBufferRepository.findStreamByIdAndSource(streamId, SOURCE).collect(toList());
    assertThat(streamBufferEvents, hasSize(1));
    assertThat(streamBufferEvents.get(0).getStreamId(), is(streamId));
    assertThat(streamBufferEvents.get(0).getVersion(), is(2L));
    assertThat(streamBufferEvents.get(0).getSource(), is(SOURCE));
    final List<JsonEnvelope> handledEnvelopes = abcEventHandler.recordedEnvelopes();
    assertThat(handledEnvelopes, empty());
}
Also used : StreamBufferEvent(uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferEvent) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Example 5 with StreamBufferEvent

use of uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferEvent in project microservice_framework by CJSCommonPlatform.

the class EventBufferIT method shouldReleaseBufferWhenMissingEventArrives.

@Test
public void shouldReleaseBufferWhenMissingEventArrives() throws SQLException, NamingException {
    final UUID metadataId2 = randomUUID();
    final UUID streamId = randomUUID();
    final JsonEnvelope jsonEnvelope = envelope().with(metadataOf(metadataId2, EVENT_ABC).withStreamId(streamId).withVersion(2L)).build();
    statusRepository.insert(new StreamStatus(streamId, 1L, SOURCE));
    final UUID metadataId3 = randomUUID();
    final UUID metadataId4 = randomUUID();
    final UUID metadataId5 = randomUUID();
    jdbcStreamBufferRepository.insert(new StreamBufferEvent(streamId, 3L, envelope().with(metadataOf(metadataId3, EVENT_ABC).withStreamId(streamId).withVersion(3L)).toJsonString(), SOURCE));
    jdbcStreamBufferRepository.insert(new StreamBufferEvent(streamId, 4L, envelope().with(metadataOf(metadataId4, EVENT_ABC).withStreamId(streamId).withVersion(4L)).toJsonString(), SOURCE));
    jdbcStreamBufferRepository.insert(new StreamBufferEvent(streamId, 5L, envelope().with(metadataOf(metadataId5, EVENT_ABC).withStreamId(streamId).withVersion(5L)).toJsonString(), SOURCE));
    interceptorChainProcessor.process(interceptorContextWithInput(jsonEnvelope));
    final List<StreamBufferEvent> streamBufferEvents = jdbcStreamBufferRepository.findStreamByIdAndSource(streamId, SOURCE).collect(toList());
    final Optional<StreamStatus> streamStatus = statusRepository.findByStreamIdAndSource(streamId, SOURCE);
    assertThat(streamStatus.isPresent(), is(true));
    assertThat(streamStatus.get().getVersion(), is(5L));
    final List<JsonEnvelope> handledEnvelopes = abcEventHandler.recordedEnvelopes();
    assertThat(handledEnvelopes, hasSize(4));
    assertThat(handledEnvelopes.get(0).metadata().id(), is(metadataId2));
    assertThat(handledEnvelopes.get(0).metadata().version(), contains(2L));
    assertThat(handledEnvelopes.get(1).metadata().id(), is(metadataId3));
    assertThat(handledEnvelopes.get(1).metadata().version(), contains(3L));
    assertThat(handledEnvelopes.get(2).metadata().id(), is(metadataId4));
    assertThat(handledEnvelopes.get(2).metadata().version(), contains(4L));
    assertThat(handledEnvelopes.get(3).metadata().id(), is(metadataId5));
    assertThat(handledEnvelopes.get(3).metadata().version(), contains(5L));
    assertThat(streamBufferEvents, hasSize(0));
}
Also used : StreamBufferEvent(uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferEvent) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) StreamStatus(uk.gov.justice.services.event.buffer.core.repository.streamstatus.StreamStatus) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Aggregations

StreamBufferEvent (uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferEvent)11 UUID (java.util.UUID)10 UUID.randomUUID (java.util.UUID.randomUUID)10 Test (org.junit.Test)10 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)10 StreamStatus (uk.gov.justice.services.event.buffer.core.repository.streamstatus.StreamStatus)6 StreamCloseSpy (uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy)1