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.servlet.ServletConfig;
31 import javax.servlet.ServletInputStream;
32 import javax.servlet.ServletOutputStream;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
36 import org.junit.Before;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mockito;
39 import org.onap.policy.common.im.AdministrativeStateException;
40 import org.onap.policy.common.im.IntegrityMonitor;
41 import org.onap.policy.common.im.StandbyStatusException;
42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
43 import org.onap.policy.common.logging.flexlogger.Logger;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47 import org.springframework.mock.web.MockHttpServletResponse;
48 import org.springframework.mock.web.MockServletConfig;
50 import com.mockrunner.mock.web.MockServletInputStream;
52 import junit.framework.TestCase;
54 @RunWith(PowerMockRunner.class)
55 @PrepareForTest(IntegrityMonitor.class) // so PowerMock can mock static method of IntegrityMonitor
56 public class XACMLPdpServletTest extends TestCase{
57 private static Logger LOGGER = FlexLogger.getLogger(XACMLPdpServletTest.class);
59 private List<String> headers = new ArrayList<>();
61 private HttpServletRequest httpServletRequest;
62 private HttpServletResponse httpServletResponse;
63 private ServletOutputStream mockOutput;
64 private ServletInputStream mockInput;
65 private ServletConfig servletConfig;
66 private XACMLPdpServlet pdpServlet;
67 private IntegrityMonitor im;
73 httpServletRequest = Mockito.mock(HttpServletRequest.class);
74 Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
75 Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
76 Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
79 mockOutput = Mockito.mock(ServletOutputStream.class);
81 httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
84 Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
85 } catch (IOException e) {
89 servletConfig = Mockito.mock(MockServletConfig.class);
91 Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
92 pdpServlet = new XACMLPdpServlet();
94 Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("xacml.pdp.properties");
96 System.setProperty("xacml.properties", "xacml.pdp.properties");
97 System.setProperty("xacml.rest.pdp.config", "config_testing");
98 System.setProperty("xacml.rest.pdp.webapps", "/webapps");
99 System.setProperty("xacml.rootPolicies", "test_PolicyEngine.xml");
100 System.setProperty("xacml.referencedPolicies", "test_PolicyEngine.xml");
101 System.setProperty("test_PolicyEngine.xml.file", "config_testing\\test_PolicyEngine.xml");
102 System.setProperty("xacml.rest.pdp.register", "false");
103 System.setProperty("com.sun.management.jmxremote.port", "9999");
105 im = Mockito.mock(IntegrityMonitor.class);
106 // Need PowerMockito for mocking static method getInstance(...)
107 PowerMockito.mockStatic(IntegrityMonitor.class);
109 // when IntegrityMonitor.getInstance is called, return the mock object
110 PowerMockito.when(IntegrityMonitor.getInstance(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(im);
111 } catch (Exception e1) {
112 LOGGER.error("Exception Occured"+e1);
116 Mockito.doNothing().when(im).startTransaction();
117 } catch (StandbyStatusException | AdministrativeStateException e) {
120 Mockito.doNothing().when(im).endTransaction();
123 public void testInit(){
124 LOGGER.info("XACMLPdpServletTest - testInit");
126 pdpServlet.init(servletConfig);
128 } catch (Exception e) {
129 LOGGER.error("Exception Occured"+e);
136 public void testDoGetNoTypeError(){
137 LOGGER.info("XACMLPdpServletTest - testDoGetNoTypeError");
139 pdpServlet.init(servletConfig);
140 pdpServlet.doGet(httpServletRequest, httpServletResponse);
141 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "type not 'config' or 'hb'");
144 System.out.println("Unexpected exception in testDoGetNoTypeError");
145 LOGGER.error("Exception Occured"+e);
150 public void testDoGetConfigType(){
151 LOGGER.info("XACMLPdpServletTest - testDoGetConfigType");
152 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("config");
155 pdpServlet.init(servletConfig);
156 pdpServlet.doGet(httpServletRequest, httpServletResponse);
157 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
159 }catch (Exception e){
160 System.out.println("Unexpected exception in testDoGetConfigType");
161 LOGGER.error("Exception Occured"+e);
168 public void testDoGetTypeHb(){
169 LOGGER.info("XACMLPdpServletTest - testDoGetTypeHb");
171 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb");
172 pdpServlet.init(servletConfig);
173 pdpServlet.doGet(httpServletRequest, httpServletResponse);
174 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_NO_CONTENT);
177 System.out.println("Unexpected exception in testDoGetTypeHb");
178 LOGGER.error("Exception Occured"+e);
182 public void testDoGetTypeStatus(){
183 LOGGER.info("XACMLPdpServletTest - testDoGetTypeStatus");
185 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("Status");
186 pdpServlet.init(servletConfig);
187 pdpServlet.doGet(httpServletRequest, httpServletResponse);
188 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
191 System.out.println("Unexpected exception in testDoGetTypeStatus");
192 LOGGER.error("Exception Occured"+e);
197 public void testDoPost(){
198 LOGGER.info("XACMLPdpServletTest - testDoPost");
200 pdpServlet.init(servletConfig);
201 pdpServlet.doPost(httpServletRequest, httpServletResponse);
203 }catch (Exception e){
204 System.out.println("Unexpected exception in testDoPost");
205 LOGGER.error("Exception Occured"+e);
210 public void testDoPostToLong(){
211 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
213 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
214 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
216 pdpServlet.init(servletConfig);
217 pdpServlet.doPost(httpServletRequest, httpServletResponse);
219 }catch (Exception e){
220 System.out.println("Unexpected exception in testDoPostToLong");
221 LOGGER.error("Exception Occured"+e);
226 public void testDoPostContentLengthNegative(){
227 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
229 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
230 Mockito.when(httpServletRequest.getContentLength()).thenReturn(-1);
232 pdpServlet.init(servletConfig);
233 pdpServlet.doPost(httpServletRequest, httpServletResponse);
235 }catch (Exception e){
236 System.out.println("Unexpected exception in testDoPostContentLengthNegative");
237 LOGGER.error("Exception Occured"+e);
242 public void testDoPostContentTypeNonValid(){
243 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
245 Mockito.when(httpServletRequest.getContentType()).thenReturn(";");
246 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
248 pdpServlet.init(servletConfig);
249 pdpServlet.doPost(httpServletRequest, httpServletResponse);
251 }catch (Exception e){
252 System.out.println("Unexpected exception in testDoPostContentTypeNonValid");
253 LOGGER.error("Exception Occured"+e);
258 public void testDoPostContentTypeConfigurationError(){
259 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
261 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
262 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
264 pdpServlet.init(servletConfig);
265 pdpServlet.doPost(httpServletRequest, httpServletResponse);
267 }catch (Exception e){
268 System.out.println("Unexpected exception in testDoPostContentTypeConfigurationError");
269 LOGGER.error("Exception Occured"+e);
274 public void testDoPutCacheEmpty(){
275 LOGGER.info("XACMLPdpServletTest - testDoPutCacheEmpty");
276 mockInput = Mockito.mock(ServletInputStream.class);
279 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("cache");
280 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
281 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
282 pdpServlet.init(servletConfig);
283 pdpServlet.doPut(httpServletRequest, httpServletResponse);
284 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "PUT must contain at least one property");
286 }catch (Exception e){
287 System.out.println("Unexpected exception in testDoPutCacheEmpty");
288 LOGGER.error("Exception Occured"+e);
293 public void testDoPutConfigPolicies(){
294 LOGGER.info("XACMLPdpServletTest - testDoPutConfigPolicies");
295 byte[] b = new byte[20];
296 new Random().nextBytes(b);
298 mockInput = new MockServletInputStream(b);
301 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
302 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
303 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
304 pdpServlet.init(servletConfig);
305 pdpServlet.doPut(httpServletRequest, httpServletResponse);
307 }catch (Exception e){
308 System.out.println("Unexpected exception in testDoPutConfigPolicies");
309 LOGGER.error("Exception Occured"+e);
314 public void testDoPutToLong(){
315 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
317 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
319 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
320 Mockito.when(httpServletRequest.getContentLength()).thenReturn(1000000000);
322 pdpServlet.init(servletConfig);
323 pdpServlet.doPut(httpServletRequest, httpServletResponse);
324 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Content-Length larger than server will accept.");
326 }catch (Exception e){
327 System.out.println("Unexpected exception in testDoPutToLong");
328 LOGGER.error("Exception Occured"+e);
333 public void testDoPutInvalidContentType(){
334 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
336 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
338 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/json");
339 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
341 pdpServlet.init(servletConfig);
342 pdpServlet.doPut(httpServletRequest, httpServletResponse);
343 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid cache: 'policies' or content-type: 'text/json'");
345 }catch (Exception e){
346 System.out.println("Unexpected exception in testDoPutInvalidContentType");
347 LOGGER.error("Exception Occured"+e);
352 public void testDestroy(){
353 LOGGER.info("XACMLPdpServletTest - testDestroy");
356 pdpServlet.init(servletConfig);
357 pdpServlet.destroy();
359 System.out.println("Unexpected exception in testDestroy");
360 LOGGER.error("Exception Occured"+e);