SuspensionBean { @Interceptors(MdcLoggerInterceptor.class) @Transactional(REQUIRED) public void runSuspension(final UUID commandId, final SuspensionCommand suspensionCommand) { final String systemCommandName = suspensionCommand.getName(); logger.info(format("Running %s", systemCommandName)); systemCommandStateChangedEventFirer.fire(new SystemCommandStateChangedEvent( commandId, suspensionCommand, COMMAND_IN_PROGRESS, clock.now(), format("%s started", systemCommandName) )); final List<SuspensionResult> results = suspendablesRunner.findAndRunSuspendables( commandId, suspensionCommand ); final List<SuspensionResult> failureResults = suspensionResultsMapper.getFailedResults(results); final List<SuspensionResult> successfulResults = suspensionResultsMapper.getSuccessfulResults(results); logger.info(format( "%s ran with %s success(es) and %s error(s)", systemCommandName, successfulResults.size(), failureResults.size())); if (failureResults.isEmpty()) { suspensionPostProcess.completeSuspensionSuccessfully(successfulResults, commandId, suspensionCommand); } else { suspensionPostProcess.completeSuspensionWithFailures(failureResults, commandId, suspensionCommand); } }  @Interceptors(MdcLoggerInterceptor.class) @Transactional(REQUIRED) void runSuspension(final UUID commandId, final SuspensionCommand suspensionCommand);  }
@Test public void shouldRunAllShutteringExecutorsAndFireTheSuccessEvent() throws Exception { final String commandName = "SHUTTER"; final UUID commandId = randomUUID(); final SuspensionCommand suspensionCommand = mock(SuspensionCommand.class); final ZonedDateTime stateChangedAt = new UtcClock().now(); when(suspensionCommand.getName()).thenReturn(commandName); when(clock.now()).thenReturn(stateChangedAt); final List<SuspensionResult> results = singletonList(mock(SuspensionResult.class)); final List<SuspensionResult> successfulResults = singletonList(mock(SuspensionResult.class)); final List<SuspensionResult> failureResults = emptyList(); when(suspendablesRunner.findAndRunSuspendables( commandId, suspensionCommand )).thenReturn(results); when(suspensionResultsMapper.getFailedResults(results)).thenReturn(failureResults); when(suspensionResultsMapper.getSuccessfulResults(results)).thenReturn(successfulResults); suspensionBean.runSuspension(commandId, suspensionCommand); final InOrder inOrder = inOrder( logger, systemCommandStateChangedEventFirer, suspensionPostProcess); inOrder.verify(logger).info("Running SHUTTER"); inOrder.verify(systemCommandStateChangedEventFirer).fire(systemCommandStateChangedEventCaptor.capture()); inOrder.verify(logger).info("SHUTTER ran with 1 success(es) and 0 error(s)"); inOrder.verify(suspensionPostProcess).completeSuspensionSuccessfully(successfulResults, commandId, suspensionCommand); }
@Test public void shouldFireTheFailureEventIfAnyOfTheSutteringExectorsFail() throws Exception { final String commandName = "SHUTTER"; final UUID commandId = randomUUID(); final SuspensionCommand suspensionCommand = mock(SuspensionCommand.class); final ZonedDateTime stateChangedAt = new UtcClock().now(); when(suspensionCommand.getName()).thenReturn(commandName); when(clock.now()).thenReturn(stateChangedAt); final List<SuspensionResult> results = singletonList(mock(SuspensionResult.class)); final List<SuspensionResult> successfulResults = singletonList(mock(SuspensionResult.class)); final List<SuspensionResult> failureResults = singletonList(mock(SuspensionResult.class)); when(suspendablesRunner.findAndRunSuspendables( commandId, suspensionCommand )).thenReturn(results); when(suspensionResultsMapper.getFailedResults(results)).thenReturn(failureResults); when(suspensionResultsMapper.getSuccessfulResults(results)).thenReturn(successfulResults); suspensionBean.runSuspension(commandId, suspensionCommand); final InOrder inOrder = inOrder( logger, systemCommandStateChangedEventFirer, suspensionPostProcess); inOrder.verify(logger).info("Running SHUTTER"); inOrder.verify(systemCommandStateChangedEventFirer).fire(systemCommandStateChangedEventCaptor.capture()); inOrder.verify(logger).info("SHUTTER ran with 1 success(es) and 1 error(s)"); inOrder.verify(suspensionPostProcess).completeSuspensionWithFailures(failureResults, commandId, suspensionCommand); }