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(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 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Param", "test", "testing",
226 "BRMS_PARAM_RULE", false, "test",
227 matchingAttributes, 0, "DROOLS",
228 null, ruleAttributes, "5",
229 "default", "false", "", null, null);
230 MockServletInputStream mockInput =
231 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
232 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
237 pap.service(httpServletRequest, httpServletResponse);
239 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
240 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
241 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_BRMS_Param_test.1.xml");
245 public void testBRMSRawCreatePolicy() throws IOException, ServletException, SQLException {
246 httpServletRequest = Mockito.mock(HttpServletRequest.class);
247 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
248 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
249 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
250 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
251 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
252 Map<String, String> ruleAttributes = new HashMap<>();
253 ruleAttributes.put("value", "test");
254 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
255 .configPolicyType("BRMS_Raw")
257 .description("testig description")
258 .configName("BRMS_RAW_RULE")
261 .dynamicFieldConfigAttributes(ruleAttributes)
264 .configBodyData("test")
269 MockServletInputStream mockInput =
270 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
271 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
276 pap.service(httpServletRequest, httpServletResponse);
278 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
279 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
280 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_BRMS_Raw_test.1.xml");
284 public void testClosedLoopPMCreatePolicy() throws IOException, ServletException, SQLException {
285 httpServletRequest = Mockito.mock(HttpServletRequest.class);
286 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
287 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
288 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
289 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
290 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
291 String json = "{\"test\":\"java\"}";
292 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("ClosedLoop_PM", "test", "testing", "onap",
293 json, false, null, "Registration Failure(Trinity)", false, "test", 0, null,
294 "default", "true", "");
295 MockServletInputStream mockInput =
296 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
297 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
302 pap.service(httpServletRequest, httpServletResponse);
304 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
305 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
306 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_PM_test.1.xml");
310 public void testDecisonAAFPolicy() throws IOException, ServletException, SQLException {
311 httpServletRequest = Mockito.mock(HttpServletRequest.class);
312 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
313 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
314 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
315 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
316 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
317 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
319 .description("test rule")
321 .providerComboBox("AAF")
326 MockServletInputStream mockInput =
327 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
328 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.Decision_test.1.xml");
340 public void testDecisonGuardPolicy() 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 Map<String, String> matchingAttributes = new HashMap<>();
348 matchingAttributes.put("actor", "test");
349 matchingAttributes.put("recipe", "restart");
350 matchingAttributes.put("targets", "test,test1");
351 matchingAttributes.put("clname", "");
352 matchingAttributes.put("limit", "1");
353 matchingAttributes.put("timeWindow", "15");
354 matchingAttributes.put("timeUnits", "minute");
355 matchingAttributes.put("guardActiveStart", "05:00");
356 matchingAttributes.put("guardActiveEnd", "10:00");
357 StdPAPPolicy newPAPPolicy =
359 new StdPAPPolicy(StdPAPPolicyParams.builder()
360 .policyName("testGuard")
361 .description("test rule")
363 .providerComboBox("GUARD_YAML")
364 .dynamicFieldConfigAttributes(matchingAttributes)
369 MockServletInputStream mockInput =
370 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
371 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
375 pap.service(httpServletRequest, httpServletResponse);
377 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
378 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
379 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testGuard.1.xml");
383 public void testDecisonBLGuardPolicy() throws IOException, ServletException, SQLException {
384 httpServletRequest = Mockito.mock(HttpServletRequest.class);
385 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
386 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
387 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
388 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
389 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
390 Map<String, String> matchingAttributes = new HashMap<>();
391 matchingAttributes.put("actor", "test");
392 matchingAttributes.put("recipe", "restart");
393 matchingAttributes.put("clname", "test");
394 matchingAttributes.put("guardActiveStart", "05:00");
395 matchingAttributes.put("guardActiveEnd", "10:00");
396 matchingAttributes.put("blackList", "bl1,bl2");
397 StdPAPPolicy newPAPPolicy =
398 new StdPAPPolicy(StdPAPPolicyParams.builder()
399 .policyName("testblGuard")
400 .description("test rule")
402 .providerComboBox("GUARD_BL_YAML")
403 .dynamicFieldConfigAttributes(matchingAttributes)
408 MockServletInputStream mockInput =
409 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
410 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
414 pap.service(httpServletRequest, httpServletResponse);
416 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
417 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
418 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testblGuard.1.xml");
422 public void testConfigPolicy() throws IOException, ServletException, SQLException {
423 httpServletRequest = Mockito.mock(HttpServletRequest.class);
424 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
425 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
426 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
427 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
428 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
429 Map<String, String> configAttributes = new HashMap<>();
430 configAttributes.put("value", "test");
431 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
432 .configPolicyType("Base")
434 .description("test rule")
436 .configName("config")
437 .dynamicFieldConfigAttributes(configAttributes)
439 .configBodyData("test body")
446 .ttlDate(null).build());
447 MockServletInputStream mockInput =
448 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
449 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
453 pap.service(httpServletRequest, httpServletResponse);
455 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
456 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
457 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_test.1.xml");
460 private void setPolicyCreation() {
461 CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class);
462 PolicyCreation.setCommonClassDao(commonClassDao);
463 PolicyEditorScopes editorScope = new PolicyEditorScopes();
464 UserInfo userInfo = new UserInfo();
465 userInfo.setUserName("API");
466 userInfo.setUserLoginId("API");
467 editorScope.setScopeName("test");
468 editorScope.setUserCreatedBy(userInfo);
469 editorScope.setUserModifiedBy(userInfo);
470 Mockito.when(commonClassDao.getEntityItem(PolicyEditorScopes.class, "scopeName", "test"))
471 .thenReturn(editorScope);
472 BRMSParamTemplate template = new BRMSParamTemplate();
473 template.setRuleName("testPolicy");
474 template.setUserCreatedBy(userInfo);
475 String rule = "package com.sample;\n"
476 + "import com.sample.DroolsTest.Message;\n"
481 + "///This Rule will be generated by the UI.\n"
482 + "rule \"${policyName}.Create parameters structure\"\n"
486 + "Params params = new Params();\n"
487 + "params.setSamPoll(76);\n"
488 + "params.setValue(\"test\");\n"
489 + "insertLogical(params);\n"
491 + "rule \"Rule 1: Check parameter structure access from when/then\"\n"
493 + "$param: Params()\n"
494 + "Params($param.samPoll > 50)\n"
496 + "System.out.println(\"Firing rule 1\");\n"
497 + "System.out.println($param);\n"
499 template.setRule(rule);
500 Mockito.when(commonClassDao.getEntityItem(BRMSParamTemplate.class, "ruleName", "testPolicy"))
501 .thenReturn(template);
506 public void testClosedLoopCreateDictionary() throws IOException, SQLException, ServletException {
507 httpServletRequest = Mockito.mock(HttpServletRequest.class);
509 String json = "{\"dictionaryFields\": {\"vsclaction\": \"testRestAPI\",\"description\": \"testing create\"}}";
510 dictionaryTestSetup(false, "VSCLAction", json);
512 ClosedLoopDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
513 // send Request to PAP
514 pap.service(httpServletRequest, httpServletResponse);
516 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
520 httpServletRequest = Mockito.mock(HttpServletRequest.class);
521 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
522 json = "{\"dictionaryFields\": {\"vnftype\": \"testrestAPI1\",\"description\": \"testing create\"}}";
523 dictionaryTestSetup(false, "VNFType", json);
524 // send Request to PAP
525 pap.service(httpServletRequest, httpServletResponse);
527 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
531 httpServletRequest = Mockito.mock(HttpServletRequest.class);
532 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
534 "{\"dictionaryFields\":{\"pepName\":\"testRestAPI\",\"description\":\"testing create\"," +
535 "\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
536 "\"number\":\"test\"}]}}";
537 dictionaryTestSetup(false, "PEPOptions", json);
538 // send Request to PAP
539 pap.service(httpServletRequest, httpServletResponse);
541 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
545 httpServletRequest = Mockito.mock(HttpServletRequest.class);
546 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
548 "{\"dictionaryFields\":{\"varbindName\":\"testRestAPI\",\"varbindDescription\":\"testing\"," +
549 "\"varbindOID\":\"test\"}}";
550 dictionaryTestSetup(false, "Varbind", json);
551 // send Request to PAP
552 pap.service(httpServletRequest, httpServletResponse);
554 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
558 httpServletRequest = Mockito.mock(HttpServletRequest.class);
559 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
560 json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPI\",\"description\":\"testing\"}}";
561 dictionaryTestSetup(false, "Service", json);
562 // send Request to PAP
563 pap.service(httpServletRequest, httpServletResponse);
565 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
569 httpServletRequest = Mockito.mock(HttpServletRequest.class);
570 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
571 json = "{\"dictionaryFields\":{\"siteName\":\"testRestAPI\",\"description\":\"testing\"}}";
572 dictionaryTestSetup(false, "Site", json);
573 // send Request to PAP
574 pap.service(httpServletRequest, httpServletResponse);
576 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
580 public void testFirewallCreateDictionary() throws IOException, SQLException, ServletException {
581 httpServletRequest = Mockito.mock(HttpServletRequest.class);
582 // Check SecurityZone.
583 String json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"testing\"}}";
584 dictionaryTestSetup(false, "SecurityZone", json);
586 FirewallDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
587 // send Request to PAP
588 pap.service(httpServletRequest, httpServletResponse);
590 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
594 httpServletRequest = Mockito.mock(HttpServletRequest.class);
595 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
596 json = "{\"dictionaryFields\":{\"actionName\":\"testRestAPI\",\"description\":\"test\"}}";
597 dictionaryTestSetup(false, "ActionList", json);
598 // send Request to PAP
599 pap.service(httpServletRequest, httpServletResponse);
601 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
603 // Check Protocol List.
605 httpServletRequest = Mockito.mock(HttpServletRequest.class);
606 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
607 json = "{\"dictionaryFields\":{\"protocolName\":\"testRestAPI\",\"description\":\"test\"}}";
608 dictionaryTestSetup(false, "ProtocolList", json);
609 // send Request to PAP
610 pap.service(httpServletRequest, httpServletResponse);
612 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
616 httpServletRequest = Mockito.mock(HttpServletRequest.class);
617 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
618 json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"test\"}}";
619 dictionaryTestSetup(false, "Zone", json);
620 // send Request to PAP
621 pap.service(httpServletRequest, httpServletResponse);
623 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
627 httpServletRequest = Mockito.mock(HttpServletRequest.class);
628 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
630 "{\"dictionaryFields\":{\"prefixListName\":\"testRestAPI\",\"prefixListValue\":\"127.0.0.1\"," +
631 "\"description\":\"testing\"}}";
632 dictionaryTestSetup(false, "PrefixList", json);
633 // send Request to PAP
634 pap.service(httpServletRequest, httpServletResponse);
636 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
638 // Check AddressGroup.
640 httpServletRequest = Mockito.mock(HttpServletRequest.class);
641 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
643 "{\"dictionaryFields\":{\"groupName\":\"testRestAPIgroup\",\"description\":\"testing\"," +
644 "\"attributes\":[{\"option\":\"testRestAPI\"}, {\"option\":\"testRestAPI\"}]}}";
645 dictionaryTestSetup(false, "AddressGroup", json);
646 // send Request to PAP
647 pap.service(httpServletRequest, httpServletResponse);
649 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
651 // Check ServiceGroup.
653 httpServletRequest = Mockito.mock(HttpServletRequest.class);
654 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
656 "{\"dictionaryFields\":{\"groupName\":\"testRestAPIServiceGroup\"," +
657 "\"attributes\":[{\"option\":\"testRestAPIservice\"}]}}";
658 dictionaryTestSetup(false, "ServiceGroup", json);
659 // send Request to PAP
660 pap.service(httpServletRequest, httpServletResponse);
662 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
664 // Check ServiceList.
666 httpServletRequest = Mockito.mock(HttpServletRequest.class);
667 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
669 "{\"dictionaryFields\":{\"serviceName\":\"testRestAPIservice\",\"serviceDescription\":\"test\"," +
670 "\"servicePorts\":\"8888\",\"transportProtocols\":[{\"option\":\"testRestAPI\"}," +
671 "{\"option\":\"testRestAPI1\"}],\"appProtocols\":[{\"option\":\"testRestAPI\"}," +
672 "{\"option\":\"testRestAPI1\"}]}}";
673 dictionaryTestSetup(false, "ServiceList", json);
674 // send Request to PAP
675 pap.service(httpServletRequest, httpServletResponse);
677 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
681 httpServletRequest = Mockito.mock(HttpServletRequest.class);
682 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
684 "{\"dictionaryFields\":{\"termName\":\"testRestAPIRule\",\"termDescription\":\"testing\"," +
685 "\"fromZoneDatas\":[{\"option\":\"testRestAPI\"}]," +
686 "\"toZoneDatas\":[{\"option\":\"testRestAPI1\"}]," +
687 "\"sourceListDatas\":[{\"option\":\"Group_testportal\"}]," +
688 "\"destinationListDatas\":[{\"option\":\"testRestAPI\"}]," +
689 "\"sourceServiceDatas\":[{\"option\":\"testRestAPIservice\"}," +
690 "{\"option\":\"testRestAPIservice1\"}]," +
691 "\"destinationServiceDatas\":[{\"option\":\"testRestAPIservice1\"}," +
692 "{\"option\":\"testportalservice2\"}],\"actionListDatas\":[{\"option\":\"testRestAPI\"}]}}";
693 dictionaryTestSetup(false, "TermList", json);
694 // send Request to PAP
695 pap.service(httpServletRequest, httpServletResponse);
697 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
701 public void testCommonCreateDictionary() throws IOException, SQLException, ServletException {
702 new DictionaryController(commonClassDao);
703 new ActionPolicyDictionaryController(commonClassDao);
704 new SafePolicyController(commonClassDao);
705 new DescriptiveDictionaryController(commonClassDao);
706 List<Object> object = new ArrayList<>();
707 object.add(new Category());
708 when(commonClassDao.getDataById(Category.class, "shortName", "resource")).thenReturn(object);
709 httpServletRequest = Mockito.mock(HttpServletRequest.class);
710 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
712 "{\"dictionaryFields\": {\"onapName\": \"testMMRestAPI1\",\"description\": \"testing update response " +
714 dictionaryTestSetup(false, "OnapName", json);
715 // send Request to PAP
716 pap.service(httpServletRequest, httpServletResponse);
718 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
720 httpServletRequest = Mockito.mock(HttpServletRequest.class);
721 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
723 "{\"dictionaryFields\": {\"xacmlId\": \"testMMRestAPI1\",\"datatypeBean\": {\"shortName\": " +
724 "\"string\"}, \"description\": \"testing update\",\"priority\": \"High\"," +
725 "\"userDataTypeValues\": [{\"attributeValues\": \"testAttr\"}, {\"attributeValues\": " +
726 "\"testAttr2\"}, {\"attributeValues\": \"testAttr3\"}]}}";
727 dictionaryTestSetup(false, "Attribute", json);
728 // send Request to PAP
729 pap.service(httpServletRequest, httpServletResponse);
731 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
734 httpServletRequest = Mockito.mock(HttpServletRequest.class);
735 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
737 "{\"dictionaryFields\":{\"attributeName\":\"TestMMrestAPI1\",\"type\":\"REST\",\"url\":\"testsomeurl" +
738 ".com\",\"method\":\"GET\",\"description\":\"test create\",\"body\":\"Testing Create\"," +
739 "\"headers\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
740 "\"number\":\"test\"}]}}";
741 dictionaryTestSetup(false, "Action", json);
742 // send Request to PAP
743 pap.service(httpServletRequest, httpServletResponse);
745 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
747 httpServletRequest = Mockito.mock(HttpServletRequest.class);
748 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
750 "{\"dictionaryFields\":{\"scopeName\":\"testMMRestAPI1\",\"description\":\"test\"," +
751 "\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
752 "\"number\":\"test\"}]}}";
753 dictionaryTestSetup(false, "DescriptiveScope", json);
754 // send Request to PAP
755 pap.service(httpServletRequest, httpServletResponse);
757 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
759 httpServletRequest = Mockito.mock(HttpServletRequest.class);
760 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
761 json = "{\"dictionaryFields\":{\"riskName\":\"testMMrestAPI1\",\"description\":\"test\"}}";
762 dictionaryTestSetup(false, "RiskType", json);
763 // send Request to PAP
764 pap.service(httpServletRequest, httpServletResponse);
766 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
768 httpServletRequest = Mockito.mock(HttpServletRequest.class);
769 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
771 "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"message\":\"test\"," +
772 "\"riskType\":\"testMMrestAPI1\"}}";
773 dictionaryTestSetup(false, "SafePolicyWarning", json);
774 // send Request to PAP
775 pap.service(httpServletRequest, httpServletResponse);
777 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
781 public void testDecisionCreateDictionary() throws IOException, SQLException, ServletException {
782 new DecisionPolicyDictionaryController(commonClassDao);
783 httpServletRequest = Mockito.mock(HttpServletRequest.class);
784 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
786 "{\"dictionaryFields\":{\"xacmlId\":\"testMMRestAPI1\",\"datatypeBean\":{\"shortName\":\"string\"}," +
787 "\"description\":\"test\",\"priority\":\"High\"}}";
788 dictionaryTestSetup(false, "Settings", json);
789 // send Request to PAP
790 pap.service(httpServletRequest, httpServletResponse);
792 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
794 httpServletRequest = Mockito.mock(HttpServletRequest.class);
795 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
797 "{\"dictionaryFields\":{\"bbid\":\"BB1\",\"workstep\":\"1\",\"treatments\":\"Manual Handling,Abort," +
799 dictionaryTestSetup(false, "RainyDayTreatments", json);
800 // send Request to PAP
801 pap.service(httpServletRequest, httpServletResponse);
803 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
807 public void testMSCreateDictionary() throws IOException, SQLException, ServletException {
808 new MicroServiceDictionaryController(commonClassDao);
809 new PolicyScopeDictionaryController(commonClassDao);
810 httpServletRequest = Mockito.mock(HttpServletRequest.class);
811 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
812 String json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
813 dictionaryTestSetup(false, "MicroServiceLocation", json);
814 // send Request to PAP
815 pap.service(httpServletRequest, httpServletResponse);
817 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
819 httpServletRequest = Mockito.mock(HttpServletRequest.class);
820 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
821 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
822 dictionaryTestSetup(false, "MicroServiceConfigName", json);
823 // send Request to PAP
824 pap.service(httpServletRequest, httpServletResponse);
826 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
828 httpServletRequest = Mockito.mock(HttpServletRequest.class);
829 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
830 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"description\":\"test\"}}";
831 dictionaryTestSetup(false, "DCAEUUID", json);
832 // send Request to PAP
833 pap.service(httpServletRequest, httpServletResponse);
835 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
837 httpServletRequest = Mockito.mock(HttpServletRequest.class);
838 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
839 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
840 dictionaryTestSetup(false, "PolicyScopeService", json);
841 // send Request to PAP
842 pap.service(httpServletRequest, httpServletResponse);
844 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
846 httpServletRequest = Mockito.mock(HttpServletRequest.class);
847 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
848 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
849 dictionaryTestSetup(false, "PolicyScopeResource", json);
850 // send Request to PAP
851 pap.service(httpServletRequest, httpServletResponse);
853 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
855 httpServletRequest = Mockito.mock(HttpServletRequest.class);
856 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
857 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
858 dictionaryTestSetup(false, "PolicyScopeType", json);
859 // send Request to PAP
860 pap.service(httpServletRequest, httpServletResponse);
862 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
864 httpServletRequest = Mockito.mock(HttpServletRequest.class);
865 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
866 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
867 dictionaryTestSetup(false, "PolicyScopeClosedLoop", json);
868 // send Request to PAP
869 pap.service(httpServletRequest, httpServletResponse);
871 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
873 httpServletRequest = Mockito.mock(HttpServletRequest.class);
874 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
876 "{\"dictionaryFields\":{\"groupName\":\"testMMrestAPI1\",\"description\":\"testing\"}," +
877 "\"groupPolicyScopeListData1\":{\"resource\":\"ANY\",\"type\":\"ANY\",\"service\":\"ANY\"," +
878 "\"closedloop\":\"ANY\"}}";
879 dictionaryTestSetup(false, "GroupPolicyScopeList", json);
880 // send Request to PAP
881 pap.service(httpServletRequest, httpServletResponse);
883 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
886 private void dictionaryTestSetup(Boolean updateFlag, String dictionaryType, String json)
887 throws IOException, SQLException {
888 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
889 Mockito.when(httpServletRequest.getHeader("ClientScope")).thenReturn("dictionaryItem");
890 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
891 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
893 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("update");
895 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
897 Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionaryType);
898 MockServletInputStream mockInput = new MockServletInputStream(json.getBytes());
899 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
900 Mockito.when(httpServletRequest.getReader()).thenReturn(new BufferedReader(new InputStreamReader(mockInput)));
905 public void setDBDao() throws SQLException {
906 BasicDataSource dataSource = new BasicDataSource();
907 dataSource.setDriverClassName("org.h2.Driver");
908 // In-memory DB for testing
909 dataSource.setUrl("jdbc:h2:mem:test");
910 dataSource.setUsername("sa");
911 dataSource.setPassword("");
912 LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
913 sessionBuilder.scanPackages("org.onap.*", "com.*");
915 Properties properties = new Properties();
916 properties.put("hibernate.show_sql", "false");
917 properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
918 properties.put("hibernate.hbm2ddl.auto", "drop");
919 properties.put("hibernate.hbm2ddl.auto", "create");
921 sessionBuilder.addProperties(properties);
922 sessionFactory = sessionBuilder.buildSessionFactory();
924 // Set up dao with SessionFactory
925 CommonClassDaoImpl.setSessionfactory(sessionFactory);
926 PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
930 public void getDictionary() throws ServletException, IOException {
931 String[] dictionarys = new String[]{"Attribute", "OnapName", "Action", "BRMSParamTemplate", "VSCLAction"
932 , "VNFType", "PEPOptions", "Varbind", "Service", "Site", "Settings", "RainyDayTreatments",
933 "DescriptiveScope", "ActionList", "ProtocolList", "Zone", "SecurityZone",
934 "PrefixList", "AddressGroup", "ServiceGroup", "ServiceList", "TermList",
935 "MicroServiceLocation", "MicroServiceConfigName", "DCAEUUID", "MicroServiceModels",
936 "PolicyScopeService", "PolicyScopeResource", "PolicyScopeType", "PolicyScopeClosedLoop",
937 "GroupPolicyScopeList", "RiskType", "SafePolicyWarning", "MicroServiceDictionary"};
938 for (String dictionary : dictionarys) {
939 httpServletRequest = Mockito.mock(HttpServletRequest.class);
940 httpServletResponse = new MockHttpServletResponse();
941 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
942 Mockito.when(httpServletRequest.getMethod()).thenReturn("GET");
943 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
944 Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionary);
945 pap.service(httpServletRequest, httpServletResponse);
946 assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
951 public void testDummy() throws ServletException, IOException {
953 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
954 mockOutput = Mockito.mock(ServletOutputStream.class);
957 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
958 } catch (IOException e) {
963 pap.service(httpServletRequest, httpServletResponse);
965 } catch (Exception e) {
971 public void destroy() {
972 if (sessionFactory != null) {
973 sessionFactory.close();