2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 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(StdPAPPolicyParams.builder()
186 .configPolicyType("Firewall Config")
188 .description("testDescription")
199 MockServletInputStream mockInput =
200 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
201 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
205 pap.service(httpServletRequest, httpServletResponse);
207 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
208 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
209 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_FW_test.1.xml");
213 public void testBRMSCreatePolicy() throws IOException, ServletException, SQLException {
214 httpServletRequest = Mockito.mock(HttpServletRequest.class);
215 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
216 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
217 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
218 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
219 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
220 Map<String, String> matchingAttributes = new HashMap<>();
221 Map<String, String> ruleAttributes = new HashMap<>();
222 ruleAttributes.put("templateName", "testPolicy");
223 ruleAttributes.put("samPoll", "5");
224 ruleAttributes.put("value", "test");
225 //Creating BRMS Param Policies from the Admin Console
226 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
227 .configPolicyType("BRMS_Param")
229 .description("testing")
230 .configName("BRMS_PARAM_RULE")
233 .dynamicFieldConfigAttributes(matchingAttributes)
236 .configBodyData(null)
237 .drlRuleAndUIParams(ruleAttributes)
242 .brmsController(null)
243 .brmsDependency(null)
245 MockServletInputStream mockInput =
246 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
247 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
252 pap.service(httpServletRequest, httpServletResponse);
254 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
255 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
256 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_BRMS_Param_test.1.xml");
260 public void testBRMSRawCreatePolicy() throws IOException, ServletException, SQLException {
261 httpServletRequest = Mockito.mock(HttpServletRequest.class);
262 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
263 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
264 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
265 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
266 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
267 Map<String, String> ruleAttributes = new HashMap<>();
268 ruleAttributes.put("value", "test");
269 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
270 .configPolicyType("BRMS_Raw")
272 .description("testig description")
273 .configName("BRMS_RAW_RULE")
276 .dynamicFieldConfigAttributes(ruleAttributes)
279 .configBodyData("test")
284 MockServletInputStream mockInput =
285 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
286 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
291 pap.service(httpServletRequest, httpServletResponse);
293 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
294 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
295 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_BRMS_Raw_test.1.xml");
299 public void testClosedLoopPMCreatePolicy() throws IOException, ServletException, SQLException {
300 httpServletRequest = Mockito.mock(HttpServletRequest.class);
301 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
302 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
303 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
304 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
305 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
306 String json = "{\"test\":\"java\"}";
307 //Creating CloseLoop_Fault and Performance Metric Policies
308 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
309 .configPolicyType("ClosedLoop_PM")
311 .description("testing")
315 .oldPolicyFileName(null)
316 .serviceType("Registration Failure(Trinity)")
325 MockServletInputStream mockInput =
326 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
327 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
332 pap.service(httpServletRequest, httpServletResponse);
334 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
335 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
336 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_PM_test.1.xml");
340 public void testDecisonAAFPolicy() throws IOException, ServletException, SQLException {
341 httpServletRequest = Mockito.mock(HttpServletRequest.class);
342 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
343 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
344 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
345 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
346 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
347 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
349 .description("test rule")
351 .providerComboBox("AAF")
356 MockServletInputStream mockInput =
357 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
358 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
362 pap.service(httpServletRequest, httpServletResponse);
364 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
365 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
366 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_test.1.xml");
370 public void testDecisonGuardPolicy() throws IOException, ServletException, SQLException {
371 httpServletRequest = Mockito.mock(HttpServletRequest.class);
372 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
373 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
374 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
375 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
376 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
377 Map<String, String> matchingAttributes = new HashMap<>();
378 matchingAttributes.put("actor", "test");
379 matchingAttributes.put("recipe", "restart");
380 matchingAttributes.put("targets", "test,test1");
381 matchingAttributes.put("clname", "");
382 matchingAttributes.put("limit", "1");
383 matchingAttributes.put("timeWindow", "15");
384 matchingAttributes.put("timeUnits", "minute");
385 matchingAttributes.put("guardActiveStart", "05:00");
386 matchingAttributes.put("guardActiveEnd", "10:00");
387 StdPAPPolicy newPAPPolicy =
389 new StdPAPPolicy(StdPAPPolicyParams.builder()
390 .policyName("testGuard")
391 .description("test rule")
393 .providerComboBox("GUARD_YAML")
394 .dynamicFieldConfigAttributes(matchingAttributes)
399 MockServletInputStream mockInput =
400 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
401 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
405 pap.service(httpServletRequest, httpServletResponse);
407 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
408 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
409 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testGuard.1.xml");
413 public void testDecisonBLGuardPolicy() throws IOException, ServletException, SQLException {
414 httpServletRequest = Mockito.mock(HttpServletRequest.class);
415 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
416 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
417 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
418 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
419 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
420 Map<String, String> matchingAttributes = new HashMap<>();
421 matchingAttributes.put("actor", "test");
422 matchingAttributes.put("recipe", "restart");
423 matchingAttributes.put("clname", "test");
424 matchingAttributes.put("guardActiveStart", "05:00");
425 matchingAttributes.put("guardActiveEnd", "10:00");
426 matchingAttributes.put("blackList", "bl1,bl2");
427 StdPAPPolicy newPAPPolicy =
428 new StdPAPPolicy(StdPAPPolicyParams.builder()
429 .policyName("testblGuard")
430 .description("test rule")
432 .providerComboBox("GUARD_BL_YAML")
433 .dynamicFieldConfigAttributes(matchingAttributes)
438 MockServletInputStream mockInput =
439 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
440 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
444 pap.service(httpServletRequest, httpServletResponse);
446 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
447 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
448 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testblGuard.1.xml");
452 public void testConfigPolicy() throws IOException, ServletException, SQLException {
453 httpServletRequest = Mockito.mock(HttpServletRequest.class);
454 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
455 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
456 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
457 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
458 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
459 Map<String, String> configAttributes = new HashMap<>();
460 configAttributes.put("value", "test");
461 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
462 .configPolicyType("Base")
464 .description("test rule")
466 .configName("config")
467 .dynamicFieldConfigAttributes(configAttributes)
469 .configBodyData("test body")
476 .ttlDate(null).build());
477 MockServletInputStream mockInput =
478 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
479 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
483 pap.service(httpServletRequest, httpServletResponse);
485 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
486 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
487 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_test.1.xml");
490 private void setPolicyCreation() {
491 CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class);
492 PolicyCreation.setCommonClassDao(commonClassDao);
493 PolicyEditorScopes editorScope = new PolicyEditorScopes();
494 UserInfo userInfo = new UserInfo();
495 userInfo.setUserName("API");
496 userInfo.setUserLoginId("API");
497 editorScope.setScopeName("test");
498 editorScope.setUserCreatedBy(userInfo);
499 editorScope.setUserModifiedBy(userInfo);
500 Mockito.when(commonClassDao.getEntityItem(PolicyEditorScopes.class, "scopeName", "test"))
501 .thenReturn(editorScope);
502 BRMSParamTemplate template = new BRMSParamTemplate();
503 template.setRuleName("testPolicy");
504 template.setUserCreatedBy(userInfo);
505 String rule = "package com.sample;\n"
506 + "import com.sample.DroolsTest.Message;\n"
511 + "///This Rule will be generated by the UI.\n"
512 + "rule \"${policyName}.Create parameters structure\"\n"
516 + "Params params = new Params();\n"
517 + "params.setSamPoll(76);\n"
518 + "params.setValue(\"test\");\n"
519 + "insertLogical(params);\n"
521 + "rule \"Rule 1: Check parameter structure access from when/then\"\n"
523 + "$param: Params()\n"
524 + "Params($param.samPoll > 50)\n"
526 + "System.out.println(\"Firing rule 1\");\n"
527 + "System.out.println($param);\n"
529 template.setRule(rule);
530 Mockito.when(commonClassDao.getEntityItem(BRMSParamTemplate.class, "ruleName", "testPolicy"))
531 .thenReturn(template);
536 public void testClosedLoopCreateDictionary() throws IOException, SQLException, ServletException {
537 httpServletRequest = Mockito.mock(HttpServletRequest.class);
539 String json = "{\"dictionaryFields\": {\"vsclaction\": \"testRestAPI\",\"description\": \"testing create\"}}";
540 dictionaryTestSetup(false, "VSCLAction", json);
542 ClosedLoopDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
543 // send Request to PAP
544 pap.service(httpServletRequest, httpServletResponse);
546 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
550 httpServletRequest = Mockito.mock(HttpServletRequest.class);
551 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
552 json = "{\"dictionaryFields\": {\"vnftype\": \"testrestAPI1\",\"description\": \"testing create\"}}";
553 dictionaryTestSetup(false, "VNFType", json);
554 // send Request to PAP
555 pap.service(httpServletRequest, httpServletResponse);
557 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
561 httpServletRequest = Mockito.mock(HttpServletRequest.class);
562 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
564 "{\"dictionaryFields\":{\"pepName\":\"testRestAPI\",\"description\":\"testing create\"," +
565 "\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
566 "\"number\":\"test\"}]}}";
567 dictionaryTestSetup(false, "PEPOptions", json);
568 // send Request to PAP
569 pap.service(httpServletRequest, httpServletResponse);
571 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
575 httpServletRequest = Mockito.mock(HttpServletRequest.class);
576 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
578 "{\"dictionaryFields\":{\"varbindName\":\"testRestAPI\",\"varbindDescription\":\"testing\"," +
579 "\"varbindOID\":\"test\"}}";
580 dictionaryTestSetup(false, "Varbind", json);
581 // send Request to PAP
582 pap.service(httpServletRequest, httpServletResponse);
584 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
588 httpServletRequest = Mockito.mock(HttpServletRequest.class);
589 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
590 json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPI\",\"description\":\"testing\"}}";
591 dictionaryTestSetup(false, "Service", json);
592 // send Request to PAP
593 pap.service(httpServletRequest, httpServletResponse);
595 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
599 httpServletRequest = Mockito.mock(HttpServletRequest.class);
600 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
601 json = "{\"dictionaryFields\":{\"siteName\":\"testRestAPI\",\"description\":\"testing\"}}";
602 dictionaryTestSetup(false, "Site", json);
603 // send Request to PAP
604 pap.service(httpServletRequest, httpServletResponse);
606 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
610 public void testFirewallCreateDictionary() throws IOException, SQLException, ServletException {
611 httpServletRequest = Mockito.mock(HttpServletRequest.class);
612 // Check SecurityZone.
613 String json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"testing\"}}";
614 dictionaryTestSetup(false, "SecurityZone", json);
616 FirewallDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
617 // send Request to PAP
618 pap.service(httpServletRequest, httpServletResponse);
620 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
624 httpServletRequest = Mockito.mock(HttpServletRequest.class);
625 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
626 json = "{\"dictionaryFields\":{\"actionName\":\"testRestAPI\",\"description\":\"test\"}}";
627 dictionaryTestSetup(false, "ActionList", json);
628 // send Request to PAP
629 pap.service(httpServletRequest, httpServletResponse);
631 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
633 // Check Protocol List.
635 httpServletRequest = Mockito.mock(HttpServletRequest.class);
636 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
637 json = "{\"dictionaryFields\":{\"protocolName\":\"testRestAPI\",\"description\":\"test\"}}";
638 dictionaryTestSetup(false, "ProtocolList", json);
639 // send Request to PAP
640 pap.service(httpServletRequest, httpServletResponse);
642 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
646 httpServletRequest = Mockito.mock(HttpServletRequest.class);
647 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
648 json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"test\"}}";
649 dictionaryTestSetup(false, "Zone", json);
650 // send Request to PAP
651 pap.service(httpServletRequest, httpServletResponse);
653 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
657 httpServletRequest = Mockito.mock(HttpServletRequest.class);
658 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
660 "{\"dictionaryFields\":{\"prefixListName\":\"testRestAPI\",\"prefixListValue\":\"127.0.0.1\"," +
661 "\"description\":\"testing\"}}";
662 dictionaryTestSetup(false, "PrefixList", json);
663 // send Request to PAP
664 pap.service(httpServletRequest, httpServletResponse);
666 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
668 // Check AddressGroup.
670 httpServletRequest = Mockito.mock(HttpServletRequest.class);
671 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
673 "{\"dictionaryFields\":{\"groupName\":\"testRestAPIgroup\",\"description\":\"testing\"," +
674 "\"attributes\":[{\"option\":\"testRestAPI\"}, {\"option\":\"testRestAPI\"}]}}";
675 dictionaryTestSetup(false, "AddressGroup", json);
676 // send Request to PAP
677 pap.service(httpServletRequest, httpServletResponse);
679 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
681 // Check ServiceGroup.
683 httpServletRequest = Mockito.mock(HttpServletRequest.class);
684 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
686 "{\"dictionaryFields\":{\"groupName\":\"testRestAPIServiceGroup\"," +
687 "\"attributes\":[{\"option\":\"testRestAPIservice\"}]}}";
688 dictionaryTestSetup(false, "ServiceGroup", json);
689 // send Request to PAP
690 pap.service(httpServletRequest, httpServletResponse);
692 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
694 // Check ServiceList.
696 httpServletRequest = Mockito.mock(HttpServletRequest.class);
697 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
699 "{\"dictionaryFields\":{\"serviceName\":\"testRestAPIservice\",\"serviceDescription\":\"test\"," +
700 "\"servicePorts\":\"8888\",\"transportProtocols\":[{\"option\":\"testRestAPI\"}," +
701 "{\"option\":\"testRestAPI1\"}],\"appProtocols\":[{\"option\":\"testRestAPI\"}," +
702 "{\"option\":\"testRestAPI1\"}]}}";
703 dictionaryTestSetup(false, "ServiceList", json);
704 // send Request to PAP
705 pap.service(httpServletRequest, httpServletResponse);
707 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
711 httpServletRequest = Mockito.mock(HttpServletRequest.class);
712 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
714 "{\"dictionaryFields\":{\"termName\":\"testRestAPIRule\",\"termDescription\":\"testing\"," +
715 "\"fromZoneDatas\":[{\"option\":\"testRestAPI\"}]," +
716 "\"toZoneDatas\":[{\"option\":\"testRestAPI1\"}]," +
717 "\"sourceListDatas\":[{\"option\":\"Group_testportal\"}]," +
718 "\"destinationListDatas\":[{\"option\":\"testRestAPI\"}]," +
719 "\"sourceServiceDatas\":[{\"option\":\"testRestAPIservice\"}," +
720 "{\"option\":\"testRestAPIservice1\"}]," +
721 "\"destinationServiceDatas\":[{\"option\":\"testRestAPIservice1\"}," +
722 "{\"option\":\"testportalservice2\"}],\"actionListDatas\":[{\"option\":\"testRestAPI\"}]}}";
723 dictionaryTestSetup(false, "TermList", json);
724 // send Request to PAP
725 pap.service(httpServletRequest, httpServletResponse);
727 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
731 public void testCommonCreateDictionary() throws IOException, SQLException, ServletException {
732 new DictionaryController(commonClassDao);
733 new ActionPolicyDictionaryController(commonClassDao);
734 new SafePolicyController(commonClassDao);
735 new DescriptiveDictionaryController(commonClassDao);
736 List<Object> object = new ArrayList<>();
737 object.add(new Category());
738 when(commonClassDao.getDataById(Category.class, "shortName", "resource")).thenReturn(object);
739 httpServletRequest = Mockito.mock(HttpServletRequest.class);
740 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
742 "{\"dictionaryFields\": {\"onapName\": \"testMMRestAPI1\",\"description\": \"testing update response " +
744 dictionaryTestSetup(false, "OnapName", json);
745 // send Request to PAP
746 pap.service(httpServletRequest, httpServletResponse);
748 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
750 httpServletRequest = Mockito.mock(HttpServletRequest.class);
751 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
753 "{\"dictionaryFields\": {\"xacmlId\": \"testMMRestAPI1\",\"datatypeBean\": {\"shortName\": " +
754 "\"string\"}, \"description\": \"testing update\",\"priority\": \"High\"," +
755 "\"userDataTypeValues\": [{\"attributeValues\": \"testAttr\"}, {\"attributeValues\": " +
756 "\"testAttr2\"}, {\"attributeValues\": \"testAttr3\"}]}}";
757 dictionaryTestSetup(false, "Attribute", json);
758 // send Request to PAP
759 pap.service(httpServletRequest, httpServletResponse);
761 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
764 httpServletRequest = Mockito.mock(HttpServletRequest.class);
765 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
767 "{\"dictionaryFields\":{\"attributeName\":\"TestMMrestAPI1\",\"type\":\"REST\",\"url\":\"testsomeurl" +
768 ".com\",\"method\":\"GET\",\"description\":\"test create\",\"body\":\"Testing Create\"," +
769 "\"headers\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
770 "\"number\":\"test\"}]}}";
771 dictionaryTestSetup(false, "Action", json);
772 // send Request to PAP
773 pap.service(httpServletRequest, httpServletResponse);
775 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
777 httpServletRequest = Mockito.mock(HttpServletRequest.class);
778 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
780 "{\"dictionaryFields\":{\"scopeName\":\"testMMRestAPI1\",\"description\":\"test\"," +
781 "\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
782 "\"number\":\"test\"}]}}";
783 dictionaryTestSetup(false, "DescriptiveScope", json);
784 // send Request to PAP
785 pap.service(httpServletRequest, httpServletResponse);
787 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
789 httpServletRequest = Mockito.mock(HttpServletRequest.class);
790 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
791 json = "{\"dictionaryFields\":{\"riskName\":\"testMMrestAPI1\",\"description\":\"test\"}}";
792 dictionaryTestSetup(false, "RiskType", json);
793 // send Request to PAP
794 pap.service(httpServletRequest, httpServletResponse);
796 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
798 httpServletRequest = Mockito.mock(HttpServletRequest.class);
799 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
801 "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"message\":\"test\"," +
802 "\"riskType\":\"testMMrestAPI1\"}}";
803 dictionaryTestSetup(false, "SafePolicyWarning", json);
804 // send Request to PAP
805 pap.service(httpServletRequest, httpServletResponse);
807 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
811 public void testDecisionCreateDictionary() throws IOException, SQLException, ServletException {
812 new DecisionPolicyDictionaryController(commonClassDao);
813 httpServletRequest = Mockito.mock(HttpServletRequest.class);
814 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
816 "{\"dictionaryFields\":{\"xacmlId\":\"testMMRestAPI1\",\"datatypeBean\":{\"shortName\":\"string\"}," +
817 "\"description\":\"test\",\"priority\":\"High\"}}";
818 dictionaryTestSetup(false, "Settings", 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);
827 "{\"dictionaryFields\":{\"bbid\":\"BB1\",\"workstep\":\"1\",\"treatments\":\"Manual Handling,Abort," +
829 dictionaryTestSetup(false, "RainyDayTreatments", json);
830 // send Request to PAP
831 pap.service(httpServletRequest, httpServletResponse);
833 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
837 public void testMSCreateDictionary() throws IOException, SQLException, ServletException {
838 new MicroServiceDictionaryController(commonClassDao);
839 new PolicyScopeDictionaryController(commonClassDao);
840 httpServletRequest = Mockito.mock(HttpServletRequest.class);
841 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
842 String json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
843 dictionaryTestSetup(false, "MicroServiceLocation", json);
844 // send Request to PAP
845 pap.service(httpServletRequest, httpServletResponse);
847 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
849 httpServletRequest = Mockito.mock(HttpServletRequest.class);
850 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
851 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
852 dictionaryTestSetup(false, "MicroServiceConfigName", json);
853 // send Request to PAP
854 pap.service(httpServletRequest, httpServletResponse);
856 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
858 httpServletRequest = Mockito.mock(HttpServletRequest.class);
859 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
860 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"description\":\"test\"}}";
861 dictionaryTestSetup(false, "DCAEUUID", json);
862 // send Request to PAP
863 pap.service(httpServletRequest, httpServletResponse);
865 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
867 httpServletRequest = Mockito.mock(HttpServletRequest.class);
868 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
869 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
870 dictionaryTestSetup(false, "PolicyScopeService", json);
871 // send Request to PAP
872 pap.service(httpServletRequest, httpServletResponse);
874 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
876 httpServletRequest = Mockito.mock(HttpServletRequest.class);
877 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
878 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
879 dictionaryTestSetup(false, "PolicyScopeResource", json);
880 // send Request to PAP
881 pap.service(httpServletRequest, httpServletResponse);
883 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
885 httpServletRequest = Mockito.mock(HttpServletRequest.class);
886 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
887 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
888 dictionaryTestSetup(false, "PolicyScopeType", json);
889 // send Request to PAP
890 pap.service(httpServletRequest, httpServletResponse);
892 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
894 httpServletRequest = Mockito.mock(HttpServletRequest.class);
895 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
896 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
897 dictionaryTestSetup(false, "PolicyScopeClosedLoop", json);
898 // send Request to PAP
899 pap.service(httpServletRequest, httpServletResponse);
901 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
903 httpServletRequest = Mockito.mock(HttpServletRequest.class);
904 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
906 "{\"dictionaryFields\":{\"groupName\":\"testMMrestAPI1\",\"description\":\"testing\"}," +
907 "\"groupPolicyScopeListData1\":{\"resource\":\"ANY\",\"type\":\"ANY\",\"service\":\"ANY\"," +
908 "\"closedloop\":\"ANY\"}}";
909 dictionaryTestSetup(false, "GroupPolicyScopeList", json);
910 // send Request to PAP
911 pap.service(httpServletRequest, httpServletResponse);
913 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
916 private void dictionaryTestSetup(Boolean updateFlag, String dictionaryType, String json)
917 throws IOException, SQLException {
918 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
919 Mockito.when(httpServletRequest.getHeader("ClientScope")).thenReturn("dictionaryItem");
920 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
921 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
923 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("update");
925 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
927 Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionaryType);
928 MockServletInputStream mockInput = new MockServletInputStream(json.getBytes());
929 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
930 Mockito.when(httpServletRequest.getReader()).thenReturn(new BufferedReader(new InputStreamReader(mockInput)));
935 public void setDBDao() throws SQLException {
936 BasicDataSource dataSource = new BasicDataSource();
937 dataSource.setDriverClassName("org.h2.Driver");
938 // In-memory DB for testing
939 dataSource.setUrl("jdbc:h2:mem:test");
940 dataSource.setUsername("sa");
941 dataSource.setPassword("");
942 LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
943 sessionBuilder.scanPackages("org.onap.*", "com.*");
945 Properties properties = new Properties();
946 properties.put("hibernate.show_sql", "false");
947 properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
948 properties.put("hibernate.hbm2ddl.auto", "drop");
949 properties.put("hibernate.hbm2ddl.auto", "create");
951 sessionBuilder.addProperties(properties);
952 sessionFactory = sessionBuilder.buildSessionFactory();
954 // Set up dao with SessionFactory
955 CommonClassDaoImpl.setSessionfactory(sessionFactory);
956 PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
960 public void getDictionary() throws ServletException, IOException {
961 String[] dictionarys = new String[]{"Attribute", "OnapName", "Action", "BRMSParamTemplate", "VSCLAction"
962 , "VNFType", "PEPOptions", "Varbind", "Service", "Site", "Settings", "RainyDayTreatments",
963 "DescriptiveScope", "ActionList", "ProtocolList", "Zone", "SecurityZone",
964 "PrefixList", "AddressGroup", "ServiceGroup", "ServiceList", "TermList",
965 "MicroServiceLocation", "MicroServiceConfigName", "DCAEUUID", "MicroServiceModels",
966 "PolicyScopeService", "PolicyScopeResource", "PolicyScopeType", "PolicyScopeClosedLoop",
967 "GroupPolicyScopeList", "RiskType", "SafePolicyWarning", "MicroServiceDictionary"};
968 for (String dictionary : dictionarys) {
969 httpServletRequest = Mockito.mock(HttpServletRequest.class);
970 httpServletResponse = new MockHttpServletResponse();
971 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
972 Mockito.when(httpServletRequest.getMethod()).thenReturn("GET");
973 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
974 Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionary);
975 pap.service(httpServletRequest, httpServletResponse);
976 assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
981 public void testDummy() throws ServletException, IOException {
983 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
984 mockOutput = Mockito.mock(ServletOutputStream.class);
987 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
988 } catch (IOException e) {
993 pap.service(httpServletRequest, httpServletResponse);
995 } catch (Exception e) {
1001 public void destroy() {
1002 if (sessionFactory != null) {
1003 sessionFactory.close();