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