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.UUID;
26 import javax.json.Json;
27 import javax.json.JsonObject;
28 import javax.json.JsonReader;
30 import org.junit.Test;
31 import org.mockito.Mockito;
32 import org.openecomp.policy.api.PolicyChangeResponse;
33 import org.openecomp.policy.api.PolicyConfigType;
34 import org.openecomp.policy.api.PolicyEngine;
35 import org.openecomp.policy.api.PolicyEngineException;
36 import org.openecomp.policy.api.PolicyParameters;
37 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
38 import org.openecomp.policy.common.logging.flexlogger.Logger;
39 import org.openecomp.policy.std.StdPolicyChangeResponse;
41 import junit.framework.TestCase;
43 public class ConfigFirewallPolicyTest extends TestCase {
45 private static final Logger logger = FlexLogger.getLogger(DecisionPolicyApiTest.class);
47 private PolicyEngine policyEngine = null;
48 private PolicyEngine mockPolicyEngine = null;
50 PolicyChangeResponse result = null;
51 StdPolicyChangeResponse response = new StdPolicyChangeResponse();
52 PolicyParameters policyParameters = new PolicyParameters();
56 * Perform pre-test initialization
60 * @see TestCase#setUp()
62 protected 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\":{\"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}]}";
78 policyParameters.setConfigBody(buildJSON(json).toString());
80 //policyParameters.setPolicyScope("test"); //Directory will be created where the Policies are saved... this displays a a subscope on the GUI
81 policyParameters.setRequestID(UUID.randomUUID());
84 private static JsonObject buildJSON(String jsonString) {
85 JsonObject json = null;;
86 if (jsonString != null) {
87 StringReader in = null;
89 in = new StringReader(jsonString);
91 JsonReader jsonReader = Json.createReader(in);
92 json = jsonReader.readObject();
98 * Perform post-test clean up
102 * @see TestCase#tearDown()
104 protected void tearDown() throws Exception {
106 // Add additional tear down code here
110 * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
112 public void testCreatePolicy() {
113 response.setResponseMessage("success");
114 PolicyChangeResponse result = null;
117 Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
118 result = mockPolicyEngine.createPolicy(policyParameters);
120 } catch (Exception e) {
121 logger.warn(e.getMessage());
123 assertEquals(result, response);
127 * Run the String createConfigFirewallPolicy() method test
129 @SuppressWarnings("deprecation")
131 public void testCreateConfigFirewallPolicy() {
132 String response = "success";
133 String result = null;
134 JsonObject jsonObj = buildJSON(json);
137 Mockito.when(mockPolicyEngine.createConfigFirewallPolicy("testPolicy",jsonObj, "test", null, null, null, null, null)).thenReturn(response);
138 result = mockPolicyEngine.createConfigFirewallPolicy("testPolicy",jsonObj, "test", null, null, null, null, null);
140 } catch (Exception e) {
141 logger.warn(e.getMessage());
143 assertEquals(result, response);
147 * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
149 public void testUpdatePolicy() {
150 response.setResponseMessage("success");
151 PolicyChangeResponse result = null;
154 Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
155 result = mockPolicyEngine.updatePolicy(policyParameters);
157 } catch (Exception e) {
158 logger.warn(e.getMessage());
160 assertEquals(result, response);
164 * Run the String updateConfigFirewallPolicy() method test
166 @SuppressWarnings("deprecation")
168 public void testUpdateConfigFirewallPolicy() {
169 String response = "success";
170 String result = null;
171 JsonObject jsonObj = buildJSON(json);
174 Mockito.when(mockPolicyEngine.updateConfigFirewallPolicy("testPolicy",jsonObj, "test", null, null, null, null, null)).thenReturn(response);
175 result = mockPolicyEngine.updateConfigFirewallPolicy("testPolicy",jsonObj, "test", null, null, null, null, null);
177 } catch (Exception e) {
178 logger.warn(e.getMessage());
180 assertEquals(result, response);
184 public final void testCreatePolicyNullPolicyName() {
185 response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
186 policyParameters.setPolicyName(null);
188 result = policyEngine.createPolicy(policyParameters);
189 } catch (Exception e){
190 logger.warn(e.getMessage());
192 assertEquals(result.getResponseMessage(), response.getResponseMessage());
196 public final void testCreatePolicyNullPolicyScope() {
197 response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
198 policyParameters.setPolicyName("test");
200 result = policyEngine.createPolicy(policyParameters);
201 } catch (Exception e){
202 logger.warn(e.getMessage());
204 assertEquals(result.getResponseMessage(), response.getResponseMessage());
208 public final void testCreatePolicyNullPolicyConfigBody() {
209 response.setResponseMessage("PE300 - Data Issue: No Config Body given.");
210 policyParameters.setConfigBody(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 testUpdatePolicyNullPolicyName() {
221 response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
222 policyParameters.setPolicyName(null);
224 result = policyEngine.updatePolicy(policyParameters);
225 } catch (Exception e){
226 logger.warn(e.getMessage());
228 assertEquals(result.getResponseMessage(), response.getResponseMessage());
232 public final void testUpdatePolicyNullPolicyScope() {
233 response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
234 policyParameters.setPolicyName("test");
236 result = policyEngine.updatePolicy(policyParameters);
237 } catch (Exception e){
238 logger.warn(e.getMessage());
240 assertEquals(result.getResponseMessage(), response.getResponseMessage());
244 public final void testUpdatePolicyNullPolicyConfigBody() {
245 response.setResponseMessage("PE300 - Data Issue: No Config Body given.");
246 policyParameters.setConfigBody(null);
248 result = policyEngine.createPolicy(policyParameters);
249 } catch (Exception e){
250 logger.warn(e.getMessage());
252 assertEquals(result.getResponseMessage(), response.getResponseMessage());