diff --git a/CHANGELOG.md b/CHANGELOG.md index d299b89..4d055a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,14 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added +- New command '--list' which lists the commands available on your Wildfly instance. +- Authentication to a remote Wildfly instance with commands '--username' and '--password' +### Changed +- Application now run in a Weld container to allow for dependency injection ## [1.0.0] - 2019-06-19 ### Added - Initial release of the catchup-shuttering-manager CLI tool, supporting invocation of the event sourcing frameworks SHUTTER, UNSHUTTER, CATCHUP, REBUILD operations. + diff --git a/README.md b/README.md index 3c13d33..7a8239a 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,19 @@ ## Usage catchup-shuttering-manager - -c,--command Framework command to execute, for example - CATCHUP,SHUTTER, UNSHUTTER & REBUILD. - -h,--help show help. - -ho,--host Host Remote or Localhost - -p,--port Wildfly management port + -c,--command Framework command to execute. + -h,--help Show help. + -ho,--host Hostname or IP address of the Wildfly server. Defaults to localhost + -p,--port Wildfly management port. Defaults to 9990 (the default for Wildfly) + -u, --username Optional username for Wildfly management security + -pw, --password Optional password for Wildfly management security + -l, --list List of all framework commands - java -jar catchup-shuttering-manager.jar -c "SHUTTER" -ho "localhost" -p "9999" + + java -jar catchup-shuttering-manager.jar -c "PING" + + Note: If you are running wildfly on your local machine and running as the same user as the one you are + logged in as, then username/password are not required. (However, if running wildfly in a virtual machine + like vagrant, username and password will be required) + + The default username/password for Wildfly is admin/admin diff --git a/pom.xml b/pom.xml index b611884..6290567 100644 --- a/pom.xml +++ b/pom.xml @@ -1,153 +1,171 @@ - - 4.0.0 - - - uk.gov.justice - maven-parent-pom - 1.7.1 - - - catchup-shuttering-manager - 1.0.0-SNAPSHOT - - - catchup-shuttering-manager - - 6.0.0-M30 - 2.0.0-M35 - 2.0.0-M15 - 4.0.0-M24 - 1.2 - - - - ${cpp.scm.connection} - ${cpp.scm.developerConnection} - ${cpp.scm.url} - - - - - + + 4.0.0 + + uk.gov.justice - maven-common-bom - 1.28.0 - import - pom - + maven-parent-pom + 1.7.1 + + + catchup-shuttering-manager + 1.0.0-SNAPSHOT + + + catchup-shuttering-manager + + 4.0.1 + 6.0.0-RC4 + 2.0.0-RC5 + 2.0.0-RC4 + 2.4.0 + + 1.2 + 10.0.0.Final + + + + ${cpp.scm.connection} + ${cpp.scm.developerConnection} + ${cpp.scm.url} + + + + + + uk.gov.justice + maven-common-bom + ${maven-common-bom.version} + import + pom + + + + + + + + uk.gov.justice.services + jmx-command-client + ${framework.version} + + + uk.gov.justice.services + framework-management + ${framework.version} + + + org.slf4j + log4j-over-slf4j + + + log4j + log4j + + + uk.gov.justice.event-store + event-store-management + ${event-store.version} + + + commons-cli + commons-cli + + + javax + javaee-api + + + org.jboss.weld.se + weld-se + + + uk.gov.justice.event-store + event-buffer-core + ${event-store.version} + + + org.wildfly + wildfly-client-all + + + + + + org.mockito + mockito-core + test + + + + org.hamcrest + hamcrest-library + test + + + - - - - - - org.slf4j - slf4j-api - compile - - - - uk.gov.justice.services - jmx-command-client - ${framework.version} - - - - uk.gov.justice.services - framework-management - ${framework.version} - - - - uk.gov.justice.event-store - event-store-management - ${event-store.version} - - - - commons-cli - commons-cli - ${commons.cli.version} - - - - javax - javaee-api - provided - - - - org.mockito - mockito-core - test - - - - org.hamcrest - hamcrest-library - test - - - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - ${argLine} -Xmx64m - - - - - - - - - org.apache.maven.plugins - maven-assembly-plugin - - - - jar-with-dependencies - - ${project.name} - false - - - - uk.gov.justice.CatchUpAndShutteringManager - - - - - - - make-assembly - - package - - single - - - - - - - org.eluder.coveralls - coveralls-maven-plugin - - - org.apache.maven.plugins - maven-failsafe-plugin - - - - + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + ${argLine} -Xmx64m + + + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + + jar-with-dependencies + + ${project.name} + false + + + + + uk.gov.justice.framework.command.client.startup.CatchUpAndShutteringManager + + + + + + + + make-assembly + + package + + single + + + + + + + org.eluder.coveralls + coveralls-maven-plugin + + + org.apache.maven.plugins + maven-failsafe-plugin + + + + diff --git a/src/main/java/uk/gov/justice/CatchUpAndShutteringManager.java b/src/main/java/uk/gov/justice/CatchUpAndShutteringManager.java deleted file mode 100644 index e327c17..0000000 --- a/src/main/java/uk/gov/justice/CatchUpAndShutteringManager.java +++ /dev/null @@ -1,39 +0,0 @@ -package uk.gov.justice; - -import static org.slf4j.LoggerFactory.getLogger; - -import uk.gov.justice.framework.command.tools.CommandLineArgumentParser; -import uk.gov.justice.framework.command.tools.SystemCommandInvoker; - -import javax.enterprise.context.ApplicationScoped; - -import org.slf4j.Logger; - -@ApplicationScoped -public class CatchUpAndShutteringManager { - - private static final Logger logger = getLogger(CatchUpAndShutteringManager.class.getName()); - - /** - * @param args operation, host, port - */ - public static void main(String... args) { - final CommandLineArgumentParser commandLineArgumentParser = new CommandLineArgumentParser(args); - final boolean argumentParsed = commandLineArgumentParser.parse(); - - if(argumentParsed){ - final String command = args[0]; - final String host = args[1]; - final String port = args[2]; - - final SystemCommandInvoker systemCommandInvoker = new SystemCommandInvoker(); - - systemCommandInvoker.runSystemCommand(command, host, port); - - logger.info("java -jar catchup-shuttering-manager.jar -c -{} -ho -{} -p -{} invoked successfully", command, host, port); - } - else{ - logger.error("java -jar catchup-shuttering-manager.jar not invoked successfully!!! Please check command line arguments & usage"); - } - } -} diff --git a/src/main/java/uk/gov/justice/Operation.java b/src/main/java/uk/gov/justice/Operation.java deleted file mode 100644 index 2e93539..0000000 --- a/src/main/java/uk/gov/justice/Operation.java +++ /dev/null @@ -1,3 +0,0 @@ -package uk.gov.justice; - -public enum Operation {SHUTTER, UNSHUTTER, CATCHUP, REBUILD, UNKNOWN} diff --git a/src/main/java/uk/gov/justice/framework/command/client/CommandExecutor.java b/src/main/java/uk/gov/justice/framework/command/client/CommandExecutor.java new file mode 100644 index 0000000..c416acf --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/CommandExecutor.java @@ -0,0 +1,49 @@ +package uk.gov.justice.framework.command.client; + +import uk.gov.justice.framework.command.client.io.CommandPrinter; +import uk.gov.justice.framework.command.client.io.ToConsolePrinter; +import uk.gov.justice.framework.command.client.jmx.SystemCommandInvoker; +import uk.gov.justice.services.jmx.command.SystemCommand; +import uk.gov.justice.services.jmx.system.command.client.connection.JmxParameters; + +import java.util.List; +import java.util.Optional; + +import javax.inject.Inject; + +import org.apache.commons.cli.CommandLine; + +public class CommandExecutor { + + @Inject + private SystemCommandInvoker systemCommandInvoker; + + @Inject + private CommandLocator commandLocator; + + @Inject + private ToConsolePrinter toConsolePrinter; + + @Inject + private CommandPrinter commandPrinter; + + public void executeCommand( + final CommandLine commandLine, + final JmxParameters jmxParameters, + final List systemCommands) { + + if (commandLine.hasOption("list")) { + commandPrinter.printSystemCommands(systemCommands); + } else { + final String commandName = commandLine.getOptionValue("command"); + final Optional command = commandLocator.lookupCommand(commandName, systemCommands); + + if (command.isPresent()) { + systemCommandInvoker.runSystemCommand(command.get(), jmxParameters); + } else { + toConsolePrinter.printf("No command found with name '%s'", commandName); + commandPrinter.printSystemCommands(systemCommands); + } + } + } +} diff --git a/src/main/java/uk/gov/justice/framework/command/client/CommandLineException.java b/src/main/java/uk/gov/justice/framework/command/client/CommandLineException.java new file mode 100644 index 0000000..8d72c4c --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/CommandLineException.java @@ -0,0 +1,12 @@ +package uk.gov.justice.framework.command.client; + +public class CommandLineException extends RuntimeException { + + public CommandLineException(final String message) { + super(message); + } + + public CommandLineException(final String message, final Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/uk/gov/justice/framework/command/client/CommandLocator.java b/src/main/java/uk/gov/justice/framework/command/client/CommandLocator.java new file mode 100644 index 0000000..52eb397 --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/CommandLocator.java @@ -0,0 +1,17 @@ +package uk.gov.justice.framework.command.client; + +import uk.gov.justice.services.jmx.command.SystemCommand; + +import java.util.List; +import java.util.Optional; + +public class CommandLocator { + + + public Optional lookupCommand(final String commandName, final List systemCommands) { + + return systemCommands.stream() + .filter(systemCommand -> systemCommand.getName().equals(commandName)) + .findFirst(); + } +} diff --git a/src/main/java/uk/gov/justice/framework/command/client/JmxParametersFactory.java b/src/main/java/uk/gov/justice/framework/command/client/JmxParametersFactory.java new file mode 100644 index 0000000..6fde767 --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/JmxParametersFactory.java @@ -0,0 +1,50 @@ +package uk.gov.justice.framework.command.client; + +import static java.lang.Integer.parseInt; +import static java.lang.String.format; +import static uk.gov.justice.services.jmx.system.command.client.connection.JmxParametersBuilder.jmxParameters; + +import uk.gov.justice.services.jmx.system.command.client.connection.JmxParameters; +import uk.gov.justice.services.jmx.system.command.client.connection.JmxParametersBuilder; + +import org.apache.commons.cli.CommandLine; + +public class JmxParametersFactory { + + private static final String DEFAULT_HOST = "localhost"; + private static final String DEFAULT_PORT = "9990"; + + public JmxParameters createFrom(final CommandLine commandLine) { + + final String host = commandLine.getOptionValue("host", DEFAULT_HOST); + final int port = getPort(commandLine); + + final JmxParametersBuilder jmxParameters = jmxParameters() + .withHost(host) + .withPort(port); + + if (commandLine.hasOption("username")) { + + final String username = commandLine.getOptionValue("username"); + final String password = commandLine.getOptionValue("password"); + + jmxParameters + .withUsername(username) + .withPassword(password); + } + + return jmxParameters.build(); + } + + private int getPort(final CommandLine commandLine) { + + final String portString = commandLine.getOptionValue("port", DEFAULT_PORT); + + try { + return parseInt(portString); + + } catch (final NumberFormatException e) { + throw new CommandLineException(format("Port number '%s' is not a number", portString)); + } + } +} diff --git a/src/main/java/uk/gov/justice/framework/command/client/MainApplication.java b/src/main/java/uk/gov/justice/framework/command/client/MainApplication.java new file mode 100644 index 0000000..db20c02 --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/MainApplication.java @@ -0,0 +1,54 @@ +package uk.gov.justice.framework.command.client; + +import uk.gov.justice.framework.command.client.jmx.ListCommandsInvoker; +import uk.gov.justice.framework.command.client.startup.CommandLineArgumentParser; +import uk.gov.justice.services.jmx.command.SystemCommand; +import uk.gov.justice.services.jmx.system.command.client.connection.JmxParameters; + +import java.util.List; +import java.util.Optional; + +import javax.inject.Inject; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Options; + +public class MainApplication { + + @Inject + private CommandLineArgumentParser commandLineArgumentParser; + + @Inject + private JmxParametersFactory jmxParametersFactory; + + @Inject + private ListCommandsInvoker listCommandsInvoker; + + @Inject + private Options options; + + @Inject + private HelpFormatter formatter; + + @Inject + private CommandExecutor commandExecutor; + + public void run(final String[] args) { + + final Optional commandLineOptional = commandLineArgumentParser.parse(args); + + if (commandLineOptional.isPresent()) { + + final CommandLine commandLine = commandLineOptional.get(); + + final JmxParameters jmxParameters = jmxParametersFactory.createFrom(commandLine); + + final Optional> systemCommandsOptional = listCommandsInvoker.listSystemCommands(jmxParameters); + systemCommandsOptional.ifPresent(systemCommands -> commandExecutor.executeCommand(commandLine, jmxParameters, systemCommands)); + + } else { + formatter.printHelp("java -jar catchup-shuttering-manager.jar", options); + } + } +} diff --git a/src/main/java/uk/gov/justice/framework/command/client/cdi/producers/CommandLineParserProducer.java b/src/main/java/uk/gov/justice/framework/command/client/cdi/producers/CommandLineParserProducer.java new file mode 100644 index 0000000..4fa3021 --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/cdi/producers/CommandLineParserProducer.java @@ -0,0 +1,14 @@ +package uk.gov.justice.framework.command.client.cdi.producers; + +import javax.enterprise.inject.Produces; + +import org.apache.commons.cli.BasicParser; +import org.apache.commons.cli.CommandLineParser; + +public class CommandLineParserProducer { + + @Produces + public CommandLineParser commandLineParser() { + return new BasicParser(); + } +} diff --git a/src/main/java/uk/gov/justice/framework/command/client/cdi/producers/HelpFormatterProducer.java b/src/main/java/uk/gov/justice/framework/command/client/cdi/producers/HelpFormatterProducer.java new file mode 100644 index 0000000..e671a7e --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/cdi/producers/HelpFormatterProducer.java @@ -0,0 +1,13 @@ +package uk.gov.justice.framework.command.client.cdi.producers; + +import javax.enterprise.inject.Produces; + +import org.apache.commons.cli.HelpFormatter; + +public class HelpFormatterProducer { + + @Produces + public HelpFormatter helpFormatter() { + return new HelpFormatter(); + } +} diff --git a/src/main/java/uk/gov/justice/framework/command/client/cdi/producers/OptionsProducer.java b/src/main/java/uk/gov/justice/framework/command/client/cdi/producers/OptionsProducer.java new file mode 100644 index 0000000..82e627a --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/cdi/producers/OptionsProducer.java @@ -0,0 +1,24 @@ +package uk.gov.justice.framework.command.client.cdi.producers; + +import javax.enterprise.inject.Produces; + +import org.apache.commons.cli.Options; + +public class OptionsProducer { + + @Produces + public Options options() { + final Options options = new Options(); + + options.addOption("c", "command", true, "Framework command to execute. Run with --list for a list of all commands"); + options.addOption("h", "host", true, "Hostname or IP address of the Wildfly server. Defaults to localhost"); + options.addOption("p", "port", true, "Wildfly management port. Defaults to 9990"); + options.addOption("u", "username", true, "Optional username for Wildfly management security"); + options.addOption("pw", "password", true, "Optional password for Wildfly management security"); + + options.addOption("help", false, "Show help."); + options.addOption("l", "list", false, "List of all framework commands"); + + return options; + } +} diff --git a/src/main/java/uk/gov/justice/framework/command/client/cdi/producers/WeldFactory.java b/src/main/java/uk/gov/justice/framework/command/client/cdi/producers/WeldFactory.java new file mode 100644 index 0000000..9b25ce4 --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/cdi/producers/WeldFactory.java @@ -0,0 +1,10 @@ +package uk.gov.justice.framework.command.client.cdi.producers; + +import org.jboss.weld.environment.se.Weld; + +public class WeldFactory { + + public Weld create() { + return new Weld(); + } +} diff --git a/src/main/java/uk/gov/justice/framework/command/client/cdi/workaround/WorkaroundToPleaseCdi.java b/src/main/java/uk/gov/justice/framework/command/client/cdi/workaround/WorkaroundToPleaseCdi.java new file mode 100644 index 0000000..1c33cdd --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/cdi/workaround/WorkaroundToPleaseCdi.java @@ -0,0 +1,11 @@ +package uk.gov.justice.framework.command.client.cdi.workaround; + +import uk.gov.justice.services.messaging.jms.JmsCommandHandlerDestinationNameProvider; + +public class WorkaroundToPleaseCdi implements JmsCommandHandlerDestinationNameProvider { + + @Override + public String destinationName() { + throw new UnsupportedOperationException("This class only exists to keep CDI happy and should never be called"); + } +} diff --git a/src/main/java/uk/gov/justice/framework/command/client/io/CommandPrinter.java b/src/main/java/uk/gov/justice/framework/command/client/io/CommandPrinter.java new file mode 100644 index 0000000..38faeb5 --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/io/CommandPrinter.java @@ -0,0 +1,27 @@ +package uk.gov.justice.framework.command.client.io; + +import uk.gov.justice.services.jmx.command.SystemCommand; + +import java.util.List; + +import javax.inject.Inject; + +public class CommandPrinter { + + @Inject + private ToConsolePrinter toConsolePrinter; + + public void printSystemCommands(final List commands) { + + if (commands.isEmpty()) { + toConsolePrinter.println("This instance of wildfly does not support any system commands"); + } + + toConsolePrinter.printf("This instance of wildfly supports the following %d commands:", commands.size()); + commands.forEach(this::printCommand); + } + + private void printCommand(final SystemCommand systemCommand) { + toConsolePrinter.printf("\t- %s", systemCommand.getName()); + } +} diff --git a/src/main/java/uk/gov/justice/framework/command/client/io/ToConsolePrinter.java b/src/main/java/uk/gov/justice/framework/command/client/io/ToConsolePrinter.java new file mode 100644 index 0000000..c40099b --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/io/ToConsolePrinter.java @@ -0,0 +1,24 @@ +package uk.gov.justice.framework.command.client.io; + +import java.io.PrintStream; + +public class ToConsolePrinter { + + private final PrintStream printStream; + + public ToConsolePrinter() { + this(System.out); + } + + public ToConsolePrinter(final PrintStream printStream) { + this.printStream = printStream; + } + + public void printf(final String format, final Object... args) { + printStream.printf(format + "\n", args); + } + + public void println(final Object x) { + printStream.println(x); + } +} diff --git a/src/main/java/uk/gov/justice/framework/command/client/jmx/ListCommandsInvoker.java b/src/main/java/uk/gov/justice/framework/command/client/jmx/ListCommandsInvoker.java new file mode 100644 index 0000000..4cef4df --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/jmx/ListCommandsInvoker.java @@ -0,0 +1,46 @@ +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.command.SystemCommand; +import uk.gov.justice.services.jmx.command.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; +import java.util.Optional; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; + +@ApplicationScoped +public class ListCommandsInvoker { + + @Inject + private SystemCommanderClientFactory systemCommanderClientFactory; + + @Inject + private ToConsolePrinter toConsolePrinter; + + public Optional> listSystemCommands(final JmxParameters jmxParameters) { + + toConsolePrinter.printf("Connecting to Wildfly instance at '%s' on port %d", jmxParameters.getHost(), jmxParameters.getPort()); + jmxParameters.getCredentials().ifPresent(credentials -> toConsolePrinter.printf("Connecting with credentials for user '%s'", credentials.getUsername())); + + try (final SystemCommanderClient systemCommanderClient = systemCommanderClientFactory.create(jmxParameters)) { + final SystemCommanderMBean remote = systemCommanderClient.getRemote(); + + toConsolePrinter.println("Connected to remote server"); + + return of(remote.listCommands()); + + } catch (final JmxAuthenticationException e) { + toConsolePrinter.println("Authentication failed. Please ensure your username and password are correct"); + return empty(); + } + } +} diff --git a/src/main/java/uk/gov/justice/framework/command/client/jmx/SystemCommandInvoker.java b/src/main/java/uk/gov/justice/framework/command/client/jmx/SystemCommandInvoker.java new file mode 100644 index 0000000..1d5ace4 --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/jmx/SystemCommandInvoker.java @@ -0,0 +1,43 @@ +package uk.gov.justice.framework.command.client.jmx; + +import uk.gov.justice.framework.command.client.io.ToConsolePrinter; +import uk.gov.justice.services.jmx.command.SystemCommand; +import uk.gov.justice.services.jmx.command.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 javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; + +@ApplicationScoped +public class SystemCommandInvoker { + + @Inject + private SystemCommanderClientFactory systemCommanderClientFactory; + + @Inject + private ToConsolePrinter toConsolePrinter; + + public void runSystemCommand(final SystemCommand systemCommand, final JmxParameters jmxParameters) { + + toConsolePrinter.printf("Running system command '%s'", systemCommand.getName()); + + toConsolePrinter.printf("Connecting to Wildfly instance at '%s' on port %d", jmxParameters.getHost(), jmxParameters.getPort()); + jmxParameters.getCredentials().ifPresent(credentials -> toConsolePrinter.printf("Connecting with credentials for user '%s'", credentials.getUsername())); + + try (final SystemCommanderClient systemCommanderClient = systemCommanderClientFactory.create(jmxParameters)) { + final SystemCommanderMBean systemCommanderMBean = systemCommanderClient.getRemote(); + + toConsolePrinter.println("Connected to remote server"); + + systemCommanderMBean.call(systemCommand); + + toConsolePrinter.printf("System command '%s' successfully sent", systemCommand.getName()); + + } catch (final JmxAuthenticationException e) { + toConsolePrinter.println("Authentication failed. Please ensure your username and password are correct"); + } + } +} diff --git a/src/main/java/uk/gov/justice/framework/command/client/startup/Bootstrapper.java b/src/main/java/uk/gov/justice/framework/command/client/startup/Bootstrapper.java new file mode 100644 index 0000000..2691914 --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/startup/Bootstrapper.java @@ -0,0 +1,30 @@ +package uk.gov.justice.framework.command.client.startup; + +import uk.gov.justice.framework.command.client.MainApplication; +import uk.gov.justice.framework.command.client.cdi.producers.WeldFactory; + +import org.jboss.weld.environment.se.WeldContainer; +import org.jboss.weld.inject.WeldInstance; + +public class Bootstrapper { + + private final WeldFactory weldFactory; + + public Bootstrapper() { + this(new WeldFactory()); + } + + public Bootstrapper(final WeldFactory weldFactory) { + this.weldFactory = weldFactory; + } + + public void startContainerAndRun(final String[] args) { + try (final WeldContainer container = weldFactory.create().initialize()) { + + final WeldInstance weldInstance = container.select(MainApplication.class); + final MainApplication mainApplication = weldInstance.get(); + + mainApplication.run(args); + } + } +} diff --git a/src/main/java/uk/gov/justice/framework/command/client/startup/CatchUpAndShutteringManager.java b/src/main/java/uk/gov/justice/framework/command/client/startup/CatchUpAndShutteringManager.java new file mode 100644 index 0000000..b534a55 --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/startup/CatchUpAndShutteringManager.java @@ -0,0 +1,8 @@ +package uk.gov.justice.framework.command.client.startup; + +public class CatchUpAndShutteringManager { + + public static void main(String... args) { + new Bootstrapper().startContainerAndRun(args); + } +} diff --git a/src/main/java/uk/gov/justice/framework/command/client/startup/CommandLineArgumentParser.java b/src/main/java/uk/gov/justice/framework/command/client/startup/CommandLineArgumentParser.java new file mode 100644 index 0000000..5165a8d --- /dev/null +++ b/src/main/java/uk/gov/justice/framework/command/client/startup/CommandLineArgumentParser.java @@ -0,0 +1,50 @@ +package uk.gov.justice.framework.command.client.startup; + +import static java.lang.String.format; +import static java.util.Arrays.asList; +import static java.util.Optional.empty; +import static java.util.Optional.of; + +import uk.gov.justice.framework.command.client.CommandLineException; +import uk.gov.justice.framework.command.client.io.ToConsolePrinter; + +import java.util.Optional; + +import javax.inject.Inject; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; + +public class CommandLineArgumentParser { + + @Inject + private ToConsolePrinter toConsolePrinter; + + @Inject + private Options options; + + @Inject + private CommandLineParser commandLineParser; + + public Optional parse(final String[] args) { + + try { + final CommandLine commandLine = commandLineParser.parse(options, args); + + if (commandLine.hasOption("command") || commandLine.hasOption("list")) { + return of(commandLine); + } + + toConsolePrinter.println("No system command specifed."); + + return empty(); + + } catch (final ParseException e) { + throw new CommandLineException(format("Failed to parse command line args '%s'", asList(args)), e); + } + + } + +} diff --git a/src/main/java/uk/gov/justice/framework/command/tools/CommandLineArgumentParser.java b/src/main/java/uk/gov/justice/framework/command/tools/CommandLineArgumentParser.java deleted file mode 100644 index f05f1a2..0000000 --- a/src/main/java/uk/gov/justice/framework/command/tools/CommandLineArgumentParser.java +++ /dev/null @@ -1,78 +0,0 @@ -package uk.gov.justice.framework.command.tools; - -import static org.apache.commons.lang3.StringUtils.isNotBlank; -import static org.slf4j.LoggerFactory.getLogger; - -import org.apache.commons.cli.BasicParser; -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.CommandLineParser; -import org.apache.commons.cli.HelpFormatter; -import org.apache.commons.cli.Options; -import org.apache.commons.cli.ParseException; -import org.slf4j.Logger; - - -public class CommandLineArgumentParser { - - private static final Logger logger = getLogger(CommandLineArgumentParser.class.getName()); - - private String[] args; - private Options options = new Options(); - - - public CommandLineArgumentParser(String[] args) { - - this.args = args; - - options.addOption("h", "help", false, "show help."); - options.addOption("c", "command", true, "Framework command to execute, for example CATCHUP,SHUTTER, UNSHUTTER & REBUILD."); - options.addOption("ho", "host", true, "Host Remote or Localhost"); - options.addOption("p", "port", true, "Wildfly management port"); - } - - public boolean parse() { - final CommandLineParser parser = new BasicParser(); - - try { - final CommandLine cmd = parser.parse(options, args); - - if (cmd.hasOption("h")){ - help(); - } - - if (isCommandNullOrEmpty(cmd) && isHostNullOrEmpty(cmd) && isPortNullOrEmpty(cmd)) { - return true; - } else { - help(); - return false; - } - - } catch (final ParseException e) { - logger.info("Failed to parse command line properties", e); - help(); - return false; - } - } - - private void help() { - // This prints out some help - final HelpFormatter formatter = new HelpFormatter(); - - formatter.printHelp("java -jar catchup-shuttering-manager.jar", options); - } - - private boolean isCommandNullOrEmpty(final CommandLine cmd){ - return cmd.hasOption("c") && - (isNotBlank(cmd.getOptionValue("c"))); - } - - private boolean isHostNullOrEmpty(final CommandLine cmd){ - return cmd.hasOption("ho") && - isNotBlank(cmd.getOptionValue("ho")); - } - - private boolean isPortNullOrEmpty(final CommandLine cmd){ - return cmd.hasOption("p") && - isNotBlank(cmd.getOptionValue("p")); - } -} diff --git a/src/main/java/uk/gov/justice/framework/command/tools/EnumValidator.java b/src/main/java/uk/gov/justice/framework/command/tools/EnumValidator.java deleted file mode 100644 index 72d3fe5..0000000 --- a/src/main/java/uk/gov/justice/framework/command/tools/EnumValidator.java +++ /dev/null @@ -1,23 +0,0 @@ -package uk.gov.justice.framework.command.tools; - -import uk.gov.justice.Operation; - -import javax.enterprise.context.ApplicationScoped; - -@ApplicationScoped -public class EnumValidator { - - boolean checkCommandIsValid(final String command) { - return contains(Operation.class, command); - } - - private boolean contains(final Class clazz, final String val) { - final Enum[] arr = clazz.getEnumConstants(); - for (final Enum operation : arr) { - if (operation.name().equals(val)) { - return true; - } - } - return false; - } -} diff --git a/src/main/java/uk/gov/justice/framework/command/tools/SystemCommandInvoker.java b/src/main/java/uk/gov/justice/framework/command/tools/SystemCommandInvoker.java deleted file mode 100644 index 288c64e..0000000 --- a/src/main/java/uk/gov/justice/framework/command/tools/SystemCommandInvoker.java +++ /dev/null @@ -1,80 +0,0 @@ -package uk.gov.justice.framework.command.tools; - -import static java.lang.Integer.parseInt; -import static uk.gov.justice.Operation.*; - -import uk.gov.justice.Operation; -import uk.gov.justice.services.eventstore.management.catchup.commands.CatchupCommand; -import uk.gov.justice.services.eventstore.management.rebuild.commands.RebuildCommand; -import uk.gov.justice.services.jmx.command.BaseSystemCommand; -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.management.shuttering.command.ShutterSystemCommand; -import uk.gov.justice.services.management.shuttering.command.UnshutterSystemCommand; - -import javax.enterprise.context.ApplicationScoped; -import javax.inject.Inject; - -import org.slf4j.Logger; - -@ApplicationScoped -public class SystemCommandInvoker { - - private static final String UNKNOWN = "UNKNOWN"; - private String commandName; - - @Inject - private Logger logger; - - @Inject - private EnumValidator enumValidator; - - @Inject - private SystemCommanderClientFactoryHelper systemCommanderClientFactoryHelper; - - public void runSystemCommand(final String command, final String host, final String port) { - - final SystemCommanderClientFactory systemCommanderClientFactory = systemCommanderClientFactoryHelper.makeSystemCommanderClientFactory(); - - try (final SystemCommanderClient systemCommanderClient = systemCommanderClientFactory.create(host, parseInt(port))) { - - if (!enumValidator.checkCommandIsValid(command)) { - setUnKnownCommandName(command); - } else { - commandName = command; - } - - final Operation operation = valueOf(commandName); - switch (operation) { - case SHUTTER: - callSystemCommand(systemCommanderClient, new ShutterSystemCommand()); - break; - case UNSHUTTER: - callSystemCommand(systemCommanderClient, new UnshutterSystemCommand()); - break; - case CATCHUP: - callSystemCommand(systemCommanderClient, new CatchupCommand()); - break; - case REBUILD: - callSystemCommand(systemCommanderClient, new RebuildCommand()); - break; - default: - logger.info("{} is not a valid system command", command); - break; - } - } - - } - - private void setUnKnownCommandName(final String command) { - if (!command.equalsIgnoreCase(UNKNOWN)) { - commandName = UNKNOWN; - } - } - - private static void callSystemCommand(final SystemCommanderClient systemCommanderClient, final BaseSystemCommand systemCommand) { - systemCommanderClient - .getRemote() - .call(systemCommand); - } -} diff --git a/src/main/java/uk/gov/justice/framework/command/tools/SystemCommanderClientFactoryHelper.java b/src/main/java/uk/gov/justice/framework/command/tools/SystemCommanderClientFactoryHelper.java deleted file mode 100644 index 83d03a7..0000000 --- a/src/main/java/uk/gov/justice/framework/command/tools/SystemCommanderClientFactoryHelper.java +++ /dev/null @@ -1,10 +0,0 @@ -package uk.gov.justice.framework.command.tools; - -import uk.gov.justice.services.jmx.system.command.client.SystemCommanderClientFactory; - -public class SystemCommanderClientFactoryHelper { - - SystemCommanderClientFactory makeSystemCommanderClientFactory(){ - return new SystemCommanderClientFactory(); - } -} diff --git a/src/main/resources/META-INF/beans.xml b/src/main/resources/META-INF/beans.xml new file mode 100644 index 0000000..cc3f8d2 --- /dev/null +++ b/src/main/resources/META-INF/beans.xml @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml new file mode 100644 index 0000000..17fecc4 --- /dev/null +++ b/src/main/resources/log4j2.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/src/test/java/uk/gov/justice/CatchUpAndShutteringManagerTest.java b/src/test/java/uk/gov/justice/CatchUpAndShutteringManagerTest.java deleted file mode 100644 index 4c0da7d..0000000 --- a/src/test/java/uk/gov/justice/CatchUpAndShutteringManagerTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package uk.gov.justice; - -import static uk.gov.justice.CatchUpAndShutteringManager.main; - -import org.junit.Before; -import org.junit.Test; - -public class CatchUpAndShutteringManagerTest { - private String host; - private String port; - - @Before - public void before() { - host = "-localhost"; - port = "-9999"; - } - - @Test - public void shouldReturnErrorWhenInvokingMain() { - final String [] args = {"-ch", "SHUTTER", "-ho", host, "-p", port}; - - main(args); - } -} \ No newline at end of file diff --git a/src/test/java/uk/gov/justice/framework/command/client/BootstrapperTest.java b/src/test/java/uk/gov/justice/framework/command/client/BootstrapperTest.java new file mode 100644 index 0000000..61535c2 --- /dev/null +++ b/src/test/java/uk/gov/justice/framework/command/client/BootstrapperTest.java @@ -0,0 +1,48 @@ +package uk.gov.justice.framework.command.client; + +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.jboss.weld.environment.se.Weld; +import org.jboss.weld.environment.se.WeldContainer; +import org.jboss.weld.inject.WeldInstance; +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 BootstrapperTest { + + @Mock + private WeldFactory weldFactory; + + @InjectMocks + private Bootstrapper bootstrapper; + + @SuppressWarnings("unchecked") + @Test + public void shouldStartTheWeldCdiContainerGetTheMainApplicationClassAndRun() throws Exception { + + final String[] args = { "some", "args"}; + + final Weld weld = mock(Weld.class); + final WeldContainer container = mock(WeldContainer.class); + final WeldInstance weldInstance = mock(WeldInstance.class); + final MainApplication mainApplication = mock(MainApplication.class); + + when(weldFactory.create()).thenReturn(weld); + when(weld.initialize()).thenReturn(container); + when(container.select(MainApplication.class)).thenReturn(weldInstance); + when(weldInstance.get()).thenReturn(mainApplication); + + bootstrapper.startContainerAndRun(args); + + verify(mainApplication).run(args); + } +} diff --git a/src/test/java/uk/gov/justice/framework/command/client/CommandExecutorTest.java b/src/test/java/uk/gov/justice/framework/command/client/CommandExecutorTest.java new file mode 100644 index 0000000..5f09549 --- /dev/null +++ b/src/test/java/uk/gov/justice/framework/command/client/CommandExecutorTest.java @@ -0,0 +1,109 @@ +package uk.gov.justice.framework.command.client; + +import static java.util.Arrays.asList; +import static java.util.Optional.empty; +import static java.util.Optional.of; + +import org.apache.commons.cli.CommandLine; +import org.junit.runner.RunWith; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.mockito.Mockito.when; + +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.*; + +import uk.gov.justice.framework.command.client.io.CommandPrinter; +import uk.gov.justice.framework.command.client.io.ToConsolePrinter; +import uk.gov.justice.framework.command.client.jmx.SystemCommandInvoker; +import uk.gov.justice.services.jmx.command.SystemCommand; +import uk.gov.justice.services.jmx.system.command.client.connection.JmxParameters; + +import java.util.List; + + +@RunWith(MockitoJUnitRunner.class) +public class CommandExecutorTest { + + @Mock + private SystemCommandInvoker systemCommandInvoker; + + @Mock + private CommandLocator commandLocator; + + @Mock + private ToConsolePrinter toConsolePrinter; + + @Mock + private CommandPrinter commandPrinter; + + @InjectMocks + private CommandExecutor commandExecutor; + + @Test + public void shouldLookupSystemCommandByNameAndExecute() throws Exception { + + final String commandName = "CATCHUP"; + + final SystemCommand systemCommand_1 = mock(SystemCommand.class); + final SystemCommand systemCommand_2 = mock(SystemCommand.class); + + final CommandLine commandLine = mock(CommandLine.class); + final JmxParameters jmxParameters = mock(JmxParameters.class); + final List systemCommands = asList(systemCommand_1, systemCommand_2); + + when(commandLine.hasOption("list")).thenReturn(false); + when(commandLine.getOptionValue("command")).thenReturn(commandName); + when(commandLocator.lookupCommand(commandName, systemCommands)).thenReturn(of(systemCommand_2)); + + commandExecutor.executeCommand(commandLine, jmxParameters, systemCommands); + + verify(systemCommandInvoker).runSystemCommand(systemCommand_2, jmxParameters); + } + + @Test + public void shouldPrintErrorAndListOfSystemCommandsIfTheNoCommandFound() throws Exception { + + final String commandName = "CATCHUP"; + + final SystemCommand systemCommand_1 = mock(SystemCommand.class); + final SystemCommand systemCommand_2 = mock(SystemCommand.class); + + final CommandLine commandLine = mock(CommandLine.class); + final JmxParameters jmxParameters = mock(JmxParameters.class); + final List systemCommands = asList(systemCommand_1, systemCommand_2); + + when(commandLine.hasOption("list")).thenReturn(false); + when(commandLine.getOptionValue("command")).thenReturn(commandName); + when(commandLocator.lookupCommand(commandName, systemCommands)).thenReturn(empty()); + + commandExecutor.executeCommand(commandLine, jmxParameters, systemCommands); + + verify(toConsolePrinter).printf("No command found with name '%s'", commandName); + verify(commandPrinter).printSystemCommands(systemCommands); + verifyZeroInteractions(systemCommandInvoker); + } + + @Test + public void shouldListSystemCommandsIfCommandLineOptionIsList() throws Exception { + + final SystemCommand systemCommand_1 = mock(SystemCommand.class); + final SystemCommand systemCommand_2 = mock(SystemCommand.class); + + final CommandLine commandLine = mock(CommandLine.class); + final JmxParameters jmxParameters = mock(JmxParameters.class); + final List systemCommands = asList(systemCommand_1, systemCommand_2); + + when(commandLine.hasOption("list")).thenReturn(true); + + commandExecutor.executeCommand(commandLine, jmxParameters, systemCommands); + + verify(commandPrinter).printSystemCommands(systemCommands); + verifyZeroInteractions(systemCommandInvoker); + } +} diff --git a/src/test/java/uk/gov/justice/framework/command/client/CommandLineArgumentParserTest.java b/src/test/java/uk/gov/justice/framework/command/client/CommandLineArgumentParserTest.java new file mode 100644 index 0000000..890e7a1 --- /dev/null +++ b/src/test/java/uk/gov/justice/framework/command/client/CommandLineArgumentParserTest.java @@ -0,0 +1,112 @@ +package uk.gov.justice.framework.command.client; + +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 uk.gov.justice.framework.command.client.io.ToConsolePrinter; +import uk.gov.justice.framework.command.client.startup.CommandLineArgumentParser; + +import java.util.Optional; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +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 CommandLineArgumentParserTest { + + @Mock + private ToConsolePrinter toConsolePrinter; + + @Mock + private Options options; + + @Mock + private CommandLineParser commandLineParser; + + @InjectMocks + private CommandLineArgumentParser commandLineArgumentParser; + + @Test + public void shouldParseTheCommandLineArgumentsAndReturnTheCommandLineObjectIfCommandIsSpecified() throws Exception { + + final String[] args = {"some", "args"}; + final CommandLine commandLine = mock(CommandLine.class); + + when(commandLineParser.parse(options, args)).thenReturn(commandLine); + when(commandLine.hasOption("command")).thenReturn(true); + when(commandLine.hasOption("list")).thenReturn(false); + + final Optional commandLineOptional = commandLineArgumentParser.parse(args); + + if (commandLineOptional.isPresent()) { + assertThat(commandLineOptional.get(), is(commandLine)); + } else { + fail(); + } + } + + @Test + public void shouldParseTheCommandLineArgumentsAndReturnTheCommandLineObjectIfListIsSpecified() throws Exception { + + final String[] args = {"some", "args"}; + final CommandLine commandLine = mock(CommandLine.class); + + when(commandLineParser.parse(options, args)).thenReturn(commandLine); + when(commandLine.hasOption("command")).thenReturn(false); + when(commandLine.hasOption("list")).thenReturn(true); + + final Optional commandLineOptional = commandLineArgumentParser.parse(args); + + if (commandLineOptional.isPresent()) { + assertThat(commandLineOptional.get(), is(commandLine)); + } else { + fail(); + } + } + + @Test + public void shouldReturnEmptyIfNeitherTheCommandOptionNorTheListOptionIsSpecified() throws Exception { + + final String[] args = {"some", "args"}; + final CommandLine commandLine = mock(CommandLine.class); + + when(commandLineParser.parse(options, args)).thenReturn(commandLine); + when(commandLine.hasOption("command")).thenReturn(false); + when(commandLine.hasOption("list")).thenReturn(false); + + final Optional commandLineOptional = commandLineArgumentParser.parse(args); + + assertThat(commandLineOptional.isPresent(), is(false)); + + verify(toConsolePrinter).println("No system command specifed."); + } + + @Test + public void shouldThrowExceptionIfParsingTheCommandLineFails() throws Exception { + + final ParseException parseException = new ParseException("ooops"); + + final String[] args = {"some", "args"}; + + when(commandLineParser.parse(options, args)).thenThrow(parseException); + + try { + commandLineArgumentParser.parse(args); + fail(); + } catch (final CommandLineException expected) { + assertThat(expected.getCause(), is(parseException)); + assertThat(expected.getMessage(), is("Failed to parse command line args '[some, args]'")); + } + } +} diff --git a/src/test/java/uk/gov/justice/framework/command/client/CommandLocatorTest.java b/src/test/java/uk/gov/justice/framework/command/client/CommandLocatorTest.java new file mode 100644 index 0000000..c9b5718 --- /dev/null +++ b/src/test/java/uk/gov/justice/framework/command/client/CommandLocatorTest.java @@ -0,0 +1,76 @@ +package uk.gov.justice.framework.command.client; + +import static java.util.Arrays.asList; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import uk.gov.justice.services.jmx.command.SystemCommand; + +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class CommandLocatorTest { + + + @InjectMocks + private CommandLocator commandLocator; + + @SuppressWarnings("OptionalGetWithoutIsPresent") + @Test + public void shouldFindTheCorrectCommandByName() throws Exception { + + final String commandName_1 = "commandName_1"; + final String commandName_2 = "commandName_2"; + final String commandName_3 = "commandName_3"; + final String commandName_4 = "commandName_4"; + + final SystemCommand systemCommand_1 = mock(SystemCommand.class); + final SystemCommand systemCommand_2 = mock(SystemCommand.class); + final SystemCommand systemCommand_3 = mock(SystemCommand.class); + final SystemCommand systemCommand_4 = mock(SystemCommand.class); + + final List systemCommands = asList(systemCommand_1, systemCommand_2, systemCommand_3, systemCommand_4); + + when(systemCommand_1.getName()).thenReturn(commandName_1); + when(systemCommand_2.getName()).thenReturn(commandName_2); + when(systemCommand_3.getName()).thenReturn(commandName_3); + when(systemCommand_4.getName()).thenReturn(commandName_4); + + + assertThat(commandLocator.lookupCommand(commandName_1, systemCommands).get(), is(systemCommand_1)); + assertThat(commandLocator.lookupCommand(commandName_2, systemCommands).get(), is(systemCommand_2)); + assertThat(commandLocator.lookupCommand(commandName_3, systemCommands).get(), is(systemCommand_3)); + assertThat(commandLocator.lookupCommand(commandName_4, systemCommands).get(), is(systemCommand_4)); + } + + @Test + public void shouldReturnEmptyIfTheCommandIsNotFound() throws Exception { + + final String commandName_1 = "commandName_1"; + final String commandName_2 = "commandName_2"; + final String commandName_3 = "commandName_3"; + final String commandName_4 = "commandName_4"; + + final SystemCommand systemCommand_1 = mock(SystemCommand.class); + final SystemCommand systemCommand_2 = mock(SystemCommand.class); + final SystemCommand systemCommand_3 = mock(SystemCommand.class); + final SystemCommand systemCommand_4 = mock(SystemCommand.class); + + final List systemCommands = asList(systemCommand_1, systemCommand_2, systemCommand_3, systemCommand_4); + + when(systemCommand_1.getName()).thenReturn(commandName_1); + when(systemCommand_2.getName()).thenReturn(commandName_2); + when(systemCommand_3.getName()).thenReturn(commandName_3); + when(systemCommand_4.getName()).thenReturn(commandName_4); + + + assertThat(commandLocator.lookupCommand("some other command name", systemCommands).isPresent(), is(false)); + } +} diff --git a/src/test/java/uk/gov/justice/framework/command/client/JmxParametersFactoryTest.java b/src/test/java/uk/gov/justice/framework/command/client/JmxParametersFactoryTest.java new file mode 100644 index 0000000..a2dd62b --- /dev/null +++ b/src/test/java/uk/gov/justice/framework/command/client/JmxParametersFactoryTest.java @@ -0,0 +1,94 @@ +package uk.gov.justice.framework.command.client; + +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.when; + +import uk.gov.justice.services.jmx.system.command.client.connection.Credentials; +import uk.gov.justice.services.jmx.system.command.client.connection.JmxParameters; + +import java.util.Optional; + +import org.apache.commons.cli.CommandLine; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class JmxParametersFactoryTest { + + @InjectMocks + private JmxParametersFactory jmxParametersFactory; + + @Test + public void shouldCreateJmxParametersFromTheHostAndPort() throws Exception { + + final String host = "host"; + final String port = "9990"; + + final CommandLine commandLine = mock(CommandLine.class); + + when(commandLine.getOptionValue("host", "localhost")).thenReturn(host); + when(commandLine.getOptionValue("port", "9990")).thenReturn(port); + when(commandLine.hasOption("username")).thenReturn(false); + + final JmxParameters jmxParameters = jmxParametersFactory.createFrom(commandLine); + + assertThat(jmxParameters.getHost(), is(host)); + assertThat(jmxParameters.getPort(), is(9990)); + assertThat(jmxParameters.getCredentials().isPresent(), is(false)); + } + + @Test + public void shouldAlsoAddCredentialsIfTheUsernameExists() throws Exception { + + final String host = "host"; + final String port = "9990"; + final String username = "Fred"; + final String password = "Password123"; + + final CommandLine commandLine = mock(CommandLine.class); + + when(commandLine.getOptionValue("host", "localhost")).thenReturn(host); + when(commandLine.getOptionValue("port", "9990")).thenReturn(port); + when(commandLine.hasOption("username")).thenReturn(true); + when(commandLine.getOptionValue("username")).thenReturn(username); + when(commandLine.getOptionValue("password")).thenReturn(password); + + final JmxParameters jmxParameters = jmxParametersFactory.createFrom(commandLine); + + assertThat(jmxParameters.getHost(), is(host)); + assertThat(jmxParameters.getPort(), is(9990)); + + final Optional credentials = jmxParameters.getCredentials(); + + if (credentials.isPresent()) { + assertThat(credentials.get().getUsername(), is(username)); + assertThat(credentials.get().getPassword(), is(password)); + } else { + fail(); + } + } + + @Test + public void shouldThrowExceptionIfThePortIsNotANumber() throws Exception { + + final String host = "host"; + final String port = "not-a-number"; + + final CommandLine commandLine = mock(CommandLine.class); + + when(commandLine.getOptionValue("host", "localhost")).thenReturn(host); + when(commandLine.getOptionValue("port", "9990")).thenReturn(port); + + try { + jmxParametersFactory.createFrom(commandLine); + fail(); + } catch (final CommandLineException expected) { + assertThat(expected.getMessage(), is("Port number 'not-a-number' is not a number")); + } + } +} diff --git a/src/test/java/uk/gov/justice/framework/command/client/MainApplicationTest.java b/src/test/java/uk/gov/justice/framework/command/client/MainApplicationTest.java new file mode 100644 index 0000000..c2a208c --- /dev/null +++ b/src/test/java/uk/gov/justice/framework/command/client/MainApplicationTest.java @@ -0,0 +1,88 @@ +package uk.gov.justice.framework.command.client; + +import static java.util.Arrays.asList; +import static java.util.Optional.empty; +import static java.util.Optional.of; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; + +import uk.gov.justice.framework.command.client.jmx.ListCommandsInvoker; +import uk.gov.justice.framework.command.client.startup.CommandLineArgumentParser; +import uk.gov.justice.services.jmx.command.SystemCommand; +import uk.gov.justice.services.jmx.system.command.client.connection.JmxParameters; + +import java.util.List; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Options; +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 MainApplicationTest { + + @Mock + private CommandLineArgumentParser commandLineArgumentParser; + + @Mock + private JmxParametersFactory jmxParametersFactory; + + @Mock + private ListCommandsInvoker listCommandsInvoker; + + @Mock + private Options options; + + @Mock + private HelpFormatter formatter; + + @Mock + private CommandExecutor commandExecutor; + + @InjectMocks + private MainApplication mainApplication; + + + @Test + public void shouldLookupTheCorrectCommandAndInvokeIt() throws Exception { + + final String[] args = {"some", "args"}; + final JmxParameters jmxParameters = mock(JmxParameters.class); + + final CommandLine commandLine = mock(CommandLine.class); + final SystemCommand systemCommand_1 = mock(SystemCommand.class); + final SystemCommand systemCommand_2 = mock(SystemCommand.class); + + final List systemCommands = asList(systemCommand_1, systemCommand_2); + + when(commandLineArgumentParser.parse(args)).thenReturn(of(commandLine)); + when(jmxParametersFactory.createFrom(commandLine)).thenReturn(jmxParameters); + when(listCommandsInvoker.listSystemCommands(jmxParameters)).thenReturn(of(systemCommands)); + + mainApplication.run(args); + + verify(commandExecutor).executeCommand(commandLine, jmxParameters, systemCommands); + verifyZeroInteractions(formatter); + } + + @Test + public void shouldPrintHelpIfParsingCommandFailsOrHelpIsSpecified() throws Exception { + + final String[] args = {"some", "args"}; + + when(commandLineArgumentParser.parse(args)).thenReturn(empty()); + + mainApplication.run(args); + + verify(formatter).printHelp("java -jar catchup-shuttering-manager.jar", options); + + verifyZeroInteractions(commandExecutor); + } +} diff --git a/src/test/java/uk/gov/justice/framework/command/client/cdi/producers/CommandLineParserProducerTest.java b/src/test/java/uk/gov/justice/framework/command/client/cdi/producers/CommandLineParserProducerTest.java new file mode 100644 index 0000000..8ad8af5 --- /dev/null +++ b/src/test/java/uk/gov/justice/framework/command/client/cdi/producers/CommandLineParserProducerTest.java @@ -0,0 +1,22 @@ +package uk.gov.justice.framework.command.client.cdi.producers; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class CommandLineParserProducerTest { + + @InjectMocks + private CommandLineParserProducer commandLineParserProducer; + + @Test + public void shouldCreateNewCommandLineParserProducer() throws Exception { + assertThat(commandLineParserProducer.commandLineParser(),is(notNullValue())); + } +} diff --git a/src/test/java/uk/gov/justice/framework/command/client/cdi/producers/HelpFormatterProducerTest.java b/src/test/java/uk/gov/justice/framework/command/client/cdi/producers/HelpFormatterProducerTest.java new file mode 100644 index 0000000..d95b597 --- /dev/null +++ b/src/test/java/uk/gov/justice/framework/command/client/cdi/producers/HelpFormatterProducerTest.java @@ -0,0 +1,22 @@ +package uk.gov.justice.framework.command.client.cdi.producers; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class HelpFormatterProducerTest { + + @InjectMocks + private HelpFormatterProducer helpFormatterProducer; + + @Test + public void shouldCreateNewHelpFormatter() throws Exception { + assertThat(helpFormatterProducer.helpFormatter(), is(notNullValue())); + } +} diff --git a/src/test/java/uk/gov/justice/framework/command/client/cdi/producers/OptionsProducerTest.java b/src/test/java/uk/gov/justice/framework/command/client/cdi/producers/OptionsProducerTest.java new file mode 100644 index 0000000..db22eb0 --- /dev/null +++ b/src/test/java/uk/gov/justice/framework/command/client/cdi/producers/OptionsProducerTest.java @@ -0,0 +1,40 @@ +package uk.gov.justice.framework.command.client.cdi.producers; + +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.util.Collection; + +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.runners.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class OptionsProducerTest { + + @InjectMocks + private OptionsProducer optionsProducer; + + @SuppressWarnings("unchecked") + @Test + public void shouldCreateNewOptionsObjectConfiguredWithTheCorrectCommandLineParameters() throws Exception { + + final Options options = optionsProducer.options(); + + final Collection