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