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