Search in sources :

Example 1 with StreamCloseSpy

use of uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy 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 StreamCloseSpy

use of uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy in project microservice_framework by CJSCommonPlatform.

the class EventBufferInterceptorTest method ensureStreamCLosedIfExceptionOccurs.

@Test
public void ensureStreamCLosedIfExceptionOccurs() throws Exception {
    final Deque<Interceptor> interceptors = new LinkedList<>();
    interceptors.add(eventBufferInterceptor);
    interceptors.add(new ExceptionThrowingInterceptor());
    target = new TestTarget();
    interceptorChain = new DefaultInterceptorChain(interceptors, target);
    final InterceptorContext inputContext = interceptorContextWithInput(envelope_1);
    final StreamCloseSpy streamSpy = new StreamCloseSpy();
    final Stream<JsonEnvelope> envelopes = Stream.of(this.envelope_1, envelope_2).onClose(streamSpy);
    when(eventBufferService.currentOrderedEventsWith(this.envelope_1)).thenReturn(envelopes);
    try {
        interceptorChain.processNext(inputContext);
    } catch (TestException expected) {
    // do nothing
    }
    assertThat(streamSpy.streamClosed(), is(true));
}
Also used : StreamCloseSpy(uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy) InterceptorContext(uk.gov.justice.services.core.interceptor.InterceptorContext) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) Interceptor(uk.gov.justice.services.core.interceptor.Interceptor) LinkedList(java.util.LinkedList) DefaultInterceptorChain(uk.gov.justice.services.core.interceptor.DefaultInterceptorChain) Test(org.junit.Test)

Example 3 with StreamCloseSpy

use of uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy in project microservice_framework by CJSCommonPlatform.

the class DefaultInterceptorChainTest method shouldCloseStreamIfExceptionOccurs.

@Test
public void shouldCloseStreamIfExceptionOccurs() throws Exception {
    interceptors.offer(new ExceptionThrowingInterceptor());
    final InterceptorContext interceptorContext_1 = mock(InterceptorContext.class);
    final InterceptorContext interceptorContext_2 = mock(InterceptorContext.class);
    final StreamCloseSpy streamSpy = new StreamCloseSpy();
    final Stream<InterceptorContext> interceptorContexts = Stream.of(interceptorContext_1, interceptorContext_2).onClose(streamSpy);
    try {
        interceptorChain.processNext(interceptorContexts);
    } catch (TestException e) {
    }
    assertThat(streamSpy.streamClosed(), is(true));
}
Also used : StreamCloseSpy(uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy) Test(org.junit.Test)

Example 4 with StreamCloseSpy

use of uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy in project microservice_framework by CJSCommonPlatform.

the class DefaultEventRepositoryTest method shouldCloseAllStreamsOnCloseOfStreamOfStreams.

@Test
public void shouldCloseAllStreamsOnCloseOfStreamOfStreams() throws Exception {
    final UUID streamId1 = UUID.fromString("4b4e80a0-76f7-476c-b75b-527e38fb251e");
    final UUID streamId2 = UUID.fromString("4b4e80a0-76f7-476c-b75b-527e38fb252e");
    final UUID streamId3 = UUID.fromString("4b4e80a0-76f7-476c-b75b-527e38fb253e");
    final Event event1 = eventOf(streamId1);
    final Event event2 = eventOf(streamId2);
    final Event event3 = eventOf(streamId3);
    final JsonEnvelope envelope1 = mock(JsonEnvelope.class);
    final JsonEnvelope envelope2 = mock(JsonEnvelope.class);
    final JsonEnvelope envelope3 = mock(JsonEnvelope.class);
    StreamCloseSpy streamCloseSpy1 = new StreamCloseSpy();
    StreamCloseSpy streamCloseSpy2 = new StreamCloseSpy();
    StreamCloseSpy streamCloseSpy3 = new StreamCloseSpy();
    StreamCloseSpy streamCloseSpy4 = new StreamCloseSpy();
    when(eventJdbcRepository.getStreamIds()).thenReturn(Stream.of(streamId1, streamId2, streamId3).onClose(streamCloseSpy1));
    when(eventJdbcRepository.findByStreamIdOrderByPositionAsc(streamId1)).thenReturn(Stream.of(event1).onClose(streamCloseSpy2));
    when(eventJdbcRepository.findByStreamIdOrderByPositionAsc(streamId2)).thenReturn(Stream.of(event2).onClose(streamCloseSpy3));
    when(eventJdbcRepository.findByStreamIdOrderByPositionAsc(streamId3)).thenReturn(Stream.of(event3).onClose(streamCloseSpy4));
    when(eventConverter.envelopeOf(event1)).thenReturn(envelope1);
    when(eventConverter.envelopeOf(event2)).thenReturn(envelope2);
    when(eventConverter.envelopeOf(event3)).thenReturn(envelope3);
    final Stream<Stream<JsonEnvelope>> streamOfStreams = defaultEventRepository.getStreamOfAllEventStreams();
    streamOfStreams.collect(toList());
    streamOfStreams.close();
    assertThat(streamCloseSpy1.streamClosed(), is(true));
    assertThat(streamCloseSpy4.streamClosed(), is(true));
    assertThat(streamCloseSpy2.streamClosed(), is(true));
    assertThat(streamCloseSpy3.streamClosed(), is(true));
}
Also used : StreamCloseSpy(uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy) Event(uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event) JsonEnvelope(uk.gov.justice.services.messaging.JsonEnvelope) EventStream(uk.gov.justice.services.eventsourcing.repository.jdbc.eventstream.EventStream) Stream(java.util.stream.Stream) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 StreamCloseSpy (uk.gov.justice.services.test.utils.common.stream.StreamCloseSpy)4 JsonEnvelope (uk.gov.justice.services.messaging.JsonEnvelope)3 UUID (java.util.UUID)2 UUID.randomUUID (java.util.UUID.randomUUID)2 LinkedList (java.util.LinkedList)1 Stream (java.util.stream.Stream)1 DefaultInterceptorChain (uk.gov.justice.services.core.interceptor.DefaultInterceptorChain)1 Interceptor (uk.gov.justice.services.core.interceptor.Interceptor)1 InterceptorContext (uk.gov.justice.services.core.interceptor.InterceptorContext)1 StreamBufferEvent (uk.gov.justice.services.event.buffer.core.repository.streambuffer.StreamBufferEvent)1 Event (uk.gov.justice.services.eventsourcing.repository.jdbc.event.Event)1 EventStream (uk.gov.justice.services.eventsourcing.repository.jdbc.eventstream.EventStream)1