Skip to content

Commit

Permalink
Remove unused fields, update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
alrwarner committed Oct 23, 2023
1 parent b278ff2 commit eb559ec
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]
Expand All @@ -34,8 +31,6 @@ public class Lookup {
*/
public Lookup() {
this.maxResults = MAX_RESULTS_DEFAULT;
this.distance = DISTANCE_DEFAULT;
this.geolocation = InternationalGeolocateType.NONE;
}

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -91,15 +77,6 @@ public String getPostalCode() {
return this.postalCode;
}

public Float getLatitude() {
return latitude;
}

public Float getLongitude() {
return longitude;
}


//endregion

//region [ Setters ]
Expand All @@ -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;
}
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

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

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

0 comments on commit eb559ec

Please sign in to comment.