2f2d42c3a4f09097c885c0bc80563a7563293cb3
[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 import org.junit.Test;
33 import org.onap.policy.common.parameters.ParameterService;
34 import org.onap.policy.distribution.forwarding.PolicyForwarder;
35 import org.onap.policy.distribution.forwarding.parameters.PolicyForwarderParameters;
36 import org.onap.policy.distribution.model.PolicyInput;
37 import org.onap.policy.distribution.reception.decoding.PluginInitializationException;
38 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
39 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
40 import org.onap.policy.distribution.reception.parameters.PluginHandlerParameters;
41 import org.onap.policy.distribution.reception.parameters.PolicyDecoderParameters;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
43
44 /**
45  * Class to perform unit test of AbstractReceptionHandler.
46  *
47  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
48  */
49 public class AbstractReceptionHandlerTest {
50
51     private static final String DISTRIBUTION_GROUP = "DummyDistributionGroup";
52     private static final String DECODER_TYPE = "DummyDecoder";
53     private static final String DECODER_CLASS_NAME = "org.onap.policy.distribution.reception.handling.DummyDecoder";
54     private static final String DECODER_KEY = "DummyDecoderKey";
55     private static final String FORWARDER_KEY = "DummyForwarderKey";
56     private static final String FORWARDER_TYPE = "DummyForwarder";
57     private static final String FORWARDER_CLASS_NAME =
58             "org.onap.policy.distribution.reception.handling.DummyPolicyForwarder";
59     private static final String FORWARDER_CONFIGURATION_PARAMETERS = "DummyConfiguration";
60     private static final String DECODER_CONFIGURATION_PARAMETERS = "DummyDecoderConfiguration";
61
62     @Test
63     public void testInputReceived() throws PolicyDecodingException, NoSuchFieldException, SecurityException,
64             IllegalArgumentException, IllegalAccessException, PluginInitializationException {
65         final AbstractReceptionHandler handler = new DummyReceptionHandler();
66
67         final ToscaEntity generatedPolicy1 = new DummyPolicy1();
68         final ToscaEntity generatedPolicy2 = new DummyPolicy2();
69
70         final PolicyDecoder<PolicyInput, ToscaEntity> policyDecoder1 =
71                 new DummyDecoder(true, Collections.singletonList(generatedPolicy1));
72         final PolicyDecoder<PolicyInput, ToscaEntity> policyDecoder2 =
73                 new DummyDecoder(true, Collections.singletonList(generatedPolicy2));
74
75         final Collection<PolicyDecoder<PolicyInput, ToscaEntity>> policyDecoders = new ArrayList<>();
76         policyDecoders.add(policyDecoder1);
77         policyDecoders.add(policyDecoder2);
78
79         final DummyPolicyForwarder policyForwarder1 = new DummyPolicyForwarder();
80         final DummyPolicyForwarder policyForwarder2 = new DummyPolicyForwarder();
81
82         final Collection<PolicyForwarder> policyForwarders = new ArrayList<>();
83         policyForwarders.add(policyForwarder1);
84         policyForwarders.add(policyForwarder2);
85
86         setUpPlugins(handler, policyDecoders, policyForwarders);
87
88         handler.inputReceived(new DummyPolicyInput());
89
90         assertEquals(2, policyForwarder1.getNumberOfPoliciesReceived());
91         assertTrue(policyForwarder1.receivedPolicy(generatedPolicy1));
92         assertTrue(policyForwarder1.receivedPolicy(generatedPolicy2));
93         assertEquals(2, policyForwarder2.getNumberOfPoliciesReceived());
94         assertTrue(policyForwarder2.receivedPolicy(generatedPolicy1));
95         assertTrue(policyForwarder2.receivedPolicy(generatedPolicy2));
96     }
97
98     @Test(expected = PolicyDecodingException.class)
99     public void testInputReceivedNoSupportingDecoder() throws PolicyDecodingException, NoSuchFieldException,
100             SecurityException, IllegalArgumentException, IllegalAccessException, PluginInitializationException {
101         final AbstractReceptionHandler handler = new DummyReceptionHandler();
102
103         final PolicyDecoder<PolicyInput, ToscaEntity> policyDecoder = new DummyDecoder(false, Collections.emptyList());
104         final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder();
105         setUpPlugins(handler, Collections.singleton(policyDecoder), Collections.singleton(policyForwarder));
106
107         handler.inputReceived(new DummyPolicyInput());
108     }
109
110     @Test(expected = PolicyDecodingException.class)
111     public void testInputReceivedNoDecoder() throws PolicyDecodingException, NoSuchFieldException, SecurityException,
112             IllegalArgumentException, IllegalAccessException, PluginInitializationException {
113         final AbstractReceptionHandler handler = new DummyReceptionHandler();
114
115         final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder();
116         setUpPlugins(handler, Collections.emptySet(), Collections.singleton(policyForwarder));
117
118         handler.inputReceived(new DummyPolicyInput());
119     }
120
121     class DummyPolicyInput implements PolicyInput {
122     }
123
124     class DummyPolicy1 extends ToscaEntity {
125
126         @Override
127         public String getName() {
128             return null;
129         }
130     }
131
132     class DummyPolicy2 extends ToscaEntity {
133
134         @Override
135         public String getName() {
136             return null;
137         }
138     }
139
140     private void setUpPlugins(final AbstractReceptionHandler receptionHandler,
141             final Collection<PolicyDecoder<PolicyInput, ToscaEntity>> decoders,
142             final Collection<PolicyForwarder> forwarders) throws NoSuchFieldException, SecurityException,
143             IllegalArgumentException, IllegalAccessException, PluginInitializationException {
144         final PluginHandlerParameters pluginParameters = getPluginHandlerParameters();
145         pluginParameters.setName(DISTRIBUTION_GROUP);
146         ParameterService.register(pluginParameters);
147         final PluginHandler pluginHandler = new PluginHandler(pluginParameters.getName());
148
149         final Field decodersField = pluginHandler.getClass().getDeclaredField("policyDecoders");
150         decodersField.setAccessible(true);
151         decodersField.set(pluginHandler, decoders);
152
153         final Field forwardersField = pluginHandler.getClass().getDeclaredField("policyForwarders");
154         forwardersField.setAccessible(true);
155         forwardersField.set(pluginHandler, forwarders);
156
157         final Field pluginHandlerField = AbstractReceptionHandler.class.getDeclaredField("pluginHandler");
158         pluginHandlerField.setAccessible(true);
159         pluginHandlerField.set(receptionHandler, pluginHandler);
160         ParameterService.deregister(pluginParameters.getName());
161     }
162
163     private Map<String, PolicyDecoderParameters> getPolicyDecoders() {
164         final Map<String, PolicyDecoderParameters> policyDecoders = new HashMap<String, PolicyDecoderParameters>();
165         final PolicyDecoderParameters pDParameters =
166                 new PolicyDecoderParameters(DECODER_TYPE, DECODER_CLASS_NAME, DECODER_CONFIGURATION_PARAMETERS);
167         policyDecoders.put(DECODER_KEY, pDParameters);
168         return policyDecoders;
169     }
170
171     private Map<String, PolicyForwarderParameters> getPolicyForwarders() {
172         final Map<String, PolicyForwarderParameters> policyForwarders =
173                 new HashMap<String, PolicyForwarderParameters>();
174         final PolicyForwarderParameters pFParameters =
175                 new PolicyForwarderParameters(FORWARDER_TYPE, FORWARDER_CLASS_NAME, FORWARDER_CONFIGURATION_PARAMETERS);
176         policyForwarders.put(FORWARDER_KEY, pFParameters);
177         return policyForwarders;
178     }
179
180     private PluginHandlerParameters getPluginHandlerParameters() {
181         final Map<String, PolicyDecoderParameters> policyDecoders = getPolicyDecoders();
182         final Map<String, PolicyForwarderParameters> policyForwarders = getPolicyForwarders();
183         final PluginHandlerParameters pluginHandlerParameters =
184                 new PluginHandlerParameters(policyDecoders, policyForwarders);
185         return pluginHandlerParameters;
186     }
187
188 }