2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.pap.test;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import static org.mockito.Mockito.when;
28 import com.mockrunner.mock.web.MockServletInputStream;
30 import java.io.BufferedReader;
31 import java.io.IOException;
32 import java.io.InputStreamReader;
33 import java.sql.SQLException;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.List;
40 import java.util.Properties;
42 import javax.servlet.ServletConfig;
43 import javax.servlet.ServletException;
44 import javax.servlet.ServletOutputStream;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
48 import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50 import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
51 import org.hibernate.SessionFactory;
52 import org.junit.AfterClass;
53 import org.junit.Before;
54 import org.junit.BeforeClass;
55 import org.junit.Test;
56 import org.mockito.Mockito;
57 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
58 import org.onap.policy.pap.xacml.rest.components.FirewallConfigPolicy;
59 import org.onap.policy.pap.xacml.rest.components.PolicyDBDaoTest;
60 import org.onap.policy.pap.xacml.rest.controller.ActionPolicyDictionaryController;
61 import org.onap.policy.pap.xacml.rest.controller.ClosedLoopDictionaryController;
62 import org.onap.policy.pap.xacml.rest.controller.DecisionPolicyDictionaryController;
63 import org.onap.policy.pap.xacml.rest.controller.DescriptiveDictionaryController;
64 import org.onap.policy.pap.xacml.rest.controller.DictionaryController;
65 import org.onap.policy.pap.xacml.rest.controller.FirewallDictionaryController;
66 import org.onap.policy.pap.xacml.rest.controller.MicroServiceDictionaryController;
67 import org.onap.policy.pap.xacml.rest.controller.PolicyScopeDictionaryController;
68 import org.onap.policy.pap.xacml.rest.controller.SafePolicyController;
69 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
70 import org.onap.policy.pap.xacml.rest.policycontroller.PolicyCreation;
71 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
72 import org.onap.policy.rest.dao.CommonClassDao;
73 import org.onap.policy.rest.jpa.ActionPolicyDict;
74 import org.onap.policy.rest.jpa.BrmsParamTemplate;
75 import org.onap.policy.rest.jpa.Category;
76 import org.onap.policy.rest.jpa.FunctionDefinition;
77 import org.onap.policy.rest.jpa.PolicyEditorScopes;
78 import org.onap.policy.rest.jpa.UserInfo;
79 import org.onap.policy.utils.PolicyUtils;
80 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
81 import org.onap.policy.xacml.std.pap.StdPAPPolicyParams;
82 import org.springframework.mock.web.MockHttpServletResponse;
83 import org.springframework.mock.web.MockServletConfig;
84 import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
86 public class XACMLPAPTest {
87 private static final Log logger = LogFactory.getLog(XACMLPAPTest.class);
89 private static final String ENVIRONMENT_HEADER = "Environment";
90 private static List<String> headers = new ArrayList<>();
91 private HttpServletRequest httpServletRequest;
92 private HttpServletResponse httpServletResponse;
93 private ServletOutputStream mockOutput;
94 private static ServletConfig servletConfig;
95 private static XACMLPapServlet pap;
96 private static SessionFactory sessionFactory;
97 private static CommonClassDao commonClassDao;
100 public static void beforeClassSetup() throws ServletException {
101 sessionFactory = PolicyDBDaoTest.setupH2DbDaoImpl("xacmlpaptest");
102 new FirewallConfigPolicy(new CommonClassDaoImpl());
103 new DictionaryUtils(new CommonClassDaoImpl());
107 public static void setUp() throws ServletException {
108 servletConfig = Mockito.mock(MockServletConfig.class);
109 System.setProperty("com.sun.management.jmxremote.port", "9993");
110 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
111 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
112 .thenReturn("src/test/resources/xacml.pap.properties");
113 pap = new XACMLPapServlet();
114 pap.init(servletConfig);
115 commonClassDao = Mockito.mock(CommonClassDao.class);
116 new DictionaryUtils(commonClassDao);
117 DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
118 UserInfo user = new UserInfo();
119 user.setUserLoginId("API");
120 user.setUserName("API");
121 Mockito.when(commonClassDao.getEntityItem(UserInfo.class, "userLoginId", "API")).thenReturn(user);
122 Mockito.mock(DictionaryUtils.class);
126 public void testInit() {
127 httpServletRequest = Mockito.mock(HttpServletRequest.class);
128 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
129 logger.info(httpServletResponse);
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());
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,"
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("")
163 MockServletInputStream mockInput =
164 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
165 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
166 logger.info(httpServletRequest);
167 logger.info(httpServletResponse);
168 pap.service(httpServletRequest, httpServletResponse);
169 Mockito.verify(httpServletResponse).addHeader("operation", "create");
173 public void testBRMSCreatePolicy() throws IOException, ServletException, SQLException {
174 httpServletRequest = Mockito.mock(HttpServletRequest.class);
175 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
176 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
177 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
178 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
179 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
180 Map<String, String> matchingAttributes = new HashMap<>();
181 Map<String, String> ruleAttributes = new HashMap<>();
182 ruleAttributes.put("templateName", "testPolicy");
183 ruleAttributes.put("samPoll", "5");
184 ruleAttributes.put("value", "test");
185 // Creating BRMS Param Policies from the Admin Console
186 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder().configPolicyType("BRMS_Param")
187 .policyName("test").description("testing").configName("BRMS_PARAM_RULE").editPolicy(false)
188 .domain("test").dynamicFieldConfigAttributes(matchingAttributes).highestVersion(0).onapName("DROOLS")
189 .configBodyData(null).drlRuleAndUIParams(ruleAttributes).riskLevel("5").riskType("default")
190 .guard("false").ttlDate("").brmsController(null).brmsDependency(null).build());
191 MockServletInputStream mockInput =
192 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
193 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
196 logger.info(httpServletRequest);
197 logger.info(httpServletResponse);
198 pap.service(httpServletRequest, httpServletResponse);
199 Mockito.verify(httpServletResponse).addHeader("operation", "create");
203 public void testBRMSRawCreatePolicy() throws IOException, ServletException, SQLException {
204 httpServletRequest = Mockito.mock(HttpServletRequest.class);
205 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
206 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
207 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
208 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
209 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
210 Map<String, String> ruleAttributes = new HashMap<>();
211 ruleAttributes.put("value", "test");
212 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder().configPolicyType("BRMS_Raw")
213 .policyName("test").description("testig description").configName("BRMS_RAW_RULE").editPolicy(false)
214 .domain("test").dynamicFieldConfigAttributes(ruleAttributes).highestVersion(0).onapName("DROOLS")
215 .configBodyData("test").riskLevel("4").riskType("default").guard("false").build());
216 MockServletInputStream mockInput =
217 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
218 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
221 logger.info(httpServletRequest);
222 logger.info(httpServletResponse);
223 pap.service(httpServletRequest, httpServletResponse);
224 Mockito.verify(httpServletResponse).addHeader("operation", "create");
228 public void testClosedLoopPMCreatePolicy() throws IOException, ServletException, SQLException {
229 httpServletRequest = Mockito.mock(HttpServletRequest.class);
230 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
231 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
232 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
233 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
234 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
235 String json = "{\"test\":\"java\"}";
236 // Creating CloseLoop_Fault and Performance Metric Policies
237 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder().configPolicyType("ClosedLoop_PM")
238 .policyName("test").description("testing").onapName("onap").jsonBody(json).draft(false)
239 .oldPolicyFileName(null).serviceType("Registration Failure(Trinity)").editPolicy(false).domain("test")
240 .highestVersion(0).riskLevel(null).riskType("default").guard("true").ttlDate("").build());
241 MockServletInputStream mockInput =
242 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
243 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
246 logger.info(httpServletRequest);
247 logger.info(httpServletResponse);
248 pap.service(httpServletRequest, httpServletResponse);
249 Mockito.verify(httpServletResponse).addHeader("operation", "create");
253 public void testDecisonAAFPolicy() throws IOException, ServletException, SQLException {
254 httpServletRequest = Mockito.mock(HttpServletRequest.class);
255 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
256 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
257 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
258 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
259 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
260 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(
261 StdPAPPolicyParams.builder().policyName("test").description("test rule").onapName("ONAP")
262 .providerComboBox("AAF").editPolicy(false).domain("test").highestVersion(0).build());
263 MockServletInputStream mockInput =
264 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
265 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
267 logger.info(httpServletRequest);
268 logger.info(httpServletResponse);
269 pap.service(httpServletRequest, httpServletResponse);
270 Mockito.verify(httpServletResponse).addHeader("operation", "create");
274 public void testDecisonGuardPolicy() throws IOException, ServletException, SQLException {
275 httpServletRequest = Mockito.mock(HttpServletRequest.class);
276 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
277 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
278 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
279 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
280 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
281 Map<String, String> matchingAttributes = new HashMap<>();
282 matchingAttributes.put("actor", "test");
283 matchingAttributes.put("recipe", "restart");
284 matchingAttributes.put("targets", "test,test1");
285 matchingAttributes.put("clname", "");
286 matchingAttributes.put("limit", "1");
287 matchingAttributes.put("timeWindow", "15");
288 matchingAttributes.put("timeUnits", "minute");
289 matchingAttributes.put("guardActiveStart", "05:00");
290 matchingAttributes.put("guardActiveEnd", "10:00");
291 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(
292 StdPAPPolicyParams.builder().policyName("testGuard").description("test rule").onapName("PDPD")
293 .providerComboBox("GUARD_YAML").dynamicFieldConfigAttributes(matchingAttributes)
294 .editPolicy(false).domain("test").highestVersion(0).build());
295 MockServletInputStream mockInput =
296 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
297 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
299 logger.info(httpServletRequest);
300 logger.info(httpServletResponse);
301 pap.service(httpServletRequest, httpServletResponse);
302 Mockito.verify(httpServletResponse).addHeader("operation", "create");
306 public void testDecisonGuardMinMaxPolicy() throws IOException, ServletException, SQLException {
307 httpServletRequest = Mockito.mock(HttpServletRequest.class);
308 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
309 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
310 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
311 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
312 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
313 Map<String, String> matchingAttributes = new HashMap<>();
314 matchingAttributes.put("actor", "test");
315 matchingAttributes.put("recipe", "scaleOut");
316 matchingAttributes.put("targets", "test,test1");
317 matchingAttributes.put("clname", "test");
318 matchingAttributes.put("min", "1");
319 matchingAttributes.put("max", "5");
320 matchingAttributes.put("guardActiveStart", "05:00");
321 matchingAttributes.put("guardActiveEnd", "10:00");
322 StdPAPPolicy newPAPPolicy =
324 new StdPAPPolicy(StdPAPPolicyParams.builder().policyName("testGuardMinMax").description("test rule")
325 .onapName("PDPD").providerComboBox("GUARD_MIN_MAX")
326 .dynamicFieldConfigAttributes(matchingAttributes).editPolicy(false).domain("test")
327 .highestVersion(0).build());
328 MockServletInputStream mockInput =
329 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
330 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
332 logger.info(httpServletRequest);
333 logger.info(httpServletResponse);
334 pap.service(httpServletRequest, httpServletResponse);
335 Mockito.verify(httpServletResponse).addHeader("operation", "create");
339 public void testDecisonBLGuardPolicy() throws IOException, ServletException, SQLException {
340 httpServletRequest = Mockito.mock(HttpServletRequest.class);
341 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
342 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
343 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
344 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
345 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
346 Map<String, String> matchingAttributes = new HashMap<>();
347 matchingAttributes.put("actor", "test");
348 matchingAttributes.put("recipe", "restart");
349 matchingAttributes.put("clname", "test");
350 matchingAttributes.put("guardActiveStart", "05:00");
351 matchingAttributes.put("guardActiveEnd", "10:00");
352 matchingAttributes.put("blackList", "bl1,bl2");
353 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(
354 StdPAPPolicyParams.builder().policyName("testblGuard").description("test rule").onapName("PDPD")
355 .providerComboBox("GUARD_BL_YAML").dynamicFieldConfigAttributes(matchingAttributes)
356 .editPolicy(false).domain("test").highestVersion(0).build());
357 MockServletInputStream mockInput =
358 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
359 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
361 logger.info(httpServletRequest);
362 logger.info(httpServletResponse);
363 pap.service(httpServletRequest, httpServletResponse);
364 Mockito.verify(httpServletResponse).addHeader("operation", "create");
368 public void testConfigPolicy() throws IOException, ServletException, SQLException {
369 httpServletRequest = Mockito.mock(HttpServletRequest.class);
370 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
371 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
372 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
373 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
374 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
375 Map<String, String> configAttributes = new HashMap<>();
376 configAttributes.put("value", "test");
377 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(
378 StdPAPPolicyParams.builder().configPolicyType("Base").policyName("test").description("test rule")
379 .onapName("TEST").configName("config").dynamicFieldConfigAttributes(configAttributes)
380 .configType("OTHER").configBodyData("test body").editPolicy(false).domain("test")
381 .highestVersion(0).riskLevel("5").riskType("default").guard("false").ttlDate(null).build());
382 MockServletInputStream mockInput =
383 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
384 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
386 logger.info(httpServletRequest);
387 logger.info(httpServletResponse);
388 pap.service(httpServletRequest, httpServletResponse);
389 Mockito.verify(httpServletResponse).addHeader("operation", "create");
393 public void testActionPolicy() throws IOException, ServletException, SQLException {
395 httpServletRequest = Mockito.mock(HttpServletRequest.class);
396 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
397 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
398 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("API");
399 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
400 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Action");
402 CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class);
403 PolicyCreation.setCommonClassDao(commonClassDao);
404 ActionPolicyDict actionDict = new ActionPolicyDict();
405 actionDict.setBody("{\"test\":\"test\"}");
406 actionDict.setHeader("test122=test12:test22=test34");
407 actionDict.setType("REST");
408 actionDict.setMethod("GET");
409 actionDict.setUrl("testsomeurl.com");
410 Mockito.when(commonClassDao.getEntityItem(ActionPolicyDict.class, "attributeName", "test"))
411 .thenReturn(actionDict);
412 FunctionDefinition funcDefn = new FunctionDefinition();
413 funcDefn.setXacmlid("urn:oasis:names:tc:xacml:1.0:function:and");
414 Mockito.when(commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", "and")).thenReturn(funcDefn);
415 funcDefn.setXacmlid("urn:oasis:names:tc:xacml:1.0:function:integer-equal");
416 Mockito.when(commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", "integer-equal"))
417 .thenReturn(funcDefn);
418 funcDefn.setXacmlid("urn:oasis:names:tc:xacml:3.0:function:string-contains");
419 Mockito.when(commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", "string-contains"))
420 .thenReturn(funcDefn);
421 funcDefn.setXacmlid("urn:oasis:names:tc:xacml:1.0:function:integer-greater-than");
422 Mockito.when(commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", "integer-greater-than"))
423 .thenReturn(funcDefn);
424 funcDefn.setXacmlid("urn:oasis:names:tc:xacml:1.0:function:or");
425 Mockito.when(commonClassDao.getEntityItem(FunctionDefinition.class, "short_name", "or")).thenReturn(funcDefn);
427 Map<String, String> componentAttributes = new HashMap<>();
428 componentAttributes.put("java", "test");
429 StdPAPPolicy newPapPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder().policyName("test").description("test")
430 .dynamicFieldConfigAttributes(componentAttributes)
431 .dynamicRuleAlgorithmLabels(Arrays.asList("A1", "A2", "A3", "A4", "A5", "A6", "A7"))
432 .dynamicRuleAlgorithmCombo(Arrays.asList("integer-equal", "string-contains", "integer-equal", "and",
433 "integer-greater-than", "or", "and"))
434 .dynamicRuleAlgorithmField1(Arrays.asList("cobal", "cap", "cobal", "A2", "Config", "A4", "A1"))
435 .dynamicRuleAlgorithmField2(Arrays.asList("90", "ca", "90", "A3", "45", "A5", "A6"))
436 .actionPerformer("PDP").actionAttribute("test").editPolicy(false).domain("com").highestVersion(0)
438 newPapPolicy.setActionBody("{\"test\":\"test\"}");
440 MockServletInputStream mockInput =
441 new MockServletInputStream(PolicyUtils.objectToJsonString(newPapPolicy).getBytes());
442 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
444 logger.info(httpServletRequest);
445 logger.info(httpServletResponse);
446 pap.service(httpServletRequest, httpServletResponse);
447 Mockito.verify(httpServletResponse).addHeader("operation", "create");
450 private void setPolicyCreation() {
451 CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class);
452 PolicyCreation.setCommonClassDao(commonClassDao);
453 PolicyEditorScopes editorScope = new PolicyEditorScopes();
454 UserInfo userInfo = new UserInfo();
455 userInfo.setUserName("API");
456 userInfo.setUserLoginId("API");
457 editorScope.setScopeName("test");
458 editorScope.setUserCreatedBy(userInfo);
459 editorScope.setUserModifiedBy(userInfo);
460 Mockito.when(commonClassDao.getEntityItem(PolicyEditorScopes.class, "scopeName", "test"))
461 .thenReturn(editorScope);
462 BrmsParamTemplate template = new BrmsParamTemplate();
463 template.setRuleName("testPolicy");
464 template.setUserCreatedBy(userInfo);
465 String rule = "package com.sample;\n" + "import com.sample.DroolsTest.Message;\n" + "declare Params\n"
466 + "samPoll : int\n" + "value : String\n" + "end\n" + "///This Rule will be generated by the UI.\n"
467 + "rule \"${policyName}.Create parameters structure\"\n" + "salience 1000 \n" + "when\n" + "then\n"
468 + "Params params = new Params();\n" + "params.setSamPoll(76);\n" + "params.setValue(\"test\");\n"
469 + "insertLogical(params);\n" + "end\n"
470 + "rule \"Rule 1: Check parameter structure access from when/then\"\n" + "when\n" + "$param: Params()\n"
471 + "Params($param.samPoll > 50)\n" + "then\n" + "System.out.println(\"Firing rule 1\");\n"
472 + "System.out.println($param);\n" + "end\n";
473 template.setRule(rule);
474 Mockito.when(commonClassDao.getEntityItem(BrmsParamTemplate.class, "ruleName", "testPolicy"))
475 .thenReturn(template);
479 public void testClosedLoopCreateDictionary() throws IOException, SQLException, ServletException {
480 httpServletRequest = Mockito.mock(HttpServletRequest.class);
482 String json = "{\"dictionaryFields\": {\"vsclaction\": \"testRestAPI\",\"description\": \"testing create\"}}";
483 dictionaryTestSetup(false, "VSCLAction", json);
485 ClosedLoopDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
486 // send Request to PAP
487 pap.service(httpServletRequest, httpServletResponse);
489 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
493 httpServletRequest = Mockito.mock(HttpServletRequest.class);
494 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
495 json = "{\"dictionaryFields\": {\"vnftype\": \"testrestAPI1\",\"description\": \"testing create\"}}";
496 dictionaryTestSetup(false, "VNFType", json);
497 // send Request to PAP
498 pap.service(httpServletRequest, httpServletResponse);
500 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
504 httpServletRequest = Mockito.mock(HttpServletRequest.class);
505 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
506 json = "{\"dictionaryFields\":{\"pepName\":\"testRestAPI\",\"description\":\"testing create\","
507 + "\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\","
508 + "\"number\":\"test\"}]}}";
509 dictionaryTestSetup(false, "PepOptions", json);
510 // send Request to PAP
511 pap.service(httpServletRequest, httpServletResponse);
513 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
517 httpServletRequest = Mockito.mock(HttpServletRequest.class);
518 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
519 json = "{\"dictionaryFields\":{\"varbindName\":\"testRestAPI\",\"varbindDescription\":\"testing\","
520 + "\"varbindOID\":\"test\"}}";
521 dictionaryTestSetup(false, "Varbind", json);
522 // send Request to PAP
523 pap.service(httpServletRequest, httpServletResponse);
525 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
529 httpServletRequest = Mockito.mock(HttpServletRequest.class);
530 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
531 json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPI\",\"description\":\"testing\"}}";
532 dictionaryTestSetup(false, "Service", json);
533 // send Request to PAP
534 pap.service(httpServletRequest, httpServletResponse);
536 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
540 httpServletRequest = Mockito.mock(HttpServletRequest.class);
541 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
542 json = "{\"dictionaryFields\":{\"siteName\":\"testRestAPI\",\"description\":\"testing\"}}";
543 dictionaryTestSetup(false, "Site", json);
544 // send Request to PAP
545 pap.service(httpServletRequest, httpServletResponse);
547 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
551 public void testFirewallCreateDictionary() throws IOException, SQLException, ServletException {
552 httpServletRequest = Mockito.mock(HttpServletRequest.class);
553 // Check SecurityZone.
554 String json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"testing\"}}";
555 dictionaryTestSetup(false, "SecurityZone", json);
557 FirewallDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
558 // send Request to PAP
559 pap.service(httpServletRequest, httpServletResponse);
561 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
565 httpServletRequest = Mockito.mock(HttpServletRequest.class);
566 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
567 json = "{\"dictionaryFields\":{\"actionName\":\"testRestAPI\",\"description\":\"test\"}}";
568 dictionaryTestSetup(false, "ActionList", json);
569 // send Request to PAP
570 pap.service(httpServletRequest, httpServletResponse);
572 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
574 // Check Protocol List.
576 httpServletRequest = Mockito.mock(HttpServletRequest.class);
577 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
578 json = "{\"dictionaryFields\":{\"protocolName\":\"testRestAPI\",\"description\":\"test\"}}";
579 dictionaryTestSetup(false, "ProtocolList", json);
580 // send Request to PAP
581 pap.service(httpServletRequest, httpServletResponse);
583 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
587 httpServletRequest = Mockito.mock(HttpServletRequest.class);
588 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
589 json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"test\"}}";
590 dictionaryTestSetup(false, "Zone", json);
591 // send Request to PAP
592 pap.service(httpServletRequest, httpServletResponse);
594 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
598 httpServletRequest = Mockito.mock(HttpServletRequest.class);
599 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
600 json = "{\"dictionaryFields\":{\"prefixListName\":\"testRestAPI\",\"prefixListValue\":\"127.0.0.1\","
601 + "\"description\":\"testing\"}}";
602 dictionaryTestSetup(false, "PrefixList", json);
603 // send Request to PAP
604 pap.service(httpServletRequest, httpServletResponse);
606 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
608 // Check AddressGroup.
610 httpServletRequest = Mockito.mock(HttpServletRequest.class);
611 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
612 json = "{\"dictionaryFields\":{\"groupName\":\"testRestAPIgroup\",\"description\":\"testing\","
613 + "\"attributes\":[{\"option\":\"testRestAPI\"}, {\"option\":\"testRestAPI\"}]}}";
614 dictionaryTestSetup(false, "AddressGroup", json);
615 // send Request to PAP
616 pap.service(httpServletRequest, httpServletResponse);
618 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
620 // Check ServiceGroup.
622 httpServletRequest = Mockito.mock(HttpServletRequest.class);
623 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
624 json = "{\"dictionaryFields\":{\"groupName\":\"testRestAPIServiceGroup\","
625 + "\"attributes\":[{\"option\":\"testRestAPIservice\"}]}}";
626 dictionaryTestSetup(false, "ServiceGroup", json);
627 // send Request to PAP
628 pap.service(httpServletRequest, httpServletResponse);
630 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
632 // Check ServiceList.
634 httpServletRequest = Mockito.mock(HttpServletRequest.class);
635 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
636 json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPIservice\",\"serviceDescription\":\"test\","
637 + "\"servicePorts\":\"8888\",\"transportProtocols\":[{\"option\":\"testRestAPI\"},"
638 + "{\"option\":\"testRestAPI1\"}],\"appProtocols\":[{\"option\":\"testRestAPI\"},"
639 + "{\"option\":\"testRestAPI1\"}]}}";
640 dictionaryTestSetup(false, "ServiceList", json);
641 // send Request to PAP
642 pap.service(httpServletRequest, httpServletResponse);
644 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
648 httpServletRequest = Mockito.mock(HttpServletRequest.class);
649 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
650 json = "{\"dictionaryFields\":{\"termName\":\"testRestAPIRule\",\"termDescription\":\"testing\","
651 + "\"fromZoneDatas\":[{\"option\":\"testRestAPI\"}],"
652 + "\"toZoneDatas\":[{\"option\":\"testRestAPI1\"}],"
653 + "\"sourceListDatas\":[{\"option\":\"Group_testportal\"}],"
654 + "\"destinationListDatas\":[{\"option\":\"testRestAPI\"}],"
655 + "\"sourceServiceDatas\":[{\"option\":\"testRestAPIservice\"},"
656 + "{\"option\":\"testRestAPIservice1\"}],"
657 + "\"destinationServiceDatas\":[{\"option\":\"testRestAPIservice1\"},"
658 + "{\"option\":\"testportalservice2\"}],\"actionListDatas\":[{\"option\":\"testRestAPI\"}]}}";
659 dictionaryTestSetup(false, "TermList", json);
660 // send Request to PAP
661 pap.service(httpServletRequest, httpServletResponse);
663 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
667 public void testCommonCreateDictionary() throws IOException, SQLException, ServletException {
668 new DictionaryController(commonClassDao);
669 new ActionPolicyDictionaryController(commonClassDao);
670 new SafePolicyController(commonClassDao);
671 new DescriptiveDictionaryController(commonClassDao);
672 List<Object> object = new ArrayList<>();
673 object.add(new Category());
674 when(commonClassDao.getDataById(Category.class, "shortName", "resource")).thenReturn(object);
675 httpServletRequest = Mockito.mock(HttpServletRequest.class);
676 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
678 "{\"dictionaryFields\": {\"onapName\": \"testMMRestAPI1\",\"description\": \"testing update response "
680 dictionaryTestSetup(false, "OnapName", json);
681 // send Request to PAP
682 pap.service(httpServletRequest, httpServletResponse);
684 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
686 httpServletRequest = Mockito.mock(HttpServletRequest.class);
687 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
688 json = "{\"dictionaryFields\": {\"xacmlId\": \"testMMRestAPI1\",\"datatypeBean\": {\"shortName\": "
689 + "\"string\"}, \"description\": \"testing update\",\"priority\": \"High\","
690 + "\"userDataTypeValues\": [{\"attributeValues\": \"testAttr\"}, {\"attributeValues\": "
691 + "\"testAttr2\"}, {\"attributeValues\": \"testAttr3\"}]}}";
692 dictionaryTestSetup(false, "Attribute", json);
693 // send Request to PAP
694 pap.service(httpServletRequest, httpServletResponse);
696 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
698 httpServletRequest = Mockito.mock(HttpServletRequest.class);
699 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
700 json = "{\"dictionaryFields\":{\"attributeName\":\"TestMMrestAPI1\",\"type\":\"REST\",\"url\":\"testsomeurl"
701 + ".com\",\"method\":\"GET\",\"description\":\"test create\",\"body\":\"Testing Create\","
702 + "\"headers\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\","
703 + "\"number\":\"test\"}]}}";
704 dictionaryTestSetup(false, "Action", json);
705 // send Request to PAP
706 pap.service(httpServletRequest, httpServletResponse);
708 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
710 httpServletRequest = Mockito.mock(HttpServletRequest.class);
711 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
712 json = "{\"dictionaryFields\":{\"scopeName\":\"testMMRestAPI1\",\"description\":\"test\","
713 + "\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\","
714 + "\"number\":\"test\"}]}}";
715 dictionaryTestSetup(false, "DescriptiveScope", json);
716 // send Request to PAP
717 pap.service(httpServletRequest, httpServletResponse);
719 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
721 httpServletRequest = Mockito.mock(HttpServletRequest.class);
722 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
723 json = "{\"dictionaryFields\":{\"riskName\":\"testMMrestAPI1\",\"description\":\"test\"}}";
724 dictionaryTestSetup(false, "RiskType", json);
725 // send Request to PAP
726 pap.service(httpServletRequest, httpServletResponse);
728 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
730 httpServletRequest = Mockito.mock(HttpServletRequest.class);
731 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
732 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"message\":\"test\","
733 + "\"riskType\":\"testMMrestAPI1\"}}";
734 dictionaryTestSetup(false, "SafePolicyWarning", json);
735 // send Request to PAP
736 pap.service(httpServletRequest, httpServletResponse);
738 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
742 public void testDecisionCreateDictionary() throws IOException, SQLException, ServletException {
743 new DecisionPolicyDictionaryController(commonClassDao);
744 httpServletRequest = Mockito.mock(HttpServletRequest.class);
745 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
747 "{\"dictionaryFields\":{\"xacmlId\":\"testMMRestAPI1\",\"datatypeBean\":{\"shortName\":\"string\"},"
748 + "\"description\":\"test\",\"priority\":\"High\"}}";
749 dictionaryTestSetup(false, "Settings", json);
750 // send Request to PAP
751 pap.service(httpServletRequest, httpServletResponse);
753 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
755 httpServletRequest = Mockito.mock(HttpServletRequest.class);
756 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
757 json = "{\"dictionaryFields\":{\"bbid\":\"BB1\",\"workstep\":\"1\",\"treatments\":\"Manual Handling,Abort,"
759 dictionaryTestSetup(false, "RainyDayTreatments", json);
760 // send Request to PAP
761 pap.service(httpServletRequest, httpServletResponse);
763 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
767 public void testMSCreateDictionary() throws IOException, SQLException, ServletException {
768 new MicroServiceDictionaryController(commonClassDao);
769 new PolicyScopeDictionaryController(commonClassDao);
770 httpServletRequest = Mockito.mock(HttpServletRequest.class);
771 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
772 String json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
773 dictionaryTestSetup(false, "MicroServiceLocation", json);
774 // send Request to PAP
775 pap.service(httpServletRequest, httpServletResponse);
777 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
779 httpServletRequest = Mockito.mock(HttpServletRequest.class);
780 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
781 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
782 dictionaryTestSetup(false, "MicroServiceConfigName", json);
783 // send Request to PAP
784 pap.service(httpServletRequest, httpServletResponse);
786 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
788 httpServletRequest = Mockito.mock(HttpServletRequest.class);
789 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
790 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"description\":\"test\"}}";
791 dictionaryTestSetup(false, "DCAEUUID", json);
792 // send Request to PAP
793 pap.service(httpServletRequest, httpServletResponse);
795 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
797 httpServletRequest = Mockito.mock(HttpServletRequest.class);
798 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
799 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
800 dictionaryTestSetup(false, "PolicyScopeService", json);
801 // send Request to PAP
802 pap.service(httpServletRequest, httpServletResponse);
804 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
806 httpServletRequest = Mockito.mock(HttpServletRequest.class);
807 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
808 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
809 dictionaryTestSetup(false, "PolicyScopeResource", json);
810 // send Request to PAP
811 pap.service(httpServletRequest, httpServletResponse);
813 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
815 httpServletRequest = Mockito.mock(HttpServletRequest.class);
816 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
817 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
818 dictionaryTestSetup(false, "PolicyScopeType", json);
819 // send Request to PAP
820 pap.service(httpServletRequest, httpServletResponse);
822 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
824 httpServletRequest = Mockito.mock(HttpServletRequest.class);
825 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
826 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
827 dictionaryTestSetup(false, "PolicyScopeClosedLoop", json);
828 // send Request to PAP
829 pap.service(httpServletRequest, httpServletResponse);
831 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
833 httpServletRequest = Mockito.mock(HttpServletRequest.class);
834 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
835 json = "{\"dictionaryFields\":{\"groupName\":\"testMMrestAPI1\",\"description\":\"testing\"},"
836 + "\"groupPolicyScopeListData1\":{\"resource\":\"ANY\",\"type\":\"ANY\",\"service\":\"ANY\","
837 + "\"closedloop\":\"ANY\"}}";
838 dictionaryTestSetup(false, "GroupPolicyScopeList", json);
839 // send Request to PAP
840 pap.service(httpServletRequest, httpServletResponse);
842 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
845 private void dictionaryTestSetup(Boolean updateFlag, String dictionaryType, String json)
846 throws IOException, SQLException {
847 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
848 Mockito.when(httpServletRequest.getHeader("ClientScope")).thenReturn("dictionaryItem");
849 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
850 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
852 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("update");
854 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
856 Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionaryType);
857 MockServletInputStream mockInput = new MockServletInputStream(json.getBytes());
858 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
859 Mockito.when(httpServletRequest.getReader()).thenReturn(new BufferedReader(new InputStreamReader(mockInput)));
862 public void setDBDao() throws SQLException {
863 BasicDataSource dataSource = new BasicDataSource();
864 dataSource.setDriverClassName("org.h2.Driver");
865 // In-memory DB for testing
866 dataSource.setUrl("jdbc:h2:mem:test");
867 dataSource.setUsername("sa");
868 dataSource.setPassword("");
869 LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
870 sessionBuilder.scanPackages("org.onap.*", "com.*");
872 Properties properties = new Properties();
873 properties.put("hibernate.show_sql", "false");
874 properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
875 properties.put("hibernate.hbm2ddl.auto", "drop");
876 properties.put("hibernate.hbm2ddl.auto", "create");
878 sessionBuilder.addProperties(properties);
879 sessionFactory = sessionBuilder.buildSessionFactory();
881 // Set up dao with SessionFactory
882 CommonClassDaoImpl.setSessionfactory(sessionFactory);
883 PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
887 public void getDictionary() throws ServletException, IOException {
888 String[] dictionarys = new String[] {"Attribute", "OnapName", "Action", "BRMSParamTemplate", "VSCLAction",
889 "VNFType", "PepOptions", "Varbind", "Service", "Site", "Settings", "RainyDayTreatments",
890 "DescriptiveScope", "ActionList", "ProtocolList", "Zone", "SecurityZone", "PrefixList", "AddressGroup",
891 "ServiceGroup", "ServiceList", "TermList", "MicroServiceLocation", "MicroServiceConfigName", "DCAEUUID",
892 "MicroServiceModels", "PolicyScopeService", "PolicyScopeResource", "PolicyScopeType",
893 "PolicyScopeClosedLoop", "GroupPolicyScopeList", "RiskType", "SafePolicyWarning",
894 "MicroServiceDictionary"};
895 for (String dictionary : dictionarys) {
896 httpServletRequest = Mockito.mock(HttpServletRequest.class);
897 httpServletResponse = new MockHttpServletResponse();
898 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
899 Mockito.when(httpServletRequest.getMethod()).thenReturn("GET");
900 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
901 Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionary);
902 pap.service(httpServletRequest, httpServletResponse);
903 assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
908 public void testDummy() throws ServletException, IOException {
910 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
911 mockOutput = Mockito.mock(ServletOutputStream.class);
914 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
915 } catch (IOException e) {
916 fail(e.getMessage());
920 pap.service(httpServletRequest, httpServletResponse);
922 } catch (Exception e) {
923 fail(e.getMessage());
928 public static void destroy() {
929 if (sessionFactory != null) {
930 sessionFactory.close();