aboutsummaryrefslogtreecommitdiffstats
path: root/tools/simple-wsclient/src/main
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@ericsson.com>2018-09-27 16:52:23 +0100
committerliamfallon <liam.fallon@ericsson.com>2018-09-27 16:52:34 +0100
commitafd3a44e210de3c1aed9f6f7278913b2a2f2f6d2 (patch)
tree3b7c6038ec3d526549e4795032ea1c8baf2fcc0a /tools/simple-wsclient/src/main
parent3595f7c277778a64d1d828b66ed8127d92c010b5 (diff)
Add JUnit for web service client tool
JUnit, SOnar fixes, and some minor bug fixes. Issue-ID: POLICY-1034 Change-Id: I72f46e944051f7d65d6b106afe33c759975283af Signed-off-by: liamfallon <liam.fallon@ericsson.com>
Diffstat (limited to 'tools/simple-wsclient/src/main')
-rw-r--r--tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleConsole.java3
-rw-r--r--tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/WsClientMain.java89
2 files changed, 52 insertions, 40 deletions
diff --git a/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleConsole.java b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleConsole.java
index 28c494207..edad2ebb6 100644
--- a/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleConsole.java
+++ b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/SimpleConsole.java
@@ -130,8 +130,9 @@ public class SimpleConsole extends WebSocketClient {
connect();
}
};
+ thread.setName("ClientThread");
thread.start();
-
+
final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String event = "";
String line;
diff --git a/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/WsClientMain.java b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/WsClientMain.java
index 9376cad4b..29a5de08f 100644
--- a/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/WsClientMain.java
+++ b/tools/simple-wsclient/src/main/java/org/onap/policy/apex/tools/simple/wsclient/WsClientMain.java
@@ -22,6 +22,8 @@ package org.onap.policy.apex.tools.simple.wsclient;
import java.io.IOException;
import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
import java.net.URISyntaxException;
import java.nio.channels.NotYetConnectedException;
@@ -42,6 +44,11 @@ public final class WsClientMain {
// Get a reference to the logger
private static final Logger LOGGER = LoggerFactory.getLogger(WsClientMain.class);
+ // String constants
+ private static final String APP_NAME = "ws-client";
+ private static final String APP_DESCRIPTION = "takes events from stdin and sends via WS to APEX"
+ + " and/or receives events from APEX via WS and prints them to standard out";
+
/**
* Run the command.
*
@@ -49,8 +56,6 @@ public final class WsClientMain {
* @param outStream stream for output
*/
WsClientMain(final String[] args, final PrintStream outStream) {
- String appName = "ws-simple-echo";
- String appDescr = "receives events from APEX via WS and prints them to standard out";
boolean console = false;
final CliParser cli = new CliParser();
@@ -62,41 +67,35 @@ public final class WsClientMain {
final CommandLine cmd = cli.parseCli(args);
- if (cmd.hasOption('c') || cmd.hasOption("console")) {
- appName = "ws-simple-console";
- appDescr = "takes events from stdin and sends via WS to APEX";
- console = true;
- }
-
// help is an exit option, print usage and exit
- if (cmd.hasOption('h') || cmd.hasOption("help")) {
- final HelpFormatter formatter = new HelpFormatter();
- outStream.println(appName + " v" + cli.getAppVersion() + " - " + appDescr);
- formatter.printHelp(appName, cli.getOptions());
+ if (cmd == null || cmd.hasOption('h') || cmd.hasOption("help")) {
+ outStream.println(getHelpString(cli));
outStream.println();
return;
}
+ if (cmd.hasOption('c') || cmd.hasOption("console")) {
+ console = true;
+ }
+
// version is an exit option, print version and exit
if (cmd.hasOption('v') || cmd.hasOption("version")) {
- outStream.println(appName + " " + cli.getAppVersion());
+ outStream.println(APP_NAME + " " + cli.getAppVersion());
outStream.println();
return;
}
- runConsoleOrEcho(appName, console, cmd, outStream);
+ runConsoleOrEcho(console, cmd, outStream);
}
/**
* Run the console or echo.
*
- * @param appName the application name
* @param console if true, run the console otherwise run echo
* @param cmd the command line to run
* @param outStream stream for output
*/
- private static void runConsoleOrEcho(String appName, boolean console, final CommandLine cmd,
- final PrintStream outStream) {
+ private static void runConsoleOrEcho(final boolean console, final CommandLine cmd, final PrintStream outStream) {
String server = cmd.getOptionValue('s');
if (server == null) {
server = cmd.getOptionValue("server");
@@ -114,9 +113,9 @@ public final class WsClientMain {
}
if (console) {
- runConsole(server, port, appName, outStream);
+ runConsole(server, port, outStream);
} else {
- runEcho(server, port, appName, outStream);
+ runEcho(server, port, outStream);
}
}
@@ -125,17 +124,14 @@ public final class WsClientMain {
*
* @param server the server, must not be blank
* @param port the port, must not be blank
- * @param appName the application name, must not be blank
* @param outStream stream for output
*/
- public static void runEcho(final String server, final String port, final String appName,
- final PrintStream outStream) {
+ public static void runEcho(final String server, final String port, final PrintStream outStream) {
Validate.notBlank(server);
Validate.notBlank(port);
- Validate.notBlank(appName);
outStream.println();
- outStream.println(appName + ": starting simple event echo");
+ outStream.println(APP_NAME + ": starting simple event echo");
outStream.println(" --> server: " + server);
outStream.println(" --> port: " + port);
outStream.println();
@@ -145,18 +141,18 @@ public final class WsClientMain {
outStream.println();
try {
- final SimpleEcho simpleEcho = new SimpleEcho(server, port, appName, outStream, outStream);
+ final SimpleEcho simpleEcho = new SimpleEcho(server, port, APP_NAME, outStream, outStream);
simpleEcho.connect();
} catch (final URISyntaxException uex) {
- String message = appName + ": URI exception, could not create URI from server and port settings";
+ String message = APP_NAME + ": URI exception, could not create URI from server and port settings";
outStream.println(message);
LOGGER.warn(message, uex);
} catch (final NullPointerException nex) {
- String message = appName + ": null pointer, server or port were null";
+ String message = APP_NAME + ": null pointer, server or port were null";
outStream.println(message);
LOGGER.warn(message, nex);
} catch (final IllegalArgumentException iex) {
- String message = appName + ": illegal argument, server or port were blank";
+ String message = APP_NAME + ": illegal argument, server or port were blank";
outStream.println(message);
LOGGER.warn(message, iex);
}
@@ -167,17 +163,15 @@ public final class WsClientMain {
*
* @param server the server, must not be blank
* @param port the port, must not be blank
- * @param appName the application name, must not be blank
* @param outStream stream for output
*/
- public static void runConsole(final String server, final String port, final String appName,
- final PrintStream outStream) {
+ public static void runConsole(final String server, final String port, final PrintStream outStream) {
Validate.notBlank(server);
Validate.notBlank(port);
- Validate.notBlank(appName);
+ Validate.notBlank(APP_NAME);
outStream.println();
- outStream.println(appName + ": starting simple event console");
+ outStream.println(APP_NAME + ": starting simple event console");
outStream.println(" --> server: " + server);
outStream.println(" --> port: " + port);
outStream.println();
@@ -187,32 +181,49 @@ public final class WsClientMain {
outStream.println();
try {
- final SimpleConsole simpleConsole = new SimpleConsole(server, port, appName, outStream, outStream);
+ final SimpleConsole simpleConsole = new SimpleConsole(server, port, APP_NAME, outStream, outStream);
simpleConsole.runClient();
} catch (final URISyntaxException uex) {
- String message = appName + ": URI exception, could not create URI from server and port settings";
+ String message = APP_NAME + ": URI exception, could not create URI from server and port settings";
outStream.println(message);
LOGGER.warn(message, uex);
} catch (final NullPointerException nex) {
- String message = appName + ": null pointer, server or port were null";
+ String message = APP_NAME + ": null pointer, server or port were null";
outStream.println(message);
LOGGER.warn(message, nex);
} catch (final IllegalArgumentException iex) {
- String message = appName + ": illegal argument, server or port were blank";
+ String message = APP_NAME + ": illegal argument, server or port were blank";
outStream.println(message);
LOGGER.warn(message, iex);
} catch (final NotYetConnectedException nex) {
- String message = appName + ": not yet connected, connection to server took too long";
+ String message = APP_NAME + ": not yet connected, connection to server took too long";
outStream.println(message);
LOGGER.warn(message, nex);
} catch (final IOException ioe) {
- String message = appName + ": IO exception, something went wrong on the standard input";
+ String message = APP_NAME + ": IO exception, something went wrong on the standard input";
outStream.println(message);
LOGGER.warn(message, ioe);
}
}
/**
+ * Get the help string for the application.
+ *
+ * @param cli the command line options
+ * @return the help string
+ */
+ private String getHelpString(final CliParser cli) {
+ HelpFormatter formatter = new HelpFormatter();
+
+ final StringWriter helpStringWriter = new StringWriter();
+ final PrintWriter helpPrintWriter = new PrintWriter(helpStringWriter);
+
+ formatter.printHelp(helpPrintWriter, 120, APP_NAME, APP_DESCRIPTION, cli.getOptions(), 2, 4, "");
+
+ return helpStringWriter.toString();
+ }
+
+ /**
* The main method for the WS applications.
*
* @param args command line argument s