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