1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
23 package org.onap.dmaap.datarouter.provisioning;
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;
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;
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;
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;
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;
73 private HttpServletRequest request;
76 private HttpServletResponse response;
78 ListAppender<ILoggingEvent> listAppender;
81 public static void init() {
82 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
83 em = emf.createEntityManager();
85 "org.onap.dmaap.datarouter.provserver.properties",
86 "src/test/resources/h2Database.properties");
90 public static void tearDownClass() {
97 public void setUp() throws Exception {
98 listAppender = setTestLogger(InternalServlet.class);
99 internalServlet = new InternalServlet();
100 setUpValidAuthorisedRequest();
104 public void Given_Request_Is_HTTP_GET_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
106 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
107 internalServlet.doGet(request, response);
109 .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
110 verifyEnteringExitCalled(listAppender);
114 public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_But_Not_Sent_From_Localhost_Then_Forbidden_Response_Is_Generated()
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));
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));
133 public void Given_Request_Is_HTTP_GET_With_FetchProv_In_Endpoint_Then_Request_Succeeds()
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);
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));
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));
163 public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_Then_Request_Succeeds()
165 when(request.getPathInfo()).thenReturn("/logs/TestFile");
166 internalServlet.doGet(request, response);
168 .sendError(eq(HttpServletResponse.SC_NO_CONTENT), argThat(notNullValue(String.class)));
172 public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_And_File_Exists_Then_Request_Returns_Ok()
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");
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));
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));
196 public void Given_Request_Is_HTTP_GET_With_Drlogs_In_Endpoint_Then_Request_Succeeds()
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));
206 public void Given_Request_Is_HTTP_GET_With_Incorrect_Endpoint_Then_No_Content_Response_Is_Generated()
208 when(request.getPathInfo()).thenReturn("/incorrect/");
209 internalServlet.doGet(request, response);
211 .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
215 public void Given_Request_Is_HTTP_PUT_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
217 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
218 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true);
219 internalServlet.doPut(request, response);
221 .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
222 verifyEnteringExitCalled(listAppender);
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);
238 public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_And_Update_Fails_Then_Internal_Server_Error_Is_Generated()
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)));
250 public void Given_Request_Is_HTTP_PUT_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated()
252 when(request.getPathInfo()).thenReturn("/incorrect");
253 internalServlet.doPut(request, response);
255 .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
259 public void Given_Request_Is_HTTP_DELETE_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
261 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
262 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true);
263 internalServlet.doDelete(request, response);
265 .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
266 verifyEnteringExitCalled(listAppender);
270 public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_Request_Succeeds()
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);
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());
293 public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_And_Delete_Fails_Then_Internal_Server_Error_Is_Generated()
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)));
305 public void Given_Request_Is_HTTP_DELETE_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated()
307 when(request.getPathInfo()).thenReturn("/incorrect");
308 internalServlet.doDelete(request, response);
310 .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
314 public void Given_Request_Is_HTTP_POST_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
316 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
317 internalServlet.doPost(request, response);
319 .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
320 verifyEnteringExitCalled(listAppender);
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);
336 public void Given_Request_Is_HTTP_POST_With_Api_In_Endpoint_And_Insert_Fails_Then_Internal_Server_Error_Is_Generated()
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)));
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));
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));
364 public void Given_Request_Is_HTTP_POST_To_Logs_Then_Request_Succeeds()
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");
372 testDir.deleteOnExit();
373 internalServlet.doPost(request, response);
374 verify(response).setStatus(eq(HttpServletResponse.SC_CREATED));
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));
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));
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));
404 public void Given_Request_Is_HTTP_POST_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated()
406 when(request.getPathInfo()).thenReturn("/incorrect/");
407 internalServlet.doPost(request, response);
409 .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
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");
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);
429 private void setBehalfHeader(String headerValue) {
430 when(request.getHeader(BEHALF_HEADER)).thenReturn(headerValue);
433 private void setValidPathInfoInHttpHeader() {
434 when(request.getPathInfo()).thenReturn("/123");
437 private void setPokerToNotCreateTimers() throws Exception {
438 Poker poker = mock(Poker.class);
439 FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true);
442 private InternalServlet internalServerSuccess() {
443 InternalServlet internalServlet = new InternalServlet() {
445 protected boolean doUpdate(Updateable bean) {
449 protected boolean doDelete(Deleteable bean) {
453 protected boolean doInsert(Insertable bean) {
457 return internalServlet;
460 private InternalServlet internalServerFailure() {
461 InternalServlet internalServlet = new InternalServlet() {
463 protected boolean doUpdate(Updateable bean) {
467 protected boolean doDelete(Deleteable bean) {
471 protected boolean doInsert(Insertable bean) {
475 return internalServlet;