From eb559ecc8bcdf42b32510c44cf025995ab7de438 Mon Sep 17 00:00:00 2001 From: Acacia Warner Date: Mon, 23 Oct 2023 16:15:25 -0600 Subject: [PATCH] Remove unused fields, update tests. --- .../api/InternationalGeolocateType.java | 2 +- .../international_autocomplete/Candidate.java | 6 +- .../international_autocomplete/Client.java | 10 -- .../international_autocomplete/Lookup.java | 41 +------ ...nationalAutocompleteExampleDeprecated.java | 1 - .../CandidateTest.java | 40 ++++--- .../ClientTest.java | 10 +- .../CandidateTest.java | 53 +++++++++ .../ClientTest.java | 109 ++++++++++++++++++ 9 files changed, 194 insertions(+), 78 deletions(-) create mode 100644 src/test/java/com/smartystreets/api/international_autocomplete_deprecated/CandidateTest.java create mode 100644 src/test/java/com/smartystreets/api/international_autocomplete_deprecated/ClientTest.java diff --git a/src/main/java/com/smartystreets/api/InternationalGeolocateType.java b/src/main/java/com/smartystreets/api/InternationalGeolocateType.java index 9de1e25..dd5c8ab 100644 --- a/src/main/java/com/smartystreets/api/InternationalGeolocateType.java +++ b/src/main/java/com/smartystreets/api/InternationalGeolocateType.java @@ -2,7 +2,7 @@ public enum InternationalGeolocateType { - NONE(""), IP_ADDRESS("ip_address"); + ADMIN_AREA("adminarea"), LOCALITY("locality"), POSTAL_CODE("postalcode"), GEOCODES("geocodes"), NONE(""); private final String name; InternationalGeolocateType(String name){ diff --git a/src/main/java/com/smartystreets/api/international_autocomplete/Candidate.java b/src/main/java/com/smartystreets/api/international_autocomplete/Candidate.java index cd86fb1..a01e764 100644 --- a/src/main/java/com/smartystreets/api/international_autocomplete/Candidate.java +++ b/src/main/java/com/smartystreets/api/international_autocomplete/Candidate.java @@ -16,9 +16,9 @@ public class Candidate implements Serializable { private String postalCode; private String countryISO3; - private int entries; - private String addressText; - private String addressID; + private int entries; + private String addressText; + private String addressID; //region [ Fields ] diff --git a/src/main/java/com/smartystreets/api/international_autocomplete/Client.java b/src/main/java/com/smartystreets/api/international_autocomplete/Client.java index 534861d..8988a39 100644 --- a/src/main/java/com/smartystreets/api/international_autocomplete/Client.java +++ b/src/main/java/com/smartystreets/api/international_autocomplete/Client.java @@ -44,18 +44,8 @@ private Request buildRequest(Lookup lookup) { request.putParameter("country", lookup.getCountry()); request.putParameter("search", lookup.getSearch()); request.putParameter("max_results", String.valueOf(lookup.getMaxResults())); - request.putParameter("distance", String.valueOf(lookup.getDistance())); - if (!Objects.equals(lookup.getGeolocation(), InternationalGeolocateType.NONE.getName())) { - request.putParameter("geolocation", lookup.getGeolocation()); - } request.putParameter("include_only_locality", lookup.getLocality()); request.putParameter("include_only_postal_code", lookup.getPostalCode()); - if (lookup.getLatitude() != null) { - request.putParameter("latitude", String.valueOf(lookup.getLatitude())); - } - if (lookup.getLongitude() != null) { - request.putParameter("longitude", String.valueOf(lookup.getLongitude())); - } return request; } diff --git a/src/main/java/com/smartystreets/api/international_autocomplete/Lookup.java b/src/main/java/com/smartystreets/api/international_autocomplete/Lookup.java index 3aac451..d3d4cc1 100644 --- a/src/main/java/com/smartystreets/api/international_autocomplete/Lookup.java +++ b/src/main/java/com/smartystreets/api/international_autocomplete/Lookup.java @@ -19,12 +19,9 @@ public class Lookup { private String search; private String addressID; private int maxResults; - private int distance; - private InternationalGeolocateType geolocation; private String locality; private String postalCode; - private Float latitude; - private Float longitude; + //endregion //region [ Constructors ] @@ -34,8 +31,6 @@ public class Lookup { */ public Lookup() { this.maxResults = MAX_RESULTS_DEFAULT; - this.distance = DISTANCE_DEFAULT; - this.geolocation = InternationalGeolocateType.NONE; } /** @@ -74,15 +69,6 @@ public int getMaxResults() { return this.maxResults; } - public int getDistance() { - return this.distance; - } - - public String getGeolocation() { - return this.geolocation.getName(); - } - - public String getLocality() { return this.locality; } @@ -91,15 +77,6 @@ public String getPostalCode() { return this.postalCode; } - public Float getLatitude() { - return latitude; - } - - public Float getLongitude() { - return longitude; - } - - //endregion //region [ Setters ] @@ -123,14 +100,6 @@ public void setMaxResults(int maxResults) { this.maxResults = maxResults; } - public void setDistance(int distance) { - this.distance = distance; - } - - public void setGeolocation(InternationalGeolocateType geolocation) { - this.geolocation = geolocation; - } - public void setLocality(String locality) { this.locality = locality; } @@ -139,13 +108,5 @@ public void setPostalCode(String postalCode) { this.postalCode = postalCode; } - public void setLatitude(Float latitude) { - this.latitude = latitude; - } - - public void setLongitude(Float longitude) { - this.longitude = longitude; - } - //endregion } diff --git a/src/main/java/examples/InternationalAutocompleteExampleDeprecated.java b/src/main/java/examples/InternationalAutocompleteExampleDeprecated.java index b06054d..92d81ba 100644 --- a/src/main/java/examples/InternationalAutocompleteExampleDeprecated.java +++ b/src/main/java/examples/InternationalAutocompleteExampleDeprecated.java @@ -15,7 +15,6 @@ public class InternationalAutocompleteExampleDeprecated { public static void main(String[] args) { // We recommend storing your authentication credentials in environment variables. // for server-to-server requests, use this code: - //StaticCredentials credentials = new StaticCredentials(System.getenv("SMARTY_AUTH_ID"), System.getenv("SMARTY_AUTH_TOKEN")); // for client-side requests (browser/mobile), use this code: diff --git a/src/test/java/com/smartystreets/api/international_autocomplete/CandidateTest.java b/src/test/java/com/smartystreets/api/international_autocomplete/CandidateTest.java index 919a8d8..58dc2ad 100644 --- a/src/test/java/com/smartystreets/api/international_autocomplete/CandidateTest.java +++ b/src/test/java/com/smartystreets/api/international_autocomplete/CandidateTest.java @@ -12,22 +12,27 @@ public class CandidateTest { public void testFullJSONDeserialization() throws Exception { String rawJSON = "{\n" + - "\"candidates\": [\n" + - "{\n" + - "\"street\": \"12TH AV\",\n" + - "\"locality\": \"OCEAN GROVE\",\n" + - "\"administrative_area\": \"VIC\",\n" + - "\"postal_code\": \"3226\",\n" + - "\"country_iso3\": \"AUS\"\n" + - "},\n" + - "{\n" + - "\"street\": \"MONG FAT STREET\",\n" + - "\"locality\": \"TUEN MUN\",\n" + - "\"administrative_area\": \"TUEN MUN DISTRICT\",\n" + - "\"country_iso3\": \"HKG\"\n" + - "}\n" + - "]\n" + - "}"; + "\"candidates\": [\n" + + "{\n" + + "\"street\": \"12TH AV\",\n" + + "\"locality\": \"OCEAN GROVE\",\n" + + "\"administrative_area\": \"VIC\",\n" + + "\"postal_code\": \"3226\",\n" + + "\"country_iso3\": \"AUS\"\n" + + "},\n" + + "{\n" + + "\"street\": \"MONG FAT STREET\",\n" + + "\"locality\": \"TUEN MUN\",\n" + + "\"administrative_area\": \"TUEN MUN DISTRICT\",\n" + + "\"country_iso3\": \"HKG\"\n" + + "},\n" + + "{\n" + + "\"entries\": 54,\n" + + "\"address_text\": \"11 Laguna Pky Brechin, ON, L0K 1B0\",\n" + + "\"address_id\": \"HB5SHA8DBQ8QKS0mED0nRykgGEctOhM2LRs\"\n" + + "}\n" + + "]\n" + + "}"; byte[] bytes = rawJSON.getBytes(); @@ -43,5 +48,8 @@ public void testFullJSONDeserialization() throws Exception { assertEquals("TUEN MUN", candidates[1].getLocality()); assertEquals("TUEN MUN DISTRICT", candidates[1].getAdministrativeArea()); assertEquals("HKG", candidates[1].getCountryISO3()); + assertEquals(54, candidates[2].getEntries()); + assertEquals("11 Laguna Pky Brechin, ON, L0K 1B0", candidates[2].getAddressText()); + assertEquals("HB5SHA8DBQ8QKS0mED0nRykgGEctOhM2LRs", candidates[2].getAddressID()); } } \ No newline at end of file diff --git a/src/test/java/com/smartystreets/api/international_autocomplete/ClientTest.java b/src/test/java/com/smartystreets/api/international_autocomplete/ClientTest.java index 143763a..009f33d 100644 --- a/src/test/java/com/smartystreets/api/international_autocomplete/ClientTest.java +++ b/src/test/java/com/smartystreets/api/international_autocomplete/ClientTest.java @@ -24,7 +24,7 @@ public void testSendingSinglePrefixOnlyLookup() throws Exception { client.send(new Lookup("1")); - assertEquals("http://localhost/?search=1&max_results=5&distance=5", capturingSender.getRequest().getUrl()); + assertEquals("http://localhost/?search=1&max_results=5", capturingSender.getRequest().getUrl()); } @Test @@ -33,7 +33,7 @@ public void testSendingSinglePartiallyPopulatedLookup() throws Exception { URLPrefixSender sender = new URLPrefixSender("http://localhost/", capturingSender); FakeSerializer serializer = new FakeSerializer(new Result()); Client client = new Client(sender, serializer); - String expectedURL = "http://localhost/?country=1&search=2&max_results=5&distance=5&include_only_locality=4&include_only_postal_code=5"; + String expectedURL = "http://localhost/?country=1&search=2&max_results=5&include_only_locality=4&include_only_postal_code=5"; Lookup lookup = new Lookup(); lookup.setCountry("1"); lookup.setSearch("2"); @@ -51,17 +51,13 @@ public void testSendingSingleFullyPopulatedLookup() throws Exception { URLPrefixSender sender = new URLPrefixSender("http://localhost/", capturingSender); FakeSerializer serializer = new FakeSerializer(new Result()); Client client = new Client(sender, serializer); - String expectedURL = "http://localhost/?country=1&search=2&max_results=10&distance=8&geolocation=ip_address&include_only_locality=4&include_only_postal_code=5&latitude=10.1&longitude=11.2"; + String expectedURL = "http://localhost/?country=1&search=2&max_results=10&include_only_locality=4&include_only_postal_code=5"; Lookup lookup = new Lookup(); lookup.setCountry("1"); lookup.setSearch("2"); lookup.setMaxResults(10); - lookup.setDistance(8); - lookup.setGeolocation(InternationalGeolocateType.IP_ADDRESS); lookup.setLocality("4"); lookup.setPostalCode("5"); - lookup.setLatitude(10.1f); - lookup.setLongitude(11.2f); client.send(lookup); diff --git a/src/test/java/com/smartystreets/api/international_autocomplete_deprecated/CandidateTest.java b/src/test/java/com/smartystreets/api/international_autocomplete_deprecated/CandidateTest.java new file mode 100644 index 0000000..d61bdae --- /dev/null +++ b/src/test/java/com/smartystreets/api/international_autocomplete_deprecated/CandidateTest.java @@ -0,0 +1,53 @@ +package com.smartystreets.api.international_autocomplete_deprecated; + +import com.smartystreets.api.SmartySerializer; +import com.smartystreets.api.international_autocomplete_deprecated.Candidate; +import com.smartystreets.api.international_autocomplete_deprecated.Result; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class CandidateTest { + + + @Test + public void testFullJSONDeserialization() throws Exception { + + String rawJSON = "{\n" + + "\"candidates\": [\n" + + "{\n" + + "\"street\": \"12TH AV\",\n" + + "\"locality\": \"OCEAN GROVE\",\n" + + "\"administrative_area\": \"VIC\",\n" + + "\"sub_administrative_area\": \"WHITTLESEA\"," + + "\"postal_code\": \"3226\",\n" + + "\"country_iso3\": \"AUS\"\n" + + "},\n" + + "{\n" + + "\"street\": \"MONG FAT STREET\",\n" + + "\"locality\": \"TUEN MUN\",\n" + + "\"administrative_area\": \"TUEN MUN DISTRICT\",\n" + + "\"super_administrative_area\": \"HONG KONG\",\n" + + "\"country_iso3\": \"HKG\"\n" + + "}\n" + + "]\n" + + "}"; + + byte[] bytes = rawJSON.getBytes(); + + Result result = new SmartySerializer().deserialize(bytes, Result.class); + Candidate[] candidates = result.getCandidates(); + + assertEquals("12TH AV", candidates[0].getStreet()); + assertEquals("OCEAN GROVE", candidates[0].getLocality()); + assertEquals("VIC", candidates[0].getAdministrativeArea()); + assertEquals("WHITTLESEA", candidates[0].getSubAdministrativeArea()); + assertEquals("3226", candidates[0].getPostalCode()); + assertEquals("AUS", candidates[0].getCountryISO3()); + assertEquals("MONG FAT STREET", candidates[1].getStreet()); + assertEquals("TUEN MUN", candidates[1].getLocality()); + assertEquals("TUEN MUN DISTRICT", candidates[1].getAdministrativeArea()); + assertEquals("HONG KONG", candidates[1].getSuperAdministrativeArea()); + assertEquals("HKG", candidates[1].getCountryISO3()); + } +} \ No newline at end of file diff --git a/src/test/java/com/smartystreets/api/international_autocomplete_deprecated/ClientTest.java b/src/test/java/com/smartystreets/api/international_autocomplete_deprecated/ClientTest.java new file mode 100644 index 0000000..b295c5a --- /dev/null +++ b/src/test/java/com/smartystreets/api/international_autocomplete_deprecated/ClientTest.java @@ -0,0 +1,109 @@ +package com.smartystreets.api.international_autocomplete_deprecated; + +import com.smartystreets.api.InternationalGeolocateType; +import com.smartystreets.api.Response; +import com.smartystreets.api.URLPrefixSender; +import com.smartystreets.api.international_autocomplete_deprecated.Client; +import com.smartystreets.api.international_autocomplete_deprecated.Lookup; +import com.smartystreets.api.international_autocomplete_deprecated.Result; +import com.smartystreets.api.mocks.FakeDeserializer; +import com.smartystreets.api.mocks.FakeSerializer; +import com.smartystreets.api.mocks.MockSender; +import com.smartystreets.api.mocks.RequestCapturingSender; +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +public class ClientTest { + //region [ Single Lookup ] + + @Test + public void testSendingSinglePrefixOnlyLookup() throws Exception { + RequestCapturingSender capturingSender = new RequestCapturingSender(); + URLPrefixSender sender = new URLPrefixSender("http://localhost/", capturingSender); + FakeSerializer serializer = new FakeSerializer(new Result()); + Client client = new Client(sender, serializer); + + client.send(new Lookup("1")); + + assertEquals("http://localhost/?search=1&max_results=5&distance=5", capturingSender.getRequest().getUrl()); + } + + @Test + public void testSendingSinglePartiallyPopulatedLookup() throws Exception { + RequestCapturingSender capturingSender = new RequestCapturingSender(); + URLPrefixSender sender = new URLPrefixSender("http://localhost/", capturingSender); + FakeSerializer serializer = new FakeSerializer(new Result()); + Client client = new Client(sender, serializer); + String expectedURL = "http://localhost/?country=1&search=2&max_results=5&distance=5&include_only_administrative_area=3&include_only_locality=4&include_only_postal_code=5"; + Lookup lookup = new Lookup(); + lookup.setCountry("1"); + lookup.setSearch("2"); + lookup.setAdministrativeArea("3"); + lookup.setLocality("4"); + lookup.setPostalCode("5"); + + client.send(lookup); + + assertEquals(expectedURL, capturingSender.getRequest().getUrl()); + } + + @Test + public void testSendingSingleFullyPopulatedLookup() throws Exception { + RequestCapturingSender capturingSender = new RequestCapturingSender(); + URLPrefixSender sender = new URLPrefixSender("http://localhost/", capturingSender); + FakeSerializer serializer = new FakeSerializer(new Result()); + Client client = new Client(sender, serializer); + String expectedURL = "http://localhost/?country=1&search=2&max_results=10&distance=8&geolocation=postalcode&include_only_administrative_area=3&include_only_locality=4&include_only_postal_code=5&latitude=10.1&longitude=11.2"; + Lookup lookup = new Lookup(); + lookup.setCountry("1"); + lookup.setSearch("2"); + lookup.setMaxResults(10); + lookup.setDistance(8); + lookup.setGeolocation(InternationalGeolocateType.POSTAL_CODE); + lookup.setAdministrativeArea("3"); + lookup.setLocality("4"); + lookup.setPostalCode("5"); + lookup.setLatitude(10.1f); + lookup.setLongitude(11.2f); + + client.send(lookup); + + assertEquals(expectedURL, capturingSender.getRequest().getUrl()); + } + + //endregion + + //region [ Response Handling ] + + @Test + public void testDeserializeCalledWithResponseBody() throws Exception { + Response response = new Response(0, "Hello, World!".getBytes()); + MockSender mockSender = new MockSender(response); + URLPrefixSender sender = new URLPrefixSender("http://localhost/", mockSender); + FakeDeserializer deserializer = new FakeDeserializer(new Result()); + Client client = new Client(sender, deserializer); + + client.send(new Lookup("1")); + + assertEquals(response.getPayload(), deserializer.getPayload()); + } + + @Test + public void testResultCorrectlyAssignedToCorrespondingLookup() throws Exception { + Lookup lookup = new Lookup("1"); + Result expectedResult = new Result(); + + MockSender mockSender = new MockSender(new Response(0, "{[]}".getBytes())); + URLPrefixSender sender = new URLPrefixSender("http://localhost/", mockSender); + FakeDeserializer deserializer = new FakeDeserializer(expectedResult); + Client client = new Client(sender, deserializer); + + client.send(lookup); + + assertArrayEquals(expectedResult.getCandidates(), lookup.getResult()); + } + + //endregion +}