SystemCommander implements SystemCommanderMBean { @Override public UUID call(final String systemCommandName) { logger.info(format("Received System Command '%s'", systemCommandName)); final SystemCommand systemCommand = systemCommandLocator .forName(systemCommandName) .orElseThrow(() -> new UnrunnableSystemCommandException(format("The system command '%s' is not supported on this context.", systemCommandName))); if (systemCommandStateBean.commandInProgress(systemCommand)) { throw new UnrunnableSystemCommandException(format("Cannot run system command '%s'. A previous call to that command is still in progress.", systemCommand.getName())); } return asynchronousCommandRunner.run(systemCommand); }  @Override UUID call(final String systemCommandName); @Override List<SystemCommandDetails> listCommands(); @Override SystemCommandStatus getCommandStatus(final UUID commandId);  }
@Test public void shouldRunTheSystemCommandIfSupported() throws Exception { final UUID commandId = randomUUID(); final TestCommand testCommand = new TestCommand(); when(systemCommandLocator.forName(testCommand.getName())).thenReturn(of(testCommand)); when(asynchronousCommandRunner.run(testCommand)).thenReturn(commandId); assertThat(systemCommander.call("TEST_COMMAND"), is(commandId)); final InOrder inOrder = inOrder(logger, asynchronousCommandRunner); inOrder.verify(logger).info("Received System Command 'TEST_COMMAND'"); inOrder.verify(asynchronousCommandRunner).run(testCommand); }
@Test public void shouldFailIfSystemCommandNotSupported() throws Exception { final TestCommand testCommand = new TestCommand(); when(systemCommandLocator.forName(testCommand.getName())).thenReturn(empty()); try { systemCommander.call("TEST_COMMAND"); fail(); } catch (final UnrunnableSystemCommandException expected) { assertThat(expected.getMessage(), is("The system command 'TEST_COMMAND' is not supported on this context.")); } }
@Test public void shouldFailIfPreviousSystemCommandIsInProgress() throws Exception { final TestCommand testCommand = new TestCommand(); when(systemCommandLocator.forName(testCommand.getName())).thenReturn(of(testCommand)); when(systemCommandStateBean.commandInProgress(testCommand)).thenReturn(true); try { systemCommander.call("TEST_COMMAND"); fail(); } catch (final UnrunnableSystemCommandException expected) { assertThat(expected.getMessage(), is("Cannot run system command 'TEST_COMMAND'. A previous call to that command is still in progress.")); } }