Fix compilation issue due to generic Exception
[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.persistence.EntityManager;
31 import javax.persistence.EntityManagerFactory;
32 import javax.persistence.EntityTransaction;
33 import javax.persistence.Persistence;
34 import javax.servlet.ServletConfig;
35 import javax.servlet.ServletInputStream;
36 import javax.servlet.ServletOutputStream;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.Mockito;
43 import org.onap.policy.common.ia.DbDAO;
44 import org.onap.policy.common.ia.IntegrityAuditProperties;
45 import org.onap.policy.common.im.IntegrityMonitor;
46 import org.onap.policy.common.im.IntegrityMonitorException;
47 import org.onap.policy.common.logging.flexlogger.FlexLogger;
48 import org.onap.policy.common.logging.flexlogger.Logger;
49 import org.powermock.api.mockito.PowerMockito;
50 import org.springframework.mock.web.MockHttpServletResponse;
51 import org.springframework.mock.web.MockServletConfig;
52
53 import com.mockrunner.mock.web.MockServletInputStream;
54
55 import junit.framework.TestCase;
56
57 public class XACMLPdpServletTest extends TestCase{
58         private static Logger LOGGER    = FlexLogger.getLogger(XACMLPdpServletTest.class);
59         
60         private List<String> headers = new ArrayList<>();
61         
62         private HttpServletRequest httpServletRequest;
63         private HttpServletResponse httpServletResponse;
64         private ServletOutputStream mockOutput;
65         private ServletInputStream mockInput;
66         private ServletConfig servletConfig; 
67         private XACMLPdpServlet pdpServlet;
68         private IntegrityMonitor im;
69         
70         private DbDAO dbDAO;
71         private String persistenceUnit;
72         private Properties properties;
73         private String resourceName;
74         private String dbDriver;
75         private String dbUrl;
76         private String dbUser;
77         private String dbPwd;
78         private String siteName;
79         private String nodeType;
80         private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";
81         private static final String DEFAULT_DB_USER = "sa";
82         private static final String DEFAULT_DB_PWD = "";
83
84          
85     @Before
86     public void setUp(){
87         
88         properties = new Properties();
89                 properties.put(IntegrityAuditProperties.DB_DRIVER, XACMLPdpServletTest.DEFAULT_DB_DRIVER);
90                 properties.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/xacmlTest");
91                 properties.put(IntegrityAuditProperties.DB_USER, XACMLPdpServletTest.DEFAULT_DB_USER);
92                 properties.put(IntegrityAuditProperties.DB_PWD, XACMLPdpServletTest.DEFAULT_DB_PWD);
93                 properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
94                 properties.put(IntegrityAuditProperties.NODE_TYPE, "pap");
95                 //properties.put("com.sun.management.jmxremote.port", "9999");
96                 dbDriver = XACMLPdpServletTest.DEFAULT_DB_DRIVER;
97                 dbUrl = "jdbc:h2:file:./sql/xacmlTest";
98                 dbUser = XACMLPdpServletTest.DEFAULT_DB_USER;
99                 dbPwd = XACMLPdpServletTest.DEFAULT_DB_PWD;
100                 siteName = "SiteA";
101                 nodeType = "pdp";
102                 persistenceUnit = "testPdpPU";
103                 resourceName = "siteA.pdp1";
104                 
105                 System.setProperty("com.sun.management.jmxremote.port", "9999");
106                 
107                 EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit, properties);
108                 
109                 EntityManager em = emf.createEntityManager();
110                 // Start a transaction
111                 EntityTransaction et = em.getTransaction();
112                 
113                 IntegrityMonitor im = null;
114                 try {
115                         im = IntegrityMonitor.getInstance(resourceName, properties);
116                 } catch (Exception e2) {
117                         // TODO Auto-generated catch block
118                         e2.printStackTrace();
119                 }
120                 //cleanDb(persistenceUnit, properties);
121                 
122         httpServletRequest = Mockito.mock(HttpServletRequest.class);
123         Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
124         Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
125         Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
126         Mockito.when(httpServletRequest.getRequestURI()).thenReturn("/pdp/test");
127         
128         mockOutput = Mockito.mock(ServletOutputStream.class);
129         
130         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
131         
132         try {
133                         Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
134                 } catch (IOException e) {
135                         fail();
136                 }
137
138         servletConfig = Mockito.mock(MockServletConfig.class);
139         //servletConfig
140         Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
141         pdpServlet = new XACMLPdpServlet();
142         pdpServlet.setIm(im);
143         
144         Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME")).thenReturn("src/test/resources/xacml.pdp.properties");
145         
146                 System.setProperty("xacml.properties", "src/test/resources/xacml.pdp.properties");
147                 System.setProperty("xacml.rest.pdp.config", "src/test/resources/config_testing");
148                 System.setProperty("xacml.rest.pdp.webapps", "src/test/resources/webapps");
149                 /*System.setProperty("xacml.rootPolicies", "test_PolicyEngine.xml");
150                 System.setProperty("xacml.referencedPolicies", "test_PolicyEngine.xml");
151                 System.setProperty("test_PolicyEngine.xml.file", "config_testing\\test_PolicyEngine.xml");
152                 */
153                 System.setProperty("xacml.rest.pdp.register", "false");
154                 System.setProperty("com.sun.management.jmxremote.port", "9999");
155                 
156                 im = Mockito.mock(IntegrityMonitor.class);
157                 // Need PowerMockito for mocking static method getInstance(...)
158                 PowerMockito.mockStatic(IntegrityMonitor.class);
159                 try {
160                         // when IntegrityMonitor.getInstance is called, return the mock object
161                         PowerMockito.when(IntegrityMonitor.getInstance(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(im);
162                 } catch (Exception e1) {
163                         LOGGER.error("Exception Occured"+e1);
164                 }
165                 
166                 try {
167                         Mockito.doNothing().when(im).startTransaction();
168                 } catch (IntegrityMonitorException e) {
169                         fail();
170                 }
171                 Mockito.doNothing().when(im).endTransaction();
172     }
173     
174         @Test
175     public void testInit(){
176                 LOGGER.info("XACMLPdpServletTest - testInit");
177                 try {   
178                         pdpServlet.init(servletConfig);
179                         assertTrue(true);
180                 } catch (Exception e) {
181                         LOGGER.error("Exception Occured"+e);
182                         fail();
183                         
184                 }
185
186         }
187         
188         @Test
189         public void testDoGetNoTypeError(){
190                 LOGGER.info("XACMLPdpServletTest - testDoGetNoTypeError");
191                 try{
192                         
193                         pdpServlet.init(servletConfig);
194                         pdpServlet.doGet(httpServletRequest, httpServletResponse);
195                         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
196                         assertTrue(true);
197                 }catch(Exception e){
198                         System.out.println("Unexpected exception in testDoGetNoTypeError");
199                         LOGGER.error("Exception Occured"+e);
200                         fail();
201                 }
202         }
203         
204         @Test
205         public void testDoGetConfigType(){
206                 LOGGER.info("XACMLPdpServletTest - testDoGetConfigType");
207                 Mockito.when(httpServletRequest.getParameter("type")).thenReturn("config");     
208
209                 try{    
210                         pdpServlet.init(servletConfig);
211                         pdpServlet.doGet(httpServletRequest, httpServletResponse);
212                         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
213                         assertTrue(true);
214                 }catch (Exception e){
215                         System.out.println("Unexpected exception in testDoGetConfigType");
216                         LOGGER.error("Exception Occured"+e);
217                         fail();
218                 }
219
220         }
221         
222         @Test
223         public void testDoGetTypeHb(){
224                 LOGGER.info("XACMLPdpServletTest - testDoGetTypeHb");
225                 try{
226                         Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb");
227                         pdpServlet.init(servletConfig);
228                         pdpServlet.doGet(httpServletRequest, httpServletResponse);
229                         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
230                         assertTrue(true);
231                 }catch(Exception e){
232                         System.out.println("Unexpected exception in testDoGetTypeHb");
233                         LOGGER.error("Exception Occured"+e);
234                         fail();
235                 }
236         }
237         
238         @Test
239         public void testDoGetTypeStatus(){
240                 LOGGER.info("XACMLPdpServletTest - testDoGetTypeStatus");
241                 try{
242                         Mockito.when(httpServletRequest.getParameter("type")).thenReturn("Status");
243                         pdpServlet.init(servletConfig);
244                         pdpServlet.doGet(httpServletRequest, httpServletResponse);
245                         Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
246                         assertTrue(true);
247                 }catch(Exception e){
248                         System.out.println("Unexpected exception in testDoGetTypeStatus");
249                         LOGGER.error("Exception Occured"+e);
250                         fail();
251                 }
252         }       
253         
254         @Test
255         public void testDoPost(){
256                 LOGGER.info("XACMLPdpServletTest - testDoPost");
257                 try{
258                         pdpServlet.init(servletConfig);
259                         pdpServlet.doPost(httpServletRequest, httpServletResponse);
260                         assertTrue(true);
261                 }catch (Exception e){
262                         System.out.println("Unexpected exception in testDoPost");
263                         LOGGER.error("Exception Occured"+e);
264                         fail();
265                 }
266         }
267         
268         @Test
269         public void testDoPostToLong(){
270                 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
271                 try{
272                         Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
273                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
274                         
275                         pdpServlet.init(servletConfig);
276                         pdpServlet.doPost(httpServletRequest, httpServletResponse);
277                         assertTrue(true);
278                 }catch (Exception e){
279                         System.out.println("Unexpected exception in testDoPostToLong");
280                         LOGGER.error("Exception Occured"+e);
281                         fail();
282                 }
283         }       
284         
285         @Test
286         public void testDoPostContentLengthNegative(){
287                 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
288                 try{
289                         Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
290                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(-1);
291                         
292                         pdpServlet.init(servletConfig);
293                         pdpServlet.doPost(httpServletRequest, httpServletResponse);
294                         assertTrue(true);
295                 }catch (Exception e){
296                         System.out.println("Unexpected exception in testDoPostContentLengthNegative");
297                         LOGGER.error("Exception Occured"+e);
298                         fail();
299                 }
300         }       
301         
302         @Test
303         public void testDoPostContentTypeNonValid(){
304                 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
305                 try{
306                         Mockito.when(httpServletRequest.getContentType()).thenReturn(";");
307                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
308                         
309                         pdpServlet.init(servletConfig);
310                         pdpServlet.doPost(httpServletRequest, httpServletResponse);
311                         assertTrue(true);
312                 }catch (Exception e){
313                         System.out.println("Unexpected exception in testDoPostContentTypeNonValid");
314                         LOGGER.error("Exception Occured"+e);
315                         fail();
316                 }
317         }       
318         
319         @Test
320         public void testDoPostContentTypeConfigurationError(){
321                 LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
322                 try{
323                         Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
324                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
325                         
326                         pdpServlet.init(servletConfig);
327                         pdpServlet.doPost(httpServletRequest, httpServletResponse);
328                         assertTrue(true);
329                 }catch (Exception e){
330                         System.out.println("Unexpected exception in testDoPostContentTypeConfigurationError");
331                         LOGGER.error("Exception Occured"+e);
332                         fail();
333                 }
334         }       
335         
336         @Test
337         public void testDoPutCacheEmpty(){
338                 LOGGER.info("XACMLPdpServletTest - testDoPutCacheEmpty");
339                 mockInput = Mockito.mock(ServletInputStream.class);
340                 
341                 try{
342                         Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("cache");
343                         Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
344                         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
345                         pdpServlet.init(servletConfig);
346                         pdpServlet.doPut(httpServletRequest, httpServletResponse);
347                         Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "PUT must contain at least one property");
348                         assertTrue(true);
349                 }catch (Exception e){
350                         System.out.println("Unexpected exception in testDoPutCacheEmpty");
351                         LOGGER.error("Exception Occured"+e);
352                         fail();
353                 }
354         }
355         
356         @Test
357         public void testDoPutConfigPolicies(){
358                 LOGGER.info("XACMLPdpServletTest - testDoPutConfigPolicies");
359                 byte[] b = new byte[20];
360                 new Random().nextBytes(b);
361                 
362                 mockInput = new MockServletInputStream(b);
363         
364                 try{
365                         Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
366                         Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
367                         Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
368                         pdpServlet.init(servletConfig);
369                         pdpServlet.doPut(httpServletRequest, httpServletResponse);
370                         assertTrue(true);
371                 }catch (Exception e){
372                         System.out.println("Unexpected exception in testDoPutConfigPolicies");
373                         LOGGER.error("Exception Occured"+e);
374                         fail();
375                 }
376         }       
377         
378         public void testDoPutToLong(){
379                 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
380                 try{
381                         Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
382                         
383                         Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
384                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(1000000000);
385                         
386                         pdpServlet.init(servletConfig);
387                         pdpServlet.doPut(httpServletRequest, httpServletResponse);
388                         Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Content-Length larger than server will accept.");
389                         assertTrue(true);
390                 }catch (Exception e){
391                         System.out.println("Unexpected exception in testDoPutToLong");
392                         LOGGER.error("Exception Occured"+e);
393                         fail();
394                 }
395         }       
396         
397         @Test
398         public void testDoPutInvalidContentType(){
399                 LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
400                 try{
401                         Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
402                         
403                         Mockito.when(httpServletRequest.getContentType()).thenReturn("text/json");
404                         Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
405                         
406                         pdpServlet.init(servletConfig);
407                         pdpServlet.doPut(httpServletRequest, httpServletResponse);
408                         Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid cache: 'policies' or content-type: 'text/json'");
409                         assertTrue(true);
410                 }catch (Exception e){
411                         System.out.println("Unexpected exception in testDoPutInvalidContentType");
412                         LOGGER.error("Exception Occured"+e);
413                         fail();
414                 }
415         }               
416         
417         @Test
418         public void testDestroy(){
419                 LOGGER.info("XACMLPdpServletTest - testDestroy");
420                 
421                 try{
422                         pdpServlet.init(servletConfig);
423                         pdpServlet.destroy();
424                 }catch(Exception e){
425                         System.out.println("Unexpected exception in testDestroy");
426                         LOGGER.error("Exception Occured"+e);
427                         fail();
428                 }
429         }
430 }