ChildDeletionCheckerReceiver implements IMessageReceiver { @Override public void receive(final IMessageProcessor processor) throws MessageReceiveException, AsynchronousOperationException { try { underlyingReceiver.receive(processor); checkDeletion(processor); } catch (MessageReceiveException e) { try { checkDeletion(processor); } catch (ActionExecuteException | DeletionCheckException | InvalidArgumentException ee) { e.addSuppressed(ee); } throw new MessageReceiveException(e); } catch (ActionExecuteException | DeletionCheckException | InvalidArgumentException e) { throw new MessageReceiveException(e); } } ChildDeletionCheckerReceiver(
            final IMessageReceiver underlyingReceiver,
            final IObject creationContext,
            final IChildDeletionCheckStrategy deletionCheckStrategy,
            final IAction<IObject> deletionAction); @Override void receive(final IMessageProcessor processor);  }
@Test public void Should_check() throws Exception { IMessageReceiver receiver = new ChildDeletionCheckerReceiver(underlyingReceiverMock, ctxMock, checkStrategyMock, deletionActionMock); receiver.receive(messageProcessorMock); verify(underlyingReceiverMock).receive(same(messageProcessorMock)); verify(checkStrategyMock).checkDelete(same(ctxMock), same(envMock)); verifyNoMoreInteractions(deletionActionMock); }
@Test public void Should_delete() throws Exception { IMessageReceiver receiver = new ChildDeletionCheckerReceiver(underlyingReceiverMock, ctxMock, checkStrategyMock, deletionActionMock); when(checkStrategyMock.checkDelete(same(ctxMock), same(envMock))).thenReturn(true); receiver.receive(messageProcessorMock); verify(underlyingReceiverMock).receive(messageProcessorMock); verify(checkStrategyMock).checkDelete(same(ctxMock), same(envMock)); verify(deletionActionMock).execute(same(ctxMock)); }
@Test public void Should_checkWhenReceiverThrows() throws Exception { IMessageReceiver receiver = new ChildDeletionCheckerReceiver(underlyingReceiverMock, ctxMock, checkStrategyMock, deletionActionMock); doThrow(new MessageReceiveException()).when(underlyingReceiverMock).receive(same(messageProcessorMock)); doThrow(new DeletionCheckException("")).when(checkStrategyMock).checkDelete(same(ctxMock), same(envMock)); try { receiver.receive(messageProcessorMock); fail(); } catch (MessageReceiveException e) { assertEquals(1, e.getCause().getSuppressed().length); assertTrue(e.getCause().getSuppressed()[0] instanceof DeletionCheckException); } }