From f85f63f52d38ca962fc0e68eae184dd7018dc47b Mon Sep 17 00:00:00 2001 From: mmis Date: Thu, 2 Aug 2018 09:45:44 +0100 Subject: Create interfaces for Policy Distribution Created interfaces and some skeletal implementations. Implememtations to be further developed in subsequent stories Change-Id: If55be78e34c8783451cb9aa755a563bc0850e569 Issue-ID: POLICY-1029 Signed-off-by: mmis --- .../decoding/PolicyDecodingExceptionTest.java | 42 +++++ .../handling/AbstractReceptionHandlerTest.java | 174 +++++++++++++++++++++ 2 files changed, 216 insertions(+) create mode 100644 reception/src/test/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingExceptionTest.java create mode 100644 reception/src/test/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandlerTest.java (limited to 'reception/src/test/java') 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 new file mode 100644 index 00000000..eba19503 --- /dev/null +++ b/reception/src/test/java/org/onap/policy/distribution/reception/decoding/PolicyDecodingExceptionTest.java @@ -0,0 +1,42 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-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.decoding; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class PolicyDecodingExceptionTest { + + @Test + public void testPolicyDecodingExceptionString() { + 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); + 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 new file mode 100644 index 00000000..3f033eb0 --- /dev/null +++ b/reception/src/test/java/org/onap/policy/distribution/reception/handling/AbstractReceptionHandlerTest.java @@ -0,0 +1,174 @@ +/*- + * ============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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import org.junit.Test; +import org.onap.policy.distribution.forwarding.PolicyForwarder; +import org.onap.policy.distribution.forwarding.PolicyForwardingException; +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; + +public class AbstractReceptionHandlerTest { + + @Test + public void testInputReceived() throws PolicyDecodingException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { + AbstractReceptionHandler handler = new DummyReceptionHandler(); + + Policy generatedPolicy1 = new DummyPolicy1(); + Policy generatedPolicy2 = new DummyPolicy2(); + + PolicyDecoder policyDecoder1 = + new DummyDecoder(true, Collections.singletonList(generatedPolicy1)); + PolicyDecoder policyDecoder2 = + new DummyDecoder(true, Collections.singletonList(generatedPolicy2)); + + Collection> policyDecoders = new ArrayList<>(); + policyDecoders.add(policyDecoder1); + policyDecoders.add(policyDecoder2); + + DummyPolicyForwarder policyForwarder1 = new DummyPolicyForwarder(); + DummyPolicyForwarder policyForwarder2 = new DummyPolicyForwarder(); + + Collection policyForwarders = new ArrayList<>(); + policyForwarders.add(policyForwarder1); + policyForwarders.add(policyForwarder2); + + setUpPlugins(handler, policyDecoders, policyForwarders); + + handler.inputReceived(new DummyPolicyInput()); + + assertEquals(2, policyForwarder1.getNumberOfPoliciesReceived()); + assertTrue(policyForwarder1.receivedPolicy(generatedPolicy1)); + assertTrue(policyForwarder1.receivedPolicy(generatedPolicy2)); + assertEquals(2, policyForwarder2.getNumberOfPoliciesReceived()); + assertTrue(policyForwarder2.receivedPolicy(generatedPolicy1)); + assertTrue(policyForwarder2.receivedPolicy(generatedPolicy2)); + } + + @Test(expected = PolicyDecodingException.class) + public void testInputReceivedNoSupportingDecoder() throws PolicyDecodingException, NoSuchFieldException, + SecurityException, IllegalArgumentException, IllegalAccessException { + AbstractReceptionHandler handler = new DummyReceptionHandler(); + + PolicyDecoder policyDecoder = new DummyDecoder(false, Collections.emptyList()); + DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder(); + setUpPlugins(handler, Collections.singleton(policyDecoder), Collections.singleton(policyForwarder)); + + handler.inputReceived(new DummyPolicyInput()); + } + + @Test(expected = PolicyDecodingException.class) + public void testInputReceivedNoDecoder() throws PolicyDecodingException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException { + AbstractReceptionHandler handler = new DummyReceptionHandler(); + + DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder(); + setUpPlugins(handler, Collections.emptySet(), Collections.singleton(policyForwarder)); + + handler.inputReceived(new DummyPolicyInput()); + } + + class DummyReceptionHandler extends AbstractReceptionHandler { + @Override + protected void initializeReception(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(boolean canHandleValue, Collection policesToReturn) { + this.canHandleValue = canHandleValue; + this.policesToReturn = policesToReturn; + } + + @Override + public boolean canHandle(PolicyInput policyInput) { + return canHandleValue; + } + + @Override + public Collection decode(PolicyInput input) throws PolicyDecodingException { + return policesToReturn; + } + } + + public class DummyPolicyForwarder implements PolicyForwarder { + private int numberOfPoliciesReceived = 0; + private Collection policiesReceived = new ArrayList<>(); + + @Override + public void forward(Collection policies) throws PolicyForwardingException { + numberOfPoliciesReceived += policies.size(); + policiesReceived.addAll(policies); + } + + public int getNumberOfPoliciesReceived() { + return numberOfPoliciesReceived; + } + + public boolean receivedPolicy(Policy policy) { + return policiesReceived.contains(policy); + } + } + + /** + * Only needed until code is added for instantiating plugins from paramater file + */ + private void setUpPlugins(AbstractReceptionHandler receptionHandler, + Collection> decoders, Collection forwarders) + throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { + PluginHandler pluginHandler = new PluginHandler(""); + + Field decodersField = pluginHandler.getClass().getDeclaredField("policyDecoders"); + decodersField.setAccessible(true); + decodersField.set(pluginHandler, decoders); + + Field forwardersField = pluginHandler.getClass().getDeclaredField("policyForwarders"); + forwardersField.setAccessible(true); + forwardersField.set(pluginHandler, forwarders); + + Field pluginHandlerField = AbstractReceptionHandler.class.getDeclaredField("pluginHandler"); + pluginHandlerField.setAccessible(true); + pluginHandlerField.set(receptionHandler, pluginHandler); + } + +} -- cgit 1.2.3-korg