Use builder for std pap policy
[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  * Modifications Copyright (C) 2018 Samsung Electronics Co., Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.test;
23
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Mockito.when;
27
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.sql.SQLException;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Properties;
38
39 import javax.persistence.EntityManager;
40 import javax.persistence.EntityManagerFactory;
41 import javax.persistence.EntityTransaction;
42 import javax.persistence.Persistence;
43 import javax.servlet.ServletConfig;
44 import javax.servlet.ServletException;
45 import javax.servlet.ServletOutputStream;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48
49 import org.apache.commons.logging.Log;
50 import org.apache.commons.logging.LogFactory;
51 import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
52 import org.hibernate.SessionFactory;
53 import org.junit.After;
54 import org.junit.Before;
55 import org.junit.Test;
56 import org.mockito.Mockito;
57 import org.onap.policy.common.ia.IntegrityAuditProperties;
58 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
59 import org.onap.policy.pap.xacml.rest.controller.ActionPolicyDictionaryController;
60 import org.onap.policy.pap.xacml.rest.controller.ClosedLoopDictionaryController;
61 import org.onap.policy.pap.xacml.rest.controller.DecisionPolicyDictionaryController;
62 import org.onap.policy.pap.xacml.rest.controller.DescriptiveDictionaryController;
63 import org.onap.policy.pap.xacml.rest.controller.DictionaryController;
64 import org.onap.policy.pap.xacml.rest.controller.FirewallDictionaryController;
65 import org.onap.policy.pap.xacml.rest.controller.MicroServiceDictionaryController;
66 import org.onap.policy.pap.xacml.rest.controller.PolicyScopeDictionaryController;
67 import org.onap.policy.pap.xacml.rest.controller.SafePolicyController;
68 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
69 import org.onap.policy.pap.xacml.rest.policycontroller.PolicyCreation;
70 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
71 import org.onap.policy.rest.dao.CommonClassDao;
72 import org.onap.policy.rest.jpa.BRMSParamTemplate;
73 import org.onap.policy.rest.jpa.Category;
74 import org.onap.policy.rest.jpa.PolicyEditorScopes;
75 import org.onap.policy.rest.jpa.UserInfo;
76 import org.onap.policy.utils.PolicyUtils;
77 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
78 import org.onap.policy.xacml.std.pap.StdPAPPolicyParams;
79 import org.springframework.mock.web.MockHttpServletResponse;
80 import org.springframework.mock.web.MockServletConfig;
81 import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
82
83 import com.mockrunner.mock.web.MockServletInputStream;
84
85 public class XACMLPAPTest {
86     private static final Log logger = LogFactory.getLog(XACMLPAPTest.class);
87
88     private static final String ENVIRONMENT_HEADER = "Environment";
89     private List<String> headers = new ArrayList<>();
90     private HttpServletRequest httpServletRequest;
91     private HttpServletResponse httpServletResponse;
92     private ServletOutputStream mockOutput;
93     private ServletConfig servletConfig;
94     private XACMLPapServlet pap;
95     private SessionFactory sessionFactory;
96     private CommonClassDao commonClassDao;
97
98     private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";
99     private static final String DEFAULT_DB_USER = "sa";
100     private static final String DEFAULT_DB_PWD = "";
101
102     @Before
103     public void setUpDB() throws Exception {
104         logger.info("setUpDB: Entering");
105
106         Properties properties = new Properties();
107         properties.put(IntegrityAuditProperties.DB_DRIVER, XACMLPAPTest.DEFAULT_DB_DRIVER);
108         properties.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/xacmlTest");
109         properties.put(IntegrityAuditProperties.DB_USER, XACMLPAPTest.DEFAULT_DB_USER);
110         properties.put(IntegrityAuditProperties.DB_PWD, XACMLPAPTest.DEFAULT_DB_PWD);
111         properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
112         properties.put(IntegrityAuditProperties.NODE_TYPE, "pap");
113
114         //Clean the iaTest DB table for IntegrityAuditEntity entries
115         cleanDb("testPapPU", properties);
116
117         logger.info("setUpDB: Exiting");
118     }
119
120     public void cleanDb(String persistenceUnit, Properties properties) {
121         logger.debug("cleanDb: enter");
122
123         EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties);
124
125         EntityManager em = emf.createEntityManager();
126         // Start a transaction
127         EntityTransaction et = em.getTransaction();
128
129         et.begin();
130
131         // Clean up the DB
132         em.createQuery("Delete from IntegrityAuditEntity").executeUpdate();
133
134         // commit transaction
135         et.commit();
136         em.close();
137         logger.debug("cleanDb: exit");
138     }
139
140     @Before
141     public void setUp() throws ServletException {
142         httpServletRequest = Mockito.mock(HttpServletRequest.class);
143         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
144         Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
145         Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
146
147         servletConfig = Mockito.mock(MockServletConfig.class);
148         System.setProperty("com.sun.management.jmxremote.port", "9993");
149         Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
150         Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
151                 .thenReturn("src/test/resources/xacml.pap.properties");
152         pap = new XACMLPapServlet();
153         pap.init(servletConfig);
154         commonClassDao = Mockito.mock(CommonClassDao.class);
155         new DictionaryUtils(commonClassDao);
156         DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
157         Mockito.mock(DictionaryUtils.class);
158     }
159
160     @Test
161     public void testFirwallCreatePolicy() throws IOException, ServletException, SQLException {
162         httpServletRequest = Mockito.mock(HttpServletRequest.class);
163         String json =
164                 "{\"serviceTypeId\":\"/v0/firewall/pan\",\"configName\":\"TestFwPolicyConfig\"," +
165                         "\"deploymentOption\":{\"deployNow\":false},\"securityZoneId\":\"cloudsite:dev1a\"," +
166                         "\"serviceGroups\":[{\"name\":\"SSH\",\"description\":\"Sshservice entry in servicelist\"," +
167                         "\"type\":\"SERVICE\",\"transportProtocol\":\"tcp\",\"appProtocol\":null,\"ports\":\"22\"}]," +
168                         "\"addressGroups\":[{\"name\":\"test\",\"description\":\"Destination\"," +
169                         "\"members\":[{\"type\":\"SUBNET\",\"value\":\"127.0.0.1/12\"}]},{\"name\":\"TestServers\"," +
170                         "\"description\":\"SourceTestServers for firsttesting\",\"members\":[{\"type\":\"SUBNET\"," +
171                         "\"value\":\"127.0.0.1/23\"}]}],\"firewallRuleList\":[{\"position\":\"1\"," +
172                         "\"ruleName\":\"FWRuleTestServerToTest\",\"fromZones\":[\"UntrustedZoneTestName\"]," +
173                         "\"toZones\":[\"TrustedZoneTestName\"],\"negateSource\":false,\"negateDestination\":false," +
174                         "\"sourceList\":[{\"type\":\"REFERENCE\",\"name\":\"TestServers\"}]," +
175                         "\"destinationList\":[{\"type\":\"REFERENCE\",\"name\":\"Test\"}],\"sourceServices\":[]," +
176                         "\"destServices\":[{\"type\":\"REFERENCE\",\"name\":\"SSH\"}],\"action\":\"accept\"," +
177                         "\"description\":\"FWrule for Test source to Test destination\",\"enabled\":true," +
178                         "\"log\":true}]}";
179         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
180         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
181         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
182         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
183         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
184         StdPAPPolicy newPAPPolicy =
185                 new StdPAPPolicy(StdPAPPolicyParams.builder()
186                         .configPolicyType("Firewall Config")
187                         .policyName("test")
188                         .description("testDescription")
189                         .configName("Test")
190                         .editPolicy(false)
191                         .domain("test")
192                         .jsonBody(json)
193                         .highestVersion(0)
194                         .riskLevel("5")
195                         .riskType("default")
196                         .guard("false")
197                         .ttlDate("")
198                         .build());
199         MockServletInputStream mockInput =
200                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
201         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
202
203         // set DBDao
204         setDBDao();
205         pap.service(httpServletRequest, httpServletResponse);
206
207         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
208         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
209         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_FW_test.1.xml");
210     }
211
212     @Test
213     public void testBRMSCreatePolicy() throws IOException, ServletException, SQLException {
214         httpServletRequest = Mockito.mock(HttpServletRequest.class);
215         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
216         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
217         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
218         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
219         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
220         Map<String, String> matchingAttributes = new HashMap<>();
221         Map<String, String> ruleAttributes = new HashMap<>();
222         ruleAttributes.put("templateName", "testPolicy");
223         ruleAttributes.put("samPoll", "5");
224         ruleAttributes.put("value", "test");
225         //Creating BRMS Param Policies from the Admin Console
226         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
227                 .configPolicyType("BRMS_Param")
228                 .policyName("test")
229                 .description("testing")
230                 .configName("BRMS_PARAM_RULE")
231                 .editPolicy(false)
232                 .domain("test")
233                 .dynamicFieldConfigAttributes(matchingAttributes)
234                 .highestVersion(0)
235                 .onapName("DROOLS")
236                 .configBodyData(null)
237                 .drlRuleAndUIParams(ruleAttributes)
238                 .riskLevel("5")
239                 .riskType("default")
240                 .guard("false")
241                 .ttlDate("")
242                 .brmsController(null)
243                 .brmsDependency(null)
244                 .build());
245         MockServletInputStream mockInput =
246                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
247         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
248
249         // set DBDao
250         setDBDao();
251         setPolicyCreation();
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.Config_BRMS_Param_test.1.xml");
257     }
258
259     @Test
260     public void testBRMSRawCreatePolicy() 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("Config");
267         Map<String, String> ruleAttributes = new HashMap<>();
268         ruleAttributes.put("value", "test");
269         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
270                 .configPolicyType("BRMS_Raw")
271                 .policyName("test")
272                 .description("testig description")
273                 .configName("BRMS_RAW_RULE")
274                 .editPolicy(false)
275                 .domain("test")
276                 .dynamicFieldConfigAttributes(ruleAttributes)
277                 .highestVersion(0)
278                 .onapName("DROOLS")
279                 .configBodyData("test")
280                 .riskLevel("4")
281                 .riskType("default")
282                 .guard("false")
283                 .build());
284         MockServletInputStream mockInput =
285                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
286         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
287
288         // set DBDao
289         setDBDao();
290         setPolicyCreation();
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.Config_BRMS_Raw_test.1.xml");
296     }
297
298     @Test
299     public void testClosedLoopPMCreatePolicy() 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         String json = "{\"test\":\"java\"}";
307         //Creating CloseLoop_Fault and Performance Metric Policies
308         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
309                 .configPolicyType("ClosedLoop_PM")
310                 .policyName("test")
311                 .description("testing")
312                 .onapName("onap")
313                 .jsonBody(json)
314                 .draft(false)
315                 .oldPolicyFileName(null)
316                 .serviceType("Registration Failure(Trinity)")
317                 .editPolicy(false)
318                 .domain("test")
319                 .highestVersion(0)
320                 .riskLevel(null)
321                 .riskType("default")
322                 .guard("true")
323                 .ttlDate("")
324                 .build());
325         MockServletInputStream mockInput =
326                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
327         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
328
329         // set DBDao
330         setDBDao();
331         setPolicyCreation();
332         pap.service(httpServletRequest, httpServletResponse);
333
334         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
335         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
336         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_PM_test.1.xml");
337     }
338
339     @Test
340     public void testDecisonAAFPolicy() throws IOException, ServletException, SQLException {
341         httpServletRequest = Mockito.mock(HttpServletRequest.class);
342         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
343         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
344         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
345         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
346         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
347         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
348                 .policyName("test")
349                 .description("test rule")
350                 .onapName("ONAP")
351                 .providerComboBox("AAF")
352                 .editPolicy(false)
353                 .domain("test")
354                 .highestVersion(0)
355                 .build());
356         MockServletInputStream mockInput =
357                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
358         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
359
360         // set DBDao
361         setDBDao();
362         pap.service(httpServletRequest, httpServletResponse);
363
364         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
365         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
366         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_test.1.xml");
367     }
368
369     @Test
370     public void testDecisonGuardPolicy() throws IOException, ServletException, SQLException {
371         httpServletRequest = Mockito.mock(HttpServletRequest.class);
372         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
373         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
374         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
375         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
376         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
377         Map<String, String> matchingAttributes = new HashMap<>();
378         matchingAttributes.put("actor", "test");
379         matchingAttributes.put("recipe", "restart");
380         matchingAttributes.put("targets", "test,test1");
381         matchingAttributes.put("clname", "");
382         matchingAttributes.put("limit", "1");
383         matchingAttributes.put("timeWindow", "15");
384         matchingAttributes.put("timeUnits", "minute");
385         matchingAttributes.put("guardActiveStart", "05:00");
386         matchingAttributes.put("guardActiveEnd", "10:00");
387         StdPAPPolicy newPAPPolicy =
388
389                 new StdPAPPolicy(StdPAPPolicyParams.builder()
390                         .policyName("testGuard")
391                         .description("test rule")
392                         .onapName("PDPD")
393                         .providerComboBox("GUARD_YAML")
394                         .dynamicFieldConfigAttributes(matchingAttributes)
395                         .editPolicy(false)
396                         .domain("test")
397                         .highestVersion(0)
398                         .build());
399         MockServletInputStream mockInput =
400                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
401         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
402
403         // set DBDao
404         setDBDao();
405         pap.service(httpServletRequest, httpServletResponse);
406
407         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
408         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
409         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testGuard.1.xml");
410     }
411
412     @Test
413     public void testDecisonBLGuardPolicy() throws IOException, ServletException, SQLException {
414         httpServletRequest = Mockito.mock(HttpServletRequest.class);
415         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
416         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
417         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
418         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
419         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
420         Map<String, String> matchingAttributes = new HashMap<>();
421         matchingAttributes.put("actor", "test");
422         matchingAttributes.put("recipe", "restart");
423         matchingAttributes.put("clname", "test");
424         matchingAttributes.put("guardActiveStart", "05:00");
425         matchingAttributes.put("guardActiveEnd", "10:00");
426         matchingAttributes.put("blackList", "bl1,bl2");
427         StdPAPPolicy newPAPPolicy =
428                 new StdPAPPolicy(StdPAPPolicyParams.builder()
429                         .policyName("testblGuard")
430                         .description("test rule")
431                         .onapName("PDPD")
432                         .providerComboBox("GUARD_BL_YAML")
433                         .dynamicFieldConfigAttributes(matchingAttributes)
434                         .editPolicy(false)
435                         .domain("test")
436                         .highestVersion(0)
437                         .build());
438         MockServletInputStream mockInput =
439                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
440         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
441
442         // set DBDao
443         setDBDao();
444         pap.service(httpServletRequest, httpServletResponse);
445
446         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
447         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
448         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testblGuard.1.xml");
449     }
450
451     @Test
452     public void testConfigPolicy() throws IOException, ServletException, SQLException {
453         httpServletRequest = Mockito.mock(HttpServletRequest.class);
454         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
455         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
456         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
457         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
458         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
459         Map<String, String> configAttributes = new HashMap<>();
460         configAttributes.put("value", "test");
461         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
462                 .configPolicyType("Base")
463                 .policyName("test")
464                 .description("test rule")
465                 .onapName("TEST")
466                 .configName("config")
467                 .dynamicFieldConfigAttributes(configAttributes)
468                 .configType("OTHER")
469                 .configBodyData("test body")
470                 .editPolicy(false)
471                 .domain("test")
472                 .highestVersion(0)
473                 .riskLevel("5")
474                 .riskType("default")
475                 .guard("false")
476                 .ttlDate(null).build());
477         MockServletInputStream mockInput =
478                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
479         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
480
481         // set DBDao
482         setDBDao();
483         pap.service(httpServletRequest, httpServletResponse);
484
485         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
486         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
487         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_test.1.xml");
488     }
489
490     private void setPolicyCreation() {
491         CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class);
492         PolicyCreation.setCommonClassDao(commonClassDao);
493         PolicyEditorScopes editorScope = new PolicyEditorScopes();
494         UserInfo userInfo = new UserInfo();
495         userInfo.setUserName("API");
496         userInfo.setUserLoginId("API");
497         editorScope.setScopeName("test");
498         editorScope.setUserCreatedBy(userInfo);
499         editorScope.setUserModifiedBy(userInfo);
500         Mockito.when(commonClassDao.getEntityItem(PolicyEditorScopes.class, "scopeName", "test"))
501                 .thenReturn(editorScope);
502         BRMSParamTemplate template = new BRMSParamTemplate();
503         template.setRuleName("testPolicy");
504         template.setUserCreatedBy(userInfo);
505         String rule = "package com.sample;\n"
506                 + "import com.sample.DroolsTest.Message;\n"
507                 + "declare Params\n"
508                 + "samPoll : int\n"
509                 + "value : String\n"
510                 + "end\n"
511                 + "///This Rule will be generated by the UI.\n"
512                 + "rule \"${policyName}.Create parameters structure\"\n"
513                 + "salience 1000  \n"
514                 + "when\n"
515                 + "then\n"
516                 + "Params params = new Params();\n"
517                 + "params.setSamPoll(76);\n"
518                 + "params.setValue(\"test\");\n"
519                 + "insertLogical(params);\n"
520                 + "end\n"
521                 + "rule \"Rule 1: Check parameter structure access from when/then\"\n"
522                 + "when\n"
523                 + "$param: Params()\n"
524                 + "Params($param.samPoll > 50)\n"
525                 + "then\n"
526                 + "System.out.println(\"Firing rule 1\");\n"
527                 + "System.out.println($param);\n"
528                 + "end\n";
529         template.setRule(rule);
530         Mockito.when(commonClassDao.getEntityItem(BRMSParamTemplate.class, "ruleName", "testPolicy"))
531                 .thenReturn(template);
532
533     }
534
535     @Test
536     public void testClosedLoopCreateDictionary() throws IOException, SQLException, ServletException {
537         httpServletRequest = Mockito.mock(HttpServletRequest.class);
538         // Check VSCLAction. 
539         String json = "{\"dictionaryFields\": {\"vsclaction\": \"testRestAPI\",\"description\": \"testing create\"}}";
540         dictionaryTestSetup(false, "VSCLAction", json);
541         // set DBDao
542         ClosedLoopDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
543         // send Request to PAP
544         pap.service(httpServletRequest, httpServletResponse);
545         // Verify 
546         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
547         //
548         // Check VNFType
549         //
550         httpServletRequest = Mockito.mock(HttpServletRequest.class);
551         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
552         json = "{\"dictionaryFields\": {\"vnftype\": \"testrestAPI1\",\"description\": \"testing create\"}}";
553         dictionaryTestSetup(false, "VNFType", json);
554         // send Request to PAP
555         pap.service(httpServletRequest, httpServletResponse);
556         // Verify 
557         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
558         //
559         // Check PEPOptions
560         //
561         httpServletRequest = Mockito.mock(HttpServletRequest.class);
562         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
563         json =
564                 "{\"dictionaryFields\":{\"pepName\":\"testRestAPI\",\"description\":\"testing create\"," +
565                         "\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
566                         "\"number\":\"test\"}]}}";
567         dictionaryTestSetup(false, "PEPOptions", json);
568         // send Request to PAP
569         pap.service(httpServletRequest, httpServletResponse);
570         // Verify 
571         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
572         //
573         // Check Varbind
574         //
575         httpServletRequest = Mockito.mock(HttpServletRequest.class);
576         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
577         json =
578                 "{\"dictionaryFields\":{\"varbindName\":\"testRestAPI\",\"varbindDescription\":\"testing\"," +
579                         "\"varbindOID\":\"test\"}}";
580         dictionaryTestSetup(false, "Varbind", json);
581         // send Request to PAP
582         pap.service(httpServletRequest, httpServletResponse);
583         // Verify 
584         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
585         //
586         // Check Service
587         //
588         httpServletRequest = Mockito.mock(HttpServletRequest.class);
589         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
590         json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPI\",\"description\":\"testing\"}}";
591         dictionaryTestSetup(false, "Service", json);
592         // send Request to PAP
593         pap.service(httpServletRequest, httpServletResponse);
594         // Verify 
595         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
596         //
597         // Check Site
598         //
599         httpServletRequest = Mockito.mock(HttpServletRequest.class);
600         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
601         json = "{\"dictionaryFields\":{\"siteName\":\"testRestAPI\",\"description\":\"testing\"}}";
602         dictionaryTestSetup(false, "Site", json);
603         // send Request to PAP
604         pap.service(httpServletRequest, httpServletResponse);
605         // Verify 
606         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
607     }
608
609     @Test
610     public void testFirewallCreateDictionary() throws IOException, SQLException, ServletException {
611         httpServletRequest = Mockito.mock(HttpServletRequest.class);
612         // Check SecurityZone. 
613         String json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"testing\"}}";
614         dictionaryTestSetup(false, "SecurityZone", json);
615         // set DBDao
616         FirewallDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
617         // send Request to PAP
618         pap.service(httpServletRequest, httpServletResponse);
619         // Verify 
620         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
621         //
622         // Check Action List
623         //
624         httpServletRequest = Mockito.mock(HttpServletRequest.class);
625         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
626         json = "{\"dictionaryFields\":{\"actionName\":\"testRestAPI\",\"description\":\"test\"}}";
627         dictionaryTestSetup(false, "ActionList", json);
628         // send Request to PAP
629         pap.service(httpServletRequest, httpServletResponse);
630         // Verify 
631         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
632         //
633         // Check Protocol List. 
634         //
635         httpServletRequest = Mockito.mock(HttpServletRequest.class);
636         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
637         json = "{\"dictionaryFields\":{\"protocolName\":\"testRestAPI\",\"description\":\"test\"}}";
638         dictionaryTestSetup(false, "ProtocolList", json);
639         // send Request to PAP
640         pap.service(httpServletRequest, httpServletResponse);
641         // Verify 
642         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
643         //
644         // Check Zone. 
645         //
646         httpServletRequest = Mockito.mock(HttpServletRequest.class);
647         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
648         json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"test\"}}";
649         dictionaryTestSetup(false, "Zone", json);
650         // send Request to PAP
651         pap.service(httpServletRequest, httpServletResponse);
652         // Verify 
653         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
654         //
655         // Check PrefixList. 
656         //
657         httpServletRequest = Mockito.mock(HttpServletRequest.class);
658         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
659         json =
660                 "{\"dictionaryFields\":{\"prefixListName\":\"testRestAPI\",\"prefixListValue\":\"127.0.0.1\"," +
661                         "\"description\":\"testing\"}}";
662         dictionaryTestSetup(false, "PrefixList", json);
663         // send Request to PAP
664         pap.service(httpServletRequest, httpServletResponse);
665         // Verify 
666         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
667         //
668         // Check AddressGroup. 
669         //
670         httpServletRequest = Mockito.mock(HttpServletRequest.class);
671         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
672         json =
673                 "{\"dictionaryFields\":{\"groupName\":\"testRestAPIgroup\",\"description\":\"testing\"," +
674                         "\"attributes\":[{\"option\":\"testRestAPI\"}, {\"option\":\"testRestAPI\"}]}}";
675         dictionaryTestSetup(false, "AddressGroup", json);
676         // send Request to PAP
677         pap.service(httpServletRequest, httpServletResponse);
678         // Verify 
679         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
680         //
681         // Check ServiceGroup. 
682         //
683         httpServletRequest = Mockito.mock(HttpServletRequest.class);
684         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
685         json =
686                 "{\"dictionaryFields\":{\"groupName\":\"testRestAPIServiceGroup\"," +
687                         "\"attributes\":[{\"option\":\"testRestAPIservice\"}]}}";
688         dictionaryTestSetup(false, "ServiceGroup", json);
689         // send Request to PAP
690         pap.service(httpServletRequest, httpServletResponse);
691         // Verify 
692         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
693         //
694         // Check ServiceList. 
695         //
696         httpServletRequest = Mockito.mock(HttpServletRequest.class);
697         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
698         json =
699                 "{\"dictionaryFields\":{\"serviceName\":\"testRestAPIservice\",\"serviceDescription\":\"test\"," +
700                         "\"servicePorts\":\"8888\",\"transportProtocols\":[{\"option\":\"testRestAPI\"}," +
701                         "{\"option\":\"testRestAPI1\"}],\"appProtocols\":[{\"option\":\"testRestAPI\"}," +
702                         "{\"option\":\"testRestAPI1\"}]}}";
703         dictionaryTestSetup(false, "ServiceList", json);
704         // send Request to PAP
705         pap.service(httpServletRequest, httpServletResponse);
706         // Verify 
707         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
708         //
709         // Check TermList. 
710         //
711         httpServletRequest = Mockito.mock(HttpServletRequest.class);
712         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
713         json =
714                 "{\"dictionaryFields\":{\"termName\":\"testRestAPIRule\",\"termDescription\":\"testing\"," +
715                         "\"fromZoneDatas\":[{\"option\":\"testRestAPI\"}]," +
716                         "\"toZoneDatas\":[{\"option\":\"testRestAPI1\"}]," +
717                         "\"sourceListDatas\":[{\"option\":\"Group_testportal\"}]," +
718                         "\"destinationListDatas\":[{\"option\":\"testRestAPI\"}]," +
719                         "\"sourceServiceDatas\":[{\"option\":\"testRestAPIservice\"}," +
720                         "{\"option\":\"testRestAPIservice1\"}]," +
721                         "\"destinationServiceDatas\":[{\"option\":\"testRestAPIservice1\"}," +
722                         "{\"option\":\"testportalservice2\"}],\"actionListDatas\":[{\"option\":\"testRestAPI\"}]}}";
723         dictionaryTestSetup(false, "TermList", json);
724         // send Request to PAP
725         pap.service(httpServletRequest, httpServletResponse);
726         // Verify 
727         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
728     }
729
730     @Test
731     public void testCommonCreateDictionary() throws IOException, SQLException, ServletException {
732         new DictionaryController(commonClassDao);
733         new ActionPolicyDictionaryController(commonClassDao);
734         new SafePolicyController(commonClassDao);
735         new DescriptiveDictionaryController(commonClassDao);
736         List<Object> object = new ArrayList<>();
737         object.add(new Category());
738         when(commonClassDao.getDataById(Category.class, "shortName", "resource")).thenReturn(object);
739         httpServletRequest = Mockito.mock(HttpServletRequest.class);
740         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
741         String json =
742                 "{\"dictionaryFields\": {\"onapName\": \"testMMRestAPI1\",\"description\": \"testing update response " +
743                         "message\"}}";
744         dictionaryTestSetup(false, "OnapName", json);
745         // send Request to PAP
746         pap.service(httpServletRequest, httpServletResponse);
747         // Verify
748         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
749
750         httpServletRequest = Mockito.mock(HttpServletRequest.class);
751         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
752         json =
753                 "{\"dictionaryFields\": {\"xacmlId\": \"testMMRestAPI1\",\"datatypeBean\": {\"shortName\": " +
754                         "\"string\"}, \"description\": \"testing update\",\"priority\": \"High\"," +
755                         "\"userDataTypeValues\": [{\"attributeValues\": \"testAttr\"}, {\"attributeValues\": " +
756                         "\"testAttr2\"}, {\"attributeValues\": \"testAttr3\"}]}}";
757         dictionaryTestSetup(false, "Attribute", json);
758         // send Request to PAP
759         pap.service(httpServletRequest, httpServletResponse);
760         // Verify
761         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
762
763
764         httpServletRequest = Mockito.mock(HttpServletRequest.class);
765         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
766         json =
767                 "{\"dictionaryFields\":{\"attributeName\":\"TestMMrestAPI1\",\"type\":\"REST\",\"url\":\"testsomeurl" +
768                         ".com\",\"method\":\"GET\",\"description\":\"test create\",\"body\":\"Testing Create\"," +
769                         "\"headers\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
770                         "\"number\":\"test\"}]}}";
771         dictionaryTestSetup(false, "Action", json);
772         // send Request to PAP
773         pap.service(httpServletRequest, httpServletResponse);
774         // Verify
775         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
776
777         httpServletRequest = Mockito.mock(HttpServletRequest.class);
778         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
779         json =
780                 "{\"dictionaryFields\":{\"scopeName\":\"testMMRestAPI1\",\"description\":\"test\"," +
781                         "\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
782                         "\"number\":\"test\"}]}}";
783         dictionaryTestSetup(false, "DescriptiveScope", json);
784         // send Request to PAP
785         pap.service(httpServletRequest, httpServletResponse);
786         // Verify
787         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
788
789         httpServletRequest = Mockito.mock(HttpServletRequest.class);
790         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
791         json = "{\"dictionaryFields\":{\"riskName\":\"testMMrestAPI1\",\"description\":\"test\"}}";
792         dictionaryTestSetup(false, "RiskType", json);
793         // send Request to PAP
794         pap.service(httpServletRequest, httpServletResponse);
795         // Verify
796         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
797
798         httpServletRequest = Mockito.mock(HttpServletRequest.class);
799         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
800         json =
801                 "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"message\":\"test\"," +
802                         "\"riskType\":\"testMMrestAPI1\"}}";
803         dictionaryTestSetup(false, "SafePolicyWarning", json);
804         // send Request to PAP
805         pap.service(httpServletRequest, httpServletResponse);
806         // Verify
807         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
808     }
809
810     @Test
811     public void testDecisionCreateDictionary() throws IOException, SQLException, ServletException {
812         new DecisionPolicyDictionaryController(commonClassDao);
813         httpServletRequest = Mockito.mock(HttpServletRequest.class);
814         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
815         String json =
816                 "{\"dictionaryFields\":{\"xacmlId\":\"testMMRestAPI1\",\"datatypeBean\":{\"shortName\":\"string\"}," +
817                         "\"description\":\"test\",\"priority\":\"High\"}}";
818         dictionaryTestSetup(false, "Settings", json);
819         // send Request to PAP
820         pap.service(httpServletRequest, httpServletResponse);
821         // Verify
822         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
823
824         httpServletRequest = Mockito.mock(HttpServletRequest.class);
825         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
826         json =
827                 "{\"dictionaryFields\":{\"bbid\":\"BB1\",\"workstep\":\"1\",\"treatments\":\"Manual Handling,Abort," +
828                         "Retry\"}}";
829         dictionaryTestSetup(false, "RainyDayTreatments", json);
830         // send Request to PAP
831         pap.service(httpServletRequest, httpServletResponse);
832         // Verify
833         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
834     }
835
836     @Test
837     public void testMSCreateDictionary() throws IOException, SQLException, ServletException {
838         new MicroServiceDictionaryController(commonClassDao);
839         new PolicyScopeDictionaryController(commonClassDao);
840         httpServletRequest = Mockito.mock(HttpServletRequest.class);
841         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
842         String json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
843         dictionaryTestSetup(false, "MicroServiceLocation", json);
844         // send Request to PAP
845         pap.service(httpServletRequest, httpServletResponse);
846         // Verify
847         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
848
849         httpServletRequest = Mockito.mock(HttpServletRequest.class);
850         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
851         json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
852         dictionaryTestSetup(false, "MicroServiceConfigName", json);
853         // send Request to PAP
854         pap.service(httpServletRequest, httpServletResponse);
855         // Verify
856         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
857
858         httpServletRequest = Mockito.mock(HttpServletRequest.class);
859         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
860         json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"description\":\"test\"}}";
861         dictionaryTestSetup(false, "DCAEUUID", json);
862         // send Request to PAP
863         pap.service(httpServletRequest, httpServletResponse);
864         // Verify
865         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
866
867         httpServletRequest = Mockito.mock(HttpServletRequest.class);
868         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
869         json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
870         dictionaryTestSetup(false, "PolicyScopeService", json);
871         // send Request to PAP
872         pap.service(httpServletRequest, httpServletResponse);
873         // Verify
874         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
875
876         httpServletRequest = Mockito.mock(HttpServletRequest.class);
877         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
878         json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
879         dictionaryTestSetup(false, "PolicyScopeResource", json);
880         // send Request to PAP
881         pap.service(httpServletRequest, httpServletResponse);
882         // Verify
883         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
884
885         httpServletRequest = Mockito.mock(HttpServletRequest.class);
886         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
887         json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
888         dictionaryTestSetup(false, "PolicyScopeType", json);
889         // send Request to PAP
890         pap.service(httpServletRequest, httpServletResponse);
891         // Verify
892         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
893
894         httpServletRequest = Mockito.mock(HttpServletRequest.class);
895         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
896         json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
897         dictionaryTestSetup(false, "PolicyScopeClosedLoop", json);
898         // send Request to PAP
899         pap.service(httpServletRequest, httpServletResponse);
900         // Verify
901         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
902
903         httpServletRequest = Mockito.mock(HttpServletRequest.class);
904         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
905         json =
906                 "{\"dictionaryFields\":{\"groupName\":\"testMMrestAPI1\",\"description\":\"testing\"}," +
907                         "\"groupPolicyScopeListData1\":{\"resource\":\"ANY\",\"type\":\"ANY\",\"service\":\"ANY\"," +
908                         "\"closedloop\":\"ANY\"}}";
909         dictionaryTestSetup(false, "GroupPolicyScopeList", json);
910         // send Request to PAP
911         pap.service(httpServletRequest, httpServletResponse);
912         // Verify
913         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
914     }
915
916     private void dictionaryTestSetup(Boolean updateFlag, String dictionaryType, String json)
917             throws IOException, SQLException {
918         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
919         Mockito.when(httpServletRequest.getHeader("ClientScope")).thenReturn("dictionaryItem");
920         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
921         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
922         if (updateFlag) {
923             Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("update");
924         } else {
925             Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
926         }
927         Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionaryType);
928         MockServletInputStream mockInput = new MockServletInputStream(json.getBytes());
929         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
930         Mockito.when(httpServletRequest.getReader()).thenReturn(new BufferedReader(new InputStreamReader(mockInput)));
931         // set DBDao
932         setDBDao();
933     }
934
935     public void setDBDao() throws SQLException {
936         BasicDataSource dataSource = new BasicDataSource();
937         dataSource.setDriverClassName("org.h2.Driver");
938         // In-memory DB for testing
939         dataSource.setUrl("jdbc:h2:mem:test");
940         dataSource.setUsername("sa");
941         dataSource.setPassword("");
942         LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
943         sessionBuilder.scanPackages("org.onap.*", "com.*");
944
945         Properties properties = new Properties();
946         properties.put("hibernate.show_sql", "false");
947         properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
948         properties.put("hibernate.hbm2ddl.auto", "drop");
949         properties.put("hibernate.hbm2ddl.auto", "create");
950
951         sessionBuilder.addProperties(properties);
952         sessionFactory = sessionBuilder.buildSessionFactory();
953
954         // Set up dao with SessionFactory
955         CommonClassDaoImpl.setSessionfactory(sessionFactory);
956         PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
957     }
958
959     @Test
960     public void getDictionary() throws ServletException, IOException {
961         String[] dictionarys = new String[]{"Attribute", "OnapName", "Action", "BRMSParamTemplate", "VSCLAction"
962                 , "VNFType", "PEPOptions", "Varbind", "Service", "Site", "Settings", "RainyDayTreatments",
963                 "DescriptiveScope", "ActionList", "ProtocolList", "Zone", "SecurityZone",
964                 "PrefixList", "AddressGroup", "ServiceGroup", "ServiceList", "TermList",
965                 "MicroServiceLocation", "MicroServiceConfigName", "DCAEUUID", "MicroServiceModels",
966                 "PolicyScopeService", "PolicyScopeResource", "PolicyScopeType", "PolicyScopeClosedLoop",
967                 "GroupPolicyScopeList", "RiskType", "SafePolicyWarning", "MicroServiceDictionary"};
968         for (String dictionary : dictionarys) {
969             httpServletRequest = Mockito.mock(HttpServletRequest.class);
970             httpServletResponse = new MockHttpServletResponse();
971             Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
972             Mockito.when(httpServletRequest.getMethod()).thenReturn("GET");
973             Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
974             Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionary);
975             pap.service(httpServletRequest, httpServletResponse);
976             assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
977         }
978     }
979
980     @Test
981     public void testDummy() throws ServletException, IOException {
982
983         Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
984         mockOutput = Mockito.mock(ServletOutputStream.class);
985
986         try {
987             Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
988         } catch (IOException e) {
989             fail();
990         }
991
992         try {
993             pap.service(httpServletRequest, httpServletResponse);
994             assertTrue(true);
995         } catch (Exception e) {
996             fail();
997         }
998     }
999
1000     @After
1001     public void destroy() {
1002         if (sessionFactory != null) {
1003             sessionFactory.close();
1004         }
1005         pap.destroy();
1006     }
1007 }