2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.test;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import static org.mockito.Mockito.when;
27 import java.io.BufferedReader;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import java.sql.SQLException;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.List;
36 import java.util.Properties;
38 import javax.persistence.EntityManager;
39 import javax.persistence.EntityManagerFactory;
40 import javax.persistence.EntityTransaction;
41 import javax.persistence.Persistence;
42 import javax.servlet.ServletConfig;
43 import javax.servlet.ServletException;
44 import javax.servlet.ServletOutputStream;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
48 import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50 import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
51 import org.hibernate.SessionFactory;
52 import org.junit.After;
53 import org.junit.Before;
54 import org.junit.Test;
55 import org.mockito.Mockito;
56 import org.onap.policy.common.ia.IntegrityAuditProperties;
57 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
58 import org.onap.policy.pap.xacml.rest.controller.ActionPolicyDictionaryController;
59 import org.onap.policy.pap.xacml.rest.controller.ClosedLoopDictionaryController;
60 import org.onap.policy.pap.xacml.rest.controller.DecisionPolicyDictionaryController;
61 import org.onap.policy.pap.xacml.rest.controller.DescriptiveDictionaryController;
62 import org.onap.policy.pap.xacml.rest.controller.DictionaryController;
63 import org.onap.policy.pap.xacml.rest.controller.FirewallDictionaryController;
64 import org.onap.policy.pap.xacml.rest.controller.MicroServiceDictionaryController;
65 import org.onap.policy.pap.xacml.rest.controller.PolicyScopeDictionaryController;
66 import org.onap.policy.pap.xacml.rest.controller.SafePolicyController;
67 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
68 import org.onap.policy.pap.xacml.rest.policycontroller.PolicyCreation;
69 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
70 import org.onap.policy.rest.dao.CommonClassDao;
71 import org.onap.policy.rest.jpa.BRMSParamTemplate;
72 import org.onap.policy.rest.jpa.Category;
73 import org.onap.policy.rest.jpa.PolicyEditorScopes;
74 import org.onap.policy.rest.jpa.UserInfo;
75 import org.onap.policy.utils.PolicyUtils;
76 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
77 import org.onap.policy.xacml.std.pap.StdPAPPolicyParams;
78 import org.springframework.mock.web.MockHttpServletResponse;
79 import org.springframework.mock.web.MockServletConfig;
80 import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
82 import com.mockrunner.mock.web.MockServletInputStream;
84 public class XACMLPAPTest {
85 private static final Log logger = LogFactory.getLog(XACMLPAPTest.class);
87 private static final String ENVIRONMENT_HEADER = "Environment";
88 private List<String> headers = new ArrayList<>();
89 private HttpServletRequest httpServletRequest;
90 private HttpServletResponse httpServletResponse;
91 private ServletOutputStream mockOutput;
92 private ServletConfig servletConfig;
93 private XACMLPapServlet pap;
94 private SessionFactory sessionFactory;
95 private CommonClassDao commonClassDao;
97 private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";
98 private static final String DEFAULT_DB_USER = "sa";
99 private static final String DEFAULT_DB_PWD = "";
102 public void setUpDB() throws Exception {
103 logger.info("setUpDB: Entering");
105 Properties properties = new Properties();
106 properties.put(IntegrityAuditProperties.DB_DRIVER, XACMLPAPTest.DEFAULT_DB_DRIVER);
107 properties.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/xacmlTest");
108 properties.put(IntegrityAuditProperties.DB_USER, XACMLPAPTest.DEFAULT_DB_USER);
109 properties.put(IntegrityAuditProperties.DB_PWD, XACMLPAPTest.DEFAULT_DB_PWD);
110 properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
111 properties.put(IntegrityAuditProperties.NODE_TYPE, "pap");
113 //Clean the iaTest DB table for IntegrityAuditEntity entries
114 cleanDb("testPapPU", properties);
116 logger.info("setUpDB: Exiting");
119 public void cleanDb(String persistenceUnit, Properties properties) {
120 logger.debug("cleanDb: enter");
122 EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties);
124 EntityManager em = emf.createEntityManager();
125 // Start a transaction
126 EntityTransaction et = em.getTransaction();
131 em.createQuery("Delete from IntegrityAuditEntity").executeUpdate();
133 // commit transaction
136 logger.debug("cleanDb: exit");
140 public void setUp() throws ServletException {
141 httpServletRequest = Mockito.mock(HttpServletRequest.class);
142 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
143 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
144 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
146 servletConfig = Mockito.mock(MockServletConfig.class);
147 System.setProperty("com.sun.management.jmxremote.port", "9993");
148 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
149 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
150 .thenReturn("src/test/resources/xacml.pap.properties");
151 pap = new XACMLPapServlet();
152 pap.init(servletConfig);
153 commonClassDao = Mockito.mock(CommonClassDao.class);
154 new DictionaryUtils(commonClassDao);
155 DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
156 Mockito.mock(DictionaryUtils.class);
160 public void testFirwallCreatePolicy() throws IOException, ServletException, SQLException {
161 httpServletRequest = Mockito.mock(HttpServletRequest.class);
163 "{\"serviceTypeId\":\"/v0/firewall/pan\",\"configName\":\"TestFwPolicyConfig\"," +
164 "\"deploymentOption\":{\"deployNow\":false},\"securityZoneId\":\"cloudsite:dev1a\"," +
165 "\"serviceGroups\":[{\"name\":\"SSH\",\"description\":\"Sshservice entry in servicelist\"," +
166 "\"type\":\"SERVICE\",\"transportProtocol\":\"tcp\",\"appProtocol\":null,\"ports\":\"22\"}]," +
167 "\"addressGroups\":[{\"name\":\"test\",\"description\":\"Destination\"," +
168 "\"members\":[{\"type\":\"SUBNET\",\"value\":\"127.0.0.1/12\"}]},{\"name\":\"TestServers\"," +
169 "\"description\":\"SourceTestServers for firsttesting\",\"members\":[{\"type\":\"SUBNET\"," +
170 "\"value\":\"127.0.0.1/23\"}]}],\"firewallRuleList\":[{\"position\":\"1\"," +
171 "\"ruleName\":\"FWRuleTestServerToTest\",\"fromZones\":[\"UntrustedZoneTestName\"]," +
172 "\"toZones\":[\"TrustedZoneTestName\"],\"negateSource\":false,\"negateDestination\":false," +
173 "\"sourceList\":[{\"type\":\"REFERENCE\",\"name\":\"TestServers\"}]," +
174 "\"destinationList\":[{\"type\":\"REFERENCE\",\"name\":\"Test\"}],\"sourceServices\":[]," +
175 "\"destServices\":[{\"type\":\"REFERENCE\",\"name\":\"SSH\"}],\"action\":\"accept\"," +
176 "\"description\":\"FWrule for Test source to Test destination\",\"enabled\":true," +
178 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
179 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
180 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
181 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
182 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
183 StdPAPPolicy newPAPPolicy =
184 new StdPAPPolicy("Firewall Config", "test", "testDescription", "Test", false, "test", json, 0,
185 "5", "default", "false", "");
186 MockServletInputStream mockInput =
187 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
188 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
192 pap.service(httpServletRequest, httpServletResponse);
194 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
195 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
196 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_FW_test.1.xml");
200 public void testBRMSCreatePolicy() throws IOException, ServletException, SQLException {
201 httpServletRequest = Mockito.mock(HttpServletRequest.class);
202 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
203 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
204 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
205 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
206 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
207 Map<String, String> matchingAttributes = new HashMap<>();
208 Map<String, String> ruleAttributes = new HashMap<>();
209 ruleAttributes.put("templateName", "testPolicy");
210 ruleAttributes.put("samPoll", "5");
211 ruleAttributes.put("value", "test");
212 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Param", "test", "testing",
213 "BRMS_PARAM_RULE", false, "test",
214 matchingAttributes, 0, "DROOLS",
215 null, ruleAttributes, "5",
216 "default", "false", "", null, null);
217 MockServletInputStream mockInput =
218 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
219 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
224 pap.service(httpServletRequest, httpServletResponse);
226 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
227 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
228 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_BRMS_Param_test.1.xml");
232 public void testBRMSRawCreatePolicy() throws IOException, ServletException, SQLException {
233 httpServletRequest = Mockito.mock(HttpServletRequest.class);
234 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
235 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
236 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
237 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
238 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
239 Map<String, String> ruleAttributes = new HashMap<>();
240 ruleAttributes.put("value", "test");
241 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Raw", "test", "testig description",
242 "BRMS_RAW_RULE", false, "test", ruleAttributes, 0, "DROOLS",
244 "default", "false", null, null, 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_Raw_test.1.xml");
260 public void testClosedLoopPMCreatePolicy() 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 String json = "{\"test\":\"java\"}";
268 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("ClosedLoop_PM", "test", "testing", "onap",
269 json, false, null, "Registration Failure(Trinity)", false, "test", 0, null,
270 "default", "true", "");
271 MockServletInputStream mockInput =
272 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
273 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
278 pap.service(httpServletRequest, httpServletResponse);
280 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
281 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
282 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_PM_test.1.xml");
286 public void testDecisonAAFPolicy() throws IOException, ServletException, SQLException {
287 httpServletRequest = Mockito.mock(HttpServletRequest.class);
288 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
289 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
290 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
291 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
292 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
293 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("test", "test rule", "ONAP", "AAF", null, null, null,
294 null, null, null, null, null, null, null, false, "test", 0);
295 MockServletInputStream mockInput =
296 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
297 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
301 pap.service(httpServletRequest, httpServletResponse);
303 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
304 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
305 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_test.1.xml");
309 public void testDecisonGuardPolicy() throws IOException, ServletException, SQLException {
310 httpServletRequest = Mockito.mock(HttpServletRequest.class);
311 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
312 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
313 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
314 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
315 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
316 Map<String, String> matchingAttributes = new HashMap<>();
317 matchingAttributes.put("actor", "test");
318 matchingAttributes.put("recipe", "restart");
319 matchingAttributes.put("targets", "test,test1");
320 matchingAttributes.put("clname", "");
321 matchingAttributes.put("limit", "1");
322 matchingAttributes.put("timeWindow", "15");
323 matchingAttributes.put("timeUnits", "minute");
324 matchingAttributes.put("guardActiveStart", "05:00");
325 matchingAttributes.put("guardActiveEnd", "10:00");
326 StdPAPPolicy newPAPPolicy =
327 new StdPAPPolicy("testGuard", "test rule", "PDPD", "GUARD_YAML", matchingAttributes, null, null,
328 null, null, null, null, null, null, null, false, "test", 0);
329 MockServletInputStream mockInput =
330 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
331 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
335 pap.service(httpServletRequest, httpServletResponse);
337 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
338 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
339 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testGuard.1.xml");
343 public void testDecisonBLGuardPolicy() throws IOException, ServletException, SQLException {
344 httpServletRequest = Mockito.mock(HttpServletRequest.class);
345 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
346 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
347 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
348 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
349 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
350 Map<String, String> matchingAttributes = new HashMap<>();
351 matchingAttributes.put("actor", "test");
352 matchingAttributes.put("recipe", "restart");
353 matchingAttributes.put("clname", "test");
354 matchingAttributes.put("guardActiveStart", "05:00");
355 matchingAttributes.put("guardActiveEnd", "10:00");
356 matchingAttributes.put("blackList", "bl1,bl2");
357 StdPAPPolicy newPAPPolicy =
358 new StdPAPPolicy("testblGuard", "test rule", "PDPD", "GUARD_BL_YAML", matchingAttributes, null, null,
359 null, null, null, null, null, null, null, false, "test", 0);
360 MockServletInputStream mockInput =
361 new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
362 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
366 pap.service(httpServletRequest, httpServletResponse);
368 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
369 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
370 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testblGuard.1.xml");
374 public void testConfigPolicy() throws IOException, ServletException, SQLException {
375 httpServletRequest = Mockito.mock(HttpServletRequest.class);
376 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
377 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
378 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
379 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
380 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
381 Map<String, String> configAttributes = new HashMap<>();
382 configAttributes.put("value", "test");
383 StdPAPPolicy newPAPPolicy = new StdPAPPolicy(StdPAPPolicyParams.builder()
384 .configPolicyType("Base")
386 .description("test rule")
388 .configName("config")
389 .attributes(configAttributes)
391 .configBodyData("test body")
398 .ttlDate(null).build());
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.Config_test.1.xml");
412 private void setPolicyCreation() {
413 CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class);
414 PolicyCreation.setCommonClassDao(commonClassDao);
415 PolicyEditorScopes editorScope = new PolicyEditorScopes();
416 UserInfo userInfo = new UserInfo();
417 userInfo.setUserName("API");
418 userInfo.setUserLoginId("API");
419 editorScope.setScopeName("test");
420 editorScope.setUserCreatedBy(userInfo);
421 editorScope.setUserModifiedBy(userInfo);
422 Mockito.when(commonClassDao.getEntityItem(PolicyEditorScopes.class, "scopeName", "test"))
423 .thenReturn(editorScope);
424 BRMSParamTemplate template = new BRMSParamTemplate();
425 template.setRuleName("testPolicy");
426 template.setUserCreatedBy(userInfo);
427 String rule = "package com.sample;\n"
428 + "import com.sample.DroolsTest.Message;\n"
433 + "///This Rule will be generated by the UI.\n"
434 + "rule \"${policyName}.Create parameters structure\"\n"
438 + "Params params = new Params();\n"
439 + "params.setSamPoll(76);\n"
440 + "params.setValue(\"test\");\n"
441 + "insertLogical(params);\n"
443 + "rule \"Rule 1: Check parameter structure access from when/then\"\n"
445 + "$param: Params()\n"
446 + "Params($param.samPoll > 50)\n"
448 + "System.out.println(\"Firing rule 1\");\n"
449 + "System.out.println($param);\n"
451 template.setRule(rule);
452 Mockito.when(commonClassDao.getEntityItem(BRMSParamTemplate.class, "ruleName", "testPolicy"))
453 .thenReturn(template);
458 public void testClosedLoopCreateDictionary() throws IOException, SQLException, ServletException {
459 httpServletRequest = Mockito.mock(HttpServletRequest.class);
461 String json = "{\"dictionaryFields\": {\"vsclaction\": \"testRestAPI\",\"description\": \"testing create\"}}";
462 dictionaryTestSetup(false, "VSCLAction", json);
464 ClosedLoopDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
465 // send Request to PAP
466 pap.service(httpServletRequest, httpServletResponse);
468 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
472 httpServletRequest = Mockito.mock(HttpServletRequest.class);
473 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
474 json = "{\"dictionaryFields\": {\"vnftype\": \"testrestAPI1\",\"description\": \"testing create\"}}";
475 dictionaryTestSetup(false, "VNFType", json);
476 // send Request to PAP
477 pap.service(httpServletRequest, httpServletResponse);
479 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
483 httpServletRequest = Mockito.mock(HttpServletRequest.class);
484 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
486 "{\"dictionaryFields\":{\"pepName\":\"testRestAPI\",\"description\":\"testing create\"," +
487 "\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
488 "\"number\":\"test\"}]}}";
489 dictionaryTestSetup(false, "PEPOptions", json);
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);
500 "{\"dictionaryFields\":{\"varbindName\":\"testRestAPI\",\"varbindDescription\":\"testing\"," +
501 "\"varbindOID\":\"test\"}}";
502 dictionaryTestSetup(false, "Varbind", json);
503 // send Request to PAP
504 pap.service(httpServletRequest, httpServletResponse);
506 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
510 httpServletRequest = Mockito.mock(HttpServletRequest.class);
511 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
512 json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPI\",\"description\":\"testing\"}}";
513 dictionaryTestSetup(false, "Service", json);
514 // send Request to PAP
515 pap.service(httpServletRequest, httpServletResponse);
517 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
521 httpServletRequest = Mockito.mock(HttpServletRequest.class);
522 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
523 json = "{\"dictionaryFields\":{\"siteName\":\"testRestAPI\",\"description\":\"testing\"}}";
524 dictionaryTestSetup(false, "Site", json);
525 // send Request to PAP
526 pap.service(httpServletRequest, httpServletResponse);
528 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
532 public void testFirewallCreateDictionary() throws IOException, SQLException, ServletException {
533 httpServletRequest = Mockito.mock(HttpServletRequest.class);
534 // Check SecurityZone.
535 String json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"testing\"}}";
536 dictionaryTestSetup(false, "SecurityZone", json);
538 FirewallDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
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\":{\"actionName\":\"testRestAPI\",\"description\":\"test\"}}";
549 dictionaryTestSetup(false, "ActionList", json);
550 // send Request to PAP
551 pap.service(httpServletRequest, httpServletResponse);
553 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
555 // Check Protocol List.
557 httpServletRequest = Mockito.mock(HttpServletRequest.class);
558 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
559 json = "{\"dictionaryFields\":{\"protocolName\":\"testRestAPI\",\"description\":\"test\"}}";
560 dictionaryTestSetup(false, "ProtocolList", json);
561 // send Request to PAP
562 pap.service(httpServletRequest, httpServletResponse);
564 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
568 httpServletRequest = Mockito.mock(HttpServletRequest.class);
569 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
570 json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"test\"}}";
571 dictionaryTestSetup(false, "Zone", json);
572 // send Request to PAP
573 pap.service(httpServletRequest, httpServletResponse);
575 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
579 httpServletRequest = Mockito.mock(HttpServletRequest.class);
580 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
582 "{\"dictionaryFields\":{\"prefixListName\":\"testRestAPI\",\"prefixListValue\":\"127.0.0.1\"," +
583 "\"description\":\"testing\"}}";
584 dictionaryTestSetup(false, "PrefixList", json);
585 // send Request to PAP
586 pap.service(httpServletRequest, httpServletResponse);
588 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
590 // Check AddressGroup.
592 httpServletRequest = Mockito.mock(HttpServletRequest.class);
593 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
595 "{\"dictionaryFields\":{\"groupName\":\"testRestAPIgroup\",\"description\":\"testing\"," +
596 "\"attributes\":[{\"option\":\"testRestAPI\"}, {\"option\":\"testRestAPI\"}]}}";
597 dictionaryTestSetup(false, "AddressGroup", json);
598 // send Request to PAP
599 pap.service(httpServletRequest, httpServletResponse);
601 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
603 // Check ServiceGroup.
605 httpServletRequest = Mockito.mock(HttpServletRequest.class);
606 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
608 "{\"dictionaryFields\":{\"groupName\":\"testRestAPIServiceGroup\"," +
609 "\"attributes\":[{\"option\":\"testRestAPIservice\"}]}}";
610 dictionaryTestSetup(false, "ServiceGroup", json);
611 // send Request to PAP
612 pap.service(httpServletRequest, httpServletResponse);
614 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
616 // Check ServiceList.
618 httpServletRequest = Mockito.mock(HttpServletRequest.class);
619 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
621 "{\"dictionaryFields\":{\"serviceName\":\"testRestAPIservice\",\"serviceDescription\":\"test\"," +
622 "\"servicePorts\":\"8888\",\"transportProtocols\":[{\"option\":\"testRestAPI\"}," +
623 "{\"option\":\"testRestAPI1\"}],\"appProtocols\":[{\"option\":\"testRestAPI\"}," +
624 "{\"option\":\"testRestAPI1\"}]}}";
625 dictionaryTestSetup(false, "ServiceList", json);
626 // send Request to PAP
627 pap.service(httpServletRequest, httpServletResponse);
629 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
633 httpServletRequest = Mockito.mock(HttpServletRequest.class);
634 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
636 "{\"dictionaryFields\":{\"termName\":\"testRestAPIRule\",\"termDescription\":\"testing\"," +
637 "\"fromZoneDatas\":[{\"option\":\"testRestAPI\"}]," +
638 "\"toZoneDatas\":[{\"option\":\"testRestAPI1\"}]," +
639 "\"sourceListDatas\":[{\"option\":\"Group_testportal\"}]," +
640 "\"destinationListDatas\":[{\"option\":\"testRestAPI\"}]," +
641 "\"sourceServiceDatas\":[{\"option\":\"testRestAPIservice\"}," +
642 "{\"option\":\"testRestAPIservice1\"}]," +
643 "\"destinationServiceDatas\":[{\"option\":\"testRestAPIservice1\"}," +
644 "{\"option\":\"testportalservice2\"}],\"actionListDatas\":[{\"option\":\"testRestAPI\"}]}}";
645 dictionaryTestSetup(false, "TermList", json);
646 // send Request to PAP
647 pap.service(httpServletRequest, httpServletResponse);
649 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
653 public void testCommonCreateDictionary() throws IOException, SQLException, ServletException {
654 new DictionaryController(commonClassDao);
655 new ActionPolicyDictionaryController(commonClassDao);
656 new SafePolicyController(commonClassDao);
657 new DescriptiveDictionaryController(commonClassDao);
658 List<Object> object = new ArrayList<>();
659 object.add(new Category());
660 when(commonClassDao.getDataById(Category.class, "shortName", "resource")).thenReturn(object);
661 httpServletRequest = Mockito.mock(HttpServletRequest.class);
662 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
664 "{\"dictionaryFields\": {\"onapName\": \"testMMRestAPI1\",\"description\": \"testing update response " +
666 dictionaryTestSetup(false, "OnapName", json);
667 // send Request to PAP
668 pap.service(httpServletRequest, httpServletResponse);
670 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
672 httpServletRequest = Mockito.mock(HttpServletRequest.class);
673 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
675 "{\"dictionaryFields\": {\"xacmlId\": \"testMMRestAPI1\",\"datatypeBean\": {\"shortName\": " +
676 "\"string\"}, \"description\": \"testing update\",\"priority\": \"High\"," +
677 "\"userDataTypeValues\": [{\"attributeValues\": \"testAttr\"}, {\"attributeValues\": " +
678 "\"testAttr2\"}, {\"attributeValues\": \"testAttr3\"}]}}";
679 dictionaryTestSetup(false, "Attribute", json);
680 // send Request to PAP
681 pap.service(httpServletRequest, httpServletResponse);
683 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
686 httpServletRequest = Mockito.mock(HttpServletRequest.class);
687 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
689 "{\"dictionaryFields\":{\"attributeName\":\"TestMMrestAPI1\",\"type\":\"REST\",\"url\":\"testsomeurl" +
690 ".com\",\"method\":\"GET\",\"description\":\"test create\",\"body\":\"Testing Create\"," +
691 "\"headers\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
692 "\"number\":\"test\"}]}}";
693 dictionaryTestSetup(false, "Action", json);
694 // send Request to PAP
695 pap.service(httpServletRequest, httpServletResponse);
697 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
699 httpServletRequest = Mockito.mock(HttpServletRequest.class);
700 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
702 "{\"dictionaryFields\":{\"scopeName\":\"testMMRestAPI1\",\"description\":\"test\"," +
703 "\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\"," +
704 "\"number\":\"test\"}]}}";
705 dictionaryTestSetup(false, "DescriptiveScope", json);
706 // send Request to PAP
707 pap.service(httpServletRequest, httpServletResponse);
709 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
711 httpServletRequest = Mockito.mock(HttpServletRequest.class);
712 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
713 json = "{\"dictionaryFields\":{\"riskName\":\"testMMrestAPI1\",\"description\":\"test\"}}";
714 dictionaryTestSetup(false, "RiskType", 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\":{\"name\":\"testMMrestAPI1\",\"message\":\"test\"," +
724 "\"riskType\":\"testMMrestAPI1\"}}";
725 dictionaryTestSetup(false, "SafePolicyWarning", json);
726 // send Request to PAP
727 pap.service(httpServletRequest, httpServletResponse);
729 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
733 public void testDecisionCreateDictionary() throws IOException, SQLException, ServletException {
734 new DecisionPolicyDictionaryController(commonClassDao);
735 httpServletRequest = Mockito.mock(HttpServletRequest.class);
736 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
738 "{\"dictionaryFields\":{\"xacmlId\":\"testMMRestAPI1\",\"datatypeBean\":{\"shortName\":\"string\"}," +
739 "\"description\":\"test\",\"priority\":\"High\"}}";
740 dictionaryTestSetup(false, "Settings", json);
741 // send Request to PAP
742 pap.service(httpServletRequest, httpServletResponse);
744 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
746 httpServletRequest = Mockito.mock(HttpServletRequest.class);
747 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
749 "{\"dictionaryFields\":{\"bbid\":\"BB1\",\"workstep\":\"1\",\"treatments\":\"Manual Handling,Abort," +
751 dictionaryTestSetup(false, "RainyDayTreatments", json);
752 // send Request to PAP
753 pap.service(httpServletRequest, httpServletResponse);
755 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
759 public void testMSCreateDictionary() throws IOException, SQLException, ServletException {
760 new MicroServiceDictionaryController(commonClassDao);
761 new PolicyScopeDictionaryController(commonClassDao);
762 httpServletRequest = Mockito.mock(HttpServletRequest.class);
763 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
764 String json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
765 dictionaryTestSetup(false, "MicroServiceLocation", 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);
773 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
774 dictionaryTestSetup(false, "MicroServiceConfigName", json);
775 // send Request to PAP
776 pap.service(httpServletRequest, httpServletResponse);
778 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
780 httpServletRequest = Mockito.mock(HttpServletRequest.class);
781 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
782 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"description\":\"test\"}}";
783 dictionaryTestSetup(false, "DCAEUUID", 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\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
792 dictionaryTestSetup(false, "PolicyScopeService", 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);
800 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
801 dictionaryTestSetup(false, "PolicyScopeResource", json);
802 // send Request to PAP
803 pap.service(httpServletRequest, httpServletResponse);
805 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
807 httpServletRequest = Mockito.mock(HttpServletRequest.class);
808 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
809 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
810 dictionaryTestSetup(false, "PolicyScopeType", json);
811 // send Request to PAP
812 pap.service(httpServletRequest, httpServletResponse);
814 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
816 httpServletRequest = Mockito.mock(HttpServletRequest.class);
817 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
818 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
819 dictionaryTestSetup(false, "PolicyScopeClosedLoop", json);
820 // send Request to PAP
821 pap.service(httpServletRequest, httpServletResponse);
823 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
825 httpServletRequest = Mockito.mock(HttpServletRequest.class);
826 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
828 "{\"dictionaryFields\":{\"groupName\":\"testMMrestAPI1\",\"description\":\"testing\"}," +
829 "\"groupPolicyScopeListData1\":{\"resource\":\"ANY\",\"type\":\"ANY\",\"service\":\"ANY\"," +
830 "\"closedloop\":\"ANY\"}}";
831 dictionaryTestSetup(false, "GroupPolicyScopeList", json);
832 // send Request to PAP
833 pap.service(httpServletRequest, httpServletResponse);
835 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
838 private void dictionaryTestSetup(Boolean updateFlag, String dictionaryType, String json)
839 throws IOException, SQLException {
840 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
841 Mockito.when(httpServletRequest.getHeader("ClientScope")).thenReturn("dictionaryItem");
842 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
843 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
845 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("update");
847 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
849 Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionaryType);
850 MockServletInputStream mockInput = new MockServletInputStream(json.getBytes());
851 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
852 Mockito.when(httpServletRequest.getReader()).thenReturn(new BufferedReader(new InputStreamReader(mockInput)));
857 public void setDBDao() throws SQLException {
858 BasicDataSource dataSource = new BasicDataSource();
859 dataSource.setDriverClassName("org.h2.Driver");
860 // In-memory DB for testing
861 dataSource.setUrl("jdbc:h2:mem:test");
862 dataSource.setUsername("sa");
863 dataSource.setPassword("");
864 LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
865 sessionBuilder.scanPackages("org.onap.*", "com.*");
867 Properties properties = new Properties();
868 properties.put("hibernate.show_sql", "false");
869 properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
870 properties.put("hibernate.hbm2ddl.auto", "drop");
871 properties.put("hibernate.hbm2ddl.auto", "create");
873 sessionBuilder.addProperties(properties);
874 sessionFactory = sessionBuilder.buildSessionFactory();
876 // Set up dao with SessionFactory
877 CommonClassDaoImpl.setSessionfactory(sessionFactory);
878 PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
882 public void getDictionary() throws ServletException, IOException {
883 String[] dictionarys = new String[]{"Attribute", "OnapName", "Action", "BRMSParamTemplate", "VSCLAction"
884 , "VNFType", "PEPOptions", "Varbind", "Service", "Site", "Settings", "RainyDayTreatments",
885 "DescriptiveScope", "ActionList", "ProtocolList", "Zone", "SecurityZone",
886 "PrefixList", "AddressGroup", "ServiceGroup", "ServiceList", "TermList",
887 "MicroServiceLocation", "MicroServiceConfigName", "DCAEUUID", "MicroServiceModels",
888 "PolicyScopeService", "PolicyScopeResource", "PolicyScopeType", "PolicyScopeClosedLoop",
889 "GroupPolicyScopeList", "RiskType", "SafePolicyWarning", "MicroServiceDictionary"};
890 for (String dictionary : dictionarys) {
891 httpServletRequest = Mockito.mock(HttpServletRequest.class);
892 httpServletResponse = new MockHttpServletResponse();
893 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
894 Mockito.when(httpServletRequest.getMethod()).thenReturn("GET");
895 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
896 Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionary);
897 pap.service(httpServletRequest, httpServletResponse);
898 assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
903 public void testDummy() throws ServletException, IOException {
905 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
906 mockOutput = Mockito.mock(ServletOutputStream.class);
909 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
910 } catch (IOException e) {
915 pap.service(httpServletRequest, httpServletResponse);
917 } catch (Exception e) {
923 public void destroy() {
924 if (sessionFactory != null) {
925 sessionFactory.close();