Policy TestSuite Enabled
[policy/engine.git] / ECOMP-PDP-REST / src / test / java / org / openecomp / policy / pdp / rest / XACMLPdpServletTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PDP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.pdp.rest;
22
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;
29
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;
35
36 import org.junit.Before;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mockito;
39 import org.openecomp.policy.common.im.AdministrativeStateException;
40 import org.openecomp.policy.common.im.IntegrityMonitor;
41 import org.openecomp.policy.common.im.StandbyStatusException;
42 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
43 import org.openecomp.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;
49
50 import com.mockrunner.mock.web.MockServletInputStream;
51
52 import junit.framework.TestCase;
53
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);
58         
59         private List<String> headers = new ArrayList<String>();
60         
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;
68
69
70          
71     @Before
72     public void setUp(){
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));
77         
78         
79         mockOutput = Mockito.mock(ServletOutputStream.class);
80         
81         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
82         
83         try {
84                         Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
85                 } catch (IOException e) {
86                         fail();
87                 }
88
89         servletConfig = Mockito.mock(MockServletConfig.class);
90         //servletConfig
91         Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
92         pdpServlet = new XACMLPdpServlet();
93         
94         Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("xacml.pdp.properties");
95         
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");
105                 
106                 im = Mockito.mock(IntegrityMonitor.class);
107                 // Need PowerMockito for mocking static method getInstance(...)
108                 PowerMockito.mockStatic(IntegrityMonitor.class);
109                 try {
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();
114                 }
115                 
116                 try {
117                         Mockito.doNothing().when(im).startTransaction();
118                 } catch (StandbyStatusException | AdministrativeStateException e) {
119                         fail();
120                 }
121                 Mockito.doNothing().when(im).endTransaction();
122     }
123         
124         public void testInit(){
125                 LOGGER.info("XACMLPdpServletTest - testInit");
126                 try {   
127                         pdpServlet.init(servletConfig);
128                         assertTrue(true);
129                 } catch (Exception e) {
130                         LOGGER.error("Exception Occured"+e);
131                         fail();
132                         
133                 }
134
135         }
136         
137         public void testDoGetNoTypeError(){
138                 LOGGER.info("XACMLPdpServletTest - testDoGetNoTypeError");
139                 try{
140                         pdpServlet.init(servletConfig);
141                         pdpServlet.doGet(httpServletRequest, httpServletResponse);
142                         Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "type not 'config' or 'hb'");
143                         assertTrue(true);
144                 }catch(Exception e){
145                         System.out.println("Unexpected exception in testDoGetNoTypeError");
146                         LOGGER.error("Exception Occured"+e);
147                         fail();
148                 }
149         }
150         
151         public void testDoGetConfigType(){
152                 LOGGER.info("XACMLPdpServletTest - testDoGetConfigType");
153                 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("config");     
154
155                 try{    
156                         pdpServlet.init(servletConfig);
157                         pdpServlet.doGet(httpServletRequest, httpServletResponse);
158                         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
159                         assertTrue(true);
160                 }catch (Exception e){
161                         System.out.println("Unexpected exception in testDoGetConfigType");
162                         LOGGER.error("Exception Occured"+e);
163                         fail();
164                 }
165
166         }
167         
168         
169         public void testDoGetTypeHb(){
170                 LOGGER.info("XACMLPdpServletTest - testDoGetTypeHb");
171                 try{
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);
176                         assertTrue(true);
177                 }catch(Exception e){
178                         System.out.println("Unexpected exception in testDoGetTypeHb");
179                         LOGGER.error("Exception Occured"+e);
180                         fail();
181                 }
182         }
183         public void testDoGetTypeStatus(){
184                 LOGGER.info("XACMLPdpServletTest - testDoGetTypeStatus");
185                 try{
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);
190                         assertTrue(true);
191                 }catch(Exception e){
192                         System.out.println("Unexpected exception in testDoGetTypeStatus");
193                         LOGGER.error("Exception Occured"+e);
194                         fail();
195                 }
196         }       
197         
198         public void testDoPost(){
199                 LOGGER.info("XACMLPdpServletTest - testDoPost");
200                 try{
201                         pdpServlet.init(servletConfig);
202                         pdpServlet.doPost(httpServletRequest, httpServletResponse);
203                         assertTrue(true);
204                 }catch (Exception e){
205                         System.out.println("Unexpected exception in testDoPost");
206                         LOGGER.error("Exception Occured"+e);
207                         fail();
208                 }
209         }
210         
211         public void testDoPostToLong(){
212                 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
213                 try{
214                         Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
215                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
216                         
217                         pdpServlet.init(servletConfig);
218                         pdpServlet.doPost(httpServletRequest, httpServletResponse);
219                         assertTrue(true);
220                 }catch (Exception e){
221                         System.out.println("Unexpected exception in testDoPostToLong");
222                         LOGGER.error("Exception Occured"+e);
223                         fail();
224                 }
225         }       
226         
227         public void testDoPostContentLengthNegative(){
228                 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
229                 try{
230                         Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
231                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(-1);
232                         
233                         pdpServlet.init(servletConfig);
234                         pdpServlet.doPost(httpServletRequest, httpServletResponse);
235                         assertTrue(true);
236                 }catch (Exception e){
237                         System.out.println("Unexpected exception in testDoPostContentLengthNegative");
238                         LOGGER.error("Exception Occured"+e);
239                         fail();
240                 }
241         }       
242         
243         public void testDoPostContentTypeNonValid(){
244                 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
245                 try{
246                         Mockito.when(httpServletRequest.getContentType()).thenReturn(";");
247                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
248                         
249                         pdpServlet.init(servletConfig);
250                         pdpServlet.doPost(httpServletRequest, httpServletResponse);
251                         assertTrue(true);
252                 }catch (Exception e){
253                         System.out.println("Unexpected exception in testDoPostContentTypeNonValid");
254                         LOGGER.error("Exception Occured"+e);
255                         fail();
256                 }
257         }       
258         
259         public void testDoPostContentTypeConfigurationError(){
260                 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
261                 try{
262                         Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
263                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
264                         
265                         pdpServlet.init(servletConfig);
266                         pdpServlet.doPost(httpServletRequest, httpServletResponse);
267                         assertTrue(true);
268                 }catch (Exception e){
269                         System.out.println("Unexpected exception in testDoPostContentTypeConfigurationError");
270                         LOGGER.error("Exception Occured"+e);
271                         fail();
272                 }
273         }       
274         
275         public void testDoPutCacheEmpty(){
276                 LOGGER.info("XACMLPdpServletTest - testDoPutCacheEmpty");
277                 mockInput = Mockito.mock(ServletInputStream.class);
278                 
279                 try{
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");
286                         assertTrue(true);
287                 }catch (Exception e){
288                         System.out.println("Unexpected exception in testDoPutCacheEmpty");
289                         LOGGER.error("Exception Occured"+e);
290                         fail();
291                 }
292         }
293         
294         public void testDoPutConfigPolicies(){
295                 LOGGER.info("XACMLPdpServletTest - testDoPutConfigPolicies");
296                 byte[] b = new byte[20];
297                 new Random().nextBytes(b);
298                 
299                 mockInput = new MockServletInputStream(b);
300         
301                 try{
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);
307                         assertTrue(true);
308                 }catch (Exception e){
309                         System.out.println("Unexpected exception in testDoPutConfigPolicies");
310                         LOGGER.error("Exception Occured"+e);
311                         fail();
312                 }
313         }       
314         
315         public void testDoPutToLong(){
316                 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
317                 try{
318                         Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
319                         
320                         Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
321                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(1000000000);
322                         
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.");
326                         assertTrue(true);
327                 }catch (Exception e){
328                         System.out.println("Unexpected exception in testDoPutToLong");
329                         LOGGER.error("Exception Occured"+e);
330                         fail();
331                 }
332         }       
333         
334         public void testDoPutInvalidContentType(){
335                 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
336                 try{
337                         Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
338                         
339                         Mockito.when(httpServletRequest.getContentType()).thenReturn("text/json");
340                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
341                         
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'");
345                         assertTrue(true);
346                 }catch (Exception e){
347                         System.out.println("Unexpected exception in testDoPutInvalidContentType");
348                         LOGGER.error("Exception Occured"+e);
349                         fail();
350                 }
351         }               
352         
353         public void testDestroy(){
354                 LOGGER.info("XACMLPdpServletTest - testDestroy");
355                 
356                 try{
357                         pdpServlet.init(servletConfig);
358                         pdpServlet.destroy();
359                 }catch(Exception e){
360                         System.out.println("Unexpected exception in testDestroy");
361                         LOGGER.error("Exception Occured"+e);
362                         fail();
363                 }
364         }
365 }