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.pdp.rest;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Properties;
28 import java.util.Random;
29 import javax.persistence.EntityManager;
30 import javax.persistence.EntityManagerFactory;
31 import javax.persistence.EntityTransaction;
32 import javax.persistence.Persistence;
33 import javax.servlet.ServletConfig;
34 import javax.servlet.ServletInputStream;
35 import javax.servlet.ServletOutputStream;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mockito;
42 import org.onap.policy.common.ia.IntegrityAuditProperties;
43 import org.onap.policy.common.im.IntegrityMonitor;
44 import org.onap.policy.common.im.IntegrityMonitorException;
45 import org.onap.policy.common.logging.ONAPLoggingContext;
46 import org.onap.policy.common.logging.flexlogger.FlexLogger;
47 import org.onap.policy.common.logging.flexlogger.Logger;
48 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
49 import org.onap.policy.xacml.std.pap.StdPDPStatus;
50 import org.powermock.api.mockito.PowerMockito;
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 @PrepareForTest({IntegrityMonitor.class})
61 public class XACMLPdpServletTest extends TestCase {
62 private static Logger LOGGER = FlexLogger.getLogger(XACMLPdpServletTest.class);
63 private List<String> headers = new ArrayList<>();
65 private HttpServletRequest httpServletRequest;
66 private HttpServletResponse httpServletResponse;
67 private ServletOutputStream mockOutput;
68 private ServletInputStream mockInput;
69 private ServletConfig servletConfig;
70 private XACMLPdpServlet pdpServlet;
71 private String persistenceUnit;
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;
83 status = new StdPDPStatus();
84 foobarPolicy = new StdPDPPolicy();
85 foobarPolicy.setId("foobar");
86 foobarPolicy.setVersion("123");
87 foobarPolicy.setName("nothing");
88 status.addLoadedPolicy(foobarPolicy);
91 properties = new Properties();
92 properties.put(IntegrityAuditProperties.DB_DRIVER, XACMLPdpServletTest.DEFAULT_DB_DRIVER);
93 properties.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/xacmlTest");
94 properties.put(IntegrityAuditProperties.DB_USER, XACMLPdpServletTest.DEFAULT_DB_USER);
95 properties.put(IntegrityAuditProperties.DB_PWD, XACMLPdpServletTest.DEFAULT_DB_PWD);
96 properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
97 properties.put(IntegrityAuditProperties.NODE_TYPE, "pap");
98 persistenceUnit = "testPdpPU";
99 resourceName = "siteA.pdp1";
101 System.setProperty("com.sun.management.jmxremote.port", "9999");
103 EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties);
105 EntityManager em = emf.createEntityManager();
106 // Start a transaction
107 EntityTransaction et = em.getTransaction();
109 IntegrityMonitor im = null;
111 im = IntegrityMonitor.getInstance(resourceName, properties);
112 } catch (Exception e2) {
113 // TODO Auto-generated catch block
114 e2.printStackTrace();
116 // cleanDb(persistenceUnit, properties);
118 httpServletRequest = Mockito.mock(HttpServletRequest.class);
119 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
120 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
121 Mockito.when(httpServletRequest.getAttributeNames())
122 .thenReturn(Collections.enumeration(headers));
123 Mockito.when(httpServletRequest.getRequestURI()).thenReturn("/pdp/test");
125 mockOutput = Mockito.mock(ServletOutputStream.class);
127 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
130 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
131 } catch (IOException e) {
135 servletConfig = Mockito.mock(MockServletConfig.class);
137 Mockito.when(servletConfig.getInitParameterNames())
138 .thenReturn(Collections.enumeration(headers));
139 pdpServlet = new XACMLPdpServlet();
140 pdpServlet.setIm(im);
142 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
143 .thenReturn("src/test/resources/xacml.pdp.properties");
145 System.setProperty("xacml.properties", "src/test/resources/xacml.pdp.properties");
146 System.setProperty("xacml.rest.pdp.config", "src/test/resources/config_testing");
147 System.setProperty("xacml.rest.pdp.webapps", "src/test/resources/webapps");
149 * System.setProperty("xacml.rootPolicies", "test_PolicyEngine.xml");
150 * System.setProperty("xacml.referencedPolicies", "test_PolicyEngine.xml");
151 * System.setProperty("test_PolicyEngine.xml.file", "config_testing\\test_PolicyEngine.xml");
153 System.setProperty("xacml.rest.pdp.register", "false");
154 System.setProperty("com.sun.management.jmxremote.port", "9999");
156 im = Mockito.mock(IntegrityMonitor.class);
157 // Need PowerMockito for mocking static method getInstance(...)
158 PowerMockito.mockStatic(IntegrityMonitor.class);
160 // when IntegrityMonitor.getInstance is called, return the mock object
162 .when(IntegrityMonitor.getInstance(Mockito.anyString(), Mockito.any(Properties.class)))
164 } catch (Exception e1) {
165 LOGGER.error("Exception Occured" + e1);
169 Mockito.doNothing().when(im).startTransaction();
170 } catch (IntegrityMonitorException e) {
173 Mockito.doNothing().when(im).endTransaction();
177 public void testInit() {
178 LOGGER.info("XACMLPdpServletTest - testInit");
180 pdpServlet.init(servletConfig);
183 } catch (Exception e) {
184 LOGGER.error("Exception Occured" + e);
192 public void testUebNotification() {
193 LOGGER.info("XACMLPdpServletTest - testUebNotification");
196 XACMLProperties.reloadProperties();
197 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME,
198 "src/test/resources/xacml.pdp.ueb.properties");
199 XACMLProperties.getProperties();
201 pdpServlet.init(servletConfig);
203 status.setStatus(com.att.research.xacml.api.pap.PDPStatus.Status.UPDATING_CONFIGURATION);
205 XACMLPdpLoader.validatePolicies(properties, status);
206 XACMLPdpLoader.sendNotification();
208 } catch (Exception e) {
209 LOGGER.error("Exception Occured" + e);
217 public void testDmaapNotification() {
218 LOGGER.info("XACMLPdpServletTest - testDmaapNotification");
221 XACMLProperties.reloadProperties();
222 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME,
223 "src/test/resources/xacml.pdp.dmaap.properties");
224 XACMLProperties.getProperties();
226 pdpServlet.init(servletConfig);
228 status.setStatus(com.att.research.xacml.api.pap.PDPStatus.Status.UPDATING_CONFIGURATION);
230 XACMLPdpLoader.validatePolicies(properties, status);
231 XACMLPdpLoader.sendNotification();
233 } catch (Exception e) {
234 LOGGER.error("Exception Occured" + e);
243 public void testXACMLPdpRegisterThread() {
244 LOGGER.info("XACMLPdpServletTest - testXACMLPdpRegisterThread");
246 ONAPLoggingContext baseLoggingContext = new ONAPLoggingContext();
247 baseLoggingContext.setServer("localhost");
248 XACMLPdpRegisterThread regThread = new XACMLPdpRegisterThread(baseLoggingContext);
251 } catch (Exception e) {
252 LOGGER.error("Exception Occured" + e);
258 public void testDoGetNoTypeError() {
259 LOGGER.info("XACMLPdpServletTest - testDoGetNoTypeError");
262 pdpServlet.init(servletConfig);
263 pdpServlet.doGet(httpServletRequest, httpServletResponse);
264 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
266 } catch (Exception e) {
267 System.out.println("Unexpected exception in testDoGetNoTypeError");
268 LOGGER.error("Exception Occured" + e);
274 public void testDoGetConfigType() {
275 LOGGER.info("XACMLPdpServletTest - testDoGetConfigType");
276 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("config");
279 pdpServlet.init(servletConfig);
280 pdpServlet.doGet(httpServletRequest, httpServletResponse);
281 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
283 } catch (Exception e) {
284 System.out.println("Unexpected exception in testDoGetConfigType");
285 LOGGER.error("Exception Occured" + e);
292 public void testDoGetTypeHb() {
293 LOGGER.info("XACMLPdpServletTest - testDoGetTypeHb");
295 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb");
296 pdpServlet.init(servletConfig);
297 pdpServlet.doGet(httpServletRequest, httpServletResponse);
298 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
300 } catch (Exception e) {
301 System.out.println("Unexpected exception in testDoGetTypeHb");
302 LOGGER.error("Exception Occured" + e);
308 public void testDoGetTypeStatus() {
309 LOGGER.info("XACMLPdpServletTest - testDoGetTypeStatus");
311 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("Status");
312 pdpServlet.init(servletConfig);
313 pdpServlet.doGet(httpServletRequest, httpServletResponse);
314 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
316 } catch (Exception e) {
317 System.out.println("Unexpected exception in testDoGetTypeStatus");
318 LOGGER.error("Exception Occured" + e);
324 public void testDoPost() {
325 LOGGER.info("XACMLPdpServletTest - testDoPost");
327 pdpServlet.init(servletConfig);
328 pdpServlet.doPost(httpServletRequest, httpServletResponse);
330 } catch (Exception e) {
331 System.out.println("Unexpected exception in testDoPost");
332 LOGGER.error("Exception Occured" + e);
338 public void testDoPostToLong() {
339 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
341 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
342 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
344 pdpServlet.init(servletConfig);
345 pdpServlet.doPost(httpServletRequest, httpServletResponse);
347 } catch (Exception e) {
348 System.out.println("Unexpected exception in testDoPostToLong");
349 LOGGER.error("Exception Occured" + e);
355 public void testDoPostContentLengthNegative() {
356 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
358 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
359 Mockito.when(httpServletRequest.getContentLength()).thenReturn(-1);
361 pdpServlet.init(servletConfig);
362 pdpServlet.doPost(httpServletRequest, httpServletResponse);
364 } catch (Exception e) {
365 System.out.println("Unexpected exception in testDoPostContentLengthNegative");
366 LOGGER.error("Exception Occured" + e);
372 public void testDoPostContentTypeNonValid() {
373 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
375 Mockito.when(httpServletRequest.getContentType()).thenReturn(";");
376 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
378 pdpServlet.init(servletConfig);
379 pdpServlet.doPost(httpServletRequest, httpServletResponse);
381 } catch (Exception e) {
382 System.out.println("Unexpected exception in testDoPostContentTypeNonValid");
383 LOGGER.error("Exception Occured" + e);
389 public void testDoPostContentTypeConfigurationError() {
390 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
392 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
393 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
395 pdpServlet.init(servletConfig);
396 pdpServlet.doPost(httpServletRequest, httpServletResponse);
398 } catch (Exception e) {
399 System.out.println("Unexpected exception in testDoPostContentTypeConfigurationError");
400 LOGGER.error("Exception Occured" + e);
406 public void testDoPutCacheEmpty() {
407 LOGGER.info("XACMLPdpServletTest - testDoPutCacheEmpty");
408 mockInput = Mockito.mock(ServletInputStream.class);
411 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("cache");
412 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
413 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
414 pdpServlet.init(servletConfig);
415 pdpServlet.doPut(httpServletRequest, httpServletResponse);
416 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST,
417 "PUT must contain at least one property");
419 } catch (Exception e) {
420 System.out.println("Unexpected exception in testDoPutCacheEmpty");
421 LOGGER.error("Exception Occured" + e);
427 public void testDoPutConfigPolicies() {
428 LOGGER.info("XACMLPdpServletTest - testDoPutConfigPolicies");
429 byte[] b = new byte[20];
430 new Random().nextBytes(b);
432 mockInput = new MockServletInputStream(b);
435 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
436 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
437 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
438 pdpServlet.init(servletConfig);
439 pdpServlet.doPut(httpServletRequest, httpServletResponse);
441 } catch (Exception e) {
442 System.out.println("Unexpected exception in testDoPutConfigPolicies");
443 LOGGER.error("Exception Occured" + e);
448 public void testDoPutToLong() {
449 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
451 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
453 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
454 Mockito.when(httpServletRequest.getContentLength()).thenReturn(1000000000);
456 pdpServlet.init(servletConfig);
457 pdpServlet.doPut(httpServletRequest, httpServletResponse);
458 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST,
459 "Content-Length larger than server will accept.");
461 } catch (Exception e) {
462 System.out.println("Unexpected exception in testDoPutToLong");
463 LOGGER.error("Exception Occured" + e);
469 public void testDoPutInvalidContentType() {
470 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
472 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
474 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/json");
475 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
477 pdpServlet.init(servletConfig);
478 pdpServlet.doPut(httpServletRequest, httpServletResponse);
479 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST,
480 "Invalid cache: 'policies' or content-type: 'text/json'");
482 } catch (Exception e) {
483 System.out.println("Unexpected exception in testDoPutInvalidContentType");
484 LOGGER.error("Exception Occured" + e);
490 public void testDestroy() {
491 LOGGER.info("XACMLPdpServletTest - testDestroy");
494 pdpServlet.init(servletConfig);
495 pdpServlet.destroy();
496 } catch (Exception e) {
497 System.out.println("Unexpected exception in testDestroy");
498 LOGGER.error("Exception Occured" + e);