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