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