New min/max Guard 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 testDecisonGuardMinMaxPolicy() 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", "scaleOut");
423         matchingAttributes.put("targets", "test,test1");
424         matchingAttributes.put("clname", "test");
425         matchingAttributes.put("min", "1");
426         matchingAttributes.put("max", "5");
427         matchingAttributes.put("guardActiveStart", "05:00");
428         matchingAttributes.put("guardActiveEnd", "10:00");
429         StdPAPPolicy newPAPPolicy =
430
431                 new StdPAPPolicy(
432                         StdPAPPolicyParams.builder().policyName("testGuard").description("test rule").onapName("PDPD")
433                                 .providerComboBox("GUARD_MIN_MAX").dynamicFieldConfigAttributes(matchingAttributes)
434                                 .editPolicy(false).domain("test").highestVersion(0).build());
435         MockServletInputStream mockInput = new MockServletInputStream(
436                 PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
437         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
438
439         // set DBDao
440         setDBDao();
441         pap.service(httpServletRequest, httpServletResponse);
442
443         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
444         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
445         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testGuard.1.xml");
446     }
447
448
449     @Test
450     public void testDecisonBLGuardPolicy() throws IOException, ServletException, SQLException {
451         httpServletRequest = Mockito.mock(HttpServletRequest.class);
452         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
453         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
454         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
455         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
456         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
457         Map<String, String> matchingAttributes = new HashMap<>();
458         matchingAttributes.put("actor", "test");
459         matchingAttributes.put("recipe", "restart");
460         matchingAttributes.put("clname", "test");
461         matchingAttributes.put("guardActiveStart", "05:00");
462         matchingAttributes.put("guardActiveEnd", "10:00");
463         matchingAttributes.put("blackList", "bl1,bl2");
464         StdPAPPolicy newPAPPolicy =
465                 new StdPAPPolicy(StdPAPPolicyParams.builder()
466                         .policyName("testblGuard")
467                         .description("test rule")
468                         .onapName("PDPD")
469                         .providerComboBox("GUARD_BL_YAML")
470                         .dynamicFieldConfigAttributes(matchingAttributes)
471                         .editPolicy(false)
472                         .domain("test")
473                         .highestVersion(0)
474                         .build());
475         MockServletInputStream mockInput =
476                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
477         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
478
479         // set DBDao
480         setDBDao();
481         pap.service(httpServletRequest, httpServletResponse);
482
483         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
484         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
485         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testblGuard.1.xml");
486     }
487
488     @Test
489     public void testConfigPolicy() throws IOException, ServletException, SQLException {
490         httpServletRequest = Mockito.mock(HttpServletRequest.class);
491         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
492         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
493         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
494         Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
495         Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
496         Map<String, String> configAttributes = new HashMap<>();
497         configAttributes.put("value", "test");
498         StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
499                 .configPolicyType("Base")
500                 .policyName("test")
501                 .description("test rule")
502                 .onapName("TEST")
503                 .configName("config")
504                 .dynamicFieldConfigAttributes(configAttributes)
505                 .configType("OTHER")
506                 .configBodyData("test body")
507                 .editPolicy(false)
508                 .domain("test")
509                 .highestVersion(0)
510                 .riskLevel("5")
511                 .riskType("default")
512                 .guard("false")
513                 .ttlDate(null).build());
514         MockServletInputStream mockInput =
515                 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
516         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
517
518         // set DBDao
519         setDBDao();
520         pap.service(httpServletRequest, httpServletResponse);
521
522         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
523         Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
524         Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_test.1.xml");
525     }
526
527     private void setPolicyCreation() {
528         CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class);
529         PolicyCreation.setCommonClassDao(commonClassDao);
530         PolicyEditorScopes editorScope = new PolicyEditorScopes();
531         UserInfo userInfo = new UserInfo();
532         userInfo.setUserName("API");
533         userInfo.setUserLoginId("API");
534         editorScope.setScopeName("test");
535         editorScope.setUserCreatedBy(userInfo);
536         editorScope.setUserModifiedBy(userInfo);
537         Mockito.when(commonClassDao.getEntityItem(PolicyEditorScopes.class, "scopeName", "test"))
538                 .thenReturn(editorScope);
539         BRMSParamTemplate template = new BRMSParamTemplate();
540         template.setRuleName("testPolicy");
541         template.setUserCreatedBy(userInfo);
542         String rule = "package com.sample;\n"
543                 + "import com.sample.DroolsTest.Message;\n"
544                 + "declare Params\n"
545                 + "samPoll : int\n"
546                 + "value : String\n"
547                 + "end\n"
548                 + "///This Rule will be generated by the UI.\n"
549                 + "rule \"${policyName}.Create parameters structure\"\n"
550                 + "salience 1000  \n"
551                 + "when\n"
552                 + "then\n"
553                 + "Params params = new Params();\n"
554                 + "params.setSamPoll(76);\n"
555                 + "params.setValue(\"test\");\n"
556                 + "insertLogical(params);\n"
557                 + "end\n"
558                 + "rule \"Rule 1: Check parameter structure access from when/then\"\n"
559                 + "when\n"
560                 + "$param: Params()\n"
561                 + "Params($param.samPoll > 50)\n"
562                 + "then\n"
563                 + "System.out.println(\"Firing rule 1\");\n"
564                 + "System.out.println($param);\n"
565                 + "end\n";
566         template.setRule(rule);
567         Mockito.when(commonClassDao.getEntityItem(BRMSParamTemplate.class, "ruleName", "testPolicy"))
568                 .thenReturn(template);
569
570     }
571
572     @Test
573     public void testClosedLoopCreateDictionary() throws IOException, SQLException, ServletException {
574         httpServletRequest = Mockito.mock(HttpServletRequest.class);
575         // Check VSCLAction. 
576         String json = "{\"dictionaryFields\": {\"vsclaction\": \"testRestAPI\",\"description\": \"testing create\"}}";
577         dictionaryTestSetup(false, "VSCLAction", json);
578         // set DBDao
579         ClosedLoopDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
580         // send Request to PAP
581         pap.service(httpServletRequest, httpServletResponse);
582         // Verify 
583         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
584         //
585         // Check VNFType
586         //
587         httpServletRequest = Mockito.mock(HttpServletRequest.class);
588         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
589         json = "{\"dictionaryFields\": {\"vnftype\": \"testrestAPI1\",\"description\": \"testing create\"}}";
590         dictionaryTestSetup(false, "VNFType", json);
591         // send Request to PAP
592         pap.service(httpServletRequest, httpServletResponse);
593         // Verify 
594         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
595         //
596         // Check PEPOptions
597         //
598         httpServletRequest = Mockito.mock(HttpServletRequest.class);
599         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
600         json =
601                 "{\"dictionaryFields\":{\"pepName\":\"testRestAPI\",\"description\":\"testing create\"," +
602                         "\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
603                         "\"number\":\"test\"}]}}";
604         dictionaryTestSetup(false, "PEPOptions", json);
605         // send Request to PAP
606         pap.service(httpServletRequest, httpServletResponse);
607         // Verify 
608         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
609         //
610         // Check Varbind
611         //
612         httpServletRequest = Mockito.mock(HttpServletRequest.class);
613         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
614         json =
615                 "{\"dictionaryFields\":{\"varbindName\":\"testRestAPI\",\"varbindDescription\":\"testing\"," +
616                         "\"varbindOID\":\"test\"}}";
617         dictionaryTestSetup(false, "Varbind", json);
618         // send Request to PAP
619         pap.service(httpServletRequest, httpServletResponse);
620         // Verify 
621         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
622         //
623         // Check Service
624         //
625         httpServletRequest = Mockito.mock(HttpServletRequest.class);
626         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
627         json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPI\",\"description\":\"testing\"}}";
628         dictionaryTestSetup(false, "Service", json);
629         // send Request to PAP
630         pap.service(httpServletRequest, httpServletResponse);
631         // Verify 
632         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
633         //
634         // Check Site
635         //
636         httpServletRequest = Mockito.mock(HttpServletRequest.class);
637         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
638         json = "{\"dictionaryFields\":{\"siteName\":\"testRestAPI\",\"description\":\"testing\"}}";
639         dictionaryTestSetup(false, "Site", json);
640         // send Request to PAP
641         pap.service(httpServletRequest, httpServletResponse);
642         // Verify 
643         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
644     }
645
646     @Test
647     public void testFirewallCreateDictionary() throws IOException, SQLException, ServletException {
648         httpServletRequest = Mockito.mock(HttpServletRequest.class);
649         // Check SecurityZone. 
650         String json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"testing\"}}";
651         dictionaryTestSetup(false, "SecurityZone", json);
652         // set DBDao
653         FirewallDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
654         // send Request to PAP
655         pap.service(httpServletRequest, httpServletResponse);
656         // Verify 
657         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
658         //
659         // Check Action List
660         //
661         httpServletRequest = Mockito.mock(HttpServletRequest.class);
662         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
663         json = "{\"dictionaryFields\":{\"actionName\":\"testRestAPI\",\"description\":\"test\"}}";
664         dictionaryTestSetup(false, "ActionList", json);
665         // send Request to PAP
666         pap.service(httpServletRequest, httpServletResponse);
667         // Verify 
668         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
669         //
670         // Check Protocol List. 
671         //
672         httpServletRequest = Mockito.mock(HttpServletRequest.class);
673         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
674         json = "{\"dictionaryFields\":{\"protocolName\":\"testRestAPI\",\"description\":\"test\"}}";
675         dictionaryTestSetup(false, "ProtocolList", json);
676         // send Request to PAP
677         pap.service(httpServletRequest, httpServletResponse);
678         // Verify 
679         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
680         //
681         // Check Zone. 
682         //
683         httpServletRequest = Mockito.mock(HttpServletRequest.class);
684         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
685         json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"test\"}}";
686         dictionaryTestSetup(false, "Zone", json);
687         // send Request to PAP
688         pap.service(httpServletRequest, httpServletResponse);
689         // Verify 
690         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
691         //
692         // Check PrefixList. 
693         //
694         httpServletRequest = Mockito.mock(HttpServletRequest.class);
695         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
696         json =
697                 "{\"dictionaryFields\":{\"prefixListName\":\"testRestAPI\",\"prefixListValue\":\"127.0.0.1\"," +
698                         "\"description\":\"testing\"}}";
699         dictionaryTestSetup(false, "PrefixList", json);
700         // send Request to PAP
701         pap.service(httpServletRequest, httpServletResponse);
702         // Verify 
703         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
704         //
705         // Check AddressGroup. 
706         //
707         httpServletRequest = Mockito.mock(HttpServletRequest.class);
708         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
709         json =
710                 "{\"dictionaryFields\":{\"groupName\":\"testRestAPIgroup\",\"description\":\"testing\"," +
711                         "\"attributes\":[{\"option\":\"testRestAPI\"}, {\"option\":\"testRestAPI\"}]}}";
712         dictionaryTestSetup(false, "AddressGroup", json);
713         // send Request to PAP
714         pap.service(httpServletRequest, httpServletResponse);
715         // Verify 
716         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
717         //
718         // Check ServiceGroup. 
719         //
720         httpServletRequest = Mockito.mock(HttpServletRequest.class);
721         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
722         json =
723                 "{\"dictionaryFields\":{\"groupName\":\"testRestAPIServiceGroup\"," +
724                         "\"attributes\":[{\"option\":\"testRestAPIservice\"}]}}";
725         dictionaryTestSetup(false, "ServiceGroup", json);
726         // send Request to PAP
727         pap.service(httpServletRequest, httpServletResponse);
728         // Verify 
729         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
730         //
731         // Check ServiceList. 
732         //
733         httpServletRequest = Mockito.mock(HttpServletRequest.class);
734         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
735         json =
736                 "{\"dictionaryFields\":{\"serviceName\":\"testRestAPIservice\",\"serviceDescription\":\"test\"," +
737                         "\"servicePorts\":\"8888\",\"transportProtocols\":[{\"option\":\"testRestAPI\"}," +
738                         "{\"option\":\"testRestAPI1\"}],\"appProtocols\":[{\"option\":\"testRestAPI\"}," +
739                         "{\"option\":\"testRestAPI1\"}]}}";
740         dictionaryTestSetup(false, "ServiceList", json);
741         // send Request to PAP
742         pap.service(httpServletRequest, httpServletResponse);
743         // Verify 
744         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
745         //
746         // Check TermList. 
747         //
748         httpServletRequest = Mockito.mock(HttpServletRequest.class);
749         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
750         json =
751                 "{\"dictionaryFields\":{\"termName\":\"testRestAPIRule\",\"termDescription\":\"testing\"," +
752                         "\"fromZoneDatas\":[{\"option\":\"testRestAPI\"}]," +
753                         "\"toZoneDatas\":[{\"option\":\"testRestAPI1\"}]," +
754                         "\"sourceListDatas\":[{\"option\":\"Group_testportal\"}]," +
755                         "\"destinationListDatas\":[{\"option\":\"testRestAPI\"}]," +
756                         "\"sourceServiceDatas\":[{\"option\":\"testRestAPIservice\"}," +
757                         "{\"option\":\"testRestAPIservice1\"}]," +
758                         "\"destinationServiceDatas\":[{\"option\":\"testRestAPIservice1\"}," +
759                         "{\"option\":\"testportalservice2\"}],\"actionListDatas\":[{\"option\":\"testRestAPI\"}]}}";
760         dictionaryTestSetup(false, "TermList", json);
761         // send Request to PAP
762         pap.service(httpServletRequest, httpServletResponse);
763         // Verify 
764         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
765     }
766
767     @Test
768     public void testCommonCreateDictionary() throws IOException, SQLException, ServletException {
769         new DictionaryController(commonClassDao);
770         new ActionPolicyDictionaryController(commonClassDao);
771         new SafePolicyController(commonClassDao);
772         new DescriptiveDictionaryController(commonClassDao);
773         List<Object> object = new ArrayList<>();
774         object.add(new Category());
775         when(commonClassDao.getDataById(Category.class, "shortName", "resource")).thenReturn(object);
776         httpServletRequest = Mockito.mock(HttpServletRequest.class);
777         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
778         String json =
779                 "{\"dictionaryFields\": {\"onapName\": \"testMMRestAPI1\",\"description\": \"testing update response " +
780                         "message\"}}";
781         dictionaryTestSetup(false, "OnapName", json);
782         // send Request to PAP
783         pap.service(httpServletRequest, httpServletResponse);
784         // Verify
785         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
786
787         httpServletRequest = Mockito.mock(HttpServletRequest.class);
788         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
789         json =
790                 "{\"dictionaryFields\": {\"xacmlId\": \"testMMRestAPI1\",\"datatypeBean\": {\"shortName\": " +
791                         "\"string\"}, \"description\": \"testing update\",\"priority\": \"High\"," +
792                         "\"userDataTypeValues\": [{\"attributeValues\": \"testAttr\"}, {\"attributeValues\": " +
793                         "\"testAttr2\"}, {\"attributeValues\": \"testAttr3\"}]}}";
794         dictionaryTestSetup(false, "Attribute", json);
795         // send Request to PAP
796         pap.service(httpServletRequest, httpServletResponse);
797         // Verify
798         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
799
800
801         httpServletRequest = Mockito.mock(HttpServletRequest.class);
802         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
803         json =
804                 "{\"dictionaryFields\":{\"attributeName\":\"TestMMrestAPI1\",\"type\":\"REST\",\"url\":\"testsomeurl" +
805                         ".com\",\"method\":\"GET\",\"description\":\"test create\",\"body\":\"Testing Create\"," +
806                         "\"headers\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
807                         "\"number\":\"test\"}]}}";
808         dictionaryTestSetup(false, "Action", json);
809         // send Request to PAP
810         pap.service(httpServletRequest, httpServletResponse);
811         // Verify
812         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
813
814         httpServletRequest = Mockito.mock(HttpServletRequest.class);
815         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
816         json =
817                 "{\"dictionaryFields\":{\"scopeName\":\"testMMRestAPI1\",\"description\":\"test\"," +
818                         "\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
819                         "\"number\":\"test\"}]}}";
820         dictionaryTestSetup(false, "DescriptiveScope", json);
821         // send Request to PAP
822         pap.service(httpServletRequest, httpServletResponse);
823         // Verify
824         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
825
826         httpServletRequest = Mockito.mock(HttpServletRequest.class);
827         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
828         json = "{\"dictionaryFields\":{\"riskName\":\"testMMrestAPI1\",\"description\":\"test\"}}";
829         dictionaryTestSetup(false, "RiskType", json);
830         // send Request to PAP
831         pap.service(httpServletRequest, httpServletResponse);
832         // Verify
833         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
834
835         httpServletRequest = Mockito.mock(HttpServletRequest.class);
836         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
837         json =
838                 "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"message\":\"test\"," +
839                         "\"riskType\":\"testMMrestAPI1\"}}";
840         dictionaryTestSetup(false, "SafePolicyWarning", json);
841         // send Request to PAP
842         pap.service(httpServletRequest, httpServletResponse);
843         // Verify
844         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
845     }
846
847     @Test
848     public void testDecisionCreateDictionary() throws IOException, SQLException, ServletException {
849         new DecisionPolicyDictionaryController(commonClassDao);
850         httpServletRequest = Mockito.mock(HttpServletRequest.class);
851         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
852         String json =
853                 "{\"dictionaryFields\":{\"xacmlId\":\"testMMRestAPI1\",\"datatypeBean\":{\"shortName\":\"string\"}," +
854                         "\"description\":\"test\",\"priority\":\"High\"}}";
855         dictionaryTestSetup(false, "Settings", json);
856         // send Request to PAP
857         pap.service(httpServletRequest, httpServletResponse);
858         // Verify
859         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
860
861         httpServletRequest = Mockito.mock(HttpServletRequest.class);
862         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
863         json =
864                 "{\"dictionaryFields\":{\"bbid\":\"BB1\",\"workstep\":\"1\",\"treatments\":\"Manual Handling,Abort," +
865                         "Retry\"}}";
866         dictionaryTestSetup(false, "RainyDayTreatments", json);
867         // send Request to PAP
868         pap.service(httpServletRequest, httpServletResponse);
869         // Verify
870         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
871     }
872
873     @Test
874     public void testMSCreateDictionary() throws IOException, SQLException, ServletException {
875         new MicroServiceDictionaryController(commonClassDao);
876         new PolicyScopeDictionaryController(commonClassDao);
877         httpServletRequest = Mockito.mock(HttpServletRequest.class);
878         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
879         String json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
880         dictionaryTestSetup(false, "MicroServiceLocation", json);
881         // send Request to PAP
882         pap.service(httpServletRequest, httpServletResponse);
883         // Verify
884         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
885
886         httpServletRequest = Mockito.mock(HttpServletRequest.class);
887         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
888         json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
889         dictionaryTestSetup(false, "MicroServiceConfigName", json);
890         // send Request to PAP
891         pap.service(httpServletRequest, httpServletResponse);
892         // Verify
893         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
894
895         httpServletRequest = Mockito.mock(HttpServletRequest.class);
896         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
897         json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"description\":\"test\"}}";
898         dictionaryTestSetup(false, "DCAEUUID", json);
899         // send Request to PAP
900         pap.service(httpServletRequest, httpServletResponse);
901         // Verify
902         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
903
904         httpServletRequest = Mockito.mock(HttpServletRequest.class);
905         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
906         json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
907         dictionaryTestSetup(false, "PolicyScopeService", json);
908         // send Request to PAP
909         pap.service(httpServletRequest, httpServletResponse);
910         // Verify
911         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
912
913         httpServletRequest = Mockito.mock(HttpServletRequest.class);
914         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
915         json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
916         dictionaryTestSetup(false, "PolicyScopeResource", json);
917         // send Request to PAP
918         pap.service(httpServletRequest, httpServletResponse);
919         // Verify
920         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
921
922         httpServletRequest = Mockito.mock(HttpServletRequest.class);
923         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
924         json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
925         dictionaryTestSetup(false, "PolicyScopeType", json);
926         // send Request to PAP
927         pap.service(httpServletRequest, httpServletResponse);
928         // Verify
929         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
930
931         httpServletRequest = Mockito.mock(HttpServletRequest.class);
932         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
933         json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
934         dictionaryTestSetup(false, "PolicyScopeClosedLoop", json);
935         // send Request to PAP
936         pap.service(httpServletRequest, httpServletResponse);
937         // Verify
938         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
939
940         httpServletRequest = Mockito.mock(HttpServletRequest.class);
941         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
942         json =
943                 "{\"dictionaryFields\":{\"groupName\":\"testMMrestAPI1\",\"description\":\"testing\"}," +
944                         "\"groupPolicyScopeListData1\":{\"resource\":\"ANY\",\"type\":\"ANY\",\"service\":\"ANY\"," +
945                         "\"closedloop\":\"ANY\"}}";
946         dictionaryTestSetup(false, "GroupPolicyScopeList", json);
947         // send Request to PAP
948         pap.service(httpServletRequest, httpServletResponse);
949         // Verify
950         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
951     }
952
953     private void dictionaryTestSetup(Boolean updateFlag, String dictionaryType, String json)
954             throws IOException, SQLException {
955         Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
956         Mockito.when(httpServletRequest.getHeader("ClientScope")).thenReturn("dictionaryItem");
957         Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
958         Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
959         if (updateFlag) {
960             Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("update");
961         } else {
962             Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
963         }
964         Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionaryType);
965         MockServletInputStream mockInput = new MockServletInputStream(json.getBytes());
966         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
967         Mockito.when(httpServletRequest.getReader()).thenReturn(new BufferedReader(new InputStreamReader(mockInput)));
968         // set DBDao
969         setDBDao();
970     }
971
972     public void setDBDao() throws SQLException {
973         BasicDataSource dataSource = new BasicDataSource();
974         dataSource.setDriverClassName("org.h2.Driver");
975         // In-memory DB for testing
976         dataSource.setUrl("jdbc:h2:mem:test");
977         dataSource.setUsername("sa");
978         dataSource.setPassword("");
979         LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
980         sessionBuilder.scanPackages("org.onap.*", "com.*");
981
982         Properties properties = new Properties();
983         properties.put("hibernate.show_sql", "false");
984         properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
985         properties.put("hibernate.hbm2ddl.auto", "drop");
986         properties.put("hibernate.hbm2ddl.auto", "create");
987
988         sessionBuilder.addProperties(properties);
989         sessionFactory = sessionBuilder.buildSessionFactory();
990
991         // Set up dao with SessionFactory
992         CommonClassDaoImpl.setSessionfactory(sessionFactory);
993         PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
994     }
995
996     @Test
997     public void getDictionary() throws ServletException, IOException {
998         String[] dictionarys = new String[]{"Attribute", "OnapName", "Action", "BRMSParamTemplate", "VSCLAction"
999                 , "VNFType", "PEPOptions", "Varbind", "Service", "Site", "Settings", "RainyDayTreatments",
1000                 "DescriptiveScope", "ActionList", "ProtocolList", "Zone", "SecurityZone",
1001                 "PrefixList", "AddressGroup", "ServiceGroup", "ServiceList", "TermList",
1002                 "MicroServiceLocation", "MicroServiceConfigName", "DCAEUUID", "MicroServiceModels",
1003                 "PolicyScopeService", "PolicyScopeResource", "PolicyScopeType", "PolicyScopeClosedLoop",
1004                 "GroupPolicyScopeList", "RiskType", "SafePolicyWarning", "MicroServiceDictionary"};
1005         for (String dictionary : dictionarys) {
1006             httpServletRequest = Mockito.mock(HttpServletRequest.class);
1007             httpServletResponse = new MockHttpServletResponse();
1008             Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
1009             Mockito.when(httpServletRequest.getMethod()).thenReturn("GET");
1010             Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
1011             Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionary);
1012             pap.service(httpServletRequest, httpServletResponse);
1013             assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
1014         }
1015     }
1016
1017     @Test
1018     public void testDummy() throws ServletException, IOException {
1019
1020         Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
1021         mockOutput = Mockito.mock(ServletOutputStream.class);
1022
1023         try {
1024             Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
1025         } catch (IOException e) {
1026             fail();
1027         }
1028
1029         try {
1030             pap.service(httpServletRequest, httpServletResponse);
1031             assertTrue(true);
1032         } catch (Exception e) {
1033             fail();
1034         }
1035     }
1036
1037     @After
1038     public void destroy() {
1039         if (sessionFactory != null) {
1040             sessionFactory.close();
1041         }
1042         pap.destroy();
1043     }
1044 }