2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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;
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.io.InputStreamReader;
29 import java.sql.SQLException;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
35 import java.util.Properties;
37 import javax.servlet.ServletConfig;
38 import javax.servlet.ServletException;
39 import javax.servlet.ServletOutputStream;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
43 import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
44 import org.hibernate.SessionFactory;
45 import org.junit.After;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.mockito.Mockito;
49 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
50 import org.onap.policy.pap.xacml.rest.controller.ClosedLoopDictionaryController;
51 import org.onap.policy.pap.xacml.rest.controller.FirewallDictionaryController;
52 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
53 import org.onap.policy.pap.xacml.rest.policycontroller.PolicyCreation;
54 import org.onap.policy.rest.dao.CommonClassDao;
55 import org.onap.policy.rest.jpa.BRMSParamTemplate;
56 import org.onap.policy.rest.jpa.PolicyEditorScopes;
57 import org.onap.policy.rest.jpa.UserInfo;
58 import org.onap.policy.utils.PolicyUtils;
59 import org.onap.policy.xacml.std.pap.StdPAPPolicy;
60 import org.springframework.mock.web.MockHttpServletResponse;
61 import org.springframework.mock.web.MockServletConfig;
62 import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
64 import com.mockrunner.mock.web.MockServletInputStream;
67 public class XACMLPAPTest {
69 private static final String ENVIRONMENT_HEADER = "Environment";
70 private List<String> headers = new ArrayList<>();
71 private HttpServletRequest httpServletRequest;
72 private HttpServletResponse httpServletResponse;
73 private ServletOutputStream mockOutput;
74 private ServletConfig servletConfig;
75 private XACMLPapServlet pap;
76 private SessionFactory sessionFactory;
79 public void setUp() throws ServletException {
80 httpServletRequest = Mockito.mock(HttpServletRequest.class);
81 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
82 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
83 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
85 servletConfig = Mockito.mock(MockServletConfig.class);
86 System.setProperty("com.sun.management.jmxremote.port", "9993");
87 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
88 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
89 .thenReturn("src/test/resources/xacml.pap.properties");
90 pap = new XACMLPapServlet();
91 pap.init(servletConfig);
95 public void testFirwallCreatePolicy() throws IOException, ServletException, SQLException {
96 httpServletRequest = Mockito.mock(HttpServletRequest.class);
97 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}]}";
98 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
99 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
100 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
101 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
102 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
103 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Firewall Config", "test", "testDescription", "Test", false, "test", json, 0,
104 "5","default", "false", "");
105 MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
106 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
110 pap.service(httpServletRequest, httpServletResponse);
112 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
113 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
114 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_FW_test.1.xml");
118 public void testBRMSCreatePolicy() throws IOException, ServletException, SQLException {
119 httpServletRequest = Mockito.mock(HttpServletRequest.class);
120 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
121 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
122 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
123 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
124 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
125 Map<String, String> matchingAttributes = new HashMap<>();
126 Map<String, String> ruleAttributes = new HashMap<>();
127 ruleAttributes.put("templateName", "testPolicy");
128 ruleAttributes.put("samPoll", "5");
129 ruleAttributes.put("value", "test");
130 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Param","test", "testing",
131 "BRMS_PARAM_RULE",false,"test",
132 matchingAttributes, 0, "DROOLS",
133 null, ruleAttributes, "5",
134 "default", "false", "", null, null);
135 MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
136 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
141 pap.service(httpServletRequest, httpServletResponse);
143 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
144 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
145 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_BRMS_Param_test.1.xml");
149 public void testBRMSRawCreatePolicy() throws IOException, ServletException, SQLException {
150 httpServletRequest = Mockito.mock(HttpServletRequest.class);
151 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
152 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
153 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
154 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
155 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Config");
156 Map<String, String> ruleAttributes = new HashMap<>();
157 ruleAttributes.put("value", "test");
158 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("BRMS_Raw","test","testig description",
159 "BRMS_RAW_RULE",false,"test", ruleAttributes, 0, "DROOLS",
161 "default", "false", null, null, null);
162 MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
163 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
168 pap.service(httpServletRequest, httpServletResponse);
170 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
171 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
172 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_BRMS_Raw_test.1.xml");
176 public void testClosedLoopPMCreatePolicy() throws IOException, ServletException, SQLException {
177 httpServletRequest = Mockito.mock(HttpServletRequest.class);
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 String json = "{\"test\":\"java\"}";
184 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("ClosedLoop_PM", "test", "testing", "onap",
185 json, false, null, "Registration Failure(Trinity)", false, "test", 0, null,
186 "default", "true", "");
187 MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
188 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
193 pap.service(httpServletRequest, httpServletResponse);
195 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
196 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
197 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_PM_test.1.xml");
201 public void testDecisonAAFPolicy() throws IOException, ServletException, SQLException {
202 httpServletRequest = Mockito.mock(HttpServletRequest.class);
203 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
204 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
205 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
206 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
207 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
208 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("test", "test rule", "ONAP", "AAF", null, null, null,
209 null, null, null, null, null, null, null, false, "test", 0);
210 MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
211 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
215 pap.service(httpServletRequest, httpServletResponse);
217 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
218 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
219 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_test.1.xml");
223 public void testDecisonGuardPolicy() throws IOException, ServletException, SQLException {
224 httpServletRequest = Mockito.mock(HttpServletRequest.class);
225 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
226 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
227 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
228 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
229 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
230 Map<String, String> matchingAttributes = new HashMap<>();
231 matchingAttributes.put("actor","test");
232 matchingAttributes.put("recipe","restart");
233 matchingAttributes.put("targets","test,test1");
234 matchingAttributes.put("clname","");
235 matchingAttributes.put("limit","1");
236 matchingAttributes.put("timeWindow","15");
237 matchingAttributes.put("timeUnits","minute");
238 matchingAttributes.put("guardActiveStart","05:00");
239 matchingAttributes.put("guardActiveEnd","10:00");
240 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("testGuard", "test rule", "PDPD", "GUARD_YAML", matchingAttributes , null, null,
241 null, null, null, null, null, null, null, false, "test", 0);
242 MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
243 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
247 pap.service(httpServletRequest, httpServletResponse);
249 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
250 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
251 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Decision_testGuard.1.xml");
255 public void testDecisonBLGuardPolicy() throws IOException, ServletException, SQLException {
256 httpServletRequest = Mockito.mock(HttpServletRequest.class);
257 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
258 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
259 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
260 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
261 Mockito.when(httpServletRequest.getParameter("policyType")).thenReturn("Decision");
262 Map<String, String> matchingAttributes = new HashMap<>();
263 matchingAttributes.put("actor","test");
264 matchingAttributes.put("recipe","restart");
265 matchingAttributes.put("clname","test");
266 matchingAttributes.put("guardActiveStart","05:00");
267 matchingAttributes.put("guardActiveEnd","10:00");
268 matchingAttributes.put("blackList","bl1,bl2");
269 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("testblGuard", "test rule", "PDPD", "GUARD_BL_YAML", matchingAttributes , null, null,
270 null, null, null, null, null, null, null, false, "test", 0);
271 MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
272 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.Decision_testblGuard.1.xml");
284 public void testConfigPolicy() 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 Map<String, String> configAttributes = new HashMap<>();
292 configAttributes.put("value", "test");
293 StdPAPPolicy newPAPPolicy = new StdPAPPolicy("Base", "test", "test rule", "TEST", "config", configAttributes, "OTHER",
294 "test body", false, "test",0, "5","default", "false", null);
295 MockServletInputStream mockInput = new MockServletInputStream(PolicyUtils.objectToJsonString(newPAPPolicy).getBytes());
296 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
300 pap.service(httpServletRequest, httpServletResponse);
302 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
303 Mockito.verify(httpServletResponse).addHeader("successMapKey", "success");
304 Mockito.verify(httpServletResponse).addHeader("policyName", "test.Config_test.1.xml");
307 private void setPolicyCreation() {
308 CommonClassDao commonClassDao = Mockito.mock(CommonClassDao.class);
309 PolicyCreation.setCommonClassDao(commonClassDao);
310 PolicyEditorScopes editorScope = new PolicyEditorScopes();
311 UserInfo userInfo = new UserInfo();
312 userInfo.setUserName("API");
313 userInfo.setUserLoginId("API");
314 editorScope.setScopeName("test");
315 editorScope.setUserCreatedBy(userInfo);
316 editorScope.setUserModifiedBy(userInfo);
317 Mockito.when(commonClassDao.getEntityItem(PolicyEditorScopes.class, "scopeName", "test")).thenReturn(editorScope);
318 BRMSParamTemplate template = new BRMSParamTemplate();
319 template.setRuleName("testPolicy");
320 template.setUserCreatedBy(userInfo);
321 String rule = "package com.sample;\n"
322 + "import com.sample.DroolsTest.Message;\n"
323 + "declare PapParams\n"
327 + "///This Rule will be generated by the UI.\n"
328 + "rule \"${policyName}.Create parameters structure\"\n"
332 + "Params params = new Params();\n"
333 + "params.setSamPoll(76);\n"
334 + "params.setValue(\"test\");\n"
335 + "insertLogical(params);\n"
337 + "rule \"Rule 1: Check parameter structure access from when/then\"\n"
339 + "$param: Params()\n"
340 + "Params($param.samPoll > 50)\n"
342 + "System.out.println(\"Firing rule 1\");\n"
343 + "System.out.println($param);\n"
345 template.setRule(rule );
346 Mockito.when(commonClassDao.getEntityItem(BRMSParamTemplate.class, "ruleName", "testPolicy")).thenReturn(template);
351 public void testClosedLoopCreateDictionary() throws IOException, SQLException, ServletException {
352 httpServletRequest = Mockito.mock(HttpServletRequest.class);
354 String json = "{\"dictionaryFields\": {\"vsclaction\": \"testRestAPI\",\"description\": \"testing create\"}}";
355 dictionaryTestSetup(false, "VSCLAction", json);
357 ClosedLoopDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
358 // send Request to PAP
359 pap.service(httpServletRequest, httpServletResponse);
361 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
365 httpServletRequest = Mockito.mock(HttpServletRequest.class);
366 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
367 json = "{\"dictionaryFields\": {\"vnftype\": \"testrestAPI1\",\"description\": \"testing create\"}}";
368 dictionaryTestSetup(false, "VNFType", json);
369 // send Request to PAP
370 pap.service(httpServletRequest, httpServletResponse);
372 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
376 httpServletRequest = Mockito.mock(HttpServletRequest.class);
377 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
378 json = "{\"dictionaryFields\":{\"pepName\":\"testRestAPI\",\"description\":\"testing create\",\"attributes\":[{\"option\":\"test1\",\"number\":\"test\"},{\"option\":\"test2\",\"number\":\"test\"}]}}";
379 dictionaryTestSetup(false, "PEPOptions", json);
380 // send Request to PAP
381 pap.service(httpServletRequest, httpServletResponse);
383 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
387 httpServletRequest = Mockito.mock(HttpServletRequest.class);
388 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
389 json = "{\"dictionaryFields\":{\"varbindName\":\"testRestAPI\",\"varbindDescription\":\"testing\",\"varbindOID\":\"test\"}}";
390 dictionaryTestSetup(false, "Varbind", json);
391 // send Request to PAP
392 pap.service(httpServletRequest, httpServletResponse);
394 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
398 httpServletRequest = Mockito.mock(HttpServletRequest.class);
399 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
400 json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPI\",\"description\":\"testing\"}}";
401 dictionaryTestSetup(false, "Service", json);
402 // send Request to PAP
403 pap.service(httpServletRequest, httpServletResponse);
405 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
409 httpServletRequest = Mockito.mock(HttpServletRequest.class);
410 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
411 json = "{\"dictionaryFields\":{\"siteName\":\"testRestAPI\",\"description\":\"testing\"}}";
412 dictionaryTestSetup(false, "Site", json);
413 // send Request to PAP
414 pap.service(httpServletRequest, httpServletResponse);
416 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
420 public void testFirewallCreateDictionary() throws IOException, SQLException, ServletException {
421 httpServletRequest = Mockito.mock(HttpServletRequest.class);
422 // Check SecurityZone.
423 String json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"testing\"}}";
424 dictionaryTestSetup(false, "SecurityZone", json);
426 FirewallDictionaryController.setCommonClassDao(new CommonClassDaoImpl());
427 // send Request to PAP
428 pap.service(httpServletRequest, httpServletResponse);
430 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
434 httpServletRequest = Mockito.mock(HttpServletRequest.class);
435 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
436 json = "{\"dictionaryFields\":{\"actionName\":\"testRestAPI\",\"description\":\"test\"}}";
437 dictionaryTestSetup(false, "ActionList", json);
438 // send Request to PAP
439 pap.service(httpServletRequest, httpServletResponse);
441 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
443 // Check Protocol List.
445 httpServletRequest = Mockito.mock(HttpServletRequest.class);
446 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
447 json = "{\"dictionaryFields\":{\"protocolName\":\"testRestAPI\",\"description\":\"test\"}}";
448 dictionaryTestSetup(false, "ProtocolList", json);
449 // send Request to PAP
450 pap.service(httpServletRequest, httpServletResponse);
452 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
456 httpServletRequest = Mockito.mock(HttpServletRequest.class);
457 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
458 json = "{\"dictionaryFields\":{\"zoneName\":\"testRestAPI\",\"zoneValue\":\"test\"}}";
459 dictionaryTestSetup(false, "Zone", json);
460 // send Request to PAP
461 pap.service(httpServletRequest, httpServletResponse);
463 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
467 httpServletRequest = Mockito.mock(HttpServletRequest.class);
468 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
469 json = "{\"dictionaryFields\":{\"prefixListName\":\"testRestAPI\",\"prefixListValue\":\"127.0.0.1\",\"description\":\"testing\"}}";
470 dictionaryTestSetup(false, "PrefixList", json);
471 // send Request to PAP
472 pap.service(httpServletRequest, httpServletResponse);
474 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
476 // Check AddressGroup.
478 httpServletRequest = Mockito.mock(HttpServletRequest.class);
479 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
480 json = "{\"dictionaryFields\":{\"groupName\":\"testRestAPIgroup\",\"description\":\"testing\",\"attributes\":[{\"option\":\"testRestAPI\"}, {\"option\":\"testRestAPI\"}]}}";
481 dictionaryTestSetup(false, "AddressGroup", json);
482 // send Request to PAP
483 pap.service(httpServletRequest, httpServletResponse);
485 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
487 // Check ServiceGroup.
489 httpServletRequest = Mockito.mock(HttpServletRequest.class);
490 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
491 json = "{\"dictionaryFields\":{\"groupName\":\"testRestAPIServiceGroup\",\"attributes\":[{\"option\":\"testRestAPIservice\"}]}}";
492 dictionaryTestSetup(false, "ServiceGroup", json);
493 // send Request to PAP
494 pap.service(httpServletRequest, httpServletResponse);
496 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
498 // Check ServiceList.
500 httpServletRequest = Mockito.mock(HttpServletRequest.class);
501 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
502 json = "{\"dictionaryFields\":{\"serviceName\":\"testRestAPIservice\",\"serviceDescription\":\"test\",\"servicePorts\":\"8888\",\"transportProtocols\":[{\"option\":\"testRestAPI\"},{\"option\":\"testRestAPI1\"}],\"appProtocols\":[{\"option\":\"testRestAPI\"},{\"option\":\"testRestAPI1\"}]}}";
503 dictionaryTestSetup(false, "ServiceList", json);
504 // send Request to PAP
505 pap.service(httpServletRequest, httpServletResponse);
507 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
511 httpServletRequest = Mockito.mock(HttpServletRequest.class);
512 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
513 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\"}]}}";
514 dictionaryTestSetup(false, "TermList", json);
515 // send Request to PAP
516 pap.service(httpServletRequest, httpServletResponse);
518 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
521 private void dictionaryTestSetup(Boolean updateFlag, String dictionaryType, String json) throws IOException, SQLException {
522 Mockito.when(httpServletRequest.getHeader(ENVIRONMENT_HEADER)).thenReturn("DEVL");
523 Mockito.when(httpServletRequest.getHeader("ClientScope")).thenReturn("dictionaryItem");
524 Mockito.when(httpServletRequest.getMethod()).thenReturn("PUT");
525 Mockito.when(httpServletRequest.getParameter("apiflag")).thenReturn("api");
527 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("update");
529 Mockito.when(httpServletRequest.getParameter("operation")).thenReturn("create");
531 Mockito.when(httpServletRequest.getParameter("dictionaryType")).thenReturn(dictionaryType);
532 MockServletInputStream mockInput = new MockServletInputStream(json.getBytes());
533 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
534 Mockito.when(httpServletRequest.getReader()).thenReturn(new BufferedReader(new InputStreamReader(mockInput)));
539 private void setDBDao() throws SQLException {
540 BasicDataSource dataSource = new BasicDataSource();
541 dataSource.setDriverClassName("org.h2.Driver");
542 // In-memory DB for testing
543 dataSource.setUrl("jdbc:h2:mem:test");
544 dataSource.setUsername("sa");
545 dataSource.setPassword("");
546 LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
547 sessionBuilder.scanPackages("org.onap.*", "com.*");
549 Properties properties = new Properties();
550 properties.put("hibernate.show_sql", "false");
551 properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
552 properties.put("hibernate.hbm2ddl.auto", "drop");
553 properties.put("hibernate.hbm2ddl.auto", "create");
555 sessionBuilder.addProperties(properties);
556 sessionFactory = sessionBuilder.buildSessionFactory();
558 // Set up dao with SessionFactory
559 CommonClassDaoImpl.setSessionfactory(sessionFactory);
560 PolicyCreation.setCommonClassDao(new CommonClassDaoImpl());
564 public void testDummy() throws ServletException, IOException {
566 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
567 mockOutput = Mockito.mock(ServletOutputStream.class);
570 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
571 } catch (IOException e) {
576 pap.service(httpServletRequest, httpServletResponse);
578 } catch (Exception e) {
584 public void destroy(){
585 if(sessionFactory!=null){
586 sessionFactory.close();