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;
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.Collections;
34 import java.util.HashMap;
35 import java.util.List;
37 import java.util.Properties;
38 import javax.servlet.ServletConfig;
39 import javax.servlet.ServletException;
40 import javax.servlet.ServletOutputStream;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45 import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
46 import org.hibernate.SessionFactory;
47 import org.junit.AfterClass;
48 import org.junit.Before;
49 import org.junit.BeforeClass;
50 import org.junit.Test;
51 import org.mockito.Mockito;
52 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
53 import org.onap.policy.pap.xacml.rest.components.FirewallConfigPolicy;
54 import org.onap.policy.pap.xacml.rest.components.PolicyDBDaoTest;
55 import org.onap.policy.pap.xacml.rest.controller.ActionPolicyDictionaryController;
56 import org.onap.policy.pap.xacml.rest.controller.ClosedLoopDictionaryController;
57 import org.onap.policy.pap.xacml.rest.controller.DecisionPolicyDictionaryController;
58 import org.onap.policy.pap.xacml.rest.controller.DescriptiveDictionaryController;
59 import org.onap.policy.pap.xacml.rest.controller.DictionaryController;
60 import org.onap.policy.pap.xacml.rest.controller.FirewallDictionaryController;
61 import org.onap.policy.pap.xacml.rest.controller.MicroServiceDictionaryController;
62 import org.onap.policy.pap.xacml.rest.controller.PolicyScopeDictionaryController;
63 import org.onap.policy.pap.xacml.rest.controller.SafePolicyController;
64 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
65 import org.onap.policy.pap.xacml.rest.policycontroller.PolicyCreation;
66 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
67 import org.onap.policy.rest.dao.CommonClassDao;
68 import org.onap.policy.rest.jpa.BRMSParamTemplate;
69 import org.onap.policy.rest.jpa.Category;
70 import org.onap.policy.rest.jpa.PolicyEditorScopes;
71 import org.onap.policy.rest.jpa.UserInfo;
72 import org.onap.policy.utils.PolicyUtils;
73 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
74 import org.onap.policy.xacml.std.pap.StdPAPPolicyParams;
75 import org.springframework.mock.web.MockHttpServletResponse;
76 import org.springframework.mock.web.MockServletConfig;
77 import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
80 public class XACMLPAPTest {
81 private static final Log logger = LogFactory.getLog(XACMLPAPTest.class);
83 private static final String ENVIRONMENT_HEADER = "Environment";
84 private static List<String> headers = new ArrayList<>();
85 private HttpServletRequest httpServletRequest;
86 private HttpServletResponse httpServletResponse;
87 private ServletOutputStream mockOutput;
88 private static ServletConfig servletConfig;
89 private static XACMLPapServlet pap;
90 private static SessionFactory sessionFactory;
91 private static CommonClassDao commonClassDao;
93 private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";
94 private static final String DEFAULT_DB_USER = "sa";
95 private static final String DEFAULT_DB_PWD = "";
98 public static void beforeClassSetup() throws ServletException {
99 sessionFactory = PolicyDBDaoTest.setupH2DbDaoImpl("xacmlpaptest");
100 new FirewallConfigPolicy(new CommonClassDaoImpl());
101 new DictionaryUtils(new CommonClassDaoImpl());
105 public static void setUp() throws ServletException {
106 servletConfig = Mockito.mock(MockServletConfig.class);
107 System.setProperty("com.sun.management.jmxremote.port", "9993");
108 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
109 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
110 .thenReturn("src/test/resources/xacml.pap.properties");
111 pap = new XACMLPapServlet();
112 pap.init(servletConfig);
113 commonClassDao = Mockito.mock(CommonClassDao.class);
114 new DictionaryUtils(commonClassDao);
115 DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
116 UserInfo user = new UserInfo();
117 user.setUserLoginId("API");
118 user.setUserName("API");
119 Mockito.when(commonClassDao.getEntityItem(UserInfo.class, "userLoginId", "API")).thenReturn(user);
120 Mockito.mock(DictionaryUtils.class);
124 public void testInit() {
125 httpServletRequest = Mockito.mock(HttpServletRequest.class);
126 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
127 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
128 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
129 CommonClassDaoImpl.setSessionfactory(sessionFactory);
130 PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
134 public void testFirwallCreatePolicy() throws IOException, ServletException, SQLException {
135 httpServletRequest = Mockito.mock(HttpServletRequest.class);
136 String json = "{\"serviceTypeId\":\"/v0/firewall/pan\",\"configName\":\"TestFwPolicyConfig\","
137 + "\"deploymentOption\":{\"deployNow\":false},\"securityZoneId\":\"cloudsite:dev1a\","
138 + "\"serviceGroups\":[{\"name\":\"SSH\",\"description\":\"Sshservice entry in servicelist\","
139 + "\"type\":\"SERVICE\",\"transportProtocol\":\"tcp\",\"appProtocol\":null,\"ports\":\"22\"}],"
140 + "\"addressGroups\":[{\"name\":\"test\",\"description\":\"Destination\","
141 + "\"members\":[{\"type\":\"SUBNET\",\"value\":\"127.0.0.1/12\"}]},{\"name\":\"TestServers\","
142 + "\"description\":\"SourceTestServers for firsttesting\",\"members\":[{\"type\":\"SUBNET\","
143 + "\"value\":\"127.0.0.1/23\"}]}],\"firewallRuleList\":[{\"position\":\"1\","
144 + "\"ruleName\":\"FWRuleTestServerToTest\",\"fromZones\":[\"UntrustedZoneTestName\"],"
145 + "\"toZones\":[\"TrustedZoneTestName\"],\"negateSource\":false,\"negateDestination\":false,"
146 + "\"sourceList\":[{\"type\":\"REFERENCE\",\"name\":\"TestServers\"}],"
147 + "\"destinationList\":[{\"type\":\"REFERENCE\",\"name\":\"Test\"}],\"sourceServices\":[],"
148 + "\"destServices\":[{\"type\":\"REFERENCE\",\"name\":\"SSH\"}],\"action\":\"accept\","
149 + "\"description\":\"FWrule for Test source to Test destination\",\"enabled\":true,"
151 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
152 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
153 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
154 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
155 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
156 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder().configPolicyType("Firewall Config")
157 .policyName("test").description("testDescription").configName("Test").editPolicy(false).domain("test")
158 .jsonBody(json).highestVersion(0).riskLevel("5").riskType("default").guard("false").ttlDate("")
160 MockServletInputStream mockInput =
161 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
162 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
164 pap.service(httpServletRequest, httpServletResponse);
165 Mockito.verify(httpServletResponse).addHeader("operation", "create");
169 public void testBRMSCreatePolicy() throws IOException, ServletException, SQLException {
170 httpServletRequest = Mockito.mock(HttpServletRequest.class);
171 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
172 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
173 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
174 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
175 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
176 Map<String, String> matchingAttributes = new HashMap<>();
177 Map<String, String> ruleAttributes = new HashMap<>();
178 ruleAttributes.put("templateName", "testPolicy");
179 ruleAttributes.put("samPoll", "5");
180 ruleAttributes.put("value", "test");
181 // Creating BRMS Param Policies from the Admin Console
182 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder().configPolicyType("BRMS_Param")
183 .policyName("test").description("testing").configName("BRMS_PARAM_RULE").editPolicy(false)
184 .domain("test").dynamicFieldConfigAttributes(matchingAttributes).highestVersion(0).onapName("DROOLS")
185 .configBodyData(null).drlRuleAndUIParams(ruleAttributes).riskLevel("5").riskType("default")
186 .guard("false").ttlDate("").brmsController(null).brmsDependency(null).build());
187 MockServletInputStream mockInput =
188 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
189 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
192 pap.service(httpServletRequest, httpServletResponse);
193 Mockito.verify(httpServletResponse).addHeader("operation", "create");
197 public void testBRMSRawCreatePolicy() throws IOException, ServletException, SQLException {
198 httpServletRequest = Mockito.mock(HttpServletRequest.class);
199 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
200 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
201 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
202 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
203 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
204 Map<String, String> ruleAttributes = new HashMap<>();
205 ruleAttributes.put("value", "test");
206 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder().configPolicyType("BRMS_Raw")
207 .policyName("test").description("testig description").configName("BRMS_RAW_RULE").editPolicy(false)
208 .domain("test").dynamicFieldConfigAttributes(ruleAttributes).highestVersion(0).onapName("DROOLS")
209 .configBodyData("test").riskLevel("4").riskType("default").guard("false").build());
210 MockServletInputStream mockInput =
211 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
212 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
215 pap.service(httpServletRequest, httpServletResponse);
216 Mockito.verify(httpServletResponse).addHeader("operation", "create");
220 public void testClosedLoopPMCreatePolicy() throws IOException, ServletException, SQLException {
221 httpServletRequest = Mockito.mock(HttpServletRequest.class);
222 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
223 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
224 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
225 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
226 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
227 String json = "{\"test\":\"java\"}";
228 // Creating CloseLoop_Fault and Performance Metric Policies
229 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder().configPolicyType("ClosedLoop_PM")
230 .policyName("test").description("testing").onapName("onap").jsonBody(json).draft(false)
231 .oldPolicyFileName(null).serviceType("Registration Failure(Trinity)").editPolicy(false).domain("test")
232 .highestVersion(0).riskLevel(null).riskType("default").guard("true").ttlDate("").build());
233 MockServletInputStream mockInput =
234 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
235 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
238 pap.service(httpServletRequest, httpServletResponse);
239 Mockito.verify(httpServletResponse).addHeader("operation", "create");
243 public void testDecisonAAFPolicy() throws IOException, ServletException, SQLException {
244 httpServletRequest = Mockito.mock(HttpServletRequest.class);
245 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
246 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
247 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
248 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
249 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
250 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(
251 StdPAPPolicyParams.builder().policyName("test").description("test rule").onapName("ONAP")
252 .providerComboBox("AAF").editPolicy(false).domain("test").highestVersion(0).build());
253 MockServletInputStream mockInput =
254 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
255 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
257 pap.service(httpServletRequest, httpServletResponse);
258 Mockito.verify(httpServletResponse).addHeader("operation", "create");
262 public void testDecisonGuardPolicy() throws IOException, ServletException, SQLException {
263 httpServletRequest = Mockito.mock(HttpServletRequest.class);
264 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
265 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
266 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
267 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
268 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
269 Map<String, String> matchingAttributes = new HashMap<>();
270 matchingAttributes.put("actor", "test");
271 matchingAttributes.put("recipe", "restart");
272 matchingAttributes.put("targets", "test,test1");
273 matchingAttributes.put("clname", "");
274 matchingAttributes.put("limit", "1");
275 matchingAttributes.put("timeWindow", "15");
276 matchingAttributes.put("timeUnits", "minute");
277 matchingAttributes.put("guardActiveStart", "05:00");
278 matchingAttributes.put("guardActiveEnd", "10:00");
279 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(
280 StdPAPPolicyParams.builder().policyName("testGuard").description("test rule").onapName("PDPD")
281 .providerComboBox("GUARD_YAML").dynamicFieldConfigAttributes(matchingAttributes)
282 .editPolicy(false).domain("test").highestVersion(0).build());
283 MockServletInputStream mockInput =
284 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
285 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
287 pap.service(httpServletRequest, httpServletResponse);
288 Mockito.verify(httpServletResponse).addHeader("operation", "create");
292 public void testDecisonGuardMinMaxPolicy() throws IOException, ServletException, SQLException {
293 httpServletRequest = Mockito.mock(HttpServletRequest.class);
294 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
295 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
296 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
297 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
298 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
299 Map<String, String> matchingAttributes = new HashMap<>();
300 matchingAttributes.put("actor", "test");
301 matchingAttributes.put("recipe", "scaleOut");
302 matchingAttributes.put("targets", "test,test1");
303 matchingAttributes.put("clname", "test");
304 matchingAttributes.put("min", "1");
305 matchingAttributes.put("max", "5");
306 matchingAttributes.put("guardActiveStart", "05:00");
307 matchingAttributes.put("guardActiveEnd", "10:00");
308 StdPAPPolicy newPAPPolicy =
311 StdPAPPolicyParams.builder().policyName("testGuardMinMax").description("test rule").onapName("PDPD")
312 .providerComboBox("GUARD_MIN_MAX").dynamicFieldConfigAttributes(matchingAttributes)
313 .editPolicy(false).domain("test").highestVersion(0).build());
314 MockServletInputStream mockInput =
315 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
316 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
318 pap.service(httpServletRequest, httpServletResponse);
319 Mockito.verify(httpServletResponse).addHeader("operation", "create");
324 public void testDecisonBLGuardPolicy() throws IOException, ServletException, SQLException {
325 httpServletRequest = Mockito.mock(HttpServletRequest.class);
326 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
327 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
328 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
329 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
330 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
331 Map<String, String> matchingAttributes = new HashMap<>();
332 matchingAttributes.put("actor", "test");
333 matchingAttributes.put("recipe", "restart");
334 matchingAttributes.put("clname", "test");
335 matchingAttributes.put("guardActiveStart", "05:00");
336 matchingAttributes.put("guardActiveEnd", "10:00");
337 matchingAttributes.put("blackList", "bl1,bl2");
338 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(
339 StdPAPPolicyParams.builder().policyName("testblGuard").description("test rule").onapName("PDPD")
340 .providerComboBox("GUARD_BL_YAML").dynamicFieldConfigAttributes(matchingAttributes)
341 .editPolicy(false).domain("test").highestVersion(0).build());
342 MockServletInputStream mockInput =
343 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
344 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
346 pap.service(httpServletRequest, httpServletResponse);
347 Mockito.verify(httpServletResponse).addHeader("operation", "create");
351 public void testConfigPolicy() throws IOException, ServletException, SQLException {
352 httpServletRequest = Mockito.mock(HttpServletRequest.class);
353 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
354 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
355 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
356 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
357 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
358 Map<String, String> configAttributes = new HashMap<>();
359 configAttributes.put("value", "test");
360 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(
361 StdPAPPolicyParams.builder().configPolicyType("Base").policyName("test").description("test rule")
362 .onapName("TEST").configName("config").dynamicFieldConfigAttributes(configAttributes)
363 .configType("OTHER").configBodyData("test body").editPolicy(false).domain("test")
364 .highestVersion(0).riskLevel("5").riskType("default").guard("false").ttlDate(null).build());
365 MockServletInputStream mockInput =
366 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
367 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
369 pap.service(httpServletRequest, httpServletResponse);
370 Mockito.verify(httpServletResponse).addHeader("operation", "create");
373 private void setPolicyCreation() {
374 CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class);
375 PolicyCreation.setCommonClassDao(commonClassDao);
376 PolicyEditorScopes editorScope = new PolicyEditorScopes();
377 UserInfo userInfo = new UserInfo();
378 userInfo.setUserName("API");
379 userInfo.setUserLoginId("API");
380 editorScope.setScopeName("test");
381 editorScope.setUserCreatedBy(userInfo);
382 editorScope.setUserModifiedBy(userInfo);
383 Mockito.when(commonClassDao.getEntityItem(PolicyEditorScopes.class, "scopeName", "test"))
384 .thenReturn(editorScope);
385 BRMSParamTemplate template = new BRMSParamTemplate();
386 template.setRuleName("testPolicy");
387 template.setUserCreatedBy(userInfo);
388 String rule = "package com.sample;\n" + "import com.sample.DroolsTest.Message;\n" + "declare Params\n"
389 + "samPoll : int\n" + "value : String\n" + "end\n" + "///This Rule will be generated by the UI.\n"
390 + "rule \"${policyName}.Create parameters structure\"\n" + "salience 1000 \n" + "when\n" + "then\n"
391 + "Params params = new Params();\n" + "params.setSamPoll(76);\n" + "params.setValue(\"test\");\n"
392 + "insertLogical(params);\n" + "end\n"
393 + "rule \"Rule 1: Check parameter structure access from when/then\"\n" + "when\n" + "$param: Params()\n"
394 + "Params($param.samPoll > 50)\n" + "then\n" + "System.out.println(\"Firing rule 1\");\n"
395 + "System.out.println($param);\n" + "end\n";
396 template.setRule(rule);
397 Mockito.when(commonClassDao.getEntityItem(BRMSParamTemplate.class, "ruleName", "testPolicy"))
398 .thenReturn(template);
403 public void testClosedLoopCreateDictionary() throws IOException, SQLException, ServletException {
404 httpServletRequest = Mockito.mock(HttpServletRequest.class);
406 String json = "{\"dictionaryFields\": {\"vsclaction\": \"testRestAPI\",\"description\": \"testing create\"}}";
407 dictionaryTestSetup(false, "VSCLAction", json);
409 ClosedLoopDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
410 // send Request to PAP
411 pap.service(httpServletRequest, httpServletResponse);
413 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
417 httpServletRequest = Mockito.mock(HttpServletRequest.class);
418 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
419 json = "{\"dictionaryFields\": {\"vnftype\": \"testrestAPI1\",\"description\": \"testing create\"}}";
420 dictionaryTestSetup(false, "VNFType", json);
421 // send Request to PAP
422 pap.service(httpServletRequest, httpServletResponse);
424 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
428 httpServletRequest = Mockito.mock(HttpServletRequest.class);
429 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
430 json = "{\"dictionaryFields\":{\"pepName\":\"testRestAPI\",\"description\":\"testing create\","
431 + "\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\","
432 + "\"number\":\"test\"}]}}";
433 dictionaryTestSetup(false, "PEPOptions", json);
434 // send Request to PAP
435 pap.service(httpServletRequest, httpServletResponse);
437 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
441 httpServletRequest = Mockito.mock(HttpServletRequest.class);
442 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
443 json = "{\"dictionaryFields\":{\"varbindName\":\"testRestAPI\",\"varbindDescription\":\"testing\","
444 + "\"varbindOID\":\"test\"}}";
445 dictionaryTestSetup(false, "Varbind", json);
446 // send Request to PAP
447 pap.service(httpServletRequest, httpServletResponse);
449 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
453 httpServletRequest = Mockito.mock(HttpServletRequest.class);
454 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
455 json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPI\",\"description\":\"testing\"}}";
456 dictionaryTestSetup(false, "Service", json);
457 // send Request to PAP
458 pap.service(httpServletRequest, httpServletResponse);
460 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
464 httpServletRequest = Mockito.mock(HttpServletRequest.class);
465 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
466 json = "{\"dictionaryFields\":{\"siteName\":\"testRestAPI\",\"description\":\"testing\"}}";
467 dictionaryTestSetup(false, "Site", json);
468 // send Request to PAP
469 pap.service(httpServletRequest, httpServletResponse);
471 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
475 public void testFirewallCreateDictionary() throws IOException, SQLException, ServletException {
476 httpServletRequest = Mockito.mock(HttpServletRequest.class);
477 // Check SecurityZone.
478 String json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"testing\"}}";
479 dictionaryTestSetup(false, "SecurityZone", json);
481 FirewallDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
482 // send Request to PAP
483 pap.service(httpServletRequest, httpServletResponse);
485 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
489 httpServletRequest = Mockito.mock(HttpServletRequest.class);
490 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
491 json = "{\"dictionaryFields\":{\"actionName\":\"testRestAPI\",\"description\":\"test\"}}";
492 dictionaryTestSetup(false, "ActionList", json);
493 // send Request to PAP
494 pap.service(httpServletRequest, httpServletResponse);
496 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
498 // Check Protocol List.
500 httpServletRequest = Mockito.mock(HttpServletRequest.class);
501 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
502 json = "{\"dictionaryFields\":{\"protocolName\":\"testRestAPI\",\"description\":\"test\"}}";
503 dictionaryTestSetup(false, "ProtocolList", json);
504 // send Request to PAP
505 pap.service(httpServletRequest, httpServletResponse);
507 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
511 httpServletRequest = Mockito.mock(HttpServletRequest.class);
512 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
513 json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"test\"}}";
514 dictionaryTestSetup(false, "Zone", json);
515 // send Request to PAP
516 pap.service(httpServletRequest, httpServletResponse);
518 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
522 httpServletRequest = Mockito.mock(HttpServletRequest.class);
523 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
524 json = "{\"dictionaryFields\":{\"prefixListName\":\"testRestAPI\",\"prefixListValue\":\"127.0.0.1\","
525 + "\"description\":\"testing\"}}";
526 dictionaryTestSetup(false, "PrefixList", json);
527 // send Request to PAP
528 pap.service(httpServletRequest, httpServletResponse);
530 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
532 // Check AddressGroup.
534 httpServletRequest = Mockito.mock(HttpServletRequest.class);
535 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
536 json = "{\"dictionaryFields\":{\"groupName\":\"testRestAPIgroup\",\"description\":\"testing\","
537 + "\"attributes\":[{\"option\":\"testRestAPI\"}, {\"option\":\"testRestAPI\"}]}}";
538 dictionaryTestSetup(false, "AddressGroup", json);
539 // send Request to PAP
540 pap.service(httpServletRequest, httpServletResponse);
542 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
544 // Check ServiceGroup.
546 httpServletRequest = Mockito.mock(HttpServletRequest.class);
547 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
548 json = "{\"dictionaryFields\":{\"groupName\":\"testRestAPIServiceGroup\","
549 + "\"attributes\":[{\"option\":\"testRestAPIservice\"}]}}";
550 dictionaryTestSetup(false, "ServiceGroup", json);
551 // send Request to PAP
552 pap.service(httpServletRequest, httpServletResponse);
554 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
556 // Check ServiceList.
558 httpServletRequest = Mockito.mock(HttpServletRequest.class);
559 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
560 json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPIservice\",\"serviceDescription\":\"test\","
561 + "\"servicePorts\":\"8888\",\"transportProtocols\":[{\"option\":\"testRestAPI\"},"
562 + "{\"option\":\"testRestAPI1\"}],\"appProtocols\":[{\"option\":\"testRestAPI\"},"
563 + "{\"option\":\"testRestAPI1\"}]}}";
564 dictionaryTestSetup(false, "ServiceList", json);
565 // send Request to PAP
566 pap.service(httpServletRequest, httpServletResponse);
568 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
572 httpServletRequest = Mockito.mock(HttpServletRequest.class);
573 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
574 json = "{\"dictionaryFields\":{\"termName\":\"testRestAPIRule\",\"termDescription\":\"testing\","
575 + "\"fromZoneDatas\":[{\"option\":\"testRestAPI\"}],"
576 + "\"toZoneDatas\":[{\"option\":\"testRestAPI1\"}],"
577 + "\"sourceListDatas\":[{\"option\":\"Group_testportal\"}],"
578 + "\"destinationListDatas\":[{\"option\":\"testRestAPI\"}],"
579 + "\"sourceServiceDatas\":[{\"option\":\"testRestAPIservice\"},"
580 + "{\"option\":\"testRestAPIservice1\"}],"
581 + "\"destinationServiceDatas\":[{\"option\":\"testRestAPIservice1\"},"
582 + "{\"option\":\"testportalservice2\"}],\"actionListDatas\":[{\"option\":\"testRestAPI\"}]}}";
583 dictionaryTestSetup(false, "TermList", json);
584 // send Request to PAP
585 pap.service(httpServletRequest, httpServletResponse);
587 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
591 public void testCommonCreateDictionary() throws IOException, SQLException, ServletException {
592 new DictionaryController(commonClassDao);
593 new ActionPolicyDictionaryController(commonClassDao);
594 new SafePolicyController(commonClassDao);
595 new DescriptiveDictionaryController(commonClassDao);
596 List<Object> object = new ArrayList<>();
597 object.add(new Category());
598 when(commonClassDao.getDataById(Category.class, "shortName", "resource")).thenReturn(object);
599 httpServletRequest = Mockito.mock(HttpServletRequest.class);
600 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
602 "{\"dictionaryFields\": {\"onapName\": \"testMMRestAPI1\",\"description\": \"testing update response "
604 dictionaryTestSetup(false, "OnapName", json);
605 // send Request to PAP
606 pap.service(httpServletRequest, httpServletResponse);
608 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
610 httpServletRequest = Mockito.mock(HttpServletRequest.class);
611 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
612 json = "{\"dictionaryFields\": {\"xacmlId\": \"testMMRestAPI1\",\"datatypeBean\": {\"shortName\": "
613 + "\"string\"}, \"description\": \"testing update\",\"priority\": \"High\","
614 + "\"userDataTypeValues\": [{\"attributeValues\": \"testAttr\"}, {\"attributeValues\": "
615 + "\"testAttr2\"}, {\"attributeValues\": \"testAttr3\"}]}}";
616 dictionaryTestSetup(false, "Attribute", json);
617 // send Request to PAP
618 pap.service(httpServletRequest, httpServletResponse);
620 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
623 httpServletRequest = Mockito.mock(HttpServletRequest.class);
624 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
625 json = "{\"dictionaryFields\":{\"attributeName\":\"TestMMrestAPI1\",\"type\":\"REST\",\"url\":\"testsomeurl"
626 + ".com\",\"method\":\"GET\",\"description\":\"test create\",\"body\":\"Testing Create\","
627 + "\"headers\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\","
628 + "\"number\":\"test\"}]}}";
629 dictionaryTestSetup(false, "Action", json);
630 // send Request to PAP
631 pap.service(httpServletRequest, httpServletResponse);
633 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
635 httpServletRequest = Mockito.mock(HttpServletRequest.class);
636 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
637 json = "{\"dictionaryFields\":{\"scopeName\":\"testMMRestAPI1\",\"description\":\"test\","
638 + "\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\","
639 + "\"number\":\"test\"}]}}";
640 dictionaryTestSetup(false, "DescriptiveScope", json);
641 // send Request to PAP
642 pap.service(httpServletRequest, httpServletResponse);
644 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
646 httpServletRequest = Mockito.mock(HttpServletRequest.class);
647 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
648 json = "{\"dictionaryFields\":{\"riskName\":\"testMMrestAPI1\",\"description\":\"test\"}}";
649 dictionaryTestSetup(false, "RiskType", json);
650 // send Request to PAP
651 pap.service(httpServletRequest, httpServletResponse);
653 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
655 httpServletRequest = Mockito.mock(HttpServletRequest.class);
656 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
657 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"message\":\"test\","
658 + "\"riskType\":\"testMMrestAPI1\"}}";
659 dictionaryTestSetup(false, "SafePolicyWarning", json);
660 // send Request to PAP
661 pap.service(httpServletRequest, httpServletResponse);
663 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
667 public void testDecisionCreateDictionary() throws IOException, SQLException, ServletException {
668 new DecisionPolicyDictionaryController(commonClassDao);
669 httpServletRequest = Mockito.mock(HttpServletRequest.class);
670 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
672 "{\"dictionaryFields\":{\"xacmlId\":\"testMMRestAPI1\",\"datatypeBean\":{\"shortName\":\"string\"},"
673 + "\"description\":\"test\",\"priority\":\"High\"}}";
674 dictionaryTestSetup(false, "Settings", json);
675 // send Request to PAP
676 pap.service(httpServletRequest, httpServletResponse);
678 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
680 httpServletRequest = Mockito.mock(HttpServletRequest.class);
681 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
682 json = "{\"dictionaryFields\":{\"bbid\":\"BB1\",\"workstep\":\"1\",\"treatments\":\"Manual Handling,Abort,"
684 dictionaryTestSetup(false, "RainyDayTreatments", json);
685 // send Request to PAP
686 pap.service(httpServletRequest, httpServletResponse);
688 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
692 public void testMSCreateDictionary() throws IOException, SQLException, ServletException {
693 new MicroServiceDictionaryController(commonClassDao);
694 new PolicyScopeDictionaryController(commonClassDao);
695 httpServletRequest = Mockito.mock(HttpServletRequest.class);
696 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
697 String json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
698 dictionaryTestSetup(false, "MicroServiceLocation", json);
699 // send Request to PAP
700 pap.service(httpServletRequest, httpServletResponse);
702 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
704 httpServletRequest = Mockito.mock(HttpServletRequest.class);
705 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
706 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
707 dictionaryTestSetup(false, "MicroServiceConfigName", json);
708 // send Request to PAP
709 pap.service(httpServletRequest, httpServletResponse);
711 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
713 httpServletRequest = Mockito.mock(HttpServletRequest.class);
714 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
715 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"description\":\"test\"}}";
716 dictionaryTestSetup(false, "DCAEUUID", json);
717 // send Request to PAP
718 pap.service(httpServletRequest, httpServletResponse);
720 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
722 httpServletRequest = Mockito.mock(HttpServletRequest.class);
723 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
724 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
725 dictionaryTestSetup(false, "PolicyScopeService", json);
726 // send Request to PAP
727 pap.service(httpServletRequest, httpServletResponse);
729 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
731 httpServletRequest = Mockito.mock(HttpServletRequest.class);
732 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
733 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
734 dictionaryTestSetup(false, "PolicyScopeResource", json);
735 // send Request to PAP
736 pap.service(httpServletRequest, httpServletResponse);
738 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
740 httpServletRequest = Mockito.mock(HttpServletRequest.class);
741 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
742 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
743 dictionaryTestSetup(false, "PolicyScopeType", json);
744 // send Request to PAP
745 pap.service(httpServletRequest, httpServletResponse);
747 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
749 httpServletRequest = Mockito.mock(HttpServletRequest.class);
750 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
751 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
752 dictionaryTestSetup(false, "PolicyScopeClosedLoop", json);
753 // send Request to PAP
754 pap.service(httpServletRequest, httpServletResponse);
756 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
758 httpServletRequest = Mockito.mock(HttpServletRequest.class);
759 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
760 json = "{\"dictionaryFields\":{\"groupName\":\"testMMrestAPI1\",\"description\":\"testing\"},"
761 + "\"groupPolicyScopeListData1\":{\"resource\":\"ANY\",\"type\":\"ANY\",\"service\":\"ANY\","
762 + "\"closedloop\":\"ANY\"}}";
763 dictionaryTestSetup(false, "GroupPolicyScopeList", json);
764 // send Request to PAP
765 pap.service(httpServletRequest, httpServletResponse);
767 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
770 private void dictionaryTestSetup(Boolean updateFlag, String dictionaryType, String json)
771 throws IOException, SQLException {
772 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
773 Mockito.when(httpServletRequest.getHeader("ClientScope")).thenReturn("dictionaryItem");
774 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
775 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
777 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("update");
779 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
781 Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionaryType);
782 MockServletInputStream mockInput = new MockServletInputStream(json.getBytes());
783 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
784 Mockito.when(httpServletRequest.getReader()).thenReturn(new BufferedReader(new InputStreamReader(mockInput)));
787 public void setDBDao() throws SQLException {
788 BasicDataSource dataSource = new BasicDataSource();
789 dataSource.setDriverClassName("org.h2.Driver");
790 // In-memory DB for testing
791 dataSource.setUrl("jdbc:h2:mem:test");
792 dataSource.setUsername("sa");
793 dataSource.setPassword("");
794 LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
795 sessionBuilder.scanPackages("org.onap.*", "com.*");
797 Properties properties = new Properties();
798 properties.put("hibernate.show_sql", "false");
799 properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
800 properties.put("hibernate.hbm2ddl.auto", "drop");
801 properties.put("hibernate.hbm2ddl.auto", "create");
803 sessionBuilder.addProperties(properties);
804 sessionFactory = sessionBuilder.buildSessionFactory();
806 // Set up dao with SessionFactory
807 CommonClassDaoImpl.setSessionfactory(sessionFactory);
808 PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
812 public void getDictionary() throws ServletException, IOException {
813 String[] dictionarys = new String[] {"Attribute", "OnapName", "Action", "BRMSParamTemplate", "VSCLAction",
814 "VNFType", "PEPOptions", "Varbind", "Service", "Site", "Settings", "RainyDayTreatments",
815 "DescriptiveScope", "ActionList", "ProtocolList", "Zone", "SecurityZone", "PrefixList", "AddressGroup",
816 "ServiceGroup", "ServiceList", "TermList", "MicroServiceLocation", "MicroServiceConfigName", "DCAEUUID",
817 "MicroServiceModels", "PolicyScopeService", "PolicyScopeResource", "PolicyScopeType",
818 "PolicyScopeClosedLoop", "GroupPolicyScopeList", "RiskType", "SafePolicyWarning",
819 "MicroServiceDictionary"};
820 for (String dictionary : dictionarys) {
821 httpServletRequest = Mockito.mock(HttpServletRequest.class);
822 httpServletResponse = new MockHttpServletResponse();
823 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
824 Mockito.when(httpServletRequest.getMethod()).thenReturn("GET");
825 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
826 Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionary);
827 pap.service(httpServletRequest, httpServletResponse);
828 assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
833 public void testDummy() throws ServletException, IOException {
835 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
836 mockOutput = Mockito.mock(ServletOutputStream.class);
839 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
840 } catch (IOException e) {
845 pap.service(httpServletRequest, httpServletResponse);
847 } catch (Exception e) {
853 public static void destroy() {
854 if (sessionFactory != null) {
855 sessionFactory.close();