diff --git a/build.gradle b/build.gradle index e5839a87f..2d4e547b1 100644 --- a/build.gradle +++ b/build.gradle @@ -22,7 +22,7 @@ buildscript { tasks.withType(JavaCompile) { options.encoding = 'UTF-8' - options.compilerArgs << '-Xlint:all' << '-Xlint:unchecked' + options.compilerArgs += ['-Xlint:all', '-Xlint:unchecked', 'autovaluegson.defaultCollectionsToEmpty:true'] } allprojects { diff --git a/gradle/dependencies.gradle b/gradle/dependencies.gradle index 0add5d165..c28215ee9 100644 --- a/gradle/dependencies.gradle +++ b/gradle/dependencies.gradle @@ -2,7 +2,7 @@ ext { version = [ autoValue : '1.5', - autoValueGson : '0.6.0', + autoValueGson : '0.7.0', junit : '4.12', supportLibVersion: '26.1.0', gson : '2.8.2', diff --git a/services-core/src/test/java/com/mapbox/core/TestUtils.java b/services-core/src/test/java/com/mapbox/core/TestUtils.java index 7db719ea1..7b619ba31 100644 --- a/services-core/src/test/java/com/mapbox/core/TestUtils.java +++ b/services-core/src/test/java/com/mapbox/core/TestUtils.java @@ -1,6 +1,11 @@ package com.mapbox.core; +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + import com.google.gson.JsonParser; +import org.hamcrest.Matchers; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -11,18 +16,14 @@ import java.io.Serializable; import java.util.Scanner; -import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - public class TestUtils { public static final double DELTA = 1E-10; public static final String ACCESS_TOKEN = "pk.XXX"; - public void compareJson(String json1, String json2) { + public void compareJson(String expectedJson, String actualJson) { JsonParser parser = new JsonParser(); - assertEquals(parser.parse(json1), parser.parse(json2)); + assertThat(parser.parse(actualJson), Matchers.equalTo(parser.parse(expectedJson))); } protected String loadJsonFixture(String filename) throws IOException { diff --git a/services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/models/CarmenContext.java b/services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/models/CarmenContext.java index 4049824de..c3cb9ffd9 100644 --- a/services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/models/CarmenContext.java +++ b/services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/models/CarmenContext.java @@ -1,10 +1,16 @@ package com.mapbox.api.geocoding.v5.models; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.google.auto.value.AutoValue; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.TypeAdapter; import com.google.gson.annotations.SerializedName; +import com.mapbox.geojson.Geometry; +import com.mapbox.geojson.Point; +import com.mapbox.geojson.gson.GeometryDeserializer; +import com.mapbox.geojson.gson.PointDeserializer; import java.io.Serializable; @@ -29,6 +35,14 @@ public static Builder builder() { return new AutoValue_CarmenContext.Builder(); } + @SuppressWarnings("unused") + public static CarmenContext fromJson(@NonNull String json) { + Gson gson = new GsonBuilder() + .registerTypeAdapterFactory(GeocodingAdapterFactory.create()) + .create(); + return gson.fromJson(json, CarmenContext.class); + } + /** * ID of the feature of the form {index}.{id} where index is the id/handle of the data-source * that contributed the result. @@ -99,12 +113,30 @@ public static TypeAdapter typeAdapter(Gson gson) { return new AutoValue_CarmenContext.GsonTypeAdapter(gson); } + @SuppressWarnings("unused") + public String toJson() { + Gson gson = new GsonBuilder() + .registerTypeAdapterFactory(GeocodingAdapterFactory.create()) + .create(); + return gson.toJson(this); + } + + /** + * Convert current instance values into another Builder to quickly change one or more values. + * + * @return a new instance of {@link CarmenContext} using the newly defined values + * @since 3.0.0 + */ + @SuppressWarnings("unused") + public abstract Builder toBuilder(); + /** * This builder can be used to set the values describing the {@link CarmenFeature}. * * @since 3.0.0 */ @AutoValue.Builder + @SuppressWarnings("unused") public abstract static class Builder { /** diff --git a/services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/models/CarmenFeature.java b/services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/models/CarmenFeature.java index 48da03e35..df56bdedc 100644 --- a/services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/models/CarmenFeature.java +++ b/services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/models/CarmenFeature.java @@ -3,24 +3,20 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.google.auto.value.AutoValue; -import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; import com.google.gson.TypeAdapter; -import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; import com.mapbox.api.geocoding.v5.GeocodingCriteria.GeocodingTypeCriteria; import com.mapbox.geojson.BoundingBox; import com.mapbox.geojson.Feature; import com.mapbox.geojson.Geometry; import com.mapbox.geojson.Point; -import com.mapbox.geojson.gson.BoundingBoxDeserializer; import com.mapbox.geojson.gson.BoundingBoxSerializer; -import com.mapbox.geojson.gson.GeoJsonAdapterFactory; import com.mapbox.geojson.gson.GeometryDeserializer; +import com.mapbox.geojson.gson.GeometryTypeAdapter; import com.mapbox.geojson.gson.PointDeserializer; -import com.mapbox.geojson.gson.PointSerializer; import java.io.Serializable; import java.util.List; @@ -46,20 +42,8 @@ @AutoValue public abstract class CarmenFeature implements Serializable { - @Expose - @SerializedName("type") private static final String TYPE = "Feature"; - /** - * Create a new instance of this class by using the {@link Builder} class. - * - * @return this classes {@link Builder} for creating a new instance - * @since 3.0.0 - */ - public static Builder builder() { - return new AutoValue_CarmenFeature.Builder(); - } - /** * Create a CarmenFeature object from JSON. * @@ -67,17 +51,31 @@ public static Builder builder() { * @return this class using the defined information in the provided JSON string * @since 2.0.0 */ + @NonNull public static CarmenFeature fromJson(@NonNull String json) { - GsonBuilder gson = new GsonBuilder(); - gson.registerTypeAdapterFactory(GeocodingAdapterFactory.create()); - gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create()); - gson.registerTypeAdapter(Point.class, new PointDeserializer()); - gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxDeserializer()); - gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer()); - return gson.create().fromJson(json, CarmenFeature.class); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Point.class, new PointDeserializer()) + .registerTypeAdapter(Geometry.class, new GeometryDeserializer()) + .registerTypeAdapterFactory(GeocodingAdapterFactory.create()) + .create(); + return gson.fromJson(json, CarmenFeature.class); + } + + /** + * Create a new instance of this class by using the {@link Builder} class. + * + * @return this classes {@link Builder} for creating a new instance + * @since 3.0.0 + */ + @NonNull + public static Builder builder() { + return new AutoValue_CarmenFeature.Builder() + .type(TYPE); } + // // Feature specific attributes + // /** * This describes the TYPE of GeoJson geometry this object is, thus this will always return @@ -88,9 +86,8 @@ public static CarmenFeature fromJson(@NonNull String json) { * @since 1.0.0 */ @NonNull - public String type() { - return TYPE; - } + @SerializedName("type") + public abstract String type(); /** * A {@link CarmenFeature} might have a member named {@code bbox} to include information on the @@ -136,7 +133,9 @@ public String type() { @NonNull public abstract JsonObject properties(); + // // CarmenFeature specific attributes + // /** * A string representing the feature in the requested language, if specified. @@ -281,13 +280,14 @@ public static TypeAdapter typeAdapter(Gson gson) { * @return a JSON string which represents this CarmenFeature * @since 3.0.0 */ + @SuppressWarnings("unused") public String toJson() { - GsonBuilder gson = new GsonBuilder(); - gson.registerTypeAdapter(Point.class, new PointSerializer()); - gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()); - gson.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT); - gson.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); - return gson.create().toJson(this); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Geometry.class, new GeometryTypeAdapter()) + .registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()) + .registerTypeAdapterFactory(GeocodingAdapterFactory.create()) + .create(); + return gson.toJson(this, CarmenFeature.class); } /** @@ -296,6 +296,7 @@ public String toJson() { * @return a new instance of {@link CarmenFeature} using the newly defined values * @since 3.0.0 */ + @SuppressWarnings("unused") public abstract Builder toBuilder(); /** @@ -304,8 +305,12 @@ public String toJson() { * @since 3.0.0 */ @AutoValue.Builder + @SuppressWarnings("unused") public abstract static class Builder { + // Type will always be set to "Feature" + abstract Builder type(@NonNull String type); + /** * A Feature might have a member named {@code bbox} to include information on the coordinate * range for it's {@link Feature}s. The value of the bbox member MUST be a list of size 2*n diff --git a/services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/models/GeocodingResponse.java b/services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/models/GeocodingResponse.java index dbf9713a2..c2499c428 100644 --- a/services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/models/GeocodingResponse.java +++ b/services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/models/GeocodingResponse.java @@ -1,22 +1,19 @@ package com.mapbox.api.geocoding.v5.models; import android.support.annotation.NonNull; -import android.support.annotation.Nullable; import com.google.auto.value.AutoValue; -import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.TypeAdapter; -import com.mapbox.geojson.gson.GeoJsonAdapterFactory; -import com.mapbox.geojson.gson.GeometryDeserializer; +import com.mapbox.geojson.BoundingBox; import com.mapbox.geojson.FeatureCollection; +import com.mapbox.geojson.Geometry; +import com.mapbox.geojson.Point; import com.mapbox.geojson.gson.BoundingBoxDeserializer; import com.mapbox.geojson.gson.BoundingBoxSerializer; +import com.mapbox.geojson.gson.GeometryDeserializer; +import com.mapbox.geojson.gson.GeometryTypeAdapter; import com.mapbox.geojson.gson.PointDeserializer; -import com.mapbox.geojson.Geometry; -import com.mapbox.geojson.Point; -import com.mapbox.geojson.BoundingBox; -import com.mapbox.geojson.gson.PointSerializer; import java.io.Serializable; import java.util.List; @@ -34,31 +31,34 @@ public abstract class GeocodingResponse implements Serializable { private static final String TYPE = "FeatureCollection"; /** - * Create a new instance of this class by using the {@link Builder} class. + * Create a new instance of this class by passing in a formatted valid JSON String. * - * @return this classes {@link Builder} for creating a new instance + * @param json a formatted valid JSON string defining a GeoJson Geocoding Response + * @return a new instance of this class defined by the values passed inside this static factory + * method * @since 3.0.0 */ - public static Builder builder() { - return new AutoValue_GeocodingResponse.Builder() - .type(TYPE); + @NonNull + public static GeocodingResponse fromJson(@NonNull String json) { + Gson gson = new GsonBuilder() + .registerTypeAdapter(Point.class, new PointDeserializer()) + .registerTypeAdapter(Geometry.class, new GeometryDeserializer()) + .registerTypeAdapter(BoundingBox.class, new BoundingBoxDeserializer()) + .registerTypeAdapterFactory(GeocodingAdapterFactory.create()) + .create(); + return gson.fromJson(json, GeocodingResponse.class); } /** - * Create a new instance of this class by passing in a formatted valid JSON String. + * Create a new instance of this class by using the {@link Builder} class. * - * @param json a formatted valid JSON string defining a GeoJson Geocoding Response - * @return a new instance of this class defined by the values passed inside this static factory - * method - * @since 1.0.0 + * @return this classes {@link Builder} for creating a new instance + * @since 3.0.0 */ - public static GeocodingResponse fromJson(String json) { - GsonBuilder gson = new GsonBuilder(); - gson.registerTypeAdapter(Point.class, new PointDeserializer()); - gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer()); - gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxDeserializer()); - gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create()); - return gson.create().fromJson(json, GeocodingResponse.class); + @NonNull + public static Builder builder() { + return new AutoValue_GeocodingResponse.Builder() + .type(TYPE); } /** @@ -68,29 +68,16 @@ public static GeocodingResponse fromJson(String json) { * @return the type of GeoJSON this is * @since 1.0.0 */ - @Nullable + @NonNull public abstract String type(); - /** - * A Geocoding Response might have a member named {@code bbox} to include information on the - * coordinate range for it's {@link CarmenFeature}s. The value of the bbox member MUST be a list - * of size 2*n where n is the number of dimensions represented in the contained feature - * geometries, with all axes of the most southwesterly point followed by all axes of the more - * northeasterly point. The axes order of a bbox follows the axes order of geometries. - * - * @return a list of double coordinate values describing a bounding box - * @since 3.0.0 - */ - @Nullable - public abstract BoundingBox bbox(); - /** * A list of space and punctuation-separated strings from the original query. * * @return a list containing the original query * @since 1.0.0 */ - @Nullable + @NonNull public abstract List query(); /** @@ -101,7 +88,7 @@ public static GeocodingResponse fromJson(String json) { * query * @since 1.0.0 */ - @Nullable + @NonNull public abstract List features(); /** @@ -111,9 +98,21 @@ public static GeocodingResponse fromJson(String json) { * @return information about Mapbox's terms of service and the data sources * @since 1.0.0 */ - @Nullable + @NonNull public abstract String attribution(); + /** + * Convert the current {@link GeocodingResponse} to its builder holding the currently assigned + * values. This allows you to modify a single variable and then rebuild the object resulting in + * an updated and modified {@link GeocodingResponse}. + * + * @return a {@link GeocodingResponse.Builder} with the same values set to match the ones defined + * in this {@link GeocodingResponse} + * @since 3.0.0 + */ + @NonNull + public abstract Builder toBuilder(); + /** * This takes the currently defined values found inside this instance and converts it to a GeoJson * string. @@ -121,12 +120,14 @@ public static GeocodingResponse fromJson(String json) { * @return a JSON string which represents this Geocoding Response * @since 1.0.0 */ + @NonNull public String toJson() { - GsonBuilder gson = new GsonBuilder(); - gson.registerTypeAdapter(Point.class, new PointSerializer()); - gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()); - gson.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); - return gson.create().toJson(this); + Gson gson = new GsonBuilder() + .registerTypeAdapter(Geometry.class, new GeometryTypeAdapter()) + .registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()) + .registerTypeAdapterFactory(GeocodingAdapterFactory.create()) + .create(); + return gson.toJson(this, GeocodingResponse.class); } /** @@ -146,6 +147,7 @@ public static TypeAdapter typeAdapter(Gson gson) { * @since 3.0.0 */ @AutoValue.Builder + @SuppressWarnings("unused") public abstract static class Builder { /** @@ -160,20 +162,6 @@ public abstract static class Builder { */ abstract Builder type(@NonNull String type); - /** - * A Geocoding Response might have a member named {@code bbox} to include information on the - * coordinate range for it's {@link CarmenFeature}s. The value of the bbox member MUST be a list - * of size 2*n where n is the number of dimensions represented in the contained feature - * geometries, with all axes of the most southwesterly point followed by all axes of the more - * northeasterly point. The axes order of a bbox follows the axes order of geometries. - * - * @param bbox a list of double coordinate values describing a bounding box - * @return this builder for chaining options together - * @since 3.0.0 - */ - @Nullable - public abstract Builder bbox(@Nullable BoundingBox bbox); - /** * A list of space and punctuation-separated strings from the original query. * @@ -181,7 +169,7 @@ public abstract static class Builder { * @return this builder for chaining options together * @since 3.0.0 */ - public abstract Builder query(@Nullable List query); + public abstract Builder query(@NonNull List query); /** * A list of the CarmenFeatures which contain the results and are ordered from most relevant to @@ -192,7 +180,7 @@ public abstract static class Builder { * @return this builder for chaining options together * @since 3.0.0 */ - public abstract Builder features(@Nullable List features); + public abstract Builder features(@NonNull List features); /** * A string attributing the results of the Mapbox Geocoding API to Mapbox and links to Mapbox's @@ -202,7 +190,7 @@ public abstract static class Builder { * @return this builder for chaining options together * @since 1.0.0 */ - public abstract Builder attribution(@Nullable String attribution); + public abstract Builder attribution(@NonNull String attribution); /** * Build a new {@link GeocodingResponse} object. diff --git a/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/GeocodingTestUtils.java b/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/GeocodingTestUtils.java new file mode 100644 index 000000000..2653b915e --- /dev/null +++ b/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/GeocodingTestUtils.java @@ -0,0 +1,63 @@ +package com.mapbox.api.geocoding.v5; + +import com.mapbox.core.TestUtils; +import okhttp3.HttpUrl; +import okhttp3.mockwebserver.Dispatcher; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; +import org.hamcrest.junit.ExpectedException; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; + +import java.io.IOException; + +public class GeocodingTestUtils extends TestUtils { + + protected static final String FORWARD_VALID = "forward_valid.json"; + private static final String FORWARD_GEOCODING = "geocoding.json"; + private static final String FORWARD_INVALID = "forward_invalid.json"; + private static final String FORWARD_VALID_ZH = "forward_valid_zh.json"; + private static final String FORWARD_BATCH_GEOCODING = "geocoding_batch.json"; + + private MockWebServer server; + protected HttpUrl mockUrl; + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() throws Exception { + server = new MockWebServer(); + server.setDispatcher(new Dispatcher() { + @Override + public MockResponse dispatch(RecordedRequest request) throws InterruptedException { + try { + String response; + if (request.getPath().contains(GeocodingCriteria.MODE_PLACES_PERMANENT)) { + response = loadJsonFixture(FORWARD_BATCH_GEOCODING); + } else if (request.getPath().contains("1600") && !request.getPath().contains("nw")) { + response = loadJsonFixture(FORWARD_VALID); + } else if (request.getPath().contains("nw")) { + response = loadJsonFixture(FORWARD_GEOCODING); + } else if (request.getPath().contains("sandy")) { + response = loadJsonFixture(FORWARD_INVALID); + } else { + response = loadJsonFixture(FORWARD_VALID_ZH); + } + return new MockResponse().setBody(response); + } catch (IOException ioException) { + throw new RuntimeException(ioException); + } + } + }); + server.start(); + mockUrl = server.url(""); + } + + @After + public void tearDown() throws IOException { + server.shutdown(); + } +} diff --git a/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/MapboxGeocodingTest.java b/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/MapboxGeocodingTest.java index a8655d620..266807ca3 100644 --- a/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/MapboxGeocodingTest.java +++ b/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/MapboxGeocodingTest.java @@ -26,44 +26,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -public class MapboxGeocodingTest extends TestUtils { - - private static final String GEOCODING_FIXTURE = "geocoding.json"; - private static final String GEOCODING_BATCH_FIXTURE = "geocoding_batch.json"; - - private MockWebServer server; - private HttpUrl mockUrl; - - @Before - public void setUp() throws Exception { - server = new MockWebServer(); - server.setDispatcher(new okhttp3.mockwebserver.Dispatcher() { - @Override - public MockResponse dispatch(RecordedRequest request) throws InterruptedException { - try { - String response; - if (request.getPath().contains(GeocodingCriteria.MODE_PLACES_PERMANENT)) { - response = loadJsonFixture(GEOCODING_BATCH_FIXTURE); - } else { - response = loadJsonFixture(GEOCODING_FIXTURE); - } - return new MockResponse().setBody(response); - } catch (IOException ioException) { - throw new RuntimeException(ioException); - } - } - }); - server.start(); - mockUrl = server.url(""); - } - - @After - public void tearDown() throws IOException { - server.shutdown(); - } - - @Rule - public ExpectedException thrown = ExpectedException.none(); +public class MapboxGeocodingTest extends GeocodingTestUtils { @Test public void sanity() throws Exception { diff --git a/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/models/CarmenContextTest.java b/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/models/CarmenContextTest.java index ced40f67f..bec83daf8 100644 --- a/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/models/CarmenContextTest.java +++ b/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/models/CarmenContextTest.java @@ -1,74 +1,21 @@ package com.mapbox.api.geocoding.v5.models; -import com.mapbox.api.geocoding.v5.GeocodingCriteria; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import com.mapbox.api.geocoding.v5.GeocodingTestUtils; import com.mapbox.api.geocoding.v5.MapboxGeocoding; import com.mapbox.core.TestUtils; - -import org.hamcrest.junit.ExpectedException; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; - -import java.io.IOException; - import okhttp3.HttpUrl; -import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; -import okhttp3.mockwebserver.RecordedRequest; +import org.hamcrest.Matchers; +import org.junit.Test; import retrofit2.Response; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -public class CarmenContextTest extends TestUtils { - - private static final String GEOCODING_FIXTURE = "geocoding.json"; - private static final String GEOCODING_BATCH_FIXTURE = "geocoding_batch.json"; - private static final String REVERSE_GEOCODE_FIXTURE = "geocoding_reverse.json"; - private static final String GEOCODE_WITH_BBOX_FIXTURE = "bbox_geocoding_result.json"; - private static final String GEOCODE_LANGUAGE_FIXTURE = "language_geocoding_result.json"; - - private MockWebServer server; - private HttpUrl mockUrl; - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Before - public void setUp() throws Exception { - server = new MockWebServer(); - server.setDispatcher(new okhttp3.mockwebserver.Dispatcher() { - @Override - public MockResponse dispatch(RecordedRequest request) throws InterruptedException { - try { - String response; - if (request.getPath().contains(GeocodingCriteria.MODE_PLACES_PERMANENT)) { - response = loadJsonFixture(GEOCODING_BATCH_FIXTURE); - } else if (request.getPath().contains("-77.0366,38.8971")) { - response = loadJsonFixture(REVERSE_GEOCODE_FIXTURE); - } else if (request.getPath().contains("texas")) { - response = loadJsonFixture(GEOCODE_WITH_BBOX_FIXTURE); - } else if (request.getPath().contains("language")) { - response = loadJsonFixture(GEOCODE_LANGUAGE_FIXTURE); - } else { - response = loadJsonFixture(GEOCODING_FIXTURE); - } - return new MockResponse().setBody(response); - } catch (IOException ioException) { - throw new RuntimeException(ioException); - } - } - }); - server.start(); - mockUrl = server.url(""); - } - - @After - public void tearDown() throws IOException { - server.shutdown(); - } +public class CarmenContextTest extends GeocodingTestUtils { @Test public void sanity() throws Exception { @@ -83,9 +30,9 @@ public void id_returnsCorrectString() throws Exception { .query("1600 pennsylvania ave nw") .baseUrl(mockUrl.toString()) .build(); - Response response = mapboxGeocoding.executeCall(); - assertTrue(response.body().features().get(0).context().get(0).id() - .equals("neighborhood.291451")); + GeocodingResponse response = mapboxGeocoding.executeCall().body(); + assert response != null; + assertThat(response.features().get(0).context().get(0).id(), equalTo("neighborhood.291451")); } @Test @@ -95,21 +42,21 @@ public void text_returnsCorrectString() throws Exception { .query("1600 pennsylvania ave nw") .baseUrl(mockUrl.toString()) .build(); - Response response = mapboxGeocoding.executeCall(); - assertTrue(response.body().features().get(0).context().get(0).text() - .equals("Downtown")); + GeocodingResponse response = mapboxGeocoding.executeCall().body(); + assert response != null; + assertThat(response.features().get(0).context().get(0).text(), equalTo("Downtown")); } @Test public void shortCode_returnsCorrectString() throws Exception { MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() .accessToken(ACCESS_TOKEN) - .query("texas") + .query("1600 pennsylvania ave nw") .baseUrl(mockUrl.toString()) .build(); - Response response = mapboxGeocoding.executeCall(); - assertTrue(response.body().features().get(0).context().get(0).shortCode() - .equals("us")); + GeocodingResponse response = mapboxGeocoding.executeCall().body(); + assert response != null; + assertThat(response.features().get(0).context().get(3).shortCode(), equalTo("US-DC")); } @Test @@ -119,40 +66,11 @@ public void wikidata_returnsCorrectString() throws Exception { .query("texas") .baseUrl(mockUrl.toString()) .build(); - Response response = mapboxGeocoding.executeCall(); - System.out.println(response.body().features().get(0).context().get(0).wikidata()); - assertTrue(response.body().features().get(0).context().get(0).wikidata() - .equals("Q30")); + GeocodingResponse response = mapboxGeocoding.executeCall().body(); + assert response != null; + assertThat(response.features().get(0).context().get(2).wikidata(), equalTo("Q148")); } -// @Test -// public void category_returnsCorrectString() throws Exception { - // TODO find a fixture with category -// MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() -// .accessToken(ACCESS_TOKEN) -// .query("1600 pennsylvania ave nw") -// .baseUrl(mockUrl.toString()) -// .build(); -// Response response = mapboxGeocoding.executeCall(); -// System.out.println(response.body().features().get(0).context().get(0).category()); -// assertTrue(response.body().features().get(0).context().get(0).category() -// .equals("Q30")); -// } - -// @Test -// public void maki_returnsCorrectString() throws Exception { -// // TODO find a fixture with category -// MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() -// .accessToken(ACCESS_TOKEN) -// .query("texas") -// .baseUrl(mockUrl.toString()) -// .build(); -// Response response = mapboxGeocoding.executeCall(); -// System.out.println(response.body().features().get(0).context().get(0).maki()); -// assertTrue(response.body().features().get(0).context().get(0).maki() -// .equals("Q30")); -// } - @Test public void testSerializable() throws Exception { CarmenContext carmenContext = CarmenContext.builder() diff --git a/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/models/CarmenFeatureTest.java b/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/models/CarmenFeatureTest.java index 37b4c8e4f..575b4ca58 100644 --- a/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/models/CarmenFeatureTest.java +++ b/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/models/CarmenFeatureTest.java @@ -1,103 +1,26 @@ package com.mapbox.api.geocoding.v5.models; +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertTrue; +import static org.hamcrest.Matchers.notNullValue; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.junit.MatcherAssert.assertThat; + import com.google.gson.JsonObject; -import com.mapbox.api.geocoding.v5.GeocodingCriteria; +import com.mapbox.api.geocoding.v5.GeocodingTestUtils; import com.mapbox.api.geocoding.v5.MapboxGeocoding; import com.mapbox.core.TestUtils; -import com.mapbox.geojson.BoundingBox; import com.mapbox.geojson.Point; - -import org.hamcrest.junit.ExpectedException; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; import org.junit.Test; +import retrofit2.Response; import java.io.IOException; import java.util.Locale; -import okhttp3.HttpUrl; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.MockWebServer; -import okhttp3.mockwebserver.RecordedRequest; -import retrofit2.Response; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -public class CarmenFeatureTest extends TestUtils { - - private static final String GEOCODING_FIXTURE = "geocoding.json"; - private static final String GEOCODING_BATCH_FIXTURE = "geocoding_batch.json"; - private static final String REVERSE_GEOCODE_FIXTURE = "geocoding_reverse.json"; - private static final String GEOCODE_WITH_BBOX_FIXTURE = "bbox_geocoding_result.json"; - private static final String GEOCODE_LANGUAGE_FIXTURE = "language_geocoding_result.json"; - - private MockWebServer server; - private HttpUrl mockUrl; - private CarmenFeature carmenFeature; - private BoundingBox bbox; - private Point geometry; - - @Before - public void setUp() throws Exception { - server = new MockWebServer(); - server.setDispatcher(new okhttp3.mockwebserver.Dispatcher() { - @Override - public MockResponse dispatch(RecordedRequest request) throws InterruptedException { - try { - String response; - if (request.getPath().contains(GeocodingCriteria.MODE_PLACES_PERMANENT)) { - response = loadJsonFixture(GEOCODING_BATCH_FIXTURE); - } else if (request.getPath().contains("-77.0366,38.8971")) { - response = loadJsonFixture(REVERSE_GEOCODE_FIXTURE); - } else if (request.getPath().contains("texas")) { - response = loadJsonFixture(GEOCODE_WITH_BBOX_FIXTURE); - } else if (request.getPath().contains("language")) { - response = loadJsonFixture(GEOCODE_LANGUAGE_FIXTURE); - } else { - response = loadJsonFixture(GEOCODING_FIXTURE); - } - return new MockResponse().setBody(response); - } catch (IOException ioException) { - throw new RuntimeException(ioException); - } - } - }); - server.start(); - mockUrl = server.url(""); - - JsonObject properties = new JsonObject(); - properties.addProperty("key", "value"); - geometry = Point.fromLngLat(2.0, 2.0); - bbox = BoundingBox.fromCoordinates(1.0, 2.0, 3.0, 4.0); - - carmenFeature = CarmenFeature.builder() - .address("1000") - .bbox(bbox) - .context(null) - .geometry(geometry) - .id("poi.123456789") - .language("fr") - .matchingPlaceName("matchingPlaceName") - .matchingText("matchingText") - .placeName("placeName") - .placeType(null) - .properties(properties) - .relevance(0.5) - .text("text") - .build(); - } - - @After - public void tearDown() throws IOException { - server.shutdown(); - } +public class CarmenFeatureTest extends GeocodingTestUtils { - @Rule - public ExpectedException thrown = ExpectedException.none(); + private static final String FORWARD_FEATURE_VALID = "forward_feature_valid.json"; @Test public void sanity() throws Exception { @@ -105,54 +28,20 @@ public void sanity() throws Exception { .properties(new JsonObject()) .address("1234") .build(); - assertNotNull(carmenFeature); - assertTrue(carmenFeature.address().equals("1234")); - } - - @Test - public void center_returnsPointRepresentingCenter() throws Exception { - Point centerPoint = Point.fromLngLat(-77.036491, 38.897291); - MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() - .accessToken(ACCESS_TOKEN) - .query(Point.fromLngLat(-77.0366, 38.8971)) - .baseUrl(mockUrl.toString()) - .build(); - assertNotNull(mapboxGeocoding); - Response response = mapboxGeocoding.executeCall(); - assertTrue(response.body().features().get(0).center().equals(centerPoint)); - } - - @Test - public void type_returnsFeatureString() throws Exception { - MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() - .accessToken(ACCESS_TOKEN) - .query("1600 pennsylvania ave nw") - .baseUrl(mockUrl.toString()) - .build(); - Response response = mapboxGeocoding.executeCall(); - assertTrue(response.body().features().get(0).type().equals("Feature")); + assertThat(carmenFeature, notNullValue()); + assertThat(carmenFeature.address(), equalTo("1234")); } @Test public void bbox_returnsGeoJsonBoundingBoxObject() throws Exception { MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() .accessToken(ACCESS_TOKEN) - .query("texas") - .baseUrl(mockUrl.toString()) - .build(); - Response response = mapboxGeocoding.executeCall(); - assertNotNull(response.body().features().get(0).bbox()); - } - - @Test - public void id_doesReturnCorrectId() throws Exception { - MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() - .accessToken(ACCESS_TOKEN) - .query(Point.fromLngLat(-77.0366, 38.8971)) + .query(Point.fromLngLat(1.0, 2.0)) + .country(Locale.CHINESE) .baseUrl(mockUrl.toString()) .build(); Response response = mapboxGeocoding.executeCall(); - assertTrue(response.body().features().get(0).id().equals("poi.7298394581225630")); + assertThat(response.body().features().get(0).bbox(), notNullValue()); } @Test @@ -162,164 +51,100 @@ public void geometry_returnsPointGeometry() throws Exception { .query(Point.fromLngLat(-77.0366, 38.8971)) .baseUrl(mockUrl.toString()) .build(); - Response response = mapboxGeocoding.executeCall(); - assertNotNull(response.body().features().get(0).geometry()); - assertTrue(response.body().features().get(0).geometry() instanceof Point); - assertEquals(-77.036491, - ((Point) response.body().features().get(0).geometry()).longitude(), DELTA); - assertEquals(38.897291, - ((Point) response.body().features().get(0).geometry()).latitude(), DELTA); - } - @Test - public void properties_isJsonObject() throws Exception { - MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() - .accessToken(ACCESS_TOKEN) - .query(Point.fromLngLat(-77.0366, 38.8971)) - .baseUrl(mockUrl.toString()) - .build(); - Response response = mapboxGeocoding.executeCall(); - assertNotNull(response.body().features().get(0).properties()); - assertTrue(response.body().features().get(0).properties().get("address") - .getAsString().equals("1600 Pennsylvania Ave NW")); - assertTrue(response.body().features().get(0).properties().get("category") - .getAsString().equals("restaurant")); - assertTrue(response.body().features().get(0).properties().get("landmark") - .getAsBoolean()); - assertTrue(response.body().features().get(0).properties().get("maki") - .getAsString().equals("restaurant")); + GeocodingResponse response = mapboxGeocoding.executeCall().body(); + assert response != null; + CarmenFeature feature = response.features().get(0); + assertThat(feature.geometry(), notNullValue()); + assertTrue(feature.geometry() instanceof Point); + assertThat(((Point) feature.geometry()).longitude(), equalTo(106.820552)); + assertThat(((Point) feature.geometry()).latitude(), equalTo(39.458115)); } @Test - public void text_returnsCorrectString() throws Exception { - MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() - .accessToken(ACCESS_TOKEN) - .query("texas") - .baseUrl(mockUrl.toString()) - .build(); - Response response = mapboxGeocoding.executeCall(); - assertTrue(response.body().features().get(0).text().equals("Texas")); - } - - @Test - public void placeName_returnsCorrectString() throws Exception { - MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() - .accessToken(ACCESS_TOKEN) - .query(Point.fromLngLat(-77.0366, 38.8971)) - .baseUrl(mockUrl.toString()) - .build(); - Response response = mapboxGeocoding.executeCall(); - assertTrue(response.body().features().get(0).placeName().equals("Harry S. Truman Bowling Alley," - + " 1600 Pennsylvania Ave NW, Washington, District of Columbia 20006, United States")); - } - - @Test - public void placeType_returnsCorrectString() throws Exception { + public void properties_isJsonObject() throws Exception { MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() .accessToken(ACCESS_TOKEN) .query(Point.fromLngLat(-77.0366, 38.8971)) .baseUrl(mockUrl.toString()) .build(); Response response = mapboxGeocoding.executeCall(); - assertEquals(1, response.body().features().get(0).placeType().size()); - assertTrue(response.body().features().get(0).placeType().get(0).equals("poi")); - } - - @Test - public void address_returnsCorrectString() throws Exception { - MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() - .accessToken(ACCESS_TOKEN) - .query("1600 pennsylvania ave nw") - .baseUrl(mockUrl.toString()) - .build(); - Response response = mapboxGeocoding.executeCall(); - assertTrue(response.body().features().get(0).address().equals("1600")); - } - - @Test - public void context_returnsListOfContext() throws Exception { - MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() - .accessToken(ACCESS_TOKEN) - .query("1600 pennsylvania ave nw") - .baseUrl(mockUrl.toString()) - .build(); - Response response = mapboxGeocoding.executeCall(); - assertEquals(5, response.body().features().get(0).context().size()); + CarmenFeature feature = response.body().features().get(0); + assertThat(feature.properties(), notNullValue()); } @Test - public void relevance_returnsAccurateValue() throws Exception { - MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() - .accessToken(ACCESS_TOKEN) - .query("1600 pennsylvania ave nw") - .baseUrl(mockUrl.toString()) - .build(); - Response response = mapboxGeocoding.executeCall(); - assertEquals(1.0, response.body().features().get(0).relevance(), DELTA); - } + public void fromJson_handlesConversionCorrectly() throws Exception { + String json = loadJsonFixture(FORWARD_FEATURE_VALID); + CarmenFeature feature = CarmenFeature.fromJson(json); - @Test - public void matchingText_returnsCorrectString() throws Exception { - // TODO complete test with fixture - MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() - .accessToken(ACCESS_TOKEN) - .query("1600 pennsylvania ave nw") - .baseUrl(mockUrl.toString()) - .build(); - Response response = mapboxGeocoding.executeCall(); - System.out.println(response.body().features().get(0).matchingText()); + assertThat(feature.type(), equalTo("Feature")); + assertEquals(5, feature.context().size()); + assertThat(feature.geometry().type(), equalTo("Point")); + assertThat(feature.geometry().coordinates().toString(), equalTo("[-77.036543, " + + "38.897702]")); + assertThat(feature.address(), equalTo("1600")); + assertThat(feature.id(), equalTo("address.3982178573139850")); + assertEquals(1, feature.placeType().size()); + assertThat(feature.placeType().get(0), equalTo("address")); + assertThat(feature.relevance(), equalTo(1.0)); + assertThat(feature.placeName(), equalTo("1600 Pennsylvania Ave NW, Washington," + + " District of Columbia 20006, United States")); + assertThat(feature.text(), equalTo("Pennsylvania Ave NW")); + assertThat(feature.center().latitude(), equalTo(38.897702)); + assertThat(feature.center().longitude(), equalTo(-77.036543)); + assertThat(feature.language(), nullValue()); } @Test - public void matchingPlaceName_returnsCorrectString() throws Exception { - // TODO complete test with fixture + public void toJson_handlesConversionCorrectly() throws IOException { + String json = loadJsonFixture(FORWARD_FEATURE_VALID); MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() .accessToken(ACCESS_TOKEN) .query("1600 pennsylvania ave nw") .baseUrl(mockUrl.toString()) .build(); - Response response = mapboxGeocoding.executeCall(); - System.out.println(response.body().features().get(0).matchingPlaceName()); + GeocodingResponse response = mapboxGeocoding.executeCall().body(); + assert response != null; + CarmenFeature feature = response.features().get(0); + compareJson(json, feature.toJson()); } @Test - public void language_returnCorrectString() throws Exception { - // TODO complete test for language + public void ForwardGeocode_withValidChineseResponse() throws Exception { MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() .accessToken(ACCESS_TOKEN) - .query("foobar") + .query(Point.fromLngLat(1.0, 2.0)) + .country(Locale.CHINESE) .baseUrl(mockUrl.toString()) - .languages(Locale.FRANCE) .build(); - System.out.println(mapboxGeocoding.cloneCall().request().url()); Response response = mapboxGeocoding.executeCall(); - System.out.println(response.body().features().get(0).language()); - } - - @Test - public void toJson_doesConvertToJsonStringCorrectly() throws Exception { - String json = carmenFeature.toJson(); - CarmenFeature carmenFeature = CarmenFeature.fromJson(json); - assertNotNull(carmenFeature); - assertNotNull(carmenFeature.placeName()); - assertNotNull(carmenFeature.matchingText()); - assertNotNull(carmenFeature.matchingPlaceName()); - } - - @Test - public void carmenFeatureBuilder_sanity() throws Exception { - assertTrue(carmenFeature.address().equals("1000")); - assertTrue(carmenFeature.bbox().equals(bbox)); - assertNull(carmenFeature.context()); - assertTrue(carmenFeature.geometry().equals(geometry)); - assertTrue(carmenFeature.id().equals("poi.123456789")); - assertTrue(carmenFeature.language().equals("fr")); - assertTrue(carmenFeature.matchingPlaceName().equals("matchingPlaceName")); - assertTrue(carmenFeature.matchingText().equals("matchingText")); - assertTrue(carmenFeature.placeName().equals("placeName")); - assertNull(carmenFeature.placeType()); - assertTrue(carmenFeature.properties().get("key").getAsString().equals("value")); - assertEquals(0.5, carmenFeature.relevance(), DELTA); - assertTrue(carmenFeature.text().equals("text")); + assertEquals(200, response.code()); + GeocodingResponse object = response.body(); + assert object != null; + + CarmenFeature feature = object.features().get(0); + + assertEquals(1, object.query().size()); + assertThat(object.query().get(0), equalTo("hainan")); + assertThat(feature.type(), equalTo("Feature")); + + + assertEquals(3, feature.context().size()); + assertThat(feature.geometry().type(), equalTo("Point")); + assertThat(feature.geometry().coordinates().toString(), equalTo("[106.820552, " + + "39.458115]")); + assertThat(feature.id(), equalTo("place.10514057239276310")); + assertThat(feature.relevance(), equalTo(0.99)); + assertThat(feature.placeName(), equalTo("中国内蒙古乌海市海南区")); + assertThat(feature.text(), equalTo("海南区")); + assertThat(feature.center().latitude(), equalTo(39.458115)); + assertThat(feature.center().longitude(), equalTo(106.820552)); + assertThat(feature.language(), nullValue()); + assertTrue(feature.properties().get("wikidata").isJsonNull()); + assertThat(feature.bbox().west(), equalTo(106.733581544)); + assertThat(feature.bbox().south(), equalTo(39.308357239)); + assertThat(feature.bbox().east(), equalTo(107.025123596)); + assertThat(feature.bbox().north(), equalTo(39.6012458800001)); } } diff --git a/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/models/GeocodingResponseTest.java b/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/models/GeocodingResponseTest.java index 100f42a31..cb6887f0d 100644 --- a/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/models/GeocodingResponseTest.java +++ b/services-geocoding/src/test/java/com/mapbox/api/geocoding/v5/models/GeocodingResponseTest.java @@ -1,90 +1,101 @@ package com.mapbox.api.geocoding.v5.models; -import com.mapbox.api.geocoding.v5.GeocodingCriteria; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +import com.mapbox.api.geocoding.v5.GeocodingTestUtils; import com.mapbox.api.geocoding.v5.MapboxGeocoding; import com.mapbox.core.TestUtils; -import com.mapbox.geojson.Point; - -import org.hamcrest.junit.ExpectedException; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; import org.junit.Test; +import retrofit2.Response; import java.io.IOException; +import java.util.ArrayList; -import okhttp3.HttpUrl; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.MockWebServer; -import okhttp3.mockwebserver.RecordedRequest; -import retrofit2.Response; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +public class GeocodingResponseTest extends GeocodingTestUtils { -public class GeocodingResponseTest extends TestUtils { + @Test + public void sanity() throws Exception { + MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() + .accessToken(ACCESS_TOKEN) + .query("1600 pennsylvania ave") + .baseUrl(mockUrl.toString()) + .build(); + Response response = mapboxGeocoding.executeCall(); + assertEquals(200, response.code()); + } - private static final String GEOCODING_FIXTURE = "geocoding.json"; - private static final String GEOCODING_BATCH_FIXTURE = "geocoding_batch.json"; - private static final String REVERSE_GEOCODE_FIXTURE = "geocoding_reverse.json"; - private static final String GEOCODE_WITH_BBOX_FIXTURE = "bbox_geocoding_result.json"; + @Test + public void builder_doesSuccessfullyBuildGeocodingResponse() throws Exception { + GeocodingResponse response = GeocodingResponse.builder() + .attribution("attribution") + .features(new ArrayList()) + .query(new ArrayList()) + .build(); + assertThat(response, notNullValue()); + assertThat(response.attribution(), equalTo("attribution")); + assertThat(response.type(), equalTo("FeatureCollection")); + assertEquals(0, response.features().size()); + assertEquals(0, response.query().size()); + } - private MockWebServer server; - private HttpUrl mockUrl; + @Test + public void fromJson_handlesConversionCorrectly() throws Exception { + String json = loadJsonFixture(FORWARD_VALID); + GeocodingResponse response = GeocodingResponse.fromJson(json); - @Before - public void setUp() throws Exception { - server = new MockWebServer(); - server.setDispatcher(new okhttp3.mockwebserver.Dispatcher() { - @Override - public MockResponse dispatch(RecordedRequest request) throws InterruptedException { - try { - String response; - if (request.getPath().contains(GeocodingCriteria.MODE_PLACES_PERMANENT)) { - response = loadJsonFixture(GEOCODING_BATCH_FIXTURE); - } else if (request.getPath().contains("")) { - response = loadJsonFixture(REVERSE_GEOCODE_FIXTURE); - } else if (request.getPath().contains("texas")) { - response = loadJsonFixture(GEOCODE_WITH_BBOX_FIXTURE); - } else { - response = loadJsonFixture(GEOCODING_FIXTURE); - } - return new MockResponse().setBody(response); - } catch (IOException ioException) { - throw new RuntimeException(ioException); - } - } - }); - server.start(); - mockUrl = server.url(""); - } + assertThat(response.attribution(), equalTo("NOTICE: © 2016 Mapbox and its " + + "suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service" + + " (https://www.mapbox.com/about/maps/). This response and the information it contains may " + + "not be retained.")); + assertThat(response.type(), equalTo("FeatureCollection")); + assertThat(response.query().get(0), equalTo("1600")); + assertThat(response.query().get(1), equalTo("pennsylvania")); + assertThat(response.query().get(2), equalTo("ave")); + assertEquals(3, response.query().size()); - @After - public void tearDown() throws IOException { - server.shutdown(); + // response carmen features are checked in separate test class. + assertEquals(4, response.features().size()); } - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test - public void sanity() throws Exception { - GeocodingResponse response = GeocodingResponse.builder() - .attribution("") + public void toJson_handlesConversionCorrectly() throws IOException { + String json = loadJsonFixture(FORWARD_VALID); + MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() + .accessToken(ACCESS_TOKEN) + .query("1600 pennsylvania ave") + .baseUrl(mockUrl.toString()) .build(); - assertNotNull(response); + Response response = mapboxGeocoding.executeCall(); + assertEquals(200, response.code()); + compareJson(json, response.body().toJson()); } @Test - public void reverseGeocoding_responseSuccessfully() throws Exception { + public void forwardRequest_invalidQuery() throws Exception { MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder() .accessToken(ACCESS_TOKEN) - .query(Point.fromLngLat(-77.0366, 38.8971)) + .query("sandy") .baseUrl(mockUrl.toString()) .build(); - assertNotNull(mapboxGeocoding); Response response = mapboxGeocoding.executeCall(); assertEquals(200, response.code()); - assertNotNull(response.body()); + GeocodingResponse object = response.body(); + assert object != null; + + assertEquals(4, object.query().size()); + assertThat(object.query().get(0), equalTo("sandy")); + assertThat(object.query().get(1), equalTo("island")); + assertThat(object.query().get(2), equalTo("new")); + assertThat(object.query().get(3), equalTo("caledonia")); + assertThat(object.attribution(), equalTo("NOTICE: © 2016 Mapbox and its " + + "suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service" + + " (https://www.mapbox.com/about/maps/). This response and the information it contains may " + + "not be retained.")); + assertThat(object.type(), equalTo("FeatureCollection")); + assertThat(object.features(), notNullValue()); + assertEquals(0, object.features().size()); } -} +} \ No newline at end of file diff --git a/services-geocoding/src/test/resources/forward_feature_valid.json b/services-geocoding/src/test/resources/forward_feature_valid.json new file mode 100644 index 000000000..94b37456e --- /dev/null +++ b/services-geocoding/src/test/resources/forward_feature_valid.json @@ -0,0 +1 @@ +{"id":"address.3982178573139850","type":"Feature","place_type":["address"],"relevance":1,"properties":{},"text":"Pennsylvania Ave NW","place_name":"1600 Pennsylvania Ave NW, Washington, District of Columbia 20006, United States","center":[-77.036543,38.897702],"geometry":{"type":"Point","coordinates":[-77.036543,38.897702]},"address":"1600","context":[{"id":"neighborhood.291451","text":"Downtown"},{"id":"postcode.8031694603652840","text":"20006"},{"id":"place.11387590027246050","wikidata":"Q61","text":"Washington"},{"id":"region.3403","short_code":"US-DC","wikidata":"Q61","text":"District of Columbia"},{"id":"country.3145","short_code":"us","wikidata":"Q30","text":"United States"}]} diff --git a/services-geocoding/src/test/resources/forward_invalid.json b/services-geocoding/src/test/resources/forward_invalid.json new file mode 100644 index 000000000..ce407dd65 --- /dev/null +++ b/services-geocoding/src/test/resources/forward_invalid.json @@ -0,0 +1 @@ +{"type":"FeatureCollection","query":["sandy","island","new","caledonia"],"features":[],"attribution":"NOTICE: © 2016 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained."} diff --git a/services-geocoding/src/test/resources/forward_valid.json b/services-geocoding/src/test/resources/forward_valid.json new file mode 100644 index 000000000..53f7556b7 --- /dev/null +++ b/services-geocoding/src/test/resources/forward_valid.json @@ -0,0 +1 @@ +{"type":"FeatureCollection","query":["1600","pennsylvania","ave"],"features":[{"id":"address.10133733172573100","type":"Feature","text":"Pennsylvania Ave","place_name":"Pennsylvania Ave, Wasaga Beach, Ontario L9Z 3A8, Canada","relevance":0.39,"properties":{},"center":[-79.9850737,44.5047077],"geometry":{"type":"Point","coordinates":[-79.9850737,44.5047077]},"context":[{"id":"place.655624","text":"Wasaga Beach","wikidata":"Q3047234"},{"id":"postcode.17609609551414490","text":"L9Z 3A8"},{"id":"region.13373639426376420","text":"Ontario","short_code":"CA-ON","wikidata":"Q1904"},{"id":"country.15589894856372040","text":"Canada","short_code":"ca","wikidata":"Q16"}]},{"id":"address.6485281316573100","type":"Feature","text":"Pennsylvania Ave","place_name":"Pennsylvania Ave, Vaughan, Ontario L4K 3X6, Canada","relevance":0.39,"properties":{},"center":[-79.532497,43.802232],"geometry":{"type":"Point","coordinates":[-79.532497,43.802232]},"context":[{"id":"neighborhood.5043724752221980","text":"Vellore Woods"},{"id":"place.646758","text":"Vaughan","wikidata":"Q44013"},{"id":"postcode.6385804433309570","text":"L4K 3X6"},{"id":"region.13373639426376420","text":"Ontario","short_code":"CA-ON","wikidata":"Q1904"},{"id":"country.15589894856372040","text":"Canada","short_code":"ca","wikidata":"Q16"}]},{"id":"address.12769812476573100","type":"Feature","text":"Pennsylvania Ave","place_name":"Pennsylvania Ave, Stellarton, Nova Scotia B0K 1S0, Canada","relevance":0.39,"properties":{},"center":[-62.661376,45.556068],"geometry":{"type":"Point","coordinates":[-62.661376,45.556068]},"context":[{"id":"place.593196","text":"Stellarton","wikidata":"Q3498168"},{"id":"postcode.13310912212063190","text":"B0K 1S0"},{"id":"region.10539239752558240","text":"Nova Scotia","short_code":"CA-NS","wikidata":"Q1952"},{"id":"country.15589894856372040","text":"Canada","short_code":"ca","wikidata":"Q16"}]},{"id":"address.6456604709573100","type":"Feature","text":"Pennsylvania Ave","place_name":"Pennsylvania Ave, Vaughan, Ontario L4K 3X8, Canada","relevance":0.39,"properties":{},"center":[-79.531684,43.802706],"geometry":{"type":"Point","coordinates":[-79.531684,43.802706]},"context":[{"id":"neighborhood.5043724752221980","text":"Vellore Woods"},{"id":"place.646758","text":"Vaughan","wikidata":"Q44013"},{"id":"postcode.6367605302663760","text":"L4K 3X8"},{"id":"region.13373639426376420","text":"Ontario","short_code":"CA-ON","wikidata":"Q1904"},{"id":"country.15589894856372040","text":"Canada","short_code":"ca","wikidata":"Q16"}]}],"attribution":"NOTICE: © 2016 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained."} \ No newline at end of file diff --git a/services-geocoding/src/test/resources/forward_valid_zh.json b/services-geocoding/src/test/resources/forward_valid_zh.json new file mode 100644 index 000000000..721f3be4c --- /dev/null +++ b/services-geocoding/src/test/resources/forward_valid_zh.json @@ -0,0 +1 @@ +{"type":"FeatureCollection","query":["hainan"],"features":[{"id":"place.10514057239276310","type":"Feature","text":"海南区","place_name":"中国内蒙古乌海市海南区","relevance":0.99,"properties":{"wikidata":null},"bbox":[106.733581544,39.308357239,107.025123596,39.6012458800001],"center":[106.820552,39.458115],"geometry":{"type":"Point","coordinates":[106.820552,39.458115]},"context":[{"id":"district.119561774838770","text":"乌海市","language":"zh"},{"id":"region.12259570057963950","text":"内蒙古","language":"zh","short_code":"CN-15","wikidata":"Q41079"},{"id":"country.6702069377157440","text":"中国","language":"zh","short_code":"cn","wikidata":"Q148"}]},{"id":"region.5200090080520630","type":"Feature","text":"海南省","place_name":"中国海南省","relevance":0.99,"properties":{"short_code":"CN-46","wikidata":"Q42200"},"bbox":[108.515578237005,18.0698119320092,111.134676114995,20.2631455569989],"center":[109.5771,19.164822],"geometry":{"type":"Point","coordinates":[109.5771,19.164822]},"context":[{"id":"country.6702069377157440","text":"中国","language":"zh","short_code":"cn","wikidata":"Q148"}]},{"id":"district.12821841197016360","type":"Feature","text":"海南藏族自治州","place_name":"中国青海省海南藏族自治州","relevance":0.99,"properties":{},"bbox":[98.930977,34.6406900000001,101.797241,37.141197],"center":[100.6214,36.2742],"geometry":{"type":"Point","coordinates":[100.6214,36.2742]},"context":[{"id":"region.11017319148773520","text":"青海省","language":"zh","short_code":"CN-63","wikidata":"Q45833"},{"id":"country.6702069377157440","text":"中国","language":"zh","short_code":"cn","wikidata":"Q148"}]}],"attribution":"NOTICE: © 2016 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained."} diff --git a/services-geocoding/src/test/resources/geocoding.json b/services-geocoding/src/test/resources/geocoding.json index fbe732a8c..2c3d384e5 100644 --- a/services-geocoding/src/test/resources/geocoding.json +++ b/services-geocoding/src/test/resources/geocoding.json @@ -1 +1 @@ -{"type":"FeatureCollection","query":["1600","pennsylvania","ave","nw"],"features":[{"id":"address.3982178573139850","type":"Feature","place_type":["address"],"relevance":1,"properties":{},"text":"Pennsylvania Ave NW","place_name":"1600 Pennsylvania Ave NW, Washington, District of Columbia 20006, United States","center":[-77.036543,38.897702],"geometry":{"type":"Point","coordinates":[-77.036543,38.897702]},"address":"1600","context":[{"id":"neighborhood.291451","text":"Downtown"},{"id":"postcode.8031694603652840","text":"20006"},{"id":"place.11387590027246050","wikidata":"Q61","text":"Washington"},{"id":"region.3403","short_code":"US-DC","wikidata":"Q61","text":"District of Columbia"},{"id":"country.3145","short_code":"us","wikidata":"Q30","text":"United States"}]},{"id":"address.10962143377573100","type":"Feature","place_type":["address"],"relevance":0.5,"properties":{},"text":"Pennsylvania Ave","place_name":"1600 Pennsylvania Ave, Baltimore, Maryland 21217, United States","center":[-76.634388,39.30307],"geometry":{"type":"Point","coordinates":[-76.634388,39.30307]},"address":"1600","context":[{"id":"neighborhood.2101296","text":"Upton"},{"id":"postcode.11848926911131250","text":"21217"},{"id":"place.5593629283339210","wikidata":"Q5092","text":"Baltimore"},{"id":"region.211891","short_code":"US-MD","wikidata":"Q1391","text":"Maryland"},{"id":"country.3145","short_code":"us","wikidata":"Q30","text":"United States"}]},{"id":"address.6056934002573100","type":"Feature","place_type":["address"],"relevance":0.5,"properties":{},"text":"Pennsylvania Ave","place_name":"1600 Pennsylvania Ave, West Sacramento, California 95691, United States","center":[-121.529598,38.568027],"geometry":{"type":"Point","coordinates":[-121.529598,38.568027]},"address":"1600","context":[{"id":"neighborhood.291835","text":"Old West Sacramento"},{"id":"postcode.11727280407938200","text":"95691"},{"id":"place.10001298613630220","wikidata":"Q983600","text":"West Sacramento"},{"id":"region.3591","short_code":"US-CA","wikidata":"Q99","text":"California"},{"id":"country.3145","short_code":"us","wikidata":"Q30","text":"United States"}]},{"id":"address.2948163863573100","type":"Feature","place_type":["address"],"relevance":0.5,"properties":{},"text":"Pennsylvania Ave","place_name":"1600 Pennsylvania Ave, Saint Louis, Missouri 63133, United States","center":[-90.313554,38.681546],"geometry":{"type":"Point","coordinates":[-90.313554,38.681546],"interpolated":true},"address":"1600","context":[{"id":"postcode.10872861601127320","text":"63133"},{"id":"place.9782398796128090","wikidata":"Q38022","text":"Saint Louis"},{"id":"region.28004","short_code":"US-MO","wikidata":"Q1581","text":"Missouri"},{"id":"country.3145","short_code":"us","wikidata":"Q30","text":"United States"}]},{"id":"address.11159252373890170","type":"Feature","place_type":["address"],"relevance":0.4,"properties":{},"text":"Pennsylvania Ave SE","place_name":"1600 Pennsylvania Ave SE, Washington, District of Columbia 20003, United States","center":[-76.981041,38.878649],"geometry":{"type":"Point","coordinates":[-76.981041,38.878649],"interpolated":true},"address":"1600","context":[{"id":"neighborhood.295468","text":"Barney Circle"},{"id":"postcode.7407455452898840","text":"20003"},{"id":"place.11387590027246050","wikidata":"Q61","text":"Washington"},{"id":"region.3403","short_code":"US-DC","wikidata":"Q61","text":"District of Columbia"},{"id":"country.3145","short_code":"us","wikidata":"Q30","text":"United States"}]}],"attribution":"NOTICE: © 2017 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained."} \ No newline at end of file +{"type":"FeatureCollection","query":["1600","pennsylvania","ave","nw"],"features":[{"id":"address.3982178573139850","type":"Feature","place_type":["address"],"relevance":1,"properties":{},"text":"Pennsylvania Ave NW","place_name":"1600 Pennsylvania Ave NW, Washington, District of Columbia 20006, United States","center":[-77.036543,38.897702],"geometry":{"type":"Point","coordinates":[-77.036543,38.897702]},"address":"1600","context":[{"id":"neighborhood.291451","text":"Downtown"},{"id":"postcode.8031694603652840","text":"20006"},{"id":"place.11387590027246050","wikidata":"Q61","text":"Washington"},{"id":"region.3403","short_code":"US-DC","wikidata":"Q61","text":"District of Columbia"},{"id":"country.3145","short_code":"us","wikidata":"Q30","text":"United States"}]},{"id":"address.10962143377573100","type":"Feature","place_type":["address"],"relevance":0.5,"properties":{},"text":"Pennsylvania Ave","place_name":"1600 Pennsylvania Ave, Baltimore, Maryland 21217, United States","center":[-76.634388,39.30307],"geometry":{"type":"Point","coordinates":[-76.634388,39.30307]},"address":"1600","context":[{"id":"neighborhood.2101296","text":"Upton"},{"id":"postcode.11848926911131250","text":"21217"},{"id":"place.5593629283339210","wikidata":"Q5092","text":"Baltimore"},{"id":"region.211891","short_code":"US-MD","wikidata":"Q1391","text":"Maryland"},{"id":"country.3145","short_code":"us","wikidata":"Q30","text":"United States"}]},{"id":"address.6056934002573100","type":"Feature","place_type":["address"],"relevance":0.5,"properties":{},"text":"Pennsylvania Ave","place_name":"1600 Pennsylvania Ave, West Sacramento, California 95691, United States","center":[-121.529598,38.568027],"geometry":{"type":"Point","coordinates":[-121.529598,38.568027]},"address":"1600","context":[{"id":"neighborhood.291835","text":"Old West Sacramento"},{"id":"postcode.11727280407938200","text":"95691"},{"id":"place.10001298613630220","wikidata":"Q983600","text":"West Sacramento"},{"id":"region.3591","short_code":"US-CA","wikidata":"Q99","text":"California"},{"id":"country.3145","short_code":"us","wikidata":"Q30","text":"United States"}]},{"id":"address.2948163863573100","type":"Feature","place_type":["address"],"relevance":0.5,"properties":{},"text":"Pennsylvania Ave","place_name":"1600 Pennsylvania Ave, Saint Louis, Missouri 63133, United States","center":[-90.313554,38.681546],"geometry":{"type":"Point","coordinates":[-90.313554,38.681546],"interpolated":true},"address":"1600","context":[{"id":"postcode.10872861601127320","text":"63133"},{"id":"place.9782398796128090","wikidata":"Q38022","text":"Saint Louis"},{"id":"region.28004","short_code":"US-MO","wikidata":"Q1581","text":"Missouri"},{"id":"country.3145","short_code":"us","wikidata":"Q30","text":"United States"}]},{"id":"address.11159252373890170","type":"Feature","place_type":["address"],"relevance":0.4,"properties":{},"text":"Pennsylvania Ave SE","place_name":"1600 Pennsylvania Ave SE, Washington, District of Columbia 20003, United States","center":[-76.981041,38.878649],"geometry":{"type":"Point","coordinates":[-76.981041,38.878649],"interpolated":true},"address":"1600","context":[{"id":"neighborhood.295468","text":"Barney Circle"},{"id":"postcode.7407455452898840","text":"20003"},{"id":"place.11387590027246050","wikidata":"Q61","text":"Washington"},{"id":"region.3403","short_code":"US-DC","wikidata":"Q61","text":"District of Columbia"},{"id":"country.3145","short_code":"us","wikidata":"Q30","text":"United States"}]}],"attribution":"NOTICE: © 2017 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained."} diff --git a/services-geocoding/src/test/resources/reverse_invalid.json b/services-geocoding/src/test/resources/reverse_invalid.json new file mode 100644 index 000000000..a5b50232c --- /dev/null +++ b/services-geocoding/src/test/resources/reverse_invalid.json @@ -0,0 +1 @@ +{"type":"FeatureCollection","query":[0,0],"features":[],"attribution":"NOTICE: © 2016 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained."} \ No newline at end of file diff --git a/services-geocoding/src/test/resources/reverse_valid.json b/services-geocoding/src/test/resources/reverse_valid.json new file mode 100644 index 000000000..6d4d1ec7d --- /dev/null +++ b/services-geocoding/src/test/resources/reverse_valid.json @@ -0,0 +1 @@ +{"type":"FeatureCollection","query":[-95.78558,37.13284],"features":[{"id":"poi.16590256005799510","type":"Feature","text":"Jones Jerry","place_name":"Jones Jerry, 2850 CR 3100, Independence, Kansas 67301, United States","relevance":1,"properties":{"tel":"(620) 289-4288","address":"2850 CR 3100","category":"legal, lawyer, law, law office","maki":"suitcase"},"center":[-95.782951,37.128003],"geometry":{"type":"Point","coordinates":[-95.782951,37.128003]},"context":[{"id":"place.296541","text":"Independence","wikidata":"Q1904322"},{"id":"postcode.18742859650495760","text":"67301"},{"id":"region.14040296094955790","text":"Kansas","short_code":"US-KS","wikidata":"Q1558"},{"id":"country.12862386939497690","text":"United States","wikidata":"Q30","short_code":"us"}]},{"id":"place.296541","type":"Feature","text":"Independence","place_name":"Independence, Kansas, United States","relevance":1,"properties":{"wikidata":"Q1904322"},"bbox":[-95.9279900056451,37.0332299928927,-95.5946289926707,37.3563280070598],"center":[-95.7083,37.2242],"geometry":{"type":"Point","coordinates":[-95.7083,37.2242]},"context":[{"id":"postcode.18742859650495760","text":"67301"},{"id":"region.14040296094955790","text":"Kansas","short_code":"US-KS","wikidata":"Q1558"},{"id":"country.12862386939497690","text":"United States","wikidata":"Q30","short_code":"us"}]},{"id":"postcode.18742859650495760","type":"Feature","text":"67301","place_name":"67301, Kansas, United States","relevance":1,"properties":{},"bbox":[-95.92799,37.03323,-95.594629,37.356328],"center":[-95.761294,37.189473],"geometry":{"type":"Point","coordinates":[-95.761294,37.189473]},"context":[{"id":"region.14040296094955790","text":"Kansas","short_code":"US-KS","wikidata":"Q1558"},{"id":"country.12862386939497690","text":"United States","wikidata":"Q30","short_code":"us"}]},{"id":"region.14040296094955790","type":"Feature","text":"Kansas","place_name":"Kansas, United States","relevance":1,"properties":{"short_code":"US-KS","wikidata":"Q1558"},"bbox":[-102.051744,36.993016,-94.588658,40.003078],"center":[-98.327818,38.642763],"geometry":{"type":"Point","coordinates":[-98.327818,38.642763]},"context":[{"id":"country.12862386939497690","text":"United States","wikidata":"Q30","short_code":"us"}]},{"id":"country.12862386939497690","type":"Feature","text":"United States","place_name":"United States","relevance":1,"properties":{"wikidata":"Q30","short_code":"us"},"bbox":[-179.330950579,18.765563302,179.959578044,71.540723637],"center":[-97.922211,39.381266],"geometry":{"type":"Point","coordinates":[-97.922211,39.381266]}}],"attribution":"NOTICE: © 2016 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained."} \ No newline at end of file diff --git a/services-geojson/src/main/java/com/mapbox/geojson/BoundingBox.java b/services-geojson/src/main/java/com/mapbox/geojson/BoundingBox.java index 3de0863ad..ab55b9e47 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/BoundingBox.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/BoundingBox.java @@ -5,11 +5,12 @@ import android.support.annotation.FloatRange; import android.support.annotation.NonNull; - import com.google.auto.value.AutoValue; -import com.google.gson.FieldNamingPolicy; +import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.TypeAdapter; import com.mapbox.geojson.constants.GeoJsonConstants; +import com.mapbox.geojson.gson.GeoJsonAdapterFactory; import java.io.Serializable; @@ -31,6 +32,11 @@ @AutoValue public abstract class BoundingBox implements Serializable { + public static BoundingBox fromJson(String json) { + Gson gson = new GsonBuilder().registerTypeAdapterFactory(GeoJsonAdapterFactory.create()).create(); + return gson.fromJson(json, BoundingBox.class); + } + /** * Define a new instance of this class by passing in two {@link Point}s, representing both the * southwest and northwest corners of the bounding box. @@ -158,6 +164,10 @@ public final double north() { return northeast().latitude(); } + public static TypeAdapter typeAdapter(Gson gson) { + return new AutoValue_BoundingBox.GsonTypeAdapter(gson); + } + /** * This takes the currently defined values found inside this instance and converts it to a GeoJson * string. @@ -166,9 +176,10 @@ public final double north() { * @since 3.0.0 */ public final String toJson() { - GsonBuilder gson = new GsonBuilder(); - gson.excludeFieldsWithoutExposeAnnotation(); - gson.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); - return gson.create().toJson(this); + Gson gson = new GsonBuilder() + .setPrettyPrinting() + .registerTypeAdapterFactory(GeoJsonAdapterFactory.create()) + .create(); + return gson.toJson(this, BoundingBox.class); } } diff --git a/services-geojson/src/main/java/com/mapbox/geojson/Feature.java b/services-geojson/src/main/java/com/mapbox/geojson/Feature.java index 9d6c830d2..401ca0f93 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/Feature.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/Feature.java @@ -2,21 +2,17 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; - import com.google.auto.value.AutoValue; -import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.TypeAdapter; -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; +import com.mapbox.geojson.gson.BoundingBoxDeserializer; +import com.mapbox.geojson.gson.BoundingBoxSerializer; import com.mapbox.geojson.gson.GeoJsonAdapterFactory; import com.mapbox.geojson.gson.GeometryDeserializer; import com.mapbox.geojson.gson.PointDeserializer; -import com.mapbox.geojson.gson.BoundingBoxDeserializer; -import com.mapbox.geojson.gson.BoundingBoxSerializer; import com.mapbox.geojson.gson.PointSerializer; /** @@ -52,8 +48,6 @@ @AutoValue public abstract class Feature implements GeoJson { - @Expose - @SerializedName("type") private static final String TYPE = "Feature"; /** @@ -84,7 +78,7 @@ public static Feature fromJson(@NonNull String json) { * @since 1.0.0 */ public static Feature fromGeometry(@Nullable Geometry geometry) { - return new AutoValue_Feature(null, null, geometry, new JsonObject()); + return new AutoValue_Feature(TYPE, null, null, geometry, new JsonObject()); } /** @@ -98,7 +92,7 @@ public static Feature fromGeometry(@Nullable Geometry geometry) { * @since 1.0.0 */ public static Feature fromGeometry(@Nullable Geometry geometry, @Nullable BoundingBox bbox) { - return new AutoValue_Feature(bbox, null, geometry, new JsonObject()); + return new AutoValue_Feature(TYPE, bbox, null, geometry, new JsonObject()); } /** @@ -112,7 +106,7 @@ public static Feature fromGeometry(@Nullable Geometry geometry, @Nullable Boundi * @since 1.0.0 */ public static Feature fromGeometry(@Nullable Geometry geometry, @NonNull JsonObject properties) { - return new AutoValue_Feature(null, null, geometry, properties); + return new AutoValue_Feature(TYPE, null, null, geometry, properties); } /** @@ -128,7 +122,7 @@ public static Feature fromGeometry(@Nullable Geometry geometry, @NonNull JsonObj */ public static Feature fromGeometry(@Nullable Geometry geometry, @NonNull JsonObject properties, @Nullable BoundingBox bbox) { - return new AutoValue_Feature(bbox, null, geometry, properties); + return new AutoValue_Feature(TYPE, bbox, null, geometry, properties); } /** @@ -143,7 +137,7 @@ public static Feature fromGeometry(@Nullable Geometry geometry, @NonNull JsonObj */ public static Feature fromGeometry(@Nullable Geometry geometry, @NonNull JsonObject properties, @Nullable String id) { - return new AutoValue_Feature(null, id, geometry, properties); + return new AutoValue_Feature(TYPE, null, id, geometry, properties); } /** @@ -159,7 +153,7 @@ public static Feature fromGeometry(@Nullable Geometry geometry, @NonNull JsonObj */ public static Feature fromGeometry(@Nullable Geometry geometry, @NonNull JsonObject properties, @Nullable String id, @Nullable BoundingBox bbox) { - return new AutoValue_Feature(bbox, id, geometry, properties); + return new AutoValue_Feature(TYPE, bbox, id, geometry, properties); } /** @@ -172,9 +166,7 @@ public static Feature fromGeometry(@Nullable Geometry geometry, @NonNull JsonObj */ @NonNull @Override - public String type() { - return TYPE; - } + public abstract String type(); /** * A Feature Collection might have a member named {@code bbox} to include information on the @@ -233,8 +225,6 @@ public String toJson() { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapter(Point.class, new PointSerializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()); - gson.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT); - gson.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); return gson.create().toJson(this); } diff --git a/services-geojson/src/main/java/com/mapbox/geojson/FeatureCollection.java b/services-geojson/src/main/java/com/mapbox/geojson/FeatureCollection.java index 9c531594c..21128630c 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/FeatureCollection.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/FeatureCollection.java @@ -3,18 +3,15 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.google.auto.value.AutoValue; -import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.TypeAdapter; -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; import com.mapbox.geojson.gson.BoundingBoxDeserializer; +import com.mapbox.geojson.gson.BoundingBoxSerializer; import com.mapbox.geojson.gson.GeoJsonAdapterFactory; import com.mapbox.geojson.gson.GeometryDeserializer; import com.mapbox.geojson.gson.PointDeserializer; import com.mapbox.geojson.gson.PointSerializer; -import com.mapbox.geojson.gson.BoundingBoxSerializer; import java.util.ArrayList; import java.util.Arrays; @@ -43,8 +40,6 @@ @AutoValue public abstract class FeatureCollection implements GeoJson { - @Expose - @SerializedName("type") private static final String TYPE = "FeatureCollection"; /** @@ -77,7 +72,7 @@ public static FeatureCollection fromJson(@NonNull String json) { * @since 1.0.0 */ public static FeatureCollection fromFeatures(@NonNull Feature[] features) { - return new AutoValue_FeatureCollection(null, Arrays.asList(features)); + return new AutoValue_FeatureCollection(TYPE, null, Arrays.asList(features)); } /** @@ -90,7 +85,7 @@ public static FeatureCollection fromFeatures(@NonNull Feature[] features) { * @since 1.0.0 */ public static FeatureCollection fromFeatures(@NonNull List features) { - return new AutoValue_FeatureCollection(null, features); + return new AutoValue_FeatureCollection(TYPE, null, features); } /** @@ -104,7 +99,7 @@ public static FeatureCollection fromFeatures(@NonNull List features) { public static FeatureCollection fromFeature(@NonNull Feature feature) { List featureList = new ArrayList<>(); featureList.add(feature); - return new AutoValue_FeatureCollection(null, featureList); + return new AutoValue_FeatureCollection(TYPE, null, featureList); } /** @@ -120,7 +115,7 @@ public static FeatureCollection fromFeature(@NonNull Feature feature) { */ public static FeatureCollection fromFeatures(@NonNull Feature[] features, @Nullable BoundingBox bbox) { - return new AutoValue_FeatureCollection(bbox, Arrays.asList(features)); + return new AutoValue_FeatureCollection(TYPE, bbox, Arrays.asList(features)); } /** @@ -136,7 +131,7 @@ public static FeatureCollection fromFeatures(@NonNull Feature[] features, */ public static FeatureCollection fromFeatures(@NonNull List features, @Nullable BoundingBox bbox) { - return new AutoValue_FeatureCollection(bbox, features); + return new AutoValue_FeatureCollection(TYPE, bbox, features); } /** @@ -152,7 +147,7 @@ public static FeatureCollection fromFeature(@NonNull Feature feature, @Nullable BoundingBox bbox) { List featureList = new ArrayList<>(); featureList.add(feature); - return new AutoValue_FeatureCollection(bbox, featureList); + return new AutoValue_FeatureCollection(TYPE, bbox, featureList); } /** @@ -165,9 +160,7 @@ public static FeatureCollection fromFeature(@NonNull Feature feature, */ @NonNull @Override - public String type() { - return TYPE; - } + public abstract String type(); /** * A Feature Collection might have a member named {@code bbox} to include information on the @@ -206,8 +199,6 @@ public String toJson() { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapter(Point.class, new PointSerializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()); - gson.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT); - gson.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); return gson.create().toJson(this); } diff --git a/services-geojson/src/main/java/com/mapbox/geojson/GeometryCollection.java b/services-geojson/src/main/java/com/mapbox/geojson/GeometryCollection.java index 4bfd5c3ba..6fce1593e 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/GeometryCollection.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/GeometryCollection.java @@ -3,18 +3,15 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.google.auto.value.AutoValue; -import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.TypeAdapter; -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; import com.mapbox.geojson.gson.BoundingBoxDeserializer; +import com.mapbox.geojson.gson.BoundingBoxSerializer; import com.mapbox.geojson.gson.GeoJsonAdapterFactory; import com.mapbox.geojson.gson.GeometryDeserializer; import com.mapbox.geojson.gson.PointDeserializer; import com.mapbox.geojson.gson.PointSerializer; -import com.mapbox.geojson.gson.BoundingBoxSerializer; import java.io.Serializable; import java.util.ArrayList; @@ -65,8 +62,6 @@ @AutoValue public abstract class GeometryCollection implements GeoJson, Serializable { - @Expose - @SerializedName("type") private static final String TYPE = "GeometryCollection"; /** @@ -97,7 +92,7 @@ public static GeometryCollection fromJson(String json) { * @since 1.0.0 */ public static GeometryCollection fromGeometries(@NonNull List geometries) { - return new AutoValue_GeometryCollection(null, geometries); + return new AutoValue_GeometryCollection(TYPE, null, geometries); } /** @@ -111,7 +106,7 @@ public static GeometryCollection fromGeometries(@NonNull List geometri public static GeometryCollection fromGeometry(@NonNull Geometry geometry) { List geometries = new ArrayList<>(); geometries.add(geometry); - return new AutoValue_GeometryCollection(null, geometries); + return new AutoValue_GeometryCollection(TYPE, null, geometries); } /** @@ -125,7 +120,7 @@ public static GeometryCollection fromGeometry(@NonNull Geometry geometry) { */ public static GeometryCollection fromGeometries(@NonNull List geometries, @Nullable BoundingBox bbox) { - return new AutoValue_GeometryCollection(bbox, geometries); + return new AutoValue_GeometryCollection(TYPE, bbox, geometries); } /** @@ -141,7 +136,7 @@ public static GeometryCollection fromGeometry(@NonNull Geometry geometry, @Nullable BoundingBox bbox) { List geometries = new ArrayList<>(); geometries.add(geometry); - return new AutoValue_GeometryCollection(bbox, geometries); + return new AutoValue_GeometryCollection(TYPE, bbox, geometries); } /** @@ -154,9 +149,7 @@ public static GeometryCollection fromGeometry(@NonNull Geometry geometry, */ @NonNull @Override - public String type() { - return TYPE; - } + public abstract String type(); /** * A Feature Collection might have a member named {@code bbox} to include information on the @@ -195,8 +188,6 @@ public String toJson() { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapter(Point.class, new PointSerializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()); - gson.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT); - gson.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); return gson.create().toJson(this); } diff --git a/services-geojson/src/main/java/com/mapbox/geojson/LineString.java b/services-geojson/src/main/java/com/mapbox/geojson/LineString.java index b78baef84..4ae27aa76 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/LineString.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/LineString.java @@ -3,18 +3,15 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.google.auto.value.AutoValue; -import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.TypeAdapter; -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; -import com.mapbox.geojson.gson.GeoJsonAdapterFactory; -import com.mapbox.geojson.utils.PolylineUtils; import com.mapbox.geojson.gson.BoundingBoxDeserializer; import com.mapbox.geojson.gson.BoundingBoxSerializer; +import com.mapbox.geojson.gson.GeoJsonAdapterFactory; import com.mapbox.geojson.gson.PointDeserializer; import com.mapbox.geojson.gson.PointSerializer; +import com.mapbox.geojson.utils.PolylineUtils; import java.io.Serializable; import java.util.List; @@ -55,8 +52,6 @@ @AutoValue public abstract class LineString implements Geometry>, Serializable { - @Expose - @SerializedName("type") private static final String TYPE = "LineString"; /** @@ -89,7 +84,7 @@ public static LineString fromJson(String json) { * @since 3.0.0 */ public static LineString fromLngLats(@NonNull MultiPoint multiPoint) { - return new AutoValue_LineString(null, multiPoint.coordinates()); + return new AutoValue_LineString(TYPE, null, multiPoint.coordinates()); } /** @@ -107,7 +102,7 @@ public static LineString fromLngLats(@NonNull MultiPoint multiPoint) { * @since 3.0.0 */ public static LineString fromLngLats(@NonNull List points) { - return new AutoValue_LineString(null, points); + return new AutoValue_LineString(TYPE, null, points); } /** @@ -126,7 +121,7 @@ public static LineString fromLngLats(@NonNull List points) { * @since 3.0.0 */ public static LineString fromLngLats(@NonNull List points, @Nullable BoundingBox bbox) { - return new AutoValue_LineString(bbox, points); + return new AutoValue_LineString(TYPE, bbox, points); } /** @@ -140,7 +135,7 @@ public static LineString fromLngLats(@NonNull List points, @Nullable Boun * @since 3.0.0 */ public static LineString fromLngLats(@NonNull MultiPoint multiPoint, @Nullable BoundingBox bbox) { - return new AutoValue_LineString(bbox, multiPoint.coordinates()); + return new AutoValue_LineString(TYPE, bbox, multiPoint.coordinates()); } /** @@ -171,9 +166,7 @@ public static LineString fromPolyline(@NonNull String polyline, int precision) { */ @NonNull @Override - public String type() { - return TYPE; - } + public abstract String type(); /** * A Feature Collection might have a member named {@code bbox} to include information on the @@ -211,8 +204,6 @@ public String toJson() { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapter(Point.class, new PointSerializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()); - gson.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT); - gson.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); return gson.create().toJson(this); } diff --git a/services-geojson/src/main/java/com/mapbox/geojson/MultiLineString.java b/services-geojson/src/main/java/com/mapbox/geojson/MultiLineString.java index 047015394..35c41e8eb 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/MultiLineString.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/MultiLineString.java @@ -3,15 +3,12 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.google.auto.value.AutoValue; -import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.TypeAdapter; -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; +import com.mapbox.geojson.gson.BoundingBoxSerializer; import com.mapbox.geojson.gson.GeoJsonAdapterFactory; import com.mapbox.geojson.gson.PointDeserializer; -import com.mapbox.geojson.gson.BoundingBoxSerializer; import com.mapbox.geojson.gson.PointSerializer; import java.io.Serializable; @@ -55,8 +52,6 @@ @AutoValue public abstract class MultiLineString implements Geometry>>, Serializable { - @Expose - @SerializedName("type") private static final String TYPE = "MultiLineString"; /** @@ -91,7 +86,7 @@ public static MultiLineString fromLineStrings(@NonNull List lineStri for (LineString lineString : lineStrings) { coordinates.add(lineString.coordinates()); } - return new AutoValue_MultiLineString(null, coordinates); + return new AutoValue_MultiLineString(TYPE, null, coordinates); } /** @@ -106,7 +101,7 @@ public static MultiLineString fromLineStrings(@NonNull List lineStri public static MultiLineString fromLineString(@NonNull LineString lineString) { List> coordinates = new ArrayList<>(); coordinates.add(lineString.coordinates()); - return new AutoValue_MultiLineString(null, coordinates); + return new AutoValue_MultiLineString(TYPE, null, coordinates); } /** @@ -127,7 +122,7 @@ public static MultiLineString fromLineStrings(@NonNull List lineStri for (LineString lineString : lineStrings) { coordinates.add(lineString.coordinates()); } - return new AutoValue_MultiLineString(bbox, coordinates); + return new AutoValue_MultiLineString(TYPE, bbox, coordinates); } /** @@ -144,7 +139,7 @@ public static MultiLineString fromLineString(@NonNull LineString lineString, @Nullable BoundingBox bbox) { List> coordinates = new ArrayList<>(); coordinates.add(lineString.coordinates()); - return new AutoValue_MultiLineString(bbox, coordinates); + return new AutoValue_MultiLineString(TYPE, bbox, coordinates); } /** @@ -159,7 +154,7 @@ public static MultiLineString fromLineString(@NonNull LineString lineString, * @since 3.0.0 */ public static MultiLineString fromLngLats(@NonNull List> points) { - return new AutoValue_MultiLineString(null, points); + return new AutoValue_MultiLineString(TYPE, null, points); } /** @@ -176,7 +171,7 @@ public static MultiLineString fromLngLats(@NonNull List> points) { */ public static MultiLineString fromLngLats(@NonNull List> points, @Nullable BoundingBox bbox) { - return new AutoValue_MultiLineString(bbox, points); + return new AutoValue_MultiLineString(TYPE, bbox, points); } /** @@ -189,9 +184,7 @@ public static MultiLineString fromLngLats(@NonNull List> points, */ @NonNull @Override - public String type() { - return TYPE; - } + public abstract String type(); /** * A Feature Collection might have a member named {@code bbox} to include information on the @@ -243,8 +236,6 @@ public String toJson() { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapter(Point.class, new PointSerializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()); - gson.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT); - gson.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); return gson.create().toJson(this); } diff --git a/services-geojson/src/main/java/com/mapbox/geojson/MultiPoint.java b/services-geojson/src/main/java/com/mapbox/geojson/MultiPoint.java index 34d515c1f..755ddf47b 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/MultiPoint.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/MultiPoint.java @@ -40,8 +40,6 @@ @AutoValue public abstract class MultiPoint implements Geometry>, Serializable { - @Expose - @SerializedName("type") private static final String TYPE = "MultiPoint"; /** @@ -74,7 +72,7 @@ public static MultiPoint fromJson(@NonNull String json) { * @since 3.0.0 */ public static MultiPoint fromLngLats(@NonNull List points) { - return new AutoValue_MultiPoint(null, points); + return new AutoValue_MultiPoint(TYPE, null, points); } /** @@ -90,7 +88,7 @@ public static MultiPoint fromLngLats(@NonNull List points) { * @since 3.0.0 */ public static MultiPoint fromLngLats(@NonNull List points, @Nullable BoundingBox bbox) { - return new AutoValue_MultiPoint(bbox, points); + return new AutoValue_MultiPoint(TYPE, bbox, points); } /** @@ -103,9 +101,7 @@ public static MultiPoint fromLngLats(@NonNull List points, @Nullable Boun */ @NonNull @Override - public String type() { - return TYPE; - } + public abstract String type(); /** * A Feature Collection might have a member named {@code bbox} to include information on the @@ -143,8 +139,6 @@ public String toJson() { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapter(Point.class, new PointSerializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()); - gson.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT); - gson.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); return gson.create().toJson(this); } diff --git a/services-geojson/src/main/java/com/mapbox/geojson/MultiPolygon.java b/services-geojson/src/main/java/com/mapbox/geojson/MultiPolygon.java index ce3889a7b..5e0bb9128 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/MultiPolygon.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/MultiPolygon.java @@ -2,17 +2,13 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; - import com.google.auto.value.AutoValue; -import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.TypeAdapter; -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; +import com.mapbox.geojson.gson.BoundingBoxSerializer; import com.mapbox.geojson.gson.GeoJsonAdapterFactory; import com.mapbox.geojson.gson.PointDeserializer; -import com.mapbox.geojson.gson.BoundingBoxSerializer; import com.mapbox.geojson.gson.PointSerializer; import java.io.Serializable; @@ -74,8 +70,6 @@ @AutoValue public abstract class MultiPolygon implements Geometry>>>, Serializable { - @Expose - @SerializedName("type") private static final String TYPE = "MultiPolygon"; /** @@ -110,7 +104,7 @@ public static MultiPolygon fromPolygons(@NonNull List polygons) { for (Polygon polygon : polygons) { coordinates.add(polygon.coordinates()); } - return new AutoValue_MultiPolygon(null, coordinates); + return new AutoValue_MultiPolygon(TYPE, null, coordinates); } /** @@ -131,7 +125,7 @@ public static MultiPolygon fromPolygons(@NonNull List polygons, for (Polygon polygon : polygons) { coordinates.add(polygon.coordinates()); } - return new AutoValue_MultiPolygon(bbox, coordinates); + return new AutoValue_MultiPolygon(TYPE, bbox, coordinates); } /** @@ -147,7 +141,7 @@ public static MultiPolygon fromPolygons(@NonNull List polygons, public static MultiPolygon fromPolygon(@NonNull Polygon polygon) { List>> coordinates = new ArrayList<>(); coordinates.add(polygon.coordinates()); - return new AutoValue_MultiPolygon(null, coordinates); + return new AutoValue_MultiPolygon(TYPE, null, coordinates); } /** @@ -164,7 +158,7 @@ public static MultiPolygon fromPolygon(@NonNull Polygon polygon) { public static MultiPolygon fromPolygon(@NonNull Polygon polygon, @Nullable BoundingBox bbox) { List>> coordinates = new ArrayList<>(); coordinates.add(polygon.coordinates()); - return new AutoValue_MultiPolygon(bbox, coordinates); + return new AutoValue_MultiPolygon(TYPE, bbox, coordinates); } /** @@ -177,7 +171,7 @@ public static MultiPolygon fromPolygon(@NonNull Polygon polygon, @Nullable Bound * @since 3.0.0 */ public static MultiPolygon fromLngLats(@NonNull List>> points) { - return new AutoValue_MultiPolygon(null, points); + return new AutoValue_MultiPolygon(TYPE, null, points); } /** @@ -192,7 +186,7 @@ public static MultiPolygon fromLngLats(@NonNull List>> points) */ public static MultiPolygon fromLngLats(@NonNull List>> points, @Nullable BoundingBox bbox) { - return new AutoValue_MultiPolygon(bbox, points); + return new AutoValue_MultiPolygon(TYPE, bbox, points); } /** @@ -219,9 +213,7 @@ public List polygons() { */ @NonNull @Override - public String type() { - return TYPE; - } + public abstract String type(); /** * A Feature Collection might have a member named {@code bbox} to include information on the @@ -259,8 +251,6 @@ public String toJson() { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapter(Point.class, new PointSerializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()); - gson.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT); - gson.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); return gson.create().toJson(this); } diff --git a/services-geojson/src/main/java/com/mapbox/geojson/Point.java b/services-geojson/src/main/java/com/mapbox/geojson/Point.java index b4ccc4c57..0c8f32483 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/Point.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/Point.java @@ -18,6 +18,7 @@ import com.mapbox.geojson.gson.BoundingBoxDeserializer; import com.mapbox.geojson.gson.BoundingBoxSerializer; import com.mapbox.geojson.gson.GeoJsonAdapterFactory; +import com.mapbox.geojson.gson.PointDeserializer; import com.mapbox.geojson.gson.PointSerializer; import java.io.Serializable; @@ -58,8 +59,6 @@ @AutoValue public abstract class Point implements Geometry>, Serializable { - @Expose - @SerializedName("type") private static final String TYPE = "Point"; /** @@ -101,7 +100,7 @@ public static Point fromLngLat( List coordinates = new ArrayList<>(); coordinates.add(longitude); coordinates.add(latitude); - return new AutoValue_Point(null, coordinates); + return new AutoValue_Point(TYPE, null, coordinates); } /** @@ -126,7 +125,7 @@ public static Point fromLngLat( List coordinates = new ArrayList<>(); coordinates.add(longitude); coordinates.add(latitude); - return new AutoValue_Point(bbox, coordinates); + return new AutoValue_Point(TYPE, bbox, coordinates); } /** @@ -153,7 +152,7 @@ public static Point fromLngLat( coordinates.add(longitude); coordinates.add(latitude); coordinates.add(altitude); - return new AutoValue_Point(null, coordinates); + return new AutoValue_Point(TYPE, null, coordinates); } /** @@ -181,7 +180,7 @@ public static Point fromLngLat( coordinates.add(longitude); coordinates.add(latitude); coordinates.add(altitude); - return new AutoValue_Point(bbox, coordinates); + return new AutoValue_Point(TYPE, bbox, coordinates); } /** @@ -248,9 +247,7 @@ public boolean hasAltitude() { */ @NonNull @Override - public String type() { - return TYPE; - } + public abstract String type(); /** * A Feature Collection might have a member named {@code bbox} to include information on the @@ -288,10 +285,7 @@ public String type() { @Override public String toJson() { GsonBuilder gson = new GsonBuilder(); - gson.registerTypeAdapter(Point.class, new PointSerializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()); - gson.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT); - gson.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); return gson.create().toJson(this); } diff --git a/services-geojson/src/main/java/com/mapbox/geojson/Polygon.java b/services-geojson/src/main/java/com/mapbox/geojson/Polygon.java index 66ce804dd..52c8bebd4 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/Polygon.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/Polygon.java @@ -64,8 +64,6 @@ @AutoValue public abstract class Polygon implements Geometry>>, Serializable { - @Expose - @SerializedName("type") private static final String TYPE = "Polygon"; /** @@ -100,7 +98,7 @@ public static Polygon fromJson(@NonNull String json) { * @since 3.0.0 */ public static Polygon fromLngLats(@NonNull List> coordinates) { - return new AutoValue_Polygon(null, coordinates); + return new AutoValue_Polygon(TYPE, null, coordinates); } /** @@ -116,7 +114,7 @@ public static Polygon fromLngLats(@NonNull List> coordinates) { */ public static Polygon fromLngLats(@NonNull List> coordinates, @Nullable BoundingBox bbox) { - return new AutoValue_Polygon(bbox, coordinates); + return new AutoValue_Polygon(TYPE, bbox, coordinates); } /** @@ -137,7 +135,7 @@ public static Polygon fromLngLats(@NonNull double[][][] coordinates) { } converted.add(innerList); } - return new AutoValue_Polygon(null, converted); + return new AutoValue_Polygon(TYPE, null, converted); } /** @@ -159,13 +157,13 @@ public static Polygon fromOuterInner(@NonNull LineString outer, @Nullable LineSt coordinates.add(outer.coordinates()); // If inner rings are set to null, return early. if (inner == null) { - return new AutoValue_Polygon(null, coordinates); + return new AutoValue_Polygon(TYPE, null, coordinates); } for (LineString lineString : inner) { isLinearRing(lineString); coordinates.add(lineString.coordinates()); } - return new AutoValue_Polygon(null, coordinates); + return new AutoValue_Polygon(TYPE, null, coordinates); } /** @@ -189,13 +187,13 @@ public static Polygon fromOuterInner(@NonNull LineString outer, @Nullable Boundi coordinates.add(outer.coordinates()); // If inner rings are set to null, return early. if (inner == null) { - return new AutoValue_Polygon(bbox, coordinates); + return new AutoValue_Polygon(TYPE, bbox, coordinates); } for (LineString lineString : inner) { isLinearRing(lineString); coordinates.add(lineString.coordinates()); } - return new AutoValue_Polygon(bbox, coordinates); + return new AutoValue_Polygon(TYPE, bbox, coordinates); } /** @@ -220,13 +218,13 @@ public static Polygon fromOuterInner(@NonNull LineString outer, coordinates.add(outer.coordinates()); // If inner rings are set to null, return early. if (inner == null || inner.isEmpty()) { - return new AutoValue_Polygon(null, coordinates); + return new AutoValue_Polygon(TYPE, null, coordinates); } for (LineString lineString : inner) { isLinearRing(lineString); coordinates.add(lineString.coordinates()); } - return new AutoValue_Polygon(null, coordinates); + return new AutoValue_Polygon(TYPE, null, coordinates); } /** @@ -252,13 +250,13 @@ public static Polygon fromOuterInner(@NonNull LineString outer, @Nullable Boundi coordinates.add(outer.coordinates()); // If inner rings are set to null, return early. if (inner == null) { - return new AutoValue_Polygon(bbox, coordinates); + return new AutoValue_Polygon(TYPE, bbox, coordinates); } for (LineString lineString : inner) { isLinearRing(lineString); coordinates.add(lineString.coordinates()); } - return new AutoValue_Polygon(bbox, coordinates); + return new AutoValue_Polygon(TYPE, bbox, coordinates); } /** @@ -303,9 +301,7 @@ public List inner() { */ @NonNull @Override - public String type() { - return TYPE; - } + public abstract String type(); /** * A Feature Collection might have a member named {@code bbox} to include information on the @@ -345,8 +341,6 @@ public String toJson() { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapter(Point.class, new PointSerializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()); - gson.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT); - gson.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES); return gson.create().toJson(this); } diff --git a/services-geojson/src/main/java/com/mapbox/geojson/gson/GeometryTypeAdapter.java b/services-geojson/src/main/java/com/mapbox/geojson/gson/GeometryTypeAdapter.java new file mode 100644 index 000000000..73ab05d47 --- /dev/null +++ b/services-geojson/src/main/java/com/mapbox/geojson/gson/GeometryTypeAdapter.java @@ -0,0 +1,27 @@ +package com.mapbox.geojson.gson; + +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; +import com.mapbox.geojson.Geometry; + +import java.io.IOException; + +public class GeometryTypeAdapter extends TypeAdapter { + + @Override + public void write(JsonWriter out, Geometry value) throws IOException { + out.beginObject(); + out.name("type").value(value.type()); + if (value.bbox() != null) { + out.name("bbox").jsonValue(value.bbox().toJson()); + } + out.name("coordinates").jsonValue(value.coordinates().toString()); + out.endObject(); + } + + @Override + public Geometry read(JsonReader in) throws IOException { + return null; + } +} diff --git a/services-geojson/src/test/java/com/mapbox/geojson/FeatureTest.java b/services-geojson/src/test/java/com/mapbox/geojson/FeatureTest.java index 66992ec69..ce8ccf4a7 100644 --- a/services-geojson/src/test/java/com/mapbox/geojson/FeatureTest.java +++ b/services-geojson/src/test/java/com/mapbox/geojson/FeatureTest.java @@ -75,9 +75,9 @@ public void bbox_doesSerializeWhenPresent() throws Exception { BoundingBox bbox = BoundingBox.fromCoordinates(1.0, 2.0, 3.0, 4.0); Feature feature = Feature.fromGeometry(lineString, bbox); - compareJson(feature.toJson(), - "{\"type\":\"Feature\",\"bbox\":[1.0,2.0,3.0,4.0],\"geometry\":" - + "{\"type\":\"LineString\",\"coordinates\":[[1,2],[2,3]]},\"properties\":{}}"); + compareJson("{\"type\":\"Feature\",\"bbox\":[1.0,2.0,3.0,4.0],\"geometry\":" + + "{\"type\":\"LineString\",\"coordinates\":[[1,2],[2,3]]},\"properties\":{}}", + feature.toJson()); } @Test diff --git a/services-geojson/src/test/java/com/mapbox/geojson/gson/BoundingBoxDeserializerTest.java b/services-geojson/src/test/java/com/mapbox/geojson/gson/BoundingBoxDeserializerTest.java index f46f0d836..891b1f065 100644 --- a/services-geojson/src/test/java/com/mapbox/geojson/gson/BoundingBoxDeserializerTest.java +++ b/services-geojson/src/test/java/com/mapbox/geojson/gson/BoundingBoxDeserializerTest.java @@ -27,7 +27,7 @@ public void bboxDeserializer_handlesOnlyOneAltitudeCorrectly() throws Exception } @Test - public void bboxDeserializer_deserializeThreeDimensionalArray() throws Exception { + public void bboxDeserializer_deserializeThreeDimensionalArray() { String fixtureJson = "{\"type\":\"LineString\",\"bbox\": " + "[100.0,0.0,-100.0,105.0,1.0,0.0],\"coordinates\":[[100.0, 0.0],[101.0, 1.0]]}"; @@ -36,7 +36,7 @@ public void bboxDeserializer_deserializeThreeDimensionalArray() throws Exception } @Test - public void bboxDeserializer_deserializeTwoDimensionalArray() throws Exception { + public void bboxDeserializer_deserializeTwoDimensionalArray() { String fixtureJson = "{\"type\":\"Point\",\"bbox\":[1,2,3,4],\"coordinates\":[100,0]}"; Point point = Point.fromJson(fixtureJson); diff --git a/services-staticmap/src/test/java/com/mapbox/api/staticmap/v1/MapboxStaticMapTest.java b/services-staticmap/src/test/java/com/mapbox/api/staticmap/v1/MapboxStaticMapTest.java index e4780ba30..68c3d9dec 100644 --- a/services-staticmap/src/test/java/com/mapbox/api/staticmap/v1/MapboxStaticMapTest.java +++ b/services-staticmap/src/test/java/com/mapbox/api/staticmap/v1/MapboxStaticMapTest.java @@ -1,13 +1,17 @@ package com.mapbox.api.staticmap.v1; +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import com.mapbox.api.staticmap.v1.models.StaticMarkerAnnotation; +import com.mapbox.api.staticmap.v1.models.StaticPolylineAnnotation; import com.mapbox.core.TestUtils; import com.mapbox.core.exceptions.ServicesException; import com.mapbox.core.utils.TextUtils; import com.mapbox.geojson.LineString; import com.mapbox.geojson.Point; -import com.mapbox.api.staticmap.v1.models.StaticMarkerAnnotation; -import com.mapbox.api.staticmap.v1.models.StaticPolylineAnnotation; - import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -16,9 +20,6 @@ import java.util.ArrayList; import java.util.List; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - public class MapboxStaticMapTest extends TestUtils { @Rule @@ -192,8 +193,9 @@ public void geoJson_getsAddedToUrlCorrectly() throws Exception { .cameraAuto(true) .geoJson(lineString) .build(); - assertTrue(staticMap.url().toString().contains( - "geojson(%7B%22coordinates%22:[[100.0,0.0],[101.0,1.0]],%22type%22:%22LineString%22%7D)")); + assertThat(staticMap.url().toString(), + containsString("geojson(%7B%22type%22:%22LineString%22,%22coordinates%22:" + + "[[100.0,0.0],[101.0,1.0]]%7D)")); } @Test diff --git a/services-turf/src/test/java/com/mapbox/turf/TurfAssertionsTest.java b/services-turf/src/test/java/com/mapbox/turf/TurfAssertionsTest.java index a12e73074..6e15c7264 100644 --- a/services-turf/src/test/java/com/mapbox/turf/TurfAssertionsTest.java +++ b/services-turf/src/test/java/com/mapbox/turf/TurfAssertionsTest.java @@ -56,7 +56,7 @@ public void testInvariantFeatureOf1() throws TurfException { @Test public void testInvariantFeatureOf2() throws TurfException { - String json = "{}"; + String json = "{ type: 'Feature'}"; thrown.expect(TurfException.class); thrown.expectMessage(startsWith("Invalid input to foo, Feature with geometry required")); TurfAssertions.featureOf(Feature.fromJson(json), "Polygon", "foo"); @@ -88,7 +88,7 @@ public void testInvariantCollectionOf1() throws TurfException { @Test public void testInvariantCollectionOf2() throws TurfException { - String json = "{}"; + String json = "{type: 'FeatureCollection'}"; thrown.expect(TurfException.class); thrown.expectMessage(startsWith("collectionOf() requires a name")); TurfAssertions.collectionOf(FeatureCollection.fromJson(json), "Polygon", null); @@ -96,7 +96,7 @@ public void testInvariantCollectionOf2() throws TurfException { @Test public void testInvariantCollectionOf3() throws TurfException { - String json = "{}"; + String json = "{type: 'FeatureCollection'}"; thrown.expect(TurfException.class); thrown.expectMessage(startsWith("Invalid input to foo, FeatureCollection required")); TurfAssertions.collectionOf(FeatureCollection.fromJson(json), "Polygon", "foo");