Skip to content

Commit

Permalink
Route options should be serialized/deserialized
Browse files Browse the repository at this point in the history
using the matching names from backend api
  • Loading branch information
“osana” committed Sep 27, 2018
1 parent 0890444 commit 51d6222
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.SerializedName;
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
import com.mapbox.api.directions.v5.DirectionsCriteria;
import com.mapbox.api.directions.v5.MapboxDirections;
Expand Down Expand Up @@ -134,6 +135,7 @@ public static Builder builder() {
* not during the initial request
* @since 3.0.0
*/
@SerializedName("continue_straight")
@Nullable
public abstract Boolean continueStraight();

Expand All @@ -145,6 +147,7 @@ public static Builder builder() {
* during the initial route request
* @since 3.1.0
*/
@SerializedName("roundabout_exits")
@Nullable
public abstract Boolean roundaboutExits();

Expand Down Expand Up @@ -201,6 +204,7 @@ public static Builder builder() {
* @return true if the original request included voice instructions
* @since 3.0.0
*/
@SerializedName("voice_instructions")
@Nullable
public abstract Boolean voiceInstructions();

Expand All @@ -210,6 +214,7 @@ public static Builder builder() {
* @return true if the original request included banner instructions
* @since 3.0.0
*/
@SerializedName("banner_instructions")
@Nullable
public abstract Boolean bannerInstructions();

Expand All @@ -219,6 +224,7 @@ public static Builder builder() {
* @return a string matching either imperial or metric
* @since 3.0.0
*/
@SerializedName("voice_units")
@Nullable
public abstract String voiceUnits();

Expand All @@ -228,6 +234,7 @@ public static Builder builder() {
* @return a string representing the Mapbox access token
* @since 3.0.0
*/
@SerializedName("access_token")
@NonNull
public abstract String accessToken();

Expand All @@ -239,6 +246,7 @@ public static Builder builder() {
* @return a string containing the request UUID
* @since 3.0.0
*/
@SerializedName("uuid")
@NonNull
public abstract String requestUuid();

Expand Down Expand Up @@ -267,6 +275,7 @@ public static Builder builder() {
* @return a string representing names for each waypoint
* @since 3.3.0
*/
@SerializedName("waypoint_names")
@Nullable
public abstract String waypointNames();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

Expand Down Expand Up @@ -214,4 +215,93 @@ public void toJson_fromJson() throws Exception {

assertEquals(routeOptions, routeOptionsFromJson);
}

@Test
public void fromJson() {
String jsonString = "{" +
"\"profile\": \"auto\"," +
"\"user\": \"mapbox\"," +
"\"baseUrl\": \"https://api.mapbox.com\"," +
"\"coordinates\": [[-3.707788,40.395039],[-3.712179,40.401819]]," +
"\"access_token\": \"ACCESS_TOKEN\"," +
"\"geometries\": \"polyline6\"," +
"\"overview\": \"full\"," +
"\"steps\": true," +
"\"bearings\": \";\"," +
"\"continue_straight\": true," +
"\"annotations\": \"congestion,distance\"," +
"\"language\": \"en\"," +
"\"roundabout_exits\": true," +
"\"voice_instructions\": true," +
"\"banner_instructions\": true," +
"\"voice_units\": \"imperial\"," +
"\"uuid\": \"uuid1\"" +
"}";

RouteOptions routeOptions = RouteOptions.fromJson(jsonString);

assertEquals("auto", routeOptions.profile());
assertEquals("mapbox", routeOptions.user());
assertEquals("https://api.mapbox.com", routeOptions.baseUrl());
assertEquals(2, routeOptions.coordinates().size());
assertEquals("ACCESS_TOKEN", routeOptions.accessToken());
assertEquals("polyline6", routeOptions.geometries());
assertEquals("full", routeOptions.overview());
assertEquals(true, routeOptions.steps());
assertEquals(";", routeOptions.bearings());
assertEquals(true, routeOptions.continueStraight());
assertEquals("congestion,distance", routeOptions.annotations());
assertEquals("en", routeOptions.language());
assertEquals(true, routeOptions.roundaboutExits());
assertEquals(true, routeOptions.voiceInstructions());
assertEquals(true, routeOptions.bannerInstructions());
assertEquals("imperial", routeOptions.voiceUnits());
assertEquals("uuid1", routeOptions.requestUuid());
}

@Test
public void toJson() {
RouteOptions routeOptions = RouteOptions.builder()
.profile("auto")
.user("mapbox")
.coordinates(Arrays.asList(Point.fromLngLat(-3.707788, 40.395039),
Point.fromLngLat(-3.712179, 40.401819)))
.accessToken("ACCESS_TOKEN")
.baseUrl("https://api.mapbox.com")
.geometries("polyline6")
.overview("full")
.steps(true)
.bearings(";")
.continueStraight(true)
.annotations("congestion,distance")
.language("en")
.roundaboutExits(true)
.voiceInstructions(true)
.bannerInstructions(true)
.voiceUnits("imperial")
.requestUuid("uuid1")
.build();

String jsonString = routeOptions.toJson();

String expectedJsonString = "{" +
"\"profile\": \"auto\"," +
"\"user\": \"mapbox\"," +
"\"baseUrl\": \"https://api.mapbox.com\"," +
"\"coordinates\": [[-3.707788,40.395039],[-3.712179,40.401819]]," +
"\"access_token\": \"ACCESS_TOKEN\"," +
"\"geometries\": \"polyline6\"," +
"\"overview\": \"full\"," +
"\"steps\": true," +
"\"bearings\": \";\"," +
"\"continue_straight\": true," +
"\"annotations\": \"congestion,distance\"," +
"\"language\": \"en\"," +
"\"roundabout_exits\": true," +
"\"voice_instructions\": true," +
"\"banner_instructions\": true," +
"\"voice_units\": \"imperial\"," +
"\"uuid\": \"uuid1\"}";
compareJson(expectedJsonString, jsonString);
}
}

0 comments on commit 51d6222

Please sign in to comment.