2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.powermock.api.mockito.PowerMockito;
50 import org.springframework.mock.web.MockHttpServletResponse;
51 import org.springframework.mock.web.MockServletConfig;
53 import com.mockrunner.mock.web.MockServletInputStream;
55 import junit.framework.TestCase;
57 public class XACMLPdpServletTest extends TestCase{
58 private static Logger LOGGER = FlexLogger.getLogger(XACMLPdpServletTest.class);
60 private List<String> headers = new ArrayList<>();
62 private HttpServletRequest httpServletRequest;
63 private HttpServletResponse httpServletResponse;
64 private ServletOutputStream mockOutput;
65 private ServletInputStream mockInput;
66 private ServletConfig servletConfig;
67 private XACMLPdpServlet pdpServlet;
68 private IntegrityMonitor im;
71 private String persistenceUnit;
72 private Properties properties;
73 private String resourceName;
74 private String dbDriver;
76 private String dbUser;
78 private String siteName;
79 private String nodeType;
80 private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";
81 private static final String DEFAULT_DB_USER = "sa";
82 private static final String DEFAULT_DB_PWD = "";
88 properties = new Properties();
89 properties.put(IntegrityAuditProperties.DB_DRIVER, XACMLPdpServletTest.DEFAULT_DB_DRIVER);
90 properties.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/xacmlTest");
91 properties.put(IntegrityAuditProperties.DB_USER, XACMLPdpServletTest.DEFAULT_DB_USER);
92 properties.put(IntegrityAuditProperties.DB_PWD, XACMLPdpServletTest.DEFAULT_DB_PWD);
93 properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
94 properties.put(IntegrityAuditProperties.NODE_TYPE, "pap");
95 //properties.put("com.sun.management.jmxremote.port", "9999");
96 dbDriver = XACMLPdpServletTest.DEFAULT_DB_DRIVER;
97 dbUrl = "jdbc:h2:file:./sql/xacmlTest";
98 dbUser = XACMLPdpServletTest.DEFAULT_DB_USER;
99 dbPwd = XACMLPdpServletTest.DEFAULT_DB_PWD;
102 persistenceUnit = "testPdpPU";
103 resourceName = "siteA.pdp1";
105 System.setProperty("com.sun.management.jmxremote.port", "9999");
107 EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties);
109 EntityManager em = emf.createEntityManager();
110 // Start a transaction
111 EntityTransaction et = em.getTransaction();
113 IntegrityMonitor im = null;
115 im = IntegrityMonitor.getInstance(resourceName, properties);
116 } catch (Exception e2) {
117 // TODO Auto-generated catch block
118 e2.printStackTrace();
120 //cleanDb(persistenceUnit, properties);
122 httpServletRequest = Mockito.mock(HttpServletRequest.class);
123 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
124 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
125 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
126 Mockito.when(httpServletRequest.getRequestURI()).thenReturn("/pdp/test");
128 mockOutput = Mockito.mock(ServletOutputStream.class);
130 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
133 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
134 } catch (IOException e) {
138 servletConfig = Mockito.mock(MockServletConfig.class);
140 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
141 pdpServlet = new XACMLPdpServlet();
142 pdpServlet.setIm(im);
144 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("src/test/resources/xacml.pdp.properties");
146 System.setProperty("xacml.properties", "src/test/resources/xacml.pdp.properties");
147 System.setProperty("xacml.rest.pdp.config", "src/test/resources/config_testing");
148 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
161 PowerMockito.when(IntegrityMonitor.getInstance(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(im);
162 } catch (Exception e1) {
163 LOGGER.error("Exception Occured"+e1);
167 Mockito.doNothing().when(im).startTransaction();
168 } catch (IntegrityMonitorException e) {
171 Mockito.doNothing().when(im).endTransaction();
175 public void testInit(){
176 LOGGER.info("XACMLPdpServletTest - testInit");
178 pdpServlet.init(servletConfig);
180 } catch (Exception e) {
181 LOGGER.error("Exception Occured"+e);
189 public void testDoGetNoTypeError(){
190 LOGGER.info("XACMLPdpServletTest - testDoGetNoTypeError");
193 pdpServlet.init(servletConfig);
194 pdpServlet.doGet(httpServletRequest, httpServletResponse);
195 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
198 System.out.println("Unexpected exception in testDoGetNoTypeError");
199 LOGGER.error("Exception Occured"+e);
205 public void testDoGetConfigType(){
206 LOGGER.info("XACMLPdpServletTest - testDoGetConfigType");
207 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("config");
210 pdpServlet.init(servletConfig);
211 pdpServlet.doGet(httpServletRequest, httpServletResponse);
212 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
214 }catch (Exception e){
215 System.out.println("Unexpected exception in testDoGetConfigType");
216 LOGGER.error("Exception Occured"+e);
223 public void testDoGetTypeHb(){
224 LOGGER.info("XACMLPdpServletTest - testDoGetTypeHb");
226 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb");
227 pdpServlet.init(servletConfig);
228 pdpServlet.doGet(httpServletRequest, httpServletResponse);
229 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
232 System.out.println("Unexpected exception in testDoGetTypeHb");
233 LOGGER.error("Exception Occured"+e);
239 public void testDoGetTypeStatus(){
240 LOGGER.info("XACMLPdpServletTest - testDoGetTypeStatus");
242 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("Status");
243 pdpServlet.init(servletConfig);
244 pdpServlet.doGet(httpServletRequest, httpServletResponse);
245 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
248 System.out.println("Unexpected exception in testDoGetTypeStatus");
249 LOGGER.error("Exception Occured"+e);
255 public void testDoPost(){
256 LOGGER.info("XACMLPdpServletTest - testDoPost");
258 pdpServlet.init(servletConfig);
259 pdpServlet.doPost(httpServletRequest, httpServletResponse);
261 }catch (Exception e){
262 System.out.println("Unexpected exception in testDoPost");
263 LOGGER.error("Exception Occured"+e);
269 public void testDoPostToLong(){
270 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
272 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
273 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
275 pdpServlet.init(servletConfig);
276 pdpServlet.doPost(httpServletRequest, httpServletResponse);
278 }catch (Exception e){
279 System.out.println("Unexpected exception in testDoPostToLong");
280 LOGGER.error("Exception Occured"+e);
286 public void testDoPostContentLengthNegative(){
287 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
289 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
290 Mockito.when(httpServletRequest.getContentLength()).thenReturn(-1);
292 pdpServlet.init(servletConfig);
293 pdpServlet.doPost(httpServletRequest, httpServletResponse);
295 }catch (Exception e){
296 System.out.println("Unexpected exception in testDoPostContentLengthNegative");
297 LOGGER.error("Exception Occured"+e);
303 public void testDoPostContentTypeNonValid(){
304 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
306 Mockito.when(httpServletRequest.getContentType()).thenReturn(";");
307 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
309 pdpServlet.init(servletConfig);
310 pdpServlet.doPost(httpServletRequest, httpServletResponse);
312 }catch (Exception e){
313 System.out.println("Unexpected exception in testDoPostContentTypeNonValid");
314 LOGGER.error("Exception Occured"+e);
320 public void testDoPostContentTypeConfigurationError(){
321 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
323 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
324 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
326 pdpServlet.init(servletConfig);
327 pdpServlet.doPost(httpServletRequest, httpServletResponse);
329 }catch (Exception e){
330 System.out.println("Unexpected exception in testDoPostContentTypeConfigurationError");
331 LOGGER.error("Exception Occured"+e);
337 public void testDoPutCacheEmpty(){
338 LOGGER.info("XACMLPdpServletTest - testDoPutCacheEmpty");
339 mockInput = Mockito.mock(ServletInputStream.class);
342 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("cache");
343 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
344 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
345 pdpServlet.init(servletConfig);
346 pdpServlet.doPut(httpServletRequest, httpServletResponse);
347 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "PUT must contain at least one property");
349 }catch (Exception e){
350 System.out.println("Unexpected exception in testDoPutCacheEmpty");
351 LOGGER.error("Exception Occured"+e);
357 public void testDoPutConfigPolicies(){
358 LOGGER.info("XACMLPdpServletTest - testDoPutConfigPolicies");
359 byte[] b = new byte[20];
360 new Random().nextBytes(b);
362 mockInput = new MockServletInputStream(b);
365 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
366 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
367 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
368 pdpServlet.init(servletConfig);
369 pdpServlet.doPut(httpServletRequest, httpServletResponse);
371 }catch (Exception e){
372 System.out.println("Unexpected exception in testDoPutConfigPolicies");
373 LOGGER.error("Exception Occured"+e);
378 public void testDoPutToLong(){
379 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
381 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
383 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
384 Mockito.when(httpServletRequest.getContentLength()).thenReturn(1000000000);
386 pdpServlet.init(servletConfig);
387 pdpServlet.doPut(httpServletRequest, httpServletResponse);
388 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Content-Length larger than server will accept.");
390 }catch (Exception e){
391 System.out.println("Unexpected exception in testDoPutToLong");
392 LOGGER.error("Exception Occured"+e);
398 public void testDoPutInvalidContentType(){
399 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
401 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
403 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/json");
404 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
406 pdpServlet.init(servletConfig);
407 pdpServlet.doPut(httpServletRequest, httpServletResponse);
408 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid cache: 'policies' or content-type: 'text/json'");
410 }catch (Exception e){
411 System.out.println("Unexpected exception in testDoPutInvalidContentType");
412 LOGGER.error("Exception Occured"+e);
418 public void testDestroy(){
419 LOGGER.info("XACMLPdpServletTest - testDestroy");
422 pdpServlet.init(servletConfig);
423 pdpServlet.destroy();
425 System.out.println("Unexpected exception in testDestroy");
426 LOGGER.error("Exception Occured"+e);