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 org.apache.commons.lang3.reflect.FieldUtils;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mock;
30 import org.onap.dmaap.datarouter.authz.AuthorizationResponse;
31 import org.onap.dmaap.datarouter.authz.Authorizer;
32 import org.onap.dmaap.datarouter.provisioning.beans.*;
33 import org.onap.dmaap.datarouter.provisioning.utils.LogfileLoader;
34 import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet;
35 import org.powermock.api.mockito.PowerMockito;
36 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
37 import org.powermock.modules.junit4.PowerMockRunner;
39 import javax.servlet.ServletInputStream;
40 import javax.servlet.ServletOutputStream;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
44 import java.net.InetAddress;
45 import java.util.HashMap;
48 import static org.hamcrest.Matchers.notNullValue;
49 import static org.mockito.Matchers.*;
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.verify;
52 import static org.mockito.Mockito.when;
53 import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER;
55 @RunWith(PowerMockRunner.class)
56 @SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Feed", "org.onap.dmaap.datarouter.provisioning.beans.Parameters", "org.onap.dmaap.datarouter.provisioning.beans.NodeClass",
57 "org.onap.dmaap.datarouter.provisioning.beans.Subscription", "org.onap.dmaap.datarouter.provisioning.utils.LogfileLoader"})
58 public class InternalServletTest extends DrServletTestBase {
59 private InternalServlet internalServlet;
62 private HttpServletRequest request;
65 private HttpServletResponse response;
68 public void setUp() throws Exception {
70 internalServlet = new InternalServlet();
71 setAuthoriserToReturnRequestIsAuthorized();
72 setUpValidAuthorisedRequest();
76 public void Given_Request_Is_HTTP_GET_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception {
77 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
78 internalServlet.doGet(request, response);
79 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
83 public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_But_Not_Sent_From_Localhost_Then_Forbidden_Response_Is_Generated() throws Exception {
84 when(request.getPathInfo()).thenReturn("/halt");
85 when(request.isSecure()).thenReturn(false);
86 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
87 internalServlet.doGet(request, response);
88 verify(response).setStatus(eq(HttpServletResponse.SC_FORBIDDEN));
92 public void Given_Request_Is_HTTP_GET_With_Halt_In_Endpoint_Request_Succeeds() throws Exception {
93 when(request.getPathInfo()).thenReturn("/halt");
94 when(request.isSecure()).thenReturn(false);
95 when(request.getRemoteAddr()).thenReturn("127.0.0.1");
96 internalServlet.doGet(request, response);
97 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
101 public void Given_Request_Is_HTTP_GET_With_FetchProv_In_Endpoint_Request_Succeeds() throws Exception {
102 when(request.getPathInfo()).thenReturn("/fetchProv");
103 when(request.isSecure()).thenReturn(false);
104 internalServlet.doGet(request, response);
105 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
109 public void Given_Request_Is_HTTP_GET_With_Prov_In_Endpoint_Request_Succeeds() throws Exception {
110 when(request.getPathInfo()).thenReturn("/prov");
111 when(request.getQueryString()).thenReturn(null);
112 setPokerToNotCreateTimers();
113 ServletOutputStream outStream = mock(ServletOutputStream.class);
114 when(response.getOutputStream()).thenReturn(outStream);
115 internalServlet.doGet(request, response);
116 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
120 public void Given_Request_Is_HTTP_GET_With_Logs_In_Endpoint_Request_Succeeds() throws Exception {
121 when(request.getPathInfo()).thenReturn("/logs/");
122 ServletOutputStream outStream = mock(ServletOutputStream.class);
123 when(response.getOutputStream()).thenReturn(outStream);
124 internalServlet.doGet(request, response);
125 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
129 public void Given_Request_Is_HTTP_GET_Starts_With_Logs_In_Endpoint_Request_Succeeds() throws Exception {
130 when(request.getPathInfo()).thenReturn("/logs/TestFile");
131 internalServlet.doGet(request, response);
132 verify(response).sendError(eq(HttpServletResponse.SC_NO_CONTENT), argThat(notNullValue(String.class)));
136 public void Given_Request_Is_HTTP_GET_With_Api_In_Endpoint_Request_Succeeds() throws Exception {
137 when(request.getPathInfo()).thenReturn("/api/Key");
138 setParametersToNotContactDb(false);
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_With_Drlogs_In_Endpoint_Request_Succeeds() throws Exception {
147 when(request.getPathInfo()).thenReturn("/drlogs/");
148 PowerMockito.mockStatic(LogfileLoader.class);
149 LogfileLoader logfileLoader = mock(LogfileLoader.class);
150 when(logfileLoader.getBitSet()).thenReturn(new RLEBitSet());
151 PowerMockito.when(LogfileLoader.getLoader()).thenReturn(logfileLoader);
152 ServletOutputStream outStream = mock(ServletOutputStream.class);
153 when(response.getOutputStream()).thenReturn(outStream);
154 internalServlet.doGet(request, response);
155 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
159 public void Given_Request_Is_HTTP_GET_Incorrect_Endpoint_Then_No_Content_Response_Is_Generated() throws Exception {
160 when(request.getPathInfo()).thenReturn("/incorrect/");
161 internalServlet.doGet(request, response);
162 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
166 public void Given_Request_Is_HTTP_PUT_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception {
167 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
168 internalServlet.doPut(request, response);
169 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
173 public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_Request_Succeeds() throws Exception {
174 when(request.getPathInfo()).thenReturn("/api/Key");
175 setParametersToNotContactDb(false);
176 String[] values = {"V", "a", "l", "u", "e", "s"};
177 when(request.getParameterValues(anyString())).thenReturn(values);
178 internalServlet = internalServerSuccess();
179 setPokerToNotCreateTimers();
180 mockProvisioningParametersChanged();
181 internalServlet.doPut(request, response);
182 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
186 public void Given_Request_Is_HTTP_PUT_With_Api_In_Endpoint_And_Update_Fails_Then_Internal_Server_Error_Is_Generated() throws Exception {
187 when(request.getPathInfo()).thenReturn("/api/Key");
188 setParametersToNotContactDb(false);
189 String[] values = {"V", "a", "l", "u", "e", "s"};
190 when(request.getParameterValues(anyString())).thenReturn(values);
191 internalServlet = internalServerFailure();
192 internalServlet.doPut(request, response);
193 verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));
197 public void Given_Request_Is_HTTP_PUT_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated() throws Exception {
198 when(request.getPathInfo()).thenReturn("/incorrect");
199 internalServlet.doPut(request, response);
200 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
204 public void Given_Request_Is_HTTP_DELETE_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception {
205 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
206 internalServlet.doDelete(request, response);
207 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
211 public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_Request_Succeeds() throws Exception {
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 = internalServerSuccess();
217 setPokerToNotCreateTimers();
218 mockProvisioningParametersChanged();
219 internalServlet.doDelete(request, response);
220 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
224 public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_And_Delete_Fails_Then_Internal_Server_Error_Is_Generated() throws Exception {
225 when(request.getPathInfo()).thenReturn("/api/Key");
226 setParametersToNotContactDb(false);
227 String[] values = {"V", "a", "l", "u", "e", "s"};
228 when(request.getParameterValues(anyString())).thenReturn(values);
229 internalServlet = internalServerFailure();
230 internalServlet.doDelete(request, response);
231 verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));
235 public void Given_Request_Is_HTTP_DELETE_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated() throws Exception {
236 when(request.getPathInfo()).thenReturn("/incorrect");
237 internalServlet.doDelete(request, response);
238 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
242 public void Given_Request_Is_HTTP_POST_And_Address_Not_Authorized_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception {
243 when(request.getRemoteAddr()).thenReturn("127.100.0.3");
244 internalServlet.doPost(request, response);
245 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
249 public void Given_Request_Is_HTTP_POST_With_Api_In_Endpoint_Request_Succeeds() throws Exception {
250 when(request.getPathInfo()).thenReturn("/api/Key");
251 setParametersToNotContactDb(true);
252 String[] values = {"V", "a", "l", "u", "e", "s"};
253 when(request.getParameterValues(anyString())).thenReturn(values);
254 internalServlet = internalServerSuccess();
255 setPokerToNotCreateTimers();
256 mockProvisioningParametersChanged();
257 internalServlet.doPost(request, response);
258 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
262 public void Given_Request_Is_HTTP_POST_With_Api_In_Endpoint_And_Insert_Fails_Then_Internal_Server_Error_Is_Generated() throws Exception {
263 when(request.getPathInfo()).thenReturn("/api/Key");
264 setParametersToNotContactDb(true);
265 String[] values = {"V", "a", "l", "u", "e", "s"};
266 when(request.getParameterValues(anyString())).thenReturn(values);
267 internalServlet = internalServerFailure();
268 internalServlet.doPost(request, response);
269 verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));
273 public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() throws Exception {
274 when(request.getHeader("Content-Type")).thenReturn("stub_contentType");
275 when(request.getPathInfo()).thenReturn("/logs/");
276 internalServlet.doPost(request, response);
277 verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE));
281 public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Encoding_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() throws Exception {
282 when(request.getHeader("Content-Encoding")).thenReturn("not-supported");
283 when(request.getPathInfo()).thenReturn("/logs/");
284 internalServlet.doPost(request, response);
285 verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE));
289 public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Then_Unsupported_Media_Type_Response_Is_Generated() throws Exception {
290 when(request.getHeader("Content-Type")).thenReturn("stub_contentType");
291 when(request.getPathInfo()).thenReturn("/drlogs/");
292 internalServlet.doPost(request, response);
293 verify(response).setStatus(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE));
297 public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Request_Succeeds() throws Exception {
298 when(request.getPathInfo()).thenReturn("/drlogs/");
299 ServletInputStream inStream = mock(ServletInputStream.class);
300 when(inStream.read()).thenReturn(1, -1);
301 when(request.getInputStream()).thenReturn(inStream);
302 internalServlet.doPost(request, response);
303 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
307 public void Given_Request_Is_HTTP_POST_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated() throws Exception {
308 when(request.getPathInfo()).thenReturn("/incorrect/");
309 internalServlet.doPost(request, response);
310 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
313 private void setAuthoriserToReturnRequestIsAuthorized() throws IllegalAccessException {
314 AuthorizationResponse authResponse = mock(AuthorizationResponse.class);
315 Authorizer authorizer = mock(Authorizer.class);
316 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true);
317 when(authorizer.decide(request)).thenReturn(authResponse);
318 when(authResponse.isAuthorized()).thenReturn(true);
321 private void setUpValidAuthorisedRequest() throws Exception {
322 setUpValidSecurityOnHttpRequest();
323 setBehalfHeader("Stub_Value");
324 setValidPathInfoInHttpHeader();
325 when(request.getHeader("Content-Type")).thenReturn("text/plain");
326 when(request.getHeader("Content-Encoding")).thenReturn("gzip");
329 private void setUpValidSecurityOnHttpRequest() throws Exception {
330 when(request.isSecure()).thenReturn(true);
331 when(request.getRemoteAddr()).thenReturn(InetAddress.getLocalHost().getHostAddress());
332 InetAddress[] nodeAddresses = new InetAddress[1];
333 nodeAddresses[0] = InetAddress.getLocalHost();
334 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "nodeAddresses", nodeAddresses, true);
335 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true);
338 private void setBehalfHeader(String headerValue) {
339 when(request.getHeader(BEHALF_HEADER)).thenReturn(headerValue);
342 private void setValidPathInfoInHttpHeader() {
343 when(request.getPathInfo()).thenReturn("/123");
346 private void setPokerToNotCreateTimers() throws Exception {
347 Poker poker = mock(Poker.class);
348 FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true);
351 private void setParametersToNotContactDb(boolean isPost) {
352 PowerMockito.mockStatic(Parameters.class);
353 Parameters parameters = mock(Parameters.class);
355 PowerMockito.when(Parameters.getParameter(anyString())).thenReturn(null);
357 PowerMockito.when(Parameters.getParameter(anyString())).thenReturn(parameters);
361 private InternalServlet internalServerSuccess() {
362 InternalServlet internalServlet = new InternalServlet() {
364 protected boolean doUpdate(Updateable bean) {
368 protected boolean doDelete(Deleteable bean) {
372 protected boolean doInsert(Insertable bean) {
376 return internalServlet;
379 private InternalServlet internalServerFailure() {
380 InternalServlet internalServlet = new InternalServlet() {
382 protected boolean doUpdate(Updateable bean) {
386 protected boolean doDelete(Deleteable bean) {
390 protected boolean doInsert(Insertable bean) {
394 return internalServlet;
397 private void mockProvisioningParametersChanged() throws IllegalAccessException{
398 PowerMockito.mockStatic(Feed.class);
399 PowerMockito.mockStatic(Subscription.class);
400 PowerMockito.when(Feed.countActiveFeeds()).thenReturn(0);
401 PowerMockito.when(Subscription.countActiveSubscriptions()).thenReturn(0);
402 Map<String, Integer> map = new HashMap<>();
403 FieldUtils.writeDeclaredStaticField(NodeClass.class, "map", map, true);