View Javadoc

1   package com.atlassian.sal.testresources.net;
2   
3   import com.atlassian.sal.api.net.Request;
4   import com.atlassian.sal.api.net.RequestFilePart;
5   import com.atlassian.sal.api.net.Response;
6   import com.atlassian.sal.api.net.ResponseException;
7   import com.atlassian.sal.api.net.ResponseHandler;
8   import com.atlassian.sal.api.net.ReturningResponseHandler;
9   import com.atlassian.sal.api.net.auth.Authenticator;
10  
11  import java.util.ArrayList;
12  import java.util.Arrays;
13  import java.util.HashMap;
14  import java.util.List;
15  import java.util.Map;
16  
17  /**
18   * Mock request that provides getters to all the information that is passed in, and also setters for the
19   * response body that should be returned for execute(), or the response that should be passed to the
20   * response handler.
21   */
22  public class MockRequest implements Request<MockRequest, MockResponse>
23  {
24      private final Request.MethodType methodType;
25      private String url;
26      private int connectionTimeout;
27      private int soTimeout;
28      private String requestBody;
29      private String requestContentType;
30      private final Map<String, List<String>> headers = new HashMap<String, List<String>>();
31      private final List<String> requestParameters = new ArrayList<String>();
32      private final List<Authenticator> authenticators = new ArrayList<Authenticator>();
33      private boolean trustedTokenAuthentication;
34      private String trustedTokenUser;
35      private String basicUser;
36      private String basicPassword;
37      private String seraphUser;
38      private String seraphPassword;
39      private MockResponse response;
40      private String responseBody;
41      private boolean followRedirects = true;
42  
43      public MockRequest(final MethodType methodType, final String url)
44      {
45          this.methodType = methodType;
46          this.url = url;
47      }
48  
49      public MockRequest setConnectionTimeout(final int connectionTimeout)
50      {
51          this.connectionTimeout = connectionTimeout;
52          return this;
53      }
54  
55      public MockRequest setSoTimeout(final int soTimeout)
56      {
57          this.soTimeout = soTimeout;
58          return this;
59      }
60  
61      public MockRequest setUrl(final String url)
62      {
63          this.url = url;
64          return this;
65      }
66  
67      public MockRequest setRequestBody(final String requestBody)
68      {
69          this.requestBody = requestBody;
70          return this;
71      }
72  
73      public MockRequest setFiles(final List<RequestFilePart> files)
74      {
75          return null;
76      }
77  
78      public MockRequest setEntity(Object entity)
79      {
80          throw new UnsupportedOperationException();
81      }
82  
83      public MockRequest setRequestContentType(final String contentType)
84      {
85          this.requestContentType = contentType;
86          return this;
87      }
88  
89      public MockRequest addRequestParameters(final String... params)
90      {
91          requestParameters.addAll(Arrays.asList(params));
92          return this;
93      }
94  
95      public MockRequest addHeader(final String headerName, final String headerValue)
96      {
97          List<String> list = headers.get(headerName);
98          if (list == null)
99          {
100             list = new ArrayList<String>();
101             headers.put(headerName, list);
102         }
103         list.add(headerValue);
104         return this;
105     }
106     
107     public MockRequest setHeader(final String headerName, final String headerValue)
108     {
109         headers.put(headerName, new ArrayList<String>(Arrays.asList(headerValue)));
110         return this;
111     }
112     
113     public MockRequest setFollowRedirects(boolean follow)
114     {
115         this.followRedirects  = follow;
116         return this;
117     }
118     public MockRequest addAuthentication(final Authenticator authenticator)
119     {
120         authenticators.add(authenticator);
121         return this;
122     }
123 
124     public MockRequest addTrustedTokenAuthentication()
125     {
126         trustedTokenAuthentication = true;
127         return this;
128     }
129 
130     public MockRequest addTrustedTokenAuthentication(final String username)
131     {
132         trustedTokenAuthentication = true;
133         trustedTokenUser = username;
134         return this;
135     }
136 
137     public MockRequest addBasicAuthentication(final String username, final String password)
138     {
139         basicUser = username;
140         basicPassword = password;
141         return this;
142     }
143 
144     public MockRequest addSeraphAuthentication(final String username, final String password)
145     {
146         seraphUser = username;
147         seraphPassword = password;
148         return this;
149     }
150 
151     public <RET> RET executeAndReturn(ReturningResponseHandler<MockResponse, RET> responseHandler) throws ResponseException
152     {
153         if (response == null)
154         {
155             response = new MockResponse();
156         }
157         return responseHandler.handle(response);
158     }
159 
160     public void execute(final ResponseHandler responseHandler) throws ResponseException
161     {
162         if (response == null)
163         {
164             response = new MockResponse();
165         }
166         responseHandler.handle(response);
167     }
168 
169     public String execute() throws ResponseException
170     {
171         return responseBody;
172     }
173 
174     public MethodType getMethodType()
175     {
176         return methodType;
177     }
178 
179     public String getUrl()
180     {
181         return url;
182     }
183 
184     public int getConnectionTimeout()
185     {
186         return connectionTimeout;
187     }
188 
189     public int getSoTimeout()
190     {
191         return soTimeout;
192     }
193 
194     public String getRequestBody()
195     {
196         return requestBody;
197     }
198 
199     public String getRequestContentType()
200     {
201         return requestContentType;
202     }
203 
204     public List<String> getRequestParameters()
205     {
206         return requestParameters;
207     }
208 
209     public Map<String, List<String>> getHeaders()
210     {
211         return headers;
212     }
213 
214     public List<String> getHeader(final String headerName)
215     {
216         return headers.get(headerName);
217     }
218 
219     public List<Authenticator> getAuthenticators()
220     {
221         return authenticators;
222     }
223 
224     public boolean isTrustedTokenAuthentication()
225     {
226         return trustedTokenAuthentication;
227     }
228 
229     public String getTrustedTokenUser()
230     {
231         return trustedTokenUser;
232     }
233 
234     public String getBasicUser()
235     {
236         return basicUser;
237     }
238 
239     public String getBasicPassword()
240     {
241         return basicPassword;
242     }
243 
244     public String getSeraphUser()
245     {
246         return seraphUser;
247     }
248 
249     public String getSeraphPassword()
250     {
251         return seraphPassword;
252     }
253 
254     public Response getResponse()
255     {
256         return response;
257     }
258 
259     public void setResponse(final MockResponse response)
260     {
261         this.response = response;
262     }
263 
264     public void setResponseBody(final String responseBody)
265     {
266         this.responseBody = responseBody;
267     }
268 }