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;
25 import java.lang.reflect.Field;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import org.junit.Test;
30 import org.onap.policy.distribution.forwarding.PolicyForwarder;
31 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
32 import org.onap.policy.distribution.model.Policy;
33 import org.onap.policy.distribution.model.PolicyInput;
34 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
35 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
37 public class AbstractReceptionHandlerTest {
40 public void testInputReceived() throws PolicyDecodingException, NoSuchFieldException, SecurityException,
41 IllegalArgumentException, IllegalAccessException {
42 AbstractReceptionHandler handler = new DummyReceptionHandler();
44 Policy generatedPolicy1 = new DummyPolicy1();
45 Policy generatedPolicy2 = new DummyPolicy2();
47 PolicyDecoder<PolicyInput, Policy> policyDecoder1 =
48 new DummyDecoder(true, Collections.singletonList(generatedPolicy1));
49 PolicyDecoder<PolicyInput, Policy> policyDecoder2 =
50 new DummyDecoder(true, Collections.singletonList(generatedPolicy2));
52 Collection<PolicyDecoder<PolicyInput, Policy>> policyDecoders = new ArrayList<>();
53 policyDecoders.add(policyDecoder1);
54 policyDecoders.add(policyDecoder2);
56 DummyPolicyForwarder policyForwarder1 = new DummyPolicyForwarder();
57 DummyPolicyForwarder policyForwarder2 = new DummyPolicyForwarder();
59 Collection<PolicyForwarder> policyForwarders = new ArrayList<>();
60 policyForwarders.add(policyForwarder1);
61 policyForwarders.add(policyForwarder2);
63 setUpPlugins(handler, policyDecoders, policyForwarders);
65 handler.inputReceived(new DummyPolicyInput());
67 assertEquals(2, policyForwarder1.getNumberOfPoliciesReceived());
68 assertTrue(policyForwarder1.receivedPolicy(generatedPolicy1));
69 assertTrue(policyForwarder1.receivedPolicy(generatedPolicy2));
70 assertEquals(2, policyForwarder2.getNumberOfPoliciesReceived());
71 assertTrue(policyForwarder2.receivedPolicy(generatedPolicy1));
72 assertTrue(policyForwarder2.receivedPolicy(generatedPolicy2));
75 @Test(expected = PolicyDecodingException.class)
76 public void testInputReceivedNoSupportingDecoder() throws PolicyDecodingException, NoSuchFieldException,
77 SecurityException, IllegalArgumentException, IllegalAccessException {
78 AbstractReceptionHandler handler = new DummyReceptionHandler();
80 PolicyDecoder<PolicyInput, Policy> policyDecoder = new DummyDecoder(false, Collections.emptyList());
81 DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder();
82 setUpPlugins(handler, Collections.singleton(policyDecoder), Collections.singleton(policyForwarder));
84 handler.inputReceived(new DummyPolicyInput());
87 @Test(expected = PolicyDecodingException.class)
88 public void testInputReceivedNoDecoder() throws PolicyDecodingException, NoSuchFieldException, SecurityException,
89 IllegalArgumentException, IllegalAccessException {
90 AbstractReceptionHandler handler = new DummyReceptionHandler();
92 DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder();
93 setUpPlugins(handler, Collections.emptySet(), Collections.singleton(policyForwarder));
95 handler.inputReceived(new DummyPolicyInput());
98 class DummyReceptionHandler extends AbstractReceptionHandler {
100 protected void initializeReception(String parameterGroupName) {}
103 public void destroy() {}
106 class DummyPolicyInput implements PolicyInput {
108 class DummyPolicy1 implements Policy {
110 class DummyPolicy2 implements Policy {
113 public class DummyDecoder implements PolicyDecoder<PolicyInput, Policy> {
115 private boolean canHandleValue;
116 private Collection<Policy> policesToReturn;
118 public DummyDecoder(boolean canHandleValue, Collection<Policy> policesToReturn) {
119 this.canHandleValue = canHandleValue;
120 this.policesToReturn = policesToReturn;
124 public boolean canHandle(PolicyInput policyInput) {
125 return canHandleValue;
129 public Collection<Policy> decode(PolicyInput input) throws PolicyDecodingException {
130 return policesToReturn;
134 public class DummyPolicyForwarder implements PolicyForwarder {
135 private int numberOfPoliciesReceived = 0;
136 private Collection<Policy> policiesReceived = new ArrayList<>();
139 public void forward(Collection<Policy> policies) throws PolicyForwardingException {
140 numberOfPoliciesReceived += policies.size();
141 policiesReceived.addAll(policies);
144 public int getNumberOfPoliciesReceived() {
145 return numberOfPoliciesReceived;
148 public boolean receivedPolicy(Policy policy) {
149 return policiesReceived.contains(policy);
154 * Only needed until code is added for instantiating plugins from paramater file
156 private void setUpPlugins(AbstractReceptionHandler receptionHandler,
157 Collection<PolicyDecoder<PolicyInput, Policy>> decoders, Collection<PolicyForwarder> forwarders)
158 throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
159 PluginHandler pluginHandler = new PluginHandler("");
161 Field decodersField = pluginHandler.getClass().getDeclaredField("policyDecoders");
162 decodersField.setAccessible(true);
163 decodersField.set(pluginHandler, decoders);
165 Field forwardersField = pluginHandler.getClass().getDeclaredField("policyForwarders");
166 forwardersField.setAccessible(true);
167 forwardersField.set(pluginHandler, forwarders);
169 Field pluginHandlerField = AbstractReceptionHandler.class.getDeclaredField("pluginHandler");
170 pluginHandlerField.setAccessible(true);
171 pluginHandlerField.set(receptionHandler, pluginHandler);