diff options
author | adheli.tavares <adheli.tavares@est.tech> | 2021-04-13 16:33:41 +0100 |
---|---|---|
committer | adheli.tavares <adheli.tavares@est.tech> | 2021-04-16 10:34:17 +0100 |
commit | adfc3ee02f3194a143bcea430e5e3aeb99a23206 (patch) | |
tree | e54813c7d0d91b87a4d2c31f9b1e5d8ec7cdc477 /main | |
parent | 8c6123d8c19d95115dc865784ccb1dc87d24f35c (diff) |
Refactor DistributionCommandLineArguments class
CMD Class to use the CMD Handler from Common.
Issue-ID: POLICY-3129
Change-Id: I2219cdb7db5669de71c3b2b90c496321b61d14eb
Signed-off-by: adheli.tavares <adheli.tavares@est.tech>
Diffstat (limited to 'main')
4 files changed, 122 insertions, 314 deletions
diff --git a/main/src/main/java/org/onap/policy/distribution/main/startstop/DistributionCommandLineArguments.java b/main/src/main/java/org/onap/policy/distribution/main/startstop/DistributionCommandLineArguments.java index 69849c47..fedbe508 100644 --- a/main/src/main/java/org/onap/policy/distribution/main/startstop/DistributionCommandLineArguments.java +++ b/main/src/main/java/org/onap/policy/distribution/main/startstop/DistributionCommandLineArguments.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * Modifications Copyright (C) 2019 AT&T Intellectual Property. + * Modifications Copyright (C) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,62 +22,21 @@ package org.onap.policy.distribution.main.startstop; -import java.io.File; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.net.URL; -import java.util.Arrays; -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.DefaultParser; -import org.apache.commons.cli.HelpFormatter; -import org.apache.commons.cli.Option; -import org.apache.commons.cli.Options; -import org.apache.commons.cli.ParseException; -import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.distribution.main.PolicyDistributionException; +import org.onap.policy.common.utils.cmd.CommandLineArgumentsHandler; +import org.onap.policy.common.utils.cmd.CommandLineException; +import org.onap.policy.common.utils.resources.MessageConstants; import org.onap.policy.distribution.main.PolicyDistributionRuntimeException; /** * This class reads and handles command line parameters for the policy distribution main program. */ -public class DistributionCommandLineArguments { - private static final String FILE_MESSAGE_PREAMBLE = " file \""; - private static final int HELP_LINE_LENGTH = 120; - - // Apache Commons CLI options - private final Options options; - - // The command line options - private String configurationFilePath = null; +public class DistributionCommandLineArguments extends CommandLineArgumentsHandler { /** - * Construct the options for the CLI editor. + * Construct the options for the CLI editor from super. */ public DistributionCommandLineArguments() { - //@formatter:off - options = new Options(); - options.addOption(Option.builder("h") - .longOpt("help") - .desc("outputs the usage of this command") - .required(false) - .type(Boolean.class) - .build()); - options.addOption(Option.builder("v") - .longOpt("version") - .desc("outputs the version of policy distribution") - .required(false) - .type(Boolean.class) - .build()); - options.addOption(Option.builder("c") - .longOpt("config-file") - .desc("the full path to the configuration file to use, " - + "the configuration file must be a Json file containing the policy distribution parameters") - .hasArg() - .argName("CONFIG_FILE") - .required(false) - .type(String.class) - .build()); - //@formatter:on + super(Main.class.getName(), MessageConstants.POLICY_DISTRIBUTION); } /** @@ -85,162 +45,12 @@ public class DistributionCommandLineArguments { * @param args The command line arguments */ public DistributionCommandLineArguments(final String[] args) { - // Set up the options with the default constructor this(); - // Parse the arguments try { parse(args); - } catch (final PolicyDistributionException e) { + } catch (final CommandLineException e) { throw new PolicyDistributionRuntimeException("parse error on policy distribution parameters", e); } } - - /** - * Parse the command line options. - * - * @param args The command line arguments - * @return a string with a message for help and version, or null if there is no message - * @throws PolicyDistributionException on command argument errors - */ - public String parse(final String[] args) throws PolicyDistributionException { - // Clear all our arguments - setConfigurationFilePath(null); - - CommandLine commandLine = null; - try { - commandLine = new DefaultParser().parse(options, args); - } catch (final ParseException e) { - throw new PolicyDistributionException("invalid command line arguments specified : " + e.getMessage()); - } - - // Arguments left over after Commons CLI does its stuff - final String[] remainingArgs = commandLine.getArgs(); - - if (remainingArgs.length > 0 && commandLine.hasOption('c') || remainingArgs.length > 0) { - throw new PolicyDistributionException( - "too many command line arguments specified : " + Arrays.toString(args)); - } - - if (remainingArgs.length == 1) { - configurationFilePath = remainingArgs[0]; - } - - if (commandLine.hasOption('h')) { - return help(Main.class.getName()); - } - - if (commandLine.hasOption('v')) { - return version(); - } - - if (commandLine.hasOption('c')) { - setConfigurationFilePath(commandLine.getOptionValue('c')); - } - - return null; - } - - /** - * Validate the command line options. - * - * @throws PolicyDistributionException on command argument validation errors - */ - public void validate() throws PolicyDistributionException { - validateReadableFile("policy distribution configuration", configurationFilePath); - } - - /** - * Print version information for policy distribution. - * - * @return the version string - */ - public String version() { - return ResourceUtils.getResourceAsString("version.txt"); - } - - /** - * Print help information for policy distribution. - * - * @param mainClassName the main class name - * @return the help string - */ - public String help(final String mainClassName) { - final HelpFormatter helpFormatter = new HelpFormatter(); - final StringWriter stringWriter = new StringWriter(); - final PrintWriter printWriter = new PrintWriter(stringWriter); - - helpFormatter.printHelp(printWriter, HELP_LINE_LENGTH, mainClassName + " [options...]", "options", options, 0, - 0, ""); - - return stringWriter.toString(); - } - - /** - * Gets the configuration file path. - * - * @return the configuration file path - */ - public String getConfigurationFilePath() { - return configurationFilePath; - } - - /** - * Gets the full expanded configuration file path. - * - * @return the configuration file path - */ - public String getFullConfigurationFilePath() { - return ResourceUtils.getFilePath4Resource(getConfigurationFilePath()); - } - - /** - * Sets the configuration file path. - * - * @param configurationFilePath the configuration file path - */ - public void setConfigurationFilePath(final String configurationFilePath) { - this.configurationFilePath = configurationFilePath; - - } - - /** - * Check set configuration file path. - * - * @return true, if check set configuration file path - */ - public boolean checkSetConfigurationFilePath() { - return configurationFilePath != null && configurationFilePath.length() > 0; - } - - /** - * Validate readable file. - * - * @param fileTag the file tag - * @param fileName the file name - * @throws PolicyDistributionException on the file name passed as a parameter - */ - private void validateReadableFile(final String fileTag, final String fileName) throws PolicyDistributionException { - if (fileName == null || fileName.length() == 0) { - throw new PolicyDistributionException(fileTag + " file was not specified as an argument"); - } - - // The file name refers to a resource on the local file system - final URL fileUrl = ResourceUtils.getUrl4Resource(fileName); - if (fileUrl == null) { - throw new PolicyDistributionException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist"); - } - - final File theFile = new File(fileUrl.getPath()); - if (!theFile.exists()) { - throw new PolicyDistributionException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" does not exist"); - } - if (!theFile.isFile()) { - throw new PolicyDistributionException( - fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is not a normal file"); - } - if (!theFile.canRead()) { - throw new PolicyDistributionException(fileTag + FILE_MESSAGE_PREAMBLE + fileName + "\" is ureadable"); - } - } } diff --git a/main/src/main/java/org/onap/policy/distribution/main/startstop/Main.java b/main/src/main/java/org/onap/policy/distribution/main/startstop/Main.java index 6acef4d4..83eee73c 100644 --- a/main/src/main/java/org/onap/policy/distribution/main/startstop/Main.java +++ b/main/src/main/java/org/onap/policy/distribution/main/startstop/Main.java @@ -4,6 +4,7 @@ * Copyright (C) 2019 Nordix Foundation. * Modifications Copyright (C) 2020 AT&T Inc. * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. + * Modifications Copyright (C) 2021 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +25,7 @@ package org.onap.policy.distribution.main.startstop; import java.util.Arrays; +import org.onap.policy.common.utils.cmd.CommandLineException; import org.onap.policy.common.utils.resources.MessageConstants; import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.PolicyDistributionRuntimeException; @@ -76,7 +78,7 @@ public class Main { // Start the activator activator.initialize(); - } catch (final PolicyDistributionException e) { + } catch (final PolicyDistributionException | CommandLineException e) { throw new PolicyDistributionRuntimeException( String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_DISTRIBUTION), e); } diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterHandler.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterHandler.java index 6eb4cf77..dcafe68c 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterHandler.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterHandler.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation + * Modifications Copyright (C) 2020-2021 Nordix Foundation. * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -31,6 +31,7 @@ import java.nio.file.Files; import java.nio.file.Paths; import org.junit.Test; import org.onap.policy.common.parameters.ParameterRuntimeException; +import org.onap.policy.common.utils.cmd.CommandLineException; import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.startstop.DistributionCommandLineArguments; import org.onap.policy.distribution.main.testclasses.DummyPolicyDecoderParameterGroup; @@ -43,48 +44,41 @@ import org.onap.policy.distribution.main.testclasses.DummyPolicyForwarderParamet */ public class TestDistributionParameterHandler { @Test - public void testParameterHandlerNoParameterFile() throws PolicyDistributionException { - verifyFailure("NoParameterFile.json", - PolicyDistributionException.class, - "FileNotFoundException"); + public void testParameterHandlerNoParameterFile() throws PolicyDistributionException, CommandLineException { + verifyFailure("NoParameterFile.json", PolicyDistributionException.class, "FileNotFoundException"); } @Test - public void testParameterHandlerEmptyParameters() throws PolicyDistributionException { - verifyFailure("EmptyParameters.json", - PolicyDistributionException.class, - "no parameters found in \"parameters/EmptyParameters.json\""); + public void testParameterHandlerEmptyParameters() throws PolicyDistributionException, CommandLineException { + verifyFailure("EmptyParameters.json", PolicyDistributionException.class, + "no parameters found in \"parameters/EmptyParameters.json\""); } @Test - public void testParameterHandlerBadParameters() throws PolicyDistributionException { - verifyFailure("BadParameters.json", - PolicyDistributionException.class, - "error reading parameters from \"parameters/BadParameters.json\"\n" - + "(JsonSyntaxException):java.lang.IllegalStateException: " - + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name"); + public void testParameterHandlerBadParameters() throws PolicyDistributionException, CommandLineException { + verifyFailure("BadParameters.json", PolicyDistributionException.class, + "error reading parameters from \"parameters/BadParameters.json\"\n" + + "(JsonSyntaxException):java.lang.IllegalStateException: " + + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name"); } @Test - public void testParameterHandlerInvalidParameters() throws PolicyDistributionException { - verifyFailure("InvalidParameters.json", - PolicyDistributionException.class, - "error reading parameters from \"parameters/InvalidParameters.json\"\n" - + "(JsonSyntaxException):java.lang.IllegalStateException: " - + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name"); + public void testParameterHandlerInvalidParameters() throws PolicyDistributionException, CommandLineException { + verifyFailure("InvalidParameters.json", PolicyDistributionException.class, + "error reading parameters from \"parameters/InvalidParameters.json\"\n" + + "(JsonSyntaxException):java.lang.IllegalStateException: " + + "Expected a string but was BEGIN_ARRAY at line 2 column 15 path $.name"); } @Test - public void testParameterHandlerNoParameters() throws PolicyDistributionException { - verifyFailure("NoParameters.json", - ParameterRuntimeException.class, - "map parameter \"receptionHandlerParameters\" is null"); + public void testParameterHandlerNoParameters() throws PolicyDistributionException, CommandLineException { + verifyFailure("NoParameters.json", ParameterRuntimeException.class, + "map parameter \"receptionHandlerParameters\" is null"); } @Test - public void testParameterHandlerMinumumParameters() throws PolicyDistributionException { - final String[] minArgumentString = - { "-c", "parameters/MinimumParameters.json" }; + public void testParameterHandlerMinumumParameters() throws PolicyDistributionException, CommandLineException { + final String[] minArgumentString = {"-c", "parameters/MinimumParameters.json"}; final DistributionCommandLineArguments minArguments = new DistributionCommandLineArguments(); minArguments.parse(minArgumentString); @@ -94,9 +88,8 @@ public class TestDistributionParameterHandler { } @Test - public void testDistributionParameterGroup() throws PolicyDistributionException { - final String[] distributionConfigParameters = - { "-c", "parameters/DistributionConfigParameters.json" }; + public void testDistributionParameterGroup() throws PolicyDistributionException, CommandLineException { + final String[] distributionConfigParameters = {"-c", "parameters/DistributionConfigParameters.json"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); arguments.parse(distributionConfigParameters); @@ -127,121 +120,116 @@ public class TestDistributionParameterHandler { @Test public void testDistributionParameterGroup_InvalidForwarderConfigurationClassName() - throws PolicyDistributionException { + throws PolicyDistributionException, CommandLineException { verifyFailure("DistributionConfigParameters_InvalidForwarderConfigurationClassName.json", - PolicyDistributionException.class, - "parameter \"parameterClassName\" value \"\" invalid in JSON file"); + PolicyDistributionException.class, "parameter \"parameterClassName\" value \"\" invalid in JSON file"); } @Test public void testDistributionParameterGroup_UnknownForwarderConfigurationClassName() - throws PolicyDistributionException { + throws PolicyDistributionException, CommandLineException { verifyFailure("DistributionConfigParameters_UnknownForwarderConfigurationClassName.json", - PolicyDistributionException.class, - "parameter \"parameterClassName\" value \"org.onap.policy.Unknown\", could not find class"); + PolicyDistributionException.class, + "parameter \"parameterClassName\" value \"org.onap.policy.Unknown\", could not find class"); } @Test - public void testDistributionParameterGroup_InvalidName() throws PolicyDistributionException { - verifyFailure("DistributionConfigParameters_InvalidName.json", - PolicyDistributionException.class, - "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"); + public void testDistributionParameterGroup_InvalidName() throws PolicyDistributionException, CommandLineException { + verifyFailure("DistributionConfigParameters_InvalidName.json", PolicyDistributionException.class, + "field \"name\" type \"java.lang.String\" value \" \" INVALID, must be a non-blank string"); } @Test - public void testDistributionParameterGroup_NoReceptionHandler() throws PolicyDistributionException { - verifyFailure("DistributionConfigParameters_NoReceptionHandler.json", - ParameterRuntimeException.class, - "map parameter \"receptionHandlerParameters\" is null"); + public void testDistributionParameterGroup_NoReceptionHandler() + throws PolicyDistributionException, CommandLineException { + verifyFailure("DistributionConfigParameters_NoReceptionHandler.json", ParameterRuntimeException.class, + "map parameter \"receptionHandlerParameters\" is null"); } @Test - public void testDistributionParameterGroup_EmptyReceptionHandler() throws PolicyDistributionException { - verifyFailure("DistributionConfigParameters_EmptyReceptionHandler.json", - PolicyDistributionException.class, - "must have at least one reception handler\n"); + public void testDistributionParameterGroup_EmptyReceptionHandler() + throws PolicyDistributionException, CommandLineException { + verifyFailure("DistributionConfigParameters_EmptyReceptionHandler.json", PolicyDistributionException.class, + "must have at least one reception handler\n"); } @Test - public void testDistributionParameterGroup_NoPolicyDecoder() throws PolicyDistributionException { - verifyFailure("DistributionConfigParameters_NoPolicyDecoder.json", - ParameterRuntimeException.class, - "map parameter \"policyDecoders\" is null"); + public void testDistributionParameterGroup_NoPolicyDecoder() + throws PolicyDistributionException, CommandLineException { + verifyFailure("DistributionConfigParameters_NoPolicyDecoder.json", ParameterRuntimeException.class, + "map parameter \"policyDecoders\" is null"); } @Test - public void testDistributionParameterGroup_NoPolicyForwarder() throws PolicyDistributionException { - verifyFailure("DistributionConfigParameters_NoPolicyForwarder.json", - ParameterRuntimeException.class, - "map parameter \"policyForwarders\" is null"); + public void testDistributionParameterGroup_NoPolicyForwarder() + throws PolicyDistributionException, CommandLineException { + verifyFailure("DistributionConfigParameters_NoPolicyForwarder.json", ParameterRuntimeException.class, + "map parameter \"policyForwarders\" is null"); } @Test - public void testDistributionParameterGroup_EmptyPolicyDecoder() throws PolicyDistributionException { - verifyFailure("DistributionConfigParameters_EmptyPolicyDecoder.json", - PolicyDistributionException.class, - "must have at least one policy decoder\n"); + public void testDistributionParameterGroup_EmptyPolicyDecoder() + throws PolicyDistributionException, CommandLineException { + verifyFailure("DistributionConfigParameters_EmptyPolicyDecoder.json", PolicyDistributionException.class, + "must have at least one policy decoder\n"); } @Test - public void testDistributionParameterGroup_EmptyPolicyForwarder() throws PolicyDistributionException { - verifyFailure("DistributionConfigParameters_EmptyPolicyForwarder.json", - PolicyDistributionException.class, - "must have at least one policy forwarder\n"); + public void testDistributionParameterGroup_EmptyPolicyForwarder() + throws PolicyDistributionException, CommandLineException { + verifyFailure("DistributionConfigParameters_EmptyPolicyForwarder.json", PolicyDistributionException.class, + "must have at least one policy forwarder\n"); } @Test public void testDistributionParameterGroup_InvalidReceptionHandlerParameters() - throws PolicyDistributionException, IOException { + throws PolicyDistributionException, IOException, CommandLineException { - String resultString = Files.readString(Paths.get( - "src/test/resources/expectedValidationResults/InvalidReceptionHandlerParameters.txt")) - .trim().replaceAll("\\r\\n", "\\\n"); + String resultString = Files + .readString( + Paths.get("src/test/resources/expectedValidationResults/InvalidReceptionHandlerParameters.txt")) + .trim().replaceAll("\\r\\n", "\\\n"); verifyFailure("DistributionConfigParameters_InvalidReceptionHandlerParameters.json", - PolicyDistributionException.class, - resultString); + PolicyDistributionException.class, resultString); } @Test public void testDistributionParameterGroup_InvalidDecoderAndForwarderParameters() - throws PolicyDistributionException, IOException { + throws PolicyDistributionException, IOException, CommandLineException { - String resultString = new String(Files.readString(Paths.get( - "src/test/resources/expectedValidationResults/InvalidDecoderAndForwarderParameters.txt")) - .trim().replaceAll("\\r\\n", "\\\n")); + String resultString = new String(Files + .readString(Paths + .get("src/test/resources/expectedValidationResults/InvalidDecoderAndForwarderParameters.txt")) + .trim().replaceAll("\\r\\n", "\\\n")); verifyFailure("DistributionConfigParameters_InvalidDecoderAndForwarderParameters.json", - PolicyDistributionException.class, - resultString); + PolicyDistributionException.class, resultString); } @Test public void testDistributionParameterGroup_InvalidRestServerParameters() - throws PolicyDistributionException, IOException { + throws PolicyDistributionException, IOException, CommandLineException { - String resultString = new String(Files.readString(Paths.get( - "src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt")) + String resultString = new String(Files + .readString(Paths.get("src/test/resources/expectedValidationResults/InvalidRestServerParameters.txt")) .trim().replaceAll("\\r\\n", "\\\n")); verifyFailure("DistributionConfigParameters_InvalidRestServerParameters.json", - PolicyDistributionException.class, - resultString); + PolicyDistributionException.class, resultString); } @Test - public void testDistributionVersion() throws PolicyDistributionException { - final String[] distributionConfigParameters = - { "-v" }; + public void testDistributionVersion() throws PolicyDistributionException, CommandLineException { + final String[] distributionConfigParameters = {"-v"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); final String version = arguments.parse(distributionConfigParameters); assertTrue(version.startsWith("ONAP Policy Framework Distribution Service")); } @Test - public void testDistributionHelp() throws PolicyDistributionException { - final String[] distributionConfigParameters = - { "-h" }; + public void testDistributionHelp() throws PolicyDistributionException, CommandLineException { + final String[] distributionConfigParameters = {"-h"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); final String help = arguments.parse(distributionConfigParameters); assertTrue(help.startsWith("usage:")); @@ -249,45 +237,43 @@ public class TestDistributionParameterHandler { @Test public void testDistributionInvalidOption() throws PolicyDistributionException { - final String[] distributionConfigParameters = - { "-d" }; + final String[] distributionConfigParameters = {"-d"}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); - assertThatThrownBy(() -> - arguments.parse(distributionConfigParameters) - ).isInstanceOf(PolicyDistributionException.class) - .hasMessageContaining("invalid command line arguments specified"); + assertThatThrownBy(() -> arguments.parse(distributionConfigParameters)) + .hasMessageContaining("invalid command line arguments specified"); } @Test - public void testDistributionParameterGroup_InvalidReceptionHandlerClass() throws PolicyDistributionException { + public void testDistributionParameterGroup_InvalidReceptionHandlerClass() + throws PolicyDistributionException, CommandLineException { verifyFailure("DistributionConfigParameters_InvalidReceptionHandlerClass.json", - PolicyDistributionException.class, "could not find class"); + PolicyDistributionException.class, "could not find class"); } @Test - public void testDistributionParameterGroup_EmptyReceptionHandlerClass() throws PolicyDistributionException { - verifyFailure("DistributionConfigParameters_EmptyReceptionHandlerClass.json", - PolicyDistributionException.class, "invalid in JSON file"); + public void testDistributionParameterGroup_EmptyReceptionHandlerClass() + throws PolicyDistributionException, CommandLineException { + verifyFailure("DistributionConfigParameters_EmptyReceptionHandlerClass.json", PolicyDistributionException.class, + "invalid in JSON file"); } @Test public void testDistributionParameterGroup_InvalidDecoderConfigurationClassName() - throws PolicyDistributionException { + throws PolicyDistributionException, CommandLineException { verifyFailure("DistributionConfigParameters_InvalidDecoderConfigurationClassName.json", - PolicyDistributionException.class, - "parameter \"parameterClassName\" value \"\" invalid in JSON file"); + PolicyDistributionException.class, "parameter \"parameterClassName\" value \"\" invalid in JSON file"); } @Test public void testDistributionParameterGroup_UnknownDecoderConfigurationClassName() - throws PolicyDistributionException { + throws PolicyDistributionException, CommandLineException { verifyFailure("DistributionConfigParameters_UnknownDecoderConfigurationClassName.json", - PolicyDistributionException.class, - "parameter \"parameterClassName\" value \"org.onap.policy.Unknown\", could not find class"); + PolicyDistributionException.class, + "parameter \"parameterClassName\" value \"org.onap.policy.Unknown\", could not find class"); } private <T> void verifyFailure(String fileName, Class<T> clazz, String expectedMessage) - throws PolicyDistributionException { + throws PolicyDistributionException, CommandLineException { final String[] distributionConfigParameters = {"-c", "parameters/" + fileName}; final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); @@ -296,6 +282,6 @@ public class TestDistributionParameterHandler { DistributionParameterHandler paramHandler = new DistributionParameterHandler(); assertThatThrownBy(() -> paramHandler.getParameters(arguments)).isInstanceOf(clazz) - .hasMessageContaining(expectedMessage); + .hasMessageContaining(expectedMessage); } } diff --git a/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionCommandLineArguments.java b/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionCommandLineArguments.java index 7e72b852..0a0e5fa0 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionCommandLineArguments.java +++ b/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionCommandLineArguments.java @@ -20,11 +20,11 @@ package org.onap.policy.distribution.main.startstop; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertThrows; import org.junit.Test; -import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.main.PolicyDistributionRuntimeException; /** @@ -36,6 +36,12 @@ import org.onap.policy.distribution.main.PolicyDistributionRuntimeException; public class TestDistributionCommandLineArguments { @Test + public void testDistributionOnlyFileName() { + String[] testArgs = {"src/test/resources/parameters/DistributionConfigParameters.json"}; + assertThrows(PolicyDistributionRuntimeException.class, () -> new DistributionCommandLineArguments(testArgs)); + } + + @Test public void testDistributionCommandLineArgumentsException() { String[] wrongParams = {"arg1", "nothing", "{\"someJson\":1}"}; assertThrows(PolicyDistributionRuntimeException.class, () -> new DistributionCommandLineArguments(wrongParams)); @@ -44,28 +50,32 @@ public class TestDistributionCommandLineArguments { @Test public void testValidateFileNameEmpty() { String[] argsOnlyKeyNoValue = {"-c", ""}; - assertValidate(argsOnlyKeyNoValue, "policy distribution configuration file was not specified as an argument"); + assertValidate(argsOnlyKeyNoValue, "policy-distribution configuration file was not specified as an argument"); } @Test public void testValidateFileNameDoesNotExist() { String[] fileNameNotExistentArgs = {"-c", "someFileName.json"}; assertValidate(fileNameNotExistentArgs, - "policy distribution configuration file \"someFileName.json\" does not exist"); + "policy-distribution configuration file \"someFileName.json\" does not exist"); } @Test public void testValidateFileNameIsNotFile() { String[] folderAsFileNameArgs = {"-c", "src/test/resources/parameters"}; assertValidate(folderAsFileNameArgs, - "policy distribution configuration file \"src/test/resources/parameters\" is not a normal file"); + "policy-distribution configuration file \"src/test/resources/parameters\" is not a normal file"); } - protected void assertValidate(String[] testArgs, String expectedErrorMsg) { + @Test + public void testDistributionVersion() { + String[] testArgs = {"-v"}; DistributionCommandLineArguments sutArgs = new DistributionCommandLineArguments(testArgs); + assertThat(sutArgs.version()).startsWith("ONAP Policy Framework Distribution Service"); + } - assertThatThrownBy(() -> sutArgs.validate()) - .isInstanceOf(PolicyDistributionException.class) - .hasMessage(expectedErrorMsg); + private void assertValidate(String[] testArgs, String expectedErrorMsg) { + DistributionCommandLineArguments sutArgs = new DistributionCommandLineArguments(testArgs); + assertThatThrownBy(() -> sutArgs.validate()).hasMessage(expectedErrorMsg); } } |