Removing code smells
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / utils / ThrottleFilterTest.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
24 package org.onap.dmaap.datarouter.provisioning.utils;
25
26 import org.apache.commons.lang3.reflect.FieldUtils;
27 import org.eclipse.jetty.continuation.Continuation;
28 import org.eclipse.jetty.continuation.ContinuationSupport;
29 import org.eclipse.jetty.io.EndPoint;
30 import org.eclipse.jetty.server.HttpChannel;
31 import org.eclipse.jetty.server.HttpConnection;
32 import org.eclipse.jetty.server.Request;
33 import org.junit.Test;
34
35 import org.junit.runner.RunWith;
36 import static org.junit.Assert.*;
37 import static org.hamcrest.core.Is.is;
38 import org.mockito.Mock;
39
40 import static org.mockito.Matchers.*;
41 import static org.mockito.Mockito.*;
42
43 import org.onap.dmaap.datarouter.provisioning.beans.Parameters;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
46 import org.powermock.modules.junit4.PowerMockRunner;
47
48 import javax.servlet.FilterChain;
49 import javax.servlet.FilterConfig;
50 import javax.servlet.ServletInputStream;
51 import javax.servlet.http.HttpServletRequest;
52 import javax.servlet.http.HttpServletResponse;
53 import java.io.IOException;
54 import java.util.*;
55
56 @RunWith(PowerMockRunner.class)
57 @SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Parameters",
58                                   "org.eclipse.jetty.server.Request",
59                                   "org.eclipse.jetty.continuation.ContinuationSupport",
60                                   "org.eclipse.jetty.server.HttpConnection"})
61 public class ThrottleFilterTest {
62
63     @Mock
64     private HttpServletRequest request;
65
66     @Mock
67     private HttpServletResponse response;
68
69     @Mock
70     private FilterChain filterchain;
71
72     @Mock
73     private HttpConnection httpconnection;
74
75     @Mock
76     private ContinuationSupport continuationsupport;
77
78     @Mock
79     private Request req;
80
81     @Mock
82     private HttpChannel httpchannel;
83
84     @Mock
85     private Continuation continuation;
86
87
88
89     @Test
90     public void Given_Throttle_Filter_Configure_And_Parameter_Is_Not_Null_Then_Enabled_And_Action_Is_True() throws Exception {
91         mockParametersWithValues("2,5,throttle");
92         ThrottleFilter.configure();
93         boolean enabled = (boolean) FieldUtils.readStaticField(ThrottleFilter.class, "enabled", true);
94         int action = (int) FieldUtils.readStaticField(ThrottleFilter.class, "action", true);
95         assertThat(enabled, is(true));
96         assertThat(action, is(0));
97
98     }
99
100     @Test
101     public void Given_Do_Filter_run_and_enabled_and_action_is_true_and_rate_is_0_then_continiuation_will_call_setAttribute_and_resume_once() throws Exception {
102         mockParametersWithValues("100,5,thing");
103         ThrottleFilter throttlefilter = new ThrottleFilter();
104         ThrottleFilter.configure();
105         mockServletInputStream();
106         FieldUtils.writeDeclaredStaticField(ThrottleFilter.class, "action", 1, true);
107         Map<String, List<Continuation>> suspended_requests = new HashMap<String, List<Continuation>>();
108         List<Continuation> continuation_list = new ArrayList<>();
109         continuation_list.add(continuation);
110         suspended_requests.put("null/-1", continuation_list);
111         FieldUtils.writeDeclaredField(throttlefilter, "suspendedRequests", suspended_requests, true);
112         throttlefilter.doFilter(request, response, filterchain);
113         verify(continuation, times(1)).setAttribute(anyString(), any());
114         verify(continuation, times(1)).resume();
115     }
116
117     @Test
118     public void Given_Do_Filter_Run_and_enabled_and_action_is_true_and_rate_is_greater_than_0_then_continuation_will_call_suspend_and_dispatch_once() throws Exception {
119         mockParametersWithValues("0,5,thing");
120         mockContinuationSupport();
121         ThrottleFilter.configure();
122         mockServletInputStream();
123         FieldUtils.writeDeclaredStaticField(ThrottleFilter.class, "action", 1, true);
124         ThrottleFilter throttlefilter = new ThrottleFilter();
125         throttlefilter.doFilter(request, response, filterchain);
126         verify(continuation, times(1)).undispatch();
127         verify(continuation, times(1)).suspend();
128     }
129
130
131     @Test
132     public void Given_Do_Filter_Run_and_enabled_and_action_is_true_and_rate_is_greater_than_0_and_getFeedId_returns_id_then_continuation_will_call_suspend_and_dispatch_once() throws Exception {
133         mockParametersWithValues("0,5,thing");
134         PowerMockito.mockStatic(ContinuationSupport.class);
135         PowerMockito.when(continuationsupport.getContinuation(any())).thenReturn(continuation);
136         ThrottleFilter.configure();
137         mockServletInputStream();
138         FieldUtils.writeDeclaredStaticField(ThrottleFilter.class, "action", 1, true);
139         ThrottleFilter throttlefilter = new ThrottleFilter();
140         when(request.getPathInfo()).thenReturn("/123/fileName.txt");
141         throttlefilter.doFilter(request, response, filterchain);
142         verify(continuation, times(1)).undispatch();
143         verify(continuation, times(1)).suspend();
144     }
145
146     @Test
147     public void Given_Do_Filter_and_only_enabled_is_true_and_drop_filter_ran_then_request_will_call_getHttpChannel_and_HttpChannel_will_call_getEndPoint_once() throws Exception {
148         mockParametersWithValues("0,5,thing");
149         ServletInputStream serverinputstream = mock(ServletInputStream.class);
150         mockHttpConnectionHttpChannelAndRequest(serverinputstream);
151         ThrottleFilter.configure();
152         FieldUtils.writeDeclaredStaticField(ThrottleFilter.class, "action", 0, true);
153         ThrottleFilter throttlefilter = new ThrottleFilter();
154         throttlefilter.doFilter(request, response, filterchain);
155         verify(req, times(1)).getHttpChannel();
156         verify(httpchannel, times(1)).getEndPoint();
157     }
158
159     @Test
160     public void Given_run_is_called_then_continuation_will_call_prune_once() throws Exception {
161         ThrottleFilter tf = new ThrottleFilter();
162         Map<String, ThrottleFilter.Counter> map = new HashMap<String, ThrottleFilter.Counter>();
163         ThrottleFilter.Counter tfc = mock(ThrottleFilter.Counter.class);
164         map.put("Key", tfc);
165         when(tfc.prune()).thenReturn(-1);
166         FieldUtils.writeDeclaredField(tf, "map", map, true);
167         tf.run();
168         verify(tfc, times(1)).prune();
169     }
170
171     @Test
172     public void Given_destroy_is_called_then_map_is_empty() throws Exception
173     {
174         ThrottleFilter throttleFilter = new ThrottleFilter();
175         FilterConfig filterconfig = mock(FilterConfig.class);
176         mockParametersWithValues("0,5,thing");
177         PowerMockito.mockStatic(ContinuationSupport.class);
178         PowerMockito.when(continuationsupport.getContinuation(any())).thenReturn(continuation);
179
180         throttleFilter.init(filterconfig);
181         throttleFilter.destroy();
182     }
183
184     private Parameters getParameters() {
185         PowerMockito.mockStatic(Parameters.class);
186         return mock(Parameters.class);
187     }
188
189     private void mockServletInputStream() throws IOException {
190         ServletInputStream serverinputstream = mock(ServletInputStream.class);
191         when(serverinputstream.read(any())).thenReturn(2).thenReturn(1).thenReturn(0);
192         when(request.getInputStream()).thenReturn(serverinputstream);
193     }
194
195     private void mockParametersWithValues(String values) {
196         Parameters parameters = getParameters();
197         PowerMockito.when(parameters.getParameter(anyString())).thenReturn(new Parameters("key", values));
198     }
199
200     private void mockContinuationSupport() {
201         PowerMockito.mockStatic(ContinuationSupport.class);
202         PowerMockito.when(continuationsupport.getContinuation(any())).thenReturn(continuation);
203     }
204
205     private void mockHttpConnectionHttpChannelAndRequest(ServletInputStream serverinputstream) throws IOException {
206         PowerMockito.mockStatic(ContinuationSupport.class);
207         PowerMockito.when(continuationsupport.getContinuation(any())).thenReturn(continuation);
208         when(serverinputstream.read(any())).thenReturn(2).thenReturn(1).thenReturn(0);
209         when(request.getInputStream()).thenReturn(serverinputstream);
210         PowerMockito.mockStatic(HttpConnection.class);
211         EndPoint endpoint = mock(EndPoint.class);
212         PowerMockito.when(httpconnection.getCurrentConnection()).thenReturn(httpconnection);
213         PowerMockito.when(httpconnection.getHttpChannel()).thenReturn(httpchannel);
214         when(httpchannel.getRequest()).thenReturn(req);
215         when(req.getHttpChannel()).thenReturn(httpchannel);
216         when(httpchannel.getEndPoint()).thenReturn(endpoint);
217     }
218 }