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.springframework.mock.web.MockHttpServletResponse;
78 import org.springframework.mock.web.MockServletConfig;
79 import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
81 import com.mockrunner.mock.web.MockServletInputStream;
83 public class XACMLPAPTest {
84 private static final Log logger = LogFactory.getLog(XACMLPAPTest.class);
86 private static final String ENVIRONMENT_HEADER = "Environment";
87 private List<String> headers = new ArrayList<>();
88 private HttpServletRequest httpServletRequest;
89 private HttpServletResponse httpServletResponse;
90 private ServletOutputStream mockOutput;
91 private ServletConfig servletConfig;
92 private XACMLPapServlet pap;
93 private SessionFactory sessionFactory;
94 private CommonClassDao commonClassDao;
96 private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";
97 private static final String DEFAULT_DB_USER = "sa";
98 private static final String DEFAULT_DB_PWD = "";
101 public void setUpDB() throws Exception {
102 logger.info("setUpDB: Entering");
104 Properties properties = new Properties();
105 properties.put(IntegrityAuditProperties.DB_DRIVER, XACMLPAPTest.DEFAULT_DB_DRIVER);
106 properties.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/xacmlTest");
107 properties.put(IntegrityAuditProperties.DB_USER, XACMLPAPTest.DEFAULT_DB_USER);
108 properties.put(IntegrityAuditProperties.DB_PWD, XACMLPAPTest.DEFAULT_DB_PWD);
109 properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
110 properties.put(IntegrityAuditProperties.NODE_TYPE, "pap");
112 //Clean the iaTest DB table for IntegrityAuditEntity entries
113 cleanDb("testPapPU", properties);
115 logger.info("setUpDB: Exiting");
118 public void cleanDb(String persistenceUnit, Properties properties){
119 logger.debug("cleanDb: enter");
121 EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties);
123 EntityManager em = emf.createEntityManager();
124 // Start a transaction
125 EntityTransaction et = em.getTransaction();
130 em.createQuery("Delete from IntegrityAuditEntity").executeUpdate();
132 // commit transaction
135 logger.debug("cleanDb: exit");
139 public void setUp() throws ServletException {
140 httpServletRequest = Mockito.mock(HttpServletRequest.class);
141 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
142 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
143 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
145 servletConfig = Mockito.mock(MockServletConfig.class);
146 System.setProperty("com.sun.management.jmxremote.port", "9993");
147 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
148 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
149 .thenReturn("src/test/resources/xacml.pap.properties");
150 pap = new XACMLPapServlet();
151 pap.init(servletConfig);
152 commonClassDao = Mockito.mock(CommonClassDao.class);
153 new DictionaryUtils(commonClassDao);
154 DictionaryUtils.setDictionaryUtils(new DictionaryUtils());
155 Mockito.mock(DictionaryUtils.class);
159 public void testFirwallCreatePolicy() throws IOException, ServletException, SQLException {
160 httpServletRequest = Mockito.mock(HttpServletRequest.class);
161 String json = "{\"serviceTypeId\":\"/v0/firewall/pan\",\"configName\":\"TestFwPolicyConfig\",\"deploymentOption\":{\"deployNow\":false},\"securityZoneId\":\"cloudsite:dev1a\",\"serviceGroups\":[{\"name\":\"SSH\",\"description\":\"Sshservice entry in servicelist\",\"type\":\"SERVICE\",\"transportProtocol\":\"tcp\",\"appProtocol\":null,\"ports\":\"22\"}],\"addressGroups\":[{\"name\":\"test\",\"description\":\"Destination\",\"members\":[{\"type\":\"SUBNET\",\"value\":\"127.0.0.1/12\"}]},{\"name\":\"TestServers\",\"description\":\"SourceTestServers for firsttesting\",\"members\":[{\"type\":\"SUBNET\",\"value\":\"127.0.0.1/23\"}]}],\"firewallRuleList\":[{\"position\":\"1\",\"ruleName\":\"FWRuleTestServerToTest\",\"fromZones\":[\"UntrustedZoneTestName\"],\"toZones\":[\"TrustedZoneTestName\"],\"negateSource\":false,\"negateDestination\":false,\"sourceList\":[{\"type\":\"REFERENCE\",\"name\":\"TestServers\"}],\"destinationList\":[{\"type\":\"REFERENCE\",\"name\":\"Test\"}],\"sourceServices\":[],\"destServices\":[{\"type\":\"REFERENCE\",\"name\":\"SSH\"}],\"action\":\"accept\",\"description\":\"FWrule for Test source to Test destination\",\"enabled\":true,\"log\":true}]}";
162 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
163 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
164 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
165 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
166 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
167 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Firewall Config", "test", "testDescription", "Test", false, "test", json, 0,
168 "5","default", "false", "");
169 MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
170 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
174 pap.service(httpServletRequest, httpServletResponse);
176 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
177 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
178 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_FW_test.1.xml");
182 public void testBRMSCreatePolicy() throws IOException, ServletException, SQLException {
183 httpServletRequest = Mockito.mock(HttpServletRequest.class);
184 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
185 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
186 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
187 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
188 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
189 Map<String, String> matchingAttributes = new HashMap<>();
190 Map<String, String> ruleAttributes = new HashMap<>();
191 ruleAttributes.put("templateName", "testPolicy");
192 ruleAttributes.put("samPoll", "5");
193 ruleAttributes.put("value", "test");
194 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Param","test", "testing",
195 "BRMS_PARAM_RULE",false,"test",
196 matchingAttributes, 0, "DROOLS",
197 null, ruleAttributes, "5",
198 "default", "false", "", null, null);
199 MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
200 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_BRMS_Param_test.1.xml");
213 public void testBRMSRawCreatePolicy() 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> ruleAttributes = new HashMap<>();
221 ruleAttributes.put("value", "test");
222 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Raw","test","testig description",
223 "BRMS_RAW_RULE",false,"test", ruleAttributes, 0, "DROOLS",
225 "default", "false", null, null, null);
226 MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
227 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
232 pap.service(httpServletRequest, httpServletResponse);
234 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
235 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
236 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_BRMS_Raw_test.1.xml");
240 public void testClosedLoopPMCreatePolicy() throws IOException, ServletException, SQLException {
241 httpServletRequest = Mockito.mock(HttpServletRequest.class);
242 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
243 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
244 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
245 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
246 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
247 String json = "{\"test\":\"java\"}";
248 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("ClosedLoop_PM", "test", "testing", "onap",
249 json, false, null, "Registration Failure(Trinity)", false, "test", 0, null,
250 "default", "true", "");
251 MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
252 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
257 pap.service(httpServletRequest, httpServletResponse);
259 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
260 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
261 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_PM_test.1.xml");
265 public void testDecisonAAFPolicy() throws IOException, ServletException, SQLException {
266 httpServletRequest = Mockito.mock(HttpServletRequest.class);
267 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
268 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
269 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
270 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
271 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
272 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("test", "test rule", "ONAP", "AAF", null, null, null,
273 null, null, null, null, null, null, null, false, "test", 0);
274 MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
275 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
279 pap.service(httpServletRequest, httpServletResponse);
281 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
282 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
283 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_test.1.xml");
287 public void testDecisonGuardPolicy() throws IOException, ServletException, SQLException {
288 httpServletRequest = Mockito.mock(HttpServletRequest.class);
289 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
290 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
291 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
292 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
293 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
294 Map<String, String> matchingAttributes = new HashMap<>();
295 matchingAttributes.put("actor","test");
296 matchingAttributes.put("recipe","restart");
297 matchingAttributes.put("targets","test,test1");
298 matchingAttributes.put("clname","");
299 matchingAttributes.put("limit","1");
300 matchingAttributes.put("timeWindow","15");
301 matchingAttributes.put("timeUnits","minute");
302 matchingAttributes.put("guardActiveStart","05:00");
303 matchingAttributes.put("guardActiveEnd","10:00");
304 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("testGuard", "test rule", "PDPD", "GUARD_YAML", matchingAttributes , null, null,
305 null, null, null, null, null, null, null, false, "test", 0);
306 MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
307 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
311 pap.service(httpServletRequest, httpServletResponse);
313 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
314 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
315 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testGuard.1.xml");
319 public void testDecisonBLGuardPolicy() throws IOException, ServletException, SQLException {
320 httpServletRequest = Mockito.mock(HttpServletRequest.class);
321 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
322 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
323 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
324 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
325 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
326 Map<String, String> matchingAttributes = new HashMap<>();
327 matchingAttributes.put("actor","test");
328 matchingAttributes.put("recipe","restart");
329 matchingAttributes.put("clname","test");
330 matchingAttributes.put("guardActiveStart","05:00");
331 matchingAttributes.put("guardActiveEnd","10:00");
332 matchingAttributes.put("blackList","bl1,bl2");
333 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("testblGuard", "test rule", "PDPD", "GUARD_BL_YAML", matchingAttributes , null, null,
334 null, null, null, null, null, null, null, false, "test", 0);
335 MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
336 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
340 pap.service(httpServletRequest, httpServletResponse);
342 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
343 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
344 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testblGuard.1.xml");
348 public void testConfigPolicy() throws IOException, ServletException, SQLException {
349 httpServletRequest = Mockito.mock(HttpServletRequest.class);
350 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
351 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
352 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
353 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
354 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
355 Map<String, String> configAttributes = new HashMap<>();
356 configAttributes.put("value", "test");
357 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Base", "test", "test rule", "TEST", "config", configAttributes, "OTHER",
358 "test body", false, "test",0, "5","default", "false", null);
359 MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
360 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
364 pap.service(httpServletRequest, httpServletResponse);
366 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
367 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
368 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_test.1.xml");
371 private void setPolicyCreation() {
372 CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class);
373 PolicyCreation.setCommonClassDao(commonClassDao);
374 PolicyEditorScopes editorScope = new PolicyEditorScopes();
375 UserInfo userInfo = new UserInfo();
376 userInfo.setUserName("API");
377 userInfo.setUserLoginId("API");
378 editorScope.setScopeName("test");
379 editorScope.setUserCreatedBy(userInfo);
380 editorScope.setUserModifiedBy(userInfo);
381 Mockito.when(commonClassDao.getEntityItem(PolicyEditorScopes.class, "scopeName", "test")).thenReturn(editorScope);
382 BRMSParamTemplate template = new BRMSParamTemplate();
383 template.setRuleName("testPolicy");
384 template.setUserCreatedBy(userInfo);
385 String rule = "package com.sample;\n"
386 + "import com.sample.DroolsTest.Message;\n"
391 + "///This Rule will be generated by the UI.\n"
392 + "rule \"${policyName}.Create parameters structure\"\n"
396 + "Params params = new Params();\n"
397 + "params.setSamPoll(76);\n"
398 + "params.setValue(\"test\");\n"
399 + "insertLogical(params);\n"
401 + "rule \"Rule 1: Check parameter structure access from when/then\"\n"
403 + "$param: Params()\n"
404 + "Params($param.samPoll > 50)\n"
406 + "System.out.println(\"Firing rule 1\");\n"
407 + "System.out.println($param);\n"
409 template.setRule(rule );
410 Mockito.when(commonClassDao.getEntityItem(BRMSParamTemplate.class, "ruleName", "testPolicy")).thenReturn(template);
415 public void testClosedLoopCreateDictionary() throws IOException, SQLException, ServletException {
416 httpServletRequest = Mockito.mock(HttpServletRequest.class);
418 String json = "{\"dictionaryFields\": {\"vsclaction\": \"testRestAPI\",\"description\": \"testing create\"}}";
419 dictionaryTestSetup(false, "VSCLAction", json);
421 ClosedLoopDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
422 // send Request to PAP
423 pap.service(httpServletRequest, httpServletResponse);
425 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
429 httpServletRequest = Mockito.mock(HttpServletRequest.class);
430 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
431 json = "{\"dictionaryFields\": {\"vnftype\": \"testrestAPI1\",\"description\": \"testing create\"}}";
432 dictionaryTestSetup(false, "VNFType", json);
433 // send Request to PAP
434 pap.service(httpServletRequest, httpServletResponse);
436 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
440 httpServletRequest = Mockito.mock(HttpServletRequest.class);
441 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
442 json = "{\"dictionaryFields\":{\"pepName\":\"testRestAPI\",\"description\":\"testing create\",\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\",\"number\":\"test\"}]}}";
443 dictionaryTestSetup(false, "PEPOptions", json);
444 // send Request to PAP
445 pap.service(httpServletRequest, httpServletResponse);
447 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
451 httpServletRequest = Mockito.mock(HttpServletRequest.class);
452 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
453 json = "{\"dictionaryFields\":{\"varbindName\":\"testRestAPI\",\"varbindDescription\":\"testing\",\"varbindOID\":\"test\"}}";
454 dictionaryTestSetup(false, "Varbind", json);
455 // send Request to PAP
456 pap.service(httpServletRequest, httpServletResponse);
458 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
462 httpServletRequest = Mockito.mock(HttpServletRequest.class);
463 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
464 json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPI\",\"description\":\"testing\"}}";
465 dictionaryTestSetup(false, "Service", json);
466 // send Request to PAP
467 pap.service(httpServletRequest, httpServletResponse);
469 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
473 httpServletRequest = Mockito.mock(HttpServletRequest.class);
474 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
475 json = "{\"dictionaryFields\":{\"siteName\":\"testRestAPI\",\"description\":\"testing\"}}";
476 dictionaryTestSetup(false, "Site", json);
477 // send Request to PAP
478 pap.service(httpServletRequest, httpServletResponse);
480 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
484 public void testFirewallCreateDictionary() throws IOException, SQLException, ServletException {
485 httpServletRequest = Mockito.mock(HttpServletRequest.class);
486 // Check SecurityZone.
487 String json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"testing\"}}";
488 dictionaryTestSetup(false, "SecurityZone", json);
490 FirewallDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
491 // send Request to PAP
492 pap.service(httpServletRequest, httpServletResponse);
494 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
498 httpServletRequest = Mockito.mock(HttpServletRequest.class);
499 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
500 json = "{\"dictionaryFields\":{\"actionName\":\"testRestAPI\",\"description\":\"test\"}}";
501 dictionaryTestSetup(false, "ActionList", json);
502 // send Request to PAP
503 pap.service(httpServletRequest, httpServletResponse);
505 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
507 // Check Protocol List.
509 httpServletRequest = Mockito.mock(HttpServletRequest.class);
510 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
511 json = "{\"dictionaryFields\":{\"protocolName\":\"testRestAPI\",\"description\":\"test\"}}";
512 dictionaryTestSetup(false, "ProtocolList", json);
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\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"test\"}}";
523 dictionaryTestSetup(false, "Zone", 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);
533 json = "{\"dictionaryFields\":{\"prefixListName\":\"testRestAPI\",\"prefixListValue\":\"127.0.0.1\",\"description\":\"testing\"}}";
534 dictionaryTestSetup(false, "PrefixList", json);
535 // send Request to PAP
536 pap.service(httpServletRequest, httpServletResponse);
538 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
540 // Check AddressGroup.
542 httpServletRequest = Mockito.mock(HttpServletRequest.class);
543 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
544 json = "{\"dictionaryFields\":{\"groupName\":\"testRestAPIgroup\",\"description\":\"testing\",\"attributes\":[{\"option\":\"testRestAPI\"}, {\"option\":\"testRestAPI\"}]}}";
545 dictionaryTestSetup(false, "AddressGroup", json);
546 // send Request to PAP
547 pap.service(httpServletRequest, httpServletResponse);
549 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
551 // Check ServiceGroup.
553 httpServletRequest = Mockito.mock(HttpServletRequest.class);
554 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
555 json = "{\"dictionaryFields\":{\"groupName\":\"testRestAPIServiceGroup\",\"attributes\":[{\"option\":\"testRestAPIservice\"}]}}";
556 dictionaryTestSetup(false, "ServiceGroup", json);
557 // send Request to PAP
558 pap.service(httpServletRequest, httpServletResponse);
560 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
562 // Check ServiceList.
564 httpServletRequest = Mockito.mock(HttpServletRequest.class);
565 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
566 json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPIservice\",\"serviceDescription\":\"test\",\"servicePorts\":\"8888\",\"transportProtocols\":[{\"option\":\"testRestAPI\"},{\"option\":\"testRestAPI1\"}],\"appProtocols\":[{\"option\":\"testRestAPI\"},{\"option\":\"testRestAPI1\"}]}}";
567 dictionaryTestSetup(false, "ServiceList", 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);
577 json = "{\"dictionaryFields\":{\"termName\":\"testRestAPIRule\",\"termDescription\":\"testing\",\"fromZoneDatas\":[{\"option\":\"testRestAPI\"}],\"toZoneDatas\":[{\"option\":\"testRestAPI1\"}],\"sourceListDatas\":[{\"option\":\"Group_testportal\"}],\"destinationListDatas\":[{\"option\":\"testRestAPI\"}],\"sourceServiceDatas\":[{\"option\":\"testRestAPIservice\"},{\"option\":\"testRestAPIservice1\"}],\"destinationServiceDatas\":[{\"option\":\"testRestAPIservice1\"},{\"option\":\"testportalservice2\"}],\"actionListDatas\":[{\"option\":\"testRestAPI\"}]}}";
578 dictionaryTestSetup(false, "TermList", json);
579 // send Request to PAP
580 pap.service(httpServletRequest, httpServletResponse);
582 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
586 public void testCommonCreateDictionary() throws IOException, SQLException, ServletException {
587 new DictionaryController(commonClassDao);
588 new ActionPolicyDictionaryController(commonClassDao);
589 new SafePolicyController(commonClassDao);
590 new DescriptiveDictionaryController(commonClassDao);
591 List<Object> object = new ArrayList<>();
592 object.add(new Category());
593 when(commonClassDao.getDataById(Category.class, "shortName", "resource")).thenReturn(object);
594 httpServletRequest = Mockito.mock(HttpServletRequest.class);
595 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
596 String json = "{\"dictionaryFields\": {\"onapName\": \"testMMRestAPI1\",\"description\": \"testing update response message\"}}";
597 dictionaryTestSetup(false, "OnapName", json);
598 // send Request to PAP
599 pap.service(httpServletRequest, httpServletResponse);
601 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
603 httpServletRequest = Mockito.mock(HttpServletRequest.class);
604 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
605 json = "{\"dictionaryFields\": {\"xacmlId\": \"testMMRestAPI1\",\"datatypeBean\": {\"shortName\": \"string\"}, \"description\": \"testing update\",\"priority\": \"High\",\"userDataTypeValues\": [{\"attributeValues\": \"testAttr\"}, {\"attributeValues\": \"testAttr2\"}, {\"attributeValues\": \"testAttr3\"}]}}";
606 dictionaryTestSetup(false, "Attribute", json);
607 // send Request to PAP
608 pap.service(httpServletRequest, httpServletResponse);
610 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
613 httpServletRequest = Mockito.mock(HttpServletRequest.class);
614 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
615 json = "{\"dictionaryFields\":{\"attributeName\":\"TestMMrestAPI1\",\"type\":\"REST\",\"url\":\"testsomeurl.com\",\"method\":\"GET\",\"description\":\"test create\",\"body\":\"Testing Create\",\"headers\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\",\"number\":\"test\"}]}}";
616 dictionaryTestSetup(false, "Action", json);
617 // send Request to PAP
618 pap.service(httpServletRequest, httpServletResponse);
620 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
622 httpServletRequest = Mockito.mock(HttpServletRequest.class);
623 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
624 json = "{\"dictionaryFields\":{\"scopeName\":\"testMMRestAPI1\",\"description\":\"test\",\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\",\"number\":\"test\"}]}}";
625 dictionaryTestSetup(false, "DescriptiveScope", json);
626 // send Request to PAP
627 pap.service(httpServletRequest, httpServletResponse);
629 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
631 httpServletRequest = Mockito.mock(HttpServletRequest.class);
632 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
633 json = "{\"dictionaryFields\":{\"riskName\":\"testMMrestAPI1\",\"description\":\"test\"}}";
634 dictionaryTestSetup(false, "RiskType", json);
635 // send Request to PAP
636 pap.service(httpServletRequest, httpServletResponse);
638 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
640 httpServletRequest = Mockito.mock(HttpServletRequest.class);
641 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
642 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"message\":\"test\",\"riskType\":\"testMMrestAPI1\"}}";
643 dictionaryTestSetup(false, "SafePolicyWarning", json);
644 // send Request to PAP
645 pap.service(httpServletRequest, httpServletResponse);
647 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
651 public void testDecisionCreateDictionary() throws IOException, SQLException, ServletException {
652 new DecisionPolicyDictionaryController(commonClassDao);
653 httpServletRequest = Mockito.mock(HttpServletRequest.class);
654 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
655 String json = "{\"dictionaryFields\":{\"xacmlId\":\"testMMRestAPI1\",\"datatypeBean\":{\"shortName\":\"string\"},\"description\":\"test\",\"priority\":\"High\"}}";
656 dictionaryTestSetup(false, "Settings", json);
657 // send Request to PAP
658 pap.service(httpServletRequest, httpServletResponse);
660 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
662 httpServletRequest = Mockito.mock(HttpServletRequest.class);
663 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
664 json = "{\"dictionaryFields\":{\"bbid\":\"BB1\",\"workstep\":\"1\",\"treatments\":\"Manual Handling,Abort,Retry\"}}";
665 dictionaryTestSetup(false, "RainyDayTreatments", json);
666 // send Request to PAP
667 pap.service(httpServletRequest, httpServletResponse);
669 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
673 public void testMSCreateDictionary() throws IOException, SQLException, ServletException {
674 new MicroServiceDictionaryController(commonClassDao);
675 new PolicyScopeDictionaryController(commonClassDao);
676 httpServletRequest = Mockito.mock(HttpServletRequest.class);
677 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
678 String json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
679 dictionaryTestSetup(false, "MicroServiceLocation", json);
680 // send Request to PAP
681 pap.service(httpServletRequest, httpServletResponse);
683 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
685 httpServletRequest = Mockito.mock(HttpServletRequest.class);
686 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
687 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"descriptionValue\":\"test\"}}";
688 dictionaryTestSetup(false, "MicroServiceConfigName", json);
689 // send Request to PAP
690 pap.service(httpServletRequest, httpServletResponse);
692 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
694 httpServletRequest = Mockito.mock(HttpServletRequest.class);
695 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
696 json = "{\"dictionaryFields\":{\"name\":\"testMMrestAPI1\",\"description\":\"test\"}}";
697 dictionaryTestSetup(false, "DCAEUUID", json);
698 // send Request to PAP
699 pap.service(httpServletRequest, httpServletResponse);
701 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
703 httpServletRequest = Mockito.mock(HttpServletRequest.class);
704 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
705 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
706 dictionaryTestSetup(false, "PolicyScopeService", json);
707 // send Request to PAP
708 pap.service(httpServletRequest, httpServletResponse);
710 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
712 httpServletRequest = Mockito.mock(HttpServletRequest.class);
713 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
714 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
715 dictionaryTestSetup(false, "PolicyScopeResource", json);
716 // send Request to PAP
717 pap.service(httpServletRequest, httpServletResponse);
719 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
721 httpServletRequest = Mockito.mock(HttpServletRequest.class);
722 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
723 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
724 dictionaryTestSetup(false, "PolicyScopeType", json);
725 // send Request to PAP
726 pap.service(httpServletRequest, httpServletResponse);
728 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
730 httpServletRequest = Mockito.mock(HttpServletRequest.class);
731 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
732 json = "{\"dictionaryFields\":{\"name\":\"testApiANY\",\"descriptionValue\":\"default test\"}}";
733 dictionaryTestSetup(false, "PolicyScopeClosedLoop", json);
734 // send Request to PAP
735 pap.service(httpServletRequest, httpServletResponse);
737 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
739 httpServletRequest = Mockito.mock(HttpServletRequest.class);
740 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
741 json = "{\"dictionaryFields\":{\"groupName\":\"testMMrestAPI1\",\"description\":\"testing\"},\"groupPolicyScopeListData1\":{\"resource\":\"ANY\",\"type\":\"ANY\",\"service\":\"ANY\",\"closedloop\":\"ANY\"}}";
742 dictionaryTestSetup(false, "GroupPolicyScopeList", json);
743 // send Request to PAP
744 pap.service(httpServletRequest, httpServletResponse);
746 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
749 private void dictionaryTestSetup(Boolean updateFlag, String dictionaryType, String json) throws IOException, SQLException {
750 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
751 Mockito.when(httpServletRequest.getHeader("ClientScope")).thenReturn("dictionaryItem");
752 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
753 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
755 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("update");
757 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
759 Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionaryType);
760 MockServletInputStream mockInput = new MockServletInputStream(json.getBytes());
761 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
762 Mockito.when(httpServletRequest.getReader()).thenReturn(new BufferedReader(new InputStreamReader(mockInput)));
767 public void setDBDao() throws SQLException {
768 BasicDataSource dataSource = new BasicDataSource();
769 dataSource.setDriverClassName("org.h2.Driver");
770 // In-memory DB for testing
771 dataSource.setUrl("jdbc:h2:mem:test");
772 dataSource.setUsername("sa");
773 dataSource.setPassword("");
774 LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
775 sessionBuilder.scanPackages("org.onap.*", "com.*");
777 Properties properties = new Properties();
778 properties.put("hibernate.show_sql", "false");
779 properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
780 properties.put("hibernate.hbm2ddl.auto", "drop");
781 properties.put("hibernate.hbm2ddl.auto", "create");
783 sessionBuilder.addProperties(properties);
784 sessionFactory = sessionBuilder.buildSessionFactory();
786 // Set up dao with SessionFactory
787 CommonClassDaoImpl.setSessionfactory(sessionFactory);
788 PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
792 public void getDictionary() throws ServletException, IOException{
793 String[] dictionarys = new String[]{"Attribute", "OnapName", "Action", "BRMSParamTemplate","VSCLAction"
794 ,"VNFType","PEPOptions","Varbind","Service","Site", "Settings", "RainyDayTreatments",
795 "DescriptiveScope", "ActionList", "ProtocolList", "Zone", "SecurityZone",
796 "PrefixList", "AddressGroup", "ServiceGroup", "ServiceList", "TermList",
797 "MicroServiceLocation", "MicroServiceConfigName", "DCAEUUID", "MicroServiceModels",
798 "PolicyScopeService", "PolicyScopeResource", "PolicyScopeType", "PolicyScopeClosedLoop",
799 "GroupPolicyScopeList", "RiskType", "SafePolicyWarning", "MicroServiceDictionary"};
800 for(String dictionary : dictionarys){
801 httpServletRequest = Mockito.mock(HttpServletRequest.class);
802 httpServletResponse = new MockHttpServletResponse();
803 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
804 Mockito.when(httpServletRequest.getMethod()).thenReturn("GET");
805 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
806 Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionary);
807 pap.service(httpServletRequest, httpServletResponse);
808 assertTrue(HttpServletResponse.SC_OK == httpServletResponse.getStatus());
813 public void testDummy() throws ServletException, IOException {
815 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
816 mockOutput = Mockito.mock(ServletOutputStream.class);
819 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
820 } catch (IOException e) {
825 pap.service(httpServletRequest, httpServletResponse);
827 } catch (Exception e) {
833 public void destroy(){
834 if(sessionFactory!=null){
835 sessionFactory.close();