ChainCallReceiver implements IMessageReceiver { @Override public void receive(final IMessageProcessor processor) throws MessageReceiveException { try { Object chainId = chainChoiceStrategy.chooseChain(processor); IReceiverChain chain = chainStorage.resolve(chainId); checkAccess(chain, processor); processor.getSequence().callChain(chain); } catch (ChainChoiceException | ChainNotFoundException | NestedChainStackOverflowException e) { throw new MessageReceiveException("Could not call nested chain.", e); } } ChainCallReceiver(final IChainStorage chainStorage, final IChainChoiceStrategy chainChoiceStrategy); @Override void receive(final IMessageProcessor processor);  }
@Test public void Should_applyStrategy() throws Exception { IChainStorage chainStorageMock = mock(IChainStorage.class); IChainChoiceStrategy chainChoiceStrategyMock = mock(IChainChoiceStrategy.class); Object chainIdMock = mock(Object.class); IReceiverChain chainMock = mock(IReceiverChain.class); IMessageProcessor messageProcessorMock = mock(IMessageProcessor.class); IMessageProcessingSequence sequenceMock = mock(IMessageProcessingSequence.class); ChainChoiceException exceptionMock = mock(ChainChoiceException.class); IObject chainDescriptionMock = mock(IObject.class); IObject contextMock = mock(IObject.class); IMessageReceiver receiver = new ChainCallReceiver(chainStorageMock, chainChoiceStrategyMock); when(chainChoiceStrategyMock.chooseChain(same(messageProcessorMock))).thenReturn(chainIdMock); when(chainStorageMock.resolve(same(chainIdMock))).thenReturn(chainMock); when(messageProcessorMock.getSequence()).thenReturn(sequenceMock); when(messageProcessorMock.getContext()).thenReturn(contextMock); when(chainMock.getChainDescription()).thenReturn(chainDescriptionMock); when(chainDescriptionMock.getValue(new FieldName("externalAccess"))).thenReturn(true); receiver.receive(messageProcessorMock); verify(sequenceMock).callChain(same(chainMock)); when(chainChoiceStrategyMock.chooseChain(same(messageProcessorMock))).thenThrow(exceptionMock); try { receiver.receive(messageProcessorMock); fail(); } catch (MessageReceiveException e) { assertSame(exceptionMock, e.getCause()); } }
@Test public void checkMessageReceiveExceptionOnAccessForbidden() throws Exception { IChainStorage chainStorageMock = mock(IChainStorage.class); IChainChoiceStrategy chainChoiceStrategyMock = mock(IChainChoiceStrategy.class); Object chainIdMock = mock(Object.class); IReceiverChain chainMock = mock(IReceiverChain.class); IMessageProcessor messageProcessorMock = mock(IMessageProcessor.class); IObject chainDescriptionMock = mock(IObject.class); IObject contextMock = mock(IObject.class); IMessageReceiver receiver = new ChainCallReceiver(chainStorageMock, chainChoiceStrategyMock); when(chainChoiceStrategyMock.chooseChain(same(messageProcessorMock))).thenReturn(chainIdMock); when(chainStorageMock.resolve(same(chainIdMock))).thenReturn(chainMock); when(messageProcessorMock.getContext()).thenReturn(contextMock); when(chainMock.getChainDescription()).thenReturn(chainDescriptionMock); when(contextMock.getValue(new FieldName("fromExternal"))).thenReturn(true); when(chainDescriptionMock.getValue(new FieldName("externalAccess"))).thenReturn(false); try { receiver.receive(messageProcessorMock); fail(); } catch (MessageReceiveException e) { verify(contextMock, times(1)).setValue(new FieldName("fromExternal"), false); verify(contextMock, times(1)).setValue(new FieldName("accessToChainForbiddenError"), true); } }