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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.distribution.forwarding.apex.pdp;
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;
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;
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;
51 @RunWith(MockitoJUnitRunner.class)
52 public class ApexPdpPolicyForwarderTest {
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";
61 EngineServiceFacade engineServiceFacade;
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);
79 public static void tearDown() {
80 ParameterService.deregister(GROUP_NAME);
84 public void testForwardPolicy() throws ApexException, FileNotFoundException, IOException, PolicyForwardingException,
85 NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
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);
92 final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
93 forwarderField.setAccessible(true);
94 forwarderField.set(forwarder, engineServiceFacade);
96 final ApexPdpPolicy policy = new ApexPdpPolicy("policy", fis);
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");
109 public void testForwardPolicyError()
110 throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
111 SecurityException, IllegalArgumentException, IllegalAccessException {
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);
118 Mockito.doThrow(new ApexException("Failed")).when(engineServiceFacade).deployModel("policy1", fis,
119 IGNORE_CONFLICTS, FORCE_UPDATE);
121 final Field decodersField = forwarder.getClass().getDeclaredField("engineServiceFacade");
122 decodersField.setAccessible(true);
123 decodersField.set(forwarder, engineServiceFacade);
125 final ApexPdpPolicy policy1 = new ApexPdpPolicy("policy1", fis);
126 policies.add(policy1);
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"));
138 public void testForwardMoreThanOnePolicy()
139 throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
140 SecurityException, IllegalArgumentException, IllegalAccessException {
142 final Collection<Policy> policies = new ArrayList<>();
143 final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
144 forwarder.configure(GROUP_NAME);
146 final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
147 forwarderField.setAccessible(true);
148 forwarderField.set(forwarder, engineServiceFacade);
150 final ApexPdpPolicy policy1 =
151 new ApexPdpPolicy("policy1", new FileInputStream(File.createTempFile("policy1", null)));
152 policies.add(policy1);
154 final ApexPdpPolicy policy2 =
155 new ApexPdpPolicy("policy2", new FileInputStream(File.createTempFile("policy2", null)));
156 policies.add(policy2);
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"));
167 public void testForwardUnsupportedPolicy()
168 throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
169 SecurityException, IllegalArgumentException, IllegalAccessException {
171 final Collection<Policy> policies = new ArrayList<>();
172 final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
173 forwarder.configure(GROUP_NAME);
175 final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade");
176 forwarderField.setAccessible(true);
177 forwarderField.set(forwarder, engineServiceFacade);
179 final Policy policy = new UnsupportedPolicy();
180 policies.add(policy);
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"));
190 class UnsupportedPolicy implements Policy {
193 public String getPolicyName() {
194 return "unsupported";
198 public String getPolicyType() {
199 return "unsupported";