[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / ONAP-PDP-REST / src / test / java / org / onap / policy / pdp / rest / XACMLPdpServletTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-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.onap.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.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;
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<>();
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.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");
104                 
105                 im = Mockito.mock(IntegrityMonitor.class);
106                 // Need PowerMockito for mocking static method getInstance(...)
107                 PowerMockito.mockStatic(IntegrityMonitor.class);
108                 try {
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);
113                 }
114                 
115                 try {
116                         Mockito.doNothing().when(im).startTransaction();
117                 } catch (StandbyStatusException | AdministrativeStateException e) {
118                         fail();
119                 }
120                 Mockito.doNothing().when(im).endTransaction();
121     }
122         
123         public void testInit(){
124                 LOGGER.info("XACMLPdpServletTest - testInit");
125                 try {   
126                         pdpServlet.init(servletConfig);
127                         assertTrue(true);
128                 } catch (Exception e) {
129                         LOGGER.error("Exception Occured"+e);
130                         fail();
131                         
132                 }
133
134         }
135         
136         public void testDoGetNoTypeError(){
137                 LOGGER.info("XACMLPdpServletTest - testDoGetNoTypeError");
138                 try{
139                         pdpServlet.init(servletConfig);
140                         pdpServlet.doGet(httpServletRequest, httpServletResponse);
141                         Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "type not 'config' or 'hb'");
142                         assertTrue(true);
143                 }catch(Exception e){
144                         System.out.println("Unexpected exception in testDoGetNoTypeError");
145                         LOGGER.error("Exception Occured"+e);
146                         fail();
147                 }
148         }
149         
150         public void testDoGetConfigType(){
151                 LOGGER.info("XACMLPdpServletTest - testDoGetConfigType");
152                 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("config");     
153
154                 try{    
155                         pdpServlet.init(servletConfig);
156                         pdpServlet.doGet(httpServletRequest, httpServletResponse);
157                         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
158                         assertTrue(true);
159                 }catch (Exception e){
160                         System.out.println("Unexpected exception in testDoGetConfigType");
161                         LOGGER.error("Exception Occured"+e);
162                         fail();
163                 }
164
165         }
166         
167         
168         public void testDoGetTypeHb(){
169                 LOGGER.info("XACMLPdpServletTest - testDoGetTypeHb");
170                 try{
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);
175                         assertTrue(true);
176                 }catch(Exception e){
177                         System.out.println("Unexpected exception in testDoGetTypeHb");
178                         LOGGER.error("Exception Occured"+e);
179                         fail();
180                 }
181         }
182         public void testDoGetTypeStatus(){
183                 LOGGER.info("XACMLPdpServletTest - testDoGetTypeStatus");
184                 try{
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);
189                         assertTrue(true);
190                 }catch(Exception e){
191                         System.out.println("Unexpected exception in testDoGetTypeStatus");
192                         LOGGER.error("Exception Occured"+e);
193                         fail();
194                 }
195         }       
196         
197         public void testDoPost(){
198                 LOGGER.info("XACMLPdpServletTest - testDoPost");
199                 try{
200                         pdpServlet.init(servletConfig);
201                         pdpServlet.doPost(httpServletRequest, httpServletResponse);
202                         assertTrue(true);
203                 }catch (Exception e){
204                         System.out.println("Unexpected exception in testDoPost");
205                         LOGGER.error("Exception Occured"+e);
206                         fail();
207                 }
208         }
209         
210         public void testDoPostToLong(){
211                 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
212                 try{
213                         Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
214                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
215                         
216                         pdpServlet.init(servletConfig);
217                         pdpServlet.doPost(httpServletRequest, httpServletResponse);
218                         assertTrue(true);
219                 }catch (Exception e){
220                         System.out.println("Unexpected exception in testDoPostToLong");
221                         LOGGER.error("Exception Occured"+e);
222                         fail();
223                 }
224         }       
225         
226         public void testDoPostContentLengthNegative(){
227                 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
228                 try{
229                         Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
230                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(-1);
231                         
232                         pdpServlet.init(servletConfig);
233                         pdpServlet.doPost(httpServletRequest, httpServletResponse);
234                         assertTrue(true);
235                 }catch (Exception e){
236                         System.out.println("Unexpected exception in testDoPostContentLengthNegative");
237                         LOGGER.error("Exception Occured"+e);
238                         fail();
239                 }
240         }       
241         
242         public void testDoPostContentTypeNonValid(){
243                 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
244                 try{
245                         Mockito.when(httpServletRequest.getContentType()).thenReturn(";");
246                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
247                         
248                         pdpServlet.init(servletConfig);
249                         pdpServlet.doPost(httpServletRequest, httpServletResponse);
250                         assertTrue(true);
251                 }catch (Exception e){
252                         System.out.println("Unexpected exception in testDoPostContentTypeNonValid");
253                         LOGGER.error("Exception Occured"+e);
254                         fail();
255                 }
256         }       
257         
258         public void testDoPostContentTypeConfigurationError(){
259                 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
260                 try{
261                         Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
262                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
263                         
264                         pdpServlet.init(servletConfig);
265                         pdpServlet.doPost(httpServletRequest, httpServletResponse);
266                         assertTrue(true);
267                 }catch (Exception e){
268                         System.out.println("Unexpected exception in testDoPostContentTypeConfigurationError");
269                         LOGGER.error("Exception Occured"+e);
270                         fail();
271                 }
272         }       
273         
274         public void testDoPutCacheEmpty(){
275                 LOGGER.info("XACMLPdpServletTest - testDoPutCacheEmpty");
276                 mockInput = Mockito.mock(ServletInputStream.class);
277                 
278                 try{
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");
285                         assertTrue(true);
286                 }catch (Exception e){
287                         System.out.println("Unexpected exception in testDoPutCacheEmpty");
288                         LOGGER.error("Exception Occured"+e);
289                         fail();
290                 }
291         }
292         
293         public void testDoPutConfigPolicies(){
294                 LOGGER.info("XACMLPdpServletTest - testDoPutConfigPolicies");
295                 byte[] b = new byte[20];
296                 new Random().nextBytes(b);
297                 
298                 mockInput = new MockServletInputStream(b);
299         
300                 try{
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);
306                         assertTrue(true);
307                 }catch (Exception e){
308                         System.out.println("Unexpected exception in testDoPutConfigPolicies");
309                         LOGGER.error("Exception Occured"+e);
310                         fail();
311                 }
312         }       
313         
314         public void testDoPutToLong(){
315                 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
316                 try{
317                         Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
318                         
319                         Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
320                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(1000000000);
321                         
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.");
325                         assertTrue(true);
326                 }catch (Exception e){
327                         System.out.println("Unexpected exception in testDoPutToLong");
328                         LOGGER.error("Exception Occured"+e);
329                         fail();
330                 }
331         }       
332         
333         public void testDoPutInvalidContentType(){
334                 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
335                 try{
336                         Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
337                         
338                         Mockito.when(httpServletRequest.getContentType()).thenReturn("text/json");
339                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
340                         
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'");
344                         assertTrue(true);
345                 }catch (Exception e){
346                         System.out.println("Unexpected exception in testDoPutInvalidContentType");
347                         LOGGER.error("Exception Occured"+e);
348                         fail();
349                 }
350         }               
351         
352         public void testDestroy(){
353                 LOGGER.info("XACMLPdpServletTest - testDestroy");
354                 
355                 try{
356                         pdpServlet.init(servletConfig);
357                         pdpServlet.destroy();
358                 }catch(Exception e){
359                         System.out.println("Unexpected exception in testDestroy");
360                         LOGGER.error("Exception Occured"+e);
361                         fail();
362                 }
363         }
364 }