8dc84621c3378b602225d1f826609140f9f80b14
[policy/distribution.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.reception.handling;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
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;
31 import java.util.Map;
32
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.Policy;
38 import org.onap.policy.distribution.model.PolicyInput;
39 import org.onap.policy.distribution.reception.decoding.PluginInitializationException;
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;
44
45 /**
46  * Class to perform unit test of AbstractReceptionHandler.
47  *
48  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
49  */
50 public class AbstractReceptionHandlerTest {
51
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";
62
63     @Test
64     public void testInputReceived() throws PolicyDecodingException, NoSuchFieldException, SecurityException,
65             IllegalArgumentException, IllegalAccessException, PluginInitializationException {
66         final AbstractReceptionHandler handler = new DummyReceptionHandler();
67
68         final Policy generatedPolicy1 = new DummyPolicy1();
69         final Policy generatedPolicy2 = new DummyPolicy2();
70
71         final PolicyDecoder<PolicyInput, Policy> policyDecoder1 =
72                 new DummyDecoder(true, Collections.singletonList(generatedPolicy1));
73         final PolicyDecoder<PolicyInput, Policy> policyDecoder2 =
74                 new DummyDecoder(true, Collections.singletonList(generatedPolicy2));
75
76         final Collection<PolicyDecoder<PolicyInput, Policy>> policyDecoders = new ArrayList<>();
77         policyDecoders.add(policyDecoder1);
78         policyDecoders.add(policyDecoder2);
79
80         final DummyPolicyForwarder policyForwarder1 = new DummyPolicyForwarder();
81         final DummyPolicyForwarder policyForwarder2 = new DummyPolicyForwarder();
82
83         final Collection<PolicyForwarder> policyForwarders = new ArrayList<>();
84         policyForwarders.add(policyForwarder1);
85         policyForwarders.add(policyForwarder2);
86
87         setUpPlugins(handler, policyDecoders, policyForwarders);
88
89         handler.inputReceived(new DummyPolicyInput());
90
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));
97     }
98
99     @Test(expected = PolicyDecodingException.class)
100     public void testInputReceivedNoSupportingDecoder() throws PolicyDecodingException, NoSuchFieldException,
101             SecurityException, IllegalArgumentException, IllegalAccessException, PluginInitializationException {
102         final AbstractReceptionHandler handler = new DummyReceptionHandler();
103
104         final PolicyDecoder<PolicyInput, Policy> policyDecoder = new DummyDecoder(false, Collections.emptyList());
105         final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder();
106         setUpPlugins(handler, Collections.singleton(policyDecoder), Collections.singleton(policyForwarder));
107
108         handler.inputReceived(new DummyPolicyInput());
109     }
110
111     @Test(expected = PolicyDecodingException.class)
112     public void testInputReceivedNoDecoder() throws PolicyDecodingException, NoSuchFieldException, SecurityException,
113             IllegalArgumentException, IllegalAccessException, PluginInitializationException {
114         final AbstractReceptionHandler handler = new DummyReceptionHandler();
115
116         final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder();
117         setUpPlugins(handler, Collections.emptySet(), Collections.singleton(policyForwarder));
118
119         handler.inputReceived(new DummyPolicyInput());
120     }
121
122     class DummyPolicyInput implements PolicyInput {
123     }
124
125     class DummyPolicy1 implements Policy {
126
127         @Override
128         public String getPolicyName() {
129             return null;
130         }
131
132         @Override
133         public String getPolicyType() {
134             return null;
135         }
136     }
137
138     class DummyPolicy2 implements Policy {
139
140         @Override
141         public String getPolicyName() {
142             return null;
143         }
144
145         @Override
146         public String getPolicyType() {
147             return null;
148         }
149     }
150
151     private void setUpPlugins(final AbstractReceptionHandler receptionHandler,
152             final Collection<PolicyDecoder<PolicyInput, Policy>> decoders, final Collection<PolicyForwarder> forwarders)
153             throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException,
154             PluginInitializationException {
155         final PluginHandlerParameters pluginParameters = getPluginHandlerParameters();
156         pluginParameters.setName(DISTRIBUTION_GROUP);
157         ParameterService.register(pluginParameters);
158         final PluginHandler pluginHandler = new PluginHandler(pluginParameters.getName());
159
160         final Field decodersField = pluginHandler.getClass().getDeclaredField("policyDecoders");
161         decodersField.setAccessible(true);
162         decodersField.set(pluginHandler, decoders);
163
164         final Field forwardersField = pluginHandler.getClass().getDeclaredField("policyForwarders");
165         forwardersField.setAccessible(true);
166         forwardersField.set(pluginHandler, forwarders);
167
168         final Field pluginHandlerField = AbstractReceptionHandler.class.getDeclaredField("pluginHandler");
169         pluginHandlerField.setAccessible(true);
170         pluginHandlerField.set(receptionHandler, pluginHandler);
171         ParameterService.deregister(pluginParameters.getName());
172     }
173
174     private Map<String, PolicyDecoderParameters> getPolicyDecoders() {
175         final Map<String, PolicyDecoderParameters> policyDecoders = new HashMap<String, PolicyDecoderParameters>();
176         final PolicyDecoderParameters pDParameters =
177                 new PolicyDecoderParameters(DECODER_TYPE, DECODER_CLASS_NAME, DECODER_CONFIGURATION_PARAMETERS);
178         policyDecoders.put(DECODER_KEY, pDParameters);
179         return policyDecoders;
180     }
181
182     private Map<String, PolicyForwarderParameters> getPolicyForwarders() {
183         final Map<String, PolicyForwarderParameters> policyForwarders =
184                 new HashMap<String, PolicyForwarderParameters>();
185         final PolicyForwarderParameters pFParameters =
186                 new PolicyForwarderParameters(FORWARDER_TYPE, FORWARDER_CLASS_NAME, FORWARDER_CONFIGURATION_PARAMETERS);
187         policyForwarders.put(FORWARDER_KEY, pFParameters);
188         return policyForwarders;
189     }
190
191     private PluginHandlerParameters getPluginHandlerParameters() {
192         final Map<String, PolicyDecoderParameters> policyDecoders = getPolicyDecoders();
193         final Map<String, PolicyForwarderParameters> policyForwarders = getPolicyForwarders();
194         final PluginHandlerParameters pluginHandlerParameters =
195                 new PluginHandlerParameters(policyDecoders, policyForwarders);
196         return pluginHandlerParameters;
197     }
198
199 }