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.Mockito.anyInt;
27 import static org.mockito.Mockito.anyString;
28 import static org.mockito.Mockito.argThat;
29 import static org.mockito.Mockito.eq;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33 import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER;
35 import java.util.HashSet;
37 import javax.servlet.ServletOutputStream;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40 import org.apache.commons.lang3.reflect.FieldUtils;
41 import org.jetbrains.annotations.NotNull;
42 import org.json.JSONArray;
43 import org.json.JSONObject;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mock;
48 import org.onap.dmaap.datarouter.authz.AuthorizationResponse;
49 import org.onap.dmaap.datarouter.authz.Authorizer;
50 import org.onap.dmaap.datarouter.provisioning.beans.Feed;
51 import org.onap.dmaap.datarouter.provisioning.beans.Insertable;
52 import org.powermock.api.mockito.PowerMockito;
53 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
54 import org.powermock.modules.junit4.PowerMockRunner;
57 @RunWith(PowerMockRunner.class)
58 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Feed")
59 public class DRFeedsServletTest extends DrServletTestBase {
61 private static DRFeedsServlet drfeedsServlet;
64 private HttpServletRequest request;
66 private HttpServletResponse response;
69 public void setUp() throws Exception {
71 drfeedsServlet = new DRFeedsServlet();
72 setAuthoriserToReturnRequestIsAuthorized();
73 setPokerToNotCreateTimersWhenDeleteFeedIsCalled();
74 setupValidAuthorisedRequest();
75 setUpValidSecurityOnHttpRequest();
76 setUpValidContentHeadersAndJSONOnHttpRequest();
80 public void Given_Request_Is_HTTP_DELETE_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception {
81 drfeedsServlet.doDelete(request, response);
82 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
86 public void Given_Request_Is_HTTP_GET_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
88 when(request.isSecure()).thenReturn(false);
89 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true);
90 drfeedsServlet.doGet(request, response);
91 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
95 public void Given_Request_Is_HTTP_GET_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated()
97 setBehalfHeader(null);
98 drfeedsServlet.doGet(request, response);
99 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
104 public void Given_Request_Is_HTTP_GET_And_URL_Path_Not_Valid_Then_Bad_Request_Response_Is_Generated()
106 when(request.getRequestURI()).thenReturn("/123");
107 drfeedsServlet.doGet(request, response);
108 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
113 public void Given_Request_Is_HTTP_GET_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated()
115 setAuthoriserToReturnRequestNotAuthorized();
116 drfeedsServlet.doGet(request, response);
117 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
121 public void Given_Request_Is_HTTP_GET_And_Request_Fails_With_Valid_Name_And_Version() throws Exception {
122 when(request.getParameter("name")).thenReturn("stub_name");
123 when(request.getParameter("version")).thenReturn("stub_version");
124 drfeedsServlet.doGet(request, response);
125 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
129 public void Given_Request_Is_HTTP_GET_And_Request_Succeeds_With_Valid_Name_And_Version() throws Exception {
130 ServletOutputStream outStream = mock(ServletOutputStream.class);
131 when(response.getOutputStream()).thenReturn(outStream);
132 when(request.getParameter("name")).thenReturn("stub_name");
133 when(request.getParameter("version")).thenReturn("stub_version");
134 PowerMockito.mockStatic(Feed.class);
135 Feed feed = mock(Feed.class);
136 PowerMockito.when(Feed.getFeedByNameVersion(anyString(), anyString())).thenReturn(feed);
137 when(feed.asJSONObject(true)).thenReturn(mock(JSONObject.class));
138 drfeedsServlet.doGet(request, response);
139 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
144 public void Given_Request_Is_HTTP_GET_And_Request_Succeeds_With_Invalid_Name_And_Version() throws Exception {
145 ServletOutputStream outStream = mock(ServletOutputStream.class);
146 when(response.getOutputStream()).thenReturn(outStream);
147 drfeedsServlet.doGet(request, response);
148 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
153 public void Given_Request_Is_HTTP_PUT_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception {
154 drfeedsServlet.doPut(request, response);
155 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
160 public void Given_Request_Is_HTTP_POST_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated()
162 when(request.isSecure()).thenReturn(false);
163 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true);
164 drfeedsServlet.doPost(request, response);
165 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
169 public void Given_Request_Is_HTTP_POST_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated()
171 setBehalfHeader(null);
172 drfeedsServlet.doPost(request, response);
173 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
178 public void Given_Request_Is_HTTP_POST_And_URL_Path_Not_Valid_Then_Bad_Request_Response_Is_Generated()
180 when(request.getRequestURI()).thenReturn("/123");
181 drfeedsServlet.doPost(request, response);
182 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
187 public void Given_Request_Is_HTTP_POST_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated()
189 when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.feed; version=1.1");
190 when(request.getContentType()).thenReturn("stub_contentType");
191 drfeedsServlet.doPost(request, response);
193 .sendError(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE), argThat(notNullValue(String.class)));
197 public void Given_Request_Is_HTTP_POST_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated()
199 setAuthoriserToReturnRequestNotAuthorized();
200 drfeedsServlet.doPost(request, response);
201 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
205 public void Given_Request_Is_HTTP_POST_And_Request_Contains_Badly_Formed_JSON_Then_Bad_Request_Response_Is_Generated()
207 drfeedsServlet.doPost(request, response);
208 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
212 public void Given_Request_Is_HTTP_POST_And_Active_Feeds_Equals_Max_Feeds_Then_Bad_Request_Response_Is_Generated()
214 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "maxFeeds", 0, true);
215 DRFeedsServlet drfeedsServlet = new DRFeedsServlet() {
216 protected JSONObject getJSONfromInput(HttpServletRequest req) {
217 return new JSONObject();
220 drfeedsServlet.doPost(request, response);
221 verify(response).sendError(eq(HttpServletResponse.SC_CONFLICT), argThat(notNullValue(String.class)));
225 public void Given_Request_Is_HTTP_POST_And_Feed_Is_Not_Valid_Object_Bad_Request_Response_Is_Generated()
227 when(request.getHeader("X-ATT-DR-ON-BEHALF-OF-GROUP")).thenReturn(null);
228 JSONObject JSObject = buildRequestJsonObject();
230 DRFeedsServlet drfeedsServlet = new DRFeedsServlet() {
231 protected JSONObject getJSONfromInput(HttpServletRequest req) {
232 JSONObject jo = new JSONObject();
237 drfeedsServlet.doPost(request, response);
238 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
242 public void Given_Request_Is_HTTP_POST_And_Feed_Already_Exists_Bad_Request_Response_Is_Generated()
244 setFeedToReturnInvalidFeedIdSupplied();
245 JSONObject JSObject = buildRequestJsonObject();
246 DRFeedsServlet drfeedsServlet = new DRFeedsServlet() {
247 protected JSONObject getJSONfromInput(HttpServletRequest req) {
248 JSONObject jo = new JSONObject();
249 jo.put("name", "not_stub_name");
250 jo.put("version", "1.0");
251 jo.put("authorization", JSObject);
255 drfeedsServlet.doPost(request, response);
256 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
260 public void Given_Request_Is_HTTP_POST_And_POST_Fails_Bad_Request_Response_Is_Generated() throws Exception {
261 JSONObject JSObject = buildRequestJsonObject();
262 DRFeedsServlet drfeedsServlet = new DRFeedsServlet() {
263 protected JSONObject getJSONfromInput(HttpServletRequest req) {
264 JSONObject jo = new JSONObject();
265 jo.put("name", "stub_name");
266 jo.put("version", "2.0");
267 jo.put("authorization", JSObject);
272 protected boolean doInsert(Insertable bean) {
276 drfeedsServlet.doPost(request, response);
278 .sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));
283 public void Given_Request_Is_HTTP_POST_And_Change_On_Feeds_Succeeds_A_STATUS_OK_Response_Is_Generated()
285 ServletOutputStream outStream = mock(ServletOutputStream.class);
286 when(response.getOutputStream()).thenReturn(outStream);
287 JSONObject JSObject = buildRequestJsonObject();
288 DRFeedsServlet drfeedsServlet = new DRFeedsServlet() {
289 protected JSONObject getJSONfromInput(HttpServletRequest req) {
290 JSONObject jo = new JSONObject();
291 jo.put("name", "stub_name");
292 jo.put("version", "1.0");
293 jo.put("authorization", JSObject);
298 protected boolean doInsert(Insertable bean) {
302 drfeedsServlet.doPost(request, response);
303 verify(response).setStatus(eq(HttpServletResponse.SC_CREATED));
307 private JSONObject buildRequestJsonObject() {
308 JSONObject JSObject = new JSONObject();
309 JSONArray endpointIDs = new JSONArray();
310 JSONObject JOEndpointIDs = new JSONObject();
311 JOEndpointIDs.put("id", "stub_endpoint_id");
312 JOEndpointIDs.put("password", "stub_endpoint_password");
313 endpointIDs.put(JOEndpointIDs);
315 JSONArray endpointAddresses = new JSONArray();
316 endpointAddresses.put("127.0.0.1");
318 JSObject.put("classification", "stub_classification");
319 JSObject.put("endpoint_ids", endpointIDs);
320 JSObject.put("endpoint_addrs", endpointAddresses);
324 private void setUpValidSecurityOnHttpRequest() throws Exception {
325 when(request.isSecure()).thenReturn(true);
326 Set<String> authAddressesAndNetworks = new HashSet<String>();
327 authAddressesAndNetworks.add(("127.0.0.1"));
329 .writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks,
331 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true);
332 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "maxFeeds", 100, true);
335 private void setBehalfHeader(String headerValue) {
336 when(request.getHeader(BEHALF_HEADER)).thenReturn(headerValue);
339 private void setValidPathInfoInHttpHeader() {
340 when(request.getPathInfo()).thenReturn("/123");
343 private void setFeedToReturnInvalidFeedIdSupplied() {
344 PowerMockito.mockStatic(Feed.class);
345 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(null);
346 when(Feed.getFeedByNameVersion(anyString(), anyString())).thenReturn(mock(Feed.class));
349 private void setFeedToReturnValidFeedForSuppliedId() {
350 PowerMockito.mockStatic(Feed.class);
351 Feed feed = mock(Feed.class);
352 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed);
353 when(feed.isDeleted()).thenReturn(false);
354 when(feed.asJSONObject(true)).thenReturn(mock(JSONObject.class));
355 when(feed.getPublisher()).thenReturn("Stub_Value");
356 when(feed.getName()).thenReturn("stub_name");
357 when(feed.getVersion()).thenReturn("1.0");
358 when(feed.asLimitedJSONObject()).thenReturn(mock(JSONObject.class));
359 PowerMockito.when(feed.getFeedByNameVersion(anyString(), anyString())).thenReturn(null);
362 private void setAuthoriserToReturnRequestNotAuthorized() throws IllegalAccessException {
363 AuthorizationResponse authResponse = mock(AuthorizationResponse.class);
364 Authorizer authorizer = mock(Authorizer.class);
365 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true);
366 when(authorizer.decide(request)).thenReturn(authResponse);
367 when(authResponse.isAuthorized()).thenReturn(false);
370 private void setAuthoriserToReturnRequestIsAuthorized() throws IllegalAccessException {
371 AuthorizationResponse authResponse = mock(AuthorizationResponse.class);
372 Authorizer authorizer = mock(Authorizer.class);
373 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true);
374 when(authorizer.decide(request)).thenReturn(authResponse);
375 when(authResponse.isAuthorized()).thenReturn(true);
378 private void setPokerToNotCreateTimersWhenDeleteFeedIsCalled() throws Exception {
379 Poker poker = mock(Poker.class);
380 FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true);
383 private void setupValidAuthorisedRequest() throws Exception {
384 setUpValidSecurityOnHttpRequest();
385 setBehalfHeader("Stub_Value");
386 setValidPathInfoInHttpHeader();
387 setFeedToReturnValidFeedForSuppliedId();
390 private void setUpValidContentHeadersAndJSONOnHttpRequest() {
391 when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.feed; version=1.0");
392 when(request.getHeader("X-ATT-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");