1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
24 package org.onap.dmaap.datarouter.provisioning.utils;
26 import static org.hamcrest.core.Is.is;
27 import static org.junit.Assert.assertThat;
28 import static org.mockito.Matchers.any;
29 import static org.mockito.Matchers.anyString;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
40 import javax.servlet.FilterChain;
41 import javax.servlet.FilterConfig;
42 import javax.servlet.ServletInputStream;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45 import org.apache.commons.lang3.reflect.FieldUtils;
46 import org.eclipse.jetty.continuation.Continuation;
47 import org.eclipse.jetty.continuation.ContinuationSupport;
48 import org.eclipse.jetty.io.EndPoint;
49 import org.eclipse.jetty.server.HttpChannel;
50 import org.eclipse.jetty.server.HttpConnection;
51 import org.eclipse.jetty.server.Request;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.mockito.Mock;
55 import org.onap.dmaap.datarouter.provisioning.beans.Parameters;
56 import org.powermock.api.mockito.PowerMockito;
57 import org.powermock.core.classloader.annotations.PowerMockIgnore;
58 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
59 import org.powermock.modules.junit4.PowerMockRunner;
61 @RunWith(PowerMockRunner.class)
62 @SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Parameters",
63 "org.eclipse.jetty.server.Request",
64 "org.eclipse.jetty.continuation.ContinuationSupport",
65 "org.eclipse.jetty.server.HttpConnection"})
66 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})
67 public class ThrottleFilterTest {
70 private HttpServletRequest request;
73 private HttpServletResponse response;
76 private FilterChain filterchain;
79 private HttpConnection httpconnection;
82 private ContinuationSupport continuationsupport;
88 private HttpChannel httpchannel;
91 private Continuation continuation;
96 public void Given_Throttle_Filter_Configure_And_Parameter_Is_Not_Null_Then_Enabled_And_Action_Is_True() throws Exception {
97 mockParametersWithValues("2,5,throttle");
98 ThrottleFilter.configure();
99 boolean enabled = (boolean) FieldUtils.readStaticField(ThrottleFilter.class, "enabled", true);
100 int action = (int) FieldUtils.readStaticField(ThrottleFilter.class, "action", true);
101 assertThat(enabled, is(true));
102 assertThat(action, is(0));
107 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 {
108 mockParametersWithValues("100,5,thing");
109 ThrottleFilter throttlefilter = new ThrottleFilter();
110 ThrottleFilter.configure();
111 mockServletInputStream();
112 FieldUtils.writeDeclaredStaticField(ThrottleFilter.class, "action", 1, true);
113 Map<String, List<Continuation>> suspended_requests = new HashMap<String, List<Continuation>>();
114 List<Continuation> continuation_list = new ArrayList<>();
115 continuation_list.add(continuation);
116 suspended_requests.put("null/-1", continuation_list);
117 FieldUtils.writeDeclaredField(throttlefilter, "suspendedRequests", suspended_requests, true);
118 throttlefilter.doFilter(request, response, filterchain);
119 verify(continuation, times(1)).setAttribute(anyString(), any());
120 verify(continuation, times(1)).resume();
124 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 {
125 mockParametersWithValues("0,5,thing");
126 mockContinuationSupport();
127 ThrottleFilter.configure();
128 mockServletInputStream();
129 FieldUtils.writeDeclaredStaticField(ThrottleFilter.class, "action", 1, true);
130 ThrottleFilter throttlefilter = new ThrottleFilter();
131 throttlefilter.doFilter(request, response, filterchain);
132 verify(continuation, times(1)).undispatch();
133 verify(continuation, times(1)).suspend();
138 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 {
139 mockParametersWithValues("0,5,thing");
140 PowerMockito.mockStatic(ContinuationSupport.class);
141 PowerMockito.when(ContinuationSupport.getContinuation(any())).thenReturn(continuation);
142 ThrottleFilter.configure();
143 mockServletInputStream();
144 FieldUtils.writeDeclaredStaticField(ThrottleFilter.class, "action", 1, true);
145 ThrottleFilter throttlefilter = new ThrottleFilter();
146 when(request.getPathInfo()).thenReturn("/123/fileName.txt");
147 throttlefilter.doFilter(request, response, filterchain);
148 verify(continuation, times(1)).undispatch();
149 verify(continuation, times(1)).suspend();
153 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 {
154 mockParametersWithValues("0,5,thing");
155 ServletInputStream serverinputstream = mock(ServletInputStream.class);
156 mockHttpConnectionHttpChannelAndRequest(serverinputstream);
157 ThrottleFilter.configure();
158 FieldUtils.writeDeclaredStaticField(ThrottleFilter.class, "action", 0, true);
159 ThrottleFilter throttlefilter = new ThrottleFilter();
160 throttlefilter.doFilter(request, response, filterchain);
161 verify(req, times(1)).getHttpChannel();
162 verify(httpchannel, times(1)).getEndPoint();
166 public void Given_run_is_called_then_continuation_will_call_prune_once() throws Exception {
167 ThrottleFilter tf = new ThrottleFilter();
168 Map<String, ThrottleFilter.Counter> map = new HashMap<String, ThrottleFilter.Counter>();
169 ThrottleFilter.Counter tfc = mock(ThrottleFilter.Counter.class);
171 when(tfc.prune()).thenReturn(-1);
172 FieldUtils.writeDeclaredField(tf, "map", map, true);
174 verify(tfc, times(1)).prune();
178 public void Given_destroy_is_called_then_map_is_empty() throws Exception
180 ThrottleFilter throttleFilter = new ThrottleFilter();
181 FilterConfig filterconfig = mock(FilterConfig.class);
182 mockParametersWithValues("0,5,thing");
183 PowerMockito.mockStatic(ContinuationSupport.class);
184 PowerMockito.when(ContinuationSupport.getContinuation(any())).thenReturn(continuation);
186 throttleFilter.init(filterconfig);
187 throttleFilter.destroy();
190 private Parameters getParameters() {
191 PowerMockito.mockStatic(Parameters.class);
192 return mock(Parameters.class);
195 private void mockServletInputStream() throws IOException {
196 ServletInputStream serverinputstream = mock(ServletInputStream.class);
197 when(serverinputstream.read(any())).thenReturn(2).thenReturn(1).thenReturn(0);
198 when(request.getInputStream()).thenReturn(serverinputstream);
201 private void mockParametersWithValues(String values) {
202 Parameters parameters = getParameters();
203 PowerMockito.when(parameters.getParameter(anyString())).thenReturn(new Parameters("key", values));
206 private void mockContinuationSupport() {
207 PowerMockito.mockStatic(ContinuationSupport.class);
208 PowerMockito.when(ContinuationSupport.getContinuation(any())).thenReturn(continuation);
211 private void mockHttpConnectionHttpChannelAndRequest(ServletInputStream serverinputstream) throws IOException {
212 PowerMockito.mockStatic(ContinuationSupport.class);
213 PowerMockito.when(ContinuationSupport.getContinuation(any())).thenReturn(continuation);
214 when(serverinputstream.read(any())).thenReturn(2).thenReturn(1).thenReturn(0);
215 when(request.getInputStream()).thenReturn(serverinputstream);
216 PowerMockito.mockStatic(HttpConnection.class);
217 EndPoint endpoint = mock(EndPoint.class);
218 PowerMockito.when(HttpConnection.getCurrentConnection()).thenReturn(httpconnection);
219 PowerMockito.when(httpconnection.getHttpChannel()).thenReturn(httpchannel);
220 when(httpchannel.getRequest()).thenReturn(req);
221 when(req.getHttpChannel()).thenReturn(httpchannel);
222 when(httpchannel.getEndPoint()).thenReturn(endpoint);