a4d1b9fd353289bb57904a39001fc75f5de36451
[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.forwarding.apex.pdp;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Matchers.anyObject;
26 import static org.mockito.Matchers.eq;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.lang.reflect.Field;
33 import java.util.ArrayList;
34 import java.util.Collection;
35
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.Mockito;
42 import org.mockito.runners.MockitoJUnitRunner;
43 import org.onap.policy.apex.core.deployment.EngineServiceFacade;
44 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
45 import org.onap.policy.common.parameters.ParameterGroup;
46 import org.onap.policy.common.parameters.ParameterService;
47 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
48 import org.onap.policy.distribution.model.Policy;
49 import org.onap.policy.distribution.model.PolicyAsString;
50
51 /**
52  * Class to perform unit test of {@link ApexPdpPolicyForwarder}.
53  *
54  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
55  */
56 @RunWith(MockitoJUnitRunner.class)
57 public class ApexPdpPolicyForwarderTest {
58
59     private static final String HOST_NAME = "10.10.10.10";
60     private static final int PORT = 1234;
61     private static final boolean IGNORE_CONFLICTS = false;
62     private static final boolean FORCE_UPDATE = true;
63     private static final String GROUP_NAME = "apexPdpConfiguration";
64
65     @Mock
66     EngineServiceFacade engineServiceFacade;
67
68     /**
69      * Set up.
70      */
71     @BeforeClass
72     public static void setUp() {
73         final ApexPdpPolicyForwarderParameterBuilder builder = new ApexPdpPolicyForwarderParameterBuilder();
74         builder.setHostname(HOST_NAME).setPort(PORT).setIgnoreConflicts(IGNORE_CONFLICTS).setForceUpdate(FORCE_UPDATE);
75         final ParameterGroup parameterGroup = new ApexPdpPolicyForwarderParameterGroup(builder);
76         parameterGroup.setName(GROUP_NAME);
77         ParameterService.register(parameterGroup);
78     }
79
80     /**
81      * Tear down.
82      */
83     @AfterClass
84     public static void tearDown() {
85         ParameterService.deregister(GROUP_NAME);
86     }
87
88     @Test
89     public void testForwardPolicy() throws ApexException, FileNotFoundException, IOException, PolicyForwardingException,
90             NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
91
92         final Collection<Policy> policies = new ArrayList<>();
93         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
94         forwarder.configure(GROUP_NAME);
95
96         final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
97         forwarderField.setAccessible(true);
98         forwarderField.set(forwarder, engineServiceFacade);
99
100         final PolicyAsString policy = new PolicyAsString("policy", "APEX", "Sample Policy of apex");
101         policies.add(policy);
102
103         try {
104             forwarder.forward(policies);
105             verify(engineServiceFacade, times(1)).init();
106             verify(engineServiceFacade, times(1)).deployModel(eq("policy"), anyObject(), eq(IGNORE_CONFLICTS),
107                     eq(FORCE_UPDATE));
108         } catch (final Exception exp) {
109             fail("Test must not throw an exception");
110         }
111     }
112
113     @Test
114     public void testForwardPolicyError()
115             throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
116             SecurityException, IllegalArgumentException, IllegalAccessException {
117
118         final Collection<Policy> policies = new ArrayList<>();
119         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
120         forwarder.configure(GROUP_NAME);
121
122         Mockito.doThrow(new ApexException("Failed")).when(engineServiceFacade).deployModel(eq("policy1"), anyObject(),
123                 eq(IGNORE_CONFLICTS), eq(FORCE_UPDATE));
124
125         final Field decodersField = forwarder.getClass().getDeclaredField("engineServiceFacade");
126         decodersField.setAccessible(true);
127         decodersField.set(forwarder, engineServiceFacade);
128
129         final PolicyAsString policy1 = new PolicyAsString("policy1", "APEX", "Sample Policy of apex");
130         policies.add(policy1);
131
132         try {
133             forwarder.forward(policies);
134             fail("Test must throw an exception");
135         } catch (final Exception exp) {
136             assertTrue(exp.getMessage().contains("Error sending policy to apex-pdp engine"));
137         }
138
139     }
140
141     @Test
142     public void testForwardMoreThanOnePolicy()
143             throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
144             SecurityException, IllegalArgumentException, IllegalAccessException {
145
146         final Collection<Policy> policies = new ArrayList<>();
147         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
148         forwarder.configure(GROUP_NAME);
149
150         final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
151         forwarderField.setAccessible(true);
152         forwarderField.set(forwarder, engineServiceFacade);
153
154         final PolicyAsString policy1 = new PolicyAsString("policy1", "APEX", "Sample Policy of apex");
155         policies.add(policy1);
156
157         final PolicyAsString policy2 = new PolicyAsString("policy2", "APEX", "Sample Policy of apex");
158         policies.add(policy2);
159
160         try {
161             forwarder.forward(policies);
162             fail("Test must throw an exception");
163         } catch (final Exception exp) {
164             assertTrue(exp.getMessage().contains("More than one apex policy cannot be forwarded to an apex engine"));
165         }
166     }
167
168     @Test
169     public void testForwardUnsupportedPolicy()
170             throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
171             SecurityException, IllegalArgumentException, IllegalAccessException {
172
173         final Collection<Policy> policies = new ArrayList<>();
174         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
175         forwarder.configure(GROUP_NAME);
176
177         final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
178         forwarderField.setAccessible(true);
179         forwarderField.set(forwarder, engineServiceFacade);
180
181         final Policy policy = new UnsupportedPolicy();
182         policies.add(policy);
183
184         try {
185             forwarder.forward(policies);
186             fail("Test must throw an exception");
187         } catch (final Exception exp) {
188             assertTrue(exp.getMessage().contains("Ignoring the policy as it is not an apex-pdp policy"));
189         }
190     }
191
192     class UnsupportedPolicy implements Policy {
193
194         @Override
195         public String getPolicyName() {
196             return "unsupported";
197         }
198
199         @Override
200         public String getPolicyType() {
201             return "unsupported";
202         }
203     }
204 }