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