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