7d6743e1809bfe39fbd2699851fc0ede7cf36151
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.distribution.forwarding.apex.pdp;
23
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Matchers.anyObject;
27 import static org.mockito.Matchers.eq;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.lang.reflect.Field;
34 import java.util.ArrayList;
35 import java.util.Collection;
36
37 import org.junit.AfterClass;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.Mockito;
43 import org.mockito.runners.MockitoJUnitRunner;
44 import org.onap.policy.apex.core.deployment.EngineServiceFacade;
45 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
46 import org.onap.policy.common.parameters.ParameterGroup;
47 import org.onap.policy.common.parameters.ParameterService;
48 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
49 import org.onap.policy.distribution.forwarding.xacml.pdp.testclasses.CommonTestData;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
52
53 /**
54  * Class to perform unit test of {@link ApexPdpPolicyForwarder}.
55  *
56  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
57  */
58 @RunWith(MockitoJUnitRunner.class)
59 public class ApexPdpPolicyForwarderTest {
60
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 ParameterGroup parameterGroup = CommonTestData.getPolicyForwarderParameters(
74                 "src/test/resources/parameters/ApexPdpPolicyForwarderParameters.json",
75                 ApexPdpPolicyForwarderParameterGroup.class);
76
77         parameterGroup.setName(GROUP_NAME);
78         ParameterService.register(parameterGroup);
79     }
80
81     /**
82      * Tear down.
83      */
84     @AfterClass
85     public static void tearDown() {
86         ParameterService.deregister(GROUP_NAME);
87     }
88
89     @Test
90     public void testForwardPolicy() throws ApexException, FileNotFoundException, IOException, PolicyForwardingException,
91             NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
92
93         final Collection<ToscaEntity> policies = new ArrayList<>();
94         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
95         forwarder.configure(GROUP_NAME);
96
97         final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
98         forwarderField.setAccessible(true);
99         forwarderField.set(forwarder, engineServiceFacade);
100
101         createPolicy(policies, "policy", "APEX", "Sample Policy of apex");
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<ToscaEntity> 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         createPolicy(policies, "policy1", "APEX", "Sample Policy of apex");
130
131         try {
132             forwarder.forward(policies);
133             fail("Test must throw an exception");
134         } catch (final Exception exp) {
135             assertTrue(exp.getMessage().contains("Error sending policy to apex-pdp engine"));
136         }
137
138     }
139
140     @Test
141     public void testForwardMoreThanOnePolicy()
142             throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
143             SecurityException, IllegalArgumentException, IllegalAccessException {
144
145         final Collection<ToscaEntity> policies = new ArrayList<>();
146         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
147         forwarder.configure(GROUP_NAME);
148
149         final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
150         forwarderField.setAccessible(true);
151         forwarderField.set(forwarder, engineServiceFacade);
152
153         createPolicy(policies, "policy1", "APEX", "Sample Policy of apex");
154         createPolicy(policies, "policy2", "APEX", "Sample Policy of apex");
155
156         try {
157             forwarder.forward(policies);
158             fail("Test must throw an exception");
159         } catch (final Exception exp) {
160             assertTrue(exp.getMessage().contains("More than one apex policy cannot be forwarded to an apex engine"));
161         }
162     }
163
164     @Test
165     public void testForwardUnsupportedPolicy()
166             throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
167             SecurityException, IllegalArgumentException, IllegalAccessException {
168
169         final Collection<ToscaEntity> policies = new ArrayList<>();
170         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
171         forwarder.configure(GROUP_NAME);
172
173         final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
174         forwarderField.setAccessible(true);
175         forwarderField.set(forwarder, engineServiceFacade);
176
177         final ToscaEntity policy = new UnsupportedPolicy();
178         policies.add(policy);
179
180         try {
181             forwarder.forward(policies);
182             fail("Test must throw an exception");
183         } catch (final Exception exp) {
184             assertTrue(exp.getMessage().contains("Ignoring the policy as it is not of type ToscaPolicy"));
185         }
186     }
187
188     class UnsupportedPolicy extends ToscaEntity {
189
190         @Override
191         public String getName() {
192             return "unsupported";
193         }
194     }
195
196     private void createPolicy(final Collection<ToscaEntity> policies, final String name, final String type,
197             final String description) {
198         final ToscaPolicy policy = new ToscaPolicy();
199         policy.setName(name);
200         policy.setType(type);
201         policy.setDescription(description);
202         policies.add(policy);
203     }
204 }