2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pdp.rest;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Properties;
30 import java.util.Random;
32 import javax.servlet.ServletConfig;
33 import javax.servlet.ServletInputStream;
34 import javax.servlet.ServletOutputStream;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mockito;
43 import org.onap.policy.common.ia.IntegrityAuditProperties;
44 import org.onap.policy.common.im.IntegrityMonitor;
45 import org.onap.policy.common.im.IntegrityMonitorException;
46 import org.onap.policy.common.logging.ONAPLoggingContext;
47 import org.onap.policy.common.logging.flexlogger.FlexLogger;
48 import org.onap.policy.common.logging.flexlogger.Logger;
49 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
50 import org.onap.policy.xacml.std.pap.StdPDPStatus;
51 import org.powermock.api.mockito.PowerMockito;
52 import org.powermock.core.classloader.annotations.PrepareForTest;
53 import org.powermock.modules.junit4.PowerMockRunner;
54 import org.springframework.mock.web.MockHttpServletResponse;
55 import org.springframework.mock.web.MockServletConfig;
57 import com.att.research.xacml.util.XACMLProperties;
58 import com.mockrunner.mock.web.MockServletInputStream;
60 import junit.framework.TestCase;
62 @RunWith(PowerMockRunner.class)
63 @PrepareForTest({IntegrityMonitor.class})
64 public class XACMLPdpServletTest extends TestCase {
65 private static Logger LOGGER = FlexLogger.getLogger(XACMLPdpServletTest.class);
66 private List<String> headers = new ArrayList<>();
68 private HttpServletRequest httpServletRequest;
69 private HttpServletResponse httpServletResponse;
70 private ServletOutputStream mockOutput;
71 private ServletInputStream mockInput;
72 private ServletConfig servletConfig;
73 private XACMLPdpServlet pdpServlet;
74 private Properties properties;
75 private String resourceName;
76 private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";
77 private static final String DEFAULT_DB_USER = "sa";
78 private static final String DEFAULT_DB_PWD = "";
79 private StdPDPStatus status;
80 private StdPDPPolicy foobarPolicy;
82 private static final String ERROR_TEXT = "Exception occurred: ";
87 status = new StdPDPStatus();
88 foobarPolicy = new StdPDPPolicy();
89 foobarPolicy.setId("foobar");
90 foobarPolicy.setVersion("123");
91 foobarPolicy.setName("nothing");
92 status.addLoadedPolicy(foobarPolicy);
94 properties = new Properties();
95 properties.put(IntegrityAuditProperties.DB_DRIVER, XACMLPdpServletTest.DEFAULT_DB_DRIVER);
96 properties.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/xacmlTest");
97 properties.put(IntegrityAuditProperties.DB_USER, XACMLPdpServletTest.DEFAULT_DB_USER);
98 properties.put(IntegrityAuditProperties.DB_PWD, XACMLPdpServletTest.DEFAULT_DB_PWD);
99 properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
100 properties.put(IntegrityAuditProperties.NODE_TYPE, "pap");
101 resourceName = "siteA.pdp1";
103 System.setProperty("com.sun.management.jmxremote.port", "9999");
105 IntegrityMonitor im = null;
107 im = IntegrityMonitor.getInstance(resourceName, properties);
108 } catch (Exception e) {
112 httpServletRequest = Mockito.mock(HttpServletRequest.class);
113 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
114 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
115 Mockito.when(httpServletRequest.getAttributeNames())
116 .thenReturn(Collections.enumeration(headers));
117 Mockito.when(httpServletRequest.getRequestURI()).thenReturn("/pdp/test");
119 mockOutput = Mockito.mock(ServletOutputStream.class);
121 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
124 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
125 } catch (IOException e) {
129 servletConfig = Mockito.mock(MockServletConfig.class);
131 Mockito.when(servletConfig.getInitParameterNames())
132 .thenReturn(Collections.enumeration(headers));
133 pdpServlet = new XACMLPdpServlet();
134 pdpServlet.setIm(im);
136 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
137 .thenReturn("src/test/resources/xacml.pdp.properties");
139 System.setProperty("xacml.properties", "src/test/resources/xacml.pdp.properties");
140 System.setProperty("xacml.rest.pdp.config", "src/test/resources/config_testing");
141 System.setProperty("xacml.rest.pdp.webapps", "src/test/resources/webapps");
143 System.setProperty("xacml.rest.pdp.register", "false");
144 System.setProperty("com.sun.management.jmxremote.port", "9999");
146 im = Mockito.mock(IntegrityMonitor.class);
147 // Need PowerMockito for mocking static method getInstance(...)
148 PowerMockito.mockStatic(IntegrityMonitor.class);
150 // when IntegrityMonitor.getInstance is called, return the mock object
152 .when(IntegrityMonitor.getInstance(Mockito.anyString(), Mockito.any(Properties.class)))
154 } catch (Exception e) {
155 LOGGER.error(ERROR_TEXT + e);
159 Mockito.doNothing().when(im).startTransaction();
160 } catch (IntegrityMonitorException e) {
163 Mockito.doNothing().when(im).endTransaction();
168 public void tearDown()
170 System.clearProperty("xacml.rest.pdp.config");
174 public void testInit() {
175 LOGGER.info("XACMLPdpServletTest - testInit");
177 pdpServlet.init(servletConfig);
180 } catch (Exception e) {
181 LOGGER.error(ERROR_TEXT + e);
189 public void testUebNotification() {
190 LOGGER.info("XACMLPdpServletTest - testUebNotification");
193 XACMLProperties.reloadProperties();
194 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME,
195 "src/test/resources/xacml.pdp.ueb.properties");
196 XACMLProperties.getProperties();
198 pdpServlet.init(servletConfig);
200 status.setStatus(com.att.research.xacml.api.pap.PDPStatus.Status.UPDATING_CONFIGURATION);
202 XACMLPdpLoader.validatePolicies(properties, status);
203 XACMLPdpLoader.sendNotification();
205 } catch (Exception e) {
206 LOGGER.error(ERROR_TEXT + e);
214 public void testDmaapNotification() {
215 LOGGER.info("XACMLPdpServletTest - testDmaapNotification");
218 XACMLProperties.reloadProperties();
219 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME,
220 "src/test/resources/xacml.pdp.dmaap.properties");
221 XACMLProperties.getProperties();
223 pdpServlet.init(servletConfig);
225 status.setStatus(com.att.research.xacml.api.pap.PDPStatus.Status.UPDATING_CONFIGURATION);
227 XACMLPdpLoader.validatePolicies(properties, status);
228 XACMLPdpLoader.sendNotification();
230 } catch (Exception e) {
231 LOGGER.error(ERROR_TEXT + e);
240 public void testXACMLPdpRegisterThread() {
241 LOGGER.info("XACMLPdpServletTest - testXACMLPdpRegisterThread");
243 ONAPLoggingContext baseLoggingContext = new ONAPLoggingContext();
244 baseLoggingContext.setServer("localhost");
245 XACMLPdpRegisterThread regThread = new XACMLPdpRegisterThread(baseLoggingContext);
248 } catch (Exception e) {
249 LOGGER.error(ERROR_TEXT + e);
255 public void testDoGetNoTypeError() {
256 LOGGER.info("XACMLPdpServletTest - testDoGetNoTypeError");
259 pdpServlet.init(servletConfig);
260 pdpServlet.doGet(httpServletRequest, httpServletResponse);
261 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
263 } catch (Exception e) {
264 LOGGER.error(ERROR_TEXT + e);
270 public void testDoGetConfigType() {
271 LOGGER.info("XACMLPdpServletTest - testDoGetConfigType");
272 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("config");
275 pdpServlet.init(servletConfig);
276 pdpServlet.doGet(httpServletRequest, httpServletResponse);
277 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
279 } catch (Exception e) {
280 LOGGER.error(ERROR_TEXT + e);
287 public void testDoGetTypeHb() {
288 LOGGER.info("XACMLPdpServletTest - testDoGetTypeHb");
290 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb");
291 pdpServlet.init(servletConfig);
292 pdpServlet.doGet(httpServletRequest, httpServletResponse);
293 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
295 } catch (Exception e) {
296 LOGGER.error(ERROR_TEXT + e);
302 public void testDoGetTypeStatus() {
303 LOGGER.info("XACMLPdpServletTest - testDoGetTypeStatus");
305 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("Status");
306 pdpServlet.init(servletConfig);
307 pdpServlet.doGet(httpServletRequest, httpServletResponse);
308 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
310 } catch (Exception e) {
311 LOGGER.error(ERROR_TEXT + e);
317 public void testDoPost() {
318 LOGGER.info("XACMLPdpServletTest - testDoPost");
320 pdpServlet.init(servletConfig);
321 pdpServlet.doPost(httpServletRequest, httpServletResponse);
323 } catch (Exception e) {
324 LOGGER.error(ERROR_TEXT + e);
330 public void testDoPostToLong() {
331 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
333 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
334 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
336 pdpServlet.init(servletConfig);
337 pdpServlet.doPost(httpServletRequest, httpServletResponse);
339 } catch (Exception e) {
340 LOGGER.error(ERROR_TEXT + e);
346 public void testDoPostContentLengthNegative() {
347 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
349 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
350 Mockito.when(httpServletRequest.getContentLength()).thenReturn(-1);
352 pdpServlet.init(servletConfig);
353 pdpServlet.doPost(httpServletRequest, httpServletResponse);
355 } catch (Exception e) {
356 LOGGER.error(ERROR_TEXT + e);
362 public void testDoPostContentTypeNonValid() {
363 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
365 Mockito.when(httpServletRequest.getContentType()).thenReturn(";");
366 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
368 pdpServlet.init(servletConfig);
369 pdpServlet.doPost(httpServletRequest, httpServletResponse);
371 } catch (Exception e) {
372 LOGGER.error(ERROR_TEXT + e);
378 public void testDoPostContentTypeConfigurationError() {
379 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
381 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
382 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
384 pdpServlet.init(servletConfig);
385 pdpServlet.doPost(httpServletRequest, httpServletResponse);
387 } catch (Exception e) {
388 LOGGER.error(ERROR_TEXT + e);
394 public void testDoPutCacheEmpty() {
395 LOGGER.info("XACMLPdpServletTest - testDoPutCacheEmpty");
396 mockInput = Mockito.mock(ServletInputStream.class);
399 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("cache");
400 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
401 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
402 pdpServlet.init(servletConfig);
403 pdpServlet.doPut(httpServletRequest, httpServletResponse);
404 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST,
405 "PUT must contain at least one property");
407 } catch (Exception e) {
408 LOGGER.error(ERROR_TEXT + e);
414 public void testDoPutConfigPolicies() {
415 LOGGER.info("XACMLPdpServletTest - testDoPutConfigPolicies");
416 byte[] b = new byte[20];
417 new Random().nextBytes(b);
419 mockInput = new MockServletInputStream(b);
422 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
423 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
424 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
425 pdpServlet.init(servletConfig);
426 pdpServlet.doPut(httpServletRequest, httpServletResponse);
428 } catch (Exception e) {
429 LOGGER.error(ERROR_TEXT + e);
434 public void testDoPutToLong() {
435 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
437 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
439 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
440 Mockito.when(httpServletRequest.getContentLength()).thenReturn(1000000000);
442 pdpServlet.init(servletConfig);
443 pdpServlet.doPut(httpServletRequest, httpServletResponse);
444 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST,
445 "Content-Length larger than server will accept.");
447 } catch (Exception e) {
448 LOGGER.error(ERROR_TEXT + e);
454 public void testDoPutInvalidContentType() {
455 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
457 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
459 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/json");
460 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
462 pdpServlet.init(servletConfig);
463 pdpServlet.doPut(httpServletRequest, httpServletResponse);
464 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST,
465 "Invalid cache: 'policies' or content-type: 'text/json'");
467 } catch (Exception e) {
468 LOGGER.error(ERROR_TEXT + e);
474 public void testDestroy() {
475 LOGGER.info("XACMLPdpServletTest - testDestroy");
478 pdpServlet.init(servletConfig);
479 pdpServlet.destroy();
480 } catch (Exception e) {
481 LOGGER.error(ERROR_TEXT + e);