From 612196451afe9b16b8914858dd58f3bb0dae8579 Mon Sep 17 00:00:00 2001 From: ramverma Date: Thu, 16 Aug 2018 16:43:20 +0100 Subject: Adding termination code changes for distribution * Code changes for terminating the handlers started by distribution activator. * More test cases to increase coverage. * Fixes for checkstyle issues. * Fixes for sonar issues. Change-Id: I3299317c83fa643ac1d0ba1105ac1a932c0a83f6 Issue-ID: POLICY-1035 Signed-off-by: ramverma --- .../decoding/PolicyDecodingExceptionTest.java | 13 +-- .../handling/AbstractReceptionHandlerTest.java | 115 ++++++++++----------- .../reception/handling/DummyDecoder.java | 59 +++++++++++ .../reception/handling/DummyPolicyForwarder.java | 52 ++++++++++ .../reception/handling/DummyReceptionHandler.java | 35 +++++++ 5 files changed, 208 insertions(+), 66 deletions(-) create mode 100644 reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyDecoder.java create mode 100644 reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyPolicyForwarder.java create mode 100644 reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyReceptionHandler.java (limited to 'reception/src/test/java/org') diff --git a/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingExceptionTest.java b/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingExceptionTest.java index eba19503..a5bc072e 100644 --- a/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingExceptionTest.java +++ b/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingExceptionTest.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========================================================= */ @@ -21,20 +21,21 @@ package org.onap.policy.distribution.reception.decoding; import static org.junit.Assert.assertEquals; + import org.junit.Test; public class PolicyDecodingExceptionTest { @Test public void testPolicyDecodingExceptionString() { - PolicyDecodingException policyDecodingException = new PolicyDecodingException("error message"); + final PolicyDecodingException policyDecodingException = new PolicyDecodingException("error message"); assertEquals("error message", policyDecodingException.getMessage()); } @Test public void testPolicyDecodingExceptionStringThrowable() { - Exception cause = new IllegalArgumentException(); - PolicyDecodingException policyDecodingException = new PolicyDecodingException("error message", cause); + final Exception cause = new IllegalArgumentException(); + final PolicyDecodingException policyDecodingException = new PolicyDecodingException("error message", cause); assertEquals("error message", policyDecodingException.getMessage()); assertEquals(cause, policyDecodingException.getCause()); } diff --git a/reception/src/test/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandlerTest.java b/reception/src/test/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandlerTest.java index 7f9bb403..bb9b542a 100644 --- a/reception/src/test/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandlerTest.java +++ b/reception/src/test/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandlerTest.java @@ -27,19 +27,38 @@ import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import org.junit.Test; +import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.distribution.forwarding.PolicyForwarder; import org.onap.policy.distribution.forwarding.PolicyForwardingException; +import org.onap.policy.distribution.forwarding.parameters.PolicyForwarderParameters; import org.onap.policy.distribution.model.Policy; import org.onap.policy.distribution.model.PolicyInput; import org.onap.policy.distribution.reception.decoding.PolicyDecoder; import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; +import org.onap.policy.distribution.reception.parameters.PluginHandlerParameters; +import org.onap.policy.distribution.reception.parameters.PolicyDecoderParameters; +/** + * Class to perform unit test of AbstractReceptionHandler. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ public class AbstractReceptionHandlerTest { - // These tests won't work any more because we use Parameter Service for starting the plugins. - // Will rewrite them while implementing AbstractReceptionHandler.inputRecieved() method. - // @Test + private static final String DISTRIBUTION_GROUP = "DummyDistributionGroup"; + private static final String DECODER_TYPE = "DummyDecoder"; + private static final String DECODER_CLASS_NAME = "org.onap.policy.distribution.reception.handling.DummyDecoder"; + private static final String DECODER_KEY = "DummyDecoderKey"; + private static final String FORWARDER_KEY = "DummyForwarderKey"; + private static final String FORWARDER_TYPE = "DummyForwarder"; + private static final String FORWARDER_CLASS_NAME = + "org.onap.policy.distribution.reception.handling.DummyPolicyForwarder"; + + @Test public void testInputReceived() throws PolicyDecodingException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, PolicyForwardingException { final AbstractReceptionHandler handler = new DummyReceptionHandler(); @@ -75,7 +94,7 @@ public class AbstractReceptionHandlerTest { assertTrue(policyForwarder2.receivedPolicy(generatedPolicy2)); } - // @Test(expected = PolicyDecodingException.class) + @Test(expected = PolicyDecodingException.class) public void testInputReceivedNoSupportingDecoder() throws PolicyDecodingException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, PolicyForwardingException { final AbstractReceptionHandler handler = new DummyReceptionHandler(); @@ -87,7 +106,7 @@ public class AbstractReceptionHandlerTest { handler.inputReceived(new DummyPolicyInput()); } - // @Test(expected = PolicyDecodingException.class) + @Test(expected = PolicyDecodingException.class) public void testInputReceivedNoDecoder() throws PolicyDecodingException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, PolicyForwardingException { final AbstractReceptionHandler handler = new DummyReceptionHandler(); @@ -98,72 +117,23 @@ public class AbstractReceptionHandlerTest { handler.inputReceived(new DummyPolicyInput()); } - class DummyReceptionHandler extends AbstractReceptionHandler { - @Override - protected void initializeReception(final String parameterGroupName) {} - - @Override - public void destroy() {} - } - class DummyPolicyInput implements PolicyInput { } - class DummyPolicy1 implements Policy { - } - class DummyPolicy2 implements Policy { - } - - public class DummyDecoder implements PolicyDecoder { - - private boolean canHandleValue; - private Collection policesToReturn; - - public DummyDecoder(final boolean canHandleValue, final Collection policesToReturn) { - this.canHandleValue = canHandleValue; - this.policesToReturn = policesToReturn; - } - @Override - public boolean canHandle(final PolicyInput policyInput) { - return canHandleValue; - } - - @Override - public Collection decode(final PolicyInput input) throws PolicyDecodingException { - return policesToReturn; - } + class DummyPolicy1 implements Policy { } - public class DummyPolicyForwarder implements PolicyForwarder { - private int numberOfPoliciesReceived = 0; - private Collection policiesReceived = new ArrayList<>(); - - @Override - public void forward(final Collection policies) throws PolicyForwardingException { - numberOfPoliciesReceived += policies.size(); - policiesReceived.addAll(policies); - } - - public int getNumberOfPoliciesReceived() { - return numberOfPoliciesReceived; - } - - public boolean receivedPolicy(final Policy policy) { - return policiesReceived.contains(policy); - } + class DummyPolicy2 implements Policy { } - /** - * Only needed until code is added for instantiating plugins from paramater file - * - * @throws PolicyForwardingException - * @throws PolicyDecodingException - */ private void setUpPlugins(final AbstractReceptionHandler receptionHandler, final Collection> decoders, final Collection forwarders) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, PolicyDecodingException, PolicyForwardingException { - final PluginHandler pluginHandler = new PluginHandler(""); + final PluginHandlerParameters pluginParameters = getPluginHandlerParameters(); + pluginParameters.setName(DISTRIBUTION_GROUP); + ParameterService.register(pluginParameters); + final PluginHandler pluginHandler = new PluginHandler(pluginParameters.getName()); final Field decodersField = pluginHandler.getClass().getDeclaredField("policyDecoders"); decodersField.setAccessible(true); @@ -176,6 +146,31 @@ public class AbstractReceptionHandlerTest { final Field pluginHandlerField = AbstractReceptionHandler.class.getDeclaredField("pluginHandler"); pluginHandlerField.setAccessible(true); pluginHandlerField.set(receptionHandler, pluginHandler); + ParameterService.deregister(pluginParameters.getName()); + } + + private Map getPolicyDecoders() { + final Map policyDecoders = new HashMap(); + final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters(DECODER_TYPE, DECODER_CLASS_NAME); + policyDecoders.put(DECODER_KEY, pDParameters); + return policyDecoders; + } + + private Map getPolicyForwarders() { + final Map policyForwarders = + new HashMap(); + final PolicyForwarderParameters pFParameters = + new PolicyForwarderParameters(FORWARDER_TYPE, FORWARDER_CLASS_NAME); + policyForwarders.put(FORWARDER_KEY, pFParameters); + return policyForwarders; + } + + private PluginHandlerParameters getPluginHandlerParameters() { + final Map policyDecoders = getPolicyDecoders(); + final Map policyForwarders = getPolicyForwarders(); + final PluginHandlerParameters pluginHandlerParameters = + new PluginHandlerParameters(policyDecoders, policyForwarders); + return pluginHandlerParameters; } } diff --git a/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyDecoder.java b/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyDecoder.java new file mode 100644 index 00000000..2de1737e --- /dev/null +++ b/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyDecoder.java @@ -0,0 +1,59 @@ +/*- + * ============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.reception.handling; + +import java.util.Collection; + +import org.onap.policy.distribution.model.Policy; +import org.onap.policy.distribution.model.PolicyInput; +import org.onap.policy.distribution.reception.decoding.PolicyDecoder; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; + +/** + * Class to create a dummy decoder for test cases in AbstractReceptionHandlerTest. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DummyDecoder implements PolicyDecoder { + + private boolean canHandleValue; + private Collection policesToReturn; + + public DummyDecoder() { + this.canHandleValue = false; + this.policesToReturn = null; + } + + public DummyDecoder(final boolean canHandleValue, final Collection policesToReturn) { + this.canHandleValue = canHandleValue; + this.policesToReturn = policesToReturn; + } + + @Override + public boolean canHandle(final PolicyInput policyInput) { + return canHandleValue; + } + + @Override + public Collection decode(final PolicyInput input) throws PolicyDecodingException { + return policesToReturn; + } +} diff --git a/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyPolicyForwarder.java b/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyPolicyForwarder.java new file mode 100644 index 00000000..acee4697 --- /dev/null +++ b/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyPolicyForwarder.java @@ -0,0 +1,52 @@ +/*- + * ============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.reception.handling; + +import java.util.ArrayList; +import java.util.Collection; + +import org.onap.policy.distribution.forwarding.PolicyForwarder; +import org.onap.policy.distribution.forwarding.PolicyForwardingException; +import org.onap.policy.distribution.model.Policy; + +/** + * Class to create a dummy forwarder for test cases in AbstractReceptionHandlerTest. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DummyPolicyForwarder implements PolicyForwarder { + private int numberOfPoliciesReceived = 0; + private Collection policiesReceived = new ArrayList<>(); + + @Override + public void forward(final Collection policies) throws PolicyForwardingException { + numberOfPoliciesReceived += policies.size(); + policiesReceived.addAll(policies); + } + + public int getNumberOfPoliciesReceived() { + return numberOfPoliciesReceived; + } + + public boolean receivedPolicy(final Policy policy) { + return policiesReceived.contains(policy); + } +} diff --git a/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyReceptionHandler.java b/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyReceptionHandler.java new file mode 100644 index 00000000..43596ff2 --- /dev/null +++ b/reception/src/test/java/org/onap/policy/distribution/reception/handling/DummyReceptionHandler.java @@ -0,0 +1,35 @@ +/*- + * ============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.reception.handling; + +/** + * Class to create a dummy reception handler for test cases in AbstractReceptionHandlerTest. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +class DummyReceptionHandler extends AbstractReceptionHandler { + + @Override + protected void initializeReception(final String parameterGroupName) {} + + @Override + public void destroy() {} +} -- cgit 1.2.3-korg