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.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;
32 import ch.qos.logback.classic.spi.ILoggingEvent;
33 import ch.qos.logback.core.read.ListAppender;
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;
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;
71 private HttpServletRequest request;
74 private HttpServletResponse response;
76 ListAppender<ILoggingEvent> listAppender;
79 public static void init() {
80 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
81 em = emf.createEntityManager();
83 "org.onap.dmaap.datarouter.provserver.properties",
84 "src/test/resources/h2Database.properties");
88 public static void tearDownClass() {
95 public void setUp() throws Exception {
96 listAppender = setTestLogger(InternalServlet.class);
97 internalServlet = new InternalServlet();
98 setUpValidAuthorisedRequest();
102 public void Given_Request_Is_HTTP_GET_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
104 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
105 internalServlet.doGet(request, response);
107 .sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
108 verifyEnteringExitCalled(listAppender);
112 public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_But_Not_Sent_From_Localhost_Then_Forbidden_Response_Is_Generated()
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));
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));
131 public void Given_Request_Is_HTTP_GET_With_FetchProv_In_Endpoint_Then_Request_Succeeds()
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);
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));
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));
161 public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_Then_Request_Succeeds()
163 when(request.getPathInfo()).thenReturn("/logs/TestFile");
164 internalServlet.doGet(request, response);
166 .sendError(eq(HttpServletResponse.SC_NO_CONTENT), anyString());
170 public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_And_File_Exists_Then_Request_Returns_Ok()
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");
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));
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));
194 public void Given_Request_Is_HTTP_GET_With_Drlogs_In_Endpoint_Then_Request_Succeeds()
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));
204 public void Given_Request_Is_HTTP_GET_With_Incorrect_Endpoint_Then_No_Content_Response_Is_Generated()
206 when(request.getPathInfo()).thenReturn("/incorrect/");
207 internalServlet.doGet(request, response);
209 .sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
213 public void Given_Request_Is_HTTP_PUT_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
215 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
216 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true);
217 internalServlet.doPut(request, response);
219 .sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
220 verifyEnteringExitCalled(listAppender);
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);
236 public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_And_Update_Fails_Then_Internal_Server_Error_Is_Generated()
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),
248 public void Given_Request_Is_HTTP_PUT_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated()
250 when(request.getPathInfo()).thenReturn("/incorrect");
251 internalServlet.doPut(request, response);
253 .sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
257 public void Given_Request_Is_HTTP_DELETE_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
259 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
260 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true);
261 internalServlet.doDelete(request, response);
263 .sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
264 verifyEnteringExitCalled(listAppender);
268 public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_Request_Succeeds()
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);
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());
291 public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_And_Delete_Fails_Then_Internal_Server_Error_Is_Generated()
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),
303 public void Given_Request_Is_HTTP_DELETE_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated()
305 when(request.getPathInfo()).thenReturn("/incorrect");
306 internalServlet.doDelete(request, response);
308 .sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
312 public void Given_Request_Is_HTTP_POST_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
314 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
315 internalServlet.doPost(request, response);
317 .sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
318 verifyEnteringExitCalled(listAppender);
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);
334 public void Given_Request_Is_HTTP_POST_With_Api_In_Endpoint_And_Insert_Fails_Then_Internal_Server_Error_Is_Generated()
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),
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));
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));
362 public void Given_Request_Is_HTTP_POST_To_Logs_Then_Request_Succeeds()
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");
370 testDir.deleteOnExit();
371 internalServlet.doPost(request, response);
372 verify(response).setStatus(eq(HttpServletResponse.SC_CREATED));
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));
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));
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));
402 public void Given_Request_Is_HTTP_POST_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated()
404 when(request.getPathInfo()).thenReturn("/incorrect/");
405 internalServlet.doPost(request, response);
407 .sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
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");
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);
427 private void setBehalfHeader(String headerValue) {
428 when(request.getHeader(BEHALF_HEADER)).thenReturn(headerValue);
431 private void setValidPathInfoInHttpHeader() {
432 when(request.getPathInfo()).thenReturn("/123");
435 private void setPokerToNotCreateTimers() throws Exception {
436 Poker poker = mock(Poker.class);
437 FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true);
440 private InternalServlet internalServerSuccess() {
441 InternalServlet internalServlet = new InternalServlet() {
443 protected boolean doUpdate(Updateable bean) {
447 protected boolean doDelete(Deleteable bean) {
451 protected boolean doInsert(Insertable bean) {
455 return internalServlet;
458 private InternalServlet internalServerFailure() {
459 InternalServlet internalServlet = new InternalServlet() {
461 protected boolean doUpdate(Updateable bean) {
465 protected boolean doDelete(Deleteable bean) {
469 protected boolean doInsert(Insertable bean) {
473 return internalServlet;