Replace ATT headers
[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 ch.qos.logback.classic.Logger;
26 import ch.qos.logback.classic.spi.ILoggingEvent;
27 import ch.qos.logback.core.read.ListAppender;
28 import org.apache.commons.lang3.reflect.FieldUtils;
29 import org.jetbrains.annotations.NotNull;
30 import org.json.JSONObject;
31 import org.junit.*;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.onap.dmaap.datarouter.authz.AuthorizationResponse;
35 import org.onap.dmaap.datarouter.authz.Authorizer;
36 import org.onap.dmaap.datarouter.provisioning.beans.Deleteable;
37 import org.onap.dmaap.datarouter.provisioning.beans.SubDelivery;
38 import org.onap.dmaap.datarouter.provisioning.beans.Subscription;
39 import org.onap.dmaap.datarouter.provisioning.beans.Updateable;
40 import org.onap.dmaap.datarouter.provisioning.utils.DB;
41 import org.powermock.modules.junit4.PowerMockRunner;
42 import org.slf4j.LoggerFactory;
43
44 import javax.persistence.EntityManager;
45 import javax.persistence.EntityManagerFactory;
46 import javax.persistence.Persistence;
47 import javax.servlet.ServletInputStream;
48 import javax.servlet.ServletOutputStream;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51 import java.io.File;
52 import java.io.FileNotFoundException;
53 import java.io.PrintWriter;
54 import java.sql.SQLException;
55 import java.util.HashSet;
56 import java.util.Set;
57
58 import static org.hamcrest.Matchers.notNullValue;
59 import static org.junit.Assert.assertEquals;
60 import static org.mockito.Mockito.*;
61 import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER;
62
63
64 @RunWith(PowerMockRunner.class)
65 public class SubscriptionServletTest extends DrServletTestBase {
66     private static EntityManagerFactory emf;
67     private static EntityManager em;
68     private SubscriptionServlet subscriptionServlet;
69     private DB db;
70     private final String URL= "https://172.100.0.5";
71     private final String USER = "user1";
72     private final String PASSWORD="password1";
73
74
75     @Mock
76     private HttpServletRequest request;
77     @Mock
78     private HttpServletResponse response;
79
80     ListAppender<ILoggingEvent> listAppender;
81
82     @BeforeClass
83     public static void init() {
84         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
85         em = emf.createEntityManager();
86         System.setProperty(
87             "org.onap.dmaap.datarouter.provserver.properties",
88             "src/test/resources/h2Database.properties");
89     }
90
91     @AfterClass
92     public static void tearDownClass() throws FileNotFoundException {
93         em.clear();
94         em.close();
95         emf.close();
96     }
97
98     @Before
99     public void setUp() throws Exception {
100         listAppender = setTestLogger(SubscriptionServlet.class);
101         subscriptionServlet = new SubscriptionServlet();
102         db = new DB();
103         setAuthoriserToReturnRequestIsAuthorized();
104         setPokerToNotCreateTimersWhenDeleteSubscriptionIsCalled();
105         setupValidAuthorisedRequest();
106         setUpValidSecurityOnHttpRequest();
107     }
108
109     @Test
110     public void Given_Request_Is_HTTP_DELETE_SC_Forbidden_Response_Is_Generated() throws Exception {
111         when(request.isSecure()).thenReturn(false);
112         subscriptionServlet.doDelete(request, response);
113         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
114         verifyEnteringExitCalled(listAppender);
115     }
116
117     @Test
118     public void Given_Request_Is_HTTP_DELETE_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception {
119         setBehalfHeader(null);
120         subscriptionServlet.doDelete(request, response);
121         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
122     }
123
124     @Test
125     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 {
126         when(request.getPathInfo()).thenReturn(null);
127         subscriptionServlet.doDelete(request, response);
128         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
129     }
130
131     @Test
132     public void Given_Request_Is_HTTP_DELETE_And_Subscription_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception {
133         when(request.getPathInfo()).thenReturn("/3");
134         subscriptionServlet.doDelete(request, response);
135         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
136     }
137
138     @Test
139     public void Given_Request_Is_HTTP_DELETE_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception {
140         setAuthoriserToReturnRequestNotAuthorized();
141         subscriptionServlet.doDelete(request, response);
142         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
143     }
144
145     @Test
146     public void Given_Request_Is_HTTP_DELETE_And_Delete_On_Database_Fails_An_Internal_Server_Error_Is_Reported() throws Exception {
147         SubscriptionServlet subscriptionServlet = new SubscriptionServlet(){
148             public boolean doDelete(Deleteable deletable){
149                 return false;
150             }
151         };
152         subscriptionServlet.doDelete(request, response);
153         verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));
154     }
155
156     @Test
157     public void Given_Request_Is_HTTP_DELETE_And_Delete_On_Database_Succeeds_A_NO_CONTENT_Response_Is_Generated() throws Exception {
158         subscriptionServlet.doDelete(request, response);
159         verify(response).setStatus(eq(HttpServletResponse.SC_NO_CONTENT));
160         verifyEnteringExitCalled(listAppender);
161         insertSubscriptionIntoDb();
162     }
163
164     @Test
165     public void Given_Request_Is_HTTP_GET_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception {
166         when(request.isSecure()).thenReturn(false);
167         subscriptionServlet.doGet(request, response);
168         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
169         verifyEnteringExitCalled(listAppender);
170     }
171
172     @Test
173     public void Given_Request_Is_HTTP_GET_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception {
174         setBehalfHeader(null);
175         subscriptionServlet.doGet(request, response);
176         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
177     }
178
179     @Test
180     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 {
181         when(request.getPathInfo()).thenReturn(null);
182         subscriptionServlet.doGet(request, response);
183         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
184     }
185
186     @Test
187     public void Given_Request_Is_HTTP_GET_And_Subscription_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception {
188         when(request.getPathInfo()).thenReturn("/3");
189         subscriptionServlet.doGet(request, response);
190         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
191     }
192
193     @Test
194     public void Given_Request_Is_HTTP_GET_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception {
195         setAuthoriserToReturnRequestNotAuthorized();
196         subscriptionServlet.doGet(request, response);
197         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
198     }
199
200     @Test
201     public void Given_Request_Is_HTTP_GET_And_Request_Succeeds() throws Exception {
202         ServletOutputStream outStream = mock(ServletOutputStream.class);
203         when(response.getOutputStream()).thenReturn(outStream);
204         subscriptionServlet.doGet(request, response);
205         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
206         verifyEnteringExitCalled(listAppender);
207     }
208
209     @Test
210     public void Given_Request_Is_HTTP_PUT_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception {
211         when(request.isSecure()).thenReturn(false);
212         subscriptionServlet.doPut(request, response);
213         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
214         verifyEnteringExitCalled(listAppender);
215     }
216
217     @Test
218     public void Given_Request_Is_HTTP_PUT_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception {
219         setBehalfHeader(null);
220         subscriptionServlet.doPut(request, response);
221         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
222     }
223
224     @Test
225     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 {
226         when(request.getPathInfo()).thenReturn(null);
227         subscriptionServlet.doPut(request, response);
228         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
229     }
230
231     @Test
232     public void Given_Request_Is_HTTP_PUT_And_Subscription_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception {
233         when(request.getPathInfo()).thenReturn("/3");
234         subscriptionServlet.doPut(request, response);
235         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class)));
236     }
237
238     @Test
239     public void Given_Request_Is_HTTP_PUT_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception {
240         setAuthoriserToReturnRequestNotAuthorized();
241         subscriptionServlet.doPut(request, response);
242         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
243     }
244
245     @Test
246     public void Given_Request_Is_HTTP_PUT_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() throws Exception {
247         when(request.getContentType()).thenReturn("stub_ContentType");
248         subscriptionServlet.doPut(request, response);
249         verify(response).sendError(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE), argThat(notNullValue(String.class)));
250     }
251
252     @Test
253     public void Given_Request_Is_HTTP_PUT_And_Request_Contains_Badly_Formed_JSON_Then_Bad_Request_Response_Is_Generated() throws Exception {
254         when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription; version=1.0");
255         ServletInputStream inStream = mock(ServletInputStream.class);
256         when(request.getInputStream()).thenReturn(inStream);
257         subscriptionServlet.doPut(request, response);
258         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
259     }
260
261     @Test
262     public void Given_Request_Is_HTTP_PUT_And_Subscription_Object_Is_Invalid_Bad_Request_Response_Is_Generated() throws Exception {
263         when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription; version=1.0");
264         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
265             protected JSONObject getJSONfromInput(HttpServletRequest req) {
266                 JSONObject jo = new JSONObject();
267                 return jo;
268             }
269         };
270         subscriptionServlet.doPut(request, response);
271         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
272     }
273
274     @Test
275     public void Given_Request_Is_HTTP_PUT_And_Subscriber_Modified_By_Different_Creator_Then_Bad_Request_Is_Generated() throws Exception {
276         when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn(null);
277         when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription; version=1.0");
278         JSONObject JSObject = buildRequestJsonObject();
279         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
280             protected JSONObject getJSONfromInput(HttpServletRequest req) {
281                 JSONObject jo = new JSONObject();
282                 jo.put("name", "stub_name");
283                 jo.put("version", "2.0");
284                 jo.put("metadataOnly", true);
285                 jo.put("suspend", true);
286                 jo.put("delivery", JSObject);
287                 jo.put("subscriber", "differentSubscriber");
288                 jo.put("sync", true);
289                 return jo;
290             }
291         };
292         subscriptionServlet.doPut(request, response);
293         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
294     }
295
296     @Test
297     public void Given_Request_Is_HTTP_PUT_And_Update_Fails() throws Exception {
298         when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
299         when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription; version=1.0");
300         JSONObject JSObject = buildRequestJsonObject();
301         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
302             protected JSONObject getJSONfromInput(HttpServletRequest req) {
303                 JSONObject jo = new JSONObject();
304                 jo.put("name", "stub_name");
305                 jo.put("version", "2.0");
306                 jo.put("metadataOnly", true);
307                 jo.put("suspend", true);
308                 jo.put("delivery", JSObject);
309                 jo.put("sync", true);
310                 return jo;
311             }
312
313             @Override
314             protected boolean doUpdate(Updateable bean) {
315                 return false;
316             }
317         };
318         subscriptionServlet.doPut(request, response);
319         verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class)));
320     }
321
322     @Test
323     public void Given_Request_Is_HTTP_PUT_And_Update_Succeeds() throws Exception {
324         ServletOutputStream outStream = mock(ServletOutputStream.class);
325         when(response.getOutputStream()).thenReturn(outStream);
326         when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
327         when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription; version=1.0");
328         JSONObject JSObject = buildRequestJsonObject();
329         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
330             protected JSONObject getJSONfromInput(HttpServletRequest req) {
331                 JSONObject jo = new JSONObject();
332                 jo.put("name", "stub_name");
333                 jo.put("version", "2.0");
334                 jo.put("metadataOnly", true);
335                 jo.put("suspend", true);
336                 jo.put("delivery", JSObject);
337                 jo.put("sync", true);
338                 jo.put("changeowner", true);
339                 return jo;
340             }
341         };
342         subscriptionServlet.doPut(request, response);
343         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
344         changeSubscriptionBackToNormal();
345         verifyEnteringExitCalled(listAppender);
346     }
347
348     @Test
349     public void Given_Request_Is_HTTP_POST_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception {
350         when(request.isSecure()).thenReturn(false);
351         subscriptionServlet.doPost(request, response);
352         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
353         verifyEnteringExitCalled(listAppender);
354     }
355
356     @Test
357     public void Given_Request_Is_HTTP_POST_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception {
358         setBehalfHeader(null);
359         subscriptionServlet.doPost(request, response);
360         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
361     }
362
363     @Test
364     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 {
365         when(request.getPathInfo()).thenReturn(null);
366         subscriptionServlet.doPost(request, response);
367         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
368     }
369
370     @Test
371     public void Given_Request_Is_HTTP_POST_And_Subscription_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception {
372         when(request.getPathInfo()).thenReturn("/3");
373         subscriptionServlet.doPost(request, response);
374         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
375     }
376
377     @Test
378     public void Given_Request_Is_HTTP_POST_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() throws Exception {
379         when(request.getContentType()).thenReturn("stub_ContentType");
380         subscriptionServlet.doPost(request, response);
381         verify(response).sendError(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE), argThat(notNullValue(String.class)));
382     }
383
384     @Test
385     public void Given_Request_Is_HTTP_POST_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception {
386         when(request.getHeader(anyString())).thenReturn("application/vnd.dmaap-dr.subscription-control");
387         setAuthoriserToReturnRequestNotAuthorized();
388         subscriptionServlet.doPost(request, response);
389         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class)));
390     }
391
392     @Test
393     public void Given_Request_Is_HTTP_POST_And_Request_Contains_Badly_Formed_JSON_Then_Bad_Request_Response_Is_Generated() throws Exception {
394         when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription-control; version=1.0");
395         ServletInputStream inStream = mock(ServletInputStream.class);
396         when(request.getInputStream()).thenReturn(inStream);
397         subscriptionServlet.doPost(request, response);
398         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
399     }
400
401     @Test
402     public void Given_Request_Is_HTTP_POST_And_Post_Fails() throws Exception {
403         when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
404         when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-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                 return jo;
415             }
416         };
417         subscriptionServlet.doPost(request, response);
418         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class)));
419     }
420
421     @Test
422     public void Given_Request_Is_HTTP_POST_And_Post_Succeeds() throws Exception {
423         ServletOutputStream outStream = mock(ServletOutputStream.class);
424         when(response.getOutputStream()).thenReturn(outStream);
425         when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
426         when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription-control; version=1.0");
427         JSONObject JSObject = buildRequestJsonObject();
428         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
429             protected JSONObject getJSONfromInput(HttpServletRequest req) {
430                 JSONObject jo = new JSONObject();
431                 jo.put("name", "stub_name");
432                 jo.put("version", "2.0");
433                 jo.put("metadataOnly", true);
434                 jo.put("suspend", true);
435                 jo.put("delivery", JSObject);
436                 jo.put("failed", false);
437                 return jo;
438             }
439         };
440         subscriptionServlet.doPost(request, response);
441         verify(response).setStatus(eq(HttpServletResponse.SC_ACCEPTED));
442         verifyEnteringExitCalled(listAppender);
443     }
444
445     @NotNull
446     private JSONObject buildRequestJsonObject() {
447         JSONObject JSObject = new JSONObject();
448         JSObject.put("url", "https://stub_address");
449         JSObject.put("use100", "true");
450         JSObject.put("password", "stub_password");
451         JSObject.put("user", "stub_user");
452         return JSObject;
453     }
454
455     private void setUpValidSecurityOnHttpRequest() throws Exception {
456         when(request.isSecure()).thenReturn(true);
457         Set<String> authAddressesAndNetworks = new HashSet<String>();
458         authAddressesAndNetworks.add(("127.0.0.1"));
459         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, true);
460         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true);
461     }
462
463     private void setBehalfHeader(String headerValue) {
464         when(request.getHeader(BEHALF_HEADER)).thenReturn(headerValue);
465     }
466
467     private void setValidPathInfoInHttpHeader() {
468         when(request.getPathInfo()).thenReturn("/1");
469     }
470
471     private void setAuthoriserToReturnRequestNotAuthorized() throws IllegalAccessException {
472         AuthorizationResponse authResponse = mock(AuthorizationResponse.class);
473         Authorizer authorizer = mock(Authorizer.class);
474         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true);
475         when(authorizer.decide(request)).thenReturn(authResponse);
476         when(authResponse.isAuthorized()).thenReturn(false);
477     }
478
479     private void setAuthoriserToReturnRequestIsAuthorized() throws IllegalAccessException {
480         AuthorizationResponse authResponse = mock(AuthorizationResponse.class);
481         Authorizer authorizer = mock(Authorizer.class);
482         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true);
483         when(authorizer.decide(request)).thenReturn(authResponse);
484         when(authResponse.isAuthorized()).thenReturn(true);
485     }
486
487     private void setPokerToNotCreateTimersWhenDeleteSubscriptionIsCalled() throws Exception {
488         Poker poker = mock(Poker.class);
489         FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true);
490     }
491
492     private void setupValidAuthorisedRequest() throws Exception {
493         setUpValidSecurityOnHttpRequest();
494         setBehalfHeader("Stub_Value");
495         setValidPathInfoInHttpHeader();
496     }
497
498     private void insertSubscriptionIntoDb() throws SQLException {
499         Subscription subscription = new Subscription(URL, USER, PASSWORD);
500         subscription.setSubid(1);
501         subscription.setSubscriber("user1");
502         subscription.setFeedid(1);
503         SubDelivery subDelivery = new SubDelivery(URL, USER, PASSWORD, true);
504         subscription.setDelivery(subDelivery);
505         subscription.setGroupid(1);
506         subscription.setMetadataOnly(false);
507         subscription.setSuspended(false);
508         subscription.doInsert(db.getConnection());
509     }
510
511     private void changeSubscriptionBackToNormal() throws SQLException {
512         Subscription subscription = new Subscription("https://172.100.0.5", "user1", "password1");
513         subscription.setSubid(1);
514         subscription.setSubscriber("user1");
515         subscription.setFeedid(1);
516         SubDelivery subDelivery = new SubDelivery(URL, USER, PASSWORD, true);
517         subscription.setDelivery(subDelivery);
518         subscription.setGroupid(1);
519         subscription.setMetadataOnly(false);
520         subscription.setSuspended(false);
521         subscription.changeOwnerShip();
522         subscription.doUpdate(db.getConnection());
523     }
524 }