DefaultInterceptorChain implements InterceptorChain { public InterceptorContext processNext(final InterceptorContext interceptorContext) { if (interceptors.isEmpty()) { return target.process(interceptorContext); } return interceptors.poll().process(interceptorContext, copyOfInterceptorChain()); } DefaultInterceptorChain(final Deque<Interceptor> interceptors, final Target target); InterceptorContext processNext(final InterceptorContext interceptorContext); List<InterceptorContext> processNext(final Stream<InterceptorContext> interceptorContexts);  }
@Test public void shouldProcessNextInterceptorInTheQueue() throws Exception { final InterceptorContext interceptorContext = mock(InterceptorContext.class); final InterceptorContext result = interceptorChain.processNext(interceptorContext); assertThat(result, is(interceptorContext)); }
@Test public void shouldSendEachInterceptorContextToTheNextInterceptorInTheQueue() throws Exception { final InterceptorContext interceptorContext_1 = mock(InterceptorContext.class); final InterceptorContext interceptorContext_2 = mock(InterceptorContext.class); final InterceptorContext interceptorContext_3 = mock(InterceptorContext.class); final Stream<InterceptorContext> interceptorContexts = Stream.of( interceptorContext_1, interceptorContext_2, interceptorContext_3); final List<InterceptorContext> results = interceptorChain.processNext(interceptorContexts); assertThat(results.size(), is(3)); assertThat(results, hasItems( interceptorContext_1, interceptorContext_2, interceptorContext_3)); }
@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)); }