2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2022 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.distribution.reception.handling;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
27 import java.lang.reflect.Field;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Collections;
31 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.parameters.PolicyForwarderParameters;
37 import org.onap.policy.distribution.model.PolicyInput;
38 import org.onap.policy.distribution.reception.decoding.PluginInitializationException;
39 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
40 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
41 import org.onap.policy.distribution.reception.parameters.PluginHandlerParameters;
42 import org.onap.policy.distribution.reception.parameters.PolicyDecoderParameters;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
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";
60 private static final String FORWARDER_CONFIGURATION_PARAMETERS = "DummyConfiguration";
61 private static final String DECODER_CONFIGURATION_PARAMETERS = "DummyDecoderConfiguration";
64 public void testInputReceived() throws PolicyDecodingException, NoSuchFieldException, SecurityException,
65 IllegalArgumentException, IllegalAccessException, PluginInitializationException {
66 final AbstractReceptionHandler handler = new DummyReceptionHandler();
68 final ToscaEntity generatedPolicy1 = new DummyPolicy1();
69 final ToscaEntity generatedPolicy2 = new DummyPolicy2();
71 final PolicyDecoder<PolicyInput, ToscaEntity> policyDecoder1 =
72 new DummyDecoder(true, Collections.singletonList(generatedPolicy1));
73 final PolicyDecoder<PolicyInput, ToscaEntity> policyDecoder2 =
74 new DummyDecoder(true, Collections.singletonList(generatedPolicy2));
76 final Collection<PolicyDecoder<PolicyInput, ToscaEntity>> policyDecoders = new ArrayList<>();
77 policyDecoders.add(policyDecoder1);
78 policyDecoders.add(policyDecoder2);
80 final DummyPolicyForwarder policyForwarder1 = new DummyPolicyForwarder();
81 final DummyPolicyForwarder policyForwarder2 = new DummyPolicyForwarder();
83 final Collection<PolicyForwarder> policyForwarders = new ArrayList<>();
84 policyForwarders.add(policyForwarder1);
85 policyForwarders.add(policyForwarder2);
87 setUpPlugins(handler, policyDecoders, policyForwarders);
89 handler.inputReceived(new DummyPolicyInput());
91 assertEquals(2, policyForwarder1.getNumberOfPoliciesReceived());
92 assertTrue(policyForwarder1.receivedPolicy(generatedPolicy1));
93 assertTrue(policyForwarder1.receivedPolicy(generatedPolicy2));
94 assertEquals(2, policyForwarder2.getNumberOfPoliciesReceived());
95 assertTrue(policyForwarder2.receivedPolicy(generatedPolicy1));
96 assertTrue(policyForwarder2.receivedPolicy(generatedPolicy2));
99 @Test(expected = PolicyDecodingException.class)
100 public void testInputReceivedNoSupportingDecoder() throws PolicyDecodingException, NoSuchFieldException,
101 SecurityException, IllegalArgumentException, IllegalAccessException, PluginInitializationException {
102 final AbstractReceptionHandler handler = new DummyReceptionHandler();
104 final PolicyDecoder<PolicyInput, ToscaEntity> policyDecoder = new DummyDecoder(false, Collections.emptyList());
105 final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder();
106 setUpPlugins(handler, Collections.singleton(policyDecoder), Collections.singleton(policyForwarder));
108 handler.inputReceived(new DummyPolicyInput());
111 @Test(expected = PolicyDecodingException.class)
112 public void testInputReceivedNoDecoder() throws PolicyDecodingException, NoSuchFieldException, SecurityException,
113 IllegalArgumentException, IllegalAccessException, PluginInitializationException {
114 final AbstractReceptionHandler handler = new DummyReceptionHandler();
116 final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder();
117 setUpPlugins(handler, Collections.emptySet(), Collections.singleton(policyForwarder));
119 handler.inputReceived(new DummyPolicyInput());
122 static class DummyPolicyInput implements PolicyInput {
125 static class DummyPolicy1 extends ToscaEntity {
128 public String getName() {
133 static class DummyPolicy2 extends ToscaEntity {
136 public String getName() {
141 private void setUpPlugins(final AbstractReceptionHandler receptionHandler,
142 final Collection<PolicyDecoder<PolicyInput, ToscaEntity>> decoders,
143 final Collection<PolicyForwarder> forwarders) throws NoSuchFieldException, SecurityException,
144 IllegalArgumentException, IllegalAccessException, PluginInitializationException {
145 final PluginHandlerParameters pluginParameters = getPluginHandlerParameters();
146 pluginParameters.setName(DISTRIBUTION_GROUP);
147 ParameterService.register(pluginParameters);
148 final PluginHandler pluginHandler = new PluginHandler(pluginParameters.getName());
150 final Field decodersField = pluginHandler.getClass().getDeclaredField("policyDecoders");
151 decodersField.setAccessible(true);
152 decodersField.set(pluginHandler, decoders);
154 final Field forwardersField = pluginHandler.getClass().getDeclaredField("policyForwarders");
155 forwardersField.setAccessible(true);
156 forwardersField.set(pluginHandler, forwarders);
158 final Field pluginHandlerField = AbstractReceptionHandler.class.getDeclaredField("pluginHandler");
159 pluginHandlerField.setAccessible(true);
160 pluginHandlerField.set(receptionHandler, pluginHandler);
161 ParameterService.deregister(pluginParameters.getName());
164 private Map<String, PolicyDecoderParameters> getPolicyDecoders() {
165 final Map<String, PolicyDecoderParameters> policyDecoders = new HashMap<>();
166 final PolicyDecoderParameters pDParameters =
167 new PolicyDecoderParameters(DECODER_TYPE, DECODER_CLASS_NAME, DECODER_CONFIGURATION_PARAMETERS);
168 policyDecoders.put(DECODER_KEY, pDParameters);
169 return policyDecoders;
172 private Map<String, PolicyForwarderParameters> getPolicyForwarders() {
173 final Map<String, PolicyForwarderParameters> policyForwarders =
175 final PolicyForwarderParameters pFParameters =
176 new PolicyForwarderParameters(FORWARDER_TYPE, FORWARDER_CLASS_NAME, FORWARDER_CONFIGURATION_PARAMETERS);
177 policyForwarders.put(FORWARDER_KEY, pFParameters);
178 return policyForwarders;
181 private PluginHandlerParameters getPluginHandlerParameters() {
182 final Map<String, PolicyDecoderParameters> policyDecoders = getPolicyDecoders();
183 final Map<String, PolicyForwarderParameters> policyForwarders = getPolicyForwarders();
184 return new PluginHandlerParameters(policyDecoders, policyForwarders);