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