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;
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.PrepareForTest;
51 import org.powermock.modules.junit4.PowerMockRunner;
52 import org.springframework.mock.web.MockHttpServletResponse;
53 import org.springframework.mock.web.MockServletConfig;
54 import com.att.research.xacml.util.XACMLProperties;
55 import com.mockrunner.mock.web.MockServletInputStream;
56 import junit.framework.TestCase;
58 @RunWith(PowerMockRunner.class)
59 @PrepareForTest({IntegrityMonitor.class})
60 public class XACMLPdpServletTest extends TestCase {
61 private static Logger LOGGER = FlexLogger.getLogger(XACMLPdpServletTest.class);
62 private List<String> headers = new ArrayList<>();
64 private HttpServletRequest httpServletRequest;
65 private HttpServletResponse httpServletResponse;
66 private ServletOutputStream mockOutput;
67 private ServletInputStream mockInput;
68 private ServletConfig servletConfig;
69 private XACMLPdpServlet pdpServlet;
70 private Properties properties;
71 private String resourceName;
72 private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";
73 private static final String DEFAULT_DB_USER = "sa";
74 private static final String DEFAULT_DB_PWD = "";
75 private StdPDPStatus status;
76 private StdPDPPolicy foobarPolicy;
78 private static final String ERROR_TEXT = "Exception occurred: ";
83 status = new StdPDPStatus();
84 foobarPolicy = new StdPDPPolicy();
85 foobarPolicy.setId("foobar");
86 foobarPolicy.setVersion("123");
87 foobarPolicy.setName("nothing");
88 status.addLoadedPolicy(foobarPolicy);
90 properties = new Properties();
91 properties.put(IntegrityAuditProperties.DB_DRIVER, XACMLPdpServletTest.DEFAULT_DB_DRIVER);
92 properties.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/xacmlTest");
93 properties.put(IntegrityAuditProperties.DB_USER, XACMLPdpServletTest.DEFAULT_DB_USER);
94 properties.put(IntegrityAuditProperties.DB_PWD, XACMLPdpServletTest.DEFAULT_DB_PWD);
95 properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
96 properties.put(IntegrityAuditProperties.NODE_TYPE, "pap");
97 resourceName = "siteA.pdp1";
99 System.setProperty("com.sun.management.jmxremote.port", "9999");
101 IntegrityMonitor im = null;
103 im = IntegrityMonitor.getInstance(resourceName, properties);
104 } catch (Exception e) {
108 httpServletRequest = Mockito.mock(HttpServletRequest.class);
109 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
110 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
111 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
112 Mockito.when(httpServletRequest.getRequestURI()).thenReturn("/pdp/test");
114 mockOutput = Mockito.mock(ServletOutputStream.class);
116 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
119 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
120 } catch (IOException e) {
124 servletConfig = Mockito.mock(MockServletConfig.class);
126 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
127 pdpServlet = new XACMLPdpServlet();
128 pdpServlet.setIm(im);
130 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
131 .thenReturn("src/test/resources/xacml.pdp.properties");
133 System.setProperty("xacml.properties", "src/test/resources/xacml.pdp.properties");
134 System.setProperty("xacml.rest.pdp.config", "src/test/resources/config_testing");
135 System.setProperty("xacml.rest.pdp.webapps", "src/test/resources/webapps");
137 System.setProperty("xacml.rest.pdp.register", "false");
138 System.setProperty("com.sun.management.jmxremote.port", "9999");
140 im = Mockito.mock(IntegrityMonitor.class);
141 // Need PowerMockito for mocking static method getInstance(...)
142 PowerMockito.mockStatic(IntegrityMonitor.class);
144 // when IntegrityMonitor.getInstance is called, return the mock object
145 PowerMockito.when(IntegrityMonitor.getInstance(Mockito.anyString(), Mockito.any(Properties.class)))
147 } catch (Exception e) {
148 LOGGER.error(ERROR_TEXT + e);
152 Mockito.doNothing().when(im).startTransaction();
153 } catch (IntegrityMonitorException e) {
156 Mockito.doNothing().when(im).endTransaction();
161 public void tearDown() {
162 System.clearProperty("xacml.rest.pdp.config");
166 public void testInit() {
167 LOGGER.info("XACMLPdpServletTest - testInit");
169 pdpServlet.init(servletConfig);
172 } catch (Exception e) {
173 LOGGER.error(ERROR_TEXT + e);
179 public void testUebNotification() {
180 LOGGER.info("XACMLPdpServletTest - testUebNotification");
183 XACMLProperties.reloadProperties();
184 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.ueb.properties");
185 XACMLProperties.getProperties();
187 pdpServlet.init(servletConfig);
189 status.setStatus(com.att.research.xacml.api.pap.PDPStatus.Status.UPDATING_CONFIGURATION);
191 XACMLPdpLoader.validatePolicies(properties, status);
192 XACMLPdpLoader.sendNotification();
194 } catch (Exception e) {
195 LOGGER.error(ERROR_TEXT + e);
201 public void testDmaapNotification() {
202 LOGGER.info("XACMLPdpServletTest - testDmaapNotification");
205 XACMLProperties.reloadProperties();
206 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.dmaap.properties");
207 XACMLProperties.getProperties();
209 pdpServlet.init(servletConfig);
211 status.setStatus(com.att.research.xacml.api.pap.PDPStatus.Status.UPDATING_CONFIGURATION);
213 XACMLPdpLoader.validatePolicies(properties, status);
214 XACMLPdpLoader.sendNotification();
216 } catch (Exception e) {
217 LOGGER.error(ERROR_TEXT + e);
223 public void testXACMLPdpRegisterThread() {
224 LOGGER.info("XACMLPdpServletTest - testXACMLPdpRegisterThread");
226 ONAPLoggingContext baseLoggingContext = new ONAPLoggingContext();
227 baseLoggingContext.setServer("localhost");
228 XACMLPdpRegisterThread regThread = new XACMLPdpRegisterThread(baseLoggingContext);
231 } catch (Exception e) {
232 LOGGER.error(ERROR_TEXT + e);
238 public void testDoGetNoTypeError() {
239 LOGGER.info("XACMLPdpServletTest - testDoGetNoTypeError");
242 pdpServlet.init(servletConfig);
243 pdpServlet.doGet(httpServletRequest, httpServletResponse);
244 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
246 } catch (Exception e) {
247 LOGGER.error(ERROR_TEXT + e);
253 public void testDoGetConfigType() {
254 LOGGER.info("XACMLPdpServletTest - testDoGetConfigType");
255 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("config");
258 pdpServlet.init(servletConfig);
259 pdpServlet.doGet(httpServletRequest, httpServletResponse);
260 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
262 } catch (Exception e) {
263 LOGGER.error(ERROR_TEXT + e);
270 public void testDoGetTypeHb() {
271 LOGGER.info("XACMLPdpServletTest - testDoGetTypeHb");
273 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb");
274 pdpServlet.init(servletConfig);
275 pdpServlet.doGet(httpServletRequest, httpServletResponse);
276 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
278 } catch (Exception e) {
279 LOGGER.error(ERROR_TEXT + e);
285 public void testDoGetTypeStatus() {
286 LOGGER.info("XACMLPdpServletTest - testDoGetTypeStatus");
288 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("Status");
289 pdpServlet.init(servletConfig);
290 pdpServlet.doGet(httpServletRequest, httpServletResponse);
291 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
293 } catch (Exception e) {
294 LOGGER.error(ERROR_TEXT + e);
300 public void testDoPost() {
301 LOGGER.info("XACMLPdpServletTest - testDoPost");
303 pdpServlet.init(servletConfig);
304 pdpServlet.doPost(httpServletRequest, httpServletResponse);
306 } catch (Exception e) {
307 LOGGER.error(ERROR_TEXT + e);
313 public void testDoPostToLong() {
314 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
316 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
317 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
319 pdpServlet.init(servletConfig);
320 pdpServlet.doPost(httpServletRequest, httpServletResponse);
322 } catch (Exception e) {
323 LOGGER.error(ERROR_TEXT + e);
329 public void testDoPostContentLengthNegative() {
330 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
332 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
333 Mockito.when(httpServletRequest.getContentLength()).thenReturn(-1);
335 pdpServlet.init(servletConfig);
336 pdpServlet.doPost(httpServletRequest, httpServletResponse);
338 } catch (Exception e) {
339 LOGGER.error(ERROR_TEXT + e);
345 public void testDoPostContentTypeNonValid() {
346 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
348 Mockito.when(httpServletRequest.getContentType()).thenReturn(";");
349 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
351 pdpServlet.init(servletConfig);
352 pdpServlet.doPost(httpServletRequest, httpServletResponse);
354 } catch (Exception e) {
355 LOGGER.error(ERROR_TEXT + e);
361 public void testDoPostContentTypeConfigurationError() {
362 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
364 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
365 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
367 pdpServlet.init(servletConfig);
368 pdpServlet.doPost(httpServletRequest, httpServletResponse);
370 } catch (Exception e) {
371 LOGGER.error(ERROR_TEXT + e);
377 public void testDoPutCacheEmpty() {
378 LOGGER.info("XACMLPdpServletTest - testDoPutCacheEmpty");
379 mockInput = Mockito.mock(ServletInputStream.class);
382 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("cache");
383 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
384 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
385 pdpServlet.init(servletConfig);
386 pdpServlet.doPut(httpServletRequest, httpServletResponse);
387 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST,
388 "PUT must contain at least one property");
390 } catch (Exception e) {
391 LOGGER.error(ERROR_TEXT + e);
397 public void testDoPutConfigPolicies() {
398 LOGGER.info("XACMLPdpServletTest - testDoPutConfigPolicies");
399 byte[] b = new byte[20];
400 new Random().nextBytes(b);
402 mockInput = new MockServletInputStream(b);
405 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
406 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
407 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
408 pdpServlet.init(servletConfig);
409 pdpServlet.doPut(httpServletRequest, httpServletResponse);
411 } catch (Exception e) {
412 LOGGER.error(ERROR_TEXT + e);
417 public void testDoPutToLong() {
418 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
420 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
422 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
423 Mockito.when(httpServletRequest.getContentLength()).thenReturn(1000000000);
425 pdpServlet.init(servletConfig);
426 pdpServlet.doPut(httpServletRequest, httpServletResponse);
427 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST,
428 "Content-Length larger than server will accept.");
430 } catch (Exception e) {
431 LOGGER.error(ERROR_TEXT + e);
437 public void testDoPutInvalidContentType() {
438 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
440 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
442 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/json");
443 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
445 pdpServlet.init(servletConfig);
446 pdpServlet.doPut(httpServletRequest, httpServletResponse);
447 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST,
448 "Invalid cache: 'policies' or content-type: 'text/json'");
450 } catch (Exception e) {
451 LOGGER.error(ERROR_TEXT + e);
457 public void testDestroy() {
458 LOGGER.info("XACMLPdpServletTest - testDestroy");
461 pdpServlet.init(servletConfig);
462 pdpServlet.destroy();
463 } catch (Exception e) {
464 LOGGER.error(ERROR_TEXT + e);