2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.test;
25 import java.io.StringReader;
26 import java.util.UUID;
27 import javax.json.Json;
28 import javax.json.JsonObject;
29 import javax.json.JsonReader;
30 import org.junit.Test;
31 import org.mockito.Mockito;
32 import org.onap.policy.api.PolicyChangeResponse;
33 import org.onap.policy.api.PolicyConfigType;
34 import org.onap.policy.api.PolicyEngine;
35 import org.onap.policy.api.PolicyEngineException;
36 import org.onap.policy.api.PolicyParameters;
37 import org.onap.policy.common.logging.flexlogger.FlexLogger;
38 import org.onap.policy.common.logging.flexlogger.Logger;
39 import org.onap.policy.std.StdPolicyChangeResponse;
40 import junit.framework.TestCase;
42 public class ConfigFirewallPolicyTest extends TestCase {
44 private static final Logger logger = FlexLogger.getLogger(DecisionPolicyApiTest.class);
46 private PolicyEngine policyEngine = null;
47 private PolicyEngine mockPolicyEngine = null;
49 PolicyChangeResponse result = null;
50 StdPolicyChangeResponse response = new StdPolicyChangeResponse();
51 PolicyParameters policyParameters = new PolicyParameters();
55 * Perform pre-test initialization
59 * @see TestCase#setUp()
62 public void setUp() throws Exception {
64 policyEngine = new PolicyEngine("Test/config_pass.properties");
65 } catch (PolicyEngineException e) {
66 logger.error(e.getMessage());
67 fail("PolicyEngine Instantiation Error" + e);
69 logger.info("Loaded.. PolicyEngine");
71 mockPolicyEngine = Mockito.mock(PolicyEngine.class);
73 policyParameters.setPolicyConfigType(PolicyConfigType.Firewall); // required
74 policyParameters.setPolicyName("test.junitTest"); // required
76 json = "{\"serviceTypeId\":\"/v0/firewall/pan\",\"configName\":\"rule1607\",\"deploymentOption\":"
77 + "{\"deployNow\":false},\"securityZoneId\":\"/v0/firewall/pan\",\"serviceGroups\":"
78 + "[{\"name\":\"1607Group\",\"description\":null,\"members\":[{\"type\":\"REFERENCE\","
79 + "\"name\":\"SList\"},{\"type\":\"REFERENCE\",\"name\":\"Syslog\"}]},{\"name\":\"Syslog\","
80 + "\"description\":\"NA\",\"type\":\"SERVICE\",\"transportProtocol\":\"udp\",\"appProtocol\":null,"
81 + "\"ports\":\"514\"},{\"name\":\"SList\",\"description\":\"Service List\",\"type\":\"SERVICE\","
82 + "\"transportProtocol\":\"tcp\",\"appProtocol\":null,\"ports\":\"8080\"}],\"addressGroups\":"
83 + "[{\"name\":\"1607Group\",\"description\":null,\"members\":[{\"type\":\"SUBNET\","
84 + "\"value\":\"10.11.12.13/14\"},{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"}]},"
85 + "{\"name\":\"PL_CCE3\",\"description\":\"CCE Routers\",\"members\":"
86 + "[{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"}]}],\"firewallRuleList\":"
87 + "[{\"position\":\"1\",\"ruleName\":\"1607Rule\",\"fromZones\":[\"Trusted\"],"
88 + "\"toZones\":[\"Untrusted\"],\"negateSource\":false,\"negateDestination\":false,"
89 + "\"sourceList\":[{\"type\":\"REFERENCE\",\"value\":\"PL_CCE3\"},"
90 + "{\"type\":\"REFERENCE\",\"value\":\"1607Group\"}],\"destinationList\":"
91 + "[{\"type\":\"REFERENCE\",\"value\":\"1607Group\"}],\"sourceServices\":[],"
92 + "\"destServices\":[{\"type\":\"REFERENCE\",\"name\":\"1607Group\"}],"
93 + "\"action\":\"accept\",\"description\":\"Rule for 1607 templates\",\"enabled\":true,\"log\":true}]}";
95 policyParameters.setConfigBody(buildJSON(json).toString());
97 // policyParameters.setPolicyScope("test"); //Directory will be created where the Policies
98 // are saved... this displays a a subscope on the GUI
99 policyParameters.setRequestID(UUID.randomUUID());
102 private static JsonObject buildJSON(String jsonString) {
103 JsonObject json = null;;
104 if (jsonString != null) {
105 StringReader in = null;
107 in = new StringReader(jsonString);
109 JsonReader jsonReader = Json.createReader(in);
110 json = jsonReader.readObject();
117 * Perform post-test clean up
121 * @see TestCase#tearDown()
124 public void tearDown() throws Exception {
126 // Add additional tear down code here
130 * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
132 public void testCreatePolicy() {
133 response.setResponseMessage("success");
134 PolicyChangeResponse result = null;
137 Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
138 result = mockPolicyEngine.createPolicy(policyParameters);
140 } catch (Exception e) {
141 logger.warn(e.getMessage());
143 assertEquals(result, response);
147 * Run the String createConfigFirewallPolicy() method test
149 @SuppressWarnings("deprecation")
151 public void testCreateConfigFirewallPolicy() {
152 String response = "success";
153 String result = null;
154 JsonObject jsonObj = buildJSON(json);
157 Mockito.when(mockPolicyEngine.createConfigFirewallPolicy("testPolicy", jsonObj, "test",
158 null, null, null, null, null)).thenReturn(response);
159 result = mockPolicyEngine.createConfigFirewallPolicy("testPolicy", jsonObj, "test",
160 null, null, null, null, null);
162 } catch (Exception e) {
163 logger.warn(e.getMessage());
165 assertEquals(result, response);
169 * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
171 public void testUpdatePolicy() {
172 response.setResponseMessage("success");
173 PolicyChangeResponse result = null;
176 Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
177 result = mockPolicyEngine.updatePolicy(policyParameters);
179 } catch (Exception e) {
180 logger.warn(e.getMessage());
182 assertEquals(result, response);
186 * Run the String updateConfigFirewallPolicy() method test
188 @SuppressWarnings("deprecation")
190 public void testUpdateConfigFirewallPolicy() {
191 String response = "success";
192 String result = null;
193 JsonObject jsonObj = buildJSON(json);
196 Mockito.when(mockPolicyEngine.updateConfigFirewallPolicy("testPolicy", jsonObj, "test",
197 null, null, null, null, null)).thenReturn(response);
198 result = mockPolicyEngine.updateConfigFirewallPolicy("testPolicy", jsonObj, "test",
199 null, null, null, null, null);
201 } catch (Exception e) {
202 logger.warn(e.getMessage());
204 assertEquals(result, response);
208 public final void testCreatePolicyNullPolicyName() {
209 response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
210 policyParameters.setPolicyName(null);
212 result = policyEngine.createPolicy(policyParameters);
213 } catch (Exception e) {
214 logger.warn(e.getMessage());
216 assertEquals(result.getResponseMessage(), response.getResponseMessage());
220 public final void testCreatePolicyNullPolicyScope() {
221 response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
222 policyParameters.setPolicyName("test");
224 result = policyEngine.createPolicy(policyParameters);
225 } catch (Exception e) {
226 logger.warn(e.getMessage());
228 assertEquals(result.getResponseMessage(), response.getResponseMessage());
232 public final void testCreatePolicyNullPolicyConfigBody() {
233 response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
234 policyParameters.setConfigBody(null);
236 result = policyEngine.createPolicy(policyParameters);
237 } catch (Exception e) {
238 logger.warn(e.getMessage());
240 assertEquals(result.getResponseMessage(), response.getResponseMessage());
244 public final void testUpdatePolicyNullPolicyName() {
245 response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
246 policyParameters.setPolicyName(null);
248 result = policyEngine.updatePolicy(policyParameters);
249 } catch (Exception e) {
250 logger.warn(e.getMessage());
252 assertEquals(result.getResponseMessage(), response.getResponseMessage());
256 public final void testUpdatePolicyNullPolicyScope() {
257 response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
258 policyParameters.setPolicyName("test");
260 result = policyEngine.updatePolicy(policyParameters);
261 } catch (Exception e) {
262 logger.warn(e.getMessage());
264 assertEquals(result.getResponseMessage(), response.getResponseMessage());
268 public final void testUpdatePolicyNullPolicyConfigBody() {
269 response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
270 policyParameters.setConfigBody(null);
272 result = policyEngine.createPolicy(policyParameters);
273 } catch (Exception e) {
274 logger.warn(e.getMessage());
276 assertEquals(result.getResponseMessage(), response.getResponseMessage());