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