Reformat PolicyEngineAPI test cases
[policy/engine.git] / PolicyEngineAPI / src / test / java / org / onap / policy / test / ConfigFirewallPolicyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineAPI
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.test;
24
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;
41
42 public class ConfigFirewallPolicyTest extends TestCase {
43
44     private static final Logger logger = FlexLogger.getLogger(DecisionPolicyApiTest.class);
45
46     private PolicyEngine policyEngine = null;
47     private PolicyEngine mockPolicyEngine = null;
48
49     PolicyChangeResponse result = null;
50     StdPolicyChangeResponse response = new StdPolicyChangeResponse();
51     PolicyParameters policyParameters = new PolicyParameters();
52     String json = null;
53
54     /**
55      * Perform pre-test initialization
56      *
57      * @throws Exception
58      *
59      * @see TestCase#setUp()
60      */
61     public void setUp() throws Exception {
62         try {
63             policyEngine = new PolicyEngine("Test/config_pass.properties");
64         } catch (PolicyEngineException e) {
65             logger.error(e.getMessage());
66             fail("PolicyEngine Instantiation Error" + e);
67         }
68         logger.info("Loaded.. PolicyEngine");
69
70         mockPolicyEngine = Mockito.mock(PolicyEngine.class);
71
72         policyParameters.setPolicyConfigType(PolicyConfigType.Firewall); // required
73         policyParameters.setPolicyName("test.junitTest"); // required
74
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}]}";
93
94         policyParameters.setConfigBody(buildJSON(json).toString());
95
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());
99     }
100
101     private static JsonObject buildJSON(String jsonString) {
102         JsonObject json = null;;
103         if (jsonString != null) {
104             StringReader in = null;
105
106             in = new StringReader(jsonString);
107
108             JsonReader jsonReader = Json.createReader(in);
109             json = jsonReader.readObject();
110         }
111
112         return json;
113     }
114
115     /**
116      * Perform post-test clean up
117      *
118      * @throws Exception
119      *
120      * @see TestCase#tearDown()
121      */
122     public void tearDown() throws Exception {
123         super.tearDown();
124         // Add additional tear down code here
125     }
126
127     /**
128      * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
129      */
130     public void testCreatePolicy() {
131         response.setResponseMessage("success");
132         PolicyChangeResponse result = null;
133         try {
134
135             Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
136             result = mockPolicyEngine.createPolicy(policyParameters);
137
138         } catch (Exception e) {
139             logger.warn(e.getMessage());
140         }
141         assertEquals(result, response);
142     }
143
144     /**
145      * Run the String createConfigFirewallPolicy() method test
146      */
147     @SuppressWarnings("deprecation")
148     @Test
149     public void testCreateConfigFirewallPolicy() {
150         String response = "success";
151         String result = null;
152         JsonObject jsonObj = buildJSON(json);
153         try {
154
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);
159
160         } catch (Exception e) {
161             logger.warn(e.getMessage());
162         }
163         assertEquals(result, response);
164     }
165
166     /**
167      * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
168      */
169     public void testUpdatePolicy() {
170         response.setResponseMessage("success");
171         PolicyChangeResponse result = null;
172         try {
173
174             Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
175             result = mockPolicyEngine.updatePolicy(policyParameters);
176
177         } catch (Exception e) {
178             logger.warn(e.getMessage());
179         }
180         assertEquals(result, response);
181     }
182
183     /**
184      * Run the String updateConfigFirewallPolicy() method test
185      */
186     @SuppressWarnings("deprecation")
187     @Test
188     public void testUpdateConfigFirewallPolicy() {
189         String response = "success";
190         String result = null;
191         JsonObject jsonObj = buildJSON(json);
192         try {
193
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);
198
199         } catch (Exception e) {
200             logger.warn(e.getMessage());
201         }
202         assertEquals(result, response);
203     }
204
205     @Test
206     public final void testCreatePolicyNullPolicyName() {
207         response.setResponseMessage("PE500 - Process Flow Issue: :500:");
208         policyParameters.setPolicyName(null);
209         try {
210             result = policyEngine.createPolicy(policyParameters);
211         } catch (Exception e) {
212             logger.warn(e.getMessage());
213         }
214         assertEquals(result.getResponseMessage(), response.getResponseMessage());
215     }
216
217     @Test
218     public final void testCreatePolicyNullPolicyScope() {
219         response.setResponseMessage("PE500 - Process Flow Issue: :500:");
220         policyParameters.setPolicyName("test");
221         try {
222             result = policyEngine.createPolicy(policyParameters);
223         } catch (Exception e) {
224             logger.warn(e.getMessage());
225         }
226         assertEquals(result.getResponseMessage(), response.getResponseMessage());
227     }
228
229     @Test
230     public final void testCreatePolicyNullPolicyConfigBody() {
231         response.setResponseMessage("PE500 - Process Flow Issue: :500:");
232         policyParameters.setConfigBody(null);
233         try {
234             result = policyEngine.createPolicy(policyParameters);
235         } catch (Exception e) {
236             logger.warn(e.getMessage());
237         }
238         assertEquals(result.getResponseMessage(), response.getResponseMessage());
239     }
240
241     @Test
242     public final void testUpdatePolicyNullPolicyName() {
243         response.setResponseMessage("PE500 - Process Flow Issue: :500:");
244         policyParameters.setPolicyName(null);
245         try {
246             result = policyEngine.updatePolicy(policyParameters);
247         } catch (Exception e) {
248             logger.warn(e.getMessage());
249         }
250         assertEquals(result.getResponseMessage(), response.getResponseMessage());
251     }
252
253     @Test
254     public final void testUpdatePolicyNullPolicyScope() {
255         response.setResponseMessage("PE500 - Process Flow Issue: :500:");
256         policyParameters.setPolicyName("test");
257         try {
258             result = policyEngine.updatePolicy(policyParameters);
259         } catch (Exception e) {
260             logger.warn(e.getMessage());
261         }
262         assertEquals(result.getResponseMessage(), response.getResponseMessage());
263     }
264
265     @Test
266     public final void testUpdatePolicyNullPolicyConfigBody() {
267         response.setResponseMessage("PE500 - Process Flow Issue: :500:");
268         policyParameters.setConfigBody(null);
269         try {
270             result = policyEngine.createPolicy(policyParameters);
271         } catch (Exception e) {
272             logger.warn(e.getMessage());
273         }
274         assertEquals(result.getResponseMessage(), response.getResponseMessage());
275     }
276 }