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()
61 public void setUp() throws Exception {
63 policyEngine = new PolicyEngine("Test/config_pass.properties");
64 } catch (PolicyEngineException e) {
65 logger.error(e.getMessage());
66 fail("PolicyEngine Instantiation Error" + e);
68 logger.info("Loaded.. PolicyEngine");
70 mockPolicyEngine = Mockito.mock(PolicyEngine.class);
72 policyParameters.setPolicyConfigType(PolicyConfigType.Firewall); // required
73 policyParameters.setPolicyName("test.junitTest"); // required
75 json = "{\"serviceTypeId\":\"/v0/firewall/pan\",\"configName\":\"rule1607\",\"deploymentOption\":"
76 + "{\"deployNow\":false},\"securityZoneId\":\"/v0/firewall/pan\",\"serviceGroups\":"
77 + "[{\"name\":\"1607Group\",\"description\":null,\"members\":[{\"type\":\"REFERENCE\","
78 + "\"name\":\"SList\"},{\"type\":\"REFERENCE\",\"name\":\"Syslog\"}]},{\"name\":\"Syslog\","
79 + "\"description\":\"NA\",\"type\":\"SERVICE\",\"transportProtocol\":\"udp\",\"appProtocol\":null,"
80 + "\"ports\":\"514\"},{\"name\":\"SList\",\"description\":\"Service List\",\"type\":\"SERVICE\","
81 + "\"transportProtocol\":\"tcp\",\"appProtocol\":null,\"ports\":\"8080\"}],\"addressGroups\":"
82 + "[{\"name\":\"1607Group\",\"description\":null,\"members\":[{\"type\":\"SUBNET\","
83 + "\"value\":\"10.11.12.13/14\"},{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"}]},"
84 + "{\"name\":\"PL_CCE3\",\"description\":\"CCE Routers\",\"members\":"
85 + "[{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"}]}],\"firewallRuleList\":"
86 + "[{\"position\":\"1\",\"ruleName\":\"1607Rule\",\"fromZones\":[\"Trusted\"],"
87 + "\"toZones\":[\"Untrusted\"],\"negateSource\":false,\"negateDestination\":false,"
88 + "\"sourceList\":[{\"type\":\"REFERENCE\",\"value\":\"PL_CCE3\"},"
89 + "{\"type\":\"REFERENCE\",\"value\":\"1607Group\"}],\"destinationList\":"
90 + "[{\"type\":\"REFERENCE\",\"value\":\"1607Group\"}],\"sourceServices\":[],"
91 + "\"destServices\":[{\"type\":\"REFERENCE\",\"name\":\"1607Group\"}],"
92 + "\"action\":\"accept\",\"description\":\"Rule for 1607 templates\",\"enabled\":true,\"log\":true}]}";
94 policyParameters.setConfigBody(buildJSON(json).toString());
96 // policyParameters.setPolicyScope("test"); //Directory will be created where the Policies
97 // are saved... this displays a a subscope on the GUI
98 policyParameters.setRequestID(UUID.randomUUID());
101 private static JsonObject buildJSON(String jsonString) {
102 JsonObject json = null;;
103 if (jsonString != null) {
104 StringReader in = null;
106 in = new StringReader(jsonString);
108 JsonReader jsonReader = Json.createReader(in);
109 json = jsonReader.readObject();
116 * Perform post-test clean up
120 * @see TestCase#tearDown()
122 public void tearDown() throws Exception {
124 // Add additional tear down code here
128 * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
130 public void testCreatePolicy() {
131 response.setResponseMessage("success");
132 PolicyChangeResponse result = null;
135 Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
136 result = mockPolicyEngine.createPolicy(policyParameters);
138 } catch (Exception e) {
139 logger.warn(e.getMessage());
141 assertEquals(result, response);
145 * Run the String createConfigFirewallPolicy() method test
147 @SuppressWarnings("deprecation")
149 public void testCreateConfigFirewallPolicy() {
150 String response = "success";
151 String result = null;
152 JsonObject jsonObj = buildJSON(json);
155 Mockito.when(mockPolicyEngine.createConfigFirewallPolicy("testPolicy", jsonObj, "test",
156 null, null, null, null, null)).thenReturn(response);
157 result = mockPolicyEngine.createConfigFirewallPolicy("testPolicy", jsonObj, "test",
158 null, null, null, null, null);
160 } catch (Exception e) {
161 logger.warn(e.getMessage());
163 assertEquals(result, response);
167 * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
169 public void testUpdatePolicy() {
170 response.setResponseMessage("success");
171 PolicyChangeResponse result = null;
174 Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
175 result = mockPolicyEngine.updatePolicy(policyParameters);
177 } catch (Exception e) {
178 logger.warn(e.getMessage());
180 assertEquals(result, response);
184 * Run the String updateConfigFirewallPolicy() method test
186 @SuppressWarnings("deprecation")
188 public void testUpdateConfigFirewallPolicy() {
189 String response = "success";
190 String result = null;
191 JsonObject jsonObj = buildJSON(json);
194 Mockito.when(mockPolicyEngine.updateConfigFirewallPolicy("testPolicy", jsonObj, "test",
195 null, null, null, null, null)).thenReturn(response);
196 result = mockPolicyEngine.updateConfigFirewallPolicy("testPolicy", jsonObj, "test",
197 null, null, null, null, null);
199 } catch (Exception e) {
200 logger.warn(e.getMessage());
202 assertEquals(result, response);
206 public final void testCreatePolicyNullPolicyName() {
207 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
208 policyParameters.setPolicyName(null);
210 result = policyEngine.createPolicy(policyParameters);
211 } catch (Exception e) {
212 logger.warn(e.getMessage());
214 assertEquals(result.getResponseMessage(), response.getResponseMessage());
218 public final void testCreatePolicyNullPolicyScope() {
219 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
220 policyParameters.setPolicyName("test");
222 result = policyEngine.createPolicy(policyParameters);
223 } catch (Exception e) {
224 logger.warn(e.getMessage());
226 assertEquals(result.getResponseMessage(), response.getResponseMessage());
230 public final void testCreatePolicyNullPolicyConfigBody() {
231 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
232 policyParameters.setConfigBody(null);
234 result = policyEngine.createPolicy(policyParameters);
235 } catch (Exception e) {
236 logger.warn(e.getMessage());
238 assertEquals(result.getResponseMessage(), response.getResponseMessage());
242 public final void testUpdatePolicyNullPolicyName() {
243 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
244 policyParameters.setPolicyName(null);
246 result = policyEngine.updatePolicy(policyParameters);
247 } catch (Exception e) {
248 logger.warn(e.getMessage());
250 assertEquals(result.getResponseMessage(), response.getResponseMessage());
254 public final void testUpdatePolicyNullPolicyScope() {
255 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
256 policyParameters.setPolicyName("test");
258 result = policyEngine.updatePolicy(policyParameters);
259 } catch (Exception e) {
260 logger.warn(e.getMessage());
262 assertEquals(result.getResponseMessage(), response.getResponseMessage());
266 public final void testUpdatePolicyNullPolicyConfigBody() {
267 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
268 policyParameters.setConfigBody(null);
270 result = policyEngine.createPolicy(policyParameters);
271 } catch (Exception e) {
272 logger.warn(e.getMessage());
274 assertEquals(result.getResponseMessage(), response.getResponseMessage());