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;
30 import javax.persistence.EntityManager;
31 import javax.persistence.EntityManagerFactory;
32 import javax.persistence.EntityTransaction;
33 import javax.persistence.Persistence;
34 import javax.servlet.ServletConfig;
35 import javax.servlet.ServletInputStream;
36 import javax.servlet.ServletOutputStream;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.Mockito;
43 import org.onap.policy.common.ia.DbDAO;
44 import org.onap.policy.common.ia.IntegrityAuditProperties;
45 import org.onap.policy.common.im.IntegrityMonitor;
46 import org.onap.policy.common.im.IntegrityMonitorException;
47 import org.onap.policy.common.logging.flexlogger.FlexLogger;
48 import org.onap.policy.common.logging.flexlogger.Logger;
49 import org.onap.policy.rest.XACMLRestProperties;
50 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
51 import org.onap.policy.xacml.std.pap.StdPDPStatus;
52 import org.powermock.api.mockito.PowerMockito;
53 import org.springframework.mock.web.MockHttpServletResponse;
54 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 public class XACMLPdpServletTest extends TestCase{
63 private static Logger LOGGER = FlexLogger.getLogger(XACMLPdpServletTest.class);
65 private List<String> headers = new ArrayList<>();
67 private HttpServletRequest httpServletRequest;
68 private HttpServletResponse httpServletResponse;
69 private ServletOutputStream mockOutput;
70 private ServletInputStream mockInput;
71 private ServletConfig servletConfig;
72 private XACMLPdpServlet pdpServlet;
73 private IntegrityMonitor im;
76 private String persistenceUnit;
77 private Properties properties;
78 private String resourceName;
79 private String dbDriver;
81 private String dbUser;
83 private String siteName;
84 private String nodeType;
85 private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";
86 private static final String DEFAULT_DB_USER = "sa";
87 private static final String DEFAULT_DB_PWD = "";
89 private StdPDPStatus status;
90 private StdPDPPolicy foobarPolicy;
94 status = new StdPDPStatus();
95 foobarPolicy = new StdPDPPolicy();
96 foobarPolicy.setId("foobar");
97 foobarPolicy.setVersion("123");
98 foobarPolicy.setName("nothing");
99 status.addLoadedPolicy(foobarPolicy);
102 properties = new Properties();
103 properties.put(IntegrityAuditProperties.DB_DRIVER, XACMLPdpServletTest.DEFAULT_DB_DRIVER);
104 properties.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/xacmlTest");
105 properties.put(IntegrityAuditProperties.DB_USER, XACMLPdpServletTest.DEFAULT_DB_USER);
106 properties.put(IntegrityAuditProperties.DB_PWD, XACMLPdpServletTest.DEFAULT_DB_PWD);
107 properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
108 properties.put(IntegrityAuditProperties.NODE_TYPE, "pap");
109 //properties.put("com.sun.management.jmxremote.port", "9999");
110 dbDriver = XACMLPdpServletTest.DEFAULT_DB_DRIVER;
111 dbUrl = "jdbc:h2:file:./sql/xacmlTest";
112 dbUser = XACMLPdpServletTest.DEFAULT_DB_USER;
113 dbPwd = XACMLPdpServletTest.DEFAULT_DB_PWD;
116 persistenceUnit = "testPdpPU";
117 resourceName = "siteA.pdp1";
119 System.setProperty("com.sun.management.jmxremote.port", "9999");
121 EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties);
123 EntityManager em = emf.createEntityManager();
124 // Start a transaction
125 EntityTransaction et = em.getTransaction();
127 IntegrityMonitor im = null;
129 im = IntegrityMonitor.getInstance(resourceName, properties);
130 } catch (Exception e2) {
131 // TODO Auto-generated catch block
132 e2.printStackTrace();
134 //cleanDb(persistenceUnit, properties);
136 httpServletRequest = Mockito.mock(HttpServletRequest.class);
137 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
138 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
139 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
140 Mockito.when(httpServletRequest.getRequestURI()).thenReturn("/pdp/test");
142 mockOutput = Mockito.mock(ServletOutputStream.class);
144 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
147 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
148 } catch (IOException e) {
152 servletConfig = Mockito.mock(MockServletConfig.class);
154 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
155 pdpServlet = new XACMLPdpServlet();
156 pdpServlet.setIm(im);
158 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("src/test/resources/xacml.pdp.properties");
160 System.setProperty("xacml.properties", "src/test/resources/xacml.pdp.properties");
161 System.setProperty("xacml.rest.pdp.config", "src/test/resources/config_testing");
162 System.setProperty("xacml.rest.pdp.webapps", "src/test/resources/webapps");
163 /*System.setProperty("xacml.rootPolicies", "test_PolicyEngine.xml");
164 System.setProperty("xacml.referencedPolicies", "test_PolicyEngine.xml");
165 System.setProperty("test_PolicyEngine.xml.file", "config_testing\\test_PolicyEngine.xml");
167 System.setProperty("xacml.rest.pdp.register", "false");
168 System.setProperty("com.sun.management.jmxremote.port", "9999");
170 im = Mockito.mock(IntegrityMonitor.class);
171 // Need PowerMockito for mocking static method getInstance(...)
172 PowerMockito.mockStatic(IntegrityMonitor.class);
174 // when IntegrityMonitor.getInstance is called, return the mock object
175 PowerMockito.when(IntegrityMonitor.getInstance(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(im);
176 } catch (Exception e1) {
177 LOGGER.error("Exception Occured"+e1);
181 Mockito.doNothing().when(im).startTransaction();
182 } catch (IntegrityMonitorException e) {
185 Mockito.doNothing().when(im).endTransaction();
189 public void testInit(){
190 LOGGER.info("XACMLPdpServletTest - testInit");
192 pdpServlet.init(servletConfig);
195 } catch (Exception e) {
196 LOGGER.error("Exception Occured"+e);
204 public void testUebNotification() {
205 System.out.println("drewtest1");
206 LOGGER.info("XACMLPdpServletTest - drewTest1");
209 XACMLProperties.reloadProperties();
210 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.ueb.properties");
211 XACMLProperties.getProperties();
212 XACMLProperties.setProperty(XACMLRestProperties.PROP_NOTIFICATION_TYPE, "ueb");
214 pdpServlet.init(servletConfig);
216 status.setStatus(com.att.research.xacml.api.pap.PDPStatus.Status.UPDATING_CONFIGURATION);
218 XACMLPdpLoader.validatePolicies(properties, status);
219 XACMLPdpLoader.sendNotification();
221 } catch (Exception e) {
222 LOGGER.error("Exception Occured"+e);
230 public void testDmaapNotification() {
231 System.out.println("drewtest2");
232 LOGGER.info("XACMLPdpServletTest - drewTest2");
235 XACMLProperties.reloadProperties();
236 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.dmaap.properties");
237 XACMLProperties.getProperties();
238 XACMLProperties.setProperty(XACMLRestProperties.PROP_NOTIFICATION_TYPE, "dmaap");
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 testDoGetNoTypeError(){
258 LOGGER.info("XACMLPdpServletTest - testDoGetNoTypeError");
261 pdpServlet.init(servletConfig);
262 pdpServlet.doGet(httpServletRequest, httpServletResponse);
263 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
266 System.out.println("Unexpected exception in testDoGetNoTypeError");
267 LOGGER.error("Exception Occured"+e);
273 public void testDoGetConfigType(){
274 LOGGER.info("XACMLPdpServletTest - testDoGetConfigType");
275 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("config");
278 pdpServlet.init(servletConfig);
279 pdpServlet.doGet(httpServletRequest, httpServletResponse);
280 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
282 }catch (Exception e){
283 System.out.println("Unexpected exception in testDoGetConfigType");
284 LOGGER.error("Exception Occured"+e);
291 public void testDoGetTypeHb(){
292 LOGGER.info("XACMLPdpServletTest - testDoGetTypeHb");
294 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb");
295 pdpServlet.init(servletConfig);
296 pdpServlet.doGet(httpServletRequest, httpServletResponse);
297 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
300 System.out.println("Unexpected exception in testDoGetTypeHb");
301 LOGGER.error("Exception Occured"+e);
307 public void testDoGetTypeStatus(){
308 LOGGER.info("XACMLPdpServletTest - testDoGetTypeStatus");
310 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("Status");
311 pdpServlet.init(servletConfig);
312 pdpServlet.doGet(httpServletRequest, httpServletResponse);
313 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
316 System.out.println("Unexpected exception in testDoGetTypeStatus");
317 LOGGER.error("Exception Occured"+e);
323 public void testDoPost(){
324 LOGGER.info("XACMLPdpServletTest - testDoPost");
326 pdpServlet.init(servletConfig);
327 pdpServlet.doPost(httpServletRequest, httpServletResponse);
329 }catch (Exception e){
330 System.out.println("Unexpected exception in testDoPost");
331 LOGGER.error("Exception Occured"+e);
337 public void testDoPostToLong(){
338 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
340 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
341 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
343 pdpServlet.init(servletConfig);
344 pdpServlet.doPost(httpServletRequest, httpServletResponse);
346 }catch (Exception e){
347 System.out.println("Unexpected exception in testDoPostToLong");
348 LOGGER.error("Exception Occured"+e);
354 public void testDoPostContentLengthNegative(){
355 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
357 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
358 Mockito.when(httpServletRequest.getContentLength()).thenReturn(-1);
360 pdpServlet.init(servletConfig);
361 pdpServlet.doPost(httpServletRequest, httpServletResponse);
363 }catch (Exception e){
364 System.out.println("Unexpected exception in testDoPostContentLengthNegative");
365 LOGGER.error("Exception Occured"+e);
371 public void testDoPostContentTypeNonValid(){
372 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
374 Mockito.when(httpServletRequest.getContentType()).thenReturn(";");
375 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
377 pdpServlet.init(servletConfig);
378 pdpServlet.doPost(httpServletRequest, httpServletResponse);
380 }catch (Exception e){
381 System.out.println("Unexpected exception in testDoPostContentTypeNonValid");
382 LOGGER.error("Exception Occured"+e);
388 public void testDoPostContentTypeConfigurationError(){
389 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
391 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
392 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
394 pdpServlet.init(servletConfig);
395 pdpServlet.doPost(httpServletRequest, httpServletResponse);
397 }catch (Exception e){
398 System.out.println("Unexpected exception in testDoPostContentTypeConfigurationError");
399 LOGGER.error("Exception Occured"+e);
405 public void testDoPutCacheEmpty(){
406 LOGGER.info("XACMLPdpServletTest - testDoPutCacheEmpty");
407 mockInput = Mockito.mock(ServletInputStream.class);
410 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("cache");
411 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
412 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
413 pdpServlet.init(servletConfig);
414 pdpServlet.doPut(httpServletRequest, httpServletResponse);
415 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "PUT must contain at least one property");
417 }catch (Exception e){
418 System.out.println("Unexpected exception in testDoPutCacheEmpty");
419 LOGGER.error("Exception Occured"+e);
425 public void testDoPutConfigPolicies(){
426 LOGGER.info("XACMLPdpServletTest - testDoPutConfigPolicies");
427 byte[] b = new byte[20];
428 new Random().nextBytes(b);
430 mockInput = new MockServletInputStream(b);
433 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
434 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
435 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
436 pdpServlet.init(servletConfig);
437 pdpServlet.doPut(httpServletRequest, httpServletResponse);
439 }catch (Exception e){
440 System.out.println("Unexpected exception in testDoPutConfigPolicies");
441 LOGGER.error("Exception Occured"+e);
446 public void testDoPutToLong(){
447 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
449 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
451 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
452 Mockito.when(httpServletRequest.getContentLength()).thenReturn(1000000000);
454 pdpServlet.init(servletConfig);
455 pdpServlet.doPut(httpServletRequest, httpServletResponse);
456 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Content-Length larger than server will accept.");
458 }catch (Exception e){
459 System.out.println("Unexpected exception in testDoPutToLong");
460 LOGGER.error("Exception Occured"+e);
466 public void testDoPutInvalidContentType(){
467 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
469 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
471 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/json");
472 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
474 pdpServlet.init(servletConfig);
475 pdpServlet.doPut(httpServletRequest, httpServletResponse);
476 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid cache: 'policies' or content-type: 'text/json'");
478 }catch (Exception e){
479 System.out.println("Unexpected exception in testDoPutInvalidContentType");
480 LOGGER.error("Exception Occured"+e);
486 public void testDestroy(){
487 LOGGER.info("XACMLPdpServletTest - testDestroy");
490 pdpServlet.init(servletConfig);
491 pdpServlet.destroy();
493 System.out.println("Unexpected exception in testDestroy");
494 LOGGER.error("Exception Occured"+e);