forked from googlemaps/google-maps-services-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DistanceMatrixApiTest.java
215 lines (192 loc) · 9.02 KB
/
DistanceMatrixApiTest.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
/*
* 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.retrieveBody;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import com.google.maps.DirectionsApi.RouteRestriction;
import com.google.maps.model.DistanceMatrix;
import com.google.maps.model.DistanceMatrixElementStatus;
import com.google.maps.model.LatLng;
import com.google.maps.model.TrafficModel;
import com.google.maps.model.TravelMode;
import com.google.maps.model.Unit;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.junit.experimental.categories.Category;
@Category(MediumTests.class)
public class DistanceMatrixApiTest {
private final String getDistanceMatrixWithBasicStringParams;
public DistanceMatrixApiTest() {
getDistanceMatrixWithBasicStringParams =
retrieveBody("GetDistanceMatrixWithBasicStringParams.json");
}
@Test
public void testLatLngOriginDestinations() throws Exception {
try (LocalTestServerContext sc = new LocalTestServerContext("{\"status\" : \"OK\"}")) {
DistanceMatrixApi.newRequest(sc.context)
.origins(new LatLng(-31.9522, 115.8589), new LatLng(-37.8136, 144.9631))
.destinations(new LatLng(-25.344677, 131.036692), new LatLng(-13.092297, 132.394057))
.awaitIgnoreError();
sc.assertParamValue("-31.95220000,115.85890000|-37.81360000,144.96310000", "origins");
sc.assertParamValue("-25.34467700,131.03669200|-13.09229700,132.39405700", "destinations");
}
}
@Test
public void testGetDistanceMatrixWithBasicStringParams() throws Exception {
try (LocalTestServerContext sc =
new LocalTestServerContext(getDistanceMatrixWithBasicStringParams)) {
String[] origins =
new String[] {
"Perth, Australia", "Sydney, Australia", "Melbourne, Australia",
"Adelaide, Australia", "Brisbane, Australia", "Darwin, Australia",
"Hobart, Australia", "Canberra, Australia"
};
String[] destinations =
new String[] {
"Uluru, Australia",
"Kakadu, Australia",
"Blue Mountains, Australia",
"Bungle Bungles, Australia",
"The Pinnacles, Australia"
};
DistanceMatrix matrix =
DistanceMatrixApi.getDistanceMatrix(sc.context, origins, destinations).await();
assertNotNull(matrix.toString());
assertNotNull(Arrays.toString(matrix.rows));
// Rows length will match the number of origin elements, regardless of whether they're
// routable.
assertEquals(8, matrix.rows.length);
assertEquals(5, matrix.rows[0].elements.length);
assertEquals(DistanceMatrixElementStatus.OK, matrix.rows[0].elements[0].status);
assertEquals("Perth WA, Australia", matrix.originAddresses[0]);
assertEquals("Sydney NSW, Australia", matrix.originAddresses[1]);
assertEquals("Melbourne VIC, Australia", matrix.originAddresses[2]);
assertEquals("Adelaide SA, Australia", matrix.originAddresses[3]);
assertEquals("Brisbane QLD, Australia", matrix.originAddresses[4]);
assertEquals("Darwin NT, Australia", matrix.originAddresses[5]);
assertEquals("Hobart TAS 7000, Australia", matrix.originAddresses[6]);
assertEquals("Canberra ACT 2601, Australia", matrix.originAddresses[7]);
assertEquals("Uluru, Petermann NT 0872, Australia", matrix.destinationAddresses[0]);
assertEquals("Kakadu NT 0822, Australia", matrix.destinationAddresses[1]);
assertEquals("Blue Mountains, New South Wales, Australia", matrix.destinationAddresses[2]);
assertEquals(
"Purnululu National Park, Western Australia 6770, Australia",
matrix.destinationAddresses[3]);
assertEquals("Pinnacles Drive, Cervantes WA 6511, Australia", matrix.destinationAddresses[4]);
sc.assertParamValue(StringUtils.join(origins, "|"), "origins");
sc.assertParamValue(StringUtils.join(destinations, "|"), "destinations");
}
}
@Test
public void testNewRequestWithAllPossibleParams() throws Exception {
try (LocalTestServerContext sc = new LocalTestServerContext("{\"status\" : \"OK\"}")) {
String[] origins =
new String[] {
"Perth, Australia", "Sydney, Australia", "Melbourne, Australia",
"Adelaide, Australia", "Brisbane, Australia", "Darwin, Australia",
"Hobart, Australia", "Canberra, Australia"
};
String[] destinations =
new String[] {
"Uluru, Australia",
"Kakadu, Australia",
"Blue Mountains, Australia",
"Bungle Bungles, Australia",
"The Pinnacles, Australia"
};
DistanceMatrixApi.newRequest(sc.context)
.origins(origins)
.destinations(destinations)
.mode(TravelMode.DRIVING)
.language("en-AU")
.avoid(RouteRestriction.TOLLS)
.units(Unit.IMPERIAL)
.departureTime(
Instant.now().plus(Duration.ofMinutes(2))) // this is ignored when an API key is used
.await();
sc.assertParamValue(StringUtils.join(origins, "|"), "origins");
sc.assertParamValue(StringUtils.join(destinations, "|"), "destinations");
sc.assertParamValue(TravelMode.DRIVING.toUrlValue(), "mode");
sc.assertParamValue("en-AU", "language");
sc.assertParamValue(RouteRestriction.TOLLS.toUrlValue(), "avoid");
sc.assertParamValue(Unit.IMPERIAL.toUrlValue(), "units");
}
}
/**
* Test the language parameter.
*
* <p>Sample request: <a
* href="http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR">
* origins: Vancouver BC|Seattle, destinations: San Francisco|Victoria BC, mode: bicycling,
* language: french</a>.
*/
@Test
public void testLanguageParameter() throws Exception {
try (LocalTestServerContext sc = new LocalTestServerContext("{\"status\" : \"OK\"}")) {
String[] origins = new String[] {"Vancouver BC", "Seattle"};
String[] destinations = new String[] {"San Francisco", "Victoria BC"};
DistanceMatrixApi.newRequest(sc.context)
.origins(origins)
.destinations(destinations)
.mode(TravelMode.BICYCLING)
.language("fr-FR")
.await();
sc.assertParamValue(StringUtils.join(origins, "|"), "origins");
sc.assertParamValue(StringUtils.join(destinations, "|"), "destinations");
sc.assertParamValue(TravelMode.BICYCLING.toUrlValue(), "mode");
sc.assertParamValue("fr-FR", "language");
}
}
/** Test transit without arrival or departure times specified. */
@Test
public void testTransitWithoutSpecifyingTime() throws Exception {
try (LocalTestServerContext sc = new LocalTestServerContext("{\"status\" : \"OK\"}")) {
String[] origins =
new String[] {"Fisherman's Wharf, San Francisco", "Union Square, San Francisco"};
String[] destinations =
new String[] {"Mikkeller Bar, San Francisco", "Moscone Center, San Francisco"};
DistanceMatrixApi.newRequest(sc.context)
.origins(origins)
.destinations(destinations)
.mode(TravelMode.TRANSIT)
.await();
sc.assertParamValue(StringUtils.join(origins, "|"), "origins");
sc.assertParamValue(StringUtils.join(destinations, "|"), "destinations");
sc.assertParamValue(TravelMode.TRANSIT.toUrlValue(), "mode");
}
}
/** Test duration in traffic with traffic model set. */
@Test
public void testDurationInTrafficWithTrafficModel() throws Exception {
try (LocalTestServerContext sc = new LocalTestServerContext("{\"status\" : \"OK\"}")) {
final long ONE_HOUR_MILLIS = 60 * 60 * 1000;
DistanceMatrixApi.newRequest(sc.context)
.origins("Fisherman's Wharf, San Francisco")
.destinations("San Francisco International Airport, San Francisco, CA")
.mode(TravelMode.DRIVING)
.trafficModel(TrafficModel.PESSIMISTIC)
.departureTime(Instant.ofEpochMilli(System.currentTimeMillis() + ONE_HOUR_MILLIS))
.await();
sc.assertParamValue("Fisherman's Wharf, San Francisco", "origins");
sc.assertParamValue("San Francisco International Airport, San Francisco, CA", "destinations");
sc.assertParamValue(TravelMode.DRIVING.toUrlValue(), "mode");
sc.assertParamValue(TrafficModel.PESSIMISTIC.toUrlValue(), "traffic_model");
}
}
}