Merge "JUnit additions for ONAP-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 import static org.mockito.Mockito.when;
26
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import java.sql.SQLException;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Properties;
37
38 import javax.servlet.ServletConfig;
39 import javax.servlet.ServletException;
40 import javax.servlet.ServletOutputStream;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43
44 import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
45 import org.hibernate.SessionFactory;
46 import org.junit.After;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.mockito.Mockito;
50 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
51 import org.onap.policy.pap.xacml.rest.controller.ActionPolicyDictionaryController;
52 import org.onap.policy.pap.xacml.rest.controller.ClosedLoopDictionaryController;
53 import org.onap.policy.pap.xacml.rest.controller.DecisionPolicyDictionaryController;
54 import org.onap.policy.pap.xacml.rest.controller.DescriptiveDictionaryController;
55 import org.onap.policy.pap.xacml.rest.controller.DictionaryController;
56 import org.onap.policy.pap.xacml.rest.controller.FirewallDictionaryController;
57 import org.onap.policy.pap.xacml.rest.controller.MicroServiceDictionaryController;
58 import org.onap.policy.pap.xacml.rest.controller.PolicyScopeDictionaryController;
59 import org.onap.policy.pap.xacml.rest.controller.SafePolicyController;
60 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
61 import org.onap.policy.pap.xacml.rest.policycontroller.PolicyCreation;
62 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
63 import org.onap.policy.rest.dao.CommonClassDao;
64 import org.onap.policy.rest.jpa.BRMSParamTemplate;
65 import org.onap.policy.rest.jpa.Category;
66 import org.onap.policy.rest.jpa.PolicyEditorScopes;
67 import org.onap.policy.rest.jpa.UserInfo;
68 import org.onap.policy.utils.PolicyUtils;
69 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
70 import org.springframework.mock.web.MockHttpServletResponse;
71 import org.springframework.mock.web.MockServletConfig;
72 import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
73
74 import com.mockrunner.mock.web.MockServletInputStream;
75
76
77 public class XACMLPAPTest {
78
79     private static final String ENVIRONMENT_HEADER = "Environment";
80     private List<String> headers = new ArrayList<>();
81     private HttpServletRequest httpServletRequest;
82     private HttpServletResponse httpServletResponse;
83     private ServletOutputStream mockOutput;
84     private ServletConfig servletConfig;
85     private XACMLPapServlet pap;
86     private SessionFactory sessionFactory;
87     private CommonClassDao commonClassDao;
88
89     @Before
90     public void setUp() throws ServletException {
91         httpServletRequest = Mockito.mock(HttpServletRequest.class);
92         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
93         Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
94         Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
95
96         servletConfig = Mockito.mock(MockServletConfig.class);
97         System.setProperty("com.sun.management.jmxremote.port", "9993");
98         Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
99         Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
100                 .thenReturn("src/test/resources/xacml.pap.properties");
101         pap = new XACMLPapServlet();
102         pap.init(servletConfig);
103         commonClassDao = Mockito.mock(CommonClassDao.class);
104         new DictionaryUtils(commonClassDao);
105         DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
106         Mockito.mock(DictionaryUtils.class);  
107     }
108     
109     @Test
110     public void testFirwallCreatePolicy() throws IOException, ServletException, SQLException {
111         httpServletRequest = Mockito.mock(HttpServletRequest.class);
112         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}]}";
113         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
114         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
115         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
116         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
117         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
118         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Firewall Config", "test", "testDescription", "Test", false, "test", json, 0, 
119                 "5","default", "false", "");
120         MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
121         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
122         
123         // set DBDao
124         setDBDao();
125         pap.service(httpServletRequest, httpServletResponse);
126         
127         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
128         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
129         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_FW_test.1.xml");
130     }
131     
132     @Test
133     public void testBRMSCreatePolicy() throws IOException, ServletException, SQLException {
134         httpServletRequest = Mockito.mock(HttpServletRequest.class);
135         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
136         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
137         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
138         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
139         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
140         Map<String, String> matchingAttributes = new HashMap<>();
141         Map<String, String> ruleAttributes = new HashMap<>();
142         ruleAttributes.put("templateName", "testPolicy");
143         ruleAttributes.put("samPoll", "5");
144         ruleAttributes.put("value", "test");
145         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Param","test", "testing",
146                 "BRMS_PARAM_RULE",false,"test", 
147                 matchingAttributes, 0, "DROOLS", 
148                 null, ruleAttributes, "5",
149                 "default", "false", "", null, null);
150         MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
151         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
152         
153         // set DBDao
154         setDBDao();
155         setPolicyCreation();
156         pap.service(httpServletRequest, httpServletResponse);
157         
158         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
159         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
160         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_BRMS_Param_test.1.xml");
161     }
162     
163     @Test
164     public void testBRMSRawCreatePolicy() throws IOException, ServletException, SQLException {
165         httpServletRequest = Mockito.mock(HttpServletRequest.class);
166         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
167         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
168         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
169         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
170         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
171         Map<String, String> ruleAttributes = new HashMap<>();
172         ruleAttributes.put("value", "test");
173         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Raw","test","testig description",
174                 "BRMS_RAW_RULE",false,"test", ruleAttributes, 0, "DROOLS", 
175                 "test", "4",
176                 "default", "false", null,  null, null);
177         MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
178         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
179         
180         // set DBDao
181         setDBDao();
182         setPolicyCreation();
183         pap.service(httpServletRequest, httpServletResponse);
184         
185         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
186         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
187         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_BRMS_Raw_test.1.xml");
188     }
189     
190     @Test
191     public void testClosedLoopPMCreatePolicy() throws IOException, ServletException, SQLException {
192         httpServletRequest = Mockito.mock(HttpServletRequest.class);
193         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
194         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
195         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
196         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
197         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
198         String json = "{\"test\":\"java\"}";
199         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("ClosedLoop_PM", "test", "testing", "onap", 
200                 json, false, null, "Registration Failure(Trinity)", false, "test", 0, null,
201                 "default", "true", ""); 
202         MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
203         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
204         
205         // set DBDao
206         setDBDao();
207         setPolicyCreation();
208         pap.service(httpServletRequest, httpServletResponse);
209         
210         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
211         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
212         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_PM_test.1.xml");
213     }
214     
215     @Test
216     public void testDecisonAAFPolicy() throws IOException, ServletException, SQLException {
217         httpServletRequest = Mockito.mock(HttpServletRequest.class);
218         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
219         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
220         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
221         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
222         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
223         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("test", "test rule", "ONAP", "AAF", null, null, null, 
224                 null, null, null, null, null, null, null, false, "test", 0);
225         MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
226         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
227         
228         // set DBDao
229         setDBDao();
230         pap.service(httpServletRequest, httpServletResponse);
231         
232         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
233         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
234         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_test.1.xml");
235     }
236     
237     @Test
238     public void testDecisonGuardPolicy() throws IOException, ServletException, SQLException {
239         httpServletRequest = Mockito.mock(HttpServletRequest.class);
240         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
241         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
242         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
243         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
244         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
245         Map<String, String> matchingAttributes = new HashMap<>();
246         matchingAttributes.put("actor","test");
247         matchingAttributes.put("recipe","restart");
248         matchingAttributes.put("targets","test,test1");
249         matchingAttributes.put("clname","");
250         matchingAttributes.put("limit","1");
251         matchingAttributes.put("timeWindow","15");
252         matchingAttributes.put("timeUnits","minute");
253         matchingAttributes.put("guardActiveStart","05:00");
254         matchingAttributes.put("guardActiveEnd","10:00");
255         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("testGuard", "test rule", "PDPD", "GUARD_YAML", matchingAttributes , null, null, 
256                 null, null, null, null, null, null, null, false, "test", 0);
257         MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
258         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
259         
260         // set DBDao
261         setDBDao();
262         pap.service(httpServletRequest, httpServletResponse);
263         
264         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
265         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
266         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testGuard.1.xml");
267     }
268     
269     @Test
270     public void testDecisonBLGuardPolicy() throws IOException, ServletException, SQLException {
271         httpServletRequest = Mockito.mock(HttpServletRequest.class);
272         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
273         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
274         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
275         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
276         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
277         Map<String, String> matchingAttributes = new HashMap<>();
278         matchingAttributes.put("actor","test");
279         matchingAttributes.put("recipe","restart");
280         matchingAttributes.put("clname","test");
281         matchingAttributes.put("guardActiveStart","05:00");
282         matchingAttributes.put("guardActiveEnd","10:00");
283         matchingAttributes.put("blackList","bl1,bl2");
284         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("testblGuard", "test rule", "PDPD", "GUARD_BL_YAML", matchingAttributes , null, null, 
285                 null, null, null, null, null, null, null, false, "test", 0);
286         MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
287         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
288         
289         // set DBDao
290         setDBDao();
291         pap.service(httpServletRequest, httpServletResponse);
292         
293         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
294         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
295         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testblGuard.1.xml");
296     }
297     
298     @Test
299     public void testConfigPolicy() throws IOException, ServletException, SQLException {
300         httpServletRequest = Mockito.mock(HttpServletRequest.class);
301         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
302         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
303         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
304         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
305         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
306         Map<String, String> configAttributes = new HashMap<>();
307         configAttributes.put("value", "test");
308         StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Base", "test", "test rule", "TEST", "config", configAttributes, "OTHER", 
309                 "test body", false, "test",0, "5","default", "false", null);
310         MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
311         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
312         
313         // set DBDao
314         setDBDao();
315         pap.service(httpServletRequest, httpServletResponse);
316         
317         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
318         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
319         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_test.1.xml");
320     }
321     
322     private void setPolicyCreation() {
323         CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class);
324         PolicyCreation.setCommonClassDao(commonClassDao);
325         PolicyEditorScopes editorScope = new PolicyEditorScopes();
326         UserInfo userInfo = new UserInfo();
327         userInfo.setUserName("API");
328         userInfo.setUserLoginId("API");
329         editorScope.setScopeName("test");
330         editorScope.setUserCreatedBy(userInfo);
331         editorScope.setUserModifiedBy(userInfo);
332         Mockito.when(commonClassDao.getEntityItem(PolicyEditorScopes.class, "scopeName", "test")).thenReturn(editorScope);
333         BRMSParamTemplate template = new BRMSParamTemplate();
334         template.setRuleName("testPolicy");
335         template.setUserCreatedBy(userInfo);
336         String rule = "package com.sample;\n"
337                 + "import com.sample.DroolsTest.Message;\n"
338                 + "declare PapParams\n"
339                 + "samPoll : int\n"
340                 + "value : String\n"
341                 + "end\n"
342                 + "///This Rule will be generated by the UI.\n"
343                 + "rule \"${policyName}.Create parameters structure\"\n"
344                 + "salience 1000  \n"
345                 + "when\n"
346                 + "then\n"
347                 + "Params params = new Params();\n"
348                 + "params.setSamPoll(76);\n"
349                 + "params.setValue(\"test\");\n"
350                 + "insertLogical(params);\n"
351                 + "end\n"
352                 + "rule \"Rule 1: Check parameter structure access from when/then\"\n"
353                 + "when\n"
354                 + "$param: Params()\n"
355                 + "Params($param.samPoll > 50)\n"
356                 + "then\n"
357                 + "System.out.println(\"Firing rule 1\");\n"
358                 + "System.out.println($param);\n"
359                 + "end\n";
360         template.setRule(rule );
361         Mockito.when(commonClassDao.getEntityItem(BRMSParamTemplate.class, "ruleName", "testPolicy")).thenReturn(template);
362         
363     }
364     
365     @Test
366     public void testClosedLoopCreateDictionary() throws IOException, SQLException, ServletException {
367         httpServletRequest = Mockito.mock(HttpServletRequest.class);
368         // Check VSCLAction. 
369         String json = "{\"dictionaryFields\": {\"vsclaction\": \"testRestAPI\",\"description\": \"testing create\"}}";
370         dictionaryTestSetup(false, "VSCLAction", json);
371         // set DBDao
372         ClosedLoopDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
373         // send Request to PAP
374         pap.service(httpServletRequest, httpServletResponse);
375         // Verify 
376         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
377         //
378         // Check VNFType
379         //
380         httpServletRequest = Mockito.mock(HttpServletRequest.class);
381         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
382         json = "{\"dictionaryFields\": {\"vnftype\": \"testrestAPI1\",\"description\": \"testing create\"}}";
383         dictionaryTestSetup(false, "VNFType", json);
384         // send Request to PAP
385         pap.service(httpServletRequest, httpServletResponse);
386         // Verify 
387         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
388         //
389         // Check PEPOptions
390         //
391         httpServletRequest = Mockito.mock(HttpServletRequest.class);
392         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
393         json = "{\"dictionaryFields\":{\"pepName\":\"testRestAPI\",\"description\":\"testing create\",\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\",\"number\":\"test\"}]}}";
394         dictionaryTestSetup(false, "PEPOptions", json);
395         // send Request to PAP
396         pap.service(httpServletRequest, httpServletResponse);
397         // Verify 
398         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
399         //
400         // Check Varbind
401         //
402         httpServletRequest = Mockito.mock(HttpServletRequest.class);
403         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
404         json = "{\"dictionaryFields\":{\"varbindName\":\"testRestAPI\",\"varbindDescription\":\"testing\",\"varbindOID\":\"test\"}}";
405         dictionaryTestSetup(false, "Varbind", json);
406         // send Request to PAP
407         pap.service(httpServletRequest, httpServletResponse);
408         // Verify 
409         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
410         //
411         // Check Service
412         //
413         httpServletRequest = Mockito.mock(HttpServletRequest.class);
414         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
415         json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPI\",\"description\":\"testing\"}}";
416         dictionaryTestSetup(false, "Service", json);
417         // send Request to PAP
418         pap.service(httpServletRequest, httpServletResponse);
419         // Verify 
420         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
421         //
422         // Check Site
423         //
424         httpServletRequest = Mockito.mock(HttpServletRequest.class);
425         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
426         json = "{\"dictionaryFields\":{\"siteName\":\"testRestAPI\",\"description\":\"testing\"}}";
427         dictionaryTestSetup(false, "Site", json);
428         // send Request to PAP
429         pap.service(httpServletRequest, httpServletResponse);
430         // Verify 
431         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
432     }
433
434     @Test
435     public void testFirewallCreateDictionary() throws IOException, SQLException, ServletException {
436         httpServletRequest = Mockito.mock(HttpServletRequest.class);
437         // Check SecurityZone. 
438         String json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"testing\"}}";
439         dictionaryTestSetup(false, "SecurityZone", json);
440         // set DBDao
441         FirewallDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
442         // send Request to PAP
443         pap.service(httpServletRequest, httpServletResponse);
444         // Verify 
445         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
446         //
447         // Check Action List
448         //
449         httpServletRequest = Mockito.mock(HttpServletRequest.class);
450         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
451         json = "{\"dictionaryFields\":{\"actionName\":\"testRestAPI\",\"description\":\"test\"}}";
452         dictionaryTestSetup(false, "ActionList", json);
453         // send Request to PAP
454         pap.service(httpServletRequest, httpServletResponse);
455         // Verify 
456         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
457         //
458         // Check Protocol List. 
459         //
460         httpServletRequest = Mockito.mock(HttpServletRequest.class);
461         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
462         json = "{\"dictionaryFields\":{\"protocolName\":\"testRestAPI\",\"description\":\"test\"}}";
463         dictionaryTestSetup(false, "ProtocolList", json);
464         // send Request to PAP
465         pap.service(httpServletRequest, httpServletResponse);
466         // Verify 
467         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
468         //
469         // Check Zone. 
470         //
471         httpServletRequest = Mockito.mock(HttpServletRequest.class);
472         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
473         json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"test\"}}";
474         dictionaryTestSetup(false, "Zone", json);
475         // send Request to PAP
476         pap.service(httpServletRequest, httpServletResponse);
477         // Verify 
478         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
479         //
480         // Check PrefixList. 
481         //
482         httpServletRequest = Mockito.mock(HttpServletRequest.class);
483         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
484         json = "{\"dictionaryFields\":{\"prefixListName\":\"testRestAPI\",\"prefixListValue\":\"127.0.0.1\",\"description\":\"testing\"}}";
485         dictionaryTestSetup(false, "PrefixList", json);
486         // send Request to PAP
487         pap.service(httpServletRequest, httpServletResponse);
488         // Verify 
489         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
490         //
491         // Check AddressGroup. 
492         //
493         httpServletRequest = Mockito.mock(HttpServletRequest.class);
494         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
495         json = "{\"dictionaryFields\":{\"groupName\":\"testRestAPIgroup\",\"description\":\"testing\",\"attributes\":[{\"option\":\"testRestAPI\"}, {\"option\":\"testRestAPI\"}]}}";
496         dictionaryTestSetup(false, "AddressGroup", json);
497         // send Request to PAP
498         pap.service(httpServletRequest, httpServletResponse);
499         // Verify 
500         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
501         //
502         // Check ServiceGroup. 
503         //
504         httpServletRequest = Mockito.mock(HttpServletRequest.class);
505         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
506         json = "{\"dictionaryFields\":{\"groupName\":\"testRestAPIServiceGroup\",\"attributes\":[{\"option\":\"testRestAPIservice\"}]}}";
507         dictionaryTestSetup(false, "ServiceGroup", json);
508         // send Request to PAP
509         pap.service(httpServletRequest, httpServletResponse);
510         // Verify 
511         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
512         //
513         // Check ServiceList. 
514         //
515         httpServletRequest = Mockito.mock(HttpServletRequest.class);
516         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
517         json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPIservice\",\"serviceDescription\":\"test\",\"servicePorts\":\"8888\",\"transportProtocols\":[{\"option\":\"testRestAPI\"},{\"option\":\"testRestAPI1\"}],\"appProtocols\":[{\"option\":\"testRestAPI\"},{\"option\":\"testRestAPI1\"}]}}";
518         dictionaryTestSetup(false, "ServiceList", json);
519         // send Request to PAP
520         pap.service(httpServletRequest, httpServletResponse);
521         // Verify 
522         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
523         //
524         // Check TermList. 
525         //
526         httpServletRequest = Mockito.mock(HttpServletRequest.class);
527         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
528         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\"}]}}";
529         dictionaryTestSetup(false, "TermList", json);
530         // send Request to PAP
531         pap.service(httpServletRequest, httpServletResponse);
532         // Verify 
533         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
534     }
535     
536     @Test
537     public void testCommonCreateDictionary() throws IOException, SQLException, ServletException {
538         new DictionaryController(commonClassDao);
539         new ActionPolicyDictionaryController(commonClassDao);
540         new SafePolicyController(commonClassDao);
541         new DescriptiveDictionaryController(commonClassDao);
542         List<Object> object = new ArrayList<>();
543         object.add(new Category());
544         when(commonClassDao.getDataById(Category.class, "shortName", "resource")).thenReturn(object);
545         httpServletRequest = Mockito.mock(HttpServletRequest.class);
546         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
547         String json = "{\"dictionaryFields\": {\"onapName\": \"testMMRestAPI1\",\"description\": \"testing update response message\"}}";
548         dictionaryTestSetup(false, "OnapName", json);
549         // send Request to PAP
550         pap.service(httpServletRequest, httpServletResponse);
551         // Verify 
552         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
553         
554         httpServletRequest = Mockito.mock(HttpServletRequest.class);
555         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
556         json = "{\"dictionaryFields\": {\"xacmlId\": \"testMMRestAPI1\",\"datatypeBean\": {\"shortName\": \"string\"}, \"description\": \"testing update\",\"priority\": \"High\",\"userDataTypeValues\": [{\"attributeValues\": \"testAttr\"}, {\"attributeValues\": \"testAttr2\"}, {\"attributeValues\": \"testAttr3\"}]}}";
557         dictionaryTestSetup(false, "Attribute", json);
558         // send Request to PAP
559         pap.service(httpServletRequest, httpServletResponse);
560         // Verify 
561         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
562         
563         
564         httpServletRequest = Mockito.mock(HttpServletRequest.class);
565         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
566         json = "{\"dictionaryFields\":{\"attributeName\":\"TestMMrestAPI1\",\"type\":\"REST\",\"url\":\"testsomeurl.com\",\"method\":\"GET\",\"description\":\"test create\",\"body\":\"Testing Create\",\"headers\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\",\"number\":\"test\"}]}}";
567         dictionaryTestSetup(false, "Action", json);
568         // send Request to PAP
569         pap.service(httpServletRequest, httpServletResponse);
570         // Verify 
571         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
572         
573         httpServletRequest = Mockito.mock(HttpServletRequest.class);
574         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
575         json = "{\"dictionaryFields\":{\"scopeName\":\"testMMRestAPI1\",\"description\":\"test\",\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\",\"number\":\"test\"}]}}";
576         dictionaryTestSetup(false, "DescriptiveScope", json);
577         // send Request to PAP
578         pap.service(httpServletRequest, httpServletResponse);
579         // Verify 
580         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
581         
582         httpServletRequest = Mockito.mock(HttpServletRequest.class);
583         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
584         json = "{\"dictionaryFields\":{\"riskName\":\"testMMrestAPI1\",\"description\":\"test\"}}";
585         dictionaryTestSetup(false, "RiskType", json);
586         // send Request to PAP
587         pap.service(httpServletRequest, httpServletResponse);
588         // Verify 
589         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
590         
591         httpServletRequest = Mockito.mock(HttpServletRequest.class);
592         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
593         json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"message\":\"test\",\"riskType\":\"testMMrestAPI1\"}}";
594         dictionaryTestSetup(false, "SafePolicyWarning", json);
595         // send Request to PAP
596         pap.service(httpServletRequest, httpServletResponse);
597         // Verify 
598         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
599     }
600     
601     @Test
602     public void testDecisionCreateDictionary() throws IOException, SQLException, ServletException {
603         new DecisionPolicyDictionaryController(commonClassDao);
604         httpServletRequest = Mockito.mock(HttpServletRequest.class);
605         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
606         String json = "{\"dictionaryFields\":{\"xacmlId\":\"testMMRestAPI1\",\"datatypeBean\":{\"shortName\":\"string\"},\"description\":\"test\",\"priority\":\"High\"}}";
607         dictionaryTestSetup(false, "Settings", json);
608         // send Request to PAP
609         pap.service(httpServletRequest, httpServletResponse);
610         // Verify 
611         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
612         
613         httpServletRequest = Mockito.mock(HttpServletRequest.class);
614         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
615         json = "{\"dictionaryFields\":{\"bbid\":\"BB1\",\"workstep\":\"1\",\"treatments\":\"Manual Handling,Abort,Retry\"}}";
616         dictionaryTestSetup(false, "RainyDayTreatments", json);
617         // send Request to PAP
618         pap.service(httpServletRequest, httpServletResponse);
619         // Verify 
620         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
621     }
622     
623     @Test
624     public void testMSCreateDictionary() throws IOException, SQLException, ServletException {
625         new MicroServiceDictionaryController(commonClassDao);
626         new PolicyScopeDictionaryController(commonClassDao);
627         httpServletRequest = Mockito.mock(HttpServletRequest.class);
628         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
629         String json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
630         dictionaryTestSetup(false, "MicroServiceLocation", json);
631         // send Request to PAP
632         pap.service(httpServletRequest, httpServletResponse);
633         // Verify 
634         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
635         
636         httpServletRequest = Mockito.mock(HttpServletRequest.class);
637         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
638         json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
639         dictionaryTestSetup(false, "MicroServiceConfigName", json);
640         // send Request to PAP
641         pap.service(httpServletRequest, httpServletResponse);
642         // Verify 
643         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
644         
645         httpServletRequest = Mockito.mock(HttpServletRequest.class);
646         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
647         json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"description\":\"test\"}}";
648         dictionaryTestSetup(false, "DCAEUUID", json);
649         // send Request to PAP
650         pap.service(httpServletRequest, httpServletResponse);
651         // Verify 
652         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
653         
654         httpServletRequest = Mockito.mock(HttpServletRequest.class);
655         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
656         json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
657         dictionaryTestSetup(false, "PolicyScopeService", json);
658         // send Request to PAP
659         pap.service(httpServletRequest, httpServletResponse);
660         // Verify 
661         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
662         
663         httpServletRequest = Mockito.mock(HttpServletRequest.class);
664         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
665         json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
666         dictionaryTestSetup(false, "PolicyScopeResource", json);
667         // send Request to PAP
668         pap.service(httpServletRequest, httpServletResponse);
669         // Verify 
670         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
671         
672         httpServletRequest = Mockito.mock(HttpServletRequest.class);
673         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
674         json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
675         dictionaryTestSetup(false, "PolicyScopeType", json);
676         // send Request to PAP
677         pap.service(httpServletRequest, httpServletResponse);
678         // Verify 
679         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
680         
681         httpServletRequest = Mockito.mock(HttpServletRequest.class);
682         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
683         json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
684         dictionaryTestSetup(false, "PolicyScopeClosedLoop", json);
685         // send Request to PAP
686         pap.service(httpServletRequest, httpServletResponse);
687         // Verify 
688         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
689         
690         httpServletRequest = Mockito.mock(HttpServletRequest.class);
691         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
692         json = "{\"dictionaryFields\":{\"groupName\":\"testMMrestAPI1\",\"description\":\"testing\"},\"groupPolicyScopeListData1\":{\"resource\":\"ANY\",\"type\":\"ANY\",\"service\":\"ANY\",\"closedloop\":\"ANY\"}}";
693         dictionaryTestSetup(false, "GroupPolicyScopeList", json);
694         // send Request to PAP
695         pap.service(httpServletRequest, httpServletResponse);
696         // Verify 
697         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
698     }
699     
700     private void dictionaryTestSetup(Boolean updateFlag, String dictionaryType, String json) throws IOException, SQLException {
701         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
702         Mockito.when(httpServletRequest.getHeader("ClientScope")).thenReturn("dictionaryItem");
703         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
704         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
705         if(updateFlag){
706             Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("update");
707         }else{
708             Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
709         }
710         Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionaryType);
711         MockServletInputStream mockInput = new MockServletInputStream(json.getBytes());
712         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
713         Mockito.when(httpServletRequest.getReader()).thenReturn(new BufferedReader(new InputStreamReader(mockInput)));
714         // set DBDao
715         setDBDao();
716     }
717
718     private void setDBDao() throws SQLException {
719         BasicDataSource dataSource = new BasicDataSource();
720         dataSource.setDriverClassName("org.h2.Driver");
721         // In-memory DB for testing
722         dataSource.setUrl("jdbc:h2:mem:test");
723         dataSource.setUsername("sa");
724         dataSource.setPassword("");
725         LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
726         sessionBuilder.scanPackages("org.onap.*", "com.*");
727
728         Properties properties = new Properties();
729         properties.put("hibernate.show_sql", "false");
730         properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
731         properties.put("hibernate.hbm2ddl.auto", "drop");
732         properties.put("hibernate.hbm2ddl.auto", "create");
733
734         sessionBuilder.addProperties(properties);
735         sessionFactory = sessionBuilder.buildSessionFactory();
736
737         // Set up dao with SessionFactory
738         CommonClassDaoImpl.setSessionfactory(sessionFactory);
739         PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
740     }
741
742     @Test
743     public void getDictionary() throws ServletException, IOException{
744         String[] dictionarys = new String[]{"Attribute", "OnapName", "Action", "BRMSParamTemplate","VSCLAction"
745                         ,"VNFType","PEPOptions","Varbind","Service","Site", "Settings", "RainyDayTreatments",
746                         "DescriptiveScope", "ActionList", "ProtocolList", "Zone", "SecurityZone",
747                         "PrefixList", "AddressGroup", "ServiceGroup", "ServiceList", "TermList",
748                         "MicroServiceLocation", "MicroServiceConfigName", "DCAEUUID", "MicroServiceModels",
749                         "PolicyScopeService", "PolicyScopeResource", "PolicyScopeType", "PolicyScopeClosedLoop",
750                         "GroupPolicyScopeList", "RiskType", "SafePolicyWarning", "MicroServiceDictionary"};
751         for(String dictionary : dictionarys){
752                 httpServletRequest = Mockito.mock(HttpServletRequest.class);
753                 httpServletResponse = new MockHttpServletResponse();
754                 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
755                 Mockito.when(httpServletRequest.getMethod()).thenReturn("GET");
756                 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
757                 Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionary);
758                 pap.service(httpServletRequest, httpServletResponse);
759                 assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
760         }
761     }
762     
763     @Test
764     public void testDummy() throws ServletException, IOException {
765
766         Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
767         mockOutput = Mockito.mock(ServletOutputStream.class);
768
769         try {
770             Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
771         } catch (IOException e) {
772             fail();
773         }
774
775         try {
776             pap.service(httpServletRequest, httpServletResponse);
777             assertTrue(true);
778         } catch (Exception e) {
779             fail();
780         }
781     }
782     
783     @After
784     public void destroy(){
785         if(sessionFactory!=null){
786             sessionFactory.close();
787         }
788         pap.destroy();
789     }
790 }