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

Commit

Permalink
Merge f2d9a64 into f9e0681
Browse files Browse the repository at this point in the history
  • Loading branch information
mapingo committed Sep 23, 2019
2 parents f9e0681 + f2d9a64 commit 5dae25c
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 94 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to

## [Unreleased]

## [2.0.9] - 2019-09-23
### Added
- Result code returned : 0 - completed, 1 - authentication failed, 2 - connection failed, 3 - other failure

## [2.0.8] - 2019-09-19
### Changed
- Update to framework 6.0.15
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.0.15</framework.version>
<framework.version>6.0.16</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
Expand Up @@ -34,7 +34,10 @@ public class MainApplication {
@Inject
private CommandExecutor commandExecutor;

public void run(final String[] args) {
@Inject
private ReturnCodeFactory returnCodeFactory;

public int run(final String[] args) {

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

Expand All @@ -44,11 +47,19 @@ public void run(final String[] args) {

final JmxParameters jmxParameters = jmxParametersFactory.createFrom(commandLine);

final Optional<List<SystemCommand>> systemCommandsOptional = listCommandsInvoker.listSystemCommands(jmxParameters);
systemCommandsOptional.ifPresent(systemCommands -> commandExecutor.executeCommand(commandLine, jmxParameters, systemCommands));
try {

final Optional<List<SystemCommand>> systemCommandsOptional = listCommandsInvoker.listSystemCommands(jmxParameters);
systemCommandsOptional.ifPresent(systemCommands -> commandExecutor.executeCommand(commandLine, jmxParameters, systemCommands));

} catch (final RuntimeException e) {
return returnCodeFactory.createFor(e);
}

} else {
formatter.printHelp("java -jar catchup-shuttering-manager.jar", optionsFactory.createOptions());
}

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

import uk.gov.justice.framework.command.client.io.ToConsolePrinter;
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) {

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

if (exception instanceof MBeanClientConnectionException) {
toConsolePrinter.println(exception.getMessage());
return CONNECTION_FAILED;
}

toConsolePrinter.println(exception.getMessage());
return EXCEPTION_OCCURRED;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package uk.gov.justice.framework.command.client.jmx;

import static java.util.Optional.empty;
import static java.util.Optional.of;

import uk.gov.justice.framework.command.client.io.ToConsolePrinter;
import uk.gov.justice.services.jmx.api.command.SystemCommand;
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;
import uk.gov.justice.services.jmx.system.command.client.connection.JmxAuthenticationException;
import uk.gov.justice.services.jmx.system.command.client.connection.JmxParameters;

import java.util.List;
Expand Down Expand Up @@ -39,10 +37,6 @@ public Optional<List<SystemCommand>> listSystemCommands(final JmxParameters jmxP
toConsolePrinter.printf("Connected to %s context", contextName);

return of(remote.listCommands());

} catch (final JmxAuthenticationException e) {
toConsolePrinter.println("Authentication failed. Please ensure your username and password are correct");
return empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ public void runSystemCommand(final SystemCommand systemCommand, final JmxParamet

toConsolePrinter.printf("System command '%s' successfully sent to %s", commandName, contextName);

} catch (final JmxAuthenticationException e) {
toConsolePrinter.println("Authentication failed. Please ensure your username and password are correct");
} catch (final UnsupportedSystemCommandException e) {
toConsolePrinter.printf("The command '%s' is not supported on this %s context", commandName, contextName);
throw e;
} catch (final SystemCommandFailedException e) {
toConsolePrinter.printf("The command '%s' failed: %s", e.getMessage(), commandName);
toConsolePrinter.println(e.getServerStackTrace());
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public Bootstrapper(final WeldFactory weldFactory) {
this.weldFactory = weldFactory;
}

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

final WeldInstance<MainApplication> weldInstance = container.select(MainApplication.class);
final MainApplication mainApplication = weldInstance.get();

mainApplication.run(args);
return mainApplication.run(args);
}
}
}
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) {
new Bootstrapper().startContainerAndRun(args);
System.exit(new Bootstrapper().startContainerAndRun(args));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package uk.gov.justice.framework.command.client;

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 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 All @@ -29,7 +31,7 @@ public class BootstrapperTest {
@Test
public void shouldStartTheWeldCdiContainerGetTheMainApplicationClassAndRun() throws Exception {

final String[] args = { "some", "args"};
final String[] args = {"some", "args"};

final Weld weld = mock(Weld.class);
final WeldContainer container = mock(WeldContainer.class);
Expand All @@ -40,9 +42,11 @@ 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);

bootstrapper.startContainerAndRun(args);
final int resultCode = bootstrapper.startContainerAndRun(args);

assertThat(resultCode, CoreMatchers.is(0));
verify(mainApplication).run(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static java.util.Arrays.asList;
import static java.util.Optional.empty;
import static java.util.Optional.of;
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.verifyZeroInteractions;
Expand Down Expand Up @@ -47,6 +49,9 @@ public class MainApplicationTest {
@Mock
private CommandExecutor commandExecutor;

@Mock
private ReturnCodeFactory returnCodeFactory;

@InjectMocks
private MainApplication mainApplication;

Expand All @@ -67,7 +72,9 @@ public void shouldLookupTheCorrectCommandAndInvokeIt() throws Exception {
when(jmxParametersFactory.createFrom(commandLine)).thenReturn(jmxParameters);
when(listCommandsInvoker.listSystemCommands(jmxParameters)).thenReturn(of(systemCommands));

mainApplication.run(args);
final int result = mainApplication.run(args);

assertThat(result, is(0));

verify(commandExecutor).executeCommand(commandLine, jmxParameters, systemCommands);
verifyZeroInteractions(formatter);
Expand All @@ -89,4 +96,26 @@ public void shouldPrintHelpIfParsingCommandFailsOrHelpIsSpecified() throws Excep

verifyZeroInteractions(commandExecutor);
}

@Test
public void shouldRespondWithCorrectReturnCodeOnException() {

final String[] args = {"some", "args"};
final JmxParameters jmxParameters = mock(JmxParameters.class);

final CommandLine commandLine = mock(CommandLine.class);
final RuntimeException runtimeException = new RuntimeException();

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);

assertThat(result, is(1));

verifyZeroInteractions(commandExecutor);
verifyZeroInteractions(formatter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package uk.gov.justice.framework.command.client;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;

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

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ReturnCodeFactoryTest {

@Mock
private ToConsolePrinter toConsolePrinter;

@InjectMocks
private ReturnCodeFactory returnCodeFactory;

@Test
public void shouldReturnCorrectCodeForJmxAuthenticationException() {

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

final int resultCode = returnCodeFactory.createFor(jmxAuthenticationException);

assertThat(resultCode, is(1));
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));

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

@Test
public void shouldReturnCorrectCodeForAnyOtherException() {

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

assertThat(resultCode, is(3));

verify(toConsolePrinter).println("Test");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
import uk.gov.justice.services.jmx.system.command.client.SystemCommanderClient;
import uk.gov.justice.services.jmx.system.command.client.SystemCommanderClientFactory;
import uk.gov.justice.services.jmx.system.command.client.connection.Credentials;
import uk.gov.justice.services.jmx.system.command.client.connection.JmxAuthenticationException;
import uk.gov.justice.services.jmx.system.command.client.connection.JmxParameters;

import java.io.IOException;
import java.util.List;

import org.junit.Test;
Expand Down Expand Up @@ -65,7 +63,7 @@ public void shouldMakeAJmxCallToRetrieveTheListOfCommands() throws Exception {

final InOrder inOrder = inOrder(
toConsolePrinter,
systemCommanderClientFactory,
systemCommanderClientFactory,
systemCommanderClient);

inOrder.verify(toConsolePrinter).printf("Connecting to %s context at '%s' on port %d", contextName, host, port);
Expand All @@ -75,7 +73,7 @@ public void shouldMakeAJmxCallToRetrieveTheListOfCommands() throws Exception {
}

@Test
public void shoulLogIfUsingCredentials() throws Exception {
public void shouldLogIfUsingCredentials() throws Exception {

final String contextName = "my-context";
final String host = "localhost";
Expand Down Expand Up @@ -111,37 +109,4 @@ public void shoulLogIfUsingCredentials() throws Exception {
inOrder.verify(systemCommanderClient).getRemote(contextName);
inOrder.verify(toConsolePrinter).printf("Connected to %s context", contextName);
}

@Test
public void shoulLogAndReturnEmptyIfAuthenticationFails() throws Exception {

final String contextName = "my-context";
final String host = "localhost";
final int port = 92834;
final String username = "Fred";

final JmxAuthenticationException jmxAuthenticationException = new JmxAuthenticationException("Ooops", new IOException());

final Credentials credentials = mock(Credentials.class);
final JmxParameters jmxParameters = mock(JmxParameters.class);
final SystemCommanderClient systemCommanderClient = mock(SystemCommanderClient.class);

when(jmxParameters.getContextName()).thenReturn(contextName);
when(jmxParameters.getHost()).thenReturn(host);
when(jmxParameters.getPort()).thenReturn(port);
when(jmxParameters.getCredentials()).thenReturn(of(credentials));
when(credentials.getUsername()).thenReturn(username);
when(systemCommanderClientFactory.create(jmxParameters)).thenThrow(jmxAuthenticationException);

assertThat(listCommandsInvoker.listSystemCommands(jmxParameters), is(empty()));

final InOrder inOrder = inOrder(
toConsolePrinter,
systemCommanderClientFactory,
systemCommanderClient);

inOrder.verify(toConsolePrinter).printf("Connecting to %s context at '%s' on port %d", contextName, host, port);
inOrder.verify(toConsolePrinter).printf("Connecting with credentials for user '%s'", username);
inOrder.verify(toConsolePrinter).println("Authentication failed. Please ensure your username and password are correct");
}
}
Loading

0 comments on commit 5dae25c

Please sign in to comment.