2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modified 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 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;
39 import javax.persistence.EntityManager;
40 import javax.persistence.EntityManagerFactory;
41 import javax.persistence.EntityTransaction;
42 import javax.persistence.Persistence;
43 import javax.servlet.ServletConfig;
44 import javax.servlet.ServletException;
45 import javax.servlet.ServletOutputStream;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
49 import org.apache.commons.logging.Log;
50 import org.apache.commons.logging.LogFactory;
51 import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
52 import org.hibernate.SessionFactory;
53 import org.junit.After;
54 import org.junit.Before;
55 import org.junit.Test;
56 import org.mockito.Mockito;
57 import org.onap.policy.common.ia.IntegrityAuditProperties;
58 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
59 import org.onap.policy.pap.xacml.rest.controller.ActionPolicyDictionaryController;
60 import org.onap.policy.pap.xacml.rest.controller.ClosedLoopDictionaryController;
61 import org.onap.policy.pap.xacml.rest.controller.DecisionPolicyDictionaryController;
62 import org.onap.policy.pap.xacml.rest.controller.DescriptiveDictionaryController;
63 import org.onap.policy.pap.xacml.rest.controller.DictionaryController;
64 import org.onap.policy.pap.xacml.rest.controller.FirewallDictionaryController;
65 import org.onap.policy.pap.xacml.rest.controller.MicroServiceDictionaryController;
66 import org.onap.policy.pap.xacml.rest.controller.PolicyScopeDictionaryController;
67 import org.onap.policy.pap.xacml.rest.controller.SafePolicyController;
68 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
69 import org.onap.policy.pap.xacml.rest.policycontroller.PolicyCreation;
70 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
71 import org.onap.policy.rest.dao.CommonClassDao;
72 import org.onap.policy.rest.jpa.BRMSParamTemplate;
73 import org.onap.policy.rest.jpa.Category;
74 import org.onap.policy.rest.jpa.PolicyEditorScopes;
75 import org.onap.policy.rest.jpa.UserInfo;
76 import org.onap.policy.utils.PolicyUtils;
77 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
78 import org.onap.policy.xacml.std.pap.StdPAPPolicyParams;
79 import org.springframework.mock.web.MockHttpServletResponse;
80 import org.springframework.mock.web.MockServletConfig;
81 import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
83 import com.mockrunner.mock.web.MockServletInputStream;
85 public class XACMLPAPTest {
86 private static final Log logger = LogFactory.getLog(XACMLPAPTest.class);
88 private static final String ENVIRONMENT_HEADER = "Environment";
89 private List<String> headers = new ArrayList<>();
90 private HttpServletRequest httpServletRequest;
91 private HttpServletResponse httpServletResponse;
92 private ServletOutputStream mockOutput;
93 private ServletConfig servletConfig;
94 private XACMLPapServlet pap;
95 private SessionFactory sessionFactory;
96 private CommonClassDao commonClassDao;
98 private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";
99 private static final String DEFAULT_DB_USER = "sa";
100 private static final String DEFAULT_DB_PWD = "";
103 public void setUpDB() throws Exception {
104 logger.info("setUpDB: Entering");
106 Properties properties = new Properties();
107 properties.put(IntegrityAuditProperties.DB_DRIVER, XACMLPAPTest.DEFAULT_DB_DRIVER);
108 properties.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/xacmlTest");
109 properties.put(IntegrityAuditProperties.DB_USER, XACMLPAPTest.DEFAULT_DB_USER);
110 properties.put(IntegrityAuditProperties.DB_PWD, XACMLPAPTest.DEFAULT_DB_PWD);
111 properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
112 properties.put(IntegrityAuditProperties.NODE_TYPE, "pap");
114 //Clean the iaTest DB table for IntegrityAuditEntity entries
115 cleanDb("testPapPU", properties);
117 logger.info("setUpDB: Exiting");
120 public void cleanDb(String persistenceUnit, Properties properties) {
121 logger.debug("cleanDb: enter");
123 EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties);
125 EntityManager em = emf.createEntityManager();
126 // Start a transaction
127 EntityTransaction et = em.getTransaction();
132 em.createQuery("Delete from IntegrityAuditEntity").executeUpdate();
134 // commit transaction
137 logger.debug("cleanDb: exit");
141 public void setUp() throws ServletException {
142 httpServletRequest = Mockito.mock(HttpServletRequest.class);
143 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
144 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
145 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
147 servletConfig = Mockito.mock(MockServletConfig.class);
148 System.setProperty("com.sun.management.jmxremote.port", "9993");
149 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
150 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
151 .thenReturn("src/test/resources/xacml.pap.properties");
152 pap = new XACMLPapServlet();
153 pap.init(servletConfig);
154 commonClassDao = Mockito.mock(CommonClassDao.class);
155 new DictionaryUtils(commonClassDao);
156 DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
157 Mockito.mock(DictionaryUtils.class);
161 public void testFirwallCreatePolicy() throws IOException, ServletException, SQLException {
162 httpServletRequest = Mockito.mock(HttpServletRequest.class);
164 "{\"serviceTypeId\":\"/v0/firewall/pan\",\"configName\":\"TestFwPolicyConfig\"," +
165 "\"deploymentOption\":{\"deployNow\":false},\"securityZoneId\":\"cloudsite:dev1a\"," +
166 "\"serviceGroups\":[{\"name\":\"SSH\",\"description\":\"Sshservice entry in servicelist\"," +
167 "\"type\":\"SERVICE\",\"transportProtocol\":\"tcp\",\"appProtocol\":null,\"ports\":\"22\"}]," +
168 "\"addressGroups\":[{\"name\":\"test\",\"description\":\"Destination\"," +
169 "\"members\":[{\"type\":\"SUBNET\",\"value\":\"127.0.0.1/12\"}]},{\"name\":\"TestServers\"," +
170 "\"description\":\"SourceTestServers for firsttesting\",\"members\":[{\"type\":\"SUBNET\"," +
171 "\"value\":\"127.0.0.1/23\"}]}],\"firewallRuleList\":[{\"position\":\"1\"," +
172 "\"ruleName\":\"FWRuleTestServerToTest\",\"fromZones\":[\"UntrustedZoneTestName\"]," +
173 "\"toZones\":[\"TrustedZoneTestName\"],\"negateSource\":false,\"negateDestination\":false," +
174 "\"sourceList\":[{\"type\":\"REFERENCE\",\"name\":\"TestServers\"}]," +
175 "\"destinationList\":[{\"type\":\"REFERENCE\",\"name\":\"Test\"}],\"sourceServices\":[]," +
176 "\"destServices\":[{\"type\":\"REFERENCE\",\"name\":\"SSH\"}],\"action\":\"accept\"," +
177 "\"description\":\"FWrule for Test source to Test destination\",\"enabled\":true," +
179 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
180 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
181 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
182 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
183 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
184 StdPAPPolicy newPAPPolicy =
185 new StdPAPPolicy("Firewall Config", "test", "testDescription", "Test", false, "test", json, 0,
186 "5", "default", "false", "");
187 MockServletInputStream mockInput =
188 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
189 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
193 pap.service(httpServletRequest, httpServletResponse);
195 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
196 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
197 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_FW_test.1.xml");
201 public void testBRMSCreatePolicy() throws IOException, ServletException, SQLException {
202 httpServletRequest = Mockito.mock(HttpServletRequest.class);
203 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
204 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
205 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
206 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
207 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
208 Map<String, String> matchingAttributes = new HashMap<>();
209 Map<String, String> ruleAttributes = new HashMap<>();
210 ruleAttributes.put("templateName", "testPolicy");
211 ruleAttributes.put("samPoll", "5");
212 ruleAttributes.put("value", "test");
213 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Param", "test", "testing",
214 "BRMS_PARAM_RULE", false, "test",
215 matchingAttributes, 0, "DROOLS",
216 null, ruleAttributes, "5",
217 "default", "false", "", null, null);
218 MockServletInputStream mockInput =
219 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
220 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
225 pap.service(httpServletRequest, httpServletResponse);
227 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
228 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
229 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_BRMS_Param_test.1.xml");
233 public void testBRMSRawCreatePolicy() throws IOException, ServletException, SQLException {
234 httpServletRequest = Mockito.mock(HttpServletRequest.class);
235 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
236 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
237 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
238 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
239 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
240 Map<String, String> ruleAttributes = new HashMap<>();
241 ruleAttributes.put("value", "test");
242 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Raw", "test", "testig description",
243 "BRMS_RAW_RULE", false, "test", ruleAttributes, 0, "DROOLS",
245 "default", "false", null, null, null);
246 MockServletInputStream mockInput =
247 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
248 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
253 pap.service(httpServletRequest, httpServletResponse);
255 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
256 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
257 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_BRMS_Raw_test.1.xml");
261 public void testClosedLoopPMCreatePolicy() throws IOException, ServletException, SQLException {
262 httpServletRequest = Mockito.mock(HttpServletRequest.class);
263 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
264 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
265 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
266 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
267 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
268 String json = "{\"test\":\"java\"}";
269 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("ClosedLoop_PM", "test", "testing", "onap",
270 json, false, null, "Registration Failure(Trinity)", false, "test", 0, null,
271 "default", "true", "");
272 MockServletInputStream mockInput =
273 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
274 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
279 pap.service(httpServletRequest, httpServletResponse);
281 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
282 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
283 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_PM_test.1.xml");
287 public void testDecisonAAFPolicy() throws IOException, ServletException, SQLException {
288 httpServletRequest = Mockito.mock(HttpServletRequest.class);
289 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
290 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
291 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
292 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
293 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
294 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
296 .description("test rule")
298 .providerComboBox("AAF")
303 MockServletInputStream mockInput =
304 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
305 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
309 pap.service(httpServletRequest, httpServletResponse);
311 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
312 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
313 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_test.1.xml");
317 public void testDecisonGuardPolicy() throws IOException, ServletException, SQLException {
318 httpServletRequest = Mockito.mock(HttpServletRequest.class);
319 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
320 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
321 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
322 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
323 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
324 Map<String, String> matchingAttributes = new HashMap<>();
325 matchingAttributes.put("actor", "test");
326 matchingAttributes.put("recipe", "restart");
327 matchingAttributes.put("targets", "test,test1");
328 matchingAttributes.put("clname", "");
329 matchingAttributes.put("limit", "1");
330 matchingAttributes.put("timeWindow", "15");
331 matchingAttributes.put("timeUnits", "minute");
332 matchingAttributes.put("guardActiveStart", "05:00");
333 matchingAttributes.put("guardActiveEnd", "10:00");
334 StdPAPPolicy newPAPPolicy =
336 new StdPAPPolicy(StdPAPPolicyParams.builder()
337 .policyName("testGuard")
338 .description("test rule")
340 .providerComboBox("GUARD_YAML")
341 .dyanamicFieldConfigAttributes(matchingAttributes)
346 MockServletInputStream mockInput =
347 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
348 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
352 pap.service(httpServletRequest, httpServletResponse);
354 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
355 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
356 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testGuard.1.xml");
360 public void testDecisonBLGuardPolicy() throws IOException, ServletException, SQLException {
361 httpServletRequest = Mockito.mock(HttpServletRequest.class);
362 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
363 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
364 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
365 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
366 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
367 Map<String, String> matchingAttributes = new HashMap<>();
368 matchingAttributes.put("actor", "test");
369 matchingAttributes.put("recipe", "restart");
370 matchingAttributes.put("clname", "test");
371 matchingAttributes.put("guardActiveStart", "05:00");
372 matchingAttributes.put("guardActiveEnd", "10:00");
373 matchingAttributes.put("blackList", "bl1,bl2");
374 StdPAPPolicy newPAPPolicy =
375 new StdPAPPolicy(StdPAPPolicyParams.builder()
376 .policyName("testblGuard")
377 .description("test rule")
379 .providerComboBox("GUARD_BL_YAML")
380 .dyanamicFieldConfigAttributes(matchingAttributes)
385 MockServletInputStream mockInput =
386 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
387 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
391 pap.service(httpServletRequest, httpServletResponse);
393 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
394 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
395 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testblGuard.1.xml");
399 public void testConfigPolicy() throws IOException, ServletException, SQLException {
400 httpServletRequest = Mockito.mock(HttpServletRequest.class);
401 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
402 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
403 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
404 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
405 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
406 Map<String, String> configAttributes = new HashMap<>();
407 configAttributes.put("value", "test");
408 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
409 .configPolicyType("Base")
411 .description("test rule")
413 .configName("config")
414 .dyanamicFieldConfigAttributes(configAttributes)
416 .configBodyData("test body")
423 .ttlDate(null).build());
424 MockServletInputStream mockInput =
425 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
426 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
430 pap.service(httpServletRequest, httpServletResponse);
432 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
433 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
434 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_test.1.xml");
437 private void setPolicyCreation() {
438 CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class);
439 PolicyCreation.setCommonClassDao(commonClassDao);
440 PolicyEditorScopes editorScope = new PolicyEditorScopes();
441 UserInfo userInfo = new UserInfo();
442 userInfo.setUserName("API");
443 userInfo.setUserLoginId("API");
444 editorScope.setScopeName("test");
445 editorScope.setUserCreatedBy(userInfo);
446 editorScope.setUserModifiedBy(userInfo);
447 Mockito.when(commonClassDao.getEntityItem(PolicyEditorScopes.class, "scopeName", "test"))
448 .thenReturn(editorScope);
449 BRMSParamTemplate template = new BRMSParamTemplate();
450 template.setRuleName("testPolicy");
451 template.setUserCreatedBy(userInfo);
452 String rule = "package com.sample;\n"
453 + "import com.sample.DroolsTest.Message;\n"
458 + "///This Rule will be generated by the UI.\n"
459 + "rule \"${policyName}.Create parameters structure\"\n"
463 + "Params params = new Params();\n"
464 + "params.setSamPoll(76);\n"
465 + "params.setValue(\"test\");\n"
466 + "insertLogical(params);\n"
468 + "rule \"Rule 1: Check parameter structure access from when/then\"\n"
470 + "$param: Params()\n"
471 + "Params($param.samPoll > 50)\n"
473 + "System.out.println(\"Firing rule 1\");\n"
474 + "System.out.println($param);\n"
476 template.setRule(rule);
477 Mockito.when(commonClassDao.getEntityItem(BRMSParamTemplate.class, "ruleName", "testPolicy"))
478 .thenReturn(template);
483 public void testClosedLoopCreateDictionary() throws IOException, SQLException, ServletException {
484 httpServletRequest = Mockito.mock(HttpServletRequest.class);
486 String json = "{\"dictionaryFields\": {\"vsclaction\": \"testRestAPI\",\"description\": \"testing create\"}}";
487 dictionaryTestSetup(false, "VSCLAction", json);
489 ClosedLoopDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
490 // send Request to PAP
491 pap.service(httpServletRequest, httpServletResponse);
493 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
497 httpServletRequest = Mockito.mock(HttpServletRequest.class);
498 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
499 json = "{\"dictionaryFields\": {\"vnftype\": \"testrestAPI1\",\"description\": \"testing create\"}}";
500 dictionaryTestSetup(false, "VNFType", json);
501 // send Request to PAP
502 pap.service(httpServletRequest, httpServletResponse);
504 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
508 httpServletRequest = Mockito.mock(HttpServletRequest.class);
509 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
511 "{\"dictionaryFields\":{\"pepName\":\"testRestAPI\",\"description\":\"testing create\"," +
512 "\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
513 "\"number\":\"test\"}]}}";
514 dictionaryTestSetup(false, "PEPOptions", 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);
525 "{\"dictionaryFields\":{\"varbindName\":\"testRestAPI\",\"varbindDescription\":\"testing\"," +
526 "\"varbindOID\":\"test\"}}";
527 dictionaryTestSetup(false, "Varbind", json);
528 // send Request to PAP
529 pap.service(httpServletRequest, httpServletResponse);
531 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
535 httpServletRequest = Mockito.mock(HttpServletRequest.class);
536 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
537 json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPI\",\"description\":\"testing\"}}";
538 dictionaryTestSetup(false, "Service", json);
539 // send Request to PAP
540 pap.service(httpServletRequest, httpServletResponse);
542 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
546 httpServletRequest = Mockito.mock(HttpServletRequest.class);
547 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
548 json = "{\"dictionaryFields\":{\"siteName\":\"testRestAPI\",\"description\":\"testing\"}}";
549 dictionaryTestSetup(false, "Site", json);
550 // send Request to PAP
551 pap.service(httpServletRequest, httpServletResponse);
553 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
557 public void testFirewallCreateDictionary() throws IOException, SQLException, ServletException {
558 httpServletRequest = Mockito.mock(HttpServletRequest.class);
559 // Check SecurityZone.
560 String json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"testing\"}}";
561 dictionaryTestSetup(false, "SecurityZone", json);
563 FirewallDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
564 // send Request to PAP
565 pap.service(httpServletRequest, httpServletResponse);
567 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
571 httpServletRequest = Mockito.mock(HttpServletRequest.class);
572 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
573 json = "{\"dictionaryFields\":{\"actionName\":\"testRestAPI\",\"description\":\"test\"}}";
574 dictionaryTestSetup(false, "ActionList", json);
575 // send Request to PAP
576 pap.service(httpServletRequest, httpServletResponse);
578 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
580 // Check Protocol List.
582 httpServletRequest = Mockito.mock(HttpServletRequest.class);
583 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
584 json = "{\"dictionaryFields\":{\"protocolName\":\"testRestAPI\",\"description\":\"test\"}}";
585 dictionaryTestSetup(false, "ProtocolList", json);
586 // send Request to PAP
587 pap.service(httpServletRequest, httpServletResponse);
589 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
593 httpServletRequest = Mockito.mock(HttpServletRequest.class);
594 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
595 json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"test\"}}";
596 dictionaryTestSetup(false, "Zone", json);
597 // send Request to PAP
598 pap.service(httpServletRequest, httpServletResponse);
600 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
604 httpServletRequest = Mockito.mock(HttpServletRequest.class);
605 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
607 "{\"dictionaryFields\":{\"prefixListName\":\"testRestAPI\",\"prefixListValue\":\"127.0.0.1\"," +
608 "\"description\":\"testing\"}}";
609 dictionaryTestSetup(false, "PrefixList", json);
610 // send Request to PAP
611 pap.service(httpServletRequest, httpServletResponse);
613 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
615 // Check AddressGroup.
617 httpServletRequest = Mockito.mock(HttpServletRequest.class);
618 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
620 "{\"dictionaryFields\":{\"groupName\":\"testRestAPIgroup\",\"description\":\"testing\"," +
621 "\"attributes\":[{\"option\":\"testRestAPI\"}, {\"option\":\"testRestAPI\"}]}}";
622 dictionaryTestSetup(false, "AddressGroup", json);
623 // send Request to PAP
624 pap.service(httpServletRequest, httpServletResponse);
626 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
628 // Check ServiceGroup.
630 httpServletRequest = Mockito.mock(HttpServletRequest.class);
631 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
633 "{\"dictionaryFields\":{\"groupName\":\"testRestAPIServiceGroup\"," +
634 "\"attributes\":[{\"option\":\"testRestAPIservice\"}]}}";
635 dictionaryTestSetup(false, "ServiceGroup", json);
636 // send Request to PAP
637 pap.service(httpServletRequest, httpServletResponse);
639 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
641 // Check ServiceList.
643 httpServletRequest = Mockito.mock(HttpServletRequest.class);
644 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
646 "{\"dictionaryFields\":{\"serviceName\":\"testRestAPIservice\",\"serviceDescription\":\"test\"," +
647 "\"servicePorts\":\"8888\",\"transportProtocols\":[{\"option\":\"testRestAPI\"}," +
648 "{\"option\":\"testRestAPI1\"}],\"appProtocols\":[{\"option\":\"testRestAPI\"}," +
649 "{\"option\":\"testRestAPI1\"}]}}";
650 dictionaryTestSetup(false, "ServiceList", json);
651 // send Request to PAP
652 pap.service(httpServletRequest, httpServletResponse);
654 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
658 httpServletRequest = Mockito.mock(HttpServletRequest.class);
659 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
661 "{\"dictionaryFields\":{\"termName\":\"testRestAPIRule\",\"termDescription\":\"testing\"," +
662 "\"fromZoneDatas\":[{\"option\":\"testRestAPI\"}]," +
663 "\"toZoneDatas\":[{\"option\":\"testRestAPI1\"}]," +
664 "\"sourceListDatas\":[{\"option\":\"Group_testportal\"}]," +
665 "\"destinationListDatas\":[{\"option\":\"testRestAPI\"}]," +
666 "\"sourceServiceDatas\":[{\"option\":\"testRestAPIservice\"}," +
667 "{\"option\":\"testRestAPIservice1\"}]," +
668 "\"destinationServiceDatas\":[{\"option\":\"testRestAPIservice1\"}," +
669 "{\"option\":\"testportalservice2\"}],\"actionListDatas\":[{\"option\":\"testRestAPI\"}]}}";
670 dictionaryTestSetup(false, "TermList", json);
671 // send Request to PAP
672 pap.service(httpServletRequest, httpServletResponse);
674 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
678 public void testCommonCreateDictionary() throws IOException, SQLException, ServletException {
679 new DictionaryController(commonClassDao);
680 new ActionPolicyDictionaryController(commonClassDao);
681 new SafePolicyController(commonClassDao);
682 new DescriptiveDictionaryController(commonClassDao);
683 List<Object> object = new ArrayList<>();
684 object.add(new Category());
685 when(commonClassDao.getDataById(Category.class, "shortName", "resource")).thenReturn(object);
686 httpServletRequest = Mockito.mock(HttpServletRequest.class);
687 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
689 "{\"dictionaryFields\": {\"onapName\": \"testMMRestAPI1\",\"description\": \"testing update response " +
691 dictionaryTestSetup(false, "OnapName", json);
692 // send Request to PAP
693 pap.service(httpServletRequest, httpServletResponse);
695 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
697 httpServletRequest = Mockito.mock(HttpServletRequest.class);
698 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
700 "{\"dictionaryFields\": {\"xacmlId\": \"testMMRestAPI1\",\"datatypeBean\": {\"shortName\": " +
701 "\"string\"}, \"description\": \"testing update\",\"priority\": \"High\"," +
702 "\"userDataTypeValues\": [{\"attributeValues\": \"testAttr\"}, {\"attributeValues\": " +
703 "\"testAttr2\"}, {\"attributeValues\": \"testAttr3\"}]}}";
704 dictionaryTestSetup(false, "Attribute", json);
705 // send Request to PAP
706 pap.service(httpServletRequest, httpServletResponse);
708 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
711 httpServletRequest = Mockito.mock(HttpServletRequest.class);
712 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
714 "{\"dictionaryFields\":{\"attributeName\":\"TestMMrestAPI1\",\"type\":\"REST\",\"url\":\"testsomeurl" +
715 ".com\",\"method\":\"GET\",\"description\":\"test create\",\"body\":\"Testing Create\"," +
716 "\"headers\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
717 "\"number\":\"test\"}]}}";
718 dictionaryTestSetup(false, "Action", json);
719 // send Request to PAP
720 pap.service(httpServletRequest, httpServletResponse);
722 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
724 httpServletRequest = Mockito.mock(HttpServletRequest.class);
725 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
727 "{\"dictionaryFields\":{\"scopeName\":\"testMMRestAPI1\",\"description\":\"test\"," +
728 "\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
729 "\"number\":\"test\"}]}}";
730 dictionaryTestSetup(false, "DescriptiveScope", json);
731 // send Request to PAP
732 pap.service(httpServletRequest, httpServletResponse);
734 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
736 httpServletRequest = Mockito.mock(HttpServletRequest.class);
737 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
738 json = "{\"dictionaryFields\":{\"riskName\":\"testMMrestAPI1\",\"description\":\"test\"}}";
739 dictionaryTestSetup(false, "RiskType", json);
740 // send Request to PAP
741 pap.service(httpServletRequest, httpServletResponse);
743 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
745 httpServletRequest = Mockito.mock(HttpServletRequest.class);
746 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
748 "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"message\":\"test\"," +
749 "\"riskType\":\"testMMrestAPI1\"}}";
750 dictionaryTestSetup(false, "SafePolicyWarning", json);
751 // send Request to PAP
752 pap.service(httpServletRequest, httpServletResponse);
754 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
758 public void testDecisionCreateDictionary() throws IOException, SQLException, ServletException {
759 new DecisionPolicyDictionaryController(commonClassDao);
760 httpServletRequest = Mockito.mock(HttpServletRequest.class);
761 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
763 "{\"dictionaryFields\":{\"xacmlId\":\"testMMRestAPI1\",\"datatypeBean\":{\"shortName\":\"string\"}," +
764 "\"description\":\"test\",\"priority\":\"High\"}}";
765 dictionaryTestSetup(false, "Settings", json);
766 // send Request to PAP
767 pap.service(httpServletRequest, httpServletResponse);
769 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
771 httpServletRequest = Mockito.mock(HttpServletRequest.class);
772 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
774 "{\"dictionaryFields\":{\"bbid\":\"BB1\",\"workstep\":\"1\",\"treatments\":\"Manual Handling,Abort," +
776 dictionaryTestSetup(false, "RainyDayTreatments", json);
777 // send Request to PAP
778 pap.service(httpServletRequest, httpServletResponse);
780 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
784 public void testMSCreateDictionary() throws IOException, SQLException, ServletException {
785 new MicroServiceDictionaryController(commonClassDao);
786 new PolicyScopeDictionaryController(commonClassDao);
787 httpServletRequest = Mockito.mock(HttpServletRequest.class);
788 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
789 String json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
790 dictionaryTestSetup(false, "MicroServiceLocation", json);
791 // send Request to PAP
792 pap.service(httpServletRequest, httpServletResponse);
794 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
796 httpServletRequest = Mockito.mock(HttpServletRequest.class);
797 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
798 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
799 dictionaryTestSetup(false, "MicroServiceConfigName", json);
800 // send Request to PAP
801 pap.service(httpServletRequest, httpServletResponse);
803 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
805 httpServletRequest = Mockito.mock(HttpServletRequest.class);
806 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
807 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"description\":\"test\"}}";
808 dictionaryTestSetup(false, "DCAEUUID", json);
809 // send Request to PAP
810 pap.service(httpServletRequest, httpServletResponse);
812 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
814 httpServletRequest = Mockito.mock(HttpServletRequest.class);
815 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
816 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
817 dictionaryTestSetup(false, "PolicyScopeService", json);
818 // send Request to PAP
819 pap.service(httpServletRequest, httpServletResponse);
821 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
823 httpServletRequest = Mockito.mock(HttpServletRequest.class);
824 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
825 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
826 dictionaryTestSetup(false, "PolicyScopeResource", json);
827 // send Request to PAP
828 pap.service(httpServletRequest, httpServletResponse);
830 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
832 httpServletRequest = Mockito.mock(HttpServletRequest.class);
833 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
834 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
835 dictionaryTestSetup(false, "PolicyScopeType", json);
836 // send Request to PAP
837 pap.service(httpServletRequest, httpServletResponse);
839 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
841 httpServletRequest = Mockito.mock(HttpServletRequest.class);
842 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
843 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
844 dictionaryTestSetup(false, "PolicyScopeClosedLoop", json);
845 // send Request to PAP
846 pap.service(httpServletRequest, httpServletResponse);
848 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
850 httpServletRequest = Mockito.mock(HttpServletRequest.class);
851 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
853 "{\"dictionaryFields\":{\"groupName\":\"testMMrestAPI1\",\"description\":\"testing\"}," +
854 "\"groupPolicyScopeListData1\":{\"resource\":\"ANY\",\"type\":\"ANY\",\"service\":\"ANY\"," +
855 "\"closedloop\":\"ANY\"}}";
856 dictionaryTestSetup(false, "GroupPolicyScopeList", json);
857 // send Request to PAP
858 pap.service(httpServletRequest, httpServletResponse);
860 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
863 private void dictionaryTestSetup(Boolean updateFlag, String dictionaryType, String json)
864 throws IOException, SQLException {
865 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
866 Mockito.when(httpServletRequest.getHeader("ClientScope")).thenReturn("dictionaryItem");
867 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
868 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
870 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("update");
872 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
874 Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionaryType);
875 MockServletInputStream mockInput = new MockServletInputStream(json.getBytes());
876 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
877 Mockito.when(httpServletRequest.getReader()).thenReturn(new BufferedReader(new InputStreamReader(mockInput)));
882 public void setDBDao() throws SQLException {
883 BasicDataSource dataSource = new BasicDataSource();
884 dataSource.setDriverClassName("org.h2.Driver");
885 // In-memory DB for testing
886 dataSource.setUrl("jdbc:h2:mem:test");
887 dataSource.setUsername("sa");
888 dataSource.setPassword("");
889 LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
890 sessionBuilder.scanPackages("org.onap.*", "com.*");
892 Properties properties = new Properties();
893 properties.put("hibernate.show_sql", "false");
894 properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
895 properties.put("hibernate.hbm2ddl.auto", "drop");
896 properties.put("hibernate.hbm2ddl.auto", "create");
898 sessionBuilder.addProperties(properties);
899 sessionFactory = sessionBuilder.buildSessionFactory();
901 // Set up dao with SessionFactory
902 CommonClassDaoImpl.setSessionfactory(sessionFactory);
903 PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
907 public void getDictionary() throws ServletException, IOException {
908 String[] dictionarys = new String[]{"Attribute", "OnapName", "Action", "BRMSParamTemplate", "VSCLAction"
909 , "VNFType", "PEPOptions", "Varbind", "Service", "Site", "Settings", "RainyDayTreatments",
910 "DescriptiveScope", "ActionList", "ProtocolList", "Zone", "SecurityZone",
911 "PrefixList", "AddressGroup", "ServiceGroup", "ServiceList", "TermList",
912 "MicroServiceLocation", "MicroServiceConfigName", "DCAEUUID", "MicroServiceModels",
913 "PolicyScopeService", "PolicyScopeResource", "PolicyScopeType", "PolicyScopeClosedLoop",
914 "GroupPolicyScopeList", "RiskType", "SafePolicyWarning", "MicroServiceDictionary"};
915 for (String dictionary : dictionarys) {
916 httpServletRequest = Mockito.mock(HttpServletRequest.class);
917 httpServletResponse = new MockHttpServletResponse();
918 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
919 Mockito.when(httpServletRequest.getMethod()).thenReturn("GET");
920 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
921 Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionary);
922 pap.service(httpServletRequest, httpServletResponse);
923 assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
928 public void testDummy() throws ServletException, IOException {
930 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
931 mockOutput = Mockito.mock(ServletOutputStream.class);
934 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
935 } catch (IOException e) {
940 pap.service(httpServletRequest, httpServletResponse);
942 } catch (Exception e) {
948 public void destroy() {
949 if (sessionFactory != null) {
950 sessionFactory.close();