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.jetbrains.annotations.NotNull;
27 import org.json.JSONObject;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.onap.dmaap.datarouter.authz.AuthorizationResponse;
33 import org.onap.dmaap.datarouter.authz.Authorizer;
34 import org.onap.dmaap.datarouter.provisioning.beans.Feed;
35 import org.onap.dmaap.datarouter.provisioning.beans.Insertable;
36 import org.onap.dmaap.datarouter.provisioning.beans.Subscription;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
39 import org.powermock.modules.junit4.PowerMockRunner;
41 import javax.servlet.ServletOutputStream;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44 import java.util.ArrayList;
45 import java.util.HashSet;
46 import java.util.List;
49 import static org.hamcrest.Matchers.notNullValue;
50 import static org.mockito.Mockito.*;
51 import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER;
54 @RunWith(PowerMockRunner.class)
55 @SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Feed", "org.onap.dmaap.datarouter.provisioning.beans.Subscription"})
56 public class SubscribeServletTest extends DrServletTestBase {
57 private static SubscribeServlet subscribeServlet;
60 private HttpServletRequest request;
62 private HttpServletResponse response;
65 public void setUp() throws Exception {
67 subscribeServlet = new SubscribeServlet();
68 setAuthoriserToReturnRequestIsAuthorized();
69 setPokerToNotCreateTimersWhenDeleteFeedIsCalled();
70 setupValidAuthorisedRequest();
71 setUpValidSecurityOnHttpRequest();
72 setUpValidContentHeadersAndJSONOnHttpRequest();
76 public void Given_Request_Is_HTTP_DELETE_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception {
77 subscribeServlet.doDelete(request, response);
78 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
82 public void Given_Request_Is_HTTP_GET_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception {
83 when(request.isSecure()).thenReturn(false);
84 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true);
85 subscribeServlet.doGet(request, response);
86 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
90 public void Given_Request_Is_HTTP_GET_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception {
91 setBehalfHeader(null);
92 subscribeServlet.doGet(request, response);
93 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
98 public void Given_Request_Is_HTTP_GET_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated() throws Exception {
99 when(request.getPathInfo()).thenReturn(null);
100 subscribeServlet.doGet(request, response);
101 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
105 public void Given_Request_Is_HTTP_GET_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception {
106 setFeedToReturnInvalidFeedIdSupplied();
107 subscribeServlet.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() throws Exception {
114 setAuthoriserToReturnRequestNotAuthorized();
115 subscribeServlet.doGet(request, response);
116 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
121 public void Given_Request_Is_HTTP_GET_And_Request_Succeeds() throws Exception {
122 ServletOutputStream outStream = mock(ServletOutputStream.class);
123 when(response.getOutputStream()).thenReturn(outStream);
124 PowerMockito.mockStatic(Subscription.class);
125 List<String> list = new ArrayList<String>();
127 PowerMockito.when(Subscription.getSubscriptionUrlList(anyInt())).thenReturn(list);
128 subscribeServlet.doGet(request, response);
129 verify(response).setStatus(eq(HttpServletResponse.SC_OK));
134 public void Given_Request_Is_HTTP_PUT_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception {
135 subscribeServlet.doPut(request, response);
136 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
139 public void Given_Request_Is_HTTP_POST_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception {
140 when(request.isSecure()).thenReturn(false);
141 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true);
142 subscribeServlet.doPost(request, response);
143 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
147 public void Given_Request_Is_HTTP_POST_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception {
148 setBehalfHeader(null);
149 subscribeServlet.doPost(request, response);
150 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
155 public void Given_Request_Is_HTTP_POST_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated() throws Exception {
156 when(request.getPathInfo()).thenReturn(null);
157 subscribeServlet.doPost(request, response);
158 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
163 public void Given_Request_Is_HTTP_POST_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception {
164 setFeedToReturnInvalidFeedIdSupplied();
165 subscribeServlet.doPost(request, response);
166 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
170 public void Given_Request_Is_HTTP_POST_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception {
171 setAuthoriserToReturnRequestNotAuthorized();
172 subscribeServlet.doPost(request, response);
173 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
177 public void Given_Request_Is_HTTP_POST_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() throws Exception {
178 when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.feed; version=1.1");
179 when(request.getContentType()).thenReturn("stub_contentType");
180 subscribeServlet.doPost(request, response);
181 verify(response).sendError(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE), argThat(notNullValue(String.class)));
185 public void Given_Request_Is_HTTP_POST_And_Request_Contains_Badly_Formed_JSON_Then_Bad_Request_Response_Is_Generated() throws Exception {
186 subscribeServlet.doPost(request, response);
187 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
191 public void Given_Request_Is_HTTP_POST_And_Active_Feeds_Equals_Max_Feeds_Then_Bad_Request_Response_Is_Generated() throws Exception {
192 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "maxSubs", 0, true);
193 SubscribeServlet subscribeServlet = new SubscribeServlet() {
194 protected JSONObject getJSONfromInput(HttpServletRequest req) {
195 return new JSONObject();
198 subscribeServlet.doPost(request, response);
199 verify(response).sendError(eq(HttpServletResponse.SC_CONFLICT), argThat(notNullValue(String.class)));
203 public void Given_Request_Is_HTTP_POST_And_POST_Fails_Bad_Request_Response_Is_Generated() throws Exception {
204 PowerMockito.mockStatic(Subscription.class);
205 PowerMockito.when(Subscription.getSubscriptionMatching(mock(Subscription.class))).thenReturn(null);
206 PowerMockito.when(Subscription.countActiveSubscriptions()).thenReturn(0);
207 JSONObject JSObject = buildRequestJsonObject();
208 SubscribeServlet subscribeServlet = new SubscribeServlet() {
209 protected JSONObject getJSONfromInput(HttpServletRequest req) {
210 JSONObject jo = new JSONObject();
211 jo.put("name", "stub_name");
212 jo.put("version", "2.0");
213 jo.put("metadataOnly", true);
214 jo.put("suspend", true);
215 jo.put("delivery", JSObject);
216 jo.put("sync", false);
221 protected boolean doInsert(Insertable bean) {
225 subscribeServlet.doPost(request, response);
226 verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));
231 public void Given_Request_Is_HTTP_POST_And_Change_On_Feeds_Succeeds_A_STATUS_OK_Response_Is_Generated() throws Exception {
232 ServletOutputStream outStream = mock(ServletOutputStream.class);
233 when(response.getOutputStream()).thenReturn(outStream);
234 PowerMockito.mockStatic(Subscription.class);
235 PowerMockito.when(Subscription.getSubscriptionMatching(mock(Subscription.class))).thenReturn(null);
236 JSONObject JSObject = buildRequestJsonObject();
237 SubscribeServlet subscribeServlet = new SubscribeServlet() {
238 protected JSONObject getJSONfromInput(HttpServletRequest req) {
239 JSONObject jo = new JSONObject();
240 jo.put("name", "stub_name");
241 jo.put("version", "2.0");
242 jo.put("metadataOnly", true);
243 jo.put("suspend", true);
244 jo.put("delivery", JSObject);
245 jo.put("sync", true);
250 protected boolean doInsert(Insertable bean) {
254 subscribeServlet.doPost(request, response);
255 verify(response).setStatus(eq(HttpServletResponse.SC_CREATED));
260 private JSONObject buildRequestJsonObject() {
261 JSONObject JSObject = new JSONObject();
262 JSObject.put("url", "https://stub_address");
263 JSObject.put("use100", "true");
264 JSObject.put("password", "stub_password");
265 JSObject.put("user", "stub_user");
269 private void setUpValidSecurityOnHttpRequest() throws Exception {
270 when(request.isSecure()).thenReturn(true);
271 Set<String> authAddressesAndNetworks = new HashSet<String>();
272 authAddressesAndNetworks.add(("127.0.0.1"));
273 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, true);
274 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true);
275 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "maxSubs", 100, true);
278 private void setBehalfHeader(String headerValue) {
279 when(request.getHeader(BEHALF_HEADER)).thenReturn(headerValue);
282 private void setValidPathInfoInHttpHeader() {
283 when(request.getPathInfo()).thenReturn("/123");
286 private void setFeedToReturnInvalidFeedIdSupplied() {
287 PowerMockito.mockStatic(Feed.class);
288 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(null);
291 private void setFeedToReturnValidFeedForSuppliedId() {
292 PowerMockito.mockStatic(Feed.class);
293 Feed feed = mock(Feed.class);
294 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed);
295 when(feed.isDeleted()).thenReturn(false);
296 when(feed.asJSONObject(true)).thenReturn(mock(JSONObject.class));
297 when(feed.getPublisher()).thenReturn("Stub_Value");
298 when(feed.getName()).thenReturn("stub_name");
299 when(feed.getVersion()).thenReturn("1.0");
300 when(feed.asLimitedJSONObject()).thenReturn(mock(JSONObject.class));
303 private void setAuthoriserToReturnRequestNotAuthorized() throws IllegalAccessException {
304 AuthorizationResponse authResponse = mock(AuthorizationResponse.class);
305 Authorizer authorizer = mock(Authorizer.class);
306 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true);
307 when(authorizer.decide(request)).thenReturn(authResponse);
308 when(authResponse.isAuthorized()).thenReturn(false);
311 private void setAuthoriserToReturnRequestIsAuthorized() throws IllegalAccessException {
312 AuthorizationResponse authResponse = mock(AuthorizationResponse.class);
313 Authorizer authorizer = mock(Authorizer.class);
314 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true);
315 when(authorizer.decide(request)).thenReturn(authResponse);
316 when(authResponse.isAuthorized()).thenReturn(true);
319 private void setPokerToNotCreateTimersWhenDeleteFeedIsCalled() throws Exception {
320 Poker poker = mock(Poker.class);
321 FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true);
324 private void setupValidAuthorisedRequest() throws Exception {
325 setUpValidSecurityOnHttpRequest();
326 setBehalfHeader("Stub_Value");
327 setValidPathInfoInHttpHeader();
328 setFeedToReturnValidFeedForSuppliedId();
331 private void setUpValidContentHeadersAndJSONOnHttpRequest() {
332 when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription; version=1.0");
333 when(request.getHeader("X-ATT-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");