Fixing the BRMS rule generation issue
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / test / XACMLPAPTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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.pap.test;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.sql.SQLException;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Properties;
36
37 import javax.servlet.ServletConfig;
38 import javax.servlet.ServletException;
39 import javax.servlet.ServletOutputStream;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
42
43 import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
44 import org.hibernate.SessionFactory;
45 import org.junit.After;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.mockito.Mockito;
49 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
50 import org.onap.policy.pap.xacml.rest.controller.ClosedLoopDictionaryController;
51 import org.onap.policy.pap.xacml.rest.controller.FirewallDictionaryController;
52 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
53 import org.onap.policy.pap.xacml.rest.policycontroller.PolicyCreation;
54 import org.onap.policy.rest.dao.CommonClassDao;
55 import org.onap.policy.rest.jpa.BRMSParamTemplate;
56 import org.onap.policy.rest.jpa.PolicyEditorScopes;
57 import org.onap.policy.rest.jpa.UserInfo;
58 import org.onap.policy.utils.PolicyUtils;
59 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
60 import org.springframework.mock.web.MockHttpServletResponse;
61 import org.springframework.mock.web.MockServletConfig;
62 import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
63
64 import com.mockrunner.mock.web.MockServletInputStream;
65
66
67 public class XACMLPAPTest {
68
69     private static final String ENVIRONMENT_HEADER = "Environment";
70     private List<String> headers = new ArrayList<>();
71     private HttpServletRequest httpServletRequest;
72     private HttpServletResponse httpServletResponse;
73     private ServletOutputStream mockOutput;
74     private ServletConfig servletConfig;
75     private XACMLPapServlet pap;
76     private SessionFactory sessionFactory;
77
78     @Before
79     public void setUp() throws ServletException {
80         httpServletRequest = Mockito.mock(HttpServletRequest.class);
81         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
82         Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
83         Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
84
85         servletConfig = Mockito.mock(MockServletConfig.class);
86         System.setProperty("com.sun.management.jmxremote.port", "9993");
87         Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
88         Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
89                 .thenReturn("src/test/resources/xacml.pap.properties");
90         pap = new XACMLPapServlet();
91         pap.init(servletConfig);
92     }
93     
94     @Test
95     public void testFirwallCreatePolicy() throws IOException, ServletException, SQLException {
96         httpServletRequest = Mockito.mock(HttpServletRequest.class);
97         String json = "{\"serviceTypeId\":\"/v0/firewall/pan\",\"configName\":\"TestFwPolicyConfig\",\"deploymentOption\":{\"deployNow\":false},\"securityZoneId\":\"cloudsite:dev1a\",\"serviceGroups\":[{\"name\":\"SSH\",\"description\":\"Sshservice entry in servicelist\",\"type\":\"SERVICE\",\"transportProtocol\":\"tcp\",\"appProtocol\":null,\"ports\":\"22\"}],\"addressGroups\":[{\"name\":\"test\",\"description\":\"Destination\",\"members\":[{\"type\":\"SUBNET\",\"value\":\"127.0.0.1/12\"}]},{\"name\":\"TestServers\",\"description\":\"SourceTestServers for firsttesting\",\"members\":[{\"type\":\"SUBNET\",\"value\":\"127.0.0.1/23\"}]}],\"firewallRuleList\":[{\"position\":\"1\",\"ruleName\":\"FWRuleTestServerToTest\",\"fromZones\":[\"UntrustedZoneTestName\"],\"toZones\":[\"TrustedZoneTestName\"],\"negateSource\":false,\"negateDestination\":false,\"sourceList\":[{\"type\":\"REFERENCE\",\"name\":\"TestServers\"}],\"destinationList\":[{\"type\":\"REFERENCE\",\"name\":\"Test\"}],\"sourceServices\":[],\"destServices\":[{\"type\":\"REFERENCE\",\"name\":\"SSH\"}],\"action\":\"accept\",\"description\":\"FWrule for Test source to Test destination\",\"enabled\":true,\"log\":true}]}";
98         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
99         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
100         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
101         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
102         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
103         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Firewall Config", "test", "testDescription", "Test", false, "test", json, 0, 
104                 "5","default", "false", "");
105         MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
106         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
107         
108         // set DBDao
109         setDBDao();
110         pap.service(httpServletRequest, httpServletResponse);
111         
112         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
113         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
114         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_FW_test.1.xml");
115     }
116     
117     @Test
118     public void testBRMSCreatePolicy() throws IOException, ServletException, SQLException {
119         httpServletRequest = Mockito.mock(HttpServletRequest.class);
120         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
121         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
122         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
123         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
124         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
125         Map<String, String> matchingAttributes = new HashMap<>();
126         Map<String, String> ruleAttributes = new HashMap<>();
127         ruleAttributes.put("templateName", "testPolicy");
128         ruleAttributes.put("samPoll", "5");
129         ruleAttributes.put("value", "test");
130         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Param","test", "testing",
131                 "BRMS_PARAM_RULE",false,"test", 
132                 matchingAttributes, 0, "DROOLS", 
133                 null, ruleAttributes, "5",
134                 "default", "false", "", null, null);
135         MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
136         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
137         
138         // set DBDao
139         setDBDao();
140         setPolicyCreation();
141         pap.service(httpServletRequest, httpServletResponse);
142         
143         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
144         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
145         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_BRMS_Param_test.1.xml");
146     }
147     
148     @Test
149     public void testBRMSRawCreatePolicy() throws IOException, ServletException, SQLException {
150         httpServletRequest = Mockito.mock(HttpServletRequest.class);
151         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
152         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
153         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
154         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
155         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
156         Map<String, String> ruleAttributes = new HashMap<>();
157         ruleAttributes.put("value", "test");
158         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Raw","test","testig description",
159                 "BRMS_RAW_RULE",false,"test", ruleAttributes, 0, "DROOLS", 
160                 "test", "4",
161                 "default", "false", null,  null, null);
162         MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
163         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
164         
165         // set DBDao
166         setDBDao();
167         setPolicyCreation();
168         pap.service(httpServletRequest, httpServletResponse);
169         
170         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
171         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
172         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_BRMS_Raw_test.1.xml");
173     }
174     
175     @Test
176     public void testClosedLoopPMCreatePolicy() throws IOException, ServletException, SQLException {
177         httpServletRequest = Mockito.mock(HttpServletRequest.class);
178         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
179         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
180         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
181         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
182         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
183         String json = "{\"test\":\"java\"}";
184         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("ClosedLoop_PM", "test", "testing", "onap", 
185                 json, false, null, "Registration Failure(Trinity)", false, "test", 0, null,
186                 "default", "true", ""); 
187         MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
188         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
189         
190         // set DBDao
191         setDBDao();
192         setPolicyCreation();
193         pap.service(httpServletRequest, httpServletResponse);
194         
195         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
196         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
197         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_PM_test.1.xml");
198     }
199     
200     @Test
201     public void testDecisonAAFPolicy() throws IOException, ServletException, SQLException {
202         httpServletRequest = Mockito.mock(HttpServletRequest.class);
203         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
204         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
205         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
206         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
207         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
208         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("test", "test rule", "ONAP", "AAF", null, null, null, 
209                 null, null, null, null, null, null, null, false, "test", 0);
210         MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
211         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
212         
213         // set DBDao
214         setDBDao();
215         pap.service(httpServletRequest, httpServletResponse);
216         
217         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
218         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
219         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_test.1.xml");
220     }
221     
222     @Test
223     public void testDecisonGuardPolicy() throws IOException, ServletException, SQLException {
224         httpServletRequest = Mockito.mock(HttpServletRequest.class);
225         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
226         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
227         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
228         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
229         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
230         Map<String, String> matchingAttributes = new HashMap<>();
231         matchingAttributes.put("actor","test");
232         matchingAttributes.put("recipe","restart");
233         matchingAttributes.put("targets","test,test1");
234         matchingAttributes.put("clname","");
235         matchingAttributes.put("limit","1");
236         matchingAttributes.put("timeWindow","15");
237         matchingAttributes.put("timeUnits","minute");
238         matchingAttributes.put("guardActiveStart","05:00");
239         matchingAttributes.put("guardActiveEnd","10:00");
240         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("testGuard", "test rule", "PDPD", "GUARD_YAML", matchingAttributes , null, null, 
241                 null, null, null, null, null, null, null, false, "test", 0);
242         MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
243         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
244         
245         // set DBDao
246         setDBDao();
247         pap.service(httpServletRequest, httpServletResponse);
248         
249         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
250         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
251         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testGuard.1.xml");
252     }
253     
254     @Test
255     public void testDecisonBLGuardPolicy() throws IOException, ServletException, SQLException {
256         httpServletRequest = Mockito.mock(HttpServletRequest.class);
257         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
258         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
259         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
260         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
261         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
262         Map<String, String> matchingAttributes = new HashMap<>();
263         matchingAttributes.put("actor","test");
264         matchingAttributes.put("recipe","restart");
265         matchingAttributes.put("clname","test");
266         matchingAttributes.put("guardActiveStart","05:00");
267         matchingAttributes.put("guardActiveEnd","10:00");
268         matchingAttributes.put("blackList","bl1,bl2");
269         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("testblGuard", "test rule", "PDPD", "GUARD_BL_YAML", matchingAttributes , null, null, 
270                 null, null, null, null, null, null, null, false, "test", 0);
271         MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
272         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
273         
274         // set DBDao
275         setDBDao();
276         pap.service(httpServletRequest, httpServletResponse);
277         
278         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
279         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
280         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testblGuard.1.xml");
281     }
282     
283     @Test
284     public void testConfigPolicy() throws IOException, ServletException, SQLException {
285         httpServletRequest = Mockito.mock(HttpServletRequest.class);
286         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
287         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
288         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
289         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
290         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
291         Map<String, String> configAttributes = new HashMap<>();
292         configAttributes.put("value", "test");
293         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Base", "test", "test rule", "TEST", "config", configAttributes, "OTHER", 
294                 "test body", false, "test",0, "5","default", "false", null);
295         MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
296         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
297         
298         // set DBDao
299         setDBDao();
300         pap.service(httpServletRequest, httpServletResponse);
301         
302         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
303         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
304         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_test.1.xml");
305     }
306     
307     private void setPolicyCreation() {
308         CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class);
309         PolicyCreation.setCommonClassDao(commonClassDao);
310         PolicyEditorScopes editorScope = new PolicyEditorScopes();
311         UserInfo userInfo = new UserInfo();
312         userInfo.setUserName("API");
313         userInfo.setUserLoginId("API");
314         editorScope.setScopeName("test");
315         editorScope.setUserCreatedBy(userInfo);
316         editorScope.setUserModifiedBy(userInfo);
317         Mockito.when(commonClassDao.getEntityItem(PolicyEditorScopes.class, "scopeName", "test")).thenReturn(editorScope);
318         BRMSParamTemplate template = new BRMSParamTemplate();
319         template.setRuleName("testPolicy");
320         template.setUserCreatedBy(userInfo);
321         String rule = "package com.sample;\n"
322                 + "import com.sample.DroolsTest.Message;\n"
323                 + "declare PapParams\n"
324                 + "samPoll : int\n"
325                 + "value : String\n"
326                 + "end\n"
327                 + "///This Rule will be generated by the UI.\n"
328                 + "rule \"${policyName}.Create parameters structure\"\n"
329                 + "salience 1000  \n"
330                 + "when\n"
331                 + "then\n"
332                 + "Params params = new Params();\n"
333                 + "params.setSamPoll(76);\n"
334                 + "params.setValue(\"test\");\n"
335                 + "insertLogical(params);\n"
336                 + "end\n"
337                 + "rule \"Rule 1: Check parameter structure access from when/then\"\n"
338                 + "when\n"
339                 + "$param: Params()\n"
340                 + "Params($param.samPoll > 50)\n"
341                 + "then\n"
342                 + "System.out.println(\"Firing rule 1\");\n"
343                 + "System.out.println($param);\n"
344                 + "end\n";
345         template.setRule(rule );
346         Mockito.when(commonClassDao.getEntityItem(BRMSParamTemplate.class, "ruleName", "testPolicy")).thenReturn(template);
347         
348     }
349     
350     @Test
351     public void testClosedLoopCreateDictionary() throws IOException, SQLException, ServletException {
352         httpServletRequest = Mockito.mock(HttpServletRequest.class);
353         // Check VSCLAction. 
354         String json = "{\"dictionaryFields\": {\"vsclaction\": \"testRestAPI\",\"description\": \"testing create\"}}";
355         dictionaryTestSetup(false, "VSCLAction", json);
356         // set DBDao
357         ClosedLoopDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
358         // send Request to PAP
359         pap.service(httpServletRequest, httpServletResponse);
360         // Verify 
361         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
362         //
363         // Check VNFType
364         //
365         httpServletRequest = Mockito.mock(HttpServletRequest.class);
366         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
367         json = "{\"dictionaryFields\": {\"vnftype\": \"testrestAPI1\",\"description\": \"testing create\"}}";
368         dictionaryTestSetup(false, "VNFType", json);
369         // send Request to PAP
370         pap.service(httpServletRequest, httpServletResponse);
371         // Verify 
372         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
373         //
374         // Check PEPOptions
375         //
376         httpServletRequest = Mockito.mock(HttpServletRequest.class);
377         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
378         json = "{\"dictionaryFields\":{\"pepName\":\"testRestAPI\",\"description\":\"testing create\",\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\",\"number\":\"test\"}]}}";
379         dictionaryTestSetup(false, "PEPOptions", json);
380         // send Request to PAP
381         pap.service(httpServletRequest, httpServletResponse);
382         // Verify 
383         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
384         //
385         // Check Varbind
386         //
387         httpServletRequest = Mockito.mock(HttpServletRequest.class);
388         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
389         json = "{\"dictionaryFields\":{\"varbindName\":\"testRestAPI\",\"varbindDescription\":\"testing\",\"varbindOID\":\"test\"}}";
390         dictionaryTestSetup(false, "Varbind", json);
391         // send Request to PAP
392         pap.service(httpServletRequest, httpServletResponse);
393         // Verify 
394         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
395         //
396         // Check Service
397         //
398         httpServletRequest = Mockito.mock(HttpServletRequest.class);
399         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
400         json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPI\",\"description\":\"testing\"}}";
401         dictionaryTestSetup(false, "Service", json);
402         // send Request to PAP
403         pap.service(httpServletRequest, httpServletResponse);
404         // Verify 
405         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
406         //
407         // Check Site
408         //
409         httpServletRequest = Mockito.mock(HttpServletRequest.class);
410         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
411         json = "{\"dictionaryFields\":{\"siteName\":\"testRestAPI\",\"description\":\"testing\"}}";
412         dictionaryTestSetup(false, "Site", json);
413         // send Request to PAP
414         pap.service(httpServletRequest, httpServletResponse);
415         // Verify 
416         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
417     }
418
419     @Test
420     public void testFirewallCreateDictionary() throws IOException, SQLException, ServletException {
421         httpServletRequest = Mockito.mock(HttpServletRequest.class);
422         // Check SecurityZone. 
423         String json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"testing\"}}";
424         dictionaryTestSetup(false, "SecurityZone", json);
425         // set DBDao
426         FirewallDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
427         // send Request to PAP
428         pap.service(httpServletRequest, httpServletResponse);
429         // Verify 
430         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
431         //
432         // Check Action List
433         //
434         httpServletRequest = Mockito.mock(HttpServletRequest.class);
435         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
436         json = "{\"dictionaryFields\":{\"actionName\":\"testRestAPI\",\"description\":\"test\"}}";
437         dictionaryTestSetup(false, "ActionList", json);
438         // send Request to PAP
439         pap.service(httpServletRequest, httpServletResponse);
440         // Verify 
441         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
442         //
443         // Check Protocol List. 
444         //
445         httpServletRequest = Mockito.mock(HttpServletRequest.class);
446         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
447         json = "{\"dictionaryFields\":{\"protocolName\":\"testRestAPI\",\"description\":\"test\"}}";
448         dictionaryTestSetup(false, "ProtocolList", json);
449         // send Request to PAP
450         pap.service(httpServletRequest, httpServletResponse);
451         // Verify 
452         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
453         //
454         // Check Zone. 
455         //
456         httpServletRequest = Mockito.mock(HttpServletRequest.class);
457         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
458         json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"test\"}}";
459         dictionaryTestSetup(false, "Zone", json);
460         // send Request to PAP
461         pap.service(httpServletRequest, httpServletResponse);
462         // Verify 
463         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
464         //
465         // Check PrefixList. 
466         //
467         httpServletRequest = Mockito.mock(HttpServletRequest.class);
468         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
469         json = "{\"dictionaryFields\":{\"prefixListName\":\"testRestAPI\",\"prefixListValue\":\"127.0.0.1\",\"description\":\"testing\"}}";
470         dictionaryTestSetup(false, "PrefixList", json);
471         // send Request to PAP
472         pap.service(httpServletRequest, httpServletResponse);
473         // Verify 
474         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
475         //
476         // Check AddressGroup. 
477         //
478         httpServletRequest = Mockito.mock(HttpServletRequest.class);
479         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
480         json = "{\"dictionaryFields\":{\"groupName\":\"testRestAPIgroup\",\"description\":\"testing\",\"attributes\":[{\"option\":\"testRestAPI\"}, {\"option\":\"testRestAPI\"}]}}";
481         dictionaryTestSetup(false, "AddressGroup", json);
482         // send Request to PAP
483         pap.service(httpServletRequest, httpServletResponse);
484         // Verify 
485         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
486         //
487         // Check ServiceGroup. 
488         //
489         httpServletRequest = Mockito.mock(HttpServletRequest.class);
490         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
491         json = "{\"dictionaryFields\":{\"groupName\":\"testRestAPIServiceGroup\",\"attributes\":[{\"option\":\"testRestAPIservice\"}]}}";
492         dictionaryTestSetup(false, "ServiceGroup", json);
493         // send Request to PAP
494         pap.service(httpServletRequest, httpServletResponse);
495         // Verify 
496         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
497         //
498         // Check ServiceList. 
499         //
500         httpServletRequest = Mockito.mock(HttpServletRequest.class);
501         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
502         json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPIservice\",\"serviceDescription\":\"test\",\"servicePorts\":\"8888\",\"transportProtocols\":[{\"option\":\"testRestAPI\"},{\"option\":\"testRestAPI1\"}],\"appProtocols\":[{\"option\":\"testRestAPI\"},{\"option\":\"testRestAPI1\"}]}}";
503         dictionaryTestSetup(false, "ServiceList", json);
504         // send Request to PAP
505         pap.service(httpServletRequest, httpServletResponse);
506         // Verify 
507         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
508         //
509         // Check TermList. 
510         //
511         httpServletRequest = Mockito.mock(HttpServletRequest.class);
512         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
513         json = "{\"dictionaryFields\":{\"termName\":\"testRestAPIRule\",\"termDescription\":\"testing\",\"fromZoneDatas\":[{\"option\":\"testRestAPI\"}],\"toZoneDatas\":[{\"option\":\"testRestAPI1\"}],\"sourceListDatas\":[{\"option\":\"Group_testportal\"}],\"destinationListDatas\":[{\"option\":\"testRestAPI\"}],\"sourceServiceDatas\":[{\"option\":\"testRestAPIservice\"},{\"option\":\"testRestAPIservice1\"}],\"destinationServiceDatas\":[{\"option\":\"testRestAPIservice1\"},{\"option\":\"testportalservice2\"}],\"actionListDatas\":[{\"option\":\"testRestAPI\"}]}}";
514         dictionaryTestSetup(false, "TermList", json);
515         // send Request to PAP
516         pap.service(httpServletRequest, httpServletResponse);
517         // Verify 
518         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
519     }
520     
521     private void dictionaryTestSetup(Boolean updateFlag, String dictionaryType, String json) throws IOException, SQLException {
522         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
523         Mockito.when(httpServletRequest.getHeader("ClientScope")).thenReturn("dictionaryItem");
524         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
525         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
526         if(updateFlag){
527             Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("update");
528         }else{
529             Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
530         }
531         Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionaryType);
532         MockServletInputStream mockInput = new MockServletInputStream(json.getBytes());
533         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
534         Mockito.when(httpServletRequest.getReader()).thenReturn(new BufferedReader(new InputStreamReader(mockInput)));
535         // set DBDao
536         setDBDao();
537     }
538
539     private void setDBDao() throws SQLException {
540         BasicDataSource dataSource = new BasicDataSource();
541         dataSource.setDriverClassName("org.h2.Driver");
542         // In-memory DB for testing
543         dataSource.setUrl("jdbc:h2:mem:test");
544         dataSource.setUsername("sa");
545         dataSource.setPassword("");
546         LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
547         sessionBuilder.scanPackages("org.onap.*", "com.*");
548
549         Properties properties = new Properties();
550         properties.put("hibernate.show_sql", "false");
551         properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
552         properties.put("hibernate.hbm2ddl.auto", "drop");
553         properties.put("hibernate.hbm2ddl.auto", "create");
554
555         sessionBuilder.addProperties(properties);
556         sessionFactory = sessionBuilder.buildSessionFactory();
557
558         // Set up dao with SessionFactory
559         CommonClassDaoImpl.setSessionfactory(sessionFactory);
560         PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
561     }
562
563     @Test
564     public void testDummy() throws ServletException, IOException {
565
566         Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
567         mockOutput = Mockito.mock(ServletOutputStream.class);
568
569         try {
570             Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
571         } catch (IOException e) {
572             fail();
573         }
574
575         try {
576             pap.service(httpServletRequest, httpServletResponse);
577             assertTrue(true);
578         } catch (Exception e) {
579             fail();
580         }
581     }
582     
583     @After
584     public void destroy(){
585         if(sessionFactory!=null){
586             sessionFactory.close();
587         }
588         pap.destroy();
589     }
590 }