Skip to content

Commit 78ecdcd

Browse files
author
Łukasz Paczos
committed
exposed DirectionsRoute.Builder#routeIndex
1 parent e3f839b commit 78ecdcd

5 files changed

Lines changed: 39 additions & 6 deletions

File tree

services-directions-models/src/main/java/com/mapbox/api/directions/v5/models/DirectionsResponse.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,7 @@ public static TypeAdapter<DirectionsResponse> typeAdapter(Gson gson) {
134134
* @since 3.0.0
135135
*/
136136
public static DirectionsResponse fromJson(@NonNull String json) {
137-
GsonBuilder gson = new GsonBuilder();
138-
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
139-
gson.registerTypeAdapter(Point.class, new PointAsCoordinatesTypeAdapter());
140-
return gson.create().fromJson(json, DirectionsResponse.class);
137+
return fromJson(json, null, null);
141138
}
142139

143140
/**

services-directions-models/src/main/java/com/mapbox/api/directions/v5/models/DirectionsRoute.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,14 @@ public abstract static class Builder {
322322
@NonNull
323323
public abstract Builder requestUuid(@Nullable String requestUuid);
324324

325+
/**
326+
* The index of the route in the list of routes returned by the original response.
327+
*
328+
* @param routeIndex string of an int value representing the index
329+
* @return this builder for chaining options together
330+
*/
325331
@NonNull
326-
abstract Builder routeIndex(String routeIndex);
332+
public abstract Builder routeIndex(String routeIndex);
327333

328334
/**
329335
* Build a new {@link DirectionsRoute} object.

services-directions-models/src/test/java/com/mapbox/api/directions/v5/models/DirectionsResponseTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.mapbox.core.TestUtils;
55
import com.mapbox.geojson.Point;
66

7+
import java.util.List;
78
import org.junit.Assert;
89
import org.junit.Test;
910

@@ -15,6 +16,7 @@
1516
public class DirectionsResponseTest extends TestUtils {
1617

1718
private static final String DIRECTIONS_V5_PRECISION6_FIXTURE = "directions_v5_precision_6.json";
19+
private static final String DIRECTIONS_V5_MULTIPLE_ROUTES = "directions_v5_multiple_routes.json";
1820

1921
@Test
2022
public void sanity() throws Exception {
@@ -41,7 +43,6 @@ public void testToFromJson() throws Exception {
4143
String jsonString = responseFromJson1.toJson();
4244
DirectionsResponse responseFromJson2 = DirectionsResponse.fromJson(jsonString);
4345

44-
4546
Assert.assertEquals(responseFromJson1, responseFromJson2);
4647
Assert.assertEquals(responseFromJson2, responseFromJson1);
4748
}
@@ -62,4 +63,14 @@ public void fromJson_correctlyBuildsFromJsonWithOptionsAndUuid() throws Exceptio
6263
assertEquals(options, response.routes().get(0).routeOptions());
6364
assertEquals(uuid, response.routes().get(0).requestUuid());
6465
}
66+
67+
@Test
68+
public void fromJson_multipleRoutesHaveCorrectIndices() throws Exception {
69+
String json = loadJsonFixture(DIRECTIONS_V5_MULTIPLE_ROUTES);
70+
71+
List<DirectionsRoute> routes = DirectionsResponse.fromJson(json).routes();
72+
73+
assertEquals("0", routes.get(0).routeIndex());
74+
assertEquals("1", routes.get(1).routeIndex());
75+
}
6576
}

services-directions-models/src/test/java/com/mapbox/api/directions/v5/models/DirectionsRouteTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,22 @@ public void directionsRoute_json_withOptionsAndUUID_roundTripping() throws Excep
9696

9797
assertEquals(route, newRoute);
9898
}
99+
100+
@Test
101+
public void directionsRoute_canSetIndex() throws Exception {
102+
String json = loadJsonFixture("directions_v5-with-closure_precision_6.json");
103+
RouteOptions options = RouteOptions.builder()
104+
.profile(DirectionsCriteria.PROFILE_DRIVING_TRAFFIC)
105+
.coordinatesList(new ArrayList<Point>() {{
106+
add(Point.fromLngLat(1.0, 1.0));
107+
add(Point.fromLngLat(2.0, 2.0));
108+
}})
109+
.build();
110+
String uuid = "123";
111+
DirectionsRoute route = DirectionsRoute.fromJson(json, options, uuid);
112+
113+
DirectionsRoute newRoute = route.toBuilder().routeIndex("0").build();
114+
115+
assertEquals("0", newRoute.routeIndex());
116+
}
99117
}

services-directions-models/src/test/resources/directions_v5_multiple_routes.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)