SendEnvelopeFailureAction implements IAction<IObject> { @Override public void execute(final IObject actingObject) throws ActionExecuteException, InvalidArgumentException { try { IObject envelope = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.iobject.IObject")); envelope.setValue(messageFieldName, actingObject); MessageBus.send(envelope, targetChainId); } catch (ResolutionException | ChangeValueException | InvalidArgumentException | SendingMessageException e) { try { backupAction.execute(actingObject); } catch (Exception e1) { e.addSuppressed(e1); } throw new ActionExecuteException("Error occurred sending lost message.", e); } } SendEnvelopeFailureAction(final Object targetChainId, final IFieldName messageFieldName, final IAction<IObject> backupAction); @Override void execute(final IObject actingObject);  }
@Test public void Should_sendMessageToSpecifiedChain() throws Exception { IAction<IObject> action = new SendEnvelopeFailureAction(targetChainId, messageFN, backupActionMock); ArgumentCaptor<IObject> envelopeCaptor = ArgumentCaptor.forClass(IObject.class); action.execute(messageMock); verify(messageBusHandlerMock).handle(envelopeCaptor.capture(), same(targetChainId)); assertSame(messageMock, envelopeCaptor.getValue().getValue(messageFN)); }
@Test public void Should_callBackupActionWhenErrorOccursSendingTheEnvelope() throws Exception { doThrow(SendingMessageException.class).when(messageBusHandlerMock).handle(any(), any()); IAction<IObject> action = new SendEnvelopeFailureAction(targetChainId, messageFN, backupActionMock); try { action.execute(messageMock); fail(); } catch (ActionExecuteException ok) { } verify(backupActionMock).execute(same(messageMock)); }
@Test public void Should_suppressExceptionThrownByBackupAction() throws Exception { doThrow(SendingMessageException.class).when(messageBusHandlerMock).handle(any(), any()); doThrow(ActionExecuteException.class).when(backupActionMock).execute(same(messageMock)); IAction<IObject> action = new SendEnvelopeFailureAction(targetChainId, messageFN, backupActionMock); try { action.execute(messageMock); fail(); } catch (ActionExecuteException ok) { assertTrue(ok.getCause().getSuppressed()[0] instanceof ActionExecuteException); } }