From 212dc57ffb38f1bf61195da0f48c8d36eb8498bb Mon Sep 17 00:00:00 2001 From: Langston Smith Date: Sat, 28 Mar 2020 07:37:45 -0700 Subject: [PATCH 1/2] adding max coordinate setting for matrix builder --- .../api/directions/v5/MapboxDirections.java | 2 +- .../mapbox/api/matrix/v1/MapboxMatrix.java | 35 +++++++++- .../api/matrix/v1/MapboxMatrixTest.java | 65 ++++++++++++++++++- 3 files changed, 98 insertions(+), 4 deletions(-) diff --git a/services-directions/src/main/java/com/mapbox/api/directions/v5/MapboxDirections.java b/services-directions/src/main/java/com/mapbox/api/directions/v5/MapboxDirections.java index 371799a38..0ad1e40ea 100644 --- a/services-directions/src/main/java/com/mapbox/api/directions/v5/MapboxDirections.java +++ b/services-directions/src/main/java/com/mapbox/api/directions/v5/MapboxDirections.java @@ -382,7 +382,7 @@ public static Builder builder() { */ @AutoValue.Builder public abstract static class Builder { - //TODO change List to a custom model or to a Pair + // TODO: change List to a custom model or to a Pair private List> bearings = new ArrayList<>(); private List coordinates = new ArrayList<>(); private List annotations = new ArrayList<>(); diff --git a/services-matrix/src/main/java/com/mapbox/api/matrix/v1/MapboxMatrix.java b/services-matrix/src/main/java/com/mapbox/api/matrix/v1/MapboxMatrix.java index c05e202ce..2fb170306 100644 --- a/services-matrix/src/main/java/com/mapbox/api/matrix/v1/MapboxMatrix.java +++ b/services-matrix/src/main/java/com/mapbox/api/matrix/v1/MapboxMatrix.java @@ -138,6 +138,7 @@ public abstract static class Builder { private String[] approaches; private Integer[] destinations; private Integer[] sources; + private Integer coordinateListSizeLimit; /** * The username for the account that the directions engine runs on. In most cases, this should @@ -293,6 +294,25 @@ public Builder sources(@Nullable Integer... sources) { */ public abstract Builder baseUrl(@NonNull String baseUrl); + /** + * Override the standard maximum coordinate list size of 25 so that you can + * make a Matrix API call with a list of coordinates as large as the value you give to + * this method. + * + * You should only use this method if the Mapbox team has enabled your Mapbox + * account to be able to request Matrix API information with a list of more than 25 + * coordinates. + * + * @param coordinateListSizeLimit the max limit of coordinates used by a single call + * + * @return this builder for chaining options together + * @since 5.1.0 + */ + public Builder coordinateListSizeLimit(@NonNull Integer coordinateListSizeLimit) { + this.coordinateListSizeLimit = coordinateListSizeLimit; + return this; + } + abstract MapboxMatrix autoBuild(); /** @@ -307,8 +327,19 @@ public MapboxMatrix build() { if (coordinates == null || coordinates.size() < 2) { throw new ServicesException("At least two coordinates must be provided with your API" + " request."); - } else if (coordinates.size() > 25) { - throw new ServicesException("Maximum of 25 coordinates are allowed for this API."); + } else if (coordinateListSizeLimit != null && coordinateListSizeLimit < 0) { + throw new ServicesException("If you're going to use the coordinateListSizeLimit() method, " + + "please pass through a number that's greater than zero."); + } else if (coordinateListSizeLimit == null && coordinates.size() > 25) { + throw new ServicesException("A maximum of 25 coordinates is the default " + + " allowed for this API. If your Mapbox account has been enabled by the" + + " Mapbox team to make a request with more than 25 coordinates, please use" + + " the builder's coordinateListSizeLimit() method and pass through your account" + + "-specific maximum."); + } else if (coordinateListSizeLimit != null && coordinateListSizeLimit < coordinates.size()) { + throw new ServicesException("If you're going to use the coordinateListSizeLimit() method," + + " please pass through a number that's greater than the size of" + + " your coordinate list."); } coordinates(formatCoordinates(coordinates)); diff --git a/services-matrix/src/test/java/com/mapbox/api/matrix/v1/MapboxMatrixTest.java b/services-matrix/src/test/java/com/mapbox/api/matrix/v1/MapboxMatrixTest.java index c18838bff..062525acf 100644 --- a/services-matrix/src/test/java/com/mapbox/api/matrix/v1/MapboxMatrixTest.java +++ b/services-matrix/src/test/java/com/mapbox/api/matrix/v1/MapboxMatrixTest.java @@ -27,7 +27,9 @@ import static com.mapbox.api.directions.v5.DirectionsCriteria.APPROACH_CURB; import static org.hamcrest.core.StringStartsWith.startsWith; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; public class MapboxMatrixTest extends TestUtils { @@ -149,7 +151,13 @@ public void validCoordinatesTotal() throws ServicesException { } thrown.expect(ServicesException.class); - thrown.expectMessage(startsWith("Maximum of 25 coordinates are allowed for this API.")); + thrown.expectMessage(startsWith( + "A maximum of 25 coordinates is the default " + + " allowed for this API. If your Mapbox account has been enabled by the" + + " Mapbox team to make a request with more than 25 coordinates, please use" + + " the builder's coordinateListSizeLimit() method and pass through your account" + + "-specific maximum." + )); MapboxMatrix.builder() .accessToken(ACCESS_TOKEN) .profile(DirectionsCriteria.PROFILE_DRIVING) @@ -222,4 +230,59 @@ public void annotationsAndApproaches() throws ServicesException, IOException { assertEquals(27192.3, response.body().distances().get(0)[2], DELTA); assertEquals("McAllister Street", response.body().destinations().get(0).name()); } + + @Test + public void destinationListSizeLimitCheckThatCallWorks() throws ServicesException, IOException { + int total = 35; + ArrayList positions = new ArrayList<>(); + for (int i = 0; i < total; i++) { + positions.add(Point.fromLngLat(0.0, 0.0)); + } + + MapboxMatrix client = MapboxMatrix.builder() + .accessToken(ACCESS_TOKEN) + .profile(DirectionsCriteria.PROFILE_DRIVING) + .coordinateListSizeLimit(50) + .coordinates(positions) + .build(); + + Response response = client.executeCall(); + assertFalse(response.isSuccessful()); + assertNull(response.body()); + } + + @Test + public void build_invalidCoordinateAndDefaultMaxExceptionThrown() throws Exception { + int total = 35; + ArrayList positions = new ArrayList<>(); + for (int i = 0; i < total; i++) { + positions.add(Point.fromLngLat(0.0, 0.0)); + } + + thrown.expect(ServicesException.class); + thrown.expectMessage("If you're going to use the coordinateListSizeLimit() method," + + " please pass through a number that's greater than the size of your coordinate list."); + MapboxMatrix.builder() + .accessToken(ACCESS_TOKEN) + .coordinateListSizeLimit(20) + .coordinates(positions) + .build(); + } + + @Test + public void build_invalidLessThanZeroDefaultMaxExceptionThrown() throws Exception { + int total = 20; + ArrayList positions = new ArrayList<>(); + for (int i = 0; i < total; i++) { + positions.add(Point.fromLngLat(0.0, 0.0)); + } + thrown.expect(ServicesException.class); + thrown.expectMessage("If you're going to use the coordinateListSizeLimit() method, " + + "please pass through a number that's greater than zero."); + MapboxMatrix.builder() + .accessToken(ACCESS_TOKEN) + .coordinates(positions) + .coordinateListSizeLimit(-3) + .build(); + } } From 0b2dd50a0838a99cad90cf9b7183cc1c3e45cdcc Mon Sep 17 00:00:00 2001 From: Langston Smith Date: Sat, 28 Mar 2020 22:06:21 -0700 Subject: [PATCH 2/2] message adjustment --- .../src/main/java/com/mapbox/api/matrix/v1/MapboxMatrix.java | 2 +- .../test/java/com/mapbox/api/matrix/v1/MapboxMatrixTest.java | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/services-matrix/src/main/java/com/mapbox/api/matrix/v1/MapboxMatrix.java b/services-matrix/src/main/java/com/mapbox/api/matrix/v1/MapboxMatrix.java index 2fb170306..32e145534 100644 --- a/services-matrix/src/main/java/com/mapbox/api/matrix/v1/MapboxMatrix.java +++ b/services-matrix/src/main/java/com/mapbox/api/matrix/v1/MapboxMatrix.java @@ -338,7 +338,7 @@ public MapboxMatrix build() { + "-specific maximum."); } else if (coordinateListSizeLimit != null && coordinateListSizeLimit < coordinates.size()) { throw new ServicesException("If you're going to use the coordinateListSizeLimit() method," - + " please pass through a number that's greater than the size of" + + " please pass through a number that's equal to or greater than the size of" + " your coordinate list."); } diff --git a/services-matrix/src/test/java/com/mapbox/api/matrix/v1/MapboxMatrixTest.java b/services-matrix/src/test/java/com/mapbox/api/matrix/v1/MapboxMatrixTest.java index 062525acf..6c854af82 100644 --- a/services-matrix/src/test/java/com/mapbox/api/matrix/v1/MapboxMatrixTest.java +++ b/services-matrix/src/test/java/com/mapbox/api/matrix/v1/MapboxMatrixTest.java @@ -260,8 +260,9 @@ public void build_invalidCoordinateAndDefaultMaxExceptionThrown() throws Excepti } thrown.expect(ServicesException.class); - thrown.expectMessage("If you're going to use the coordinateListSizeLimit() method," + - " please pass through a number that's greater than the size of your coordinate list."); + thrown.expectMessage("If you're going to use the coordinateListSizeLimit() method," + + " please pass through a number that's equal to or greater than the size of" + + " your coordinate list."); MapboxMatrix.builder() .accessToken(ACCESS_TOKEN) .coordinateListSizeLimit(20)