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.openecomp.policy.pdp.rest;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Properties;
29 import java.util.Random;
31 import javax.servlet.ServletConfig;
32 import javax.servlet.ServletInputStream;
33 import javax.servlet.ServletOutputStream;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
37 import junit.framework.TestCase;
39 import org.apache.commons.lang.math.RandomUtils;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42 import org.junit.Before;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mockito;
45 import org.openecomp.policy.pdp.rest.XACMLPdpServlet;
46 import org.springframework.mock.web.MockHttpServletResponse;
47 import org.springframework.mock.web.MockServletConfig;
48 import org.powermock.api.mockito.PowerMockito;
49 import org.powermock.core.classloader.annotations.PrepareForTest;
50 import org.powermock.modules.junit4.PowerMockRunner;
52 import org.openecomp.policy.common.im.AdministrativeStateException;
53 import org.openecomp.policy.common.im.IntegrityMonitor;
54 import org.openecomp.policy.common.im.StandbyStatusException;
56 import com.att.research.xacml.util.XACMLProperties;
57 import com.mockrunner.mock.web.MockServletInputStream;
58 import org.openecomp.policy.common.logging.flexlogger.*;
60 @RunWith(PowerMockRunner.class)
61 @PrepareForTest(IntegrityMonitor.class) // so PowerMock can mock static method of IntegrityMonitor
62 public class XACMLPdpServletTest extends TestCase{
63 private static Logger logger = FlexLogger.getLogger(XACMLPdpServletTest.class);
65 private List<String> headers = new ArrayList<String>();
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;
79 httpServletRequest = Mockito.mock(HttpServletRequest.class);
80 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
81 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
82 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
85 mockOutput = Mockito.mock(ServletOutputStream.class);
87 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
90 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
91 } catch (IOException e) {
92 // TODO Auto-generated catch block
97 //when(httpServletResponse.getOutputStream()).thenReturn(servletOutputStream);
98 servletConfig = Mockito.mock(MockServletConfig.class);
99 //Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
101 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
102 pdpServlet = new XACMLPdpServlet();
104 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("xacml.pdp.properties");
106 System.setProperty("xacml.properties", "xacml.pdp.properties");
107 System.setProperty("xacml.rest.pdp.config", "config_testing");
108 System.setProperty("xacml.rest.pep.idfile", "testclient.properties");
109 System.setProperty("xacml.rest.pdp.webapps", "/webapps");
110 System.setProperty("xacml.rootPolicies", "test_PolicyEngine.xml");
111 System.setProperty("xacml.referencedPolicies", "test_PolicyEngine.xml");
112 System.setProperty("test_PolicyEngine.xml.file", "config_testing\\test_PolicyEngine.xml");
113 System.setProperty("xacml.rest.pdp.register", "false");
114 System.setProperty("com.sun.management.jmxremote.port", "9999");
116 im = Mockito.mock(IntegrityMonitor.class);
117 // Need PowerMockito for mocking static method getInstance(...)
118 PowerMockito.mockStatic(IntegrityMonitor.class);
120 // when IntegrityMonitor.getInstance is called, return the mock object
121 PowerMockito.when(IntegrityMonitor.getInstance(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(im);
122 } catch (Exception e1) {
123 // TODO Auto-generated catch block
124 e1.printStackTrace();
128 Mockito.doNothing().when(im).startTransaction();
129 } catch (StandbyStatusException | AdministrativeStateException e) {
132 Mockito.doNothing().when(im).endTransaction();
135 public void testInit(){
136 logger.info("XACMLPdpServletTest - testInit");
138 pdpServlet.init(servletConfig);
140 } catch (Exception e) {
141 // TODO Auto-generated catch block
142 System.out.println("Unexpected exception in testInit");
150 public void testDoGetNoTypeError(){
151 logger.info("XACMLPdpServletTest - testDoGetNoTypeError");
153 pdpServlet.init(servletConfig);
154 pdpServlet.doGet(httpServletRequest, httpServletResponse);
155 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "type not 'config' or 'hb'");
158 System.out.println("Unexpected exception in testDoGetNoTypeError");
164 public void testDoGetConfigType(){
165 logger.info("XACMLPdpServletTest - testDoGetConfigType");
166 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("config");
169 pdpServlet.init(servletConfig);
170 pdpServlet.doGet(httpServletRequest, httpServletResponse);
171 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
172 //Mockito.verify(httpServletResponse).sendError(400, "Failed to copy Property file");
174 }catch (Exception e){
175 System.out.println("Unexpected exception in testDoGetConfigType");
183 public void testDoGetTypeHb(){
184 logger.info("XACMLPdpServletTest - testDoGetTypeHb");
186 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb");
187 pdpServlet.init(servletConfig);
188 pdpServlet.doGet(httpServletRequest, httpServletResponse);
189 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_NO_CONTENT);
192 System.out.println("Unexpected exception in testDoGetTypeHb");
197 public void testDoGetTypeStatus(){
198 logger.info("XACMLPdpServletTest - testDoGetTypeStatus");
200 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("Status");
201 pdpServlet.init(servletConfig);
202 pdpServlet.doGet(httpServletRequest, httpServletResponse);
203 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
206 System.out.println("Unexpected exception in testDoGetTypeStatus");
212 public void testDoPost(){
213 logger.info("XACMLPdpServletTest - testDoPost");
215 pdpServlet.init(servletConfig);
216 pdpServlet.doPost(httpServletRequest, httpServletResponse);
217 //response.sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given");
218 //Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given");
220 }catch (Exception e){
221 System.out.println("Unexpected exception in testDoPost");
227 public void testDoPostToLong(){
228 logger.info("XACMLPdpServletTest - testDoPostToLong");
230 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
231 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
233 pdpServlet.init(servletConfig);
234 pdpServlet.doPost(httpServletRequest, httpServletResponse);
235 //response.sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given");
236 //Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Content-Length larger than server will accept.");
238 }catch (Exception e){
239 System.out.println("Unexpected exception in testDoPostToLong");
245 public void testDoPostContentLengthNegative(){
246 logger.info("XACMLPdpServletTest - testDoPostToLong");
248 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
249 Mockito.when(httpServletRequest.getContentLength()).thenReturn(-1);
251 pdpServlet.init(servletConfig);
252 pdpServlet.doPost(httpServletRequest, httpServletResponse);
253 //response.sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given");
254 //Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Content-Length is negative");
256 }catch (Exception e){
257 System.out.println("Unexpected exception in testDoPostContentLengthNegative");
263 public void testDoPostContentTypeNonValid(){
264 logger.info("XACMLPdpServletTest - testDoPostToLong");
266 Mockito.when(httpServletRequest.getContentType()).thenReturn(";");
267 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
269 pdpServlet.init(servletConfig);
270 pdpServlet.doPost(httpServletRequest, httpServletResponse);
271 //response.sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given");
272 //Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Parsing Content-Type: ;, error=Invalid content type: ;");
274 }catch (Exception e){
275 System.out.println("Unexpected exception in testDoPostContentTypeNonValid");
281 public void testDoPostContentTypeConfigurationError(){
282 logger.info("XACMLPdpServletTest - testDoPostToLong");
284 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
285 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
287 pdpServlet.init(servletConfig);
288 pdpServlet.doPost(httpServletRequest, httpServletResponse);
289 //response.sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given");
290 //Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "unsupported content typestuff");
292 }catch (Exception e){
293 System.out.println("Unexpected exception in testDoPostContentTypeConfigurationError");
299 public void testDoPutCacheEmpty(){
300 logger.info("XACMLPdpServletTest - testDoPutCacheEmpty");
301 mockInput = Mockito.mock(ServletInputStream.class);
304 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("cache");
305 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
306 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
307 pdpServlet.init(servletConfig);
308 pdpServlet.doPut(httpServletRequest, httpServletResponse);
309 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "PUT must contain at least one property");
311 }catch (Exception e){
312 System.out.println("Unexpected exception in testDoPutCacheEmpty");
318 public void testDoPutConfigPolicies(){
319 logger.info("XACMLPdpServletTest - testDoPutConfigPolicies");
320 byte[] b = new byte[20];
321 new Random().nextBytes(b);
323 mockInput = new MockServletInputStream(b);
326 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
327 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
328 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
329 pdpServlet.init(servletConfig);
330 pdpServlet.doPut(httpServletRequest, httpServletResponse);
332 * Started failing 8/17/16
333 * Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Missing property: xacml.rootPolicies");
336 }catch (Exception e){
337 System.out.println("Unexpected exception in testDoPutConfigPolicies");
343 public void testDoPutToLong(){
344 logger.info("XACMLPdpServletTest - testDoPutToLong");
346 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
348 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
349 Mockito.when(httpServletRequest.getContentLength()).thenReturn(1000000000);
351 pdpServlet.init(servletConfig);
352 pdpServlet.doPut(httpServletRequest, httpServletResponse);
353 //response.sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given");
354 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Content-Length larger than server will accept.");
356 }catch (Exception e){
357 System.out.println("Unexpected exception in testDoPutToLong");
363 public void testDoPutInvalidContentType(){
364 logger.info("XACMLPdpServletTest - testDoPutToLong");
366 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
368 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/json");
369 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
371 pdpServlet.init(servletConfig);
372 pdpServlet.doPut(httpServletRequest, httpServletResponse);
373 //response.sendError(HttpServletResponse.SC_BAD_REQUEST, "no content-type given");
374 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid cache: 'policies' or content-type: 'text/json'");
376 }catch (Exception e){
377 System.out.println("Unexpected exception in testDoPutInvalidContentType");
383 public void testDestroy(){
384 logger.info("XACMLPdpServletTest - testDestroy");
387 pdpServlet.init(servletConfig);
388 pdpServlet.destroy();
390 System.out.println("Unexpected exception in testDestroy");