SuspensionPostProcess { public void completeSuspensionWithFailures(final List<SuspensionResult> failureResults, final UUID commandId, final SuspensionCommand suspensionCommand) { final String suspensionCommandName = suspensionCommand.getName(); logger.error(format("%s failed with the following %d errors:", suspensionCommandName, failureResults.size())); failureResults.forEach(suspensionResult -> logger.error(format("%s Error: %s", suspensionCommandName, suspensionResult.getMessage()))); final List<String> suspendablesNames = suspensionResultsMapper .getSuspendablesNames(failureResults); final String message = format( "%s failed. The following Suspendables failed: %s", suspensionCommandName, suspendablesNames); systemCommandStateChangedEventFirer.fire(new SystemCommandStateChangedEvent( commandId, suspensionCommand, COMMAND_FAILED, clock.now(), message )); }  void completeSuspensionSuccessfully(final List<SuspensionResult> successfulResults, final UUID commandId, final SuspensionCommand suspensionCommand); void completeSuspensionWithFailures(final List<SuspensionResult> failureResults, final UUID commandId, final SuspensionCommand suspensionCommand);  }
@Test public void shouldFireFailedEventIfSuspensionFailed() throws Exception { final UUID commandId = randomUUID(); final ZonedDateTime stateChangedAt = new UtcClock().now(); final SuspensionCommand suspensionCommand = new SuspendCommand(); final SuspensionResult failureResult_1 = mock(SuspensionResult.class); final SuspensionResult failureResult_2 = mock(SuspensionResult.class); final List<SuspensionResult> unsuccessfulResults = asList(failureResult_1, failureResult_2); final List<String> suspendableNames = asList("Suspendable_1", "Suspendable_2"); when(failureResult_1.getMessage()).thenReturn("failure 1"); when(failureResult_2.getMessage()).thenReturn("failure 2"); when(suspensionResultsMapper.getSuspendablesNames(unsuccessfulResults)).thenReturn(suspendableNames); when(clock.now()).thenReturn(stateChangedAt); suspensionPostProcess.completeSuspensionWithFailures(unsuccessfulResults, commandId, suspensionCommand); final InOrder inOrder = inOrder(logger, systemCommandStateChangedEventFirer); inOrder.verify(logger).error("SUSPEND failed with the following 2 errors:"); inOrder.verify(logger).error("SUSPEND Error: failure 1"); inOrder.verify(logger).error("SUSPEND Error: failure 2"); inOrder.verify(systemCommandStateChangedEventFirer).fire(systemCommandStateChangedEventCaptor.capture()); final SystemCommandStateChangedEvent stateChangedEvent = systemCommandStateChangedEventCaptor.getValue(); assertThat(stateChangedEvent.getCommandId(), is(commandId)); assertThat(stateChangedEvent.getSystemCommand(), is(suspensionCommand)); assertThat(stateChangedEvent.getCommandState(), is(COMMAND_FAILED)); assertThat(stateChangedEvent.getStatusChangedAt(), is(stateChangedAt)); assertThat(stateChangedEvent.getMessage(), is("SUSPEND failed. The following Suspendables failed: [Suspendable_1, Suspendable_2]")); }