[DMaaP DR] JKD 11 migration
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / InternalServletTest.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 2017 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  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23 package org.onap.dmaap.datarouter.provisioning;
24
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30 import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER;
31
32 import ch.qos.logback.classic.spi.ILoggingEvent;
33 import ch.qos.logback.core.read.ListAppender;
34 import java.io.File;
35 import java.net.InetAddress;
36 import javax.persistence.EntityManager;
37 import javax.persistence.EntityManagerFactory;
38 import javax.persistence.Persistence;
39 import javax.servlet.ServletInputStream;
40 import javax.servlet.ServletOutputStream;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43 import org.apache.commons.lang3.reflect.FieldUtils;
44 import org.junit.AfterClass;
45 import org.junit.Assert;
46 import org.junit.Before;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 import org.junit.runner.RunWith;
50 import org.mockito.Mock;
51 import org.onap.dmaap.datarouter.provisioning.beans.Deleteable;
52 import org.onap.dmaap.datarouter.provisioning.beans.Insertable;
53 import org.onap.dmaap.datarouter.provisioning.beans.LogRecord;
54 import org.onap.dmaap.datarouter.provisioning.beans.Parameters;
55 import org.onap.dmaap.datarouter.provisioning.beans.Updateable;
56 import org.onap.dmaap.datarouter.provisioning.utils.Poker;
57 import org.powermock.api.mockito.PowerMockito;
58 import org.powermock.core.classloader.annotations.PowerMockIgnore;
59 import org.powermock.core.classloader.annotations.PrepareForTest;
60 import org.powermock.modules.junit4.PowerMockRunner;
61
62 @RunWith(PowerMockRunner.class)
63 @PrepareForTest(LogRecord.class)
64 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
65 public class InternalServletTest extends DrServletTestBase {
66   private static EntityManagerFactory emf;
67   private static EntityManager em;
68   private InternalServlet internalServlet;
69
70   @Mock
71   private HttpServletRequest request;
72
73   @Mock
74   private HttpServletResponse response;
75
76   ListAppender<ILoggingEvent> listAppender;
77
78   @BeforeClass
79   public static void init() {
80     emf = Persistence.createEntityManagerFactory("dr-unit-tests");
81     em = emf.createEntityManager();
82     System.setProperty(
83             "org.onap.dmaap.datarouter.provserver.properties",
84             "src/test/resources/h2Database.properties");
85   }
86
87   @AfterClass
88   public static void tearDownClass() {
89     em.clear();
90     em.close();
91     emf.close();
92   }
93
94   @Before
95   public void setUp() throws Exception {
96       listAppender = setTestLogger(InternalServlet.class);
97       internalServlet = new InternalServlet();
98       setUpValidAuthorisedRequest();
99   }
100
101   @Test
102   public void Given_Request_Is_HTTP_GET_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
103       throws Exception {
104     when(request.getRemoteAddr()).thenReturn("127.100.0.3");
105     internalServlet.doGet(request, response);
106     verify(response)
107         .sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
108     verifyEnteringExitCalled(listAppender);
109   }
110
111   @Test
112   public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_But_Not_Sent_From_Localhost_Then_Forbidden_Response_Is_Generated()
113       throws Exception {
114     when(request.getPathInfo()).thenReturn("/halt");
115     when(request.isSecure()).thenReturn(false);
116     when(request.getRemoteAddr()).thenReturn("127.100.0.3");
117     internalServlet.doGet(request, response);
118     verify(response).setStatus(eq(HttpServletResponse.SC_FORBIDDEN));
119   }
120
121   @Test
122   public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_Then_Request_Succeeds() throws Exception {
123     when(request.getPathInfo()).thenReturn("/halt");
124     when(request.isSecure()).thenReturn(false);
125     when(request.getRemoteAddr()).thenReturn("127.0.0.1");
126     internalServlet.doGet(request, response);
127     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
128   }
129
130   @Test
131   public void Given_Request_Is_HTTP_GET_With_FetchProv_In_Endpoint_Then_Request_Succeeds()
132       throws Exception {
133     when(request.getPathInfo()).thenReturn("/fetchProv");
134     when(request.isSecure()).thenReturn(false);
135     internalServlet.doGet(request, response);
136     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
137       verifyEnteringExitCalled(listAppender);
138   }
139
140   @Test
141   public void Given_Request_Is_HTTP_GET_With_Prov_In_Endpoint_Then_Request_Succeeds() throws Exception {
142     when(request.getPathInfo()).thenReturn("/prov");
143     when(request.getQueryString()).thenReturn(null);
144     setPokerToNotCreateTimers();
145     ServletOutputStream outStream = mock(ServletOutputStream.class);
146     when(response.getOutputStream()).thenReturn(outStream);
147     internalServlet.doGet(request, response);
148     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
149   }
150
151   @Test
152   public void Given_Request_Is_HTTP_GET_With_Logs_In_Endpoint_Then_Request_Succeeds() throws Exception {
153     when(request.getPathInfo()).thenReturn("/logs/");
154     ServletOutputStream outStream = mock(ServletOutputStream.class);
155     when(response.getOutputStream()).thenReturn(outStream);
156     internalServlet.doGet(request, response);
157     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
158   }
159
160   @Test
161   public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_Then_Request_Succeeds()
162       throws Exception {
163     when(request.getPathInfo()).thenReturn("/logs/TestFile");
164     internalServlet.doGet(request, response);
165     verify(response)
166         .sendError(eq(HttpServletResponse.SC_NO_CONTENT), anyString());
167   }
168
169   @Test
170   public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_And_File_Exists_Then_Request_Returns_Ok()
171       throws Exception {
172     when(request.getPathInfo()).thenReturn("/logs/testFile.txt");
173     File testDir = new File("unit-test-logs");
174     File testFile = new File("unit-test-logs/testFile.txt");
175     testDir.mkdirs();
176     testFile.createNewFile();
177     testFile.deleteOnExit();
178     ServletOutputStream outStream = mock(ServletOutputStream.class);
179     when(response.getOutputStream()).thenReturn(outStream);
180     internalServlet.doGet(request, response);
181     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
182   }
183
184   @Test
185   public void Given_Request_Is_HTTP_GET_With_Api_In_Endpoint_Then_Request_Succeeds() throws Exception {
186     when(request.getPathInfo()).thenReturn("/api/DELIVERY_MAX_RETRY_INTERVAL");
187     ServletOutputStream outStream = mock(ServletOutputStream.class);
188     when(response.getOutputStream()).thenReturn(outStream);
189     internalServlet.doGet(request, response);
190     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
191   }
192
193   @Test
194   public void Given_Request_Is_HTTP_GET_With_Drlogs_In_Endpoint_Then_Request_Succeeds()
195       throws Exception {
196     when(request.getPathInfo()).thenReturn("/drlogs/");
197     ServletOutputStream outStream = mock(ServletOutputStream.class);
198     when(response.getOutputStream()).thenReturn(outStream);
199     internalServlet.doGet(request, response);
200     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
201   }
202
203   @Test
204   public void Given_Request_Is_HTTP_GET_With_Incorrect_Endpoint_Then_No_Content_Response_Is_Generated()
205       throws Exception {
206     when(request.getPathInfo()).thenReturn("/incorrect/");
207     internalServlet.doGet(request, response);
208     verify(response)
209         .sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
210   }
211
212   @Test
213   public void Given_Request_Is_HTTP_PUT_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
214       throws Exception {
215     when(request.getRemoteAddr()).thenReturn("127.100.0.3");
216     FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true);
217     internalServlet.doPut(request, response);
218     verify(response)
219         .sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
220     verifyEnteringExitCalled(listAppender);
221   }
222
223   @Test
224   public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_Request_Succeeds() throws Exception {
225     when(request.getPathInfo()).thenReturn("/api/NODES");
226     String[] values = {"V", "a", "l", "u", "e", "s"};
227     when(request.getParameterValues(anyString())).thenReturn(values);
228     internalServlet = internalServerSuccess();
229     setPokerToNotCreateTimers();
230     internalServlet.doPut(request, response);
231     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
232     verifyEnteringExitCalled(listAppender);
233   }
234
235   @Test
236   public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_And_Update_Fails_Then_Internal_Server_Error_Is_Generated()
237       throws Exception {
238     when(request.getPathInfo()).thenReturn("/api/NODES");
239     String[] values = {"V", "a", "l", "u", "e", "s"};
240     when(request.getParameterValues(anyString())).thenReturn(values);
241     internalServlet = internalServerFailure();
242     internalServlet.doPut(request, response);
243     verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR),
244         anyString());
245   }
246
247   @Test
248   public void Given_Request_Is_HTTP_PUT_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated()
249       throws Exception {
250     when(request.getPathInfo()).thenReturn("/incorrect");
251     internalServlet.doPut(request, response);
252     verify(response)
253         .sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
254   }
255
256   @Test
257   public void Given_Request_Is_HTTP_DELETE_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
258       throws Exception {
259     when(request.getRemoteAddr()).thenReturn("127.100.0.3");
260     FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true);
261     internalServlet.doDelete(request, response);
262     verify(response)
263         .sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
264     verifyEnteringExitCalled(listAppender);
265   }
266
267   @Test
268   public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_Request_Succeeds()
269       throws Exception {
270     when(request.getPathInfo()).thenReturn("/api/NODES");
271     String[] values = {"V", "a", "l", "u", "e", "s"};
272     when(request.getParameterValues(anyString())).thenReturn(values);
273     internalServlet = internalServerSuccess();
274     setPokerToNotCreateTimers();
275     internalServlet.doDelete(request, response);
276     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
277     verifyEnteringExitCalled(listAppender);
278   }
279
280   @Test
281   public void Given_Request_Is_HTTP_DELETE_With_LogRollInterval_Api_In_Endpoint_Request_Succeeds() {
282     when(request.getPathInfo()).thenReturn("/api/LOGROLL_INTERVAL");
283     internalServlet.doDelete(request, response);
284     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
285     Parameters p1 = Parameters.getParameter("NODES");
286     Assert.assertEquals("{\"keyname\":\"NODES\",\"value\":\"dmaap-dr-node\"}", p1.asJSONObject().toString());
287     Assert.assertEquals("PARAM: keyname=NODES, value=dmaap-dr-node", p1.toString());
288   }
289
290   @Test
291   public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_And_Delete_Fails_Then_Internal_Server_Error_Is_Generated()
292       throws Exception {
293     when(request.getPathInfo()).thenReturn("/api/NODES");
294     String[] values = {"V", "a", "l", "u", "e", "s"};
295     when(request.getParameterValues(anyString())).thenReturn(values);
296     internalServlet = internalServerFailure();
297     internalServlet.doDelete(request, response);
298     verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR),
299         anyString());
300   }
301
302   @Test
303   public void Given_Request_Is_HTTP_DELETE_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated()
304       throws Exception {
305     when(request.getPathInfo()).thenReturn("/incorrect");
306     internalServlet.doDelete(request, response);
307     verify(response)
308         .sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
309   }
310
311   @Test
312   public void Given_Request_Is_HTTP_POST_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
313       throws Exception {
314     when(request.getRemoteAddr()).thenReturn("127.100.0.3");
315     internalServlet.doPost(request, response);
316     verify(response)
317         .sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
318     verifyEnteringExitCalled(listAppender);
319   }
320
321   @Test
322   public void Given_Request_Is_HTTP_POST_With_Api_In_Endpoint_Request_Succeeds() throws Exception {
323     when(request.getPathInfo()).thenReturn("/api/key");
324     String[] values = {"V", "a", "l", "u", "e", "s"};
325     when(request.getParameterValues(anyString())).thenReturn(values);
326     internalServlet = internalServerSuccess();
327     setPokerToNotCreateTimers();
328     internalServlet.doPost(request, response);
329     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
330     verifyEnteringExitCalled(listAppender);
331   }
332
333   @Test
334   public void Given_Request_Is_HTTP_POST_With_Api_In_Endpoint_And_Insert_Fails_Then_Internal_Server_Error_Is_Generated()
335       throws Exception {
336     when(request.getPathInfo()).thenReturn("/api/Key");
337     String[] values = {"V", "a", "l", "u", "e", "s"};
338     when(request.getParameterValues(anyString())).thenReturn(values);
339     internalServlet = internalServerFailure();
340     internalServlet.doPost(request, response);
341     verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR),
342         anyString());
343   }
344
345   @Test
346   public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() {
347     when(request.getHeader("Content-Type")).thenReturn("stub_contentType");
348     when(request.getPathInfo()).thenReturn("/logs/");
349     internalServlet.doPost(request, response);
350     verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE));
351   }
352
353   @Test
354   public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Encoding_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() {
355     when(request.getHeader("Content-Encoding")).thenReturn("not-supported");
356     when(request.getPathInfo()).thenReturn("/logs/");
357     internalServlet.doPost(request, response);
358     verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE));
359   }
360
361   @Test
362   public void Given_Request_Is_HTTP_POST_To_Logs_Then_Request_Succeeds()
363       throws Exception {
364     when(request.getHeader("Content-Encoding")).thenReturn("gzip");
365     when(request.getPathInfo()).thenReturn("/logs/");
366     ServletInputStream inStream = mock(ServletInputStream.class);
367     when(request.getInputStream()).thenReturn(inStream);
368     File testDir = new File("unit-test-logs/spool");
369     testDir.mkdirs();
370     testDir.deleteOnExit();
371     internalServlet.doPost(request, response);
372     verify(response).setStatus(eq(HttpServletResponse.SC_CREATED));
373   }
374
375   @Test
376   public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Then_Unsupported_Media_Type_Response_Is_Generated() {
377     when(request.getHeader("Content-Type")).thenReturn("stub_contentType");
378     when(request.getPathInfo()).thenReturn("/drlogs/");
379     internalServlet.doPost(request, response);
380     verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE));
381   }
382
383   @Test
384   public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Request_Succeeds() throws Exception {
385     when(request.getPathInfo()).thenReturn("/drlogs/");
386     ServletInputStream inStream = mock(ServletInputStream.class);
387     when(inStream.read()).thenReturn(1, -1);
388     when(request.getInputStream()).thenReturn(inStream);
389     PowerMockito.mockStatic(LogRecord.class);
390     internalServlet.doPost(request, response);
391     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
392   }
393
394   @Test
395   public void Given_Request_Is_HTTP_POST_To_Api_And_Request_Succeeds() {
396     when(request.getPathInfo()).thenReturn("/api/NEW_PARAM?val=blah");
397     internalServlet.doPost(request, response);
398     verify(response).setStatus(eq(HttpServletResponse.SC_OK));
399   }
400
401   @Test
402   public void Given_Request_Is_HTTP_POST_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated()
403       throws Exception {
404     when(request.getPathInfo()).thenReturn("/incorrect/");
405     internalServlet.doPost(request, response);
406     verify(response)
407         .sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
408   }
409
410   private void setUpValidAuthorisedRequest() throws Exception {
411     setUpValidSecurityOnHttpRequest();
412     setBehalfHeader("Stub_Value");
413     setValidPathInfoInHttpHeader();
414     when(request.getHeader("Content-Type")).thenReturn("text/plain");
415     when(request.getHeader("Content-Encoding")).thenReturn("gzip");
416   }
417
418   private void setUpValidSecurityOnHttpRequest() throws Exception {
419     when(request.isSecure()).thenReturn(true);
420     when(request.getRemoteAddr()).thenReturn(InetAddress.getLocalHost().getHostAddress());
421     InetAddress[] nodeAddresses = new InetAddress[1];
422     nodeAddresses[0] = InetAddress.getLocalHost();
423     FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodeAddresses", nodeAddresses, true);
424     FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true);
425   }
426
427   private void setBehalfHeader(String headerValue) {
428     when(request.getHeader(BEHALF_HEADER)).thenReturn(headerValue);
429   }
430
431   private void setValidPathInfoInHttpHeader() {
432     when(request.getPathInfo()).thenReturn("/123");
433   }
434
435   private void setPokerToNotCreateTimers() throws Exception {
436     Poker poker = mock(Poker.class);
437     FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true);
438   }
439
440   private InternalServlet internalServerSuccess() {
441     InternalServlet internalServlet = new InternalServlet() {
442
443       protected boolean doUpdate(Updateable bean) {
444         return true;
445       }
446
447       protected boolean doDelete(Deleteable bean) {
448         return true;
449       }
450
451       protected boolean doInsert(Insertable bean) {
452         return true;
453       }
454     };
455     return internalServlet;
456   }
457
458   private InternalServlet internalServerFailure() {
459     InternalServlet internalServlet = new InternalServlet() {
460
461       protected boolean doUpdate(Updateable bean) {
462         return false;
463       }
464
465       protected boolean doDelete(Deleteable bean) {
466         return false;
467       }
468
469       protected boolean doInsert(Insertable bean) {
470         return false;
471       }
472     };
473     return internalServlet;
474   }
475 }