SystemCommandRunner { @Transactional(NEVER) public void run(final SystemCommand systemCommand, final UUID commandId) { try { systemCommandStore.findCommandProxy(systemCommand).invokeCommand(systemCommand, commandId); } catch (final Throwable e) { final String message = format("Failed to run System Command '%s'", systemCommand.getName()); logger.error(message, e); throw new SystemCommandInvocationFailedException( message + ". Caused by " + e.getClass().getName() + ": " + e.getMessage(), stackTraceProvider.getStackTrace(e)); } }  @Transactional(NEVER) void run(final SystemCommand systemCommand, final UUID commandId);  }
@Test public void shouldFindTheCorrectProxyForTheCommandAndInvoke() throws Exception { final UUID commandId = UUID.randomUUID(); final TestCommand testCommand = new TestCommand(); final SystemCommandHandlerProxy systemCommandHandlerProxy = mock(SystemCommandHandlerProxy.class); when(systemCommandStore.findCommandProxy(testCommand)).thenReturn(systemCommandHandlerProxy); systemCommandRunner.run(testCommand, commandId); verify(systemCommandHandlerProxy).invokeCommand(testCommand, commandId); }
@Test public void shouldThrowSystemCommandFailedExceptionIfCommandFails() throws Exception { final UUID commandId = UUID.randomUUID(); final TestCommand testCommand = new TestCommand(); final SystemCommandException systemCommandException = new SystemCommandException("Ooops"); final String stackTrace = "stack trace"; final SystemCommandHandlerProxy systemCommandHandlerProxy = mock(SystemCommandHandlerProxy.class); when(systemCommandStore.findCommandProxy(testCommand)).thenReturn(systemCommandHandlerProxy); doThrow(systemCommandException).when(systemCommandHandlerProxy).invokeCommand(testCommand, commandId); when(stackTraceProvider.getStackTrace(systemCommandException)).thenReturn(stackTrace); try { systemCommandRunner.run(testCommand, commandId); fail(); } catch (final SystemCommandInvocationFailedException expected) { assertThat(expected.getMessage(), is("Failed to run System Command 'TEST_COMMAND'. Caused by uk.gov.justice.services.jmx.api.SystemCommandException: Ooops")); assertThat(expected.getServerStackTrace(), is(stackTrace)); assertThat(expected.getCause(), is(nullValue())); } }