2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.policy.test;
23 import java.io.StringReader;
24 import java.util.HashMap;
26 import java.util.UUID;
28 import javax.json.Json;
29 import javax.json.JsonObject;
30 import javax.json.JsonReader;
32 import junit.framework.TestCase;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.junit.Test;
37 import org.mockito.Mockito;
38 import org.openecomp.policy.api.PolicyChangeResponse;
39 import org.openecomp.policy.api.PolicyConfigType;
40 import org.openecomp.policy.api.PolicyEngine;
41 import org.openecomp.policy.api.PolicyEngineException;
42 import org.openecomp.policy.api.PolicyParameters;
43 import org.openecomp.policy.std.StdPolicyChangeResponse;
45 import org.openecomp.policy.common.logging.flexlogger.*;
47 public class ConfigFirewallPolicyTest extends TestCase {
49 private static final Logger logger = FlexLogger.getLogger(DecisionPolicyApiTest.class);
51 private PolicyEngine policyEngine = null;
52 private PolicyEngine mockPolicyEngine = null;
54 PolicyChangeResponse result = null;
55 StdPolicyChangeResponse response = new StdPolicyChangeResponse();
56 PolicyParameters policyParameters = new PolicyParameters();
60 * Perform pre-test initialization
64 * @see TestCase#setUp()
66 protected void setUp() throws Exception {
68 policyEngine = new PolicyEngine("Test/config_pass.properties");
69 } catch (PolicyEngineException e) {
70 logger.error(e.getMessage());
71 fail("PolicyEngine Instantiation Error" + e);
73 logger.info("Loaded.. PolicyEngine");
75 mockPolicyEngine = Mockito.mock(PolicyEngine.class);
77 policyParameters.setPolicyConfigType(PolicyConfigType.Firewall); //required
78 policyParameters.setPolicyName("test.junitTest"); //required
80 json = "{\"serviceTypeId\":\"/v0/firewall/pan\",\"configName\":\"rule1607\",\"deploymentOption\":{\"deployNow\":false},\"securityZoneId\":\"/v0/firewall/pan\",\"serviceGroups\":[{\"name\":\"1607Group\",\"description\":null,\"members\":[{\"type\":\"REFERENCE\",\"name\":\"SList\"},{\"type\":\"REFERENCE\",\"name\":\"Syslog\"}]},{\"name\":\"Syslog\",\"description\":\"NA\",\"type\":\"SERVICE\",\"transportProtocol\":\"udp\",\"appProtocol\":null,\"ports\":\"514\"},{\"name\":\"SList\",\"description\":\"Service List\",\"type\":\"SERVICE\",\"transportProtocol\":\"tcp\",\"appProtocol\":null,\"ports\":\"8080\"}],\"addressGroups\":[{\"name\":\"1607Group\",\"description\":null,\"members\":[{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"},{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"}]},{\"name\":\"PL_CCE3\",\"description\":\"CCE Routers\",\"members\":[{\"type\":\"SUBNET\",\"value\":\"10.11.12.13/14\"}]}],\"firewallRuleList\":[{\"position\":\"1\",\"ruleName\":\"1607Rule\",\"fromZones\":[\"Trusted\"],\"toZones\":[\"Untrusted\"],\"negateSource\":false,\"negateDestination\":false,\"sourceList\":[{\"type\":\"REFERENCE\",\"value\":\"PL_CCE3\"},{\"type\":\"REFERENCE\",\"value\":\"1607Group\"}],\"destinationList\":[{\"type\":\"REFERENCE\",\"value\":\"1607Group\"}],\"sourceServices\":[],\"destServices\":[{\"type\":\"REFERENCE\",\"name\":\"1607Group\"}],\"action\":\"accept\",\"description\":\"Rule for 1607 templates\",\"enabled\":true,\"log\":true}]}";
82 policyParameters.setConfigBody(buildJSON(json).toString());
84 //policyParameters.setPolicyScope("test"); //Directory will be created where the Policies are saved... this displays a a subscope on the GUI
85 policyParameters.setRequestID(UUID.randomUUID());
88 private static JsonObject buildJSON(String jsonString) {
89 JsonObject json = null;;
90 if (jsonString != null) {
91 StringReader in = null;
93 in = new StringReader(jsonString);
95 JsonReader jsonReader = Json.createReader(in);
96 json = jsonReader.readObject();
102 * Perform post-test clean up
106 * @see TestCase#tearDown()
108 protected void tearDown() throws Exception {
110 // Add additional tear down code here
114 * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
116 public void testCreatePolicy() {
117 response.setResponseMessage("success");
118 PolicyChangeResponse result = null;
121 Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
122 result = mockPolicyEngine.createPolicy(policyParameters);
124 } catch (Exception e) {
125 logger.warn(e.getMessage());
127 assertEquals(result, response);
131 * Run the String createConfigFirewallPolicy() method test
134 public void testCreateConfigFirewallPolicy() {
135 String response = "success";
136 String result = null;
137 JsonObject jsonObj = buildJSON(json);
140 Mockito.when(mockPolicyEngine.createConfigFirewallPolicy("testPolicy",jsonObj, "test", null, null, null, null, null)).thenReturn(response);
141 result = mockPolicyEngine.createConfigFirewallPolicy("testPolicy",jsonObj, "test", null, null, null, null, null);
143 } catch (Exception e) {
144 logger.warn(e.getMessage());
146 assertEquals(result, response);
150 * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
152 public void testUpdatePolicy() {
153 response.setResponseMessage("success");
154 PolicyChangeResponse result = null;
157 Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
158 result = mockPolicyEngine.updatePolicy(policyParameters);
160 } catch (Exception e) {
161 logger.warn(e.getMessage());
163 assertEquals(result, response);
167 * Run the String updateConfigFirewallPolicy() method test
170 public void testUpdateConfigFirewallPolicy() {
171 String response = "success";
172 String result = null;
173 JsonObject jsonObj = buildJSON(json);
176 Mockito.when(mockPolicyEngine.updateConfigFirewallPolicy("testPolicy",jsonObj, "test", null, null, null, null, null)).thenReturn(response);
177 result = mockPolicyEngine.updateConfigFirewallPolicy("testPolicy",jsonObj, "test", null, null, null, null, null);
179 } catch (Exception e) {
180 logger.warn(e.getMessage());
182 assertEquals(result, response);
186 public final void testCreatePolicyNullPolicyName() {
187 response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
188 policyParameters.setPolicyName(null);
190 result = policyEngine.createPolicy(policyParameters);
191 } catch (Exception e){
192 logger.warn(e.getMessage());
194 assertEquals(result.getResponseMessage(), response.getResponseMessage());
198 public final void testCreatePolicyNullPolicyScope() {
199 response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
200 policyParameters.setPolicyName("test");
202 result = policyEngine.createPolicy(policyParameters);
203 } catch (Exception e){
204 logger.warn(e.getMessage());
206 assertEquals(result.getResponseMessage(), response.getResponseMessage());
210 public final void testCreatePolicyNullPolicyConfigBody() {
211 response.setResponseMessage("PE300 - Data Issue: No Config Body given.");
212 policyParameters.setConfigBody(null);
214 result = policyEngine.createPolicy(policyParameters);
215 } catch (Exception e){
216 logger.warn(e.getMessage());
218 assertEquals(result.getResponseMessage(), response.getResponseMessage());
222 public final void testUpdatePolicyNullPolicyName() {
223 response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
224 policyParameters.setPolicyName(null);
226 result = policyEngine.updatePolicy(policyParameters);
227 } catch (Exception e){
228 logger.warn(e.getMessage());
230 assertEquals(result.getResponseMessage(), response.getResponseMessage());
234 public final void testUpdatePolicyNullPolicyScope() {
235 response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
236 policyParameters.setPolicyName("test");
238 result = policyEngine.updatePolicy(policyParameters);
239 } catch (Exception e){
240 logger.warn(e.getMessage());
242 assertEquals(result.getResponseMessage(), response.getResponseMessage());
246 public final void testUpdatePolicyNullPolicyConfigBody() {
247 response.setResponseMessage("PE300 - Data Issue: No Config Body given.");
248 policyParameters.setConfigBody(null);
250 result = policyEngine.createPolicy(policyParameters);
251 } catch (Exception e){
252 logger.warn(e.getMessage());
254 assertEquals(result.getResponseMessage(), response.getResponseMessage());