cae94f1870fae1e10eeec66d451c44a86b0183f5
[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.models.tosca.authorative.concepts.ToscaEntity;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
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<ToscaEntity> 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         createPolicy(policies, "policy", "APEX", "Sample Policy of apex");
101
102         try {
103             forwarder.forward(policies);
104             verify(engineServiceFacade, times(1)).init();
105             verify(engineServiceFacade, times(1)).deployModel(eq("policy"), anyObject(), eq(IGNORE_CONFLICTS),
106                     eq(FORCE_UPDATE));
107         } catch (final Exception exp) {
108             fail("Test must not throw an exception");
109         }
110     }
111
112     @Test
113     public void testForwardPolicyError()
114             throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
115             SecurityException, IllegalArgumentException, IllegalAccessException {
116
117         final Collection<ToscaEntity> policies = new ArrayList<>();
118         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
119         forwarder.configure(GROUP_NAME);
120
121         Mockito.doThrow(new ApexException("Failed")).when(engineServiceFacade).deployModel(eq("policy1"), anyObject(),
122                 eq(IGNORE_CONFLICTS), eq(FORCE_UPDATE));
123
124         final Field decodersField = forwarder.getClass().getDeclaredField("engineServiceFacade");
125         decodersField.setAccessible(true);
126         decodersField.set(forwarder, engineServiceFacade);
127
128         createPolicy(policies, "policy1", "APEX", "Sample Policy of apex");
129
130         try {
131             forwarder.forward(policies);
132             fail("Test must throw an exception");
133         } catch (final Exception exp) {
134             assertTrue(exp.getMessage().contains("Error sending policy to apex-pdp engine"));
135         }
136
137     }
138
139     @Test
140     public void testForwardMoreThanOnePolicy()
141             throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
142             SecurityException, IllegalArgumentException, IllegalAccessException {
143
144         final Collection<ToscaEntity> policies = new ArrayList<>();
145         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
146         forwarder.configure(GROUP_NAME);
147
148         final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
149         forwarderField.setAccessible(true);
150         forwarderField.set(forwarder, engineServiceFacade);
151
152         createPolicy(policies, "policy1", "APEX", "Sample Policy of apex");
153         createPolicy(policies, "policy2", "APEX", "Sample Policy of apex");
154
155         try {
156             forwarder.forward(policies);
157             fail("Test must throw an exception");
158         } catch (final Exception exp) {
159             assertTrue(exp.getMessage().contains("More than one apex policy cannot be forwarded to an apex engine"));
160         }
161     }
162
163     @Test
164     public void testForwardUnsupportedPolicy()
165             throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
166             SecurityException, IllegalArgumentException, IllegalAccessException {
167
168         final Collection<ToscaEntity> policies = new ArrayList<>();
169         final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
170         forwarder.configure(GROUP_NAME);
171
172         final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
173         forwarderField.setAccessible(true);
174         forwarderField.set(forwarder, engineServiceFacade);
175
176         final ToscaEntity policy = new UnsupportedPolicy();
177         policies.add(policy);
178
179         try {
180             forwarder.forward(policies);
181             fail("Test must throw an exception");
182         } catch (final Exception exp) {
183             assertTrue(exp.getMessage().contains("Ignoring the policy as it is not of type ToscaPolicy"));
184         }
185     }
186
187     class UnsupportedPolicy extends ToscaEntity {
188
189         @Override
190         public String getName() {
191             return "unsupported";
192         }
193     }
194
195     private void createPolicy(final Collection<ToscaEntity> policies, final String name, final String type,
196             final String description) {
197         final ToscaPolicy policy = new ToscaPolicy();
198         policy.setName(name);
199         policy.setType(type);
200         policy.setDescription(description);
201         policies.add(policy);
202     }
203 }