SystemCommandStateBean { @Transactional(REQUIRES_NEW) public boolean commandInProgress(final SystemCommand systemCommand) { final Optional<SystemCommandStatus> latestStatusByType = systemCommandStatusRepository.findLatestStatusByType(systemCommand); if (latestStatusByType.isPresent()) { final CommandState commandState = latestStatusByType.get().getCommandState(); return commandState == COMMAND_RECEIVED || commandState == COMMAND_IN_PROGRESS; } return false; }  @Transactional(REQUIRES_NEW) void addSystemCommandState(final SystemCommandStatus systemCommandStatus); @Transactional(REQUIRES_NEW) Optional<SystemCommandStatus> getCommandStatus(final UUID commandId); @Transactional(REQUIRES_NEW) boolean commandInProgress(final SystemCommand systemCommand);  }
@Test public void shouldDetermineIfACommandIsInProgress() throws Exception { final SystemCommand commandReceivedCommand = mock(SystemCommand.class); final SystemCommand commandInProgressCommand = mock(SystemCommand.class); final SystemCommand commandCompleteCommand = mock(SystemCommand.class); final SystemCommand commandFailedCommand = mock(SystemCommand.class); final SystemCommand notFoundCommand = mock(SystemCommand.class); final SystemCommandStatus commandReceivedStatus = mock(SystemCommandStatus.class); final SystemCommandStatus commandInProgressStatus = mock(SystemCommandStatus.class); final SystemCommandStatus commandCompleteStatus = mock(SystemCommandStatus.class); final SystemCommandStatus commandFailedStatus = mock(SystemCommandStatus.class); when(systemCommandStatusRepository.findLatestStatusByType(commandReceivedCommand)).thenReturn(of(commandReceivedStatus)); when(systemCommandStatusRepository.findLatestStatusByType(commandInProgressCommand)).thenReturn(of(commandInProgressStatus)); when(systemCommandStatusRepository.findLatestStatusByType(commandCompleteCommand)).thenReturn(of(commandCompleteStatus)); when(systemCommandStatusRepository.findLatestStatusByType(commandFailedCommand)).thenReturn(of(commandFailedStatus)); when(systemCommandStatusRepository.findLatestStatusByType(notFoundCommand)).thenReturn(empty()); when(commandReceivedStatus.getCommandState()).thenReturn(COMMAND_RECEIVED); when(commandInProgressStatus.getCommandState()).thenReturn(COMMAND_IN_PROGRESS); when(commandCompleteStatus.getCommandState()).thenReturn(COMMAND_COMPLETE); when(commandFailedStatus.getCommandState()).thenReturn(COMMAND_FAILED); assertThat(systemCommandStateBean.commandInProgress(commandReceivedCommand), is(true)); assertThat(systemCommandStateBean.commandInProgress(commandInProgressCommand), is(true)); assertThat(systemCommandStateBean.commandInProgress(commandCompleteCommand), is(false)); assertThat(systemCommandStateBean.commandInProgress(commandFailedCommand), is(false)); assertThat(systemCommandStateBean.commandInProgress(notFoundCommand), is(false)); }