Merge "Unit tests for ExpiryRecord"
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / SubscriptionServletTest.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
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
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
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====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23 package org.onap.dmaap.datarouter.provisioning;
24
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.Deleteable;
35 import org.onap.dmaap.datarouter.provisioning.beans.Subscription;
36 import org.onap.dmaap.datarouter.provisioning.beans.Updateable;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
39 import org.powermock.modules.junit4.PowerMockRunner;
40
41 import javax.servlet.ServletInputStream;
42 import javax.servlet.ServletOutputStream;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45 import java.util.HashSet;
46 import java.util.Set;
47
48 import static org.hamcrest.Matchers.notNullValue;
49 import static org.mockito.Mockito.*;
50 import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER;
51
52
53 @RunWith(PowerMockRunner.class)
54 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Subscription")
55 public class SubscriptionServletTest extends DrServletTestBase {
56     private SubscriptionServlet subscriptionServlet;
57
58     @Mock
59     private HttpServletRequest request;
60     @Mock
61     private HttpServletResponse response;
62
63     @Before
64     public void setUp() throws Exception {
65         super.setUp();
66         subscriptionServlet = new SubscriptionServlet();
67         setAuthoriserToReturnRequestIsAuthorized();
68         setPokerToNotCreateTimersWhenDeleteSubscriptionIsCalled();
69         setupValidAuthorisedRequest();
70         setUpValidSecurityOnHttpRequest();
71     }
72
73     @Test
74     public void Given_Request_Is_HTTP_DELETE_SC_Forbidden_Response_Is_Generated() throws Exception {
75         when(request.isSecure()).thenReturn(false);
76         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true);
77         subscriptionServlet.doDelete(request, response);
78         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
79     }
80
81     @Test
82     public void Given_Request_Is_HTTP_DELETE_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception {
83         setBehalfHeader(null);
84         subscriptionServlet.doDelete(request, response);
85         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
86     }
87
88     @Test
89     public void Given_Request_Is_HTTP_DELETE_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated() throws Exception {
90         when(request.getPathInfo()).thenReturn(null);
91         subscriptionServlet.doDelete(request, response);
92         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
93     }
94
95     @Test
96     public void Given_Request_Is_HTTP_DELETE_And_Subscription_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception {
97         setSubscriptionToReturnInvalidSubscriptionIdSupplied();
98         subscriptionServlet.doDelete(request, response);
99         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
100     }
101
102     @Test
103     public void Given_Request_Is_HTTP_DELETE_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception {
104         setAuthoriserToReturnRequestNotAuthorized();
105         subscriptionServlet.doDelete(request, response);
106         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
107     }
108
109     @Test
110     public void Given_Request_Is_HTTP_DELETE_And_Delete_On_Database_Fails_An_Internal_Server_Error_Is_Reported() throws Exception {
111         SubscriptionServlet subscriptionServlet = new SubscriptionServlet(){
112             public boolean doDelete(Deleteable deletable){
113                 return false;
114             }
115         };
116         subscriptionServlet.doDelete(request, response);
117         verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));
118     }
119
120     @Test
121     public void Given_Request_Is_HTTP_DELETE_And_Delete_On_Database_Succeeds_A_NO_CONTENT_Response_Is_Generated() throws Exception {
122         SubscriptionServlet subscriptionServlet = new SubscriptionServlet(){
123             public boolean doDelete(Deleteable deletable){
124                 return true;
125             }
126         };
127         subscriptionServlet.doDelete(request, response);
128         verify(response).setStatus(eq(HttpServletResponse.SC_NO_CONTENT));
129     }
130
131     @Test
132     public void Given_Request_Is_HTTP_GET_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception {
133         when(request.isSecure()).thenReturn(false);
134         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true);
135         subscriptionServlet.doGet(request, response);
136         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
137     }
138
139     @Test
140     public void Given_Request_Is_HTTP_GET_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception {
141         setBehalfHeader(null);
142         subscriptionServlet.doGet(request, response);
143         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
144     }
145
146     @Test
147     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 {
148         when(request.getPathInfo()).thenReturn(null);
149         subscriptionServlet.doGet(request, response);
150         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
151     }
152
153     @Test
154     public void Given_Request_Is_HTTP_GET_And_Subscription_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception {
155         setSubscriptionToReturnInvalidSubscriptionIdSupplied();
156         subscriptionServlet.doGet(request, response);
157         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
158     }
159
160     @Test
161     public void Given_Request_Is_HTTP_GET_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception {
162         setAuthoriserToReturnRequestNotAuthorized();
163         subscriptionServlet.doGet(request, response);
164         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
165     }
166
167     @Test
168     public void Given_Request_Is_HTTP_GET_And_Request_Succeeds() throws Exception {
169         JSONObject JSObject = buildRequestJsonObject();
170         JSONObject jo = new JSONObject();
171         jo.put("name", "stub_name");
172         jo.put("version", "2.0");
173         jo.put("metadataOnly", true);
174         jo.put("suspend", true);
175         jo.put("delivery", JSObject);
176         jo.put("sync", true);
177         Subscription sub = new Subscription(jo);
178         PowerMockito.mockStatic(Subscription.class);
179         PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(sub);
180         ServletOutputStream outStream = mock(ServletOutputStream.class);
181         when(response.getOutputStream()).thenReturn(outStream);
182         subscriptionServlet.doGet(request, response);
183         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
184     }
185
186     @Test
187     public void Given_Request_Is_HTTP_PUT_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception {
188         when(request.isSecure()).thenReturn(false);
189         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true);
190         subscriptionServlet.doPut(request, response);
191         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
192     }
193
194     @Test
195     public void Given_Request_Is_HTTP_PUT_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception {
196         setBehalfHeader(null);
197         subscriptionServlet.doPut(request, response);
198         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
199     }
200
201     @Test
202     public void Given_Request_Is_HTTP_PUT_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated() throws Exception {
203         when(request.getPathInfo()).thenReturn(null);
204         subscriptionServlet.doPut(request, response);
205         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
206     }
207
208     @Test
209     public void Given_Request_Is_HTTP_PUT_And_Subscription_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception {
210         setSubscriptionToReturnInvalidSubscriptionIdSupplied();
211         subscriptionServlet.doPut(request, response);
212         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
213     }
214
215     @Test
216     public void Given_Request_Is_HTTP_PUT_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception {
217         setAuthoriserToReturnRequestNotAuthorized();
218         subscriptionServlet.doPut(request, response);
219         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
220     }
221
222     @Test
223     public void Given_Request_Is_HTTP_PUT_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() throws Exception {
224         when(request.getContentType()).thenReturn("stub_ContentType");
225         subscriptionServlet.doPut(request, response);
226         verify(response).sendError(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE), argThat(notNullValue(String.class)));
227     }
228
229     @Test
230     public void Given_Request_Is_HTTP_PUT_And_Request_Contains_Badly_Formed_JSON_Then_Bad_Request_Response_Is_Generated() throws Exception {
231         when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription; version=1.0");
232         ServletInputStream inStream = mock(ServletInputStream.class);
233         when(request.getInputStream()).thenReturn(inStream);
234         subscriptionServlet.doPut(request, response);
235         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
236     }
237
238     @Test
239     public void Given_Request_Is_HTTP_PUT_And_Subscription_Object_Is_Invalid_Bad_Request_Response_Is_Generated() throws Exception {
240         when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription; version=1.0");
241         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
242             protected JSONObject getJSONfromInput(HttpServletRequest req) {
243                 JSONObject jo = new JSONObject();
244                 return jo;
245             }
246         };
247         subscriptionServlet.doPut(request, response);
248         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
249     }
250
251     @Test
252     public void Given_Request_Is_HTTP_PUT_And_Subscriber_Modified_By_Different_Creator() throws Exception {
253         when(request.getHeader("X-ATT-DR-ON-BEHALF-OF-GROUP")).thenReturn(null);
254         when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription; version=1.0");
255         JSONObject JSObject = buildRequestJsonObject();
256         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
257             protected JSONObject getJSONfromInput(HttpServletRequest req) {
258                 JSONObject jo = new JSONObject();
259                 jo.put("name", "stub_name");
260                 jo.put("version", "2.0");
261                 jo.put("metadataOnly", true);
262                 jo.put("suspend", true);
263                 jo.put("delivery", JSObject);
264                 jo.put("sync", true);
265                 return jo;
266             }
267         };
268         subscriptionServlet.doPut(request, response);
269         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
270     }
271
272     @Test
273     public void Given_Request_Is_HTTP_PUT_And_Update_Fails() throws Exception {
274         when(request.getHeader("X-ATT-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
275         when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription; version=1.0");
276         JSONObject JSObject = buildRequestJsonObject();
277         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
278             protected JSONObject getJSONfromInput(HttpServletRequest req) {
279                 JSONObject jo = new JSONObject();
280                 jo.put("name", "stub_name");
281                 jo.put("version", "2.0");
282                 jo.put("metadataOnly", true);
283                 jo.put("suspend", true);
284                 jo.put("delivery", JSObject);
285                 jo.put("sync", true);
286                 return jo;
287             }
288
289             @Override
290             protected boolean doUpdate(Updateable bean) {
291                 return false;
292             }
293         };
294         subscriptionServlet.doPut(request, response);
295         verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));
296     }
297
298     @Test
299     public void Given_Request_Is_HTTP_PUT_And_Update_Succeeds() throws Exception {
300         ServletOutputStream outStream = mock(ServletOutputStream.class);
301         when(response.getOutputStream()).thenReturn(outStream);
302         when(request.getHeader("X-ATT-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
303         when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription; version=1.0");
304         JSONObject JSObject = buildRequestJsonObject();
305         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
306             protected JSONObject getJSONfromInput(HttpServletRequest req) {
307                 JSONObject jo = new JSONObject();
308                 jo.put("name", "stub_name");
309                 jo.put("version", "2.0");
310                 jo.put("metadataOnly", true);
311                 jo.put("suspend", true);
312                 jo.put("delivery", JSObject);
313                 jo.put("sync", true);
314                 return jo;
315             }
316
317             @Override
318             protected boolean doUpdate(Updateable bean) {
319                 return true;
320             }
321         };
322         subscriptionServlet.doPut(request, response);
323         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
324     }
325
326     @Test
327     public void Given_Request_Is_HTTP_POST_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception {
328         when(request.isSecure()).thenReturn(false);
329         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "isAddressAuthEnabled", "true", true);
330         subscriptionServlet.doPost(request, response);
331         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
332     }
333
334     @Test
335     public void Given_Request_Is_HTTP_POST_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception {
336         setBehalfHeader(null);
337         subscriptionServlet.doPost(request, response);
338         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
339     }
340
341     @Test
342     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 {
343         when(request.getPathInfo()).thenReturn(null);
344         subscriptionServlet.doPost(request, response);
345         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
346     }
347
348     @Test
349     public void Given_Request_Is_HTTP_POST_And_Subscription_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception {
350         setSubscriptionToReturnInvalidSubscriptionIdSupplied();
351         subscriptionServlet.doPost(request, response);
352         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
353     }
354
355     @Test
356     public void Given_Request_Is_HTTP_POST_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() throws Exception {
357         when(request.getContentType()).thenReturn("stub_ContentType");
358         subscriptionServlet.doPost(request, response);
359         verify(response).sendError(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE), argThat(notNullValue(String.class)));
360     }
361
362     @Test
363     public void Given_Request_Is_HTTP_POST_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception {
364         when(request.getHeader(anyString())).thenReturn("application/vnd.att-dr.subscription-control");
365         setAuthoriserToReturnRequestNotAuthorized();
366         subscriptionServlet.doPost(request, response);
367         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
368     }
369
370     @Test
371     public void Given_Request_Is_HTTP_POST_And_Request_Contains_Badly_Formed_JSON_Then_Bad_Request_Response_Is_Generated() throws Exception {
372         when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription-control; version=1.0");
373         ServletInputStream inStream = mock(ServletInputStream.class);
374         when(request.getInputStream()).thenReturn(inStream);
375         subscriptionServlet.doPost(request, response);
376         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
377     }
378
379     @Test
380     public void Given_Request_Is_HTTP_POST_And_Post_Fails() throws Exception {
381         when(request.getHeader("X-ATT-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
382         when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription-control; version=1.0");
383         JSONObject JSObject = buildRequestJsonObject();
384         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
385             protected JSONObject getJSONfromInput(HttpServletRequest req) {
386                 JSONObject jo = new JSONObject();
387                 jo.put("name", "stub_name");
388                 jo.put("version", "2.0");
389                 jo.put("metadataOnly", true);
390                 jo.put("suspend", true);
391                 jo.put("delivery", JSObject);
392                 return jo;
393             }
394         };
395         subscriptionServlet.doPost(request, response);
396         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
397     }
398
399     @Test
400     public void Given_Request_Is_HTTP_POST_And_Post_Succeeds() throws Exception {
401         ServletOutputStream outStream = mock(ServletOutputStream.class);
402         when(response.getOutputStream()).thenReturn(outStream);
403         when(request.getHeader("X-ATT-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
404         when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.subscription-control; version=1.0");
405         JSONObject JSObject = buildRequestJsonObject();
406         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
407             protected JSONObject getJSONfromInput(HttpServletRequest req) {
408                 JSONObject jo = new JSONObject();
409                 jo.put("name", "stub_name");
410                 jo.put("version", "2.0");
411                 jo.put("metadataOnly", true);
412                 jo.put("suspend", true);
413                 jo.put("delivery", JSObject);
414                 jo.put("failed", false);
415                 return jo;
416             }
417         };
418         subscriptionServlet.doPost(request, response);
419         verify(response).setStatus(eq(HttpServletResponse.SC_ACCEPTED));
420     }
421
422     @NotNull
423     private JSONObject buildRequestJsonObject() {
424         JSONObject JSObject = new JSONObject();
425         JSObject.put("url", "https://stub_address");
426         JSObject.put("use100", "true");
427         JSObject.put("password", "stub_password");
428         JSObject.put("user", "stub_user");
429         return JSObject;
430     }
431
432     private void setUpValidSecurityOnHttpRequest() throws Exception {
433         when(request.isSecure()).thenReturn(true);
434         Set<String> authAddressesAndNetworks = new HashSet<String>();
435         authAddressesAndNetworks.add(("127.0.0.1"));
436         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, true);
437         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true);
438     }
439
440     private void setBehalfHeader(String headerValue) {
441         when(request.getHeader(BEHALF_HEADER)).thenReturn(headerValue);
442     }
443
444     private void setValidPathInfoInHttpHeader() {
445         when(request.getPathInfo()).thenReturn("/123");
446     }
447
448     private void setSubscriptionToReturnInvalidSubscriptionIdSupplied() {
449         PowerMockito.mockStatic(Subscription.class);
450         PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(null);
451     }
452
453     private void setSubscriptionToReturnValidSubscriptionForSuppliedId() {
454         PowerMockito.mockStatic(Subscription.class);
455         Subscription subscription = mock(Subscription.class);
456         PowerMockito.when(Subscription.getSubscriptionById(anyInt())).thenReturn(subscription);
457         when(subscription.getSubscriber()).thenReturn("Stub_Value");
458         when(subscription.asJSONObject()).thenReturn(mock(JSONObject.class));
459     }
460
461     private void setAuthoriserToReturnRequestNotAuthorized() throws IllegalAccessException {
462         AuthorizationResponse authResponse = mock(AuthorizationResponse.class);
463         Authorizer authorizer = mock(Authorizer.class);
464         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true);
465         when(authorizer.decide(request)).thenReturn(authResponse);
466         when(authResponse.isAuthorized()).thenReturn(false);
467     }
468
469     private void setAuthoriserToReturnRequestIsAuthorized() throws IllegalAccessException {
470         AuthorizationResponse authResponse = mock(AuthorizationResponse.class);
471         Authorizer authorizer = mock(Authorizer.class);
472         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true);
473         when(authorizer.decide(request)).thenReturn(authResponse);
474         when(authResponse.isAuthorized()).thenReturn(true);
475     }
476
477     private void setPokerToNotCreateTimersWhenDeleteSubscriptionIsCalled() throws Exception {
478         Poker poker = mock(Poker.class);
479         FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true);
480     }
481
482     private void setupValidAuthorisedRequest() throws Exception {
483         setUpValidSecurityOnHttpRequest();
484         setBehalfHeader("Stub_Value");
485         setValidPathInfoInHttpHeader();
486         setSubscriptionToReturnValidSubscriptionForSuppliedId();
487     }
488 }