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.JSONArray;
28 import org.json.JSONObject;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.onap.dmaap.datarouter.authz.AuthorizationResponse;
34 import org.onap.dmaap.datarouter.authz.Authorizer;
35 import org.onap.dmaap.datarouter.provisioning.beans.Feed;
36 import org.onap.dmaap.datarouter.provisioning.beans.Insertable;
37 import org.onap.dmaap.datarouter.provisioning.beans.Subscription;
38 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
40 import org.powermock.modules.junit4.PowerMockRunner;
42 import javax.servlet.ServletOutputStream;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45 import java.util.ArrayList;
46 import java.util.HashSet;
47 import java.util.List;
50 import static org.hamcrest.Matchers.notNullValue;
51 import static org.mockito.Mockito.*;
52 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.Subscription"})
57 public class SubscribeServletTest extends DrServletTestBase{
58 private static SubscribeServlet subscribeServlet;
61 private HttpServletRequest request;
63 private HttpServletResponse response;
66 public void setUp() throws Exception {
68 subscribeServlet = new SubscribeServlet();
69 setAuthoriserToReturnRequestIsAuthorized();
70 setPokerToNotCreateTimersWhenDeleteFeedIsCalled();
71 setupValidAuthorisedRequest();
72 setUpValidSecurityOnHttpRequest();
73 setUpValidContentHeadersAndJSONOnHttpRequest();
77 public void Given_Request_Is_HTTP_DELETE_SC_METHOD_NOT_ALLOWED_Response_Is_Generated() throws Exception {
78 subscribeServlet.doDelete(request, response);
79 verify(response).sendError(eq(HttpServletResponse.SC_METHOD_NOT_ALLOWED), argThat(notNullValue(String.class)));
83 public void Given_Request_Is_HTTP_GET_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception {
84 when(request.isSecure()).thenReturn(false);
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 subscribeServlet.doPost(request, response);
142 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
146 public void Given_Request_Is_HTTP_POST_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception {
147 setBehalfHeader(null);
148 subscribeServlet.doPost(request, response);
149 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
154 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 {
155 when(request.getPathInfo()).thenReturn(null);
156 subscribeServlet.doPost(request, response);
157 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
162 public void Given_Request_Is_HTTP_POST_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception {
163 setFeedToReturnInvalidFeedIdSupplied();
164 subscribeServlet.doPost(request, response);
165 verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
169 public void Given_Request_Is_HTTP_POST_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception {
170 setAuthoriserToReturnRequestNotAuthorized();
171 subscribeServlet.doPost(request, response);
172 verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
176 public void Given_Request_Is_HTTP_POST_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() throws Exception {
177 when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.feed; version=1.1");
178 when(request.getContentType()).thenReturn("stub_contentType");
179 subscribeServlet.doPost(request, response);
180 verify(response).sendError(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE), argThat(notNullValue(String.class)));
184 public void Given_Request_Is_HTTP_POST_And_Request_Contains_Badly_Formed_JSON_Then_Bad_Request_Response_Is_Generated() throws Exception {
185 subscribeServlet.doPost(request, response);
186 verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
190 public void Given_Request_Is_HTTP_POST_And_Active_Feeds_Equals_Max_Feeds_Then_Bad_Request_Response_Is_Generated() throws Exception {
191 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "maxSubs", 0, true);
192 SubscribeServlet subscribeServlet = new SubscribeServlet() {
193 protected JSONObject getJSONfromInput(HttpServletRequest req) {
194 return new JSONObject();
197 subscribeServlet.doPost(request, response);
198 verify(response).sendError(eq(HttpServletResponse.SC_CONFLICT), argThat(notNullValue(String.class)));
202 public void Given_Request_Is_HTTP_POST_And_POST_Fails_Bad_Request_Response_Is_Generated() throws Exception {
203 PowerMockito.mockStatic(Subscription.class);
204 PowerMockito.when(Subscription.getSubscriptionMatching(mock(Subscription.class))).thenReturn(null);
205 JSONObject JSObject = buildRequestJsonObject();
206 SubscribeServlet subscribeServlet = new SubscribeServlet() {
207 protected JSONObject getJSONfromInput(HttpServletRequest req) {
208 JSONObject jo = new JSONObject();
209 jo.put("name", "stub_name");
210 jo.put("version", "2.0");
211 jo.put("metadataOnly", true);
212 jo.put("suspend", true);
213 jo.put("delivery", JSObject);
218 protected boolean doInsert(Insertable bean) {
222 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "maxSubs", 10, true);
223 subscribeServlet.doPost(request, response);
224 verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));
229 public void Given_Request_Is_HTTP_POST_And_Change_On_Feeds_Succeeds_A_STATUS_OK_Response_Is_Generated() throws Exception {
230 ServletOutputStream outStream = mock(ServletOutputStream.class);
231 when(response.getOutputStream()).thenReturn(outStream);
232 PowerMockito.mockStatic(Subscription.class);
233 PowerMockito.when(Subscription.getSubscriptionMatching(mock(Subscription.class))).thenReturn(null);
234 JSONObject JSObject = buildRequestJsonObject();
235 SubscribeServlet subscribeServlet = new SubscribeServlet() {
236 protected JSONObject getJSONfromInput(HttpServletRequest req) {
237 JSONObject jo = new JSONObject();
238 jo.put("name", "stub_name");
239 jo.put("version", "2.0");
240 jo.put("metadataOnly", true);
241 jo.put("suspend", true);
242 jo.put("delivery", JSObject);
247 protected boolean doInsert(Insertable bean) {
251 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "maxSubs", 10, true);
252 subscribeServlet.doPost(request, response);
253 verify(response).setStatus(eq(HttpServletResponse.SC_CREATED));
258 private JSONObject buildRequestJsonObject() {
259 JSONObject JSObject = new JSONObject();
260 JSONArray endpointIDs = new JSONArray();
261 JSONObject JOEndpointIDs = new JSONObject();
262 JOEndpointIDs.put("id", "stub_endpoint_id");
263 JOEndpointIDs.put("password", "stub_endpoint_password");
264 endpointIDs.put(JOEndpointIDs);
266 JSONArray endpointAddresses = new JSONArray();
267 endpointAddresses.put("127.0.0.1");
269 JSObject.put("classification", "stub_classification");
270 JSObject.put("endpoint_ids", endpointIDs);
271 JSObject.put("endpoint_addrs", endpointAddresses);
272 JSObject.put("url", "https://stub_address");
273 JSObject.put("use100", "true");
274 JSObject.put("password", "stub_password");
275 JSObject.put("user", "stub_user");
281 private void initialiseBaseServletToBypassRetreiviingInitialisationParametersFromDatabase() throws IllegalAccessException {
282 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "startmsgFlag", false, true);
283 SynchronizerTask synchronizerTask = mock(SynchronizerTask.class);
284 when(synchronizerTask.getState()).thenReturn(SynchronizerTask.UNKNOWN);
285 FieldUtils.writeDeclaredStaticField(SynchronizerTask.class, "synctask", synchronizerTask, true);
286 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "max_subs", 10, true);
289 private void setUpValidSecurityOnHttpRequest() throws Exception {
290 when(request.isSecure()).thenReturn(true);
291 Set<String> authAddressesAndNetworks = new HashSet<String>();
292 authAddressesAndNetworks.add(("127.0.0.1"));
293 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, true);
294 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true);
297 private void setBehalfHeader(String headerValue) {
298 when(request.getHeader(BEHALF_HEADER)).thenReturn(headerValue);
301 private void setValidPathInfoInHttpHeader() {
302 when(request.getPathInfo()).thenReturn("/123");
305 private void setFeedToReturnInvalidFeedIdSupplied() {
306 PowerMockito.mockStatic(Feed.class);
307 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(null);
310 private void setFeedToReturnValidFeedForSuppliedId() {
311 PowerMockito.mockStatic(Feed.class);
312 Feed feed = mock(Feed.class);
313 PowerMockito.when(Feed.getFeedById(anyInt())).thenReturn(feed);
314 when(feed.isDeleted()).thenReturn(false);
315 when(feed.asJSONObject(true)).thenReturn(mock(JSONObject.class));
316 when(feed.getPublisher()).thenReturn("Stub_Value");
317 when(feed.getName()).thenReturn("stub_name");
318 when(feed.getVersion()).thenReturn("1.0");
319 when(feed.asLimitedJSONObject()).thenReturn(mock(JSONObject.class));
322 private void setAuthoriserToReturnRequestNotAuthorized() throws IllegalAccessException {
323 AuthorizationResponse authResponse = mock(AuthorizationResponse.class);
324 Authorizer authorizer = mock(Authorizer.class);
325 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true);
326 when(authorizer.decide(request)).thenReturn(authResponse);
327 when(authResponse.isAuthorized()).thenReturn(false);
330 private void setAuthoriserToReturnRequestIsAuthorized() throws IllegalAccessException {
331 AuthorizationResponse authResponse = mock(AuthorizationResponse.class);
332 Authorizer authorizer = mock(Authorizer.class);
333 FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true);
334 when(authorizer.decide(request)).thenReturn(authResponse);
335 when(authResponse.isAuthorized()).thenReturn(true);
338 private void setPokerToNotCreateTimersWhenDeleteFeedIsCalled() throws Exception {
339 Poker poker = mock(Poker.class);
340 FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true);
343 private void setupValidAuthorisedRequest() throws Exception {
344 setUpValidSecurityOnHttpRequest();
345 setBehalfHeader("Stub_Value");
346 setValidPathInfoInHttpHeader();
347 setFeedToReturnValidFeedForSuppliedId();
350 private void setUpValidContentHeadersAndJSONOnHttpRequest() {
351 when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription; version=1.0");
352 when(request.getHeader("X-ATT-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");