From 046b5040ce97e5faf59f3f302331bd9da6e80d02 Mon Sep 17 00:00:00 2001 From: ramverma Date: Mon, 13 Aug 2018 17:19:09 +0100 Subject: Adding code for bootstrapping policy distribution * Code changes for initializing handlers & related plugins from configuration parameter JSON file. * Using common parameter service for refering parameters at multiple places. * Moved related parameters classes from "main" to "reception" to avoid maven cyclic dependency errors. * Added test cases for new code chnages. The test coverage is around 95%. * Changed logging from slf4j to common-logging. Change-Id: Ifb77cfaa6e6472d43295a7c41a49ddd657c0e2c2 Issue-ID: POLICY-1035 Signed-off-by: ramverma --- .../parameters/DistributionParameterGroup.java | 1 + .../parameters/DistributionParameterHandler.java | 24 +++-- .../main/parameters/PluginHandlerParameters.java | 102 ------------------ .../main/parameters/PolicyDecoderParameters.java | 97 ----------------- .../main/parameters/PolicyForwarderParameters.java | 97 ----------------- .../parameters/ReceptionHandlerParameters.java | 116 --------------------- .../main/startstop/DistributionActivator.java | 72 ++++++++++--- .../policy/distribution/main/startstop/Main.java | 24 +++-- .../main/parameters/CommonTestData.java | 39 +++---- .../parameters/TestDistributionParameterGroup.java | 55 +++++----- .../TestDistributionParameterHandler.java | 20 ++-- .../parameters/TestPluginHandlerParameters.java | 10 +- .../parameters/TestPolicyDecoderParameters.java | 29 +++--- .../parameters/TestPolicyForwarderParameters.java | 31 +++--- .../parameters/TestReceptionHandlerParameters.java | 34 +++--- .../main/startstop/TestDistributionActivator.java | 65 ++++++++++++ .../distribution/main/startstop/TestMain.java | 52 ++++++++- 17 files changed, 319 insertions(+), 549 deletions(-) delete mode 100644 main/src/main/java/org/onap/policy/distribution/main/parameters/PluginHandlerParameters.java delete mode 100644 main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyDecoderParameters.java delete mode 100644 main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyForwarderParameters.java delete mode 100644 main/src/main/java/org/onap/policy/distribution/main/parameters/ReceptionHandlerParameters.java create mode 100644 main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionActivator.java (limited to 'main') 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 index d84d4c4b..df0a272c 100644 --- 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 @@ -26,6 +26,7 @@ import java.util.Map.Entry; import org.onap.policy.common.parameters.GroupValidationResult; import org.onap.policy.common.parameters.ParameterGroup; import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParameters; /** * Class to hold all parameters needed for Distribution component. 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 index 98d302c4..3174b8c6 100644 --- 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 @@ -5,37 +5,37 @@ * 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 com.google.gson.Gson; +import com.google.gson.GsonBuilder; + import java.io.FileReader; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; 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); + private static final Logger LOGGER = FlexLogger.getLogger(DistributionParameterHandler.class); /** * Read the parameters from the parameter file. @@ -44,14 +44,16 @@ public class DistributionParameterHandler { * @return the parameters read from the configuration file * @throws PolicyDistributionException on parameter exceptions */ - public DistributionParameterGroup getParameters(final DistributionCommandLineArguments arguments) throws PolicyDistributionException { + 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); + 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(); diff --git a/main/src/main/java/org/onap/policy/distribution/main/parameters/PluginHandlerParameters.java b/main/src/main/java/org/onap/policy/distribution/main/parameters/PluginHandlerParameters.java deleted file mode 100644 index 39479131..00000000 --- a/main/src/main/java/org/onap/policy/distribution/main/parameters/PluginHandlerParameters.java +++ /dev/null @@ -1,102 +0,0 @@ -/*- - * ============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.util.Map; -import java.util.Map.Entry; - -import org.onap.policy.common.parameters.GroupValidationResult; -import org.onap.policy.common.parameters.ParameterGroup; -import org.onap.policy.common.parameters.ValidationStatus; - -/** - * Class to hold all the plugin handler parameters. - * - * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) - */ -public class PluginHandlerParameters implements ParameterGroup { - - private Map policyDecoders; - private Map policyForwarders; - - /** - * Constructor for instantiating PluginHandlerParameters. - * - * @param policyDecoders the map of policy decoders - * @param policyForwarders the map of policy forwarders - */ - public PluginHandlerParameters(final Map policyDecoders, - final Map policyForwarders) { - this.policyDecoders = policyDecoders; - this.policyForwarders = policyForwarders; - } - - /** - * Return the policyDecoders of this PluginHandlerParameters instance. - * - * @return the policyDecoders - */ - public Map getPolicyDecoders() { - return policyDecoders; - } - - /** - * Return the policyForwarders of this PluginHandlerParameters instance. - * - * @return the policyForwarders - */ - public Map getPolicyForwarders() { - return policyForwarders; - } - - @Override - public String getName() { - return null; - } - - /** - * Validate the plugin handler parameters. - * - */ - @Override - public GroupValidationResult validate() { - final GroupValidationResult validationResult = new GroupValidationResult(this); - if (policyDecoders == null || policyDecoders.size() == 0) { - validationResult.setResult("policyDecoders", ValidationStatus.INVALID, - "must have at least one policy decoder"); - } else { - for (final Entry nestedGroupEntry : policyDecoders.entrySet()) { - validationResult.setResult("policyDecoders", nestedGroupEntry.getKey(), - nestedGroupEntry.getValue().validate()); - } - } - if (policyForwarders == null || policyForwarders.size() == 0) { - validationResult.setResult("policyForwarders", ValidationStatus.INVALID, - "must have at least one policy forwarder"); - } else { - for (final Entry nestedGroupEntry : policyForwarders.entrySet()) { - validationResult.setResult("policyForwarders", nestedGroupEntry.getKey(), - nestedGroupEntry.getValue().validate()); - } - } - return validationResult; - } -} diff --git a/main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyDecoderParameters.java b/main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyDecoderParameters.java deleted file mode 100644 index c8020479..00000000 --- a/main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyDecoderParameters.java +++ /dev/null @@ -1,97 +0,0 @@ -/*- - * ============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.GroupValidationResult; -import org.onap.policy.common.parameters.ParameterGroup; -import org.onap.policy.common.parameters.ValidationStatus; - -/** - * Class to hold all the policy decoder parameters. - * - * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) - */ -public class PolicyDecoderParameters implements ParameterGroup { - private String decoderType; - private String decoderClassName; - - /** - * Constructor for instantiating PolicyDecoderParameters. - * - * @param decoderType the policy decoder type - * @param decoderClassName the policy decoder class name - */ - public PolicyDecoderParameters(final String decoderType, final String decoderClassName) { - this.decoderType = decoderType; - this.decoderClassName = decoderClassName; - } - - /** - * Return the decoderType of this PolicyDecoderParameters instance. - * - * @return the decoderType - */ - public String getDecoderType() { - return decoderType; - } - - /** - * Return the decoderClassName of this PolicyDecoderParameters instance. - * - * @return the decoderClassName - */ - public String getDecoderClassName() { - return decoderClassName; - } - - @Override - public String getName() { - return null; - } - - /** - * Validate the policy decoder parameters. - * - */ - @Override - public GroupValidationResult validate() { - final GroupValidationResult validationResult = new GroupValidationResult(this); - if (decoderType == null || decoderType.trim().length() == 0) { - validationResult.setResult("decoderType", ValidationStatus.INVALID, "must be a non-blank string"); - } - if (decoderClassName == null || decoderClassName.trim().length() == 0) { - validationResult.setResult("decoderClassName", ValidationStatus.INVALID, - "must be a non-blank string containing full class name of the decoder"); - } else { - validatePolicyDecoderClass(validationResult); - } - return validationResult; - } - - private void validatePolicyDecoderClass(final GroupValidationResult validationResult) { - try { - Class.forName(decoderClassName); - } catch (final ClassNotFoundException e) { - validationResult.setResult("decoderClassName", ValidationStatus.INVALID, - "policy decoder class not found in classpath"); - } - } -} diff --git a/main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyForwarderParameters.java b/main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyForwarderParameters.java deleted file mode 100644 index 49ad1c8e..00000000 --- a/main/src/main/java/org/onap/policy/distribution/main/parameters/PolicyForwarderParameters.java +++ /dev/null @@ -1,97 +0,0 @@ -/*- - * ============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.GroupValidationResult; -import org.onap.policy.common.parameters.ParameterGroup; -import org.onap.policy.common.parameters.ValidationStatus; - -/** - * Class to hold all the policy forwarder parameters. - * - * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) - */ -public class PolicyForwarderParameters implements ParameterGroup { - private String forwarderType; - private String forwarderClassName; - - /** - * Constructor for instantiating PolicyForwarderParameters. - * - * @param forwarderType the policy forwarder type - * @param forwarderClassName the policy forwarder class name - */ - public PolicyForwarderParameters(final String forwarderType, final String forwarderClassName) { - this.forwarderType = forwarderType; - this.forwarderClassName = forwarderClassName; - } - - /** - * Return the forwarderType of this PolicyForwarderParameters instance. - * - * @return the forwarderType - */ - public String getForwarderType() { - return forwarderType; - } - - /** - * Return the forwarderClassName of this PolicyForwarderParameters instance. - * - * @return the forwarderClassName - */ - public String getForwarderClassName() { - return forwarderClassName; - } - - @Override - public String getName() { - return null; - } - - /** - * Validate the policy forwarder parameters. - * - */ - @Override - public GroupValidationResult validate() { - final GroupValidationResult validationResult = new GroupValidationResult(this); - if (forwarderType == null || forwarderType.trim().length() == 0) { - validationResult.setResult("forwarderType", ValidationStatus.INVALID, "must be a non-blank string"); - } - if (forwarderClassName == null || forwarderClassName.trim().length() == 0) { - validationResult.setResult("forwarderClassName", ValidationStatus.INVALID, - "must be a non-blank string containing full class name of the forwarder"); - } else { - validatePolicyForwarderClass(validationResult); - } - return validationResult; - } - - private void validatePolicyForwarderClass(final GroupValidationResult validationResult) { - try { - Class.forName(forwarderClassName); - } catch (final ClassNotFoundException e) { - validationResult.setResult("forwarderClassName", ValidationStatus.INVALID, - "policy forwarder class not found in classpath"); - } - } -} diff --git a/main/src/main/java/org/onap/policy/distribution/main/parameters/ReceptionHandlerParameters.java b/main/src/main/java/org/onap/policy/distribution/main/parameters/ReceptionHandlerParameters.java deleted file mode 100644 index a3c2c16c..00000000 --- a/main/src/main/java/org/onap/policy/distribution/main/parameters/ReceptionHandlerParameters.java +++ /dev/null @@ -1,116 +0,0 @@ -/*- - * ============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.GroupValidationResult; -import org.onap.policy.common.parameters.ParameterGroup; -import org.onap.policy.common.parameters.ValidationStatus; - -/** - * Class to hold all the reception handler parameters. - * - * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) - */ -public class ReceptionHandlerParameters implements ParameterGroup { - private String receptionHandlerType; - private String receptionHandlerClassName; - private PluginHandlerParameters pluginHandlerParameters; - - /** - * Constructor for instantiating ReceptionHandlerParameters. - * - * @param receptionHandlerType the reception handler type - * @param receptionHandlerClassName the reception handler class name - * @param pluginHandlerParameters the plugin handler parameters - */ - public ReceptionHandlerParameters(final String receptionHandlerType, final String receptionHandlerClassName, - final PluginHandlerParameters pluginHandlerParameters) { - this.receptionHandlerType = receptionHandlerType; - this.receptionHandlerClassName = receptionHandlerClassName; - this.pluginHandlerParameters = pluginHandlerParameters; - } - - /** - * Return the receptionHandlerType of this ReceptionHandlerParameters instance. - * - * @return the receptionHandlerType - */ - public String getReceptionHandlerType() { - return receptionHandlerType; - } - - /** - * Return the receptionHandlerClassName of this ReceptionHandlerParameters instance. - * - * @return the receptionHandlerClassName - */ - public String getReceptionHandlerClassName() { - return receptionHandlerClassName; - } - - /** - * Return the pluginHandlerParameters of this ReceptionHandlerParameters instance. - * - * @return the pluginHandlerParameters - */ - public PluginHandlerParameters getPluginHandlerParameters() { - return pluginHandlerParameters; - } - - @Override - public String getName() { - return null; - } - - /** - * Validate the reception handler parameters. - * - */ - @Override - public GroupValidationResult validate() { - final GroupValidationResult validationResult = new GroupValidationResult(this); - if (receptionHandlerType == null || receptionHandlerType.trim().length() == 0) { - validationResult.setResult("receptionHandlerType", ValidationStatus.INVALID, "must be a non-blank string"); - } - if (receptionHandlerClassName == null || receptionHandlerClassName.trim().length() == 0) { - validationResult.setResult("receptionHandlerClassName", ValidationStatus.INVALID, - "must be a non-blank string containing full class name of the reception handler"); - } else { - validateReceptionHandlerClass(validationResult); - } - if (pluginHandlerParameters == null) { - validationResult.setResult("pluginHandlerParameters", ValidationStatus.INVALID, - "must have a plugin handler"); - } else { - validationResult.setResult("pluginHandlerParameters", pluginHandlerParameters.validate()); - } - return validationResult; - } - - private void validateReceptionHandlerClass(final GroupValidationResult validationResult) { - try { - Class.forName(receptionHandlerClassName); - } catch (final ClassNotFoundException e) { - validationResult.setResult("receptionHandlerClassName", ValidationStatus.INVALID, - "reception handler class not found in classpath"); - } - } -} 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 index 3ac13389..43eb4f80 100644 --- 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 @@ -5,33 +5,38 @@ * 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.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.distribution.forwarding.PolicyForwardingException; 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; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; +import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler; +import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParameters; /** - * This class wraps a distributor so that it can be activated as a complete service together with - * all its distribution and forwarding handlers. + * 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); + private static final Logger LOGGER = FlexLogger.getLogger(DistributionActivator.class); // The parameters of this policy distribution activator private final DistributionParameterGroup distributionParameterGroup; @@ -50,11 +55,22 @@ public class DistributionActivator { * * @throws PolicyDistributionException on errors in initializing the service */ + @SuppressWarnings("unchecked") public void initialize() throws PolicyDistributionException { LOGGER.debug("Policy distribution starting as a service . . ."); - - // Real code for starting up the handlers goes here - + registerToParameterService(distributionParameterGroup); + for (final ReceptionHandlerParameters rHParameters : distributionParameterGroup.getReceptionHandlerParameters() + .values()) { + try { + final Class receptionHandlerClass = + (Class) Class.forName(rHParameters.getReceptionHandlerClassName()); + final AbstractReceptionHandler receptionHandler = receptionHandlerClass.newInstance(); + receptionHandler.initialize(rHParameters.getName()); + } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException + | PolicyDecodingException | PolicyForwardingException exp) { + throw new PolicyDistributionException(exp.getMessage(), exp); + } + } LOGGER.debug("Policy distribution started as a service"); } @@ -65,7 +81,7 @@ public class DistributionActivator { */ public void terminate() throws PolicyDistributionException { // Shut down all handlers - + deregisterToParameterService(distributionParameterGroup); } /** @@ -76,4 +92,36 @@ public class DistributionActivator { public DistributionParameterGroup getParameterGroup() { return distributionParameterGroup; } + + /** + * Method to register the parameters to Common Parameter Service. + * + * @param distributionParameterGroup + */ + public void registerToParameterService(final DistributionParameterGroup distributionParameterGroup) { + ParameterService.register(distributionParameterGroup); + for (final ReceptionHandlerParameters params : distributionParameterGroup.getReceptionHandlerParameters() + .values()) { + params.setName(distributionParameterGroup.getName()); + params.getPluginHandlerParameters().setName(distributionParameterGroup.getName()); + ParameterService.register(params); + ParameterService.register(params.getPluginHandlerParameters()); + } + } + + /** + * Method to deregister the parameters from Common Parameter Service. + * + * @param distributionParameterGroup + */ + public void deregisterToParameterService(final DistributionParameterGroup distributionParameterGroup) { + ParameterService.deregister(distributionParameterGroup.getName()); + for (final ReceptionHandlerParameters params : distributionParameterGroup.getReceptionHandlerParameters() + .values()) { + params.setName(distributionParameterGroup.getName()); + params.getPluginHandlerParameters().setName(distributionParameterGroup.getName()); + ParameterService.deregister((params.getName())); + ParameterService.deregister((params.getPluginHandlerParameters().getName())); + } + } } 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 019b7786..3ff85b8e 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 @@ -5,15 +5,15 @@ * 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========================================================= */ @@ -22,11 +22,11 @@ package org.onap.policy.distribution.main.startstop; import java.util.Arrays; +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; 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. @@ -34,7 +34,7 @@ import org.slf4j.ext.XLoggerFactory; * @author Liam Fallon (liam.fallon@ericsson.com) */ public class Main { - private static final XLogger LOGGER = XLoggerFactory.getXLogger(Main.class); + private static final Logger LOGGER = FlexLogger.getLogger(Main.class); // The policy distribution Activator that activates the policy distribution service private DistributionActivator activator; @@ -48,8 +48,8 @@ public class Main { * @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); + final String argumentString = Arrays.toString(args); + LOGGER.info("Starting policy distribution service with arguments - " + argumentString); // Check the arguments final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); @@ -84,13 +84,13 @@ public class Main { activator.initialize(); } catch (final PolicyDistributionException e) { LOGGER.error("start of policy distribution service failed, used parameters are " + Arrays.toString(args), - e); + 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"); + LOGGER.info("Started policy distribution service"); } /** @@ -108,6 +108,10 @@ public class Main { * @throws PolicyDistributionException on shutdown errors */ public void shutdown() throws PolicyDistributionException { + // clear the parameterGroup variable + parameterGroup = null; + + // clear the distribution activator if (activator != null) { activator.terminate(); } diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/CommonTestData.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/CommonTestData.java index 953fa7b0..54716fc8 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/CommonTestData.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/CommonTestData.java @@ -23,6 +23,11 @@ package org.onap.policy.distribution.main.parameters; import java.util.HashMap; import java.util.Map; +import org.onap.policy.distribution.forwarding.parameters.PolicyForwarderParameters; +import org.onap.policy.distribution.reception.parameters.PluginHandlerParameters; +import org.onap.policy.distribution.reception.parameters.PolicyDecoderParameters; +import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParameters; + /** * Class to hold/create all parameters for test cases. * @@ -30,15 +35,19 @@ import java.util.Map; */ public class CommonTestData { - public static final String decoderType = "TOSCA"; - public static final String decoderClassName = + public static final String DISTRIBUTION_GROUP_NAME = "SDCDistributionGroup"; + public static final String DECODER_TYPE = "TOSCA"; + public static final String DECODER_CLASS_NAME = "org.onap.policy.distribution.reception.decoding.pdpx.PolicyDecoderToscaPdpx"; - public static final String forwarderType = "PAPEngine"; - public static final String forwarderClassName = + public static final String FORWARDER_TYPE = "PAPEngine"; + public static final String FORWARDER_CLASS_NAME = "org.onap.policy.distribution.forwarding.pap.engine.XacmlPapServletPolicyForwarder"; - public static final String receptionHandlerType = "SDCReceptionHandler"; - public static final String receptionHandlerClassName = + public static final String RECEPTION_HANDLER_TYPE = "SDC"; + public static final String RECEPTION_HANDLER_CLASS_NAME = "org.onap.policy.distribution.reception.handling.sdc.SdcReceptionHandler"; + public static final String SDC_RECEPTION_HANDLER_KEY = "SDCReceptionHandler"; + public static final String PAP_ENGINE_FORWARDER_KEY = "PAPEngineForwarder"; + public static final String TOSCA_DECODER_KEY = "TOSCADecoder"; /** * Returns an instance of ReceptionHandlerParameters for test cases. @@ -52,13 +61,10 @@ public class CommonTestData { if (!isEmpty) { final Map policyDecoders = getPolicyDecoders(isEmpty); final Map policyForwarders = getPolicyForwarders(isEmpty); - final String receptionHandlerType = "SDC"; - final String receptionHandlerClassName = - "org.onap.policy.distribution.reception.handling.sdc.SdcReceptionHandler"; final PluginHandlerParameters pHParameters = new PluginHandlerParameters(policyDecoders, policyForwarders); final ReceptionHandlerParameters rhParameters = - new ReceptionHandlerParameters(receptionHandlerType, receptionHandlerClassName, pHParameters); - receptionHandlerParameters.put("SDCReceptionHandler", rhParameters); + new ReceptionHandlerParameters(RECEPTION_HANDLER_TYPE, RECEPTION_HANDLER_CLASS_NAME, pHParameters); + receptionHandlerParameters.put(SDC_RECEPTION_HANDLER_KEY, rhParameters); } return receptionHandlerParameters; } @@ -87,12 +93,9 @@ public class CommonTestData { final Map policyForwarders = new HashMap(); if (!isEmpty) { - final String forwarderType = "PAPEngine"; - final String forwarderClassName = - "org.onap.policy.distribution.forwarding.pap.engine.XacmlPapServletPolicyForwarder"; final PolicyForwarderParameters pFParameters = - new PolicyForwarderParameters(forwarderType, forwarderClassName); - policyForwarders.put("PAPEngineForwarder", pFParameters); + new PolicyForwarderParameters(FORWARDER_TYPE, FORWARDER_CLASS_NAME); + policyForwarders.put(PAP_ENGINE_FORWARDER_KEY, pFParameters); } return policyForwarders; } @@ -106,8 +109,8 @@ public class CommonTestData { public Map getPolicyDecoders(final boolean isEmpty) { final Map policyDecoders = new HashMap(); if (!isEmpty) { - final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters(decoderType, decoderClassName); - policyDecoders.put("TOSCADecoder", pDParameters); + final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters(DECODER_TYPE, DECODER_CLASS_NAME); + policyDecoders.put(TOSCA_DECODER_KEY, pDParameters); } return policyDecoders; } diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterGroup.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterGroup.java index cce432dc..70317971 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterGroup.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestDistributionParameterGroup.java @@ -29,6 +29,7 @@ import java.util.Map; import org.junit.Test; import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParameters; /** * Class to perform unit test of DistributionParameterGroup. @@ -40,23 +41,24 @@ public class TestDistributionParameterGroup { @Test public void testDistributionParameterGroup() { - final String name = "SDCDistributionGroup"; final Map receptionHandlerParameters = commonTestData.getReceptionHandlerParameters(false); final DistributionParameterGroup distributionParameters = - new DistributionParameterGroup(name, receptionHandlerParameters); + new DistributionParameterGroup(CommonTestData.DISTRIBUTION_GROUP_NAME, receptionHandlerParameters); final GroupValidationResult validationResult = distributionParameters.validate(); assertTrue(validationResult.isValid()); - assertEquals(name, distributionParameters.getName()); - assertEquals(receptionHandlerParameters.get("SDCReceptionHandler").getReceptionHandlerType(), - distributionParameters.getReceptionHandlerParameters().get("SDCReceptionHandler") + assertEquals(CommonTestData.DISTRIBUTION_GROUP_NAME, distributionParameters.getName()); + assertEquals(receptionHandlerParameters.get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getReceptionHandlerType(), + distributionParameters.getReceptionHandlerParameters().get(CommonTestData.SDC_RECEPTION_HANDLER_KEY) .getReceptionHandlerType()); - assertEquals(receptionHandlerParameters.get("SDCReceptionHandler").getReceptionHandlerClassName(), - distributionParameters.getReceptionHandlerParameters().get("SDCReceptionHandler") + assertEquals( + receptionHandlerParameters.get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getReceptionHandlerClassName(), + distributionParameters.getReceptionHandlerParameters().get(CommonTestData.SDC_RECEPTION_HANDLER_KEY) .getReceptionHandlerClassName()); - assertEquals(receptionHandlerParameters.get("SDCReceptionHandler").getPluginHandlerParameters(), - distributionParameters.getReceptionHandlerParameters().get("SDCReceptionHandler") + assertEquals( + receptionHandlerParameters.get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getPluginHandlerParameters(), + distributionParameters.getReceptionHandlerParameters().get(CommonTestData.SDC_RECEPTION_HANDLER_KEY) .getPluginHandlerParameters()); } @@ -70,14 +72,16 @@ public class TestDistributionParameterGroup { final GroupValidationResult validationResult = distributionParameters.validate(); assertFalse(validationResult.isValid()); assertEquals(null, distributionParameters.getName()); - assertEquals(receptionHandlerParameters.get("SDCReceptionHandler").getReceptionHandlerType(), - distributionParameters.getReceptionHandlerParameters().get("SDCReceptionHandler") + assertEquals(receptionHandlerParameters.get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getReceptionHandlerType(), + distributionParameters.getReceptionHandlerParameters().get(CommonTestData.SDC_RECEPTION_HANDLER_KEY) .getReceptionHandlerType()); - assertEquals(receptionHandlerParameters.get("SDCReceptionHandler").getReceptionHandlerClassName(), - distributionParameters.getReceptionHandlerParameters().get("SDCReceptionHandler") + assertEquals( + receptionHandlerParameters.get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getReceptionHandlerClassName(), + distributionParameters.getReceptionHandlerParameters().get(CommonTestData.SDC_RECEPTION_HANDLER_KEY) .getReceptionHandlerClassName()); - assertEquals(receptionHandlerParameters.get("SDCReceptionHandler").getPluginHandlerParameters(), - distributionParameters.getReceptionHandlerParameters().get("SDCReceptionHandler") + assertEquals( + receptionHandlerParameters.get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getPluginHandlerParameters(), + distributionParameters.getReceptionHandlerParameters().get(CommonTestData.SDC_RECEPTION_HANDLER_KEY) .getPluginHandlerParameters()); assertTrue(validationResult.getResult().contains( "field \"name\" type \"java.lang.String\" value \"null\" INVALID, " + "must be a non-blank string")); @@ -93,14 +97,16 @@ public class TestDistributionParameterGroup { final GroupValidationResult validationResult = distributionParameters.validate(); assertFalse(validationResult.isValid()); assertEquals("", distributionParameters.getName()); - assertEquals(receptionHandlerParameters.get("SDCReceptionHandler").getReceptionHandlerType(), - distributionParameters.getReceptionHandlerParameters().get("SDCReceptionHandler") + assertEquals(receptionHandlerParameters.get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getReceptionHandlerType(), + distributionParameters.getReceptionHandlerParameters().get(CommonTestData.SDC_RECEPTION_HANDLER_KEY) .getReceptionHandlerType()); - assertEquals(receptionHandlerParameters.get("SDCReceptionHandler").getReceptionHandlerClassName(), - distributionParameters.getReceptionHandlerParameters().get("SDCReceptionHandler") + assertEquals( + receptionHandlerParameters.get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getReceptionHandlerClassName(), + distributionParameters.getReceptionHandlerParameters().get(CommonTestData.SDC_RECEPTION_HANDLER_KEY) .getReceptionHandlerClassName()); - assertEquals(receptionHandlerParameters.get("SDCReceptionHandler").getPluginHandlerParameters(), - distributionParameters.getReceptionHandlerParameters().get("SDCReceptionHandler") + assertEquals( + receptionHandlerParameters.get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getPluginHandlerParameters(), + distributionParameters.getReceptionHandlerParameters().get(CommonTestData.SDC_RECEPTION_HANDLER_KEY) .getPluginHandlerParameters()); assertTrue(validationResult.getResult().contains( "field \"name\" type \"java.lang.String\" value \"\" INVALID, " + "must be a non-blank string")); @@ -108,9 +114,9 @@ public class TestDistributionParameterGroup { @Test public void testDistributionParameterGroup_NullReceptionHandlerParameters() { - final String name = "SDCDistributionGroup"; try { - final DistributionParameterGroup distributionParameters = new DistributionParameterGroup(name, null); + final DistributionParameterGroup distributionParameters = + new DistributionParameterGroup(CommonTestData.DISTRIBUTION_GROUP_NAME, null); distributionParameters.validate(); fail("test should throw an exception here"); } catch (final Exception e) { @@ -121,12 +127,11 @@ public class TestDistributionParameterGroup { @Test public void testDistributionParameterGroup_EmptyReceptionHandlerParameters() { - final String name = "SDCDistributionGroup"; final Map receptionHandlerParameters = commonTestData.getReceptionHandlerParameters(true); try { final DistributionParameterGroup distributionParameters = - new DistributionParameterGroup(name, receptionHandlerParameters); + new DistributionParameterGroup(CommonTestData.DISTRIBUTION_GROUP_NAME, receptionHandlerParameters); distributionParameters.validate(); fail("test should throw an exception here"); } catch (final Exception 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 57610b23..79b12243 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 @@ -121,7 +121,7 @@ public class TestDistributionParameterHandler { minArguments.parse(minArgumentString); final DistributionParameterGroup parGroup = new DistributionParameterHandler().getParameters(minArguments); - assertEquals("SDCDistributionGroup", parGroup.getName()); + assertEquals(CommonTestData.DISTRIBUTION_GROUP_NAME, parGroup.getName()); } @Test @@ -132,13 +132,17 @@ public class TestDistributionParameterHandler { arguments.parse(distributionConfigParameters); final DistributionParameterGroup parGroup = new DistributionParameterHandler().getParameters(arguments); - assertEquals("SDCDistributionGroup", parGroup.getName()); - assertEquals("SDC", - parGroup.getReceptionHandlerParameters().get("SDCReceptionHandler").getReceptionHandlerType()); - assertEquals("TOSCA", parGroup.getReceptionHandlerParameters().get("SDCReceptionHandler") - .getPluginHandlerParameters().getPolicyDecoders().get("TOSCADecoder").getDecoderType()); - assertEquals("PAPEngine", parGroup.getReceptionHandlerParameters().get("SDCReceptionHandler") - .getPluginHandlerParameters().getPolicyForwarders().get("PAPEngineForwarder").getForwarderType()); + assertEquals(CommonTestData.DISTRIBUTION_GROUP_NAME, parGroup.getName()); + assertEquals(CommonTestData.RECEPTION_HANDLER_TYPE, parGroup.getReceptionHandlerParameters() + .get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getReceptionHandlerType()); + assertEquals(CommonTestData.DECODER_TYPE, + parGroup.getReceptionHandlerParameters().get(CommonTestData.SDC_RECEPTION_HANDLER_KEY) + .getPluginHandlerParameters().getPolicyDecoders().get(CommonTestData.TOSCA_DECODER_KEY) + .getDecoderType()); + assertEquals(CommonTestData.FORWARDER_TYPE, + parGroup.getReceptionHandlerParameters().get(CommonTestData.SDC_RECEPTION_HANDLER_KEY) + .getPluginHandlerParameters().getPolicyForwarders().get(CommonTestData.PAP_ENGINE_FORWARDER_KEY) + .getForwarderType()); } @Test diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPluginHandlerParameters.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPluginHandlerParameters.java index b2d732e9..4d9e5847 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPluginHandlerParameters.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPluginHandlerParameters.java @@ -28,6 +28,9 @@ import java.util.Map; import org.junit.Test; import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.distribution.forwarding.parameters.PolicyForwarderParameters; +import org.onap.policy.distribution.reception.parameters.PluginHandlerParameters; +import org.onap.policy.distribution.reception.parameters.PolicyDecoderParameters; /** * Class to perform unit test of PluginHandlerParameters. @@ -43,9 +46,10 @@ public class TestPluginHandlerParameters { final Map policyForwarders = commonTestData.getPolicyForwarders(false); final PluginHandlerParameters pHParameters = new PluginHandlerParameters(policyDecoders, policyForwarders); final GroupValidationResult validationResult = pHParameters.validate(); - assertEquals(policyDecoders.get("TOSCADecoder"), pHParameters.getPolicyDecoders().get("TOSCADecoder")); - assertEquals(policyForwarders.get("PAPEngineForwarder"), - pHParameters.getPolicyForwarders().get("PAPEngineForwarder")); + assertEquals(policyDecoders.get(CommonTestData.TOSCA_DECODER_KEY), + pHParameters.getPolicyDecoders().get(CommonTestData.TOSCA_DECODER_KEY)); + assertEquals(policyForwarders.get(CommonTestData.PAP_ENGINE_FORWARDER_KEY), + pHParameters.getPolicyForwarders().get(CommonTestData.PAP_ENGINE_FORWARDER_KEY)); assertTrue(validationResult.isValid()); } diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPolicyDecoderParameters.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPolicyDecoderParameters.java index bcae6df6..35acdf2e 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPolicyDecoderParameters.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPolicyDecoderParameters.java @@ -26,6 +26,7 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.distribution.reception.parameters.PolicyDecoderParameters; /** * Class to perform unit test of PolicyDecoderParameters. @@ -37,19 +38,19 @@ public class TestPolicyDecoderParameters { @Test public void testPolicyDecoderParameters() { final PolicyDecoderParameters pDParameters = - new PolicyDecoderParameters(CommonTestData.decoderType, CommonTestData.decoderClassName); + new PolicyDecoderParameters(CommonTestData.DECODER_TYPE, CommonTestData.DECODER_CLASS_NAME); final GroupValidationResult validationResult = pDParameters.validate(); - assertEquals(CommonTestData.decoderType, pDParameters.getDecoderType()); - assertEquals(CommonTestData.decoderClassName, pDParameters.getDecoderClassName()); + assertEquals(CommonTestData.DECODER_TYPE, pDParameters.getDecoderType()); + assertEquals(CommonTestData.DECODER_CLASS_NAME, pDParameters.getDecoderClassName()); assertTrue(validationResult.isValid()); } @Test public void testPolicyDecoderParameters_InvalidDecoderType() { - final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters("", CommonTestData.decoderClassName); + final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters("", CommonTestData.DECODER_CLASS_NAME); final GroupValidationResult validationResult = pDParameters.validate(); assertEquals("", pDParameters.getDecoderType()); - assertEquals(CommonTestData.decoderClassName, pDParameters.getDecoderClassName()); + assertEquals(CommonTestData.DECODER_CLASS_NAME, pDParameters.getDecoderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult().contains( "field \"decoderType\" type \"java.lang.String\" value \"\" INVALID, must be a non-blank string")); @@ -57,9 +58,9 @@ public class TestPolicyDecoderParameters { @Test public void testPolicyDecoderParameters_InvalidDecoderClassName() { - final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters(CommonTestData.decoderType, ""); + final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters(CommonTestData.DECODER_TYPE, ""); final GroupValidationResult validationResult = pDParameters.validate(); - assertEquals(CommonTestData.decoderType, pDParameters.getDecoderType()); + assertEquals(CommonTestData.DECODER_TYPE, pDParameters.getDecoderType()); assertEquals("", pDParameters.getDecoderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() @@ -83,10 +84,10 @@ public class TestPolicyDecoderParameters { @Test public void testPolicyDecoderParameters_NullDecoderType() { - final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters(null, CommonTestData.decoderClassName); + final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters(null, CommonTestData.DECODER_CLASS_NAME); final GroupValidationResult validationResult = pDParameters.validate(); assertEquals(null, pDParameters.getDecoderType()); - assertEquals(CommonTestData.decoderClassName, pDParameters.getDecoderClassName()); + assertEquals(CommonTestData.DECODER_CLASS_NAME, pDParameters.getDecoderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult().contains( "field \"decoderType\" type \"java.lang.String\" value \"null\" INVALID, must be a non-blank string")); @@ -94,9 +95,9 @@ public class TestPolicyDecoderParameters { @Test public void testPolicyDecoderParameters_NullDecoderClassName() { - final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters(CommonTestData.decoderType, null); + final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters(CommonTestData.DECODER_TYPE, null); final GroupValidationResult validationResult = pDParameters.validate(); - assertEquals(CommonTestData.decoderType, pDParameters.getDecoderType()); + assertEquals(CommonTestData.DECODER_TYPE, pDParameters.getDecoderType()); assertEquals(null, pDParameters.getDecoderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() @@ -107,10 +108,10 @@ public class TestPolicyDecoderParameters { @Test public void testPolicyDecoderParameters_InvalidDecoderClass() { final PolicyDecoderParameters pDParameters = - new PolicyDecoderParameters(CommonTestData.decoderType, CommonTestData.decoderClassName + "Invalid"); + new PolicyDecoderParameters(CommonTestData.DECODER_TYPE, CommonTestData.DECODER_CLASS_NAME + "Invalid"); final GroupValidationResult validationResult = pDParameters.validate(); - assertEquals(CommonTestData.decoderType, pDParameters.getDecoderType()); - assertEquals(CommonTestData.decoderClassName + "Invalid", pDParameters.getDecoderClassName()); + assertEquals(CommonTestData.DECODER_TYPE, pDParameters.getDecoderType()); + assertEquals(CommonTestData.DECODER_CLASS_NAME + "Invalid", pDParameters.getDecoderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult().contains("policy decoder class not found in classpath")); } diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPolicyForwarderParameters.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPolicyForwarderParameters.java index bbcbc370..d64df1b3 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPolicyForwarderParameters.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestPolicyForwarderParameters.java @@ -26,6 +26,7 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.distribution.forwarding.parameters.PolicyForwarderParameters; /** * Class to perform unit test of PolicyForwarderParameters. @@ -37,20 +38,20 @@ public class TestPolicyForwarderParameters { @Test public void testPolicyForwarderParameters() { final PolicyForwarderParameters pFParameters = - new PolicyForwarderParameters(CommonTestData.forwarderType, CommonTestData.forwarderClassName); + new PolicyForwarderParameters(CommonTestData.FORWARDER_TYPE, CommonTestData.FORWARDER_CLASS_NAME); final GroupValidationResult validationResult = pFParameters.validate(); - assertEquals(CommonTestData.forwarderType, pFParameters.getForwarderType()); - assertEquals(CommonTestData.forwarderClassName, pFParameters.getForwarderClassName()); + assertEquals(CommonTestData.FORWARDER_TYPE, pFParameters.getForwarderType()); + assertEquals(CommonTestData.FORWARDER_CLASS_NAME, pFParameters.getForwarderClassName()); assertTrue(validationResult.isValid()); } @Test public void testPolicyForwarderParameters_InvalidForwarderType() { final PolicyForwarderParameters pFParameters = - new PolicyForwarderParameters("", CommonTestData.forwarderClassName); + new PolicyForwarderParameters("", CommonTestData.FORWARDER_CLASS_NAME); final GroupValidationResult validationResult = pFParameters.validate(); assertEquals("", pFParameters.getForwarderType()); - assertEquals(CommonTestData.forwarderClassName, pFParameters.getForwarderClassName()); + assertEquals(CommonTestData.FORWARDER_CLASS_NAME, pFParameters.getForwarderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult().contains( "field \"forwarderType\" type \"java.lang.String\" value \"\" INVALID, must be a non-blank string")); @@ -58,9 +59,9 @@ public class TestPolicyForwarderParameters { @Test public void testPolicyForwarderParameters_InvalidForwarderClassName() { - final PolicyForwarderParameters pFParameters = new PolicyForwarderParameters(CommonTestData.forwarderType, ""); + final PolicyForwarderParameters pFParameters = new PolicyForwarderParameters(CommonTestData.FORWARDER_TYPE, ""); final GroupValidationResult validationResult = pFParameters.validate(); - assertEquals(CommonTestData.forwarderType, pFParameters.getForwarderType()); + assertEquals(CommonTestData.FORWARDER_TYPE, pFParameters.getForwarderType()); assertEquals("", pFParameters.getForwarderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() @@ -85,10 +86,10 @@ public class TestPolicyForwarderParameters { @Test public void testPolicyForwarderParameters_NullForwarderType() { final PolicyForwarderParameters pFParameters = - new PolicyForwarderParameters(null, CommonTestData.forwarderClassName); + new PolicyForwarderParameters(null, CommonTestData.FORWARDER_CLASS_NAME); final GroupValidationResult validationResult = pFParameters.validate(); assertEquals(null, pFParameters.getForwarderType()); - assertEquals(CommonTestData.forwarderClassName, pFParameters.getForwarderClassName()); + assertEquals(CommonTestData.FORWARDER_CLASS_NAME, pFParameters.getForwarderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() .contains("field \"forwarderType\" type \"java.lang.String\" value \"null\" INVALID, " @@ -98,9 +99,9 @@ public class TestPolicyForwarderParameters { @Test public void testPolicyForwarderParameters_NullForwarderClassName() { final PolicyForwarderParameters pFParameters = - new PolicyForwarderParameters(CommonTestData.forwarderType, null); + new PolicyForwarderParameters(CommonTestData.FORWARDER_TYPE, null); final GroupValidationResult validationResult = pFParameters.validate(); - assertEquals(CommonTestData.forwarderType, pFParameters.getForwarderType()); + assertEquals(CommonTestData.FORWARDER_TYPE, pFParameters.getForwarderType()); assertEquals(null, pFParameters.getForwarderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() @@ -110,11 +111,11 @@ public class TestPolicyForwarderParameters { @Test public void testPolicyForwarderParameters_InvalidForwarderClass() { - final PolicyForwarderParameters pFParameters = new PolicyForwarderParameters(CommonTestData.forwarderType, - CommonTestData.forwarderClassName + "Invalid"); + final PolicyForwarderParameters pFParameters = new PolicyForwarderParameters(CommonTestData.FORWARDER_TYPE, + CommonTestData.FORWARDER_CLASS_NAME + "Invalid"); final GroupValidationResult validationResult = pFParameters.validate(); - assertEquals(CommonTestData.forwarderType, pFParameters.getForwarderType()); - assertEquals(CommonTestData.forwarderClassName + "Invalid", pFParameters.getForwarderClassName()); + assertEquals(CommonTestData.FORWARDER_TYPE, pFParameters.getForwarderType()); + assertEquals(CommonTestData.FORWARDER_CLASS_NAME + "Invalid", pFParameters.getForwarderClassName()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult().contains("policy forwarder class not found in classpath")); } diff --git a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestReceptionHandlerParameters.java b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestReceptionHandlerParameters.java index 5eb113de..10faed8c 100644 --- a/main/src/test/java/org/onap/policy/distribution/main/parameters/TestReceptionHandlerParameters.java +++ b/main/src/test/java/org/onap/policy/distribution/main/parameters/TestReceptionHandlerParameters.java @@ -27,6 +27,8 @@ import static org.junit.Assert.fail; import org.junit.Test; import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.distribution.reception.parameters.PluginHandlerParameters; +import org.onap.policy.distribution.reception.parameters.ReceptionHandlerParameters; /** * Class to perform unit test of ReceptionHandlerParameters. @@ -40,10 +42,10 @@ public class TestReceptionHandlerParameters { public void testReceptionHandlerParameters() { final PluginHandlerParameters pHParameters = commonTestData.getPluginHandlerParameters(false); final ReceptionHandlerParameters rHParameters = new ReceptionHandlerParameters( - CommonTestData.receptionHandlerType, CommonTestData.receptionHandlerClassName, pHParameters); + CommonTestData.RECEPTION_HANDLER_TYPE, CommonTestData.RECEPTION_HANDLER_CLASS_NAME, pHParameters); final GroupValidationResult validationResult = rHParameters.validate(); - assertEquals(CommonTestData.receptionHandlerType, rHParameters.getReceptionHandlerType()); - assertEquals(CommonTestData.receptionHandlerClassName, rHParameters.getReceptionHandlerClassName()); + assertEquals(CommonTestData.RECEPTION_HANDLER_TYPE, rHParameters.getReceptionHandlerType()); + assertEquals(CommonTestData.RECEPTION_HANDLER_CLASS_NAME, rHParameters.getReceptionHandlerClassName()); assertEquals(pHParameters, rHParameters.getPluginHandlerParameters()); assertTrue(validationResult.isValid()); } @@ -52,10 +54,10 @@ public class TestReceptionHandlerParameters { public void testReceptionHandlerParameters_NullReceptionHandlerType() { final PluginHandlerParameters pHParameters = commonTestData.getPluginHandlerParameters(false); final ReceptionHandlerParameters rHParameters = - new ReceptionHandlerParameters(null, CommonTestData.receptionHandlerClassName, pHParameters); + new ReceptionHandlerParameters(null, CommonTestData.RECEPTION_HANDLER_CLASS_NAME, pHParameters); final GroupValidationResult validationResult = rHParameters.validate(); assertEquals(null, rHParameters.getReceptionHandlerType()); - assertEquals(CommonTestData.receptionHandlerClassName, rHParameters.getReceptionHandlerClassName()); + assertEquals(CommonTestData.RECEPTION_HANDLER_CLASS_NAME, rHParameters.getReceptionHandlerClassName()); assertEquals(pHParameters, rHParameters.getPluginHandlerParameters()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() @@ -67,9 +69,9 @@ public class TestReceptionHandlerParameters { public void testReceptionHandlerParameters_NullReceptionHandlerClassName() { final PluginHandlerParameters pHParameters = commonTestData.getPluginHandlerParameters(false); final ReceptionHandlerParameters rHParameters = - new ReceptionHandlerParameters(CommonTestData.receptionHandlerType, null, pHParameters); + new ReceptionHandlerParameters(CommonTestData.RECEPTION_HANDLER_TYPE, null, pHParameters); final GroupValidationResult validationResult = rHParameters.validate(); - assertEquals(CommonTestData.receptionHandlerType, rHParameters.getReceptionHandlerType()); + assertEquals(CommonTestData.RECEPTION_HANDLER_TYPE, rHParameters.getReceptionHandlerType()); assertEquals(null, rHParameters.getReceptionHandlerClassName()); assertEquals(pHParameters, rHParameters.getPluginHandlerParameters()); assertFalse(validationResult.isValid()); @@ -83,10 +85,10 @@ public class TestReceptionHandlerParameters { final PluginHandlerParameters pHParameters = commonTestData.getPluginHandlerParameters(false); final ReceptionHandlerParameters rHParameters = - new ReceptionHandlerParameters("", CommonTestData.receptionHandlerClassName, pHParameters); + new ReceptionHandlerParameters("", CommonTestData.RECEPTION_HANDLER_CLASS_NAME, pHParameters); final GroupValidationResult validationResult = rHParameters.validate(); assertEquals("", rHParameters.getReceptionHandlerType()); - assertEquals(CommonTestData.receptionHandlerClassName, rHParameters.getReceptionHandlerClassName()); + assertEquals(CommonTestData.RECEPTION_HANDLER_CLASS_NAME, rHParameters.getReceptionHandlerClassName()); assertEquals(pHParameters, rHParameters.getPluginHandlerParameters()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() @@ -98,9 +100,9 @@ public class TestReceptionHandlerParameters { public void testReceptionHandlerParameters_EmptyReceptionHandlerClassName() { final PluginHandlerParameters pHParameters = commonTestData.getPluginHandlerParameters(false); final ReceptionHandlerParameters rHParameters = - new ReceptionHandlerParameters(CommonTestData.receptionHandlerType, "", pHParameters); + new ReceptionHandlerParameters(CommonTestData.RECEPTION_HANDLER_TYPE, "", pHParameters); final GroupValidationResult validationResult = rHParameters.validate(); - assertEquals(CommonTestData.receptionHandlerType, rHParameters.getReceptionHandlerType()); + assertEquals(CommonTestData.RECEPTION_HANDLER_TYPE, rHParameters.getReceptionHandlerType()); assertEquals("", rHParameters.getReceptionHandlerClassName()); assertEquals(pHParameters, rHParameters.getPluginHandlerParameters()); assertFalse(validationResult.isValid()); @@ -114,7 +116,7 @@ public class TestReceptionHandlerParameters { try { final PluginHandlerParameters pHParameters = commonTestData.getPluginHandlerParameters(true); final ReceptionHandlerParameters rHParameters = new ReceptionHandlerParameters( - CommonTestData.receptionHandlerType, CommonTestData.receptionHandlerClassName, pHParameters); + CommonTestData.RECEPTION_HANDLER_TYPE, CommonTestData.RECEPTION_HANDLER_CLASS_NAME, pHParameters); rHParameters.validate(); fail("test should throw an exception here"); } catch (final Exception e) { @@ -126,11 +128,11 @@ public class TestReceptionHandlerParameters { public void testReceptionHandlerParameters_InvalidReceptionHandlerClass() { final PluginHandlerParameters pHParameters = commonTestData.getPluginHandlerParameters(false); final ReceptionHandlerParameters rHParameters = - new ReceptionHandlerParameters(CommonTestData.receptionHandlerType, - CommonTestData.receptionHandlerClassName + "Invalid", pHParameters); + new ReceptionHandlerParameters(CommonTestData.RECEPTION_HANDLER_TYPE, + CommonTestData.RECEPTION_HANDLER_CLASS_NAME + "Invalid", pHParameters); final GroupValidationResult validationResult = rHParameters.validate(); - assertEquals(CommonTestData.receptionHandlerType, rHParameters.getReceptionHandlerType()); - assertEquals(CommonTestData.receptionHandlerClassName + "Invalid", rHParameters.getReceptionHandlerClassName()); + assertEquals(CommonTestData.RECEPTION_HANDLER_TYPE, rHParameters.getReceptionHandlerType()); + assertEquals(CommonTestData.RECEPTION_HANDLER_CLASS_NAME + "Invalid", rHParameters.getReceptionHandlerClassName()); assertEquals(pHParameters, rHParameters.getPluginHandlerParameters()); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult().contains("reception handler class not found in classpath")); diff --git a/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionActivator.java b/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionActivator.java new file mode 100644 index 00000000..6d1f83bf --- /dev/null +++ b/main/src/test/java/org/onap/policy/distribution/main/startstop/TestDistributionActivator.java @@ -0,0 +1,65 @@ +/*- + * ============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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.distribution.main.PolicyDistributionException; +import org.onap.policy.distribution.main.parameters.CommonTestData; +import org.onap.policy.distribution.main.parameters.DistributionParameterGroup; +import org.onap.policy.distribution.main.parameters.DistributionParameterHandler; + +/** + * Class to perform unit test of DistributionActivator. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class TestDistributionActivator { + + @Test + public void testDistributionActivator() throws PolicyDistributionException { + final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters.json" }; + + final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments(); + arguments.parse(distributionConfigParameters); + + final DistributionParameterGroup parGroup = new DistributionParameterHandler().getParameters(arguments); + + final DistributionActivator activator = new DistributionActivator(parGroup); + activator.initialize(); + assertTrue(activator.getParameterGroup().isValid()); + assertEquals(CommonTestData.DISTRIBUTION_GROUP_NAME, activator.getParameterGroup().getName()); + assertEquals(CommonTestData.RECEPTION_HANDLER_TYPE, + activator.getParameterGroup().getReceptionHandlerParameters() + .get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getReceptionHandlerType()); + assertEquals(CommonTestData.DECODER_TYPE, + activator.getParameterGroup().getReceptionHandlerParameters() + .get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getPluginHandlerParameters().getPolicyDecoders() + .get(CommonTestData.TOSCA_DECODER_KEY).getDecoderType()); + assertEquals(CommonTestData.FORWARDER_TYPE, + activator.getParameterGroup().getReceptionHandlerParameters() + .get(CommonTestData.SDC_RECEPTION_HANDLER_KEY).getPluginHandlerParameters() + .getPolicyForwarders().get(CommonTestData.PAP_ENGINE_FORWARDER_KEY).getForwarderType()); + activator.deregisterToParameterService(parGroup); + } +} 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 index 781a2ab8..124f49a6 100644 --- 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 @@ -5,27 +5,69 @@ * 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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import org.junit.Test; +import org.onap.policy.distribution.main.PolicyDistributionException; +import org.onap.policy.distribution.main.parameters.CommonTestData; +/** + * Class to perform unit test of Main. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ public class TestMain { @Test - public void testMain() { - Main.main(null); + public void testMain() throws PolicyDistributionException { + final String[] distributionConfigParameters = { "-c", "parameters/DistributionConfigParameters.json" }; + final Main main = new Main(distributionConfigParameters); + assertTrue(main.getParameters().isValid()); + assertEquals(CommonTestData.DISTRIBUTION_GROUP_NAME, main.getParameters().getName()); + main.shutdown(); + } + + @Test + public void testMain_NoArguments() { + final String[] distributionConfigParameters = {}; + final Main main = new Main(distributionConfigParameters); + assertTrue(main.getParameters() == null); + } + + @Test + public void testMain_InvalidArguments() { + final String[] distributionConfigParameters = { "parameters/DistributionConfigParameters.json" }; + final Main main = new Main(distributionConfigParameters); + assertTrue(main.getParameters() == null); + } + + @Test + public void testMain_Help() { + final String[] distributionConfigParameters = { "-h" }; + Main.main(distributionConfigParameters); + } + + @Test + public void testMain_InvalidParameters() { + final String[] distributionConfigParameters = + { "-c", "parameters/DistributionConfigParameters_InvalidName.json" }; + final Main main = new Main(distributionConfigParameters); + assertTrue(main.getParameters() == null); } } -- cgit 1.2.3-korg