Skip to content

Commit f4afe30

Browse files
authored
Numbers should be numbers (no quotes) in route request (#107)
* Numbers should be numbers (no quotes) in route request * Heading should be int (not double)
1 parent 56a52a9 commit f4afe30

File tree

8 files changed

+171
-51
lines changed

8 files changed

+171
-51
lines changed

library/src/main/java/com/mapzen/valhalla/JSON.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,39 @@
77
import java.util.List;
88

99
public class JSON {
10+
public static final int HEADING_NONE = -1;
11+
1012
public List<JSON.Location> locations = new ArrayList<>();
1113
public String costing;
1214

1315
@SerializedName("directions_options")
1416
public DirectionOptions directionsOptions = new DirectionOptions();
1517

1618
public static class Location {
17-
public String lat;
18-
public String lon;
19+
public double lat;
20+
public double lon;
1921
public String name;
2022
public String street;
2123
public String city;
2224
public String state;
23-
public String heading;
25+
public int heading = HEADING_NONE;
2426

25-
public Location(String lat, String lon) {
27+
public Location(double lat, double lon) {
2628
this.lat = lat;
2729
this.lon = lon;
2830
}
2931

30-
public Location(String lat, String lon, String heading) {
32+
public Location(double lat, double lon, int heading) {
33+
if (heading < 0 || heading >= 360) {
34+
throw new IllegalArgumentException("Heading value must in the range [0, 360)");
35+
}
36+
3137
this.lat = lat;
3238
this.lon = lon;
3339
this.heading = heading;
3440
}
3541

36-
public Location(String lat, String lon, String name, String street,
42+
public Location(double lat, double lon, String name, String street,
3743
String city, String state) {
3844
this.lat = lat;
3945
this.lon = lon;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.mapzen.valhalla;
2+
3+
import com.google.gson.GsonBuilder;
4+
import com.google.gson.JsonElement;
5+
import com.google.gson.JsonObject;
6+
import com.google.gson.JsonSerializationContext;
7+
import com.google.gson.JsonSerializer;
8+
9+
import java.lang.reflect.Type;
10+
11+
class LocationSerializer implements JsonSerializer<JSON.Location> {
12+
@Override public JsonElement serialize(JSON.Location src, Type typeOfSrc,
13+
JsonSerializationContext context) {
14+
JsonObject jsonObject = (JsonObject) new GsonBuilder().create().toJsonTree(src);
15+
if (src.heading < 0 || src.heading >= 360) {
16+
jsonObject.remove("heading");
17+
}
18+
19+
return jsonObject;
20+
}
21+
}

library/src/main/java/com/mapzen/valhalla/Router.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ interface Router {
4444
fun setBiking(): Router
4545
fun setMultimodal(): Router
4646
fun setLocation(point: DoubleArray): Router
47-
fun setLocation(point: DoubleArray, heading: Float): Router
47+
fun setLocation(point: DoubleArray, heading: Int): Router
4848
fun setLocation(point: DoubleArray,
4949
name: String? = null,
5050
street: String? = null,

library/src/main/java/com/mapzen/valhalla/ValhallaRouter.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.mapzen.valhalla
22

3-
import android.util.Log
43
import com.google.gson.Gson
4+
import com.google.gson.GsonBuilder
55
import retrofit2.Call
66
import retrofit2.Callback
77
import retrofit2.Response
@@ -18,7 +18,9 @@ open class ValhallaRouter : Router, Runnable {
1818

1919
private var httpHandler: HttpHandler? = null
2020

21-
var gson: Gson = Gson()
21+
private val gson: Gson = GsonBuilder()
22+
.registerTypeAdapter(JSON.Location::class.java, LocationSerializer())
23+
.create()
2224

2325
override fun setHttpHandler(handler: HttpHandler): Router {
2426
httpHandler = handler
@@ -51,13 +53,12 @@ open class ValhallaRouter : Router, Runnable {
5153
}
5254

5355
override fun setLocation(point: DoubleArray): Router {
54-
this.locations.add(JSON.Location(point[0].toString(), point[1].toString()))
56+
this.locations.add(JSON.Location(point[0], point[1]))
5557
return this
5658
}
5759

58-
override fun setLocation(point: DoubleArray, heading: Float): Router {
59-
this.locations.add(JSON.Location(point[0].toString(), point[1].toString(),
60-
heading.toInt().toString()))
60+
override fun setLocation(point: DoubleArray, heading: Int): Router {
61+
this.locations.add(JSON.Location(point[0], point[1], heading))
6162
return this
6263
}
6364

@@ -67,7 +68,7 @@ open class ValhallaRouter : Router, Runnable {
6768
city: String?,
6869
state: String?): Router {
6970

70-
this.locations.add(JSON.Location(point[0].toString(), point[1].toString(),
71+
this.locations.add(JSON.Location(point[0], point[1],
7172
name, street, city, state));
7273
return this
7374
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.mapzen.valhalla;
2+
3+
import org.junit.Test;
4+
5+
public class JSONTest {
6+
@Test
7+
public void shouldNotThrowIfHeadingEqualsZero() throws Exception {
8+
double lat = 0.0;
9+
double lon = 0.0;
10+
int heading = 0;
11+
new JSON.Location(lat, lon, heading);
12+
}
13+
14+
@Test(expected = IllegalArgumentException.class)
15+
public void shouldThrowIfHeadingLessThanZero() throws Exception {
16+
double lat = 0.0;
17+
double lon = 0.0;
18+
int heading = -1;
19+
new JSON.Location(lat, lon, heading);
20+
}
21+
22+
@Test(expected = IllegalArgumentException.class)
23+
public void shouldThrowIfHeadingEquals360() throws Exception {
24+
double lat = 0.0;
25+
double lon = 0.0;
26+
int heading = 360;
27+
new JSON.Location(lat, lon, heading);
28+
}
29+
30+
@Test(expected = IllegalArgumentException.class)
31+
public void shouldThrowIfHeadingIsGreaterThan360() throws Exception {
32+
double lat = 0.0;
33+
double lon = 0.0;
34+
int heading = 361;
35+
new JSON.Location(lat, lon, heading);
36+
}
37+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.mapzen.valhalla;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import java.util.ArrayList;
10+
11+
import static org.fest.assertions.api.Assertions.assertThat;
12+
13+
public class LocationSerializerTest {
14+
15+
private LocationSerializer locationSerializer;
16+
private JSON.Location location;
17+
18+
@Before public void setUp() throws Exception {
19+
double lat = 0.0;
20+
double lon = 0.0;
21+
int heading = 0;
22+
location = new JSON.Location(lat, lon, heading);
23+
locationSerializer = new LocationSerializer();
24+
}
25+
26+
@Test public void shouldIncludeHeading() throws Exception {
27+
assertThat(locationSerializer.serialize(location, JSON.Location.class, null).toString())
28+
.contains("\"heading\":0");
29+
}
30+
31+
@Test public void shouldExcludeHeadingLessThanZero() throws Exception {
32+
location.heading = -1;
33+
assertThat(locationSerializer.serialize(location, JSON.Location.class, null).toString())
34+
.doesNotContain("\"heading\":-1");
35+
}
36+
37+
@Test public void shouldExcludeHeadingEqualTo360() throws Exception {
38+
location.heading = 360;
39+
assertThat(locationSerializer.serialize(location, JSON.Location.class, null).toString())
40+
.doesNotContain("\"heading\":360");
41+
}
42+
43+
@Test public void shouldExcludeHeadingGreaterThan360() throws Exception {
44+
location.heading = 361;
45+
assertThat(locationSerializer.serialize(location, JSON.Location.class, null).toString())
46+
.doesNotContain("\"heading\":361");
47+
}
48+
49+
@Test public void shouldExcludeOnlyHeadingsOutOfRange() throws Exception {
50+
JSON.Location location1 = new JSON.Location(1d, 2d, 0);
51+
JSON.Location location2 = new JSON.Location(3d, 4d);
52+
ArrayList<JSON.Location> locations = new ArrayList<>();
53+
locations.add(location1);
54+
locations.add(location2);
55+
JSON json = new JSON();
56+
json.locations = locations;
57+
58+
Gson gson = new GsonBuilder()
59+
.registerTypeAdapter(JSON.Location.class, new LocationSerializer())
60+
.create();
61+
String result = gson.toJson(json);
62+
assertThat(result).contains("\"lat\":1.0,\"lon\":2.0,\"heading\":0");
63+
assertThat(result).doesNotContain("\"lat\":3.0,\"lon\":4.0,\"heading\":-1");
64+
}
65+
}

library/src/test/java/com/mapzen/valhalla/RouteTest.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.mapzen.model.ValhallaLocation;
44

5-
import com.google.common.io.Files;
6-
75
import org.apache.commons.io.FileUtils;
86
import org.fest.assertions.data.Offset;
97
import org.junit.Before;
@@ -18,7 +16,6 @@
1816

1917
import static com.mapzen.TestUtils.getLocation;
2018
import static java.lang.System.getProperty;
21-
import static java.nio.charset.Charset.defaultCharset;
2219
import static org.fest.assertions.api.Assertions.assertThat;
2320

2421
@RunWith(RobolectricTestRunner.class)
@@ -526,18 +523,4 @@ public void shouldParseUnits() throws Exception {
526523
route = getRoute("brooklyn_valhalla_miles");
527524
assertThat(route.getUnits()).isEqualTo(Router.DistanceUnits.MILES);
528525
}
529-
530-
private ArrayList<ValhallaLocation> getLocationsFromFile(String name) throws Exception {
531-
String fileName = getProperty("user.dir");
532-
File file = new File(fileName + "/src/test/fixtures/" + name + ".txt");
533-
ArrayList<ValhallaLocation> allLocations = new ArrayList<ValhallaLocation>();
534-
for(String locations: Files.readLines(file, defaultCharset())) {
535-
String[] latLng = locations.split(",");
536-
ValhallaLocation location = new ValhallaLocation();
537-
location.setLatitude(Double.valueOf(latLng[0]));
538-
location.setLongitude(Double.valueOf(latLng[1]));
539-
allLocations.add(location);
540-
}
541-
return allLocations;
542-
}
543526
}

library/src/test/java/com/mapzen/valhalla/RouterTest.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mapzen.valhalla;
22

33
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
45

56
import org.json.JSONObject;
67
import org.junit.After;
@@ -137,9 +138,9 @@ public void shouldClearLocations() throws Exception {
137138
router.setLocation(loc2);
138139
router.setLocation(loc3);
139140
JSON json = router.getJSONRequest();
140-
assertThat(json.locations.get(0).lat).doesNotContain("1.0");
141-
assertThat(json.locations.get(0).lat).contains("3.0");
142-
assertThat(json.locations.get(1).lat).contains("5.0");
141+
assertThat(json.locations.get(0).lat).isNotEqualTo(1.0);
142+
assertThat(json.locations.get(0).lat).isEqualTo(3.0);
143+
assertThat(json.locations.get(1).lat).isEqualTo(5.0);
143144
}
144145

145146
@Test(expected=MalformedURLException.class)
@@ -160,10 +161,10 @@ public void shouldAddLocations() throws Exception {
160161
.setLocation(loc1)
161162
.setLocation(loc2)
162163
.getJSONRequest();
163-
assertThat(json.locations.get(0).lat).contains("1.0");
164-
assertThat(json.locations.get(0).lon).contains("2.0");
165-
assertThat(json.locations.get(1).lat).contains("3.0");
166-
assertThat(json.locations.get(1).lon).contains("4.0");
164+
assertThat(json.locations.get(0).lat).isEqualTo(1.0);
165+
assertThat(json.locations.get(0).lon).isEqualTo(2.0);
166+
assertThat(json.locations.get(1).lat).isEqualTo(3.0);
167+
assertThat(json.locations.get(1).lon).isEqualTo(4.0);
167168
}
168169

169170
@Test
@@ -178,14 +179,14 @@ public void shouldAddWaypoints() throws Exception {
178179
.setLocation(loc3)
179180
.setLocation(loc4)
180181
.getJSONRequest();
181-
assertThat(json.locations.get(0).lat).contains("1.0");
182-
assertThat(json.locations.get(0).lon).contains("2.0");
183-
assertThat(json.locations.get(1).lat).contains("3.0");
184-
assertThat(json.locations.get(1).lon).contains("4.0");
185-
assertThat(json.locations.get(2).lat).contains("5.0");
186-
assertThat(json.locations.get(2).lon).contains("6.0");
187-
assertThat(json.locations.get(3).lat).contains("7.0");
188-
assertThat(json.locations.get(3).lon).contains("8.0");
182+
assertThat(json.locations.get(0).lat).isEqualTo(1.0);
183+
assertThat(json.locations.get(0).lon).isEqualTo(2.0);
184+
assertThat(json.locations.get(1).lat).isEqualTo(3.0);
185+
assertThat(json.locations.get(1).lon).isEqualTo(4.0);
186+
assertThat(json.locations.get(2).lat).isEqualTo(5.0);
187+
assertThat(json.locations.get(2).lon).isEqualTo(6.0);
188+
assertThat(json.locations.get(3).lat).isEqualTo(7.0);
189+
assertThat(json.locations.get(3).lon).isEqualTo(8.0);
189190
}
190191

191192
@Test
@@ -277,8 +278,11 @@ public void setLocation_shouldAppendName() throws Exception {
277278
double[] loc = new double[] {1.0, 2.0};
278279
router = new ValhallaRouter().setLocation(loc)
279280
.setLocation(loc, "Acme", null, null, null);
280-
assertThat(new Gson().toJson(router.getJSONRequest()))
281-
.contains("{\"lat\":\"1.0\",\"lon\":\"2.0\",\"name\":\"Acme\"}");
281+
Gson gson = new GsonBuilder()
282+
.registerTypeAdapter(JSON.Location.class, new LocationSerializer())
283+
.create();
284+
assertThat(gson.toJson(router.getJSONRequest()))
285+
.contains("{\"lat\":1.0,\"lon\":2.0,\"name\":\"Acme\"}");
282286
}
283287

284288
@Test
@@ -292,8 +296,11 @@ public void setLocation_shouldAppendStreetAddress() throws Exception {
292296
double[] loc = new double[] {1.0, 2.0};
293297
router = new ValhallaRouter()
294298
.setLocation(loc).setLocation(loc, "Acme", "North Main Street", "Doylestown", "PA");
295-
assertThat(new Gson().toJson(router.getJSONRequest()))
296-
.contains("{\"lat\":\"1.0\",\"lon\":\"2.0\","
299+
Gson gson = new GsonBuilder()
300+
.registerTypeAdapter(JSON.Location.class, new LocationSerializer())
301+
.create();
302+
assertThat(gson.toJson(router.getJSONRequest()))
303+
.contains("{\"lat\":1.0,\"lon\":2.0,"
297304
+ "\"name\":\"Acme\","
298305
+ "\"street\":\"North Main Street\","
299306
+ "\"city\":\"Doylestown\","
@@ -305,7 +312,7 @@ public void setLocation_shouldIncludeHeading() throws Exception {
305312
double[] loc = new double[] {1.0, 2.0};
306313
router = new ValhallaRouter().setLocation(loc, 180).setLocation(loc);
307314
assertThat(new Gson().toJson(router.getJSONRequest()))
308-
.contains("{\"lat\":\"1.0\",\"lon\":\"2.0\",\"heading\":\"180\"}");
315+
.contains("{\"lat\":1.0,\"lon\":2.0,\"heading\":180}");
309316
}
310317

311318
@Test

0 commit comments

Comments
 (0)