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.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 junit.framework.TestCase;
38 import org.junit.Before;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mockito;
41 import org.openecomp.policy.common.im.AdministrativeStateException;
42 import org.openecomp.policy.common.im.IntegrityMonitor;
43 import org.openecomp.policy.common.im.StandbyStatusException;
44 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
45 import org.openecomp.policy.common.logging.flexlogger.Logger;
46 import org.powermock.api.mockito.PowerMockito;
47 import org.powermock.core.classloader.annotations.PrepareForTest;
48 import org.powermock.modules.junit4.PowerMockRunner;
49 import org.springframework.mock.web.MockHttpServletResponse;
50 import org.springframework.mock.web.MockServletConfig;
52 import com.mockrunner.mock.web.MockServletInputStream;
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<String>();
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.pep.idfile", "testclient.properties");
99 System.setProperty("xacml.rest.pdp.webapps", "/webapps");
100 System.setProperty("xacml.rootPolicies", "test_PolicyEngine.xml");
101 System.setProperty("xacml.referencedPolicies", "test_PolicyEngine.xml");
102 System.setProperty("test_PolicyEngine.xml.file", "config_testing\\test_PolicyEngine.xml");
103 System.setProperty("xacml.rest.pdp.register", "false");
104 System.setProperty("com.sun.management.jmxremote.port", "9999");
106 im = Mockito.mock(IntegrityMonitor.class);
107 // Need PowerMockito for mocking static method getInstance(...)
108 PowerMockito.mockStatic(IntegrityMonitor.class);
110 // when IntegrityMonitor.getInstance is called, return the mock object
111 PowerMockito.when(IntegrityMonitor.getInstance(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(im);
112 } catch (Exception e1) {
113 e1.printStackTrace();
117 Mockito.doNothing().when(im).startTransaction();
118 } catch (StandbyStatusException | AdministrativeStateException e) {
121 Mockito.doNothing().when(im).endTransaction();
124 public void testInit(){
125 LOGGER.info("XACMLPdpServletTest - testInit");
127 pdpServlet.init(servletConfig);
129 } catch (Exception e) {
137 public void testDoGetNoTypeError(){
138 LOGGER.info("XACMLPdpServletTest - testDoGetNoTypeError");
140 pdpServlet.init(servletConfig);
141 pdpServlet.doGet(httpServletRequest, httpServletResponse);
142 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "type not 'config' or 'hb'");
145 System.out.println("Unexpected exception in testDoGetNoTypeError");
151 public void testDoGetConfigType(){
152 LOGGER.info("XACMLPdpServletTest - testDoGetConfigType");
153 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("config");
156 pdpServlet.init(servletConfig);
157 pdpServlet.doGet(httpServletRequest, httpServletResponse);
158 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
160 }catch (Exception e){
161 System.out.println("Unexpected exception in testDoGetConfigType");
169 public void testDoGetTypeHb(){
170 LOGGER.info("XACMLPdpServletTest - testDoGetTypeHb");
172 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb");
173 pdpServlet.init(servletConfig);
174 pdpServlet.doGet(httpServletRequest, httpServletResponse);
175 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_NO_CONTENT);
178 System.out.println("Unexpected exception in testDoGetTypeHb");
183 public void testDoGetTypeStatus(){
184 LOGGER.info("XACMLPdpServletTest - testDoGetTypeStatus");
186 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("Status");
187 pdpServlet.init(servletConfig);
188 pdpServlet.doGet(httpServletRequest, httpServletResponse);
189 Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
192 System.out.println("Unexpected exception in testDoGetTypeStatus");
198 public void testDoPost(){
199 LOGGER.info("XACMLPdpServletTest - testDoPost");
201 pdpServlet.init(servletConfig);
202 pdpServlet.doPost(httpServletRequest, httpServletResponse);
204 }catch (Exception e){
205 System.out.println("Unexpected exception in testDoPost");
211 public void testDoPostToLong(){
212 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
214 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
215 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
217 pdpServlet.init(servletConfig);
218 pdpServlet.doPost(httpServletRequest, httpServletResponse);
220 }catch (Exception e){
221 System.out.println("Unexpected exception in testDoPostToLong");
227 public void testDoPostContentLengthNegative(){
228 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
230 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
231 Mockito.when(httpServletRequest.getContentLength()).thenReturn(-1);
233 pdpServlet.init(servletConfig);
234 pdpServlet.doPost(httpServletRequest, httpServletResponse);
236 }catch (Exception e){
237 System.out.println("Unexpected exception in testDoPostContentLengthNegative");
243 public void testDoPostContentTypeNonValid(){
244 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
246 Mockito.when(httpServletRequest.getContentType()).thenReturn(";");
247 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
249 pdpServlet.init(servletConfig);
250 pdpServlet.doPost(httpServletRequest, httpServletResponse);
252 }catch (Exception e){
253 System.out.println("Unexpected exception in testDoPostContentTypeNonValid");
259 public void testDoPostContentTypeConfigurationError(){
260 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
262 Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
263 Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
265 pdpServlet.init(servletConfig);
266 pdpServlet.doPost(httpServletRequest, httpServletResponse);
268 }catch (Exception e){
269 System.out.println("Unexpected exception in testDoPostContentTypeConfigurationError");
275 public void testDoPutCacheEmpty(){
276 LOGGER.info("XACMLPdpServletTest - testDoPutCacheEmpty");
277 mockInput = Mockito.mock(ServletInputStream.class);
280 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("cache");
281 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
282 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
283 pdpServlet.init(servletConfig);
284 pdpServlet.doPut(httpServletRequest, httpServletResponse);
285 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "PUT must contain at least one property");
287 }catch (Exception e){
288 System.out.println("Unexpected exception in testDoPutCacheEmpty");
294 public void testDoPutConfigPolicies(){
295 LOGGER.info("XACMLPdpServletTest - testDoPutConfigPolicies");
296 byte[] b = new byte[20];
297 new Random().nextBytes(b);
299 mockInput = new MockServletInputStream(b);
302 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
303 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
304 Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
305 pdpServlet.init(servletConfig);
306 pdpServlet.doPut(httpServletRequest, httpServletResponse);
308 }catch (Exception e){
309 System.out.println("Unexpected exception in testDoPutConfigPolicies");
315 public void testDoPutToLong(){
316 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
318 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
320 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
321 Mockito.when(httpServletRequest.getContentLength()).thenReturn(1000000000);
323 pdpServlet.init(servletConfig);
324 pdpServlet.doPut(httpServletRequest, httpServletResponse);
325 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Content-Length larger than server will accept.");
327 }catch (Exception e){
328 System.out.println("Unexpected exception in testDoPutToLong");
334 public void testDoPutInvalidContentType(){
335 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
337 Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
339 Mockito.when(httpServletRequest.getContentType()).thenReturn("text/json");
340 Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
342 pdpServlet.init(servletConfig);
343 pdpServlet.doPut(httpServletRequest, httpServletResponse);
344 Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid cache: 'policies' or content-type: 'text/json'");
346 }catch (Exception e){
347 System.out.println("Unexpected exception in testDoPutInvalidContentType");
353 public void testDestroy(){
354 LOGGER.info("XACMLPdpServletTest - testDestroy");
357 pdpServlet.init(servletConfig);
358 pdpServlet.destroy();
360 System.out.println("Unexpected exception in testDestroy");