callback);
176 |
177 | @GET("/v1/requests/{request_id}/map")
178 | RequestMap getRequestMap(@Path("request_id") String requestId);
179 |
180 | /**
181 | * The User Activity endpoint returns a limited amount of data about a user's lifetime activity with Uber.
182 | * The response will include pickup and dropoff times, the distance of past requests, and information about
183 | * which products were requested.
184 | * The history array in the response will have a maximum length based on the limit parameter.
185 | * The response value count may exceed limit, therefore subsequent API requests may be necessary.
186 | *
187 | * @param offset Offset the list of returned results by this amount. Default is zero.
188 | * @param limit Number of items to retrieve. Default is 5, maximum is 50.
189 | * @return
190 | */
191 | @GET("/v1.1/history")
192 | UserActivity getUserActivity(@Query("offset") Integer offset, @Query("limit") Integer limit);
193 |
194 | /**
195 | * @see #getUserActivity(Integer, Integer)
196 | */
197 | @GET("/v1.1/history")
198 | void getUserActivity(@Query("offset") Integer offset, @Query("limit") Integer limit, Callback callback);
199 | }
200 |
--------------------------------------------------------------------------------
/src/test/java/com/victorsima/uber/test/SandboxServerTest.java:
--------------------------------------------------------------------------------
1 | package com.victorsima.uber.test;
2 |
3 | import com.gargoylesoftware.htmlunit.*;
4 | import com.gargoylesoftware.htmlunit.html.*;
5 | import com.gargoylesoftware.htmlunit.util.WebConnectionWrapper;
6 | import com.squareup.okhttp.Response;
7 | import com.victorsima.uber.UberAuthService;
8 | import com.victorsima.uber.Utils;
9 | import com.victorsima.uber.model.AccessToken;
10 | import com.victorsima.uber.model.Products;
11 | import com.victorsima.uber.model.UserActivity;
12 | import com.victorsima.uber.model.request.Request;
13 | import com.victorsima.uber.model.request.RequestBody;
14 | import com.victorsima.uber.model.request.RequestMap;
15 | import com.victorsima.uber.model.request.SurgeConfirmationError;
16 | import com.victorsima.uber.model.sandbox.SandboxProductBody;
17 | import com.victorsima.uber.model.sandbox.SandboxRequestBody;
18 | import org.junit.Before;
19 | import org.junit.Test;
20 | import retrofit.RetrofitError;
21 |
22 | import java.io.IOException;
23 | import java.net.URL;
24 | import java.net.URLDecoder;
25 | import java.util.Collections;
26 |
27 | import static org.junit.Assert.*;
28 |
29 | /**
30 | * Tests for requests using uber sandbox
31 | *
32 | */
33 | @SuppressWarnings("unchecked")
34 | public class SandboxServerTest extends BaseTest {
35 |
36 | private static final int delay = 3000;// use a delay to allow the PUT calls to take effect
37 |
38 | @Override
39 | public boolean runUsingSandboxServer() {
40 | return true;
41 | }
42 |
43 | @Override
44 | @Before
45 | public void setup() throws Exception {
46 | super.setup();
47 | if (!client.hasAccessToken()) {
48 | retrieveAccessToken();
49 | }
50 | }
51 |
52 | @Test
53 | public void testAccessTokenRefresh() {
54 | String oldAccessToken = client.getAccessToken();
55 | AccessToken refreshedAccessToken = client.getAuthService().requestAccessToken(clientId, clientSecret,
56 | client.getClientRedirectUri(), UberAuthService.GRANT_TYPE_REFRESH_TOKEN, null, client.getRefreshToken());
57 | assertNotNull(refreshedAccessToken);
58 | assertNotEquals("The access token was not refreshed", oldAccessToken, refreshedAccessToken.getAccessToken());
59 | }
60 |
61 | @Test
62 | public void testHistory() throws Exception {
63 | UserActivity userActivity = client.getApiService().getUserActivity(null, null);
64 | assertNotNull("User Activity is null", userActivity);
65 | assertNotNull(userActivity.getHistory());
66 | assertTrue(userActivity.getHistory().size() > 0);
67 | }
68 |
69 | @Test
70 | public void testRiderCancelRequestStatus() throws Exception {
71 | Request request = getRequest();
72 | client.getApiService().deleteRequest(request.getRequestId());
73 | Thread.sleep(delay);
74 | request = client.getApiService().getRequestDetails(request.getRequestId());
75 | assertEquals(request.getStatus(), RequestBody.Status.RIDER_CANCELED);
76 | }
77 |
78 | @Test
79 | public void testNoDriverAvailableRequestStatus() throws Exception {
80 | Products products = client.getApiService().getProducts(Double.parseDouble(latitude), Double.parseDouble(longitude));
81 | String productId = products.getProducts().get(0).getProductId();
82 | client.getSandboxService().putProducts(productId, new SandboxProductBody(null, false));
83 | Thread.sleep(delay);
84 | try {
85 | Request request = client.getApiService().postRequest(new RequestBody(productId,
86 | Double.parseDouble(startLatitude),
87 | Double.parseDouble(startLongitude),
88 | Double.parseDouble(endLatitude),
89 | Double.parseDouble(endLongitude),
90 | null));
91 | } catch (RetrofitError re) {
92 | assertEquals(re.getResponse().getStatus(), 409);
93 | assertEquals(re.getResponse().getReason(), RequestBody.Status.NO_DRIVERS_AVAILABLE.value());
94 | }
95 |
96 | }
97 |
98 | @Test
99 | public void testRequestStatus() throws Exception {
100 | Request request = getRequest();
101 | Thread.sleep(delay);
102 | /**
103 | * processing is the default state and cannot be updated, simply check status
104 | */
105 | request = client.getApiService().getRequestDetails(request.getRequestId());
106 | assertEquals(request.getStatus(), RequestBody.Status.PROCESSING);
107 |
108 | client.getSandboxService().putRequest(request.getRequestId(), new SandboxRequestBody(RequestBody.Status.ACCEPTED));
109 | Thread.sleep(delay);
110 | request = client.getApiService().getRequestDetails(request.getRequestId());
111 | assertEquals(request.getStatus(), RequestBody.Status.ACCEPTED);
112 |
113 | client.getSandboxService().putRequest(request.getRequestId(), new SandboxRequestBody(RequestBody.Status.ARRIVING));
114 | Thread.sleep(delay);
115 | request = client.getApiService().getRequestDetails(request.getRequestId());
116 | assertEquals(request.getStatus(), RequestBody.Status.ARRIVING);
117 |
118 | client.getSandboxService().putRequest(request.getRequestId(), new SandboxRequestBody(RequestBody.Status.IN_PROGRESS));
119 | Thread.sleep(delay);
120 | request = client.getApiService().getRequestDetails(request.getRequestId());
121 | assertEquals(request.getStatus(), RequestBody.Status.IN_PROGRESS);
122 |
123 | client.getSandboxService().putRequest(request.getRequestId(), new SandboxRequestBody(RequestBody.Status.DRIVER_CANCELLED));
124 | Thread.sleep(delay);
125 | request = client.getApiService().getRequestDetails(request.getRequestId());
126 | assertEquals(request.getStatus(), RequestBody.Status.DRIVER_CANCELLED);
127 | }
128 |
129 | @Test
130 | public void testCompletedRequestStatus() throws Exception {
131 | Request request = getRequest();
132 | Response response = client.getSandboxService().putRequest(request.getRequestId(), new SandboxRequestBody(RequestBody.Status.COMPLETED));
133 | Thread.sleep(delay);
134 | request = client.getApiService().getRequestDetails(request.getRequestId());
135 | assertEquals(request.getStatus(), RequestBody.Status.COMPLETED);
136 | }
137 |
138 | @Test
139 | public void testRequestMap() throws Exception {
140 | Request request = getRequest();
141 | RequestMap requestMap = client.getApiService().getRequestMap(request.getRequestId());
142 | assertNotNull(requestMap.getHref());
143 | }
144 |
145 | @Test
146 | public void testSurgeConfirmation() throws Exception {
147 | Products products = client.getApiService().getProducts(Double.parseDouble(latitude), Double.parseDouble(longitude));
148 | String productId = products.getProducts().get(0).getProductId();
149 | client.getSandboxService().putProducts(productId, new SandboxProductBody(1.5f, true));
150 | Thread.sleep(delay);
151 |
152 | try {
153 |
154 | Request request = client.getApiService().postRequest(new RequestBody(productId,
155 | Double.parseDouble(startLatitude),
156 | Double.parseDouble(startLongitude),
157 | Double.parseDouble(endLatitude),
158 | Double.parseDouble(endLongitude),
159 | null));
160 | } catch (RetrofitError re) {
161 | assertEquals(re.getResponse().getStatus(), 409);
162 |
163 | SurgeConfirmationError surgeConfirmationError = Utils.parseSurgeConfirmationError(client.getGson(), re);
164 | assertNotNull("Surge Confirmation URL not found", surgeConfirmationError.getMeta().getSurgeConfirmation().getHref());
165 | assertNotNull("Surge Confirmation Id not found", surgeConfirmationError.getMeta().getSurgeConfirmation().getSurgeConfirmationId());
166 |
167 | //mock chrome browser to display confirmation page
168 | final WebClient webClient = new WebClient(BrowserVersion.CHROME);
169 | webClient.setAjaxController(new NicelyResynchronizingAjaxController());
170 | webClient.getOptions().setRedirectEnabled(true);
171 | webClient.getOptions().setThrowExceptionOnScriptError(false);
172 | webClient.getOptions().setCssEnabled(true);
173 | webClient.getOptions().setJavaScriptEnabled(true);
174 | webClient.getOptions().setUseInsecureSSL(true);
175 | webClient.getCookieManager().setCookiesEnabled(true);
176 | final HtmlPage confirmationPage = webClient.getPage(surgeConfirmationError.getMeta().getSurgeConfirmation().getHref());
177 | HtmlAnchor acceptAnchor = (HtmlAnchor) confirmationPage.getElementById("accept-surge");
178 | assertNotNull("Accept button not found", acceptAnchor);
179 | Page page = acceptAnchor.click();
180 | Thread.sleep(delay);
181 |
182 | Request request = client.getApiService().postRequest(new RequestBody(productId,
183 | Double.parseDouble(startLatitude),
184 | Double.parseDouble(startLongitude),
185 | Double.parseDouble(endLatitude),
186 | Double.parseDouble(endLongitude),
187 | surgeConfirmationError.getMeta().getSurgeConfirmation().getSurgeConfirmationId()));
188 |
189 | assertNotNull("Request is null", request);
190 | assertNotNull("Request id is null", request.getRequestId());
191 | assertNotNull("Request status is null", request.getStatus());
192 |
193 | }
194 |
195 | }
196 |
197 | /**
198 | * Calls getProducts and uses the first result to initiate a new request
199 | * @return Request
200 | */
201 | private Request getRequest() throws Exception{
202 | Products products = client.getApiService().getProducts(Double.parseDouble(latitude), Double.parseDouble(longitude));
203 | String productId = products.getProducts().get(0).getProductId();
204 | //reset product to default product state
205 | client.getSandboxService().putProducts(productId, new SandboxProductBody(1.0f, true));
206 | Thread.sleep(delay);
207 | Request request = client.getApiService().postRequest(new RequestBody(productId,
208 | Double.parseDouble(startLatitude),
209 | Double.parseDouble(startLongitude),
210 | Double.parseDouble(endLatitude),
211 | Double.parseDouble(endLongitude),
212 | null));
213 | assertNotNull("Request is null", request);
214 | assertNotNull("Request id is null", request.getRequestId());
215 | assertNotNull("Request status is null", request.getStatus());
216 | Thread.sleep(delay);
217 | return request;
218 | }
219 |
220 | /**
221 | * Initiates OAuth authorization using a mock chrome browser and retrieves and access token
222 | * @throws Exception
223 | */
224 | private void retrieveAccessToken() throws Exception {
225 | final WebClient webClient = new WebClient(BrowserVersion.CHROME);
226 | webClient.setWebConnection(new WebConnectionWrapper(webClient) {
227 | @Override
228 | public WebResponse getResponse(final WebRequest request) throws IOException {
229 |
230 | WebResponse response = super.getResponse(request);
231 | String location = response.getResponseHeaderValue("location");
232 | if (response.getStatusCode() == 302 && location != null && location.startsWith(client.getClientRedirectUri())) {
233 |
234 | location = location.replace(client.getClientRedirectUri() + "?", "");
235 | String[] nameValues = location.split("&");
236 | for (String nameValue : nameValues) {
237 | int idx = nameValue.indexOf("=");
238 | if ("code".equals(URLDecoder.decode(nameValue.substring(0, idx), "UTF-8"))) {
239 | String authorizationCode = URLDecoder.decode(nameValue.substring(idx + 1), "UTF-8");
240 | AccessToken accessToken = client.getAuthService().requestAccessToken(clientId, clientSecret, redirectUrl, UberAuthService.GRANT_TYPE_AUTHORIZATION_CODE, authorizationCode, null);
241 | client.setAccessToken(accessToken.getAccessToken());
242 | client.setRefreshToken(accessToken.getRefreshToken());
243 | break;
244 | }
245 | }
246 |
247 | //return empty response
248 | request.setUrl(new URL("http://redirect"));
249 | WebResponseData data = new WebResponseData("".getBytes(), 200, "OK", Collections.EMPTY_LIST);
250 | return new WebResponse(data, request, 10);
251 |
252 | }
253 | return response;
254 | }
255 | });
256 | webClient.setAjaxController(new NicelyResynchronizingAjaxController());
257 | webClient.getOptions().setRedirectEnabled(true);
258 | webClient.getOptions().setThrowExceptionOnScriptError(false);
259 | webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
260 | webClient.getOptions().setCssEnabled(true);
261 | webClient.getOptions().setJavaScriptEnabled(false);
262 | webClient.getOptions().setUseInsecureSSL(true);
263 | webClient.getCookieManager().setCookiesEnabled(true);
264 |
265 | String authUrl = Utils.generateAuthorizeUrl(client.getOAuthUri(), clientId, new String[]{UberAuthService.SCOPE_PROFILE, UberAuthService.SCOPE_HISTORY_LITE, UberAuthService.SCOPE_REQUEST});
266 | final HtmlPage loginPage = webClient.getPage(authUrl);
267 | final HtmlForm form = loginPage.getForms().get(0);
268 | final HtmlTextInput emailInputText = form.getInputByName("email");
269 | final HtmlPasswordInput passwordInputText = form.getInputByName("password");
270 | final HtmlButton submitButton = (HtmlButton) form.getByXPath("//button[@type='submit']").get(0);
271 | emailInputText.setText(username);
272 | passwordInputText.setText(password);
273 | final Page scopePage = submitButton.click();
274 | /**
275 | * If the user already accepted scope permissions, the accept screen is skipped and the user will be forwarded to the authorization code redirect
276 | */
277 | //check for the Accept html page
278 | if (scopePage instanceof HtmlPage) {
279 | //click accept button
280 | if (((HtmlPage)scopePage).getForms().size() > 0) {
281 | final HtmlForm form2 = ((HtmlPage) scopePage).getForms().get(0);
282 | final HtmlButton allowButton = (HtmlButton) form2.getByXPath("//button[@value='yes']").get(0);
283 | allowButton.click();
284 | }
285 | }
286 | }
287 | }
288 |
--------------------------------------------------------------------------------