Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Merge b76cba4 into 91c7e1b
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmckenzie committed Oct 16, 2019
2 parents 91c7e1b + b76cba4 commit 648cf2e
Show file tree
Hide file tree
Showing 15 changed files with 106 additions and 53 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
[Semantic Versioning](http://semver.org/).

## [Unreleased]
### Changed
- Changed return codes to include code for command failed. New codes are
- 0: Success
- 1: Authentication Failed
- 2: Connection Failed
- 3: Command Failed
- 4: Exception invoking System Command/Anything else

## [2.2.1] - 2019-10-16
### Changed
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<cpp.repo.name>framework-jmx-command-client</cpp.repo.name>

<framework-api.version>4.1.0</framework-api.version>
<framework.version>6.2.0</framework.version>
<framework.version>6.2.1-M1</framework.version>
<maven-common-bom.version>2.4.0</maven-common-bom.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package uk.gov.justice.framework.command.client;

import static uk.gov.justice.framework.command.client.ReturnCode.SUCCESS;

import uk.gov.justice.framework.command.client.cdi.producers.OptionsFactory;
import uk.gov.justice.framework.command.client.jmx.ListCommandsInvoker;
import uk.gov.justice.framework.command.client.startup.CommandLineArgumentParser;
Expand Down Expand Up @@ -37,7 +39,7 @@ public class MainApplication {
@Inject
private ReturnCodeFactory returnCodeFactory;

public int run(final String[] args) {
public ReturnCode run(final String[] args) {

final Optional<CommandLine> commandLineOptional = commandLineArgumentParser.parse(args);

Expand All @@ -60,6 +62,6 @@ public int run(final String[] args) {
formatter.printHelp("java -jar catchup-shuttering-manager.jar", optionsFactory.createOptions());
}

return 0;
return SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package uk.gov.justice.framework.command.client;

public enum ReturnCode {

SUCCESS(0),
AUTHENTICATION_FAILED(1),
CONNECTION_FAILED(2),
COMMAND_FAILED(3),
EXCEPTION_OCCURRED(4);

private int code;

ReturnCode(final int code) {
this.code = code;
}

public int getCode() {
return code;
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package uk.gov.justice.framework.command.client;

import static uk.gov.justice.framework.command.client.ReturnCode.AUTHENTICATION_FAILED;
import static uk.gov.justice.framework.command.client.ReturnCode.COMMAND_FAILED;
import static uk.gov.justice.framework.command.client.ReturnCode.CONNECTION_FAILED;
import static uk.gov.justice.framework.command.client.ReturnCode.EXCEPTION_OCCURRED;

import uk.gov.justice.framework.command.client.io.ToConsolePrinter;
import uk.gov.justice.services.jmx.api.SystemCommandFailedException;
import uk.gov.justice.services.jmx.api.SystemCommandInvocationFailedException;
import uk.gov.justice.services.jmx.system.command.client.MBeanClientConnectionException;
import uk.gov.justice.services.jmx.system.command.client.connection.JmxAuthenticationException;

import javax.inject.Inject;

public class ReturnCodeFactory {

private static final int AUTHENTICATION_CODE = 1;
private static final int CONNECTION_FAILED = 2;
private static final int EXCEPTION_OCCURRED = 3;

@Inject
private ToConsolePrinter toConsolePrinter;

public int createFor(final Exception exception) {
public ReturnCode createFor(final Exception exception) {

if (exception instanceof JmxAuthenticationException) {
toConsolePrinter.println("Authentication failed. Please ensure your username and password are correct");
return AUTHENTICATION_CODE;
return AUTHENTICATION_FAILED;
}

if (exception instanceof MBeanClientConnectionException) {
Expand All @@ -29,8 +31,12 @@ public int createFor(final Exception exception) {
}

if (exception instanceof SystemCommandFailedException) {
return COMMAND_FAILED;
}

if (exception instanceof SystemCommandInvocationFailedException) {
toConsolePrinter.printf(exception.getMessage());
toConsolePrinter.println(((SystemCommandFailedException) exception).getServerStackTrace());
toConsolePrinter.println(((SystemCommandInvocationFailedException) exception).getServerStackTrace());
return EXCEPTION_OCCURRED;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package uk.gov.justice.framework.command.client;

public class SystemCommandFailedException extends RuntimeException {

public SystemCommandFailedException(final String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_COMPLETE;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_FAILED;

import uk.gov.justice.framework.command.client.SystemCommandFailedException;
import uk.gov.justice.framework.command.client.io.ToConsolePrinter;
import uk.gov.justice.framework.command.client.util.UtcClock;
import uk.gov.justice.services.jmx.api.domain.CommandState;
Expand Down Expand Up @@ -46,10 +47,14 @@ public boolean commandComplete(final SystemCommanderMBean systemCommanderMBean,
final long durationMillis = between(startTime, clock.now()).toMillis();

final String duration = formatDurationHMS(durationMillis);
toConsolePrinter.println(format("ERROR: Command %s failed", commandStatus.getSystemCommandName()));
toConsolePrinter.println(format("ERROR: %s", commandStatus.getMessage()));
final String errorMessage = commandStatus.getMessage();
final String systemCommandName = commandStatus.getSystemCommandName();

toConsolePrinter.println(format("ERROR: Command %s failed", systemCommandName));
toConsolePrinter.println(format("ERROR: %s", errorMessage));
toConsolePrinter.println(format("%s duration %s (hours:minutes:seconds:milliseconds)", commandStatus.getSystemCommandName(), duration));
return true;

throw new SystemCommandFailedException(format("Comand %s failed. %s", systemCommandName, errorMessage));
}

return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package uk.gov.justice.framework.command.client.jmx;

import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_COMPLETE;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_FAILED;

import uk.gov.justice.framework.command.client.io.ToConsolePrinter;
import uk.gov.justice.services.jmx.api.SystemCommandFailedException;
import uk.gov.justice.services.jmx.api.SystemCommandInvocationFailedException;
import uk.gov.justice.services.jmx.api.UnsupportedSystemCommandException;
import uk.gov.justice.services.jmx.api.command.SystemCommand;
import uk.gov.justice.services.jmx.api.domain.CommandState;
import uk.gov.justice.services.jmx.api.domain.SystemCommandStatus;
import uk.gov.justice.services.jmx.api.mbean.SystemCommanderMBean;
import uk.gov.justice.services.jmx.system.command.client.SystemCommanderClient;
import uk.gov.justice.services.jmx.system.command.client.SystemCommanderClientFactory;
Expand Down Expand Up @@ -53,7 +48,7 @@ public void runSystemCommand(final SystemCommand systemCommand, final JmxParamet
} catch (final UnsupportedSystemCommandException e) {
toConsolePrinter.printf("The command '%s' is not supported on this %s context", commandName, contextName);
throw e;
} catch (final SystemCommandFailedException e) {
} catch (final SystemCommandInvocationFailedException e) {
toConsolePrinter.printf("The command '%s' failed: %s", e.getMessage(), commandName);
toConsolePrinter.println(e.getServerStackTrace());
throw e;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.justice.framework.command.client.startup;

import uk.gov.justice.framework.command.client.MainApplication;
import uk.gov.justice.framework.command.client.ReturnCode;
import uk.gov.justice.framework.command.client.cdi.producers.WeldFactory;

import org.jboss.weld.environment.se.WeldContainer;
Expand All @@ -18,7 +19,7 @@ public Bootstrapper(final WeldFactory weldFactory) {
this.weldFactory = weldFactory;
}

public int startContainerAndRun(final String[] args) {
public ReturnCode startContainerAndRun(final String[] args) {
try (final WeldContainer container = weldFactory.create().initialize()) {

final WeldInstance<MainApplication> weldInstance = container.select(MainApplication.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public class CatchUpAndShutteringManager {

public static void main(String... args) {
System.exit(new Bootstrapper().startContainerAndRun(args));
System.exit(new Bootstrapper().startContainerAndRun(args).getCode());
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package uk.gov.justice.framework.command.client;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static uk.gov.justice.framework.command.client.ReturnCode.SUCCESS;

import uk.gov.justice.framework.command.client.cdi.producers.WeldFactory;
import uk.gov.justice.framework.command.client.startup.Bootstrapper;

import org.hamcrest.CoreMatchers;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.jboss.weld.inject.WeldInstance;
Expand Down Expand Up @@ -42,11 +43,9 @@ public void shouldStartTheWeldCdiContainerGetTheMainApplicationClassAndRun() thr
when(weld.initialize()).thenReturn(container);
when(container.select(MainApplication.class)).thenReturn(weldInstance);
when(weldInstance.get()).thenReturn(mainApplication);
when(mainApplication.run(args)).thenReturn(0);
when(mainApplication.run(args)).thenReturn(SUCCESS);

final int resultCode = bootstrapper.startContainerAndRun(args);

assertThat(resultCode, CoreMatchers.is(0));
assertThat(bootstrapper.startContainerAndRun(args), is(SUCCESS));
verify(mainApplication).run(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static uk.gov.justice.framework.command.client.ReturnCode.AUTHENTICATION_FAILED;
import static uk.gov.justice.framework.command.client.ReturnCode.SUCCESS;

import uk.gov.justice.framework.command.client.cdi.producers.OptionsFactory;
import uk.gov.justice.framework.command.client.jmx.ListCommandsInvoker;
Expand Down Expand Up @@ -72,9 +74,7 @@ public void shouldLookupTheCorrectCommandAndInvokeIt() throws Exception {
when(jmxParametersFactory.createFrom(commandLine)).thenReturn(jmxParameters);
when(listCommandsInvoker.listSystemCommands(jmxParameters)).thenReturn(of(systemCommands));

final int result = mainApplication.run(args);

assertThat(result, is(0));
assertThat(mainApplication.run(args), is(SUCCESS));

verify(commandExecutor).executeCommand(commandLine, jmxParameters, systemCommands);
verifyZeroInteractions(formatter);
Expand Down Expand Up @@ -109,11 +109,9 @@ public void shouldRespondWithCorrectReturnCodeOnException() {
when(commandLineArgumentParser.parse(args)).thenReturn(of(commandLine));
when(jmxParametersFactory.createFrom(commandLine)).thenReturn(jmxParameters);
when(listCommandsInvoker.listSystemCommands(jmxParameters)).thenThrow(runtimeException);
when(returnCodeFactory.createFor(runtimeException)).thenReturn(1);

final int result = mainApplication.run(args);
when(returnCodeFactory.createFor(runtimeException)).thenReturn(AUTHENTICATION_FAILED);

assertThat(result, is(1));
assertThat(mainApplication.run(args), is(AUTHENTICATION_FAILED));

verifyZeroInteractions(commandExecutor);
verifyZeroInteractions(formatter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static uk.gov.justice.framework.command.client.ReturnCode.AUTHENTICATION_FAILED;
import static uk.gov.justice.framework.command.client.ReturnCode.COMMAND_FAILED;
import static uk.gov.justice.framework.command.client.ReturnCode.CONNECTION_FAILED;
import static uk.gov.justice.framework.command.client.ReturnCode.EXCEPTION_OCCURRED;

import uk.gov.justice.framework.command.client.io.ToConsolePrinter;
import uk.gov.justice.services.jmx.api.SystemCommandFailedException;
import uk.gov.justice.services.jmx.api.SystemCommandInvocationFailedException;
import uk.gov.justice.services.jmx.system.command.client.MBeanClientConnectionException;
import uk.gov.justice.services.jmx.system.command.client.connection.JmxAuthenticationException;

Expand All @@ -29,28 +34,30 @@ public void shouldReturnCorrectCodeForJmxAuthenticationException() {

final JmxAuthenticationException jmxAuthenticationException = new JmxAuthenticationException("Test", new Exception());

final int resultCode = returnCodeFactory.createFor(jmxAuthenticationException);

assertThat(resultCode, is(1));
assertThat(returnCodeFactory.createFor(jmxAuthenticationException), is(AUTHENTICATION_FAILED));
verify(toConsolePrinter).println("Authentication failed. Please ensure your username and password are correct");
}

@Test
public void shouldReturnCorrectCodeForMBeanClientConnectionException() {

final int resultCode = returnCodeFactory.createFor(new MBeanClientConnectionException("Test"));

assertThat(resultCode, is(2));
assertThat(returnCodeFactory.createFor(new MBeanClientConnectionException("Test")), is(CONNECTION_FAILED));

verify(toConsolePrinter).println("Test");
}

@Test
public void shouldReturnCorrectCodeForSystemCommandFailedException() {

final int resultCode = returnCodeFactory.createFor(new SystemCommandFailedException("Test", "Stack Trace"));
assertThat(returnCodeFactory.createFor(new SystemCommandFailedException("Test")), is(COMMAND_FAILED));

assertThat(resultCode, is(3));
verifyZeroInteractions(toConsolePrinter);
}

@Test
public void shouldReturnCorrectCodeForSystemCommandInvocationFailedException() {

assertThat(returnCodeFactory.createFor(new SystemCommandInvocationFailedException("Test", "Stack Trace")), is(EXCEPTION_OCCURRED));

verify(toConsolePrinter).printf("Test");
verify(toConsolePrinter).println("Stack Trace");
Expand All @@ -61,10 +68,8 @@ public void shouldReturnCorrectCodeForAnyOtherException() {

final Exception exception = new Exception("Test");

final int resultCode = returnCodeFactory.createFor(exception);

assertThat(resultCode, is(3));
assertThat(returnCodeFactory.createFor(exception), is(EXCEPTION_OCCURRED));

verify(toConsolePrinter).println(exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import static java.util.UUID.randomUUID;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_COMPLETE;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_FAILED;
import static uk.gov.justice.services.jmx.api.domain.CommandState.COMMAND_IN_PROGRESS;

import uk.gov.justice.framework.command.client.SystemCommandFailedException;
import uk.gov.justice.framework.command.client.io.ToConsolePrinter;
import uk.gov.justice.framework.command.client.util.UtcClock;
import uk.gov.justice.services.jmx.api.domain.SystemCommandStatus;
Expand Down Expand Up @@ -59,7 +61,7 @@ public void shouldLogAndReturnTrueIfTheCommandIsComplete() throws Exception {
}

@Test
public void shouldLogAndReturnTrueIfTheCommandFails() throws Exception {
public void shouldLogAndThrowExceptionfTheCommandFails() throws Exception {

final UUID commandId = randomUUID();

Expand All @@ -77,7 +79,12 @@ public void shouldLogAndReturnTrueIfTheCommandFails() throws Exception {
when(systemCommandStatus.getSystemCommandName()).thenReturn("CATCHUP");
when(systemCommandStatus.getMessage()).thenReturn(errorMessage);

assertThat(commandChecker.commandComplete(systemCommanderMBean, commandId, startTime), is(true));
try {
commandChecker.commandComplete(systemCommanderMBean, commandId, startTime);
fail();
} catch (final SystemCommandFailedException expected) {
assertThat(expected.getMessage(), is("Comand CATCHUP failed. CATCHUP failed with 23 errors"));
}

verify(toConsolePrinter).println("ERROR: Command CATCHUP failed");
verify(toConsolePrinter).println("ERROR: CATCHUP failed with 23 errors");
Expand Down
Loading

0 comments on commit 648cf2e

Please sign in to comment.