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