Initial OpenECOMP policy/engine commit
[policy/engine.git] / PolicyEngineAPI / src / test / java / org / openecomp / 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.openecomp.policy.test;
22
23 import java.io.StringReader;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.UUID;
27
28 import javax.json.Json;
29 import javax.json.JsonObject;
30 import javax.json.JsonReader;
31
32 import junit.framework.TestCase;
33
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;
44
45 import org.openecomp.policy.common.logging.flexlogger.*; 
46
47 public class ConfigFirewallPolicyTest extends TestCase {
48
49         private static final Logger logger = FlexLogger.getLogger(DecisionPolicyApiTest.class);
50
51         private PolicyEngine policyEngine = null;
52         private PolicyEngine mockPolicyEngine = null;
53         
54         PolicyChangeResponse result = null;
55         StdPolicyChangeResponse response = new StdPolicyChangeResponse();
56         PolicyParameters policyParameters = new PolicyParameters();
57         String json = null;
58
59         /**
60          * Perform pre-test initialization
61          *
62          * @throws Exception
63          *
64          * @see TestCase#setUp()
65          */
66         protected void setUp() throws Exception {
67                 try {
68                         policyEngine = new PolicyEngine("Test/config_pass.properties");
69                 } catch (PolicyEngineException e) {
70                         logger.error(e.getMessage());
71                         fail("PolicyEngine Instantiation Error" + e);
72                 }
73                 logger.info("Loaded.. PolicyEngine");
74                 
75                 mockPolicyEngine = Mockito.mock(PolicyEngine.class);
76
77         policyParameters.setPolicyConfigType(PolicyConfigType.Firewall); //required
78         policyParameters.setPolicyName("test.junitTest"); //required
79         
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}]}";
81         
82         policyParameters.setConfigBody(buildJSON(json).toString());
83         
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());
86         }
87
88     private static JsonObject buildJSON(String jsonString) {
89         JsonObject json = null;;
90         if (jsonString != null) {
91             StringReader in = null;
92              
93             in = new StringReader(jsonString);
94              
95             JsonReader jsonReader = Json.createReader(in);
96             json = jsonReader.readObject();        
97         } 
98         
99         return json;
100     }
101         /**
102          * Perform post-test clean up
103          *
104          * @throws Exception
105          *
106          * @see TestCase#tearDown()
107          */
108         protected void tearDown() throws Exception {
109                 super.tearDown();
110                 // Add additional tear down code here
111         }
112
113         /**
114          * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
115          */
116         public void testCreatePolicy() {
117                 response.setResponseMessage("success");
118                 PolicyChangeResponse result = null;
119                 try {
120                 
121                         Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
122                         result = mockPolicyEngine.createPolicy(policyParameters);
123                         
124                 } catch (Exception e) {
125                         logger.warn(e.getMessage());
126                 }
127                 assertEquals(result, response);
128         }
129         
130         /**
131          * Run the String createConfigFirewallPolicy() method test
132          */
133         @Test
134         public void testCreateConfigFirewallPolicy() {
135                 String response = "success";
136                 String result = null;
137                 JsonObject jsonObj = buildJSON(json);
138                 try {
139                 
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);
142
143                 } catch (Exception e) {
144                         logger.warn(e.getMessage());
145                 }
146                 assertEquals(result, response);
147         }
148
149         /**
150          * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
151          */
152         public void testUpdatePolicy() {
153                 response.setResponseMessage("success");
154                 PolicyChangeResponse result = null;
155                 try {
156                 
157                         Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
158                         result = mockPolicyEngine.updatePolicy(policyParameters);
159                         
160                 } catch (Exception e) {
161                         logger.warn(e.getMessage());
162                 }
163                 assertEquals(result, response);
164         }
165         
166         /**
167          * Run the String updateConfigFirewallPolicy() method test
168          */
169         @Test
170         public void testUpdateConfigFirewallPolicy() {
171                 String response = "success";
172                 String result = null;
173                 JsonObject jsonObj = buildJSON(json);
174                 try {
175                 
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);
178
179                 } catch (Exception e) {
180                         logger.warn(e.getMessage());
181                 }
182                 assertEquals(result, response);
183         }
184         
185         @Test
186         public final void testCreatePolicyNullPolicyName() {
187                 response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
188                 policyParameters.setPolicyName(null);
189                 try{
190                         result = policyEngine.createPolicy(policyParameters);
191                 } catch (Exception e){
192                         logger.warn(e.getMessage());
193                 }
194                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
195         }
196         
197         @Test
198         public final void testCreatePolicyNullPolicyScope() {
199                 response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
200                 policyParameters.setPolicyName("test");
201                 try{
202                         result = policyEngine.createPolicy(policyParameters);
203                 } catch (Exception e){
204                         logger.warn(e.getMessage());
205                 }
206                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
207         }
208         
209         @Test
210         public final void testCreatePolicyNullPolicyConfigBody() {
211                 response.setResponseMessage("PE300 - Data Issue: No Config Body given.");
212                 policyParameters.setConfigBody(null);
213                 try{
214                         result = policyEngine.createPolicy(policyParameters);
215                 } catch (Exception e){
216                         logger.warn(e.getMessage());
217                 }
218                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
219         }
220         
221         @Test
222         public final void testUpdatePolicyNullPolicyName() {
223                 response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
224                 policyParameters.setPolicyName(null);
225                 try{
226                         result = policyEngine.updatePolicy(policyParameters);
227                 } catch (Exception e){
228                         logger.warn(e.getMessage());
229                 }
230                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
231         }
232         
233         @Test
234         public final void testUpdatePolicyNullPolicyScope() {
235                 response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
236                 policyParameters.setPolicyName("test");
237                 try{
238                         result = policyEngine.updatePolicy(policyParameters);
239                 } catch (Exception e){
240                         logger.warn(e.getMessage());
241                 }
242                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
243         }
244
245         @Test
246         public final void testUpdatePolicyNullPolicyConfigBody() {
247                 response.setResponseMessage("PE300 - Data Issue: No Config Body given.");
248                 policyParameters.setConfigBody(null);
249                 try{
250                         result = policyEngine.createPolicy(policyParameters);
251                 } catch (Exception e){
252                         logger.warn(e.getMessage());
253                 }
254                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
255         }
256 }