Skip to content

Commit 3cc393b

Browse files
committed
Add unit tests.
1 parent 1d9a8ce commit 3cc393b

File tree

4 files changed

+119
-83
lines changed

4 files changed

+119
-83
lines changed

src/main/java/com/google/maps/GeoApiContext.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.HashMap;
3939
import java.util.List;
4040
import java.util.Map;
41+
import java.util.Map.Entry;
4142
import java.util.concurrent.TimeUnit;
4243
import javax.print.DocFlavor.STRING;
4344

@@ -170,6 +171,14 @@ public void shutdown() {
170171
requestHandler.shutdown();
171172
}
172173

174+
private void addDefaultHeaders(Map<String, String> headers) {
175+
for (Entry<String, String> entry : defaultHeaders.entrySet()) {
176+
if (!headers.containsKey(entry.getKey())) {
177+
headers.put(entry.getKey(), entry.getValue());
178+
}
179+
}
180+
}
181+
173182
<T, R extends ApiResponse<T>> PendingResult<T> get(
174183
ApiConfig config, Class<? extends R> clazz, Map<String, String> headers, Map<String, List<String>> params) {
175184
if (channel != null && !channel.isEmpty() && !params.containsKey("channel")) {
@@ -238,6 +247,8 @@ <T, R extends ApiResponse<T>> PendingResult<T> get(
238247
query.append("&channel=").append(channel);
239248
}
240249

250+
addDefaultHeaders(headers);
251+
241252
return getWithPath(
242253
clazz,
243254
config.fieldNamingPolicy,
@@ -275,6 +286,8 @@ <T, R extends ApiResponse<T>> PendingResult<T> post(
275286
hostName = baseUrlOverride;
276287
}
277288

289+
addDefaultHeaders(headers);
290+
278291
return requestHandler.handlePost(
279292
hostName,
280293
url.toString(),

src/test/java/com/google/maps/GeoApiContextTest.java

Lines changed: 14 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
import com.google.maps.internal.ApiConfig;
3434
import com.google.maps.internal.ApiResponse;
3535
import com.google.maps.internal.HttpHeaders;
36+
import com.google.maps.internal.StringJoin;
3637
import com.google.maps.model.GeocodingResult;
3738
import java.io.IOException;
3839
import java.util.Collections;
3940
import java.util.HashMap;
4041
import java.util.List;
4142
import java.util.Map;
42-
import java.util.UUID;
4343
import java.util.concurrent.TimeUnit;
4444
import okhttp3.Headers;
4545
import okhttp3.mockwebserver.MockResponse;
@@ -374,42 +374,13 @@ public void testToggleIfExceptionIsAllowedToRetry() throws Exception {
374374
fail("OverQueryLimitException was expected but not observed.");
375375
}
376376

377-
@Test
378-
public void testSingleExperienceId() {
379-
final String experienceId = "experienceId";
380-
final GeoApiContext context = builder.experienceId(experienceId).build();
381-
assertEquals(experienceId, context.getExperienceId());
382-
}
383-
384-
@Test
385-
public void testMultipleExperienceId() {
386-
final String experienceId1 = "experienceId1";
387-
final String experienceId2 = "experienceId2";
388-
final GeoApiContext context = builder.experienceId(experienceId1, experienceId2).build();
389-
assertEquals(experienceId1 + "," + experienceId2, context.getExperienceId());
390-
}
391-
392-
@Test
393-
public void testNoExperienceId() {
394-
final GeoApiContext context = builder.build();
395-
assertNull(context.getExperienceId());
396-
}
397-
398-
@Test
399-
public void testClearingExperienceId() {
400-
final String experienceId = "experienceId";
401-
final GeoApiContext context = builder.experienceId(experienceId).build();
402-
assertEquals(experienceId, context.getExperienceId());
403-
404-
context.clearExperienceId();
405-
assertNull(context.getExperienceId());
406-
}
407-
408377
@Test
409378
public void testExperienceIdIsInHeader() throws Exception {
410379
final String experienceId = "exp1";
411-
final RecordedRequest request = makeMockRequest(experienceId);
412-
assertEquals(experienceId, request.getHeader(HttpHeaders.X_GOOG_MAPS_EXPERIENCE_ID));
380+
final String experienceId2 = "exp2";
381+
final RecordedRequest request = makeMockRequest(experienceId, experienceId2);
382+
assertEquals(experienceId + "," + experienceId2,
383+
request.getHeader(HttpHeaders.X_GOOG_MAPS_EXPERIENCE_ID));
413384
}
414385

415386
@Test
@@ -419,32 +390,6 @@ public void testExperienceIdNotInHeader() throws Exception {
419390
assertNull(value);
420391
}
421392

422-
@Test
423-
public void testExperienceIdSample() {
424-
// [START maps_experience_id]
425-
final String experienceId = UUID.randomUUID().toString();
426-
427-
// instantiate context with experience id
428-
final GeoApiContext context =
429-
new GeoApiContext.Builder().apiKey("AIza-Maps-API-Key").experienceId(experienceId).build();
430-
431-
// clear the current experience id
432-
context.clearExperienceId();
433-
434-
// set a new experience id
435-
final String otherExperienceId = UUID.randomUUID().toString();
436-
context.setExperienceId(experienceId, otherExperienceId);
437-
438-
// make API request, the client will set the header
439-
// X-GOOG-MAPS-EXPERIENCE-ID: experienceId,otherExperienceId
440-
441-
// get current experience id
442-
final String ids = context.getExperienceId();
443-
// [END maps_experience_id]
444-
445-
assertEquals(experienceId + "," + otherExperienceId, ids);
446-
}
447-
448393
@SuppressWarnings("unchecked")
449394
private RecordedRequest makeMockRequest(String... experienceId) throws Exception {
450395
// Set up a mock request
@@ -458,9 +403,16 @@ private RecordedRequest makeMockRequest(String... experienceId) throws Exception
458403
server.start();
459404
setMockBaseUrl();
460405

406+
// Create headers
407+
Map<String, String> headers = new HashMap<>();
408+
if (experienceId != null && experienceId.length > 0) {
409+
String experienceIds = StringJoin.join(",", experienceId);
410+
headers.put(HttpHeaders.X_GOOG_MAPS_EXPERIENCE_ID, experienceIds);
411+
}
412+
461413
// Build & execute the request using our context
462-
final GeoApiContext context = builder.experienceId(experienceId).build();
463-
context.get(new ApiConfig(path), fakeResponse.getClass(), params).awaitIgnoreError();
414+
final GeoApiContext context = builder.build();
415+
context.get(new ApiConfig(path), fakeResponse.getClass(), headers, params).awaitIgnoreError();
464416

465417
// Read the header
466418
server.shutdown();

src/test/java/com/google/maps/GeocodingApiTest.java

Lines changed: 86 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.junit.Assert.assertNotNull;
2424
import static org.junit.Assert.fail;
2525

26+
import com.google.maps.internal.HttpHeaders;
2627
import com.google.maps.model.AddressComponentType;
2728
import com.google.maps.model.AddressType;
2829
import com.google.maps.model.ComponentFilter;
@@ -32,6 +33,8 @@
3233
import java.util.ArrayList;
3334
import java.util.Arrays;
3435
import java.util.List;
36+
import java.util.UUID;
37+
import okhttp3.Headers;
3538
import org.junit.Test;
3639
import org.junit.experimental.categories.Category;
3740

@@ -93,6 +96,68 @@ public void testPlaceGeocode() throws Exception {
9396
}
9497
}
9598

99+
private void testExperienceIdSample() {
100+
// [START maps_experience_id]
101+
final String experienceId = UUID.randomUUID().toString();
102+
103+
// instantiate context
104+
final GeoApiContext context =
105+
new GeoApiContext.Builder().apiKey("AIza-Maps-API-Key").build();
106+
107+
// set the experience id on a request
108+
final GeocodingApiRequest request =
109+
GeocodingApi.newRequest(context).experienceIds(experienceId);
110+
111+
// set a new experience id on another request
112+
final String otherExperienceId = UUID.randomUUID().toString();
113+
final GeocodingApiRequest request2 =
114+
GeocodingApi.newRequest(context).experienceIds(otherExperienceId);
115+
116+
// make API request, the client will set the header
117+
// X-GOOG-MAPS-EXPERIENCE-ID: experienceId,otherExperienceId
118+
// [END maps_experience_id]
119+
}
120+
121+
@Test
122+
public void testNoExperienceId() throws Exception {
123+
try (LocalTestServerContext sc = new LocalTestServerContext(placeGeocodeResponse)) {
124+
String placeID = "ChIJP3Sa8ziYEmsRUKgyFmh9AQM";
125+
GeocodingResult[] results = GeocodingApi.newRequest(sc.context).place(placeID).await();
126+
final Headers headers = sc.headers();
127+
final List<String> experienceIds = headers.values(HttpHeaders.X_GOOG_MAPS_EXPERIENCE_ID);
128+
assertEquals(0, experienceIds.size());
129+
}
130+
}
131+
132+
@Test
133+
public void testExperienceId() throws Exception {
134+
try (LocalTestServerContext sc = new LocalTestServerContext(placeGeocodeResponse)) {
135+
String placeID = "ChIJP3Sa8ziYEmsRUKgyFmh9AQM";
136+
String expId = "experienceId";
137+
GeocodingApi.newRequest(sc.context).experienceIds(expId)
138+
.place(placeID).await();
139+
final Headers headers = sc.headers();
140+
final List<String> experienceIds = headers.values(HttpHeaders.X_GOOG_MAPS_EXPERIENCE_ID);
141+
assertEquals(1, experienceIds.size());
142+
assertEquals(expId, experienceIds.get(0));
143+
}
144+
}
145+
146+
@Test
147+
public void testExperienceIds() throws Exception {
148+
try (LocalTestServerContext sc = new LocalTestServerContext(placeGeocodeResponse)) {
149+
String placeID = "ChIJP3Sa8ziYEmsRUKgyFmh9AQM";
150+
String expId = "experienceId";
151+
String expId2 = "experienceId2";
152+
GeocodingResult[] results = GeocodingApi.newRequest(sc.context).experienceIds(expId, expId2)
153+
.place(placeID).await();
154+
final Headers headers = sc.headers();
155+
final List<String> experienceIds = headers.values(HttpHeaders.X_GOOG_MAPS_EXPERIENCE_ID);
156+
assertEquals(1, experienceIds.size());
157+
assertEquals(expId + "," + expId2, experienceIds.get(0));
158+
}
159+
}
160+
96161
@Test
97162
public void testAsync() throws Exception {
98163
try (LocalTestServerContext sc = new LocalTestServerContext(simpleGeocodeResponse)) {
@@ -148,8 +213,7 @@ public void testReverseGeocode() throws Exception {
148213
}
149214

150215
/**
151-
* Simple geocode sample: <a
152-
* href="https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA">
216+
* Simple geocode sample: <a href="https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA">
153217
* Address Geocode for "1600 Amphitheatre Parkway, Mountain View, CA"</a>.
154218
*/
155219
@Test
@@ -249,8 +313,7 @@ public void testGeocodeTheGoogleplex() throws Exception {
249313
}
250314

251315
/**
252-
* Address geocode with bounds: <a
253-
* href="https://maps.googleapis.com/maps/api/geocode/json?address=Winnetka&bounds=34.172684,-118.604794|34.236144,-118.500938">
316+
* Address geocode with bounds: <a href="https://maps.googleapis.com/maps/api/geocode/json?address=Winnetka&bounds=34.172684,-118.604794|34.236144,-118.500938">
254317
* Winnetka within (34.172684,-118.604794) - (34.236144,-118.500938)</a>.
255318
*/
256319
@Test
@@ -339,9 +402,8 @@ public void testGeocodeWithBounds() throws Exception {
339402
}
340403

341404
/**
342-
* Geocode with region biasing: <a
343-
* href="https://maps.googleapis.com/maps/api/geocode/json?address=Toledo&region=es">Geocode for
344-
* Toledo in Spain</a>.
405+
* Geocode with region biasing: <a href="https://maps.googleapis.com/maps/api/geocode/json?address=Toledo&region=es">Geocode
406+
* for Toledo in Spain</a>.
345407
*/
346408
@Test
347409
public void testGeocodeWithRegionBiasing() throws Exception {
@@ -423,8 +485,7 @@ public void testGeocodeWithRegionBiasing() throws Exception {
423485
}
424486

425487
/**
426-
* Geocode with component filtering: <a
427-
* href="https://maps.googleapis.com/maps/api/geocode/json?address=santa+cruz&components=country:ES">
488+
* Geocode with component filtering: <a href="https://maps.googleapis.com/maps/api/geocode/json?address=santa+cruz&components=country:ES">
428489
* Geocoding "santa cruz" with country set to ES</a>.
429490
*/
430491
@Test
@@ -508,8 +569,7 @@ public void testGeocodeWithComponentFilter() throws Exception {
508569
}
509570

510571
/**
511-
* Geocode with multiple component filters: <a
512-
* href="https://maps.googleapis.com/maps/api/geocode/json?address=Torun&components=administrative_area:TX|country:US">
572+
* Geocode with multiple component filters: <a href="https://maps.googleapis.com/maps/api/geocode/json?address=Torun&components=administrative_area:TX|country:US">
513573
* Geocoding Torun, with administrative area of "TX" and country of "US"</a>.
514574
*/
515575
@Test
@@ -595,8 +655,7 @@ public void testGeocodeWithMultipleComponentFilters() throws Exception {
595655
}
596656

597657
/**
598-
* Making a request using just components filter: <a
599-
* href="https://maps.googleapis.com/maps/api/geocode/json?components=route:Annegatan|administrative_area:Helsinki|country:Finland">
658+
* Making a request using just components filter: <a href="https://maps.googleapis.com/maps/api/geocode/json?components=route:Annegatan|administrative_area:Helsinki|country:Finland">
600659
* Searching for a route of Annegatan, in the administrative area of Helsinki, and the country of
601660
* Finland </a>.
602661
*/
@@ -683,8 +742,7 @@ public void testGeocodeWithJustComponents() throws Exception {
683742
}
684743

685744
/**
686-
* Simple reverse geocoding. <a
687-
* href="https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452">Reverse
745+
* Simple reverse geocoding. <a href="https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452">Reverse
688746
* geocode (40.714224,-73.961452)</a>.
689747
*/
690748
@Test
@@ -706,8 +764,7 @@ public void testSimpleReverseGeocode() throws Exception {
706764
}
707765

708766
/**
709-
* Reverse geocode restricted by type: <a
710-
* href="https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&location_type=ROOFTOP&result_type=street_address">
767+
* Reverse geocode restricted by type: <a href="https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&location_type=ROOFTOP&result_type=street_address">
711768
* Reverse Geocode (40.714224,-73.961452) with location type of ROOFTOP and result type of
712769
* street_address</a>.
713770
*/
@@ -805,7 +862,9 @@ public void testReverseGeocodeRestrictedByType() throws Exception {
805862
}
806863
}
807864

808-
/** Testing UTF8 result parsing. */
865+
/**
866+
* Testing UTF8 result parsing.
867+
*/
809868
@Test
810869
public void testUtfResult() throws Exception {
811870
try (LocalTestServerContext sc = new LocalTestServerContext(utfResultGeocodeResponse)) {
@@ -924,7 +983,9 @@ public void testCustomParameterPassThrough() throws Exception {
924983
}
925984
}
926985

927-
/** Testing Kita Ward reverse geocode. */
986+
/**
987+
* Testing Kita Ward reverse geocode.
988+
*/
928989
@Test
929990
public void testReverseGeocodeWithKitaWard() throws Exception {
930991
try (LocalTestServerContext sc =
@@ -947,7 +1008,9 @@ public void testReverseGeocodeWithKitaWard() throws Exception {
9471008
}
9481009
}
9491010

950-
/** Testing supported Address Types for Geocoding. */
1011+
/**
1012+
* Testing supported Address Types for Geocoding.
1013+
*/
9511014
@Test
9521015
public void testSupportedAddressTypesFood() throws Exception {
9531016
try (LocalTestServerContext sc =
@@ -1038,7 +1101,9 @@ public void testSupportedAddressTypesFood() throws Exception {
10381101
}
10391102
}
10401103

1041-
/** Testing supported Address Types for Geocoding - Synagogue. */
1104+
/**
1105+
* Testing supported Address Types for Geocoding - Synagogue.
1106+
*/
10421107
@Test
10431108
public void testSupportedAddressTypesSynagogue() throws Exception {
10441109
try (LocalTestServerContext sc =

src/test/java/com/google/maps/LocalTestServerContext.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.nio.charset.Charset;
2626
import java.util.List;
2727
import javax.imageio.ImageIO;
28+
import okhttp3.Headers;
2829
import okhttp3.mockwebserver.MockResponse;
2930
import okhttp3.mockwebserver.MockWebServer;
3031
import okhttp3.mockwebserver.RecordedRequest;
@@ -102,6 +103,11 @@ public String path() throws InterruptedException {
102103
return request.getPath().split("\\?", -1)[0];
103104
}
104105

106+
public Headers headers() throws InterruptedException {
107+
takeRequest();
108+
return request.getHeaders();
109+
}
110+
105111
void assertParamValue(String expected, String paramName)
106112
throws URISyntaxException, InterruptedException {
107113
if (this.params == null) {

0 commit comments

Comments
 (0)