Fixes for sonar critical issues
[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  * 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.test;
22
23 import java.io.StringReader;
24 import java.util.UUID;
25
26 import javax.json.Json;
27 import javax.json.JsonObject;
28 import javax.json.JsonReader;
29
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
41 import junit.framework.TestCase; 
42
43 public class ConfigFirewallPolicyTest extends TestCase {
44
45         private static final Logger logger = FlexLogger.getLogger(DecisionPolicyApiTest.class);
46
47         private PolicyEngine policyEngine = null;
48         private PolicyEngine mockPolicyEngine = null;
49         
50         PolicyChangeResponse result = null;
51         StdPolicyChangeResponse response = new StdPolicyChangeResponse();
52         PolicyParameters policyParameters = new PolicyParameters();
53         String json = null;
54
55         /**
56          * Perform pre-test initialization
57          *
58          * @throws Exception
59          *
60          * @see TestCase#setUp()
61          */
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\":{\"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}]}";
77         
78         policyParameters.setConfigBody(buildJSON(json).toString());
79         
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());
82         }
83
84     private static JsonObject buildJSON(String jsonString) {
85         JsonObject json = null;;
86         if (jsonString != null) {
87             StringReader in = null;
88              
89             in = new StringReader(jsonString);
90              
91             JsonReader jsonReader = Json.createReader(in);
92             json = jsonReader.readObject();        
93         } 
94         
95         return json;
96     }
97         /**
98          * Perform post-test clean up
99          *
100          * @throws Exception
101          *
102          * @see TestCase#tearDown()
103          */
104         public void tearDown() throws Exception {
105                 super.tearDown();
106                 // Add additional tear down code here
107         }
108
109         /**
110          * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
111          */
112         public void testCreatePolicy() {
113                 response.setResponseMessage("success");
114                 PolicyChangeResponse result = null;
115                 try {
116                 
117                         Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
118                         result = mockPolicyEngine.createPolicy(policyParameters);
119                         
120                 } catch (Exception e) {
121                         logger.warn(e.getMessage());
122                 }
123                 assertEquals(result, response);
124         }
125         
126         /**
127          * Run the String createConfigFirewallPolicy() method test
128          */
129         @SuppressWarnings("deprecation")
130         @Test
131         public void testCreateConfigFirewallPolicy() {
132                 String response = "success";
133                 String result = null;
134                 JsonObject jsonObj = buildJSON(json);
135                 try {
136                 
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);
139
140                 } catch (Exception e) {
141                         logger.warn(e.getMessage());
142                 }
143                 assertEquals(result, response);
144         }
145
146         /**
147          * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
148          */
149         public void testUpdatePolicy() {
150                 response.setResponseMessage("success");
151                 PolicyChangeResponse result = null;
152                 try {
153                 
154                         Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
155                         result = mockPolicyEngine.updatePolicy(policyParameters);
156                         
157                 } catch (Exception e) {
158                         logger.warn(e.getMessage());
159                 }
160                 assertEquals(result, response);
161         }
162         
163         /**
164          * Run the String updateConfigFirewallPolicy() method test
165          */
166         @SuppressWarnings("deprecation")
167         @Test
168         public void testUpdateConfigFirewallPolicy() {
169                 String response = "success";
170                 String result = null;
171                 JsonObject jsonObj = buildJSON(json);
172                 try {
173                 
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);
176
177                 } catch (Exception e) {
178                         logger.warn(e.getMessage());
179                 }
180                 assertEquals(result, response);
181         }
182         
183         @Test
184         public final void testCreatePolicyNullPolicyName() {
185                 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
186                 policyParameters.setPolicyName(null);
187                 try{
188                         result = policyEngine.createPolicy(policyParameters);
189                 } catch (Exception e){
190                         logger.warn(e.getMessage());
191                 }
192                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
193         }
194         
195         @Test
196         public final void testCreatePolicyNullPolicyScope() {
197                 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
198                 policyParameters.setPolicyName("test");
199                 try{
200                         result = policyEngine.createPolicy(policyParameters);
201                 } catch (Exception e){
202                         logger.warn(e.getMessage());
203                 }
204                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
205         }
206         
207         @Test
208         public final void testCreatePolicyNullPolicyConfigBody() {
209                 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
210                 policyParameters.setConfigBody(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 testUpdatePolicyNullPolicyName() {
221                 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
222                 policyParameters.setPolicyName(null);
223                 try{
224                         result = policyEngine.updatePolicy(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 testUpdatePolicyNullPolicyScope() {
233                 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
234                 policyParameters.setPolicyName("test");
235                 try{
236                         result = policyEngine.updatePolicy(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 testUpdatePolicyNullPolicyConfigBody() {
245                 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
246                 policyParameters.setConfigBody(null);
247                 try{
248                         result = policyEngine.createPolicy(policyParameters);
249                 } catch (Exception e){
250                         logger.warn(e.getMessage());
251                 }
252                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
253         }
254 }