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