forked from googlemaps/google-maps-services-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeoApiContextTest.java
413 lines (349 loc) · 14 KB
/
GeoApiContextTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* Copyright 2014 Google Inc. All rights reserved.
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.google.maps;
import static com.google.maps.TestUtils.findLastThreadByName;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import com.google.maps.errors.OverQueryLimitException;
import com.google.maps.internal.ApiConfig;
import com.google.maps.internal.ApiResponse;
import com.google.maps.internal.HttpHeaders;
import com.google.maps.model.GeocodingResult;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import okhttp3.Headers;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
@Category(MediumTests.class)
public class GeoApiContextTest {
private MockWebServer server;
private GeoApiContext.Builder builder;
@Before
public void Setup() {
server = new MockWebServer();
builder = new GeoApiContext.Builder().apiKey("AIza...").queryRateLimit(500);
}
@After
@SuppressWarnings("CatchAndPrintStackTrace")
public void Teardown() {
try {
server.shutdown();
} catch (IOException e) {
e.printStackTrace();
}
}
private void setMockBaseUrl() {
builder.baseUrlOverride("http://127.0.0.1:" + server.getPort());
}
@SuppressWarnings("unchecked")
@Test
public void testGetIncludesDefaultUserAgent() throws Exception {
// Set up a mock request
ApiResponse<Object> fakeResponse = mock(ApiResponse.class);
String path = "/";
Map<String, List<String>> params = new HashMap<>();
params.put("key", Collections.singletonList("value"));
// Set up the fake web server
server.enqueue(new MockResponse());
server.start();
setMockBaseUrl();
// Build & execute the request using our context
builder.build().get(new ApiConfig(path), fakeResponse.getClass(), params).awaitIgnoreError();
// Read the headers
server.shutdown();
RecordedRequest request = server.takeRequest();
Headers headers = request.getHeaders();
boolean headerFound = false;
for (String headerName : headers.names()) {
if (headerName.equals("User-Agent")) {
headerFound = true;
String headerValue = headers.get(headerName);
assertTrue(
"User agent not in correct format",
headerValue.matches("GoogleGeoApiClientJava/[^\\s]+"));
}
}
assertTrue("User agent header not present", headerFound);
}
@Test
public void testErrorResponseRetries() throws Exception {
// Set up mock responses
MockResponse errorResponse = createMockBadResponse();
MockResponse goodResponse = createMockGoodResponse();
server.enqueue(errorResponse);
server.enqueue(goodResponse);
server.start();
// Build the context under test
setMockBaseUrl();
// Execute
GeocodingResult[] result =
builder.build().get(new ApiConfig("/"), GeocodingApi.Response.class, "k", "v").await();
assertEquals(1, result.length);
assertEquals(
"1600 Amphitheatre Parkway, Mountain View, CA 94043, USA", result[0].formattedAddress);
server.shutdown();
}
@Test(expected = IOException.class)
public void testSettingMaxRetries() throws Exception {
MockResponse errorResponse = createMockBadResponse();
MockResponse goodResponse = createMockGoodResponse();
// Set up the fake web server
server.enqueue(errorResponse);
server.enqueue(errorResponse);
server.enqueue(errorResponse);
server.enqueue(goodResponse);
server.start();
setMockBaseUrl();
// This should limit the number of retries, ensuring that the success response is NOT returned.
builder.maxRetries(2);
builder.build().get(new ApiConfig("/"), GeocodingApi.Response.class, "k", "v").await();
}
private MockResponse createMockGoodResponse() {
MockResponse response = new MockResponse();
response.setResponseCode(200);
response.setBody(
"{\n"
+ " \"results\" : [\n"
+ " {\n"
+ " \"address_components\" : [\n"
+ " {\n"
+ " \"long_name\" : \"1600\",\n"
+ " \"short_name\" : \"1600\",\n"
+ " \"types\" : [ \"street_number\" ]\n"
+ " }\n"
+ " ],\n"
+ " \"formatted_address\" : \"1600 Amphitheatre Parkway, Mountain View, "
+ "CA 94043, USA\",\n"
+ " \"geometry\" : {\n"
+ " \"location\" : {\n"
+ " \"lat\" : 37.4220033,\n"
+ " \"lng\" : -122.0839778\n"
+ " },\n"
+ " \"location_type\" : \"ROOFTOP\",\n"
+ " \"viewport\" : {\n"
+ " \"northeast\" : {\n"
+ " \"lat\" : 37.4233522802915,\n"
+ " \"lng\" : -122.0826288197085\n"
+ " },\n"
+ " \"southwest\" : {\n"
+ " \"lat\" : 37.4206543197085,\n"
+ " \"lng\" : -122.0853267802915\n"
+ " }\n"
+ " }\n"
+ " },\n"
+ " \"types\" : [ \"street_address\" ]\n"
+ " }\n"
+ " ],\n"
+ " \"status\" : \"OK\"\n"
+ "}");
return response;
}
private MockResponse createMockBadResponse() {
MockResponse response = new MockResponse();
response.setStatus("HTTP/1.1 500 Internal server error");
response.setBody("Uh-oh. Server Error.");
return response;
}
@Test(expected = IOException.class)
public void testRetryCanBeDisabled() throws Exception {
// Set up 2 mock responses, an error that shouldn't be retried and a success
MockResponse errorResponse = new MockResponse();
errorResponse.setStatus("HTTP/1.1 500 Internal server error");
errorResponse.setBody("Uh-oh. Server Error.");
server.enqueue(errorResponse);
MockResponse goodResponse = new MockResponse();
goodResponse.setResponseCode(200);
goodResponse.setBody("{\n \"results\" : [],\n \"status\" : \"ZERO_RESULTS\"\n}");
server.enqueue(goodResponse);
server.start();
setMockBaseUrl();
// This should disable the retry, ensuring that the success response is NOT returned
builder.disableRetries();
// We should get the error response here, not the success response.
builder.build().get(new ApiConfig("/"), GeocodingApi.Response.class, "k", "v").await();
}
@Test
public void testRetryEventuallyReturnsTheRightException() throws Exception {
MockResponse errorResponse = new MockResponse();
errorResponse.setStatus("HTTP/1.1 500 Internal server error");
errorResponse.setBody("Uh-oh. Server Error.");
// Enqueue some error responses.
for (int i = 0; i < 10; i++) {
server.enqueue(errorResponse);
}
server.start();
// Wire the mock web server to the context
setMockBaseUrl();
builder.retryTimeout(5, TimeUnit.SECONDS);
try {
builder.build().get(new ApiConfig("/"), GeocodingApi.Response.class, "k", "v").await();
} catch (IOException ioe) {
// Ensure the message matches the status line in the mock responses.
assertEquals("Server Error: 500 Internal server error", ioe.getMessage());
return;
}
fail("Internal server error was expected but not observed.");
}
@Test
public void testQueryParamsHaveOrderPreserved() throws Exception {
// This test is important for APIs (such as the speed limits API) where multiple parameters
// must be provided with the same name with order preserved.
MockResponse response = new MockResponse();
response.setResponseCode(200);
response.setBody("{}");
server.enqueue(response);
server.start();
setMockBaseUrl();
builder
.build()
.get(new ApiConfig("/"), GeocodingApi.Response.class, "a", "1", "a", "2", "a", "3")
.awaitIgnoreError();
server.shutdown();
RecordedRequest request = server.takeRequest();
String path = request.getPath();
assertTrue(path.contains("a=1&a=2&a=3"));
}
@Test
public void testToggleIfExceptionIsAllowedToRetry() throws Exception {
// Enqueue some error responses, although only the first should be used because the response's
// exception is not allowed to be retried.
MockResponse overQueryLimitResponse = new MockResponse();
overQueryLimitResponse.setStatus("HTTP/1.1 400 Internal server error");
overQueryLimitResponse.setBody(TestUtils.retrieveBody("OverQueryLimitResponse.json"));
server.enqueue(overQueryLimitResponse);
server.enqueue(overQueryLimitResponse);
server.enqueue(overQueryLimitResponse);
server.start();
builder.retryTimeout(1, TimeUnit.MILLISECONDS);
builder.maxRetries(10);
builder.setIfExceptionIsAllowedToRetry(OverQueryLimitException.class, false);
setMockBaseUrl();
try {
builder
.build()
.get(new ApiConfig("/"), GeocodingApi.Response.class, "any-key", "any-value")
.await();
} catch (OverQueryLimitException e) {
assertEquals(1, server.getRequestCount());
return;
}
fail("OverQueryLimitException was expected but not observed.");
}
@Test
public void testSingleExperienceId() {
final String experienceId = "experienceId";
final GeoApiContext context = builder.experienceId(experienceId).build();
assertEquals(experienceId, context.getExperienceId());
}
@Test
public void testMultipleExperienceId() {
final String experienceId1 = "experienceId1";
final String experienceId2 = "experienceId2";
final GeoApiContext context = builder.experienceId(experienceId1, experienceId2).build();
assertEquals(experienceId1 + "," + experienceId2, context.getExperienceId());
}
@Test
public void testNoExperienceId() {
final GeoApiContext context = builder.build();
assertNull(context.getExperienceId());
}
@Test
public void testClearingExperienceId() {
final String experienceId = "experienceId";
final GeoApiContext context = builder.experienceId(experienceId).build();
assertEquals(experienceId, context.getExperienceId());
context.clearExperienceId();
assertNull(context.getExperienceId());
}
@Test
public void testExperienceIdIsInHeader() throws Exception {
final String experienceId = "exp1";
final RecordedRequest request = makeMockRequest(experienceId);
assertEquals(experienceId, request.getHeader(HttpHeaders.X_GOOG_MAPS_EXPERIENCE_ID));
}
@Test
public void testExperienceIdNotInHeader() throws Exception {
final RecordedRequest request = makeMockRequest();
final String value = request.getHeader(HttpHeaders.X_GOOG_MAPS_EXPERIENCE_ID);
assertNull(value);
}
@Test
public void testExperienceIdSample() {
// [START maps_experience_id]
final String experienceId = UUID.randomUUID().toString();
// instantiate context with experience id
final GeoApiContext context =
new GeoApiContext.Builder().apiKey("AIza-Maps-API-Key").experienceId(experienceId).build();
// clear the current experience id
context.clearExperienceId();
// set a new experience id
final String otherExperienceId = UUID.randomUUID().toString();
context.setExperienceId(experienceId, otherExperienceId);
// make API request, the client will set the header
// X-GOOG-MAPS-EXPERIENCE-ID: experienceId,otherExperienceId
// get current experience id
final String ids = context.getExperienceId();
// [END maps_experience_id]
assertEquals(experienceId + "," + otherExperienceId, ids);
}
@SuppressWarnings("unchecked")
private RecordedRequest makeMockRequest(String... experienceId) throws Exception {
// Set up a mock request
ApiResponse<Object> fakeResponse = mock(ApiResponse.class);
String path = "/";
Map<String, List<String>> params = new HashMap<>();
params.put("key", Collections.singletonList("value"));
// Set up the fake web server
server.enqueue(new MockResponse());
server.start();
setMockBaseUrl();
// Build & execute the request using our context
final GeoApiContext context = builder.experienceId(experienceId).build();
context.get(new ApiConfig(path), fakeResponse.getClass(), params).awaitIgnoreError();
// Read the header
server.shutdown();
return server.takeRequest();
}
@Test
public void testShutdown() throws InterruptedException {
GeoApiContext context = builder.build();
final Thread delayThread = findLastThreadByName("RateLimitExecutorDelayThread");
assertNotNull(
"Delay thread should be created in constructor of RateLimitExecutorService", delayThread);
assertTrue(
"Delay thread should start in constructor of RateLimitExecutorService",
delayThread.isAlive());
// this is needed to make sure that delay thread has reached queue.take()
delayThread.join(10);
context.shutdown();
delayThread.join(10);
assertFalse(delayThread.isAlive());
}
}