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 static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Properties;
31 import java.util.Random;
33 import javax.persistence.EntityManager;
34 import javax.persistence.EntityManagerFactory;
35 import javax.persistence.EntityTransaction;
36 import javax.persistence.Persistence;
37 import javax.servlet.ServletConfig;
38 import javax.servlet.ServletInputStream;
39 import javax.servlet.ServletOutputStream;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpServletResponse;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.mockito.Mockito;
46 import org.onap.policy.common.ia.DbDAO;
47 import org.onap.policy.common.ia.IntegrityAuditProperties;
48 import org.onap.policy.common.im.IntegrityMonitor;
49 import org.onap.policy.common.im.IntegrityMonitorException;
50 import org.onap.policy.common.logging.ONAPLoggingContext;
51 import org.onap.policy.common.logging.flexlogger.FlexLogger;
52 import org.onap.policy.common.logging.flexlogger.Logger;
53 import org.onap.policy.rest.XACMLRestProperties;
54 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
55 import org.onap.policy.xacml.std.pap.StdPDPStatus;
56 import org.powermock.api.mockito.PowerMockito;
57 import org.springframework.mock.web.MockHttpServletResponse;
58 import org.springframework.mock.web.MockServletConfig;
61 import com.att.research.xacml.util.XACMLProperties;
62 import com.mockrunner.mock.web.MockServletInputStream;
64 import junit.framework.TestCase;
66 public class XACMLPdpServletTest extends TestCase{
67 private static Logger LOGGER = FlexLogger.getLogger(XACMLPdpServletTest.class);
69 private List<String> headers = new ArrayList<>();
71 private HttpServletRequest httpServletRequest;
72 private HttpServletResponse httpServletResponse;
73 private ServletOutputStream mockOutput;
74 private ServletInputStream mockInput;
75 private ServletConfig servletConfig;
76 private XACMLPdpServlet pdpServlet;
77 private IntegrityMonitor im;
80 private String persistenceUnit;
81 private Properties properties;
82 private String resourceName;
83 private String dbDriver;
85 private String dbUser;
87 private String siteName;
88 private String nodeType;
89 private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";
90 private static final String DEFAULT_DB_USER = "sa";
91 private static final String DEFAULT_DB_PWD = "";
93 private StdPDPStatus status;
94 private StdPDPPolicy foobarPolicy;
98 status = new StdPDPStatus();
99 foobarPolicy = new StdPDPPolicy();
100 foobarPolicy.setId("foobar");
101 foobarPolicy.setVersion("123");
102 foobarPolicy.setName("nothing");
103 status.addLoadedPolicy(foobarPolicy);
106 properties = new Properties();
107 properties.put(IntegrityAuditProperties.DB_DRIVER, XACMLPdpServletTest.DEFAULT_DB_DRIVER);
108 properties.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/xacmlTest");
109 properties.put(IntegrityAuditProperties.DB_USER, XACMLPdpServletTest.DEFAULT_DB_USER);
110 properties.put(IntegrityAuditProperties.DB_PWD, XACMLPdpServletTest.DEFAULT_DB_PWD);
111 properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
112 properties.put(IntegrityAuditProperties.NODE_TYPE, "pap");
113 //properties.put("com.sun.management.jmxremote.port", "9999");
114 dbDriver = XACMLPdpServletTest.DEFAULT_DB_DRIVER;
115 dbUrl = "jdbc:h2:file:./sql/xacmlTest";
116 dbUser = XACMLPdpServletTest.DEFAULT_DB_USER;
117 dbPwd = XACMLPdpServletTest.DEFAULT_DB_PWD;
120 persistenceUnit = "testPdpPU";
121 resourceName = "siteA.pdp1";
123 System.setProperty("com.sun.management.jmxremote.port", "9999");
125 EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties);
127 EntityManager em = emf.createEntityManager();
128 // Start a transaction
129 EntityTransaction et = em.getTransaction();
131 IntegrityMonitor im = null;
133 im = IntegrityMonitor.getInstance(resourceName, properties);
134 } catch (Exception e2) {
135 // TODO Auto-generated catch block
136 e2.printStackTrace();
138 //cleanDb(persistenceUnit, properties);
140 httpServletRequest = Mockito.mock(HttpServletRequest.class);
141 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
142 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
143 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
144 Mockito.when(httpServletRequest.getRequestURI()).thenReturn("/pdp/test");
146 mockOutput = Mockito.mock(ServletOutputStream.class);
148 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
151 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
152 } catch (IOException e) {
156 servletConfig = Mockito.mock(MockServletConfig.class);
158 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
159 pdpServlet = new XACMLPdpServlet();
160 pdpServlet.setIm(im);
162 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("src/test/resources/xacml.pdp.properties");
164 System.setProperty("xacml.properties", "src/test/resources/xacml.pdp.properties");
165 System.setProperty("xacml.rest.pdp.config", "src/test/resources/config_testing");
166 System.setProperty("xacml.rest.pdp.webapps", "src/test/resources/webapps");
167 /*System.setProperty("xacml.rootPolicies", "test_PolicyEngine.xml");
168 System.setProperty("xacml.referencedPolicies", "test_PolicyEngine.xml");
169 System.setProperty("test_PolicyEngine.xml.file", "config_testing\\test_PolicyEngine.xml");
171 System.setProperty("xacml.rest.pdp.register", "false");
172 System.setProperty("com.sun.management.jmxremote.port", "9999");
174 im = Mockito.mock(IntegrityMonitor.class);
175 // Need PowerMockito for mocking static method getInstance(...)
176 PowerMockito.mockStatic(IntegrityMonitor.class);
178 // when IntegrityMonitor.getInstance is called, return the mock object
179 PowerMockito.when(IntegrityMonitor.getInstance(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(im);
180 } catch (Exception e1) {
181 LOGGER.error("Exception Occured"+e1);
185 Mockito.doNothing().when(im).startTransaction();
186 } catch (IntegrityMonitorException e) {
189 Mockito.doNothing().when(im).endTransaction();
193 public void testInit(){
194 LOGGER.info("XACMLPdpServletTest - testInit");
196 pdpServlet.init(servletConfig);
199 } catch (Exception e) {
200 LOGGER.error("Exception Occured"+e);
208 public void testUebNotification() {
209 LOGGER.info("XACMLPdpServletTest - testUebNotification");
212 XACMLProperties.reloadProperties();
213 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.ueb.properties");
214 XACMLProperties.getProperties();
216 pdpServlet.init(servletConfig);
218 status.setStatus(com.att.research.xacml.api.pap.PDPStatus.Status.UPDATING_CONFIGURATION);
220 XACMLPdpLoader.validatePolicies(properties, status);
221 XACMLPdpLoader.sendNotification();
223 } catch (Exception e) {
224 LOGGER.error("Exception Occured"+e);
232 public void testDmaapNotification() {
233 LOGGER.info("XACMLPdpServletTest - testDmaapNotification");
236 XACMLProperties.reloadProperties();
237 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.dmaap.properties");
238 XACMLProperties.getProperties();
240 pdpServlet.init(servletConfig);
242 status.setStatus(com.att.research.xacml.api.pap.PDPStatus.Status.UPDATING_CONFIGURATION);
244 XACMLPdpLoader.validatePolicies(properties, status);
245 XACMLPdpLoader.sendNotification();
247 } catch (Exception e) {
248 LOGGER.error("Exception Occured"+e);
257 public void testXACMLPdpRegisterThread() {
258 LOGGER.info("XACMLPdpServletTest - testXACMLPdpRegisterThread");
260 ONAPLoggingContext baseLoggingContext = new ONAPLoggingContext();
261 baseLoggingContext.setServer("localhost");
262 XACMLPdpRegisterThread regThread = new XACMLPdpRegisterThread(baseLoggingContext);
265 } catch (Exception e) {
266 LOGGER.error("Exception Occured"+e);
272 public void testDoGetNoTypeError(){
273 LOGGER.info("XACMLPdpServletTest - testDoGetNoTypeError");
276 pdpServlet.init(servletConfig);
277 pdpServlet.doGet(httpServletRequest, httpServletResponse);
278 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
281 System.out.println("Unexpected exception in testDoGetNoTypeError");
282 LOGGER.error("Exception Occured"+e);
288 public void testDoGetConfigType(){
289 LOGGER.info("XACMLPdpServletTest - testDoGetConfigType");
290 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("config");
293 pdpServlet.init(servletConfig);
294 pdpServlet.doGet(httpServletRequest, httpServletResponse);
295 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
297 }catch (Exception e){
298 System.out.println("Unexpected exception in testDoGetConfigType");
299 LOGGER.error("Exception Occured"+e);
306 public void testDoGetTypeHb(){
307 LOGGER.info("XACMLPdpServletTest - testDoGetTypeHb");
309 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb");
310 pdpServlet.init(servletConfig);
311 pdpServlet.doGet(httpServletRequest, httpServletResponse);
312 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
315 System.out.println("Unexpected exception in testDoGetTypeHb");
316 LOGGER.error("Exception Occured"+e);
322 public void testDoGetTypeStatus(){
323 LOGGER.info("XACMLPdpServletTest - testDoGetTypeStatus");
325 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("Status");
326 pdpServlet.init(servletConfig);
327 pdpServlet.doGet(httpServletRequest, httpServletResponse);
328 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
331 System.out.println("Unexpected exception in testDoGetTypeStatus");
332 LOGGER.error("Exception Occured"+e);
338 public void testDoPost(){
339 LOGGER.info("XACMLPdpServletTest - testDoPost");
341 pdpServlet.init(servletConfig);
342 pdpServlet.doPost(httpServletRequest, httpServletResponse);
344 }catch (Exception e){
345 System.out.println("Unexpected exception in testDoPost");
346 LOGGER.error("Exception Occured"+e);
352 public void testDoPostToLong(){
353 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
355 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
356 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
358 pdpServlet.init(servletConfig);
359 pdpServlet.doPost(httpServletRequest, httpServletResponse);
361 }catch (Exception e){
362 System.out.println("Unexpected exception in testDoPostToLong");
363 LOGGER.error("Exception Occured"+e);
369 public void testDoPostContentLengthNegative(){
370 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
372 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
373 Mockito.when(httpServletRequest.getContentLength()).thenReturn(-1);
375 pdpServlet.init(servletConfig);
376 pdpServlet.doPost(httpServletRequest, httpServletResponse);
378 }catch (Exception e){
379 System.out.println("Unexpected exception in testDoPostContentLengthNegative");
380 LOGGER.error("Exception Occured"+e);
386 public void testDoPostContentTypeNonValid(){
387 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
389 Mockito.when(httpServletRequest.getContentType()).thenReturn(";");
390 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
392 pdpServlet.init(servletConfig);
393 pdpServlet.doPost(httpServletRequest, httpServletResponse);
395 }catch (Exception e){
396 System.out.println("Unexpected exception in testDoPostContentTypeNonValid");
397 LOGGER.error("Exception Occured"+e);
403 public void testDoPostContentTypeConfigurationError(){
404 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
406 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
407 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
409 pdpServlet.init(servletConfig);
410 pdpServlet.doPost(httpServletRequest, httpServletResponse);
412 }catch (Exception e){
413 System.out.println("Unexpected exception in testDoPostContentTypeConfigurationError");
414 LOGGER.error("Exception Occured"+e);
420 public void testDoPutCacheEmpty(){
421 LOGGER.info("XACMLPdpServletTest - testDoPutCacheEmpty");
422 mockInput = Mockito.mock(ServletInputStream.class);
425 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("cache");
426 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
427 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
428 pdpServlet.init(servletConfig);
429 pdpServlet.doPut(httpServletRequest, httpServletResponse);
430 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "PUT must contain at least one property");
432 }catch (Exception e){
433 System.out.println("Unexpected exception in testDoPutCacheEmpty");
434 LOGGER.error("Exception Occured"+e);
440 public void testDoPutConfigPolicies(){
441 LOGGER.info("XACMLPdpServletTest - testDoPutConfigPolicies");
442 byte[] b = new byte[20];
443 new Random().nextBytes(b);
445 mockInput = new MockServletInputStream(b);
448 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
449 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
450 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
451 pdpServlet.init(servletConfig);
452 pdpServlet.doPut(httpServletRequest, httpServletResponse);
454 }catch (Exception e){
455 System.out.println("Unexpected exception in testDoPutConfigPolicies");
456 LOGGER.error("Exception Occured"+e);
461 public void testDoPutToLong(){
462 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
464 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
466 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
467 Mockito.when(httpServletRequest.getContentLength()).thenReturn(1000000000);
469 pdpServlet.init(servletConfig);
470 pdpServlet.doPut(httpServletRequest, httpServletResponse);
471 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Content-Length larger than server will accept.");
473 }catch (Exception e){
474 System.out.println("Unexpected exception in testDoPutToLong");
475 LOGGER.error("Exception Occured"+e);
481 public void testDoPutInvalidContentType(){
482 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
484 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
486 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/json");
487 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
489 pdpServlet.init(servletConfig);
490 pdpServlet.doPut(httpServletRequest, httpServletResponse);
491 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid cache: 'policies' or content-type: 'text/json'");
493 }catch (Exception e){
494 System.out.println("Unexpected exception in testDoPutInvalidContentType");
495 LOGGER.error("Exception Occured"+e);
501 public void testDestroy(){
502 LOGGER.info("XACMLPdpServletTest - testDestroy");
505 pdpServlet.init(servletConfig);
506 pdpServlet.destroy();
508 System.out.println("Unexpected exception in testDestroy");
509 LOGGER.error("Exception Occured"+e);