Skip to content

Commit

Permalink
Examples working
Browse files Browse the repository at this point in the history
  • Loading branch information
DuncanBeutler committed Nov 19, 2019
1 parent 254134b commit e0b2823
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 46 deletions.
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<source>7</source>
</configuration>
<version>2.9.1</version>
<executions>
<execution>
Expand Down
4 changes: 2 additions & 2 deletions smartystreets-java-sdk.iml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: com.google.http-client:google-http-client:1.21.0" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:1.3.9" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.0.1" level="project" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
26 changes: 12 additions & 14 deletions src/main/java/com/smartystreets/api/us_autocomplete_pro/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<String> list) {
private String buildString(ArrayList<String> list) {
return buildStringFromList(list, ";");
}

private String buildFilterString(ArrayList<String> list) {
return buildStringFromList(list, ",");
}

private String buildStringFromList(ArrayList<String> list, String separator) {
if (list.isEmpty())
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand All @@ -21,10 +21,11 @@ public class Lookup {
private ArrayList<String> cityFilter;
private ArrayList<String> stateFilter;
private ArrayList<String> zipcodeFilter;
private ArrayList<String> excludeStates;
private ArrayList<String> preferCity;
private ArrayList<String> preferState;
private ArrayList<String> preferZipcode;
private double preferRatio;
private int preferRatio;
private GeolocateType preferGeolocation;

//endregion
Expand All @@ -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<>();
Expand Down Expand Up @@ -78,6 +80,14 @@ public ArrayList<String> getStateFilter() {
return this.stateFilter;
}

public ArrayList<String> getZipcodeFilter() {
return this.zipcodeFilter;
}

public ArrayList<String> getExcludeStates() {
return this.excludeStates;
}

public ArrayList<String> getPreferCity() {
return this.preferCity;
}
Expand Down Expand Up @@ -130,7 +140,9 @@ public void setStateFilter(ArrayList<String> stateFilter) {
this.stateFilter = stateFilter;
}

public void setZipcodeFilter(ArrayList<String> zipcodeFilter) { this.zipcodeFilter = zipcodeFilter ;}
public void setZipcodeFilter(ArrayList<String> zipcodeFilter) { this.zipcodeFilter = zipcodeFilter; }

public void setExcludeStates(ArrayList<String> excludeStates) { this.excludeStates = excludeStates; }

public void setPreferCity(ArrayList<String> cities) {
this.preferCity = cities;
Expand All @@ -145,7 +157,7 @@ public void setPreferCity(ArrayList<String> 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;
}

Expand Down
49 changes: 24 additions & 25 deletions src/main/java/examples/UsAutocompleteProExample.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,46 +10,45 @@
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");

client.send(lookup);

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());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit e0b2823

Please sign in to comment.