update link to upper-constraints.txt
[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 static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.ArgumentMatchers.contains;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER;
32
33 import ch.qos.logback.classic.spi.ILoggingEvent;
34 import ch.qos.logback.core.read.ListAppender;
35 import jakarta.servlet.ServletInputStream;
36 import jakarta.servlet.ServletOutputStream;
37 import java.sql.Connection;
38 import java.sql.SQLException;
39 import java.util.HashSet;
40 import java.util.Set;
41 import javax.persistence.EntityManager;
42 import javax.persistence.EntityManagerFactory;
43 import javax.persistence.Persistence;
44 import jakarta.servlet.http.HttpServletRequest;
45 import jakarta.servlet.http.HttpServletResponse;
46 import org.apache.commons.lang3.reflect.FieldUtils;
47 import org.jetbrains.annotations.NotNull;
48 import org.json.JSONObject;
49 import org.junit.AfterClass;
50 import org.junit.Before;
51 import org.junit.BeforeClass;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.mockito.Mock;
55 import org.onap.dmaap.datarouter.authz.AuthorizationResponse;
56 import org.onap.dmaap.datarouter.authz.Authorizer;
57 import org.onap.dmaap.datarouter.provisioning.beans.Deleteable;
58 import org.onap.dmaap.datarouter.provisioning.beans.SubDelivery;
59 import org.onap.dmaap.datarouter.provisioning.beans.Subscription;
60 import org.onap.dmaap.datarouter.provisioning.beans.Updateable;
61 import org.onap.dmaap.datarouter.provisioning.utils.Poker;
62 import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;
63 import org.powermock.core.classloader.annotations.PowerMockIgnore;
64 import org.powermock.modules.junit4.PowerMockRunner;
65
66
67 @RunWith(PowerMockRunner.class)
68 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
69 public class SubscriptionServletTest extends DrServletTestBase {
70     private static EntityManagerFactory emf;
71     private static EntityManager em;
72     private SubscriptionServlet subscriptionServlet;
73     private final String URL= "https://172.100.0.5";
74     private final String USER = "user1";
75     private final String PASSWORD="password1";
76
77
78     @Mock
79     private HttpServletRequest request;
80     @Mock
81     private HttpServletResponse response;
82
83     private ListAppender<ILoggingEvent> listAppender;
84
85     @BeforeClass
86     public static void init() {
87         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
88         em = emf.createEntityManager();
89         System.setProperty(
90             "org.onap.dmaap.datarouter.provserver.properties",
91             "src/test/resources/h2Database.properties");
92     }
93
94     @AfterClass
95     public static void tearDownClass() {
96         em.clear();
97         em.close();
98         emf.close();
99     }
100
101     @Before
102     public void setUp() throws Exception {
103         listAppender = setTestLogger(SubscriptionServlet.class);
104         subscriptionServlet = new SubscriptionServlet();
105         setAuthoriserToReturnRequestIsAuthorized();
106         setPokerToNotCreateTimersWhenDeleteSubscriptionIsCalled();
107         setupValidAuthorisedRequest();
108         setUpValidSecurityOnHttpRequest();
109     }
110
111     @Test
112     public void Given_Request_Is_HTTP_DELETE_SC_Forbidden_Response_Is_Generated() throws Exception {
113         when(request.isSecure()).thenReturn(false);
114         subscriptionServlet.doDelete(request, response);
115         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
116         verifyEnteringExitCalled(listAppender);
117     }
118
119     @Test
120     public void Given_Request_Is_HTTP_DELETE_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception {
121         setBehalfHeader(null);
122         subscriptionServlet.doDelete(request, response);
123         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
124     }
125
126     @Test
127     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 {
128         when(request.getPathInfo()).thenReturn(null);
129         subscriptionServlet.doDelete(request, response);
130         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
131     }
132
133     @Test
134     public void Given_Request_Is_HTTP_DELETE_And_Subscription_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception {
135         when(request.getPathInfo()).thenReturn("/123");
136         subscriptionServlet.doDelete(request, response);
137         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
138     }
139
140     @Test
141     public void Given_Request_Is_HTTP_DELETE_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception {
142         setAuthoriserToReturnRequestNotAuthorized();
143         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", true, true);
144         subscriptionServlet.doDelete(request, response);
145         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
146     }
147
148     @Test
149     public void Given_Request_Is_HTTP_DELETE_And_Delete_On_Database_Fails_An_Internal_Server_Error_Is_Reported() throws Exception {
150         SubscriptionServlet subscriptionServlet = new SubscriptionServlet(){
151             public boolean doDelete(Deleteable deletable){
152                 return false;
153             }
154         };
155         subscriptionServlet.doDelete(request, response);
156         verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), anyString());
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), anyString());
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), anyString());
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), anyString());
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("/123");
184         subscriptionServlet.doGet(request, response);
185         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
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), anyString());
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), anyString());
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), anyString());
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), anyString());
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("/123");
229         subscriptionServlet.doPut(request, response);
230         verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), anyString());
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         when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription; version=1.0");
237         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", true, true);
238         JSONObject JSObject = buildRequestJsonObject();
239         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
240             public JSONObject getJSONfromInput(HttpServletRequest req) {
241                 JSONObject jo = new JSONObject();
242                 jo.put("name", "stub_name");
243                 jo.put("version", "2.0");
244                 jo.put("metadataOnly", true);
245                 jo.put("suspend", true);
246                 jo.put("delivery", JSObject);
247                 jo.put("follow_redirect", false);
248                 jo.put("decompress", true);
249                 jo.put("sync", true);
250                 jo.put("changeowner", true);
251                 return jo;
252             }
253         };
254         subscriptionServlet.doPut(request, response);
255         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
256     }
257
258     @Test
259     public void Given_Request_Is_HTTP_PUT_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() throws Exception {
260         when(request.getContentType()).thenReturn("stub_ContentType");
261         subscriptionServlet.doPut(request, response);
262         verify(response).sendError(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE), anyString());
263     }
264
265     @Test
266     public void Given_Request_Is_HTTP_PUT_And_Request_Contains_Badly_Formed_JSON_Then_Bad_Request_Response_Is_Generated() throws Exception {
267         when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription; version=1.0");
268         ServletInputStream inStream = mock(ServletInputStream.class);
269         when(request.getInputStream()).thenReturn(inStream);
270         subscriptionServlet.doPut(request, response);
271         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
272     }
273
274     @Test
275     public void Given_Request_Is_HTTP_PUT_And_Subscription_Object_Is_Invalid_Bad_Request_Response_Is_Generated() throws Exception {
276         when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription; version=1.0");
277         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
278             public JSONObject getJSONfromInput(HttpServletRequest req) {
279                 JSONObject jo = new JSONObject();
280                 return jo;
281             }
282         };
283         subscriptionServlet.doPut(request, response);
284         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
285     }
286
287     @Test
288     public void Given_Request_Is_HTTP_PUT_And_Subscriber_Modified_By_Different_Creator_Then_Bad_Request_Is_Generated() throws Exception {
289         when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn(null);
290         when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription; version=1.0");
291         JSONObject JSObject = buildRequestJsonObject();
292         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
293             public JSONObject getJSONfromInput(HttpServletRequest req) {
294                 JSONObject jo = new JSONObject();
295                 jo.put("name", "stub_name");
296                 jo.put("version", "2.0");
297                 jo.put("metadataOnly", true);
298                 jo.put("suspend", true);
299                 jo.put("privilegedSubscriber", true);
300                 jo.put("decompress", true);
301                 jo.put("delivery", JSObject);
302                 jo.put("follow_redirect", false);
303                 jo.put("subscriber", "differentSubscriber");
304                 jo.put("sync", true);
305                 return jo;
306             }
307         };
308         subscriptionServlet.doPut(request, response);
309         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
310     }
311
312     @Test
313     public void Given_Request_Is_HTTP_PUT_And_Update_Fails() throws Exception {
314         when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
315         when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription; version=1.0");
316         JSONObject JSObject = buildRequestJsonObject();
317         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
318             public JSONObject getJSONfromInput(HttpServletRequest req) {
319                 JSONObject jo = new JSONObject();
320                 jo.put("name", "stub_name");
321                 jo.put("version", "2.0");
322                 jo.put("metadataOnly", true);
323                 jo.put("suspend", true);
324                 jo.put("privilegedSubscriber", true);
325                 jo.put("delivery", JSObject);
326                 jo.put("decompress", true);
327                 jo.put("follow_redirect", false);
328                 jo.put("sync", true);
329                 return jo;
330             }
331
332             @Override
333             protected boolean doUpdate(Updateable bean) {
334                 return false;
335             }
336         };
337         subscriptionServlet.doPut(request, response);
338         verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), anyString());
339     }
340
341     @Test
342     public void Given_Request_Is_HTTP_PUT_And_Update_Succeeds() throws Exception {
343         ServletOutputStream outStream = mock(ServletOutputStream.class);
344         when(response.getOutputStream()).thenReturn(outStream);
345         when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
346         when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription; version=1.0");
347         JSONObject JSObject = buildRequestJsonObject();
348         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
349             public JSONObject getJSONfromInput(HttpServletRequest req) {
350                 JSONObject jo = new JSONObject();
351                 jo.put("name", "stub_name");
352                 jo.put("version", "2.0");
353                 jo.put("metadataOnly", true);
354                 jo.put("suspend", true);
355                 jo.put("privilegedSubscriber", true);
356                 jo.put("decompress", true);
357                 jo.put("delivery", JSObject);
358                 jo.put("follow_redirect", false);
359                 jo.put("sync", true);
360                 jo.put("changeowner", true);
361                 return jo;
362             }
363         };
364         subscriptionServlet.doPut(request, response);
365         verify(response).setStatus(eq(HttpServletResponse.SC_OK));
366         //changeSubscriptionBackToNormal();
367         verifyEnteringExitCalled(listAppender);
368     }
369
370     @Test
371     public void Given_Request_Is_HTTP_POST_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception {
372         when(request.isSecure()).thenReturn(false);
373         subscriptionServlet.doPost(request, response);
374         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
375         verifyEnteringExitCalled(listAppender);
376     }
377
378     @Test
379     public void Given_Request_Is_HTTP_POST_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception {
380         setBehalfHeader(null);
381         subscriptionServlet.doPost(request, response);
382         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
383     }
384
385     @Test
386     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 {
387         when(request.getPathInfo()).thenReturn(null);
388         subscriptionServlet.doPost(request, response);
389         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
390     }
391
392     @Test
393     public void Given_Request_Is_HTTP_POST_And_Subscription_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception {
394         when(request.getPathInfo()).thenReturn("/123");
395         subscriptionServlet.doPost(request, response);
396         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
397     }
398
399     @Test
400     public void Given_Request_Is_HTTP_POST_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() throws Exception {
401         when(request.getContentType()).thenReturn("stub_ContentType");
402         subscriptionServlet.doPost(request, response);
403         verify(response).sendError(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE), anyString());
404     }
405
406     @Test
407     public void Given_Request_Is_HTTP_POST_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception {
408         when(request.getHeader(anyString())).thenReturn("application/vnd.dmaap-dr.subscription-control");
409         setAuthoriserToReturnRequestNotAuthorized();
410         subscriptionServlet.doPost(request, response);
411         verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), anyString());
412     }
413
414     @Test
415     public void Given_Request_Is_HTTP_POST_And_Request_Contains_Badly_Formed_JSON_Then_Bad_Request_Response_Is_Generated() throws Exception {
416         when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription-control; version=1.0");
417         ServletInputStream inStream = mock(ServletInputStream.class);
418         when(request.getInputStream()).thenReturn(inStream);
419         subscriptionServlet.doPost(request, response);
420         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
421     }
422
423     @Test
424     public void Given_Request_Is_HTTP_POST_And_Post_Fails() throws Exception {
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             public 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                 return jo;
437             }
438         };
439         subscriptionServlet.doPost(request, response);
440         verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), anyString());
441     }
442
443     @Test
444     public void Given_Request_Is_HTTP_POST_And_Post_Succeeds() throws Exception {
445         ServletOutputStream outStream = mock(ServletOutputStream.class);
446         when(response.getOutputStream()).thenReturn(outStream);
447         when(request.getHeader("X-DMAAP-DR-ON-BEHALF-OF-GROUP")).thenReturn("stub_subjectGroup");
448         when(request.getHeader("Content-Type")).thenReturn("application/vnd.dmaap-dr.subscription-control; version=1.0");
449         JSONObject JSObject = buildRequestJsonObject();
450         SubscriptionServlet subscriptionServlet = new SubscriptionServlet() {
451             public JSONObject getJSONfromInput(HttpServletRequest req) {
452                 JSONObject jo = new JSONObject();
453                 jo.put("name", "stub_name");
454                 jo.put("version", "2.0");
455                 jo.put("metadataOnly", true);
456                 jo.put("suspend", true);
457                 jo.put("delivery", JSObject);
458                 jo.put("privilegedSubscriber", false);
459                 jo.put("follow_redirect", false);
460                 jo.put("decompress", false);
461                 jo.put("failed", false);
462                 return jo;
463             }
464         };
465         subscriptionServlet.doPost(request, response);
466         verify(response).setStatus(eq(HttpServletResponse.SC_ACCEPTED));
467         verifyEnteringExitCalled(listAppender);
468     }
469
470     @NotNull
471     private JSONObject buildRequestJsonObject() {
472         JSONObject JSObject = new JSONObject();
473         JSObject.put("url", "https://stub_address");
474         JSObject.put("use100", "true");
475         JSObject.put("password", "stub_password");
476         JSObject.put("user", "stub_user");
477         return JSObject;
478     }
479
480     private void setUpValidSecurityOnHttpRequest() throws Exception {
481         when(request.isSecure()).thenReturn(true);
482         Set<String> authAddressesAndNetworks = new HashSet<String>();
483         authAddressesAndNetworks.add(("127.0.0.1"));
484         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, true);
485         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true);
486     }
487
488     private void setBehalfHeader(String headerValue) {
489         when(request.getHeader(BEHALF_HEADER)).thenReturn(headerValue);
490     }
491
492     private void setValidPathInfoInHttpHeader() {
493         when(request.getPathInfo()).thenReturn("/1");
494     }
495
496     private void setAuthoriserToReturnRequestNotAuthorized() throws IllegalAccessException {
497         AuthorizationResponse authResponse = mock(AuthorizationResponse.class);
498         Authorizer authorizer = mock(Authorizer.class);
499         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true);
500         when(authorizer.decide(request)).thenReturn(authResponse);
501         when(authResponse.isAuthorized()).thenReturn(false);
502     }
503
504     private void setAuthoriserToReturnRequestIsAuthorized() throws IllegalAccessException {
505         AuthorizationResponse authResponse = mock(AuthorizationResponse.class);
506         Authorizer authorizer = mock(Authorizer.class);
507         FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authz", authorizer, true);
508         when(authorizer.decide(request)).thenReturn(authResponse);
509         when(authResponse.isAuthorized()).thenReturn(true);
510     }
511
512     private void setPokerToNotCreateTimersWhenDeleteSubscriptionIsCalled() throws Exception {
513         Poker poker = mock(Poker.class);
514         FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true);
515     }
516
517     private void setupValidAuthorisedRequest() throws Exception {
518         setUpValidSecurityOnHttpRequest();
519         setBehalfHeader("Stub_Value");
520         setValidPathInfoInHttpHeader();
521     }
522
523     private void changeSubscriptionBackToNormal() throws SQLException {
524         Subscription subscription = new Subscription("https://172.100.0.5", "user1", "password1");
525         subscription.setSubid(1);
526         subscription.setSubscriber("user1");
527         subscription.setFeedid(1);
528         SubDelivery subDelivery = new SubDelivery(URL, USER, PASSWORD, true);
529         subscription.setDelivery(subDelivery);
530         subscription.setGroupid(1);
531         subscription.setMetadataOnly(false);
532         subscription.setSuspended(false);
533         subscription.setPrivilegedSubscriber(false);
534         subscription.setDecompress(false);
535         subscription.changeOwnerShip();
536         try (Connection conn = ProvDbUtils.getInstance().getConnection()) {
537             subscription.doUpdate(conn);
538         }
539     }
540
541     private void resetAafSubscriptionInDB() throws SQLException {
542         Subscription subscription = new Subscription("https://172.100.0.5:8080", "user2", "password2");
543         subscription.setSubid(2);
544         subscription.setSubscriber("user2");
545         subscription.setFeedid(1);
546         SubDelivery subDelivery = new SubDelivery(URL, USER, PASSWORD, true);
547         subscription.setDelivery(subDelivery);
548         subscription.setGroupid(1);
549         subscription.setMetadataOnly(false);
550         subscription.setSuspended(false);
551         subscription.setDecompress(false);
552         subscription.setPrivilegedSubscriber(false);
553         try (Connection conn = ProvDbUtils.getInstance().getConnection()) {
554             subscription.doUpdate(conn);
555         }
556     }
557
558     private void addNewSubscriptionInDB() throws SQLException {
559         Subscription subscription = new Subscription("https://172.100.0.6:8080", "user3", "password3");
560         subscription.setSubid(3);
561         subscription.setSubscriber("user3");
562         subscription.setFeedid(1);
563         SubDelivery subDelivery = new SubDelivery(URL, USER, PASSWORD, true);
564         subscription.setDelivery(subDelivery);
565         subscription.setGroupid(1);
566         subscription.setMetadataOnly(false);
567         subscription.setSuspended(false);
568         subscription.setDecompress(false);
569         try (Connection conn = ProvDbUtils.getInstance().getConnection()) {
570             subscription.doInsert(conn);
571         }
572     }
573 }