uplift oparent version 1.2.1->1.2.3
[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     @Override
62     public void setUp() throws Exception {
63         try {
64             policyEngine = new PolicyEngine("Test/config_pass.properties");
65         } catch (PolicyEngineException e) {
66             logger.error(e.getMessage());
67             fail("PolicyEngine Instantiation Error" + e);
68         }
69         logger.info("Loaded.. PolicyEngine");
70
71         mockPolicyEngine = Mockito.mock(PolicyEngine.class);
72
73         policyParameters.setPolicyConfigType(PolicyConfigType.Firewall); // required
74         policyParameters.setPolicyName("test.junitTest"); // required
75
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}]}";
94
95         policyParameters.setConfigBody(buildJSON(json).toString());
96
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());
100     }
101
102     private static JsonObject buildJSON(String jsonString) {
103         JsonObject json = null;;
104         if (jsonString != null) {
105             StringReader in = null;
106
107             in = new StringReader(jsonString);
108
109             JsonReader jsonReader = Json.createReader(in);
110             json = jsonReader.readObject();
111         }
112
113         return json;
114     }
115
116     /**
117      * Perform post-test clean up
118      *
119      * @throws Exception
120      *
121      * @see TestCase#tearDown()
122      */
123     @Override
124     public void tearDown() throws Exception {
125         super.tearDown();
126         // Add additional tear down code here
127     }
128
129     /**
130      * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
131      */
132     public void testCreatePolicy() {
133         response.setResponseMessage("success");
134         PolicyChangeResponse result = null;
135         try {
136
137             Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
138             result = mockPolicyEngine.createPolicy(policyParameters);
139
140         } catch (Exception e) {
141             logger.warn(e.getMessage());
142         }
143         assertEquals(result, response);
144     }
145
146     /**
147      * Run the String createConfigFirewallPolicy() method test
148      */
149     @SuppressWarnings("deprecation")
150     @Test
151     public void testCreateConfigFirewallPolicy() {
152         String response = "success";
153         String result = null;
154         JsonObject jsonObj = buildJSON(json);
155         try {
156
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);
161
162         } catch (Exception e) {
163             logger.warn(e.getMessage());
164         }
165         assertEquals(result, response);
166     }
167
168     /**
169      * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
170      */
171     public void testUpdatePolicy() {
172         response.setResponseMessage("success");
173         PolicyChangeResponse result = null;
174         try {
175
176             Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
177             result = mockPolicyEngine.updatePolicy(policyParameters);
178
179         } catch (Exception e) {
180             logger.warn(e.getMessage());
181         }
182         assertEquals(result, response);
183     }
184
185     /**
186      * Run the String updateConfigFirewallPolicy() method test
187      */
188     @SuppressWarnings("deprecation")
189     @Test
190     public void testUpdateConfigFirewallPolicy() {
191         String response = "success";
192         String result = null;
193         JsonObject jsonObj = buildJSON(json);
194         try {
195
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);
200
201         } catch (Exception e) {
202             logger.warn(e.getMessage());
203         }
204         assertEquals(result, response);
205     }
206
207     @Test
208     public final void testCreatePolicyNullPolicyName() {
209         response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
210         policyParameters.setPolicyName(null);
211         try {
212             result = policyEngine.createPolicy(policyParameters);
213         } catch (Exception e) {
214             logger.warn(e.getMessage());
215         }
216         assertEquals(result.getResponseMessage(), response.getResponseMessage());
217     }
218
219     @Test
220     public final void testCreatePolicyNullPolicyScope() {
221         response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
222         policyParameters.setPolicyName("test");
223         try {
224             result = policyEngine.createPolicy(policyParameters);
225         } catch (Exception e) {
226             logger.warn(e.getMessage());
227         }
228         assertEquals(result.getResponseMessage(), response.getResponseMessage());
229     }
230
231     @Test
232     public final void testCreatePolicyNullPolicyConfigBody() {
233         response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
234         policyParameters.setConfigBody(null);
235         try {
236             result = policyEngine.createPolicy(policyParameters);
237         } catch (Exception e) {
238             logger.warn(e.getMessage());
239         }
240         assertEquals(result.getResponseMessage(), response.getResponseMessage());
241     }
242
243     @Test
244     public final void testUpdatePolicyNullPolicyName() {
245         response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
246         policyParameters.setPolicyName(null);
247         try {
248             result = policyEngine.updatePolicy(policyParameters);
249         } catch (Exception e) {
250             logger.warn(e.getMessage());
251         }
252         assertEquals(result.getResponseMessage(), response.getResponseMessage());
253     }
254
255     @Test
256     public final void testUpdatePolicyNullPolicyScope() {
257         response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
258         policyParameters.setPolicyName("test");
259         try {
260             result = policyEngine.updatePolicy(policyParameters);
261         } catch (Exception e) {
262             logger.warn(e.getMessage());
263         }
264         assertEquals(result.getResponseMessage(), response.getResponseMessage());
265     }
266
267     @Test
268     public final void testUpdatePolicyNullPolicyConfigBody() {
269         response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
270         policyParameters.setConfigBody(null);
271         try {
272             result = policyEngine.createPolicy(policyParameters);
273         } catch (Exception e) {
274             logger.warn(e.getMessage());
275         }
276         assertEquals(result.getResponseMessage(), response.getResponseMessage());
277     }
278 }