diff options
author | liamfallon <liam.fallon@ericsson.com> | 2018-09-27 14:29:21 +0100 |
---|---|---|
committer | liamfallon <liam.fallon@ericsson.com> | 2018-09-27 14:29:31 +0100 |
commit | 2b21188b82e21eb87c4e79a6f31beff9325ab2ea (patch) | |
tree | 4bdb78a8709ddf1956ec1b862deabbe478b1a90d /tools/model-generator/src/main/java | |
parent | 3db2feb37ac427a09790fef1ba637c16c3187ed6 (diff) |
Add unit test for Apex command line tools
THere was no unit test for the command line tools. This
review adds unit test and also fixes a few small bugs that
showed up when uit test was run.
Issue-ID: POLICY-1034
Change-Id: Ic19aacdb168fb5a6faa0cd83ed22ccfcedaa51f5
Signed-off-by: liamfallon <liam.fallon@ericsson.com>
Diffstat (limited to 'tools/model-generator/src/main/java')
-rw-r--r-- | tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/KeyInfoGetter.java | 6 | ||||
-rw-r--r-- | tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2cli/Model2CliMain.java (renamed from tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2cli/Application.java) | 82 | ||||
-rw-r--r-- | tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2event/Model2EventMain.java (renamed from tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2event/Application.java) | 89 |
3 files changed, 112 insertions, 65 deletions
diff --git a/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/KeyInfoGetter.java b/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/KeyInfoGetter.java index 73bb1269d..2376769e8 100644 --- a/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/KeyInfoGetter.java +++ b/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/KeyInfoGetter.java @@ -130,6 +130,9 @@ public class KeyInfoGetter { * @return UUID of the key, null if key was null */ public String getUuid(final AxArtifactKey key) { + if (key == null) { + return null; + } final AxKeyInfo ki = model.getKeyInformation().get(key); if (ki == null || ki.getUuid() == null) { return null; @@ -144,6 +147,9 @@ public class KeyInfoGetter { * @return description of the key, null if key was null */ public String getDesc(final AxArtifactKey key) { + if (key == null) { + return null; + } final AxKeyInfo ki = model.getKeyInformation().get(key); if (ki == null || ki.getDescription() == null) { return null; diff --git a/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2cli/Application.java b/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2cli/Model2CliMain.java index 34ab414d7..5c40b029b 100644 --- a/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2cli/Application.java +++ b/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2cli/Model2CliMain.java @@ -21,6 +21,8 @@ package org.onap.policy.apex.tools.model.generator.model2cli; import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.HelpFormatter; @@ -33,29 +35,21 @@ import org.onap.policy.apex.tools.common.OutputFile; * * @author Sven van der Meer <sven.van.der.meer@ericsson.com> */ -public final class Application { +public final class Model2CliMain { /** The name of the application. */ public static final String APP_NAME = "gen-model2cli"; /** The description 1-liner of the application. */ public static final String APP_DESCRIPTION = "generates CLI Editor Commands from a policy model"; - // Input and output streams - private static final PrintStream OUT_STREAM = System.out; - private static final PrintStream ERR_STREAM = System.err; - - /** - * Private constructor to prevent instantiation. - */ - private Application() { - } - /** - * Main method to start the application. - * + * Run the tool. + * * @param args the command line arguments + * @param outStream for command output + * @param errStream for command errors */ - public static void main(final String[] args) { + Model2CliMain(final String[] args, final PrintStream outStream, final PrintStream errStream) { final CliParser cli = new CliParser(); cli.addOption(CliOptions.HELP); cli.addOption(CliOptions.VERSION); @@ -64,39 +58,35 @@ public final class Application { cli.addOption(CliOptions.FILEOUT); cli.addOption(CliOptions.OVERWRITE); - final CommandLine cmd = cli.parseCli(args); + CommandLine cmd = cli.parseCli(args); // help is an exit option, print usage and exit - if (cmd.hasOption(CliOptions.HELP.getOpt())) { - final HelpFormatter formatter = new HelpFormatter(); - OUT_STREAM.println(APP_NAME + " v" + cli.getAppVersion() + " - " + APP_DESCRIPTION); - formatter.printHelp(APP_NAME, cli.getOptions()); - OUT_STREAM.println(); + if (cmd == null || cmd.hasOption(CliOptions.HELP.getOpt())) { + outStream.println(getHelpString(cli)); + outStream.println(); return; } // version is an exit option, print version and exit if (cmd.hasOption(CliOptions.VERSION.getOpt())) { - OUT_STREAM.println(APP_NAME + " " + cli.getAppVersion()); - OUT_STREAM.println(); + outStream.println(APP_NAME + " " + cli.getAppVersion()); return; } String modelFile = cmd.getOptionValue(CliOptions.MODELFILE.getOpt()); - if (modelFile != null) { - modelFile = cmd.getOptionValue("model"); - } if (modelFile == null) { - ERR_STREAM.println(APP_NAME + ": no '-" + CliOptions.MODELFILE.getOpt() + errStream.println(APP_NAME + ": no '-" + CliOptions.MODELFILE.getOpt() + "' model file given, cannot proceed (try -h for help)"); return; + } else { + modelFile = cmd.getOptionValue("model"); } OutputFile outfile = null; final String of = cmd.getOptionValue(CliOptions.FILEOUT.getOpt()); final boolean overwrite = cmd.hasOption(CliOptions.OVERWRITE.getOpt()); if (overwrite && of == null) { - ERR_STREAM.println(APP_NAME + ": error with '-" + CliOptions.OVERWRITE.getOpt() + errStream.println(APP_NAME + ": error with '-" + CliOptions.OVERWRITE.getOpt() + "' option. This option is only valid if a '-" + CliOptions.FILEOUT.getOpt() + "' option is also used. Cannot proceed (try -h for help)"); return; @@ -105,21 +95,47 @@ public final class Application { outfile = new OutputFile(of, overwrite); final String isoutfileok = outfile.validate(); if (isoutfileok != null) { - ERR_STREAM.println(APP_NAME + ": error with '-" + CliOptions.FILEOUT.getOpt() + "' option: \"" + errStream.println(APP_NAME + ": error with '-" + CliOptions.FILEOUT.getOpt() + "' option: \"" + isoutfileok + "\". Cannot proceed (try -h for help)"); return; } } if (outfile == null) { - OUT_STREAM.println(); - OUT_STREAM.println(APP_NAME + ": starting CLI generator"); - OUT_STREAM.println(" --> model file: " + modelFile); - OUT_STREAM.println(); - OUT_STREAM.println(); + outStream.println(); + outStream.println(APP_NAME + ": starting CLI generator"); + outStream.println(" --> model file: " + modelFile); + outStream.println(); + outStream.println(); } final Model2Cli app = new Model2Cli(modelFile, outfile, !cmd.hasOption("sv"), APP_NAME); app.runApp(); } + + /** + * 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(); + } + + /** + * Main method to start the application. + * + * @param args the command line arguments + */ + public static void main(final String[] args) { + new Model2CliMain(args, System.out, System.err); + } } diff --git a/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2event/Application.java b/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2event/Model2EventMain.java index c7fc5e115..ca49b5394 100644 --- a/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2event/Application.java +++ b/tools/model-generator/src/main/java/org/onap/policy/apex/tools/model/generator/model2event/Model2EventMain.java @@ -21,12 +21,16 @@ package org.onap.policy.apex.tools.model.generator.model2event; import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.HelpFormatter; +import org.onap.policy.apex.context.parameters.SchemaParameters; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.tools.common.CliOptions; import org.onap.policy.apex.tools.common.CliParser; +import org.onap.policy.common.parameters.ParameterService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -35,9 +39,9 @@ import org.slf4j.LoggerFactory; * * @author Sven van der Meer (sven.van.der.meer@ericsson.com) */ -public final class Application { +public final class Model2EventMain { // Get a reference to the logger - private static final Logger LOGGER = LoggerFactory.getLogger(Application.class); + private static final Logger LOGGER = LoggerFactory.getLogger(Model2EventMain.class); /** The name of the application. */ public static final String APP_NAME = "gen-model2event"; @@ -45,20 +49,16 @@ public final class Application { /** The description 1-liner of the application. */ public static final String APP_DESCRIPTION = "generates JSON templates for events generated from a policy model"; - // Input and output streams - private static final PrintStream OUT_STREAM = System.out; - private static final PrintStream ERR_STREAM = System.err; - - /** Private constructor to prevent instantiation. */ - private Application() { - } - /** - * Main method to start the application. - * + * Constructor, run the command. + * * @param args the command line arguments + * @param outStream the stream for command output */ - public static void main(final String[] args) { + Model2EventMain(final String[] args, final PrintStream outStream) { + SchemaParameters schemaParameters = new SchemaParameters(); + ParameterService.register(schemaParameters, true); + final CliParser cli = new CliParser(); cli.addOption(CliOptions.HELP); cli.addOption(CliOptions.VERSION); @@ -68,36 +68,35 @@ public final class Application { final CommandLine cmd = cli.parseCli(args); // help is an exit option, print usage and exit - if (cmd.hasOption('h') || cmd.hasOption("help")) { - final HelpFormatter formatter = new HelpFormatter(); - OUT_STREAM.println(APP_NAME + " v" + cli.getAppVersion() + " - " + APP_DESCRIPTION); - formatter.printHelp(APP_NAME, cli.getOptions()); - OUT_STREAM.println(); + if (cmd == null || cmd.hasOption('h') || cmd.hasOption("help")) { + outStream.println(getHelpString(cli)); + outStream.println(); return; } // version is an exit option, print version and exit if (cmd.hasOption('v') || cmd.hasOption("version")) { - OUT_STREAM.println(APP_NAME + " " + cli.getAppVersion()); - OUT_STREAM.println(); + outStream.println(APP_NAME + " " + cli.getAppVersion()); + outStream.println(); return; } - generateJsonEventScheam(cmd); + generateJsonEventSchema(cmd, outStream); } /** * Generate the JSON event schema. * * @param cmd the command to run + * @param outStream the output stream for output */ - private static void generateJsonEventScheam(final CommandLine cmd) { + private static void generateJsonEventSchema(final CommandLine cmd, final PrintStream outStream) { String modelFile = cmd.getOptionValue('m'); if (modelFile == null) { modelFile = cmd.getOptionValue("model"); } if (modelFile == null) { - ERR_STREAM.println(APP_NAME + ": no model file given, cannot proceed (try -h for help)"); + outStream.println(APP_NAME + ": no model file given, cannot proceed (try -h for help)"); return; } @@ -106,28 +105,54 @@ public final class Application { type = cmd.getOptionValue("type"); } if (type == null) { - ERR_STREAM.println(APP_NAME + ": no event type given, cannot proceed (try -h for help)"); + outStream.println(APP_NAME + ": no event type given, cannot proceed (try -h for help)"); return; } if (!"stimuli".equals(type) && !"response".equals(type) && !"internal".equals(type)) { - ERR_STREAM.println(APP_NAME + ": unknown type <" + type + ">, cannot proceed (try -h for help)"); + outStream.println(APP_NAME + ": unknown type <" + type + ">, cannot proceed (try -h for help)"); return; } - OUT_STREAM.println(); - OUT_STREAM.println(APP_NAME + ": starting Event generator"); - OUT_STREAM.println(" --> model file: " + modelFile); - OUT_STREAM.println(" --> type: " + type); - OUT_STREAM.println(); - OUT_STREAM.println(); + outStream.println(); + outStream.println(APP_NAME + ": starting Event generator"); + outStream.println(" --> model file: " + modelFile); + outStream.println(" --> type: " + type); + outStream.println(); + outStream.println(); try { final Model2JsonEventSchema app = new Model2JsonEventSchema(modelFile, type, APP_NAME); app.runApp(); } catch (final ApexException aex) { String message = APP_NAME + ": caught APEX exception with message: " + aex.getMessage(); - ERR_STREAM.println(message); + outStream.println(message); LOGGER.warn(message, aex); } } + + /** + * 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(); + } + + /** + * Main method to start the application. + * + * @param args the command line arguments + */ + public static void main(final String[] args) { + new Model2EventMain(args, System.out); + } } |