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