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

Commit

Permalink
Merge 7407152 into 9e8e31f
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmckenzie committed Sep 8, 2019
2 parents 9e8e31f + 7407152 commit 1131917
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to

## [Unreleased]

## [2.0.6] 2019-09-08
### Changed
- Update to framework 6.0.12
- Improved server error handling
- Now prints the stack trace from the server on any server error

## [2.0.5] 2019-08-30
### Changed
- Update to framework 6.0.11
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.0.1</framework-api.version>
<framework.version>6.0.11</framework.version>
<framework.version>6.0.12</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,6 +1,8 @@
package uk.gov.justice.framework.command.client.jmx;

import uk.gov.justice.framework.command.client.io.ToConsolePrinter;
import uk.gov.justice.services.jmx.api.SystemCommandFailedException;
import uk.gov.justice.services.jmx.api.UnsupportedSystemCommandException;
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;
Expand Down Expand Up @@ -41,6 +43,11 @@ public void runSystemCommand(final SystemCommand systemCommand, final JmxParamet

} 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);
} catch (final SystemCommandFailedException e) {
toConsolePrinter.printf("The command '%s' failed: %s", e.getMessage(), commandName);
toConsolePrinter.println(e.getServerStackTrace());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import uk.gov.justice.framework.command.client.io.ToConsolePrinter;
import uk.gov.justice.services.jmx.api.SystemCommandFailedException;
import uk.gov.justice.services.jmx.api.UnsupportedSystemCommandException;
import uk.gov.justice.services.jmx.api.command.PingCommand;
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;
Expand Down Expand Up @@ -115,7 +119,7 @@ public void shouldLogIfUsingCredentials() throws Exception {
}

@Test
public void shoulLogAndReturnEmptyIfAuthenticationFails() throws Exception {
public void shouldLogIfAuthenticationFails() throws Exception {

final String contextName = "my-context";
final String host = "localhost";
Expand Down Expand Up @@ -147,4 +151,83 @@ public void shoulLogAndReturnEmptyIfAuthenticationFails() throws Exception {
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");
}

@Test
public void shouldLogIfTheCommandIsUnsupported() throws Exception {

final String contextName = "secret";
final String host = "localhost";
final int port = 92834;
final String username = "Fred";

final UnsupportedSystemCommandException unsupportedSystemCommandException = new UnsupportedSystemCommandException("Ooops");

final SystemCommand systemCommand = new PingCommand();
final Credentials credentials = mock(Credentials.class);
final JmxParameters jmxParameters = mock(JmxParameters.class);
final SystemCommanderClient systemCommanderClient = mock(SystemCommanderClient.class);
final SystemCommanderMBean systemCommanderMBean = mock(SystemCommanderMBean.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)).thenReturn(systemCommanderClient);
when(systemCommanderClient.getRemote(contextName)).thenReturn(systemCommanderMBean);
doThrow(unsupportedSystemCommandException).when(systemCommanderMBean).call(systemCommand);

systemCommandInvoker.runSystemCommand(systemCommand, jmxParameters);

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).printf("Connected to %s context", contextName);
inOrder.verify(toConsolePrinter).printf("The command '%s' is not supported on this %s context", systemCommand.getName(), contextName);
}

@Test
public void shouldLogAndPrintTheServerStackTraceIfTheCommandFails() throws Exception {

final String contextName = "secret";
final String host = "localhost";
final int port = 92834;
final String username = "Fred";
final String serverStackTrace = "the stack trace from the server";
final String errorMessage = "Ooops";

final SystemCommandFailedException systemCommandFailedException = new SystemCommandFailedException(errorMessage, serverStackTrace);

final SystemCommand systemCommand = new PingCommand();
final Credentials credentials = mock(Credentials.class);
final JmxParameters jmxParameters = mock(JmxParameters.class);
final SystemCommanderClient systemCommanderClient = mock(SystemCommanderClient.class);
final SystemCommanderMBean systemCommanderMBean = mock(SystemCommanderMBean.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)).thenReturn(systemCommanderClient);
when(systemCommanderClient.getRemote(contextName)).thenReturn(systemCommanderMBean);
doThrow(systemCommandFailedException).when(systemCommanderMBean).call(systemCommand);

systemCommandInvoker.runSystemCommand(systemCommand, jmxParameters);

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).printf("Connected to %s context", contextName);
inOrder.verify(toConsolePrinter).printf("The command '%s' failed: %s", errorMessage, systemCommand.getName());
inOrder.verify(toConsolePrinter).println(serverStackTrace);
}
}

0 comments on commit 1131917

Please sign in to comment.