2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distribution.reception.handling;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
26 import java.lang.reflect.Field;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Collections;
31 import org.onap.policy.distribution.forwarding.PolicyForwarder;
32 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
33 import org.onap.policy.distribution.model.Policy;
34 import org.onap.policy.distribution.model.PolicyInput;
35 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
36 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
38 public class AbstractReceptionHandlerTest {
40 // These tests won't work any more because we use Parameter Service for starting the plugins.
41 // Will rewrite them while implementing AbstractReceptionHandler.inputRecieved() method.
43 public void testInputReceived() throws PolicyDecodingException, NoSuchFieldException, SecurityException,
44 IllegalArgumentException, IllegalAccessException, PolicyForwardingException {
45 final AbstractReceptionHandler handler = new DummyReceptionHandler();
47 final Policy generatedPolicy1 = new DummyPolicy1();
48 final Policy generatedPolicy2 = new DummyPolicy2();
50 final PolicyDecoder<PolicyInput, Policy> policyDecoder1 =
51 new DummyDecoder(true, Collections.singletonList(generatedPolicy1));
52 final PolicyDecoder<PolicyInput, Policy> policyDecoder2 =
53 new DummyDecoder(true, Collections.singletonList(generatedPolicy2));
55 final Collection<PolicyDecoder<PolicyInput, Policy>> policyDecoders = new ArrayList<>();
56 policyDecoders.add(policyDecoder1);
57 policyDecoders.add(policyDecoder2);
59 final DummyPolicyForwarder policyForwarder1 = new DummyPolicyForwarder();
60 final DummyPolicyForwarder policyForwarder2 = new DummyPolicyForwarder();
62 final Collection<PolicyForwarder> policyForwarders = new ArrayList<>();
63 policyForwarders.add(policyForwarder1);
64 policyForwarders.add(policyForwarder2);
66 setUpPlugins(handler, policyDecoders, policyForwarders);
68 handler.inputReceived(new DummyPolicyInput());
70 assertEquals(2, policyForwarder1.getNumberOfPoliciesReceived());
71 assertTrue(policyForwarder1.receivedPolicy(generatedPolicy1));
72 assertTrue(policyForwarder1.receivedPolicy(generatedPolicy2));
73 assertEquals(2, policyForwarder2.getNumberOfPoliciesReceived());
74 assertTrue(policyForwarder2.receivedPolicy(generatedPolicy1));
75 assertTrue(policyForwarder2.receivedPolicy(generatedPolicy2));
78 // @Test(expected = PolicyDecodingException.class)
79 public void testInputReceivedNoSupportingDecoder() throws PolicyDecodingException, NoSuchFieldException,
80 SecurityException, IllegalArgumentException, IllegalAccessException, PolicyForwardingException {
81 final AbstractReceptionHandler handler = new DummyReceptionHandler();
83 final PolicyDecoder<PolicyInput, Policy> policyDecoder = new DummyDecoder(false, Collections.emptyList());
84 final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder();
85 setUpPlugins(handler, Collections.singleton(policyDecoder), Collections.singleton(policyForwarder));
87 handler.inputReceived(new DummyPolicyInput());
90 // @Test(expected = PolicyDecodingException.class)
91 public void testInputReceivedNoDecoder() throws PolicyDecodingException, NoSuchFieldException, SecurityException,
92 IllegalArgumentException, IllegalAccessException, PolicyForwardingException {
93 final AbstractReceptionHandler handler = new DummyReceptionHandler();
95 final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder();
96 setUpPlugins(handler, Collections.emptySet(), Collections.singleton(policyForwarder));
98 handler.inputReceived(new DummyPolicyInput());
101 class DummyReceptionHandler extends AbstractReceptionHandler {
103 protected void initializeReception(final String parameterGroupName) {}
106 public void destroy() {}
109 class DummyPolicyInput implements PolicyInput {
111 class DummyPolicy1 implements Policy {
113 class DummyPolicy2 implements Policy {
116 public class DummyDecoder implements PolicyDecoder<PolicyInput, Policy> {
118 private boolean canHandleValue;
119 private Collection<Policy> policesToReturn;
121 public DummyDecoder(final boolean canHandleValue, final Collection<Policy> policesToReturn) {
122 this.canHandleValue = canHandleValue;
123 this.policesToReturn = policesToReturn;
127 public boolean canHandle(final PolicyInput policyInput) {
128 return canHandleValue;
132 public Collection<Policy> decode(final PolicyInput input) throws PolicyDecodingException {
133 return policesToReturn;
137 public class DummyPolicyForwarder implements PolicyForwarder {
138 private int numberOfPoliciesReceived = 0;
139 private Collection<Policy> policiesReceived = new ArrayList<>();
142 public void forward(final Collection<Policy> policies) throws PolicyForwardingException {
143 numberOfPoliciesReceived += policies.size();
144 policiesReceived.addAll(policies);
147 public int getNumberOfPoliciesReceived() {
148 return numberOfPoliciesReceived;
151 public boolean receivedPolicy(final Policy policy) {
152 return policiesReceived.contains(policy);
157 * Only needed until code is added for instantiating plugins from paramater file
159 * @throws PolicyForwardingException
160 * @throws PolicyDecodingException
162 private void setUpPlugins(final AbstractReceptionHandler receptionHandler,
163 final Collection<PolicyDecoder<PolicyInput, Policy>> decoders, final Collection<PolicyForwarder> forwarders)
164 throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException,
165 PolicyDecodingException, PolicyForwardingException {
166 final PluginHandler pluginHandler = new PluginHandler("");
168 final Field decodersField = pluginHandler.getClass().getDeclaredField("policyDecoders");
169 decodersField.setAccessible(true);
170 decodersField.set(pluginHandler, decoders);
172 final Field forwardersField = pluginHandler.getClass().getDeclaredField("policyForwarders");
173 forwardersField.setAccessible(true);
174 forwardersField.set(pluginHandler, forwarders);
176 final Field pluginHandlerField = AbstractReceptionHandler.class.getDeclaredField("pluginHandler");
177 pluginHandlerField.setAccessible(true);
178 pluginHandlerField.set(receptionHandler, pluginHandler);