From e0b2823fce6e8eba7f4dc5533bbf183149aae141 Mon Sep 17 00:00:00 2001 From: Duncan Beutler Date: Tue, 19 Nov 2019 16:36:50 -0700 Subject: [PATCH] Examples working --- pom.xml | 3 + smartystreets-java-sdk.iml | 4 +- .../api/us_autocomplete/Client.java | 1 + .../api/us_autocomplete_pro/Client.java | 26 +++--- .../api/us_autocomplete_pro/Lookup.java | 20 ++++- .../examples/UsAutocompleteProExample.java | 49 ++++++----- .../api/us_autocomplete/ClientTest.java | 2 +- .../api/us_autocomplete_pro/ClientTest.java | 83 +++++++++++++++++++ .../us_autocomplete_pro/SuggestionTest.java | 24 ++++++ 9 files changed, 166 insertions(+), 46 deletions(-) create mode 100644 src/test/java/com/smartystreets/api/us_autocomplete_pro/ClientTest.java create mode 100644 src/test/java/com/smartystreets/api/us_autocomplete_pro/SuggestionTest.java diff --git a/pom.xml b/pom.xml index dd659fc..c573dc1 100644 --- a/pom.xml +++ b/pom.xml @@ -100,6 +100,9 @@ org.apache.maven.plugins maven-javadoc-plugin + + 7 + 2.9.1 diff --git a/smartystreets-java-sdk.iml b/smartystreets-java-sdk.iml index b4db5c3..590b2ac 100644 --- a/smartystreets-java-sdk.iml +++ b/smartystreets-java-sdk.iml @@ -11,8 +11,8 @@ - - + + diff --git a/src/main/java/com/smartystreets/api/us_autocomplete/Client.java b/src/main/java/com/smartystreets/api/us_autocomplete/Client.java index 0e8b072..9af01e2 100644 --- a/src/main/java/com/smartystreets/api/us_autocomplete/Client.java +++ b/src/main/java/com/smartystreets/api/us_autocomplete/Client.java @@ -4,6 +4,7 @@ import com.smartystreets.api.*; import com.smartystreets.api.exceptions.SmartyException; +import com.smartystreets.api.*; import java.io.IOException; import java.util.ArrayList; diff --git a/src/main/java/com/smartystreets/api/us_autocomplete_pro/Client.java b/src/main/java/com/smartystreets/api/us_autocomplete_pro/Client.java index 37bdc49..961c3b0 100644 --- a/src/main/java/com/smartystreets/api/us_autocomplete_pro/Client.java +++ b/src/main/java/com/smartystreets/api/us_autocomplete_pro/Client.java @@ -21,7 +21,7 @@ public Client(Sender sender, Serializer serializer) { } public Suggestion[] send(Lookup lookup) throws SmartyException, IOException { - if (lookup == null || lookup.getPrefix() == null || lookup.getPrefix().length() == 0) + if (lookup == null || lookup.getSearch() == null || lookup.getSearch().length() == 0) throw new SmartyException("Send() must be passed a Lookup with the prefix field set."); Request request = this.buildRequest(lookup); @@ -38,29 +38,27 @@ public Suggestion[] send(Lookup lookup) throws SmartyException, IOException { private Request buildRequest(Lookup lookup) { Request request = new Request(); - request.putParameter("prefix", lookup.getPrefix()); - request.putParameter("suggestions", lookup.getMaxSuggestionsStringIfSet()); - request.putParameter("city_filter", this.buildFilterString(lookup.getCityFilter())); - request.putParameter("state_filter", this.buildFilterString(lookup.getStateFilter())); - request.putParameter("prefer", this.buildPreferString(lookup.getPrefer())); + request.putParameter("search", lookup.getSearch()); + request.putParameter("max_results", lookup.getMaxSuggestionsStringIfSet()); + request.putParameter("include_only_cities", this.buildString(lookup.getCityFilter())); + request.putParameter("include_only_states", this.buildString(lookup.getStateFilter())); + request.putParameter("include_only_zip_codes", this.buildString(lookup.getZipcodeFilter())); + request.putParameter("exclude_states", this.buildString(lookup.getExcludeStates())); + request.putParameter("prefer_cities", this.buildString(lookup.getPreferCity())); + request.putParameter("prefer_states", this.buildString(lookup.getPreferState())); + request.putParameter("prefer_zip_codes", this.buildString(lookup.getPreferZipcode())); request.putParameter("prefer_ratio", lookup.getPreferRatioStringIfSet()); if (lookup.getGeolocateType() != GeolocateType.NONE) { - request.putParameter("geolocate", "true"); - request.putParameter("geolocate_precision", lookup.getGeolocateType().getName()); + request.putParameter("prefer_geolocation", lookup.getGeolocateType().getName()); } - else request.putParameter("geolocate", "false"); return request; } - private String buildPreferString(ArrayList list) { + private String buildString(ArrayList list) { return buildStringFromList(list, ";"); } - private String buildFilterString(ArrayList list) { - return buildStringFromList(list, ","); - } - private String buildStringFromList(ArrayList list, String separator) { if (list.isEmpty()) return null; diff --git a/src/main/java/com/smartystreets/api/us_autocomplete_pro/Lookup.java b/src/main/java/com/smartystreets/api/us_autocomplete_pro/Lookup.java index fb1004d..f3aab46 100644 --- a/src/main/java/com/smartystreets/api/us_autocomplete_pro/Lookup.java +++ b/src/main/java/com/smartystreets/api/us_autocomplete_pro/Lookup.java @@ -10,7 +10,7 @@ * @see "https://smartystreets.com/docs/cloud/us-autocomplete-api#http-request-input-fields" */ public class Lookup { - final double PREFER_RATIO_DEFAULT = 1/3.0; + final int PREFER_RATIO_DEFAULT = 33; final int MAX_SUGGESTIONS_DEFAULT = 10; //region [ Fields ] @@ -21,10 +21,11 @@ public class Lookup { private ArrayList cityFilter; private ArrayList stateFilter; private ArrayList zipcodeFilter; + private ArrayList excludeStates; private ArrayList preferCity; private ArrayList preferState; private ArrayList preferZipcode; - private double preferRatio; + private int preferRatio; private GeolocateType preferGeolocation; //endregion @@ -40,6 +41,7 @@ public Lookup() { this.cityFilter = new ArrayList<>(); this.stateFilter = new ArrayList<>(); this.zipcodeFilter = new ArrayList<>(); + this.excludeStates = new ArrayList<>(); this.preferCity = new ArrayList<>(); this.preferState = new ArrayList<>(); this.preferZipcode = new ArrayList<>(); @@ -78,6 +80,14 @@ public ArrayList getStateFilter() { return this.stateFilter; } + public ArrayList getZipcodeFilter() { + return this.zipcodeFilter; + } + + public ArrayList getExcludeStates() { + return this.excludeStates; + } + public ArrayList getPreferCity() { return this.preferCity; } @@ -130,7 +140,9 @@ public void setStateFilter(ArrayList stateFilter) { this.stateFilter = stateFilter; } - public void setZipcodeFilter(ArrayList zipcodeFilter) { this.zipcodeFilter = zipcodeFilter ;} + public void setZipcodeFilter(ArrayList zipcodeFilter) { this.zipcodeFilter = zipcodeFilter; } + + public void setExcludeStates(ArrayList excludeStates) { this.excludeStates = excludeStates; } public void setPreferCity(ArrayList cities) { this.preferCity = cities; @@ -145,7 +157,7 @@ public void setPreferCity(ArrayList cities) { * @param preferRatio A decimal value, range [0, 1]. Default is 0.333333333. * @see "https://smartystreets.com/docs/cloud/us-autocomplete-api#preference" */ - public void setPreferRatio(double preferRatio) { + public void setPreferRatio(int preferRatio) { this.preferRatio = preferRatio; } diff --git a/src/main/java/examples/UsAutocompleteProExample.java b/src/main/java/examples/UsAutocompleteProExample.java index 0dfc317..7b692dd 100644 --- a/src/main/java/examples/UsAutocompleteProExample.java +++ b/src/main/java/examples/UsAutocompleteProExample.java @@ -1,6 +1,6 @@ package examples; -import com.smartystreets.api.StaticCredentials; +import com.smartystreets.api.SharedCredentials; import com.smartystreets.api.exceptions.SmartyException; import com.smartystreets.api.us_autocomplete_pro.*; import com.smartystreets.api.ClientBuilder; @@ -10,7 +10,7 @@ public class UsAutocompleteProExample { public static void main(String[] args) throws IOException, SmartyException { // We recommend storing your secret keys in environment variables. - StaticCredentials credentials = new StaticCredentials(System.getenv("SMARTY_AUTH_ID"), System.getenv("SMARTY_AUTH_TOKEN")); + SharedCredentials credentials = new SharedCredentials(System.getenv("SMARTY_AUTH_WEB"), System.getenv("SMARTY_AUTH_REFERER")); Client client = new ClientBuilder(credentials).buildUsAutocompleteProApiClient(); Lookup lookup = new Lookup("1 King St Apt"); @@ -18,38 +18,37 @@ public static void main(String[] args) throws IOException, SmartyException { System.out.println("*** Result with no filter ***"); System.out.println(); - for (Suggestion suggestion : lookup.getResult()) { - System.out.println(suggestion.getStreetLine()); - System.out.println(suggestion.getSecondary()); - System.out.println(suggestion.getCity()); - System.out.println(", "); - System.out.println(suggestion.getState()); - System.out.println(", "); - System.out.println(suggestion.getZipcode()); - } + printResult(lookup); // Documentation for input fields can be found at: - // https://smartystreets.com/docs/cloud/us-autocomplete-api#http-request-input-fields + // https://smartystreets.com/docs/cloud/us-autocomplete-api#pro-http-request-url lookup.addStateFilter("MA"); - lookup.addCityFilter("Dorchester"); - lookup.addCityFilter("Boston"); - lookup.addPreferCity("Dorchester"); + lookup.addCityFilter("Dorchester, MA"); + lookup.addCityFilter("Boston, MA"); + lookup.addPreferState("MN"); + lookup.addPreferCity("Dorchester,MA"); lookup.setMaxSuggestions(5); - lookup.setPreferRatio(0.33333); + lookup.setPreferRatio(33); - Suggestion[] suggestions = client.send(lookup); // The client will also return the suggestions directly + client.send(lookup); // The client will also return the suggestions directly System.out.println(); System.out.println("*** Result with some filters ***"); - for (Suggestion suggestion : suggestions) { - System.out.println(suggestion.getStreetLine()); - System.out.println(suggestion.getSecondary()); - System.out.println(suggestion.getCity()); - System.out.println(", "); - System.out.println(suggestion.getState()); - System.out.println(", "); + printResult(lookup); + } + + private static void printResult(Lookup lookup) { + for (Suggestion suggestion : lookup.getResult()) { + System.out.print(suggestion.getStreetLine()); + System.out.print(" "); + System.out.print(suggestion.getSecondary()); + System.out.print(" "); + System.out.print(suggestion.getCity()); + System.out.print(", "); + System.out.print(suggestion.getState()); + System.out.print(", "); System.out.println(suggestion.getZipcode()); } } -} +} \ No newline at end of file diff --git a/src/test/java/com/smartystreets/api/us_autocomplete/ClientTest.java b/src/test/java/com/smartystreets/api/us_autocomplete/ClientTest.java index b76980a..77d8393 100644 --- a/src/test/java/com/smartystreets/api/us_autocomplete/ClientTest.java +++ b/src/test/java/com/smartystreets/api/us_autocomplete/ClientTest.java @@ -1,6 +1,6 @@ package com.smartystreets.api.us_autocomplete; - +import com.smartystreets.api.GeolocateType; import com.smartystreets.api.Response; import com.smartystreets.api.URLPrefixSender; import com.smartystreets.api.mocks.FakeDeserializer; diff --git a/src/test/java/com/smartystreets/api/us_autocomplete_pro/ClientTest.java b/src/test/java/com/smartystreets/api/us_autocomplete_pro/ClientTest.java new file mode 100644 index 0000000..b41f784 --- /dev/null +++ b/src/test/java/com/smartystreets/api/us_autocomplete_pro/ClientTest.java @@ -0,0 +1,83 @@ +package com.smartystreets.api.us_autocomplete_pro; + +import com.smartystreets.api.GeolocateType; +import com.smartystreets.api.Response; +import com.smartystreets.api.URLPrefixSender; +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&prefer_geolocation=city", 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/?search=1&max_results=2&include_only_cities=3&include_only_states=4&prefer_ratio=60.0&prefer_geolocation=state"; + Lookup lookup = new Lookup(); + lookup.setSearch("1"); + lookup.setMaxSuggestions(2); + lookup.addCityFilter("3"); + lookup.addStateFilter("4"); + lookup.setPreferRatio(60); + lookup.setGeolocateType(GeolocateType.STATE); + + 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.getSuggestions(), lookup.getResult()); + } + + //endregion +} diff --git a/src/test/java/com/smartystreets/api/us_autocomplete_pro/SuggestionTest.java b/src/test/java/com/smartystreets/api/us_autocomplete_pro/SuggestionTest.java new file mode 100644 index 0000000..1e25f7d --- /dev/null +++ b/src/test/java/com/smartystreets/api/us_autocomplete_pro/SuggestionTest.java @@ -0,0 +1,24 @@ +package com.smartystreets.api.us_autocomplete_pro; + + +import com.smartystreets.api.GoogleSerializer; +import org.junit.Test; + +import java.io.IOException; + +import static org.junit.Assert.*; + +public class SuggestionTest { + private final GoogleSerializer googleSerializer = new GoogleSerializer(); + private static final String responsePayload = "{\"suggestions\":[{\"street_line\":\"2\",\"city\":\"3\",\"state\":\"4\"}]}"; + + @Test + public void testAllFieldGetFilledInCorrectly() throws IOException { + Result result = googleSerializer.deserialize(responsePayload.getBytes(), Result.class); + + assertNotNull(result.getSuggestions()[0]); + assertEquals("2", result.getSuggestion(0).getStreetLine()); + assertEquals("3", result.getSuggestion(0).getCity()); + assertEquals("4", result.getSuggestion(0).getState()); + } +}