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