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      private final Request.MethodType methodType;
23      private String url;
24      private int connectionTimeout;
25      private int soTimeout;
26      private String requestBody;
27      private String requestContentType;
28      private final Map<String, List<String>> headers = new HashMap<>();
29      private final List<String> requestParameters = new ArrayList<>();
30      private String basicHost;
31      private String basicUser;
32      private String basicPassword;
33      private MockResponse response;
34      private String responseBody;
35      private boolean followRedirects = true;
36  
37      public MockRequest(final MethodType methodType, final String url) {
38          this.methodType = methodType;
39          this.url = url;
40      }
41  
42      public MockRequest setConnectionTimeout(final int connectionTimeout) {
43          this.connectionTimeout = connectionTimeout;
44          return this;
45      }
46  
47      public MockRequest setSoTimeout(final int soTimeout) {
48          this.soTimeout = soTimeout;
49          return this;
50      }
51  
52      public MockRequest setUrl(final String url) {
53          this.url = url;
54          return this;
55      }
56  
57      public MockRequest setRequestBody(final String requestBody) {
58          this.requestBody = requestBody;
59          return this;
60      }
61  
62      @Override
63      public MockRequest setRequestBody(final String requestBody, final String contentType) {
64          this.requestBody = requestBody;
65          this.requestContentType = contentType;
66          return this;
67      }
68  
69      public MockRequest setFiles(final List<RequestFilePart> files) {
70          return null;
71      }
72  
73      public MockRequest setEntity(Object entity) {
74          throw new UnsupportedOperationException();
75      }
76  
77      public MockRequest setRequestContentType(final String contentType) {
78          this.requestContentType = contentType;
79          return this;
80      }
81  
82      public MockRequest addRequestParameters(final String... params) {
83          requestParameters.addAll(Arrays.asList(params));
84          return this;
85      }
86  
87      public MockRequest addHeader(final String headerName, final String headerValue) {
88          List<String> list = headers.get(headerName);
89          if (list == null) {
90              list = new ArrayList<String>();
91              headers.put(headerName, list);
92          }
93          list.add(headerValue);
94          return this;
95      }
96  
97      public MockRequest setHeader(final String headerName, final String headerValue) {
98          headers.put(headerName, new ArrayList<String>(Arrays.asList(headerValue)));
99          return this;
100     }
101 
102     public MockRequest setFollowRedirects(boolean follow) {
103         this.followRedirects = follow;
104         return this;
105     }
106 
107     public MockRequest addBasicAuthentication(final String host, final String username, final String password) {
108         basicHost = host;
109         basicUser = username;
110         basicPassword = password;
111         return this;
112     }
113 
114     public <RET> RET executeAndReturn(ReturningResponseHandler<? super MockResponse, RET> responseHandler) throws ResponseException {
115         if (response == null) {
116             response = new MockResponse();
117         }
118         return responseHandler.handle(response);
119     }
120 
121     public void execute(final ResponseHandler<? super MockResponse> responseHandler) throws ResponseException {
122         if (response == null) {
123             response = new MockResponse();
124         }
125         responseHandler.handle(response);
126     }
127 
128     public String execute() throws ResponseException {
129         return responseBody;
130     }
131 
132     public MethodType getMethodType() {
133         return methodType;
134     }
135 
136     public String getUrl() {
137         return url;
138     }
139 
140     public int getConnectionTimeout() {
141         return connectionTimeout;
142     }
143 
144     public int getSoTimeout() {
145         return soTimeout;
146     }
147 
148     public String getRequestBody() {
149         return requestBody;
150     }
151 
152     public String getRequestContentType() {
153         return requestContentType;
154     }
155 
156     public List<String> getRequestParameters() {
157         return requestParameters;
158     }
159 
160     public Map<String, List<String>> getHeaders() {
161         return headers;
162     }
163 
164     public List<String> getHeader(final String headerName) {
165         return headers.get(headerName);
166     }
167 
168     public String getBasicHost() {
169         return basicHost;
170     }
171 
172     public String getBasicUser() {
173         return basicUser;
174     }
175 
176     public String getBasicPassword() {
177         return basicPassword;
178     }
179 
180     public Response getResponse() {
181         return response;
182     }
183 
184     public void setResponse(final MockResponse response) {
185         this.response = response;
186     }
187 
188     public void setResponseBody(final String responseBody) {
189         this.responseBody = responseBody;
190     }
191 }