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