policy/engine jdk11 upgrades
[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-2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pdp.rest;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Properties;
30 import java.util.Random;
31 import javax.servlet.ServletConfig;
32 import javax.servlet.ServletInputStream;
33 import javax.servlet.ServletOutputStream;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mockito;
41 import org.onap.policy.common.ia.IntegrityAuditProperties;
42 import org.onap.policy.common.im.IntegrityMonitor;
43 import org.onap.policy.common.im.IntegrityMonitorException;
44 import org.onap.policy.common.logging.OnapLoggingContext;
45 import org.onap.policy.common.logging.flexlogger.FlexLogger;
46 import org.onap.policy.common.logging.flexlogger.Logger;
47 import org.onap.policy.xacml.std.pap.StdPDPPolicy;
48 import org.onap.policy.xacml.std.pap.StdPDPStatus;
49 import org.powermock.api.mockito.PowerMockito;
50 import org.powermock.core.classloader.annotations.PowerMockIgnore;
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 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*", "org.w3c.dom.*"})
61 @PrepareForTest({IntegrityMonitor.class})
62 public class XACMLPdpServletTest extends TestCase {
63     private static Logger LOGGER = FlexLogger.getLogger(XACMLPdpServletTest.class);
64     private List<String> headers = new ArrayList<>();
65
66     private HttpServletRequest httpServletRequest;
67     private HttpServletResponse httpServletResponse;
68     private ServletOutputStream mockOutput;
69     private ServletInputStream mockInput;
70     private ServletConfig servletConfig;
71     private XACMLPdpServlet pdpServlet;
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     private static final String ERROR_TEXT = "Exception occurred: ";
81
82     @Override
83     @Before
84     public void setUp() {
85         status = new StdPDPStatus();
86         foobarPolicy = new StdPDPPolicy();
87         foobarPolicy.setId("foobar");
88         foobarPolicy.setVersion("123");
89         foobarPolicy.setName("nothing");
90         status.addLoadedPolicy(foobarPolicy);
91
92         properties = new Properties();
93         properties.put(IntegrityAuditProperties.DB_DRIVER, XACMLPdpServletTest.DEFAULT_DB_DRIVER);
94         properties.put(IntegrityAuditProperties.DB_URL, "jdbc:h2:file:./sql/xacmlTest");
95         properties.put(IntegrityAuditProperties.DB_USER, XACMLPdpServletTest.DEFAULT_DB_USER);
96         properties.put(IntegrityAuditProperties.DB_PWD, XACMLPdpServletTest.DEFAULT_DB_PWD);
97         properties.put(IntegrityAuditProperties.SITE_NAME, "SiteA");
98         properties.put(IntegrityAuditProperties.NODE_TYPE, "pap");
99         resourceName = "siteA.pdp1";
100
101         System.setProperty("com.sun.management.jmxremote.port", "9999");
102
103         IntegrityMonitor im = null;
104         try {
105             im = IntegrityMonitor.getInstance(resourceName, properties);
106         } catch (Exception e) {
107             e.printStackTrace();
108         }
109
110         httpServletRequest = Mockito.mock(HttpServletRequest.class);
111         Mockito.when(httpServletRequest.getMethod()).thenReturn("POST");
112         Mockito.when(httpServletRequest.getHeaderNames()).thenReturn(Collections.enumeration(headers));
113         Mockito.when(httpServletRequest.getAttributeNames()).thenReturn(Collections.enumeration(headers));
114         Mockito.when(httpServletRequest.getRequestURI()).thenReturn("/pdp/test");
115
116         mockOutput = Mockito.mock(ServletOutputStream.class);
117
118         httpServletResponse = Mockito.mock(MockHttpServletResponse.class);
119
120         try {
121             Mockito.when(httpServletResponse.getOutputStream()).thenReturn(mockOutput);
122         } catch (IOException e) {
123             fail();
124         }
125
126         servletConfig = Mockito.mock(MockServletConfig.class);
127         // servletConfig
128         Mockito.when(servletConfig.getInitParameterNames()).thenReturn(Collections.enumeration(headers));
129         pdpServlet = new XACMLPdpServlet();
130         pdpServlet.setIm(im);
131
132         Mockito.when(servletConfig.getInitParameter("XACML_PROPERTIES_NAME"))
133                 .thenReturn("src/test/resources/xacml.pdp.properties");
134
135         System.setProperty("xacml.properties", "src/test/resources/xacml.pdp.properties");
136         System.setProperty("xacml.rest.pdp.config", "src/test/resources/config_testing");
137         System.setProperty("xacml.rest.pdp.webapps", "src/test/resources/webapps");
138
139         System.setProperty("xacml.rest.pdp.register", "false");
140         System.setProperty("com.sun.management.jmxremote.port", "9999");
141
142         im = Mockito.mock(IntegrityMonitor.class);
143         // Need PowerMockito for mocking static method getInstance(...)
144         PowerMockito.mockStatic(IntegrityMonitor.class);
145         try {
146             // when IntegrityMonitor.getInstance is called, return the mock object
147             PowerMockito.when(IntegrityMonitor.getInstance(Mockito.anyString(), Mockito.any(Properties.class)))
148                     .thenReturn(im);
149         } catch (Exception e) {
150             LOGGER.error(ERROR_TEXT + e);
151         }
152
153         try {
154             Mockito.doNothing().when(im).startTransaction();
155         } catch (IntegrityMonitorException e) {
156             fail();
157         }
158         Mockito.doNothing().when(im).endTransaction();
159     }
160
161     @Override
162     @After
163     public void tearDown() {
164         System.clearProperty("xacml.rest.pdp.config");
165     }
166
167     @Test
168     public void testInit() {
169         LOGGER.info("XACMLPdpServletTest - testInit");
170         try {
171             pdpServlet.init(servletConfig);
172
173             assertTrue(true);
174         } catch (Exception e) {
175             LOGGER.error(ERROR_TEXT + e);
176             fail();
177         }
178     }
179
180     @Test
181     public void testUebNotification() {
182         LOGGER.info("XACMLPdpServletTest - testUebNotification");
183         try {
184
185             XACMLProperties.reloadProperties();
186             System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.ueb.properties");
187             XACMLProperties.getProperties();
188
189             pdpServlet.init(servletConfig);
190
191             status.setStatus(com.att.research.xacml.api.pap.PDPStatus.Status.UPDATING_CONFIGURATION);
192
193             XACMLPdpLoader.validatePolicies(properties, status);
194             XACMLPdpLoader.sendNotification();
195             assertTrue(true);
196         } catch (Exception e) {
197             LOGGER.error(ERROR_TEXT + e);
198             fail();
199         }
200     }
201
202     @Test
203     public void testDmaapNotification() {
204         LOGGER.info("XACMLPdpServletTest - testDmaapNotification");
205         try {
206
207             XACMLProperties.reloadProperties();
208             System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "src/test/resources/xacml.pdp.dmaap.properties");
209             XACMLProperties.getProperties();
210
211             pdpServlet.init(servletConfig);
212
213             status.setStatus(com.att.research.xacml.api.pap.PDPStatus.Status.UPDATING_CONFIGURATION);
214
215             XACMLPdpLoader.validatePolicies(properties, status);
216             XACMLPdpLoader.sendNotification();
217             assertTrue(true);
218         } catch (Exception e) {
219             LOGGER.error(ERROR_TEXT + e);
220             fail();
221         }
222     }
223
224     @Test
225     public void testXACMLPdpRegisterThread() {
226         LOGGER.info("XACMLPdpServletTest - testXACMLPdpRegisterThread");
227         try {
228             OnapLoggingContext baseLoggingContext = new OnapLoggingContext();
229             baseLoggingContext.setServer("localhost");
230             XACMLPdpRegisterThread regThread = new XACMLPdpRegisterThread(baseLoggingContext);
231             regThread.run();
232             assertTrue(true);
233         } catch (Exception e) {
234             LOGGER.error(ERROR_TEXT + e);
235             fail();
236         }
237     }
238
239     @Test
240     public void testDoGetNoTypeError() {
241         LOGGER.info("XACMLPdpServletTest - testDoGetNoTypeError");
242         try {
243
244             pdpServlet.init(servletConfig);
245             pdpServlet.doGet(httpServletRequest, httpServletResponse);
246             Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
247             assertTrue(true);
248         } catch (Exception e) {
249             LOGGER.error(ERROR_TEXT + e);
250             fail();
251         }
252     }
253
254     @Test
255     public void testDoGetConfigType() {
256         LOGGER.info("XACMLPdpServletTest - testDoGetConfigType");
257         Mockito.when(httpServletRequest.getParameter("type")).thenReturn("config");
258
259         try {
260             pdpServlet.init(servletConfig);
261             pdpServlet.doGet(httpServletRequest, httpServletResponse);
262             Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
263             assertTrue(true);
264         } catch (Exception e) {
265             LOGGER.error(ERROR_TEXT + e);
266             fail();
267         }
268
269     }
270
271     @Test
272     public void testDoGetTypeHb() {
273         LOGGER.info("XACMLPdpServletTest - testDoGetTypeHb");
274         try {
275             Mockito.when(httpServletRequest.getParameter("type")).thenReturn("hb");
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             LOGGER.error(ERROR_TEXT + e);
282             fail();
283         }
284     }
285
286     @Test
287     public void testDoGetTypeStatus() {
288         LOGGER.info("XACMLPdpServletTest - testDoGetTypeStatus");
289         try {
290             Mockito.when(httpServletRequest.getParameter("type")).thenReturn("Status");
291             pdpServlet.init(servletConfig);
292             pdpServlet.doGet(httpServletRequest, httpServletResponse);
293             Mockito.verify(httpServletResponse).setStatus(HttpServletResponse.SC_OK);
294             assertTrue(true);
295         } catch (Exception e) {
296             LOGGER.error(ERROR_TEXT + e);
297             fail();
298         }
299     }
300
301     @Test
302     public void testDoPost() {
303         LOGGER.info("XACMLPdpServletTest - testDoPost");
304         try {
305             pdpServlet.init(servletConfig);
306             pdpServlet.doPost(httpServletRequest, httpServletResponse);
307             assertTrue(true);
308         } catch (Exception e) {
309             LOGGER.error(ERROR_TEXT + e);
310             fail();
311         }
312     }
313
314     @Test
315     public void testDoPostToLong() {
316         LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
317         try {
318             Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
319             Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
320
321             pdpServlet.init(servletConfig);
322             pdpServlet.doPost(httpServletRequest, httpServletResponse);
323             assertTrue(true);
324         } catch (Exception e) {
325             LOGGER.error(ERROR_TEXT + e);
326             fail();
327         }
328     }
329
330     @Test
331     public void testDoPostContentLengthNegative() {
332         LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
333         try {
334             Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
335             Mockito.when(httpServletRequest.getContentLength()).thenReturn(-1);
336
337             pdpServlet.init(servletConfig);
338             pdpServlet.doPost(httpServletRequest, httpServletResponse);
339             assertTrue(true);
340         } catch (Exception e) {
341             LOGGER.error(ERROR_TEXT + e);
342             fail();
343         }
344     }
345
346     @Test
347     public void testDoPostContentTypeNonValid() {
348         LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
349         try {
350             Mockito.when(httpServletRequest.getContentType()).thenReturn(";");
351             Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
352
353             pdpServlet.init(servletConfig);
354             pdpServlet.doPost(httpServletRequest, httpServletResponse);
355             assertTrue(true);
356         } catch (Exception e) {
357             LOGGER.error(ERROR_TEXT + e);
358             fail();
359         }
360     }
361
362     @Test
363     public void testDoPostContentTypeConfigurationError() {
364         LOGGER.info("XACMLPdpServletTest - testDoPostToLong");
365         try {
366             Mockito.when(httpServletRequest.getContentType()).thenReturn("stuff");
367             Mockito.when(httpServletRequest.getContentLength()).thenReturn(30768);
368
369             pdpServlet.init(servletConfig);
370             pdpServlet.doPost(httpServletRequest, httpServletResponse);
371             assertTrue(true);
372         } catch (Exception e) {
373             LOGGER.error(ERROR_TEXT + e);
374             fail();
375         }
376     }
377
378     @Test
379     public void testDoPutCacheEmpty() {
380         LOGGER.info("XACMLPdpServletTest - testDoPutCacheEmpty");
381         mockInput = Mockito.mock(ServletInputStream.class);
382
383         try {
384             Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("cache");
385             Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
386             Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
387             pdpServlet.init(servletConfig);
388             pdpServlet.doPut(httpServletRequest, httpServletResponse);
389             Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST,
390                     "PUT must contain at least one property");
391             assertTrue(true);
392         } catch (Exception e) {
393             LOGGER.error(ERROR_TEXT + e);
394             fail();
395         }
396     }
397
398     @Test
399     public void testDoPutConfigPolicies() {
400         LOGGER.info("XACMLPdpServletTest - testDoPutConfigPolicies");
401         byte[] b = new byte[20];
402         new Random().nextBytes(b);
403
404         mockInput = new MockServletInputStream(b);
405
406         try {
407             Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
408             Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
409             Mockito.when(httpServletRequest.getInputStream()).thenReturn(mockInput);
410             pdpServlet.init(servletConfig);
411             pdpServlet.doPut(httpServletRequest, httpServletResponse);
412             assertTrue(true);
413         } catch (Exception e) {
414             LOGGER.error(ERROR_TEXT + e);
415             fail();
416         }
417     }
418
419     public void testDoPutToLong() {
420         LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
421         try {
422             Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
423
424             Mockito.when(httpServletRequest.getContentType()).thenReturn("text/x-java-properties");
425             Mockito.when(httpServletRequest.getContentLength()).thenReturn(1000000000);
426
427             pdpServlet.init(servletConfig);
428             pdpServlet.doPut(httpServletRequest, httpServletResponse);
429             Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST,
430                     "Content-Length larger than server will accept.");
431             assertTrue(true);
432         } catch (Exception e) {
433             LOGGER.error(ERROR_TEXT + e);
434             fail();
435         }
436     }
437
438     @Test
439     public void testDoPutInvalidContentType() {
440         LOGGER.info("XACMLPdpServletTest - testDoPutToLong");
441         try {
442             Mockito.when(httpServletRequest.getParameter("cache")).thenReturn("policies");
443
444             Mockito.when(httpServletRequest.getContentType()).thenReturn("text/json");
445             Mockito.when(httpServletRequest.getContentLength()).thenReturn(32768);
446
447             pdpServlet.init(servletConfig);
448             pdpServlet.doPut(httpServletRequest, httpServletResponse);
449             Mockito.verify(httpServletResponse).sendError(HttpServletResponse.SC_BAD_REQUEST,
450                     "Invalid cache: 'policies' or content-type: 'text/json'");
451             assertTrue(true);
452         } catch (Exception e) {
453             LOGGER.error(ERROR_TEXT + e);
454             fail();
455         }
456     }
457
458     @Test
459     public void testDestroy() {
460         LOGGER.info("XACMLPdpServletTest - testDestroy");
461
462         try {
463             pdpServlet.init(servletConfig);
464             pdpServlet.destroy();
465         } catch (Exception e) {
466             LOGGER.error(ERROR_TEXT + e);
467             fail();
468         }
469     }
470 }