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.AdministrativeStateException;
46 import org.onap.policy.common.im.IntegrityMonitor;
47 import org.onap.policy.common.im.StandbyStatusException;
48 import org.onap.policy.common.logging.flexlogger.FlexLogger;
49 import org.onap.policy.common.logging.flexlogger.Logger;
51 import org.onap.policy.pdp.rest.XACMLPdpServletTest;
52 import org.powermock.api.mockito.PowerMockito;
53 import org.springframework.mock.web.MockHttpServletResponse;
54 import org.springframework.mock.web.MockServletConfig;
56 import com.mockrunner.mock.web.MockServletInputStream;
58 import junit.framework.TestCase;
60 public class XACMLPdpServletTest extends TestCase{
61 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 IntegrityMonitor im;
74 private String persistenceUnit;
75 private Properties properties;
76 private String resourceName;
77 private String dbDriver;
79 private String dbUser;
81 private String siteName;
82 private String nodeType;
83 private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";
84 private static final String DEFAULT_DB_USER = "sa";
85 private static final String DEFAULT_DB_PWD = "";
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 //properties.put("com.sun.management.jmxremote.port", "9999");
99 dbDriver = XACMLPdpServletTest.DEFAULT_DB_DRIVER;
100 dbUrl = "jdbc:h2:file:./sql/xacmlTest";
101 dbUser = XACMLPdpServletTest.DEFAULT_DB_USER;
102 dbPwd = XACMLPdpServletTest.DEFAULT_DB_PWD;
105 persistenceUnit = "testPdpPU";
106 resourceName = "siteA.pdp1";
108 System.setProperty("com.sun.management.jmxremote.port", "9999");
110 EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties);
112 EntityManager em = emf.createEntityManager();
113 // Start a transaction
114 EntityTransaction et = em.getTransaction();
116 IntegrityMonitor im = null;
118 im = IntegrityMonitor.getInstance(resourceName, properties);
119 } catch (Exception e2) {
120 // TODO Auto-generated catch block
121 e2.printStackTrace();
123 //cleanDb(persistenceUnit, properties);
125 httpServletRequest = Mockito.mock(HttpServletRequest.class);
126 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
127 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
128 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
129 Mockito.when(httpServletRequest.getRequestURI()).thenReturn("/pdp/test");
131 mockOutput = Mockito.mock(ServletOutputStream.class);
133 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
136 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
137 } catch (IOException e) {
141 servletConfig = Mockito.mock(MockServletConfig.class);
143 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
144 pdpServlet = new XACMLPdpServlet();
145 pdpServlet.setIm(im);
147 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("src/test/resources/xacml.pdp.properties");
149 System.setProperty("xacml.properties", "src/test/resources/xacml.pdp.properties");
150 System.setProperty("xacml.rest.pdp.config", "src/test/resources/config_testing");
151 System.setProperty("xacml.rest.pdp.webapps", "src/test/resources/webapps");
152 /*System.setProperty("xacml.rootPolicies", "test_PolicyEngine.xml");
153 System.setProperty("xacml.referencedPolicies", "test_PolicyEngine.xml");
154 System.setProperty("test_PolicyEngine.xml.file", "config_testing\\test_PolicyEngine.xml");
156 System.setProperty("xacml.rest.pdp.register", "false");
157 System.setProperty("com.sun.management.jmxremote.port", "9999");
159 im = Mockito.mock(IntegrityMonitor.class);
160 // Need PowerMockito for mocking static method getInstance(...)
161 PowerMockito.mockStatic(IntegrityMonitor.class);
163 // when IntegrityMonitor.getInstance is called, return the mock object
164 PowerMockito.when(IntegrityMonitor.getInstance(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(im);
165 } catch (Exception e1) {
166 LOGGER.error("Exception Occured"+e1);
170 Mockito.doNothing().when(im).startTransaction();
171 } catch (StandbyStatusException | AdministrativeStateException e) {
174 Mockito.doNothing().when(im).endTransaction();
178 public void testInit(){
179 LOGGER.info("XACMLPdpServletTest - testInit");
181 pdpServlet.init(servletConfig);
183 } catch (Exception e) {
184 LOGGER.error("Exception Occured"+e);
192 public void testDoGetNoTypeError(){
193 LOGGER.info("XACMLPdpServletTest - testDoGetNoTypeError");
196 pdpServlet.init(servletConfig);
197 pdpServlet.doGet(httpServletRequest, httpServletResponse);
198 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
201 System.out.println("Unexpected exception in testDoGetNoTypeError");
202 LOGGER.error("Exception Occured"+e);
208 public void testDoGetConfigType(){
209 LOGGER.info("XACMLPdpServletTest - testDoGetConfigType");
210 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("config");
213 pdpServlet.init(servletConfig);
214 pdpServlet.doGet(httpServletRequest, httpServletResponse);
215 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
217 }catch (Exception e){
218 System.out.println("Unexpected exception in testDoGetConfigType");
219 LOGGER.error("Exception Occured"+e);
226 public void testDoGetTypeHb(){
227 LOGGER.info("XACMLPdpServletTest - testDoGetTypeHb");
229 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb");
230 pdpServlet.init(servletConfig);
231 pdpServlet.doGet(httpServletRequest, httpServletResponse);
232 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
235 System.out.println("Unexpected exception in testDoGetTypeHb");
236 LOGGER.error("Exception Occured"+e);
242 public void testDoGetTypeStatus(){
243 LOGGER.info("XACMLPdpServletTest - testDoGetTypeStatus");
245 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("Status");
246 pdpServlet.init(servletConfig);
247 pdpServlet.doGet(httpServletRequest, httpServletResponse);
248 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
251 System.out.println("Unexpected exception in testDoGetTypeStatus");
252 LOGGER.error("Exception Occured"+e);
258 public void testDoPost(){
259 LOGGER.info("XACMLPdpServletTest - testDoPost");
261 pdpServlet.init(servletConfig);
262 pdpServlet.doPost(httpServletRequest, httpServletResponse);
264 }catch (Exception e){
265 System.out.println("Unexpected exception in testDoPost");
266 LOGGER.error("Exception Occured"+e);
272 public void testDoPostToLong(){
273 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
275 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
276 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
278 pdpServlet.init(servletConfig);
279 pdpServlet.doPost(httpServletRequest, httpServletResponse);
281 }catch (Exception e){
282 System.out.println("Unexpected exception in testDoPostToLong");
283 LOGGER.error("Exception Occured"+e);
289 public void testDoPostContentLengthNegative(){
290 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
292 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
293 Mockito.when(httpServletRequest.getContentLength()).thenReturn(-1);
295 pdpServlet.init(servletConfig);
296 pdpServlet.doPost(httpServletRequest, httpServletResponse);
298 }catch (Exception e){
299 System.out.println("Unexpected exception in testDoPostContentLengthNegative");
300 LOGGER.error("Exception Occured"+e);
306 public void testDoPostContentTypeNonValid(){
307 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
309 Mockito.when(httpServletRequest.getContentType()).thenReturn(";");
310 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
312 pdpServlet.init(servletConfig);
313 pdpServlet.doPost(httpServletRequest, httpServletResponse);
315 }catch (Exception e){
316 System.out.println("Unexpected exception in testDoPostContentTypeNonValid");
317 LOGGER.error("Exception Occured"+e);
323 public void testDoPostContentTypeConfigurationError(){
324 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
326 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
327 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
329 pdpServlet.init(servletConfig);
330 pdpServlet.doPost(httpServletRequest, httpServletResponse);
332 }catch (Exception e){
333 System.out.println("Unexpected exception in testDoPostContentTypeConfigurationError");
334 LOGGER.error("Exception Occured"+e);
340 public void testDoPutCacheEmpty(){
341 LOGGER.info("XACMLPdpServletTest - testDoPutCacheEmpty");
342 mockInput = Mockito.mock(ServletInputStream.class);
345 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("cache");
346 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
347 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
348 pdpServlet.init(servletConfig);
349 pdpServlet.doPut(httpServletRequest, httpServletResponse);
350 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "PUT must contain at least one property");
352 }catch (Exception e){
353 System.out.println("Unexpected exception in testDoPutCacheEmpty");
354 LOGGER.error("Exception Occured"+e);
360 public void testDoPutConfigPolicies(){
361 LOGGER.info("XACMLPdpServletTest - testDoPutConfigPolicies");
362 byte[] b = new byte[20];
363 new Random().nextBytes(b);
365 mockInput = new MockServletInputStream(b);
368 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
369 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
370 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
371 pdpServlet.init(servletConfig);
372 pdpServlet.doPut(httpServletRequest, httpServletResponse);
374 }catch (Exception e){
375 System.out.println("Unexpected exception in testDoPutConfigPolicies");
376 LOGGER.error("Exception Occured"+e);
381 public void testDoPutToLong(){
382 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
384 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
386 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
387 Mockito.when(httpServletRequest.getContentLength()).thenReturn(1000000000);
389 pdpServlet.init(servletConfig);
390 pdpServlet.doPut(httpServletRequest, httpServletResponse);
391 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Content-Length larger than server will accept.");
393 }catch (Exception e){
394 System.out.println("Unexpected exception in testDoPutToLong");
395 LOGGER.error("Exception Occured"+e);
401 public void testDoPutInvalidContentType(){
402 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
404 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
406 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/json");
407 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
409 pdpServlet.init(servletConfig);
410 pdpServlet.doPut(httpServletRequest, httpServletResponse);
411 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid cache: 'policies' or content-type: 'text/json'");
413 }catch (Exception e){
414 System.out.println("Unexpected exception in testDoPutInvalidContentType");
415 LOGGER.error("Exception Occured"+e);
421 public void testDestroy(){
422 LOGGER.info("XACMLPdpServletTest - testDestroy");
425 pdpServlet.init(servletConfig);
426 pdpServlet.destroy();
428 System.out.println("Unexpected exception in testDestroy");
429 LOGGER.error("Exception Occured"+e);