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 org.apache.commons.lang3.reflect.FieldUtils;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.junit.BeforeClass;
49 import org.junit.AfterClass;
50 import org.mockito.Mock;
52 import org.onap.dmaap.datarouter.provisioning.beans.Deleteable;
53 import org.onap.dmaap.datarouter.provisioning.beans.Insertable;
54 import org.onap.dmaap.datarouter.provisioning.beans.LogRecord;
55 import org.onap.dmaap.datarouter.provisioning.beans.Updateable;
56 import org.powermock.api.mockito.PowerMockito;
57 import org.powermock.core.classloader.annotations.PrepareForTest;
58 import org.powermock.modules.junit4.PowerMockRunner;
60 @RunWith(PowerMockRunner.class)
61 @PrepareForTest(LogRecord.class)
62 public class InternalServletTest extends DrServletTestBase {
63 private static EntityManagerFactory emf;
64 private static EntityManager em;
65 private InternalServlet internalServlet;
68 private HttpServletRequest request;
71 private HttpServletResponse response;
74 public static void init() {
75 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
76 em = emf.createEntityManager();
78 "org.onap.dmaap.datarouter.provserver.properties",
79 "src/test/resources/h2Database.properties");
83 public static void tearDownClass() {
90 public void setUp() throws Exception {
91 internalServlet = new InternalServlet();
92 setUpValidAuthorisedRequest();
96 public void Given_Request_Is_HTTP_GET_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
98 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
99 internalServlet.doGet(request, response);
101 .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
105 public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_But_Not_Sent_From_Localhost_Then_Forbidden_Response_Is_Generated()
107 when(request.getPathInfo()).thenReturn("/halt");
108 when(request.isSecure()).thenReturn(false);
109 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
110 internalServlet.doGet(request, response);
111 verify(response).setStatus(eq(HttpServletResponse.SC_FORBIDDEN));
115 public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_Then_Request_Succeeds() throws Exception {
116 when(request.getPathInfo()).thenReturn("/halt");
117 when(request.isSecure()).thenReturn(false);
118 when(request.getRemoteAddr()).thenReturn("127.0.0.1");
119 internalServlet.doGet(request, response);
120 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
124 public void Given_Request_Is_HTTP_GET_With_FetchProv_In_Endpoint_Then_Request_Succeeds()
126 when(request.getPathInfo()).thenReturn("/fetchProv");
127 when(request.isSecure()).thenReturn(false);
128 internalServlet.doGet(request, response);
129 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
133 public void Given_Request_Is_HTTP_GET_With_Prov_In_Endpoint_Then_Request_Succeeds() throws Exception {
134 when(request.getPathInfo()).thenReturn("/prov");
135 when(request.getQueryString()).thenReturn(null);
136 setPokerToNotCreateTimers();
137 ServletOutputStream outStream = mock(ServletOutputStream.class);
138 when(response.getOutputStream()).thenReturn(outStream);
139 internalServlet.doGet(request, response);
140 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
144 public void Given_Request_Is_HTTP_GET_With_Logs_In_Endpoint_Then_Request_Succeeds() throws Exception {
145 when(request.getPathInfo()).thenReturn("/logs/");
146 ServletOutputStream outStream = mock(ServletOutputStream.class);
147 when(response.getOutputStream()).thenReturn(outStream);
148 internalServlet.doGet(request, response);
149 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
153 public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_Then_Request_Succeeds()
155 when(request.getPathInfo()).thenReturn("/logs/TestFile");
156 internalServlet.doGet(request, response);
158 .sendError(eq(HttpServletResponse.SC_NO_CONTENT), argThat(notNullValue(String.class)));
162 public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_And_File_Exists_Then_Request_Returns_Ok()
164 when(request.getPathInfo()).thenReturn("/logs/testFile.txt");
165 File testFile = new File("unit-test-logs/testFile.txt");
166 testFile.createNewFile();
167 testFile.deleteOnExit();
168 ServletOutputStream outStream = mock(ServletOutputStream.class);
169 when(response.getOutputStream()).thenReturn(outStream);
170 internalServlet.doGet(request, response);
171 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
175 public void Given_Request_Is_HTTP_GET_With_Api_In_Endpoint_Then_Request_Succeeds() throws Exception {
176 when(request.getPathInfo()).thenReturn("/api/DELIVERY_MAX_RETRY_INTERVAL");
177 ServletOutputStream outStream = mock(ServletOutputStream.class);
178 when(response.getOutputStream()).thenReturn(outStream);
179 internalServlet.doGet(request, response);
180 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
184 public void Given_Request_Is_HTTP_GET_With_Drlogs_In_Endpoint_Then_Request_Succeeds()
186 when(request.getPathInfo()).thenReturn("/drlogs/");
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_Incorrect_Endpoint_Then_No_Content_Response_Is_Generated()
196 when(request.getPathInfo()).thenReturn("/incorrect/");
197 internalServlet.doGet(request, response);
199 .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
203 public void Given_Request_Is_HTTP_PUT_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
205 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
206 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true);
207 internalServlet.doPut(request, response);
209 .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
213 public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_Request_Succeeds() throws Exception {
214 when(request.getPathInfo()).thenReturn("/api/NODES");
215 String[] values = {"V", "a", "l", "u", "e", "s"};
216 when(request.getParameterValues(anyString())).thenReturn(values);
217 internalServlet = internalServerSuccess();
218 setPokerToNotCreateTimers();
219 internalServlet.doPut(request, response);
220 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
224 public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_And_Update_Fails_Then_Internal_Server_Error_Is_Generated()
226 when(request.getPathInfo()).thenReturn("/api/NODES");
227 String[] values = {"V", "a", "l", "u", "e", "s"};
228 when(request.getParameterValues(anyString())).thenReturn(values);
229 internalServlet = internalServerFailure();
230 internalServlet.doPut(request, response);
231 verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR),
232 argThat(notNullValue(String.class)));
236 public void Given_Request_Is_HTTP_PUT_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated()
238 when(request.getPathInfo()).thenReturn("/incorrect");
239 internalServlet.doPut(request, response);
241 .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
245 public void Given_Request_Is_HTTP_DELETE_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
247 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
248 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true);
249 internalServlet.doDelete(request, response);
251 .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
255 public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_Request_Succeeds()
257 when(request.getPathInfo()).thenReturn("/api/NODES");
258 String[] values = {"V", "a", "l", "u", "e", "s"};
259 when(request.getParameterValues(anyString())).thenReturn(values);
260 internalServlet = internalServerSuccess();
261 setPokerToNotCreateTimers();
262 internalServlet.doDelete(request, response);
263 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
267 public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_And_Delete_Fails_Then_Internal_Server_Error_Is_Generated()
269 when(request.getPathInfo()).thenReturn("/api/NODES");
270 String[] values = {"V", "a", "l", "u", "e", "s"};
271 when(request.getParameterValues(anyString())).thenReturn(values);
272 internalServlet = internalServerFailure();
273 internalServlet.doDelete(request, response);
274 verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR),
275 argThat(notNullValue(String.class)));
279 public void Given_Request_Is_HTTP_DELETE_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated()
281 when(request.getPathInfo()).thenReturn("/incorrect");
282 internalServlet.doDelete(request, response);
284 .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
288 public void Given_Request_Is_HTTP_POST_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
290 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
291 internalServlet.doPost(request, response);
293 .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
297 public void Given_Request_Is_HTTP_POST_With_Api_In_Endpoint_Request_Succeeds() throws Exception {
298 when(request.getPathInfo()).thenReturn("/api/key");
299 String[] values = {"V", "a", "l", "u", "e", "s"};
300 when(request.getParameterValues(anyString())).thenReturn(values);
301 internalServlet = internalServerSuccess();
302 setPokerToNotCreateTimers();
303 internalServlet.doPost(request, response);
304 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
308 public void Given_Request_Is_HTTP_POST_With_Api_In_Endpoint_And_Insert_Fails_Then_Internal_Server_Error_Is_Generated()
310 when(request.getPathInfo()).thenReturn("/api/Key");
311 String[] values = {"V", "a", "l", "u", "e", "s"};
312 when(request.getParameterValues(anyString())).thenReturn(values);
313 internalServlet = internalServerFailure();
314 internalServlet.doPost(request, response);
315 verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR),
316 argThat(notNullValue(String.class)));
320 public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated()
322 when(request.getHeader("Content-Type")).thenReturn("stub_contentType");
323 when(request.getPathInfo()).thenReturn("/logs/");
324 internalServlet.doPost(request, response);
325 verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE));
329 public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Encoding_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated()
331 when(request.getHeader("Content-Encoding")).thenReturn("not-supported");
332 when(request.getPathInfo()).thenReturn("/logs/");
333 internalServlet.doPost(request, response);
334 verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE));
338 public void Given_Request_Is_HTTP_POST_To_Logs_Then_Request_Succeeds()
340 when(request.getHeader("Content-Encoding")).thenReturn("gzip");
341 when(request.getPathInfo()).thenReturn("/logs/");
342 ServletInputStream inStream = mock(ServletInputStream.class);
343 when(request.getInputStream()).thenReturn(inStream);
344 File testDir = new File("unit-test-logs/spool");
346 testDir.deleteOnExit();
347 internalServlet.doPost(request, response);
348 verify(response).setStatus(eq(HttpServletResponse.SC_CREATED));
352 public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Then_Unsupported_Media_Type_Response_Is_Generated()
354 when(request.getHeader("Content-Type")).thenReturn("stub_contentType");
355 when(request.getPathInfo()).thenReturn("/drlogs/");
356 internalServlet.doPost(request, response);
357 verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE));
361 public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Request_Succeeds() throws Exception {
362 when(request.getPathInfo()).thenReturn("/drlogs/");
363 ServletInputStream inStream = mock(ServletInputStream.class);
364 when(inStream.read()).thenReturn(1, -1);
365 when(request.getInputStream()).thenReturn(inStream);
366 PowerMockito.mockStatic(LogRecord.class);
367 internalServlet.doPost(request, response);
368 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
372 public void Given_Request_Is_HTTP_POST_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated()
374 when(request.getPathInfo()).thenReturn("/incorrect/");
375 internalServlet.doPost(request, response);
377 .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
380 private void setUpValidAuthorisedRequest() throws Exception {
381 setUpValidSecurityOnHttpRequest();
382 setBehalfHeader("Stub_Value");
383 setValidPathInfoInHttpHeader();
384 when(request.getHeader("Content-Type")).thenReturn("text/plain");
385 when(request.getHeader("Content-Encoding")).thenReturn("gzip");
388 private void setUpValidSecurityOnHttpRequest() throws Exception {
389 when(request.isSecure()).thenReturn(true);
390 when(request.getRemoteAddr()).thenReturn(InetAddress.getLocalHost().getHostAddress());
391 InetAddress[] nodeAddresses = new InetAddress[1];
392 nodeAddresses[0] = InetAddress.getLocalHost();
393 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodeAddresses", nodeAddresses, true);
394 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true);
397 private void setBehalfHeader(String headerValue) {
398 when(request.getHeader(BEHALF_HEADER)).thenReturn(headerValue);
401 private void setValidPathInfoInHttpHeader() {
402 when(request.getPathInfo()).thenReturn("/123");
405 private void setPokerToNotCreateTimers() throws Exception {
406 Poker poker = mock(Poker.class);
407 FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true);
410 private InternalServlet internalServerSuccess() {
411 InternalServlet internalServlet = new InternalServlet() {
413 protected boolean doUpdate(Updateable bean) {
417 protected boolean doDelete(Deleteable bean) {
421 protected boolean doInsert(Insertable bean) {
425 return internalServlet;
428 private InternalServlet internalServerFailure() {
429 InternalServlet internalServlet = new InternalServlet() {
431 protected boolean doUpdate(Updateable bean) {
435 protected boolean doDelete(Deleteable bean) {
439 protected boolean doInsert(Insertable bean) {
443 return internalServlet;