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;
30 import java.util.HashMap;
33 import org.junit.Test;
34 import org.onap.policy.common.parameters.ParameterService;
35 import org.onap.policy.distribution.forwarding.PolicyForwarder;
36 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
37 import org.onap.policy.distribution.forwarding.parameters.PolicyForwarderParameters;
38 import org.onap.policy.distribution.model.Policy;
39 import org.onap.policy.distribution.model.PolicyInput;
40 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
41 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
42 import org.onap.policy.distribution.reception.parameters.PluginHandlerParameters;
43 import org.onap.policy.distribution.reception.parameters.PolicyDecoderParameters;
46 * Class to perform unit test of AbstractReceptionHandler.
48 * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
50 public class AbstractReceptionHandlerTest {
52 private static final String DISTRIBUTION_GROUP = "DummyDistributionGroup";
53 private static final String DECODER_TYPE = "DummyDecoder";
54 private static final String DECODER_CLASS_NAME = "org.onap.policy.distribution.reception.handling.DummyDecoder";
55 private static final String DECODER_KEY = "DummyDecoderKey";
56 private static final String FORWARDER_KEY = "DummyForwarderKey";
57 private static final String FORWARDER_TYPE = "DummyForwarder";
58 private static final String FORWARDER_CLASS_NAME =
59 "org.onap.policy.distribution.reception.handling.DummyPolicyForwarder";
62 public void testInputReceived() throws PolicyDecodingException, NoSuchFieldException, SecurityException,
63 IllegalArgumentException, IllegalAccessException, PolicyForwardingException {
64 final AbstractReceptionHandler handler = new DummyReceptionHandler();
66 final Policy generatedPolicy1 = new DummyPolicy1();
67 final Policy generatedPolicy2 = new DummyPolicy2();
69 final PolicyDecoder<PolicyInput, Policy> policyDecoder1 =
70 new DummyDecoder(true, Collections.singletonList(generatedPolicy1));
71 final PolicyDecoder<PolicyInput, Policy> policyDecoder2 =
72 new DummyDecoder(true, Collections.singletonList(generatedPolicy2));
74 final Collection<PolicyDecoder<PolicyInput, Policy>> policyDecoders = new ArrayList<>();
75 policyDecoders.add(policyDecoder1);
76 policyDecoders.add(policyDecoder2);
78 final DummyPolicyForwarder policyForwarder1 = new DummyPolicyForwarder();
79 final DummyPolicyForwarder policyForwarder2 = new DummyPolicyForwarder();
81 final Collection<PolicyForwarder> policyForwarders = new ArrayList<>();
82 policyForwarders.add(policyForwarder1);
83 policyForwarders.add(policyForwarder2);
85 setUpPlugins(handler, policyDecoders, policyForwarders);
87 handler.inputReceived(new DummyPolicyInput());
89 assertEquals(2, policyForwarder1.getNumberOfPoliciesReceived());
90 assertTrue(policyForwarder1.receivedPolicy(generatedPolicy1));
91 assertTrue(policyForwarder1.receivedPolicy(generatedPolicy2));
92 assertEquals(2, policyForwarder2.getNumberOfPoliciesReceived());
93 assertTrue(policyForwarder2.receivedPolicy(generatedPolicy1));
94 assertTrue(policyForwarder2.receivedPolicy(generatedPolicy2));
97 @Test(expected = PolicyDecodingException.class)
98 public void testInputReceivedNoSupportingDecoder() throws PolicyDecodingException, NoSuchFieldException,
99 SecurityException, IllegalArgumentException, IllegalAccessException, PolicyForwardingException {
100 final AbstractReceptionHandler handler = new DummyReceptionHandler();
102 final PolicyDecoder<PolicyInput, Policy> policyDecoder = new DummyDecoder(false, Collections.emptyList());
103 final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder();
104 setUpPlugins(handler, Collections.singleton(policyDecoder), Collections.singleton(policyForwarder));
106 handler.inputReceived(new DummyPolicyInput());
109 @Test(expected = PolicyDecodingException.class)
110 public void testInputReceivedNoDecoder() throws PolicyDecodingException, NoSuchFieldException, SecurityException,
111 IllegalArgumentException, IllegalAccessException, PolicyForwardingException {
112 final AbstractReceptionHandler handler = new DummyReceptionHandler();
114 final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder();
115 setUpPlugins(handler, Collections.emptySet(), Collections.singleton(policyForwarder));
117 handler.inputReceived(new DummyPolicyInput());
120 class DummyPolicyInput implements PolicyInput {
123 class DummyPolicy1 implements Policy {
126 class DummyPolicy2 implements Policy {
129 private void setUpPlugins(final AbstractReceptionHandler receptionHandler,
130 final Collection<PolicyDecoder<PolicyInput, Policy>> decoders, final Collection<PolicyForwarder> forwarders)
131 throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException,
132 PolicyDecodingException, PolicyForwardingException {
133 final PluginHandlerParameters pluginParameters = getPluginHandlerParameters();
134 pluginParameters.setName(DISTRIBUTION_GROUP);
135 ParameterService.register(pluginParameters);
136 final PluginHandler pluginHandler = new PluginHandler(pluginParameters.getName());
138 final Field decodersField = pluginHandler.getClass().getDeclaredField("policyDecoders");
139 decodersField.setAccessible(true);
140 decodersField.set(pluginHandler, decoders);
142 final Field forwardersField = pluginHandler.getClass().getDeclaredField("policyForwarders");
143 forwardersField.setAccessible(true);
144 forwardersField.set(pluginHandler, forwarders);
146 final Field pluginHandlerField = AbstractReceptionHandler.class.getDeclaredField("pluginHandler");
147 pluginHandlerField.setAccessible(true);
148 pluginHandlerField.set(receptionHandler, pluginHandler);
149 ParameterService.deregister(pluginParameters.getName());
152 private Map<String, PolicyDecoderParameters> getPolicyDecoders() {
153 final Map<String, PolicyDecoderParameters> policyDecoders = new HashMap<String, PolicyDecoderParameters>();
154 final PolicyDecoderParameters pDParameters = new PolicyDecoderParameters(DECODER_TYPE, DECODER_CLASS_NAME);
155 policyDecoders.put(DECODER_KEY, pDParameters);
156 return policyDecoders;
159 private Map<String, PolicyForwarderParameters> getPolicyForwarders() {
160 final Map<String, PolicyForwarderParameters> policyForwarders =
161 new HashMap<String, PolicyForwarderParameters>();
162 final PolicyForwarderParameters pFParameters =
163 new PolicyForwarderParameters(FORWARDER_TYPE, FORWARDER_CLASS_NAME);
164 policyForwarders.put(FORWARDER_KEY, pFParameters);
165 return policyForwarders;
168 private PluginHandlerParameters getPluginHandlerParameters() {
169 final Map<String, PolicyDecoderParameters> policyDecoders = getPolicyDecoders();
170 final Map<String, PolicyForwarderParameters> policyForwarders = getPolicyForwarders();
171 final PluginHandlerParameters pluginHandlerParameters =
172 new PluginHandlerParameters(policyDecoders, policyForwarders);
173 return pluginHandlerParameters;