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;
34 import java.net.InetAddress;
35 import java.util.HashMap;
37 import javax.servlet.ServletInputStream;
38 import javax.servlet.ServletOutputStream;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41 import org.apache.commons.lang3.reflect.FieldUtils;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.onap.dmaap.datarouter.authz.AuthorizationResponse;
47 import org.onap.dmaap.datarouter.authz.Authorizer;
48 import org.onap.dmaap.datarouter.provisioning.beans.Deleteable;
49 import org.onap.dmaap.datarouter.provisioning.beans.Feed;
50 import org.onap.dmaap.datarouter.provisioning.beans.Insertable;
51 import org.onap.dmaap.datarouter.provisioning.beans.LogRecord;
52 import org.onap.dmaap.datarouter.provisioning.beans.NodeClass;
53 import org.onap.dmaap.datarouter.provisioning.beans.Parameters;
54 import org.onap.dmaap.datarouter.provisioning.beans.Subscription;
55 import org.onap.dmaap.datarouter.provisioning.beans.Updateable;
56 import org.onap.dmaap.datarouter.provisioning.utils.LogfileLoader;
57 import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet;
58 import org.powermock.api.mockito.PowerMockito;
59 import org.powermock.core.classloader.annotations.PrepareForTest;
60 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
61 import org.powermock.modules.junit4.PowerMockRunner;
63 @RunWith(PowerMockRunner.class)
64 @PrepareForTest(LogRecord.class)
65 @SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Feed",
66 "org.onap.dmaap.datarouter.provisioning.beans.Parameters",
67 "org.onap.dmaap.datarouter.provisioning.beans.NodeClass",
68 "org.onap.dmaap.datarouter.provisioning.beans.Subscription",
69 "org.onap.dmaap.datarouter.provisioning.utils.LogfileLoader"})
70 public class InternalServletTest extends DrServletTestBase {
72 private InternalServlet internalServlet;
75 private HttpServletRequest request;
78 private HttpServletResponse response;
81 public void setUp() throws Exception {
83 internalServlet = new InternalServlet();
84 setAuthoriserToReturnRequestIsAuthorized();
85 setUpValidAuthorisedRequest();
89 public void Given_Request_Is_HTTP_GET_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
91 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
92 internalServlet.doGet(request, response);
94 .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
98 public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_But_Not_Sent_From_Localhost_Then_Forbidden_Response_Is_Generated()
100 when(request.getPathInfo()).thenReturn("/halt");
101 when(request.isSecure()).thenReturn(false);
102 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
103 internalServlet.doGet(request, response);
104 verify(response).setStatus(eq(HttpServletResponse.SC_FORBIDDEN));
108 public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_Request_Succeeds() throws Exception {
109 when(request.getPathInfo()).thenReturn("/halt");
110 when(request.isSecure()).thenReturn(false);
111 when(request.getRemoteAddr()).thenReturn("127.0.0.1");
112 internalServlet.doGet(request, response);
113 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
117 public void Given_Request_Is_HTTP_GET_With_FetchProv_In_Endpoint_Request_Succeeds()
119 when(request.getPathInfo()).thenReturn("/fetchProv");
120 when(request.isSecure()).thenReturn(false);
121 internalServlet.doGet(request, response);
122 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
126 public void Given_Request_Is_HTTP_GET_With_Prov_In_Endpoint_Request_Succeeds() throws Exception {
127 when(request.getPathInfo()).thenReturn("/prov");
128 when(request.getQueryString()).thenReturn(null);
129 setPokerToNotCreateTimers();
130 ServletOutputStream outStream = mock(ServletOutputStream.class);
131 when(response.getOutputStream()).thenReturn(outStream);
132 internalServlet.doGet(request, response);
133 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
137 public void Given_Request_Is_HTTP_GET_With_Logs_In_Endpoint_Request_Succeeds() throws Exception {
138 when(request.getPathInfo()).thenReturn("/logs/");
139 ServletOutputStream outStream = mock(ServletOutputStream.class);
140 when(response.getOutputStream()).thenReturn(outStream);
141 internalServlet.doGet(request, response);
142 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
146 public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_Request_Succeeds()
148 when(request.getPathInfo()).thenReturn("/logs/TestFile");
149 internalServlet.doGet(request, response);
151 .sendError(eq(HttpServletResponse.SC_NO_CONTENT), argThat(notNullValue(String.class)));
155 public void Given_Request_Is_HTTP_GET_With_Api_In_Endpoint_Request_Succeeds() throws Exception {
156 when(request.getPathInfo()).thenReturn("/api/Key");
157 setParametersToNotContactDb(false);
158 ServletOutputStream outStream = mock(ServletOutputStream.class);
159 when(response.getOutputStream()).thenReturn(outStream);
160 internalServlet.doGet(request, response);
161 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
165 public void Given_Request_Is_HTTP_GET_With_Drlogs_In_Endpoint_Request_Succeeds()
167 when(request.getPathInfo()).thenReturn("/drlogs/");
168 PowerMockito.mockStatic(LogfileLoader.class);
169 LogfileLoader logfileLoader = mock(LogfileLoader.class);
170 when(logfileLoader.getBitSet()).thenReturn(new RLEBitSet());
171 PowerMockito.when(LogfileLoader.getLoader()).thenReturn(logfileLoader);
172 ServletOutputStream outStream = mock(ServletOutputStream.class);
173 when(response.getOutputStream()).thenReturn(outStream);
174 internalServlet.doGet(request, response);
175 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
179 public void Given_Request_Is_HTTP_GET_Incorrect_Endpoint_Then_No_Content_Response_Is_Generated()
181 when(request.getPathInfo()).thenReturn("/incorrect/");
182 internalServlet.doGet(request, response);
184 .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
188 public void Given_Request_Is_HTTP_PUT_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
190 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
191 internalServlet.doPut(request, response);
193 .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
197 public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_Request_Succeeds() throws Exception {
198 when(request.getPathInfo()).thenReturn("/api/Key");
199 setParametersToNotContactDb(false);
200 String[] values = {"V", "a", "l", "u", "e", "s"};
201 when(request.getParameterValues(anyString())).thenReturn(values);
202 internalServlet = internalServerSuccess();
203 setPokerToNotCreateTimers();
204 mockProvisioningParametersChanged();
205 internalServlet.doPut(request, response);
206 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
210 public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_And_Update_Fails_Then_Internal_Server_Error_Is_Generated()
212 when(request.getPathInfo()).thenReturn("/api/Key");
213 setParametersToNotContactDb(false);
214 String[] values = {"V", "a", "l", "u", "e", "s"};
215 when(request.getParameterValues(anyString())).thenReturn(values);
216 internalServlet = internalServerFailure();
217 internalServlet.doPut(request, response);
218 verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR),
219 argThat(notNullValue(String.class)));
223 public void Given_Request_Is_HTTP_PUT_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated()
225 when(request.getPathInfo()).thenReturn("/incorrect");
226 internalServlet.doPut(request, response);
228 .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
232 public void Given_Request_Is_HTTP_DELETE_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
234 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
235 internalServlet.doDelete(request, response);
237 .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
241 public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_Request_Succeeds()
243 when(request.getPathInfo()).thenReturn("/api/Key");
244 setParametersToNotContactDb(false);
245 String[] values = {"V", "a", "l", "u", "e", "s"};
246 when(request.getParameterValues(anyString())).thenReturn(values);
247 internalServlet = internalServerSuccess();
248 setPokerToNotCreateTimers();
249 mockProvisioningParametersChanged();
250 internalServlet.doDelete(request, response);
251 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
255 public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_And_Delete_Fails_Then_Internal_Server_Error_Is_Generated()
257 when(request.getPathInfo()).thenReturn("/api/Key");
258 setParametersToNotContactDb(false);
259 String[] values = {"V", "a", "l", "u", "e", "s"};
260 when(request.getParameterValues(anyString())).thenReturn(values);
261 internalServlet = internalServerFailure();
262 internalServlet.doDelete(request, response);
263 verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR),
264 argThat(notNullValue(String.class)));
268 public void Given_Request_Is_HTTP_DELETE_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated()
270 when(request.getPathInfo()).thenReturn("/incorrect");
271 internalServlet.doDelete(request, response);
273 .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
277 public void Given_Request_Is_HTTP_POST_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
279 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
280 internalServlet.doPost(request, response);
282 .sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
286 public void Given_Request_Is_HTTP_POST_With_Api_In_Endpoint_Request_Succeeds() throws Exception {
287 when(request.getPathInfo()).thenReturn("/api/Key");
288 setParametersToNotContactDb(true);
289 String[] values = {"V", "a", "l", "u", "e", "s"};
290 when(request.getParameterValues(anyString())).thenReturn(values);
291 internalServlet = internalServerSuccess();
292 setPokerToNotCreateTimers();
293 mockProvisioningParametersChanged();
294 internalServlet.doPost(request, response);
295 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
299 public void Given_Request_Is_HTTP_POST_With_Api_In_Endpoint_And_Insert_Fails_Then_Internal_Server_Error_Is_Generated()
301 when(request.getPathInfo()).thenReturn("/api/Key");
302 setParametersToNotContactDb(true);
303 String[] values = {"V", "a", "l", "u", "e", "s"};
304 when(request.getParameterValues(anyString())).thenReturn(values);
305 internalServlet = internalServerFailure();
306 internalServlet.doPost(request, response);
307 verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR),
308 argThat(notNullValue(String.class)));
312 public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated()
314 when(request.getHeader("Content-Type")).thenReturn("stub_contentType");
315 when(request.getPathInfo()).thenReturn("/logs/");
316 internalServlet.doPost(request, response);
317 verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE));
321 public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Encoding_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated()
323 when(request.getHeader("Content-Encoding")).thenReturn("not-supported");
324 when(request.getPathInfo()).thenReturn("/logs/");
325 internalServlet.doPost(request, response);
326 verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE));
330 public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Then_Unsupported_Media_Type_Response_Is_Generated()
332 when(request.getHeader("Content-Type")).thenReturn("stub_contentType");
333 when(request.getPathInfo()).thenReturn("/drlogs/");
334 internalServlet.doPost(request, response);
335 verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE));
339 public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Request_Succeeds() throws Exception {
340 when(request.getPathInfo()).thenReturn("/drlogs/");
341 ServletInputStream inStream = mock(ServletInputStream.class);
342 when(inStream.read()).thenReturn(1, -1);
343 when(request.getInputStream()).thenReturn(inStream);
344 PowerMockito.mockStatic(LogRecord.class);
345 internalServlet.doPost(request, response);
346 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
350 public void Given_Request_Is_HTTP_POST_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated()
352 when(request.getPathInfo()).thenReturn("/incorrect/");
353 internalServlet.doPost(request, response);
355 .sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
358 private void setAuthoriserToReturnRequestIsAuthorized() throws IllegalAccessException {
359 AuthorizationResponse authResponse = mock(AuthorizationResponse.class);
360 Authorizer authorizer = mock(Authorizer.class);
361 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true);
362 when(authorizer.decide(request)).thenReturn(authResponse);
363 when(authResponse.isAuthorized()).thenReturn(true);
366 private void setUpValidAuthorisedRequest() throws Exception {
367 setUpValidSecurityOnHttpRequest();
368 setBehalfHeader("Stub_Value");
369 setValidPathInfoInHttpHeader();
370 when(request.getHeader("Content-Type")).thenReturn("text/plain");
371 when(request.getHeader("Content-Encoding")).thenReturn("gzip");
374 private void setUpValidSecurityOnHttpRequest() throws Exception {
375 when(request.isSecure()).thenReturn(true);
376 when(request.getRemoteAddr()).thenReturn(InetAddress.getLocalHost().getHostAddress());
377 InetAddress[] nodeAddresses = new InetAddress[1];
378 nodeAddresses[0] = InetAddress.getLocalHost();
379 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodeAddresses", nodeAddresses, true);
380 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true);
383 private void setBehalfHeader(String headerValue) {
384 when(request.getHeader(BEHALF_HEADER)).thenReturn(headerValue);
387 private void setValidPathInfoInHttpHeader() {
388 when(request.getPathInfo()).thenReturn("/123");
391 private void setPokerToNotCreateTimers() throws Exception {
392 Poker poker = mock(Poker.class);
393 FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true);
396 private void setParametersToNotContactDb(boolean isPost) {
397 PowerMockito.mockStatic(Parameters.class);
398 Parameters parameters = mock(Parameters.class);
400 PowerMockito.when(Parameters.getParameter(anyString())).thenReturn(null);
402 PowerMockito.when(Parameters.getParameter(anyString())).thenReturn(parameters);
406 private InternalServlet internalServerSuccess() {
407 InternalServlet internalServlet = new InternalServlet() {
409 protected boolean doUpdate(Updateable bean) {
413 protected boolean doDelete(Deleteable bean) {
417 protected boolean doInsert(Insertable bean) {
421 return internalServlet;
424 private InternalServlet internalServerFailure() {
425 InternalServlet internalServlet = new InternalServlet() {
427 protected boolean doUpdate(Updateable bean) {
431 protected boolean doDelete(Deleteable bean) {
435 protected boolean doInsert(Insertable bean) {
439 return internalServlet;
442 private void mockProvisioningParametersChanged() throws IllegalAccessException {
443 PowerMockito.mockStatic(Feed.class);
444 PowerMockito.mockStatic(Subscription.class);
445 PowerMockito.when(Feed.countActiveFeeds()).thenReturn(0);
446 PowerMockito.when(Subscription.countActiveSubscriptions()).thenReturn(0);
447 Map<String, Integer> map = new HashMap<>();
448 FieldUtils.writeDeclaredStaticField(NodeClass.class, "map", map, true);