7f9bb4038b8c4b66127df0d036c24d62e1f0ce7c
[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
31 import org.onap.policy.distribution.forwarding.PolicyForwarder;
32 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
33 import org.onap.policy.distribution.model.Policy;
34 import org.onap.policy.distribution.model.PolicyInput;
35 import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
36 import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
37
38 public class AbstractReceptionHandlerTest {
39
40     // These tests won't work any more because we use Parameter Service for starting the plugins.
41     // Will rewrite them while implementing AbstractReceptionHandler.inputRecieved() method.
42     // @Test
43     public void testInputReceived() throws PolicyDecodingException, NoSuchFieldException, SecurityException,
44             IllegalArgumentException, IllegalAccessException, PolicyForwardingException {
45         final AbstractReceptionHandler handler = new DummyReceptionHandler();
46
47         final Policy generatedPolicy1 = new DummyPolicy1();
48         final Policy generatedPolicy2 = new DummyPolicy2();
49
50         final PolicyDecoder<PolicyInput, Policy> policyDecoder1 =
51                 new DummyDecoder(true, Collections.singletonList(generatedPolicy1));
52         final PolicyDecoder<PolicyInput, Policy> policyDecoder2 =
53                 new DummyDecoder(true, Collections.singletonList(generatedPolicy2));
54
55         final Collection<PolicyDecoder<PolicyInput, Policy>> policyDecoders = new ArrayList<>();
56         policyDecoders.add(policyDecoder1);
57         policyDecoders.add(policyDecoder2);
58
59         final DummyPolicyForwarder policyForwarder1 = new DummyPolicyForwarder();
60         final DummyPolicyForwarder policyForwarder2 = new DummyPolicyForwarder();
61
62         final Collection<PolicyForwarder> policyForwarders = new ArrayList<>();
63         policyForwarders.add(policyForwarder1);
64         policyForwarders.add(policyForwarder2);
65
66         setUpPlugins(handler, policyDecoders, policyForwarders);
67
68         handler.inputReceived(new DummyPolicyInput());
69
70         assertEquals(2, policyForwarder1.getNumberOfPoliciesReceived());
71         assertTrue(policyForwarder1.receivedPolicy(generatedPolicy1));
72         assertTrue(policyForwarder1.receivedPolicy(generatedPolicy2));
73         assertEquals(2, policyForwarder2.getNumberOfPoliciesReceived());
74         assertTrue(policyForwarder2.receivedPolicy(generatedPolicy1));
75         assertTrue(policyForwarder2.receivedPolicy(generatedPolicy2));
76     }
77
78     // @Test(expected = PolicyDecodingException.class)
79     public void testInputReceivedNoSupportingDecoder() throws PolicyDecodingException, NoSuchFieldException,
80             SecurityException, IllegalArgumentException, IllegalAccessException, PolicyForwardingException {
81         final AbstractReceptionHandler handler = new DummyReceptionHandler();
82
83         final PolicyDecoder<PolicyInput, Policy> policyDecoder = new DummyDecoder(false, Collections.emptyList());
84         final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder();
85         setUpPlugins(handler, Collections.singleton(policyDecoder), Collections.singleton(policyForwarder));
86
87         handler.inputReceived(new DummyPolicyInput());
88     }
89
90     // @Test(expected = PolicyDecodingException.class)
91     public void testInputReceivedNoDecoder() throws PolicyDecodingException, NoSuchFieldException, SecurityException,
92             IllegalArgumentException, IllegalAccessException, PolicyForwardingException {
93         final AbstractReceptionHandler handler = new DummyReceptionHandler();
94
95         final DummyPolicyForwarder policyForwarder = new DummyPolicyForwarder();
96         setUpPlugins(handler, Collections.emptySet(), Collections.singleton(policyForwarder));
97
98         handler.inputReceived(new DummyPolicyInput());
99     }
100
101     class DummyReceptionHandler extends AbstractReceptionHandler {
102         @Override
103         protected void initializeReception(final String parameterGroupName) {}
104
105         @Override
106         public void destroy() {}
107     }
108
109     class DummyPolicyInput implements PolicyInput {
110     }
111     class DummyPolicy1 implements Policy {
112     }
113     class DummyPolicy2 implements Policy {
114     }
115
116     public class DummyDecoder implements PolicyDecoder<PolicyInput, Policy> {
117
118         private boolean canHandleValue;
119         private Collection<Policy> policesToReturn;
120
121         public DummyDecoder(final boolean canHandleValue, final Collection<Policy> policesToReturn) {
122             this.canHandleValue = canHandleValue;
123             this.policesToReturn = policesToReturn;
124         }
125
126         @Override
127         public boolean canHandle(final PolicyInput policyInput) {
128             return canHandleValue;
129         }
130
131         @Override
132         public Collection<Policy> decode(final PolicyInput input) throws PolicyDecodingException {
133             return policesToReturn;
134         }
135     }
136
137     public class DummyPolicyForwarder implements PolicyForwarder {
138         private int numberOfPoliciesReceived = 0;
139         private Collection<Policy> policiesReceived = new ArrayList<>();
140
141         @Override
142         public void forward(final Collection<Policy> policies) throws PolicyForwardingException {
143             numberOfPoliciesReceived += policies.size();
144             policiesReceived.addAll(policies);
145         }
146
147         public int getNumberOfPoliciesReceived() {
148             return numberOfPoliciesReceived;
149         }
150
151         public boolean receivedPolicy(final Policy policy) {
152             return policiesReceived.contains(policy);
153         }
154     }
155
156     /**
157      * Only needed until code is added for instantiating plugins from paramater file
158      *
159      * @throws PolicyForwardingException
160      * @throws PolicyDecodingException
161      */
162     private void setUpPlugins(final AbstractReceptionHandler receptionHandler,
163             final Collection<PolicyDecoder<PolicyInput, Policy>> decoders, final Collection<PolicyForwarder> forwarders)
164             throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException,
165             PolicyDecodingException, PolicyForwardingException {
166         final PluginHandler pluginHandler = new PluginHandler("");
167
168         final Field decodersField = pluginHandler.getClass().getDeclaredField("policyDecoders");
169         decodersField.setAccessible(true);
170         decodersField.set(pluginHandler, decoders);
171
172         final Field forwardersField = pluginHandler.getClass().getDeclaredField("policyForwarders");
173         forwardersField.setAccessible(true);
174         forwardersField.set(pluginHandler, forwarders);
175
176         final Field pluginHandlerField = AbstractReceptionHandler.class.getDeclaredField("pluginHandler");
177         pluginHandlerField.setAccessible(true);
178         pluginHandlerField.set(receptionHandler, pluginHandler);
179     }
180
181 }