use of uk.gov.justice.services.event.buffer.core.repository.streamstatus.StreamStatus in project microservice_framework by CJSCommonPlatform.
the class EventBufferIT method shouldAddStatusVersionForNewStreamIdAndProcessIncomingEvent.
@Test
public void shouldAddStatusVersionForNewStreamIdAndProcessIncomingEvent() {
final UUID metadataId = randomUUID();
final UUID streamId = randomUUID();
final JsonEnvelope jsonEnvelope = envelope().with(metadataOf(metadataId, EVENT_ABC).withStreamId(streamId).withVersion(1L)).build();
interceptorChainProcessor.process(interceptorContextWithInput(jsonEnvelope));
final List<StreamBufferEvent> streamBufferEvents = jdbcStreamBufferRepository.findStreamByIdAndSource(streamId, SOURCE).collect(toList());
final Optional<StreamStatus> streamStatus = statusRepository.findByStreamIdAndSource(streamId, SOURCE);
assertThat(streamBufferEvents, empty());
assertThat(streamStatus.isPresent(), is(true));
assertThat(streamStatus.get().getVersion(), is(1L));
final List<JsonEnvelope> handledEnvelopes = abcEventHandler.recordedEnvelopes();
assertThat(handledEnvelopes, hasSize(1));
assertThat(handledEnvelopes.get(0).metadata().id(), is(metadataId));
assertThat(handledEnvelopes.get(0).metadata().version(), contains(1L));
}
use of uk.gov.justice.services.event.buffer.core.repository.streamstatus.StreamStatus in project microservice_framework by CJSCommonPlatform.
the class EventBufferIT method shouldNotIncrementVersionWhenEventNotInOrder.
@Test
public void shouldNotIncrementVersionWhenEventNotInOrder() throws SQLException, NamingException {
final UUID metadataId = randomUUID();
final UUID streamId = randomUUID();
statusRepository.insert(new StreamStatus(streamId, 2L, SOURCE));
final JsonEnvelope jsonEnvelope = envelope().with(metadataOf(metadataId, EVENT_ABC).withStreamId(streamId).withVersion(4L)).build();
interceptorChainProcessor.process(interceptorContextWithInput(jsonEnvelope));
final List<StreamBufferEvent> streamBufferEvents = jdbcStreamBufferRepository.findStreamByIdAndSource(streamId, SOURCE).collect(toList());
final Optional<StreamStatus> streamStatus = statusRepository.findByStreamIdAndSource(streamId, SOURCE);
assertThat(streamBufferEvents, hasSize(1));
assertThat(streamBufferEvents.get(0).getStreamId(), is(streamId));
assertThat(streamBufferEvents.get(0).getVersion(), is(4L));
assertThat(streamStatus.isPresent(), is(true));
assertThat(streamStatus.get().getVersion(), is(2L));
}
use of uk.gov.justice.services.event.buffer.core.repository.streamstatus.StreamStatus in project microservice_framework by CJSCommonPlatform.
the class AnsiSQLBasedBufferInitialisationStrategyTest method shouldNotAddStatusIfItExists.
@Test
public void shouldNotAddStatusIfItExists() throws Exception {
final UUID streamId = randomUUID();
final String source = "a source";
when(streamStatusRepository.findByStreamIdAndSource(streamId, source)).thenReturn(of(new StreamStatus(streamId, 3L, source)));
bufferInitialisationStrategy.initialiseBuffer(streamId, source);
verify(streamStatusRepository).findByStreamIdAndSource(streamId, source);
verifyNoMoreInteractions(streamStatusRepository);
}
use of uk.gov.justice.services.event.buffer.core.repository.streamstatus.StreamStatus in project microservice_framework by CJSCommonPlatform.
the class ConsecutiveEventBufferServiceTest method shouldStoreEventIncomingNotInOrderAndReturnEmpty.
@Test
public void shouldStoreEventIncomingNotInOrderAndReturnEmpty() {
final String eventName = "source.events.something.happened";
final String source = "source";
final UUID streamId = randomUUID();
final JsonEnvelope incomingEvent = envelope().with(metadataWithDefaults().withName(eventName).withStreamId(streamId).withVersion(6L)).build();
when(bufferInitialisationStrategy.initialiseBuffer(streamId, source)).thenReturn(4L);
when(streamStatusRepository.findByStreamIdAndSource(streamId, source)).thenReturn(Optional.of(new StreamStatus(streamId, 4L, source)));
when(jsonObjectEnvelopeConverter.asJsonString(incomingEvent)).thenReturn("someStringRepresentation");
final Stream<JsonEnvelope> returnedEvents = bufferService.currentOrderedEventsWith(incomingEvent);
verify(streamBufferRepository).insert(new StreamBufferEvent(streamId, 6L, "someStringRepresentation", source));
assertThat(returnedEvents, empty());
}
use of uk.gov.justice.services.event.buffer.core.repository.streamstatus.StreamStatus in project microservice_framework by CJSCommonPlatform.
the class ConsecutiveEventBufferService method currentOrderedEventsWith.
/**
* Takes an incoming event and returns a stream of json envelopes. If the event is not
* consecutive according to the stream_status repository then an empty stream is returned and
* the incomingEvent is added to the event-buffer-repository. If the incomingEvent is
* consecutive then it is returned as a stream with any consecutive events from the buffer. If
* an event is the first to be processed for that streamId then the version value must be 1 or
* the incomingEvent is added to the buffer and an empty stream is returned.
*
* @return stream of consecutive events
*/
@Override
public Stream<JsonEnvelope> currentOrderedEventsWith(final JsonEnvelope incomingEvent) {
logger.trace("Message buffering for message: {}", incomingEvent);
final UUID streamId = incomingEvent.metadata().streamId().orElseThrow(() -> new IllegalStateException("Event must have a a streamId "));
final long incomingEventVersion = versionOf(incomingEvent);
final String source = getSource(incomingEvent);
final long currentVersion = bufferInitialisationStrategy.initialiseBuffer(streamId, source);
if (incomingEventObsolete(incomingEventVersion, currentVersion)) {
logger.warn("Message : {} is an obsolete version", incomingEvent);
return Stream.empty();
} else if (incomingEventNotInOrder(incomingEventVersion, currentVersion)) {
logger.trace("Message : {} is not consecutive, adding to buffer", incomingEvent);
addToBuffer(incomingEvent, streamId, incomingEventVersion);
return Stream.empty();
} else {
logger.trace("Message : {} version is valid sending stream to dispatcher", incomingEvent);
streamStatusRepository.update(new StreamStatus(streamId, incomingEventVersion, source));
return bufferedEvents(streamId, incomingEvent, incomingEventVersion);
}
}
Aggregations