2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2020 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;
31 import javax.servlet.ServletConfig;
32 import javax.servlet.ServletInputStream;
33 import javax.servlet.ServletOutputStream;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mockito;
41 import org.onap.policy.common.ia.IntegrityAuditProperties;
42 import org.onap.policy.common.im.IntegrityMonitor;
43 import org.onap.policy.common.im.IntegrityMonitorException;
44 import org.onap.policy.common.logging.OnapLoggingContext;
45 import org.onap.policy.common.logging.flexlogger.FlexLogger;
46 import org.onap.policy.common.logging.flexlogger.Logger;
47 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
48 import org.onap.policy.xacml.std.pap.StdPDPStatus;
49 import org.powermock.api.mockito.PowerMockito;
50 import org.powermock.core.classloader.annotations.PowerMockIgnore;
51 import org.powermock.core.classloader.annotations.PrepareForTest;
52 import org.powermock.modules.junit4.PowerMockRunner;
53 import org.springframework.mock.web.MockHttpServletResponse;
54 import org.springframework.mock.web.MockServletConfig;
55 import com.att.research.xacml.util.XACMLProperties;
56 import com.mockrunner.mock.web.MockServletInputStream;
57 import junit.framework.TestCase;
59 @RunWith(PowerMockRunner.class)
60 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*", "org.w3c.dom.*"})
61 @PrepareForTest({IntegrityMonitor.class})
62 public class XACMLPdpServletTest extends TestCase {
63 private static Logger LOGGER = FlexLogger.getLogger(XACMLPdpServletTest.class);
64 private List<String> headers = new ArrayList<>();
66 private HttpServletRequest httpServletRequest;
67 private HttpServletResponse httpServletResponse;
68 private ServletOutputStream mockOutput;
69 private ServletInputStream mockInput;
70 private ServletConfig servletConfig;
71 private XACMLPdpServlet pdpServlet;
72 private Properties properties;
73 private String resourceName;
74 private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";
75 private static final String DEFAULT_DB_USER = "sa";
76 private static final String DEFAULT_DB_PWD = "";
77 private StdPDPStatus status;
78 private StdPDPPolicy foobarPolicy;
80 private static final String ERROR_TEXT = "Exception occurred: ";
85 status = new StdPDPStatus();
86 foobarPolicy = new StdPDPPolicy();
87 foobarPolicy.setId("foobar");
88 foobarPolicy.setVersion("123");
89 foobarPolicy.setName("nothing");
90 status.addLoadedPolicy(foobarPolicy);
92 properties = new Properties();
93 properties.put(IntegrityAuditProperties.DB_DRIVER, XACMLPdpServletTest.DEFAULT_DB_DRIVER);
94 properties.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/xacmlTest");
95 properties.put(IntegrityAuditProperties.DB_USER, XACMLPdpServletTest.DEFAULT_DB_USER);
96 properties.put(IntegrityAuditProperties.DB_PWD, XACMLPdpServletTest.DEFAULT_DB_PWD);
97 properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
98 properties.put(IntegrityAuditProperties.NODE_TYPE, "pap");
99 resourceName = "siteA.pdp1";
101 System.setProperty("com.sun.management.jmxremote.port", "9999");
103 IntegrityMonitor im = null;
105 im = IntegrityMonitor.getInstance(resourceName, properties);
106 } catch (Exception e) {
110 httpServletRequest = Mockito.mock(HttpServletRequest.class);
111 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
112 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
113 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
114 Mockito.when(httpServletRequest.getRequestURI()).thenReturn("/pdp/test");
116 mockOutput = Mockito.mock(ServletOutputStream.class);
118 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
121 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
122 } catch (IOException e) {
126 servletConfig = Mockito.mock(MockServletConfig.class);
128 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
129 pdpServlet = new XACMLPdpServlet();
130 pdpServlet.setIm(im);
132 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
133 .thenReturn("src/test/resources/xacml.pdp.properties");
135 System.setProperty("xacml.properties", "src/test/resources/xacml.pdp.properties");
136 System.setProperty("xacml.rest.pdp.config", "src/test/resources/config_testing");
137 System.setProperty("xacml.rest.pdp.webapps", "src/test/resources/webapps");
139 System.setProperty("xacml.rest.pdp.register", "false");
140 System.setProperty("com.sun.management.jmxremote.port", "9999");
142 im = Mockito.mock(IntegrityMonitor.class);
143 // Need PowerMockito for mocking static method getInstance(...)
144 PowerMockito.mockStatic(IntegrityMonitor.class);
146 // when IntegrityMonitor.getInstance is called, return the mock object
147 PowerMockito.when(IntegrityMonitor.getInstance(Mockito.anyString(), Mockito.any(Properties.class)))
149 } catch (Exception e) {
150 LOGGER.error(ERROR_TEXT + e);
154 Mockito.doNothing().when(im).startTransaction();
155 } catch (IntegrityMonitorException e) {
158 Mockito.doNothing().when(im).endTransaction();
163 public void tearDown() {
164 System.clearProperty("xacml.rest.pdp.config");
168 public void testInit() {
169 LOGGER.info("XACMLPdpServletTest - testInit");
171 pdpServlet.init(servletConfig);
174 } catch (Exception e) {
175 LOGGER.error(ERROR_TEXT + e);
181 public void testUebNotification() {
182 LOGGER.info("XACMLPdpServletTest - testUebNotification");
185 XACMLProperties.reloadProperties();
186 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.ueb.properties");
187 XACMLProperties.getProperties();
189 pdpServlet.init(servletConfig);
191 status.setStatus(com.att.research.xacml.api.pap.PDPStatus.Status.UPDATING_CONFIGURATION);
193 XACMLPdpLoader.validatePolicies(properties, status);
194 XACMLPdpLoader.sendNotification();
196 } catch (Exception e) {
197 LOGGER.error(ERROR_TEXT + e);
203 public void testDmaapNotification() {
204 LOGGER.info("XACMLPdpServletTest - testDmaapNotification");
207 XACMLProperties.reloadProperties();
208 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.dmaap.properties");
209 XACMLProperties.getProperties();
211 pdpServlet.init(servletConfig);
213 status.setStatus(com.att.research.xacml.api.pap.PDPStatus.Status.UPDATING_CONFIGURATION);
215 XACMLPdpLoader.validatePolicies(properties, status);
216 XACMLPdpLoader.sendNotification();
218 } catch (Exception e) {
219 LOGGER.error(ERROR_TEXT + e);
225 public void testXACMLPdpRegisterThread() {
226 LOGGER.info("XACMLPdpServletTest - testXACMLPdpRegisterThread");
228 OnapLoggingContext baseLoggingContext = new OnapLoggingContext();
229 baseLoggingContext.setServer("localhost");
230 XACMLPdpRegisterThread regThread = new XACMLPdpRegisterThread(baseLoggingContext);
233 } catch (Exception e) {
234 LOGGER.error(ERROR_TEXT + e);
240 public void testDoGetNoTypeError() {
241 LOGGER.info("XACMLPdpServletTest - testDoGetNoTypeError");
244 pdpServlet.init(servletConfig);
245 pdpServlet.doGet(httpServletRequest, httpServletResponse);
246 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
248 } catch (Exception e) {
249 LOGGER.error(ERROR_TEXT + e);
255 public void testDoGetConfigType() {
256 LOGGER.info("XACMLPdpServletTest - testDoGetConfigType");
257 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("config");
260 pdpServlet.init(servletConfig);
261 pdpServlet.doGet(httpServletRequest, httpServletResponse);
262 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
264 } catch (Exception e) {
265 LOGGER.error(ERROR_TEXT + e);
272 public void testDoGetTypeHb() {
273 LOGGER.info("XACMLPdpServletTest - testDoGetTypeHb");
275 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb");
276 pdpServlet.init(servletConfig);
277 pdpServlet.doGet(httpServletRequest, httpServletResponse);
278 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
280 } catch (Exception e) {
281 LOGGER.error(ERROR_TEXT + e);
287 public void testDoGetTypeStatus() {
288 LOGGER.info("XACMLPdpServletTest - testDoGetTypeStatus");
290 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("Status");
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 testDoPost() {
303 LOGGER.info("XACMLPdpServletTest - testDoPost");
305 pdpServlet.init(servletConfig);
306 pdpServlet.doPost(httpServletRequest, httpServletResponse);
308 } catch (Exception e) {
309 LOGGER.error(ERROR_TEXT + e);
315 public void testDoPostToLong() {
316 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
318 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
319 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
321 pdpServlet.init(servletConfig);
322 pdpServlet.doPost(httpServletRequest, httpServletResponse);
324 } catch (Exception e) {
325 LOGGER.error(ERROR_TEXT + e);
331 public void testDoPostContentLengthNegative() {
332 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
334 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
335 Mockito.when(httpServletRequest.getContentLength()).thenReturn(-1);
337 pdpServlet.init(servletConfig);
338 pdpServlet.doPost(httpServletRequest, httpServletResponse);
340 } catch (Exception e) {
341 LOGGER.error(ERROR_TEXT + e);
347 public void testDoPostContentTypeNonValid() {
348 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
350 Mockito.when(httpServletRequest.getContentType()).thenReturn(";");
351 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
353 pdpServlet.init(servletConfig);
354 pdpServlet.doPost(httpServletRequest, httpServletResponse);
356 } catch (Exception e) {
357 LOGGER.error(ERROR_TEXT + e);
363 public void testDoPostContentTypeConfigurationError() {
364 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
366 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
367 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
369 pdpServlet.init(servletConfig);
370 pdpServlet.doPost(httpServletRequest, httpServletResponse);
372 } catch (Exception e) {
373 LOGGER.error(ERROR_TEXT + e);
379 public void testDoPutCacheEmpty() {
380 LOGGER.info("XACMLPdpServletTest - testDoPutCacheEmpty");
381 mockInput = Mockito.mock(ServletInputStream.class);
384 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("cache");
385 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
386 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
387 pdpServlet.init(servletConfig);
388 pdpServlet.doPut(httpServletRequest, httpServletResponse);
389 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST,
390 "PUT must contain at least one property");
392 } catch (Exception e) {
393 LOGGER.error(ERROR_TEXT + e);
399 public void testDoPutConfigPolicies() {
400 LOGGER.info("XACMLPdpServletTest - testDoPutConfigPolicies");
401 byte[] b = new byte[20];
402 new Random().nextBytes(b);
404 mockInput = new MockServletInputStream(b);
407 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
408 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
409 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
410 pdpServlet.init(servletConfig);
411 pdpServlet.doPut(httpServletRequest, httpServletResponse);
413 } catch (Exception e) {
414 LOGGER.error(ERROR_TEXT + e);
419 public void testDoPutToLong() {
420 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
422 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
424 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
425 Mockito.when(httpServletRequest.getContentLength()).thenReturn(1000000000);
427 pdpServlet.init(servletConfig);
428 pdpServlet.doPut(httpServletRequest, httpServletResponse);
429 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST,
430 "Content-Length larger than server will accept.");
432 } catch (Exception e) {
433 LOGGER.error(ERROR_TEXT + e);
439 public void testDoPutInvalidContentType() {
440 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
442 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
444 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/json");
445 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
447 pdpServlet.init(servletConfig);
448 pdpServlet.doPut(httpServletRequest, httpServletResponse);
449 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST,
450 "Invalid cache: 'policies' or content-type: 'text/json'");
452 } catch (Exception e) {
453 LOGGER.error(ERROR_TEXT + e);
459 public void testDestroy() {
460 LOGGER.info("XACMLPdpServletTest - testDestroy");
463 pdpServlet.init(servletConfig);
464 pdpServlet.destroy();
465 } catch (Exception e) {
466 LOGGER.error(ERROR_TEXT + e);