diff options
Diffstat (limited to 'main/src')
18 files changed, 990 insertions, 0 deletions
diff --git a/main/src/main/java/org/onap/policy/distribution/main/PolicyDistributionException.java b/main/src/main/java/org/onap/policy/distribution/main/PolicyDistributionException.java new file mode 100644 index 00000000..807fd936 --- /dev/null +++ b/main/src/main/java/org/onap/policy/distribution/main/PolicyDistributionException.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.main; + +/** + * This exception will be called if an error occurs in policy distribution service. + */ +public class PolicyDistributionException extends Exception { + private static final long serialVersionUID = -8507246953751956974L; + + /** + * Instantiates a new policy distribution exception with a message. + * + * @param message the message + */ + public PolicyDistributionException(final String message) { + super(message); + } + + /** + * Instantiates a new policy distribution exception with a message and a caused by exception. + * + * @param message the message + * @param e the exception that caused this exception to be thrown + */ + public PolicyDistributionException(final String message, final Exception e) { + super(message, e); + } +} diff --git a/main/src/main/java/org/onap/policy/distribution/main/PolicyDistributionRuntimeException.java b/main/src/main/java/org/onap/policy/distribution/main/PolicyDistributionRuntimeException.java new file mode 100644 index 00000000..5999e680 --- /dev/null +++ b/main/src/main/java/org/onap/policy/distribution/main/PolicyDistributionRuntimeException.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.main; + +/** + * This runtime exception will be called if a runtime error occurs when using policy distribution. + */ +public class PolicyDistributionRuntimeException extends RuntimeException { + private static final long serialVersionUID = -8507246953751956974L; + + /** + * Instantiates a new policy distribution runtime exception with a message. + * + * @param message the message + */ + public PolicyDistributionRuntimeException(final String message) { + super(message); + } + + /** + * Instantiates a new policy distribution runtime exception with a message and a caused by exception. + * + * @param message the message + * @param e the exception that caused this exception to be thrown + */ + public PolicyDistributionRuntimeException(final String message, final Exception e) { + super(message, e); + } +} diff --git a/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterGroup.java b/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterGroup.java new file mode 100644 index 00000000..f4180e01 --- /dev/null +++ b/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterGroup.java @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.main.parameters; + +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ValidationStatus; + +public class DistributionParameterGroup implements ParameterGroup { + private String name; + + /** + * Create the distribution parameter group. + * + * @param name the parameter group name + */ + public DistributionParameterGroup(final String name) { + this.name = name; + } + + /** + * Return the name of this parameter group instance + * + * @return name the parameter group name + */ + @Override + public String getName() { + return name; + } + + /** + * Validate the parameter group + * + * @return the result of the validation + */ + @Override + public GroupValidationResult validate() { + GroupValidationResult validationResult = new GroupValidationResult(this); + + if (name == null || name.trim().length() == 0) { + validationResult.setResult("name", ValidationStatus.INVALID, "must be a non-blank string"); + } + + return validationResult; + } +} diff --git a/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterHandler.java b/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterHandler.java new file mode 100644 index 00000000..98d302c4 --- /dev/null +++ b/main/src/main/java/org/onap/policy/distribution/main/parameters/DistributionParameterHandler.java @@ -0,0 +1,82 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.main.parameters; + +import java.io.FileReader; + +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.distribution.main.PolicyDistributionException; +import org.onap.policy.distribution.main.startstop.DistributionCommandLineArguments; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +/** + * This class handles reading, parsing and validating of policy distribution parameters from JSON files. + */ +public class DistributionParameterHandler { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(DistributionParameterHandler.class); + + /** + * Read the parameters from the parameter file. + * + * @param arguments the arguments passed to policy distribution + * @return the parameters read from the configuration file + * @throws PolicyDistributionException on parameter exceptions + */ + public DistributionParameterGroup getParameters(final DistributionCommandLineArguments arguments) throws PolicyDistributionException { + DistributionParameterGroup distributionParameterGroup = null; + + // Read the parameters + try { + // Read the parameters from JSON using Gson + final Gson gson = new GsonBuilder().create(); + distributionParameterGroup = gson.fromJson(new FileReader(arguments.getFullConfigurationFilePath()), DistributionParameterGroup.class); + } catch (final Exception e) { + final String errorMessage = "error reading parameters from \"" + arguments.getConfigurationFilePath() + + "\"\n" + "(" + e.getClass().getSimpleName() + "):" + e.getMessage(); + LOGGER.error(errorMessage, e); + throw new PolicyDistributionException(errorMessage, e); + } + + // The JSON processing returns null if there is an empty file + if (distributionParameterGroup == null) { + final String errorMessage = "no parameters found in \"" + arguments.getConfigurationFilePath() + "\""; + LOGGER.error(errorMessage); + throw new PolicyDistributionException(errorMessage); + } + + // validate the parameters + final GroupValidationResult validationResult = distributionParameterGroup.validate(); + if (!validationResult.isValid()) { + String returnMessage = + "validation error(s) on parameters from \"" + arguments.getConfigurationFilePath() + "\"\n"; + returnMessage += validationResult.getResult(); + + LOGGER.error(returnMessage); + throw new PolicyDistributionException(returnMessage); + } + + return distributionParameterGroup; + } +} diff --git a/main/src/main/java/org/onap/policy/distribution/main/startstop/DistributionActivator.java b/main/src/main/java/org/onap/policy/distribution/main/startstop/DistributionActivator.java new file mode 100644 index 00000000..3ac13389 --- /dev/null +++ b/main/src/main/java/org/onap/policy/distribution/main/startstop/DistributionActivator.java @@ -0,0 +1,79 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.main.startstop; + +import org.onap.policy.distribution.main.PolicyDistributionException; +import org.onap.policy.distribution.main.parameters.DistributionParameterGroup; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class wraps a distributor so that it can be activated as a complete service together with + * all its distribution and forwarding handlers. + */ +public class DistributionActivator { + // The logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(DistributionActivator.class); + + // The parameters of this policy distribution activator + private final DistributionParameterGroup distributionParameterGroup; + + /** + * Instantiate the activator for policy distribution as a complete service. + * + * @param distributionParameterGroup the parameters for the distribution service + */ + public DistributionActivator(final DistributionParameterGroup distributionParameterGroup) { + this.distributionParameterGroup = distributionParameterGroup; + } + + /** + * Initialize distribution as a complete service. + * + * @throws PolicyDistributionException on errors in initializing the service + */ + public void initialize() throws PolicyDistributionException { + LOGGER.debug("Policy distribution starting as a service . . ."); + + // Real code for starting up the handlers goes here + + LOGGER.debug("Policy distribution started as a service"); + } + + /** + * Terminate policy distribution. + * + * @throws PolicyDistributionException on termination errors + */ + public void terminate() throws PolicyDistributionException { + // Shut down all handlers + + } + + /** + * Get the parameters used by the activator. + * + * @return the parameters of the activator + */ + public DistributionParameterGroup getParameterGroup() { + return distributionParameterGroup; + } +} 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 new file mode 100644 index 00000000..7bd1d06d --- /dev/null +++ b/main/src/main/java/org/onap/policy/distribution/main/startstop/DistributionCommandLineArguments.java @@ -0,0 +1,243 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +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.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; + + /** + * Construct the options for the CLI editor. + */ + 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 + } + + /** + * Construct the options for the CLI editor and parse in the given arguments. + * + * @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) { + 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.getCanonicalName()); + } + + 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 stringPW = new PrintWriter(stringWriter); + + helpFormatter.printHelp(stringPW, 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 new file mode 100644 index 00000000..019b7786 --- /dev/null +++ b/main/src/main/java/org/onap/policy/distribution/main/startstop/Main.java @@ -0,0 +1,145 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.main.startstop; + +import java.util.Arrays; + +import org.onap.policy.distribution.main.PolicyDistributionException; +import org.onap.policy.distribution.main.parameters.DistributionParameterGroup; +import org.onap.policy.distribution.main.parameters.DistributionParameterHandler; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + +/** + * This class initiates ONAP Policy Framework policy distribution. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class Main { + private static final XLogger LOGGER = XLoggerFactory.getXLogger(Main.class); + + // The policy distribution Activator that activates the policy distribution service + private DistributionActivator activator; + + // The parameters read in from JSON + private DistributionParameterGroup parameterGroup; + + /** + * Instantiates the policy distribution service. + * + * @param args the command line arguments + */ + public Main(final String[] args) { + String argumentString = Arrays.toString(args); + LOGGER.info("Starting policy distribution service with arguments {} . . .", argumentString); + + // Check the arguments + final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); + try { + // The arguments return a string if there is a message to print and we should exit + final String argumentMessage = arguments.parse(args); + if (argumentMessage != null) { + LOGGER.info(argumentMessage); + return; + } + + // Validate that the arguments are sane + arguments.validate(); + } catch (final PolicyDistributionException e) { + LOGGER.error("start of policy distribution service failed", e); + return; + } + + // Read the parameters + try { + parameterGroup = new DistributionParameterHandler().getParameters(arguments); + } catch (final Exception e) { + LOGGER.error("start of policy distribution service failed", e); + return; + } + + // Now, create the activator for the policy distribution service + activator = new DistributionActivator(parameterGroup); + + // Start the activator + try { + activator.initialize(); + } catch (final PolicyDistributionException e) { + LOGGER.error("start of policy distribution service failed, used parameters are " + Arrays.toString(args), + e); + return; + } + + // Add a shutdown hook to shut everything down in an orderly manner + Runtime.getRuntime().addShutdownHook(new PolicyDistributionShutdownHookClass()); + LOGGER.exit("Started policy distribution service"); + } + + /** + * Get the parameters specified in JSON. + * + * @return the parameters + */ + public DistributionParameterGroup getParameters() { + return parameterGroup; + } + + /** + * Shut down Execution. + * + * @throws PolicyDistributionException on shutdown errors + */ + public void shutdown() throws PolicyDistributionException { + if (activator != null) { + activator.terminate(); + } + } + + /** + * The Class PolicyDistributionShutdownHookClass terminates the policy distribution service when its run method is + * called. + */ + private class PolicyDistributionShutdownHookClass extends Thread { + /* + * (non-Javadoc) + * + * @see java.lang.Runnable#run() + */ + @Override + public void run() { + try { + // Shutdown the policy distribution service and wait for everything to stop + activator.terminate(); + } catch (final PolicyDistributionException e) { + LOGGER.warn("error occured during shut down of the policy distribution service", e); + } + } + } + + /** + * The main method. + * + * @param args the arguments + */ + public static void main(final String[] args) { + new Main(args); + } +} diff --git a/main/src/main/java/org/onap/policy/distribution/main/startstop/package-info.java b/main/src/main/java/org/onap/policy/distribution/main/startstop/package-info.java new file mode 100644 index 00000000..19b9f951 --- /dev/null +++ b/main/src/main/java/org/onap/policy/distribution/main/startstop/package-info.java @@ -0,0 +1,25 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +/** + * Provides ONAP policy distribution as a complete service together with all its reception and forwarding support. + * A main method to allow ONAP policy distribution execution from the command line is also provided. + */ +package org.onap.policy.distribution.main.startstop; diff --git a/main/src/main/resources/version.txt b/main/src/main/resources/version.txt new file mode 100644 index 00000000..c617841e --- /dev/null +++ b/main/src/main/resources/version.txt @@ -0,0 +1,4 @@ +ONAP Policy Framework Distribution Service +Version: ${project.version} +Built (UTC): ${maven.build.timestamp} +ONAP https://wiki.onap.org
\ No newline at end of file diff --git a/main/src/test/java/org/onap/policy/distribution/main/TestExceptions.java b/main/src/test/java/org/onap/policy/distribution/main/TestExceptions.java new file mode 100644 index 00000000..b3140978 --- /dev/null +++ b/main/src/test/java/org/onap/policy/distribution/main/TestExceptions.java @@ -0,0 +1,39 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.main; + +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; + +import org.junit.Test; + +public class TestExceptions { + + @Test + public void test() { + assertNotNull(new PolicyDistributionException("Message")); + assertNotNull(new PolicyDistributionException("Message", new IOException())); + + assertNotNull(new PolicyDistributionRuntimeException("Message")); + assertNotNull(new PolicyDistributionRuntimeException("Message", new IOException())); + } +} diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestParameterGroup.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestParameterGroup.java new file mode 100644 index 00000000..bf553918 --- /dev/null +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestParameterGroup.java @@ -0,0 +1,46 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.main.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class TestParameterGroup { + + @Test + public void testParameterGroup() { + DistributionParameterGroup parameterGroup = new DistributionParameterGroup("groupName"); + assertEquals("groupName", parameterGroup.getName()); + assertTrue(parameterGroup.isValid()); + + parameterGroup = new DistributionParameterGroup(null); + assertNull(parameterGroup.getName()); + assertFalse(parameterGroup.isValid()); + + parameterGroup = new DistributionParameterGroup(""); + assertEquals("", parameterGroup.getName()); + assertFalse(parameterGroup.isValid()); + } +} diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestParameterHandler.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestParameterHandler.java new file mode 100644 index 00000000..5b938988 --- /dev/null +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestParameterHandler.java @@ -0,0 +1,127 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.main.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.distribution.main.PolicyDistributionException; +import org.onap.policy.distribution.main.startstop.DistributionCommandLineArguments; + +public class TestParameterHandler { + @Test + public void testParameterHandlerNoParameterFile() throws PolicyDistributionException { + String[] noArgumentString = { "-c", "parameters/NoParameterFile.json" }; + + DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments(); + noArguments.parse(noArgumentString); + + try { + new DistributionParameterHandler().getParameters(noArguments); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("error reading parameters from \"parameters/NoParameterFile.json\"\n" + + "(FileNotFoundException):parameters/NoParameterFile.json (No such file or directory)", + e.getMessage()); + } + } + + @Test + public void testParameterHandlerEmptyParameters() throws PolicyDistributionException { + String[] emptyArgumentString = { "-c", "parameters/EmptyParameters.json" }; + + DistributionCommandLineArguments emptyArguments = new DistributionCommandLineArguments(); + emptyArguments.parse(emptyArgumentString); + + try { + new DistributionParameterHandler().getParameters(emptyArguments); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("no parameters found in \"parameters/EmptyParameters.json\"", e.getMessage()); + } + } + + @Test + public void testParameterHandlerBadParameters() throws PolicyDistributionException { + String[] badArgumentString = { "-c", "parameters/BadParameters.json" }; + + DistributionCommandLineArguments badArguments = new DistributionCommandLineArguments(); + badArguments.parse(badArgumentString); + + try { + new DistributionParameterHandler().getParameters(badArguments); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("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", e.getMessage()); + } + } + + @Test + public void testParameterHandlerInvalidParameters() throws PolicyDistributionException { + String[] invalidArgumentString = { "-c", "parameters/InvalidParameters.json" }; + + DistributionCommandLineArguments invalidArguments = new DistributionCommandLineArguments(); + invalidArguments.parse(invalidArgumentString); + + try { + new DistributionParameterHandler().getParameters(invalidArguments); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("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", e.getMessage()); + } + } + + @Test + public void testParameterHandlerNoParameters() throws PolicyDistributionException { + String[] noArgumentString = { "-c", "parameters/NoParameters.json" }; + + DistributionCommandLineArguments noArguments = new DistributionCommandLineArguments(); + noArguments.parse(noArgumentString); + + try { + new DistributionParameterHandler().getParameters(noArguments); + fail("test should throw an exception here"); + } catch (Exception e) { + assertEquals("validation error(s) on parameters from \"parameters/NoParameters.json\"\n" + + "parameter group \"null\" type " + + "\"org.onap.policy.distribution.main.parameters.DistributionParameterGroup\"" + + " INVALID, parameter group has status INVALID\n" + + " field \"name\" type \"java.lang.String\" value \"null\" INVALID, must be a non-blank string\n", + e.getMessage()); + } + } + + @Test + public void testParameterHandlerMinumumParameters() throws PolicyDistributionException { + String[] minArgumentString = { "-c", "parameters/MinimumParameters.json" }; + + DistributionCommandLineArguments minArguments = new DistributionCommandLineArguments(); + minArguments.parse(minArgumentString); + + DistributionParameterGroup parGroup = new DistributionParameterHandler().getParameters(minArguments); + assertEquals("nameFromFile", parGroup.getName()); + } +} diff --git a/main/src/test/java/org/onap/policy/distribution/main/startstop/TestMain.java b/main/src/test/java/org/onap/policy/distribution/main/startstop/TestMain.java new file mode 100644 index 00000000..781a2ab8 --- /dev/null +++ b/main/src/test/java/org/onap/policy/distribution/main/startstop/TestMain.java @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.distribution.main.startstop; + +import org.junit.Test; + +public class TestMain { + + @Test + public void testMain() { + Main.main(null); + } +} diff --git a/main/src/test/resources/parameters/BadParameters.json b/main/src/test/resources/parameters/BadParameters.json new file mode 100644 index 00000000..de2040cc --- /dev/null +++ b/main/src/test/resources/parameters/BadParameters.json @@ -0,0 +1,3 @@ +{ + "name" : [] +}
\ No newline at end of file diff --git a/main/src/test/resources/parameters/EmptyParameters.json b/main/src/test/resources/parameters/EmptyParameters.json new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/main/src/test/resources/parameters/EmptyParameters.json diff --git a/main/src/test/resources/parameters/InvalidParameters.json b/main/src/test/resources/parameters/InvalidParameters.json new file mode 100644 index 00000000..de2040cc --- /dev/null +++ b/main/src/test/resources/parameters/InvalidParameters.json @@ -0,0 +1,3 @@ +{ + "name" : [] +}
\ No newline at end of file diff --git a/main/src/test/resources/parameters/MinimumParameters.json b/main/src/test/resources/parameters/MinimumParameters.json new file mode 100644 index 00000000..02b2afad --- /dev/null +++ b/main/src/test/resources/parameters/MinimumParameters.json @@ -0,0 +1,3 @@ +{ + "name": "nameFromFile" +}
\ No newline at end of file diff --git a/main/src/test/resources/parameters/NoParameters.json b/main/src/test/resources/parameters/NoParameters.json new file mode 100644 index 00000000..7a73a41b --- /dev/null +++ b/main/src/test/resources/parameters/NoParameters.json @@ -0,0 +1,2 @@ +{ +}
\ No newline at end of file |