From 3757a88251a890a9a5b52f52f4003ba58cf93e7a Mon Sep 17 00:00:00 2001 From: nwithan8 Date: Mon, 13 Nov 2023 15:39:49 -0700 Subject: [PATCH 1/2] - Add custom deserializer for AddressVerification object - Add unit test to verify AddressVerification deserialization works properly --- src/main/java/com/easypost/Constants.java | 3 + .../easypost/model/AddressVerification.java | 27 +++++++ .../AddressVerificationDeserializer.java | 75 +++++++++++++++++++ src/main/java/com/easypost/model/Error.java | 27 +++++++ src/test/java/com/easypost/AddressTest.java | 6 +- 5 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/easypost/model/AddressVerificationDeserializer.java diff --git a/src/main/java/com/easypost/Constants.java b/src/main/java/com/easypost/Constants.java index a93715bfa..9e1a2af2a 100644 --- a/src/main/java/com/easypost/Constants.java +++ b/src/main/java/com/easypost/Constants.java @@ -1,6 +1,8 @@ package com.easypost; import com.easypost.http.HashMapSerializer; +import com.easypost.model.AddressVerification; +import com.easypost.model.AddressVerificationDeserializer; import com.easypost.model.Error; import com.easypost.model.ErrorDeserializer; import com.easypost.model.SmartrateCollection; @@ -72,6 +74,7 @@ public abstract static class Http { .registerTypeAdapter(HashMap.class, new HashMapSerializer()) .registerTypeAdapter(SmartrateCollection.class, new SmartrateCollectionDeserializer()) .registerTypeAdapter(Error.class, new ErrorDeserializer()) + .registerTypeAdapter(AddressVerification.class, new AddressVerificationDeserializer()) .registerTypeAdapter(StatelessRate[].class, new StatelessRateDeserializer()).create(); public static final Gson PRETTY_PRINT_GSON = new GsonBuilder().setPrettyPrinting().serializeNulls() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create(); diff --git a/src/main/java/com/easypost/model/AddressVerification.java b/src/main/java/com/easypost/model/AddressVerification.java index 0cce1cf66..5e8dc7e3b 100644 --- a/src/main/java/com/easypost/model/AddressVerification.java +++ b/src/main/java/com/easypost/model/AddressVerification.java @@ -8,4 +8,31 @@ public final class AddressVerification { private Boolean success; private List errors; private AddressDetail details; + + /** + * Set the success of this address verification object. + * + * @param success The success message. + */ + void setSuccess(final boolean success) { + this.success = success; + } + + /** + * Set the errors of this address verification object. + * + * @param errors The errors. + */ + void setErrors(final List errors) { + this.errors = errors; + } + + /** + * Set the details of this address verification object. + * + * @param details The details. + */ + void setDetails(final AddressDetail details) { + this.details = details; + } } diff --git a/src/main/java/com/easypost/model/AddressVerificationDeserializer.java b/src/main/java/com/easypost/model/AddressVerificationDeserializer.java new file mode 100644 index 000000000..b2c3e80e8 --- /dev/null +++ b/src/main/java/com/easypost/model/AddressVerificationDeserializer.java @@ -0,0 +1,75 @@ +package com.easypost.model; + +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; + +import java.lang.reflect.Type; +import java.util.ArrayList; + +public final class AddressVerificationDeserializer implements JsonDeserializer { + /** + * Deserialize an AddressVerification from a JSON object. + * + * @param json JSON object to deserialize. + * @param typeOfT Type of the object to deserialize. + * @param context Deserialization context. + * @return Deserialized AddressVerification object. + * @throws JsonParseException if the JSON object is not a valid SmartrateCollection. + */ + @Override + public AddressVerification deserialize(final JsonElement json, final Type typeOfT, + final JsonDeserializationContext context) throws JsonParseException { + JsonObject jo = json.getAsJsonObject(); + + AddressVerification addressVerification = new AddressVerification(); + + boolean success = jo.get("success").getAsBoolean(); + addressVerification.setSuccess(success); + + AddressDetail details = context.deserialize(jo.get("details"), AddressDetail.class); + addressVerification.setDetails(details); + + JsonElement errorsAsJson = jo.get("errors"); + Gson gson = new Gson(); + + if (errorsAsJson != null) { + JsonArray errorsAsArray = errorsAsJson.getAsJsonArray(); + ArrayList errors = new ArrayList<>(); + for (JsonElement errorAsJson : errorsAsArray) { + JsonObject errorAsJsonObject = errorAsJson.getAsJsonObject(); + + Error error = new Error(); + + JsonElement code = errorAsJsonObject.get("code"); + if (code != null) { + error.setCode(code.getAsString()); + } + + JsonElement message = errorAsJsonObject.get("message"); + if (message != null) { + error.setMessage(message.getAsString()); + } + + JsonElement field = errorAsJsonObject.get("field"); + if (field != null) { + error.setField(field.getAsString()); + } + + JsonElement suggestion = errorAsJsonObject.get("suggestion"); + if (suggestion != null && !suggestion.isJsonNull()) { + error.setSuggestion(suggestion.getAsString()); + } + + errors.add(error); + } + addressVerification.setErrors(errors); + } + + return addressVerification; + } +} diff --git a/src/main/java/com/easypost/model/Error.java b/src/main/java/com/easypost/model/Error.java index c6b18a143..e3b2dfc6f 100644 --- a/src/main/java/com/easypost/model/Error.java +++ b/src/main/java/com/easypost/model/Error.java @@ -29,4 +29,31 @@ void setMessage(final String message) { void setCode(final String code) { this.code = code; } + + /** + * Set the errors of this error object. + * + * @param errors The errors. + */ + void setErrors(final List errors) { + this.errors = errors; + } + + /** + * Set the suggestion of this error object. + * + * @param suggestion The suggestion. + */ + void setSuggestion(final String suggestion) { + this.suggestion = suggestion; + } + + /** + * Set the field of this error object. + * + * @param field The field. + */ + void setField(final String field) { + this.field = field; + } } diff --git a/src/test/java/com/easypost/AddressTest.java b/src/test/java/com/easypost/AddressTest.java index 507488d82..6b67822c7 100644 --- a/src/test/java/com/easypost/AddressTest.java +++ b/src/test/java/com/easypost/AddressTest.java @@ -79,6 +79,10 @@ public void testCreateVerify() throws EasyPostException { assertInstanceOf(Address.class, address); assertTrue(address.getId().startsWith("adr_")); assertEquals("417 MONTGOMERY ST FL 5", address.getStreet1()); + + assertNotNull(address.getVerifications()); + assertNotNull(address.getVerifications().getZip4().getErrors()); // Should have a error due to second line + assertNotNull(address.getVerifications().getDelivery().getErrors()); } /** @@ -222,7 +226,7 @@ public void testVerify() throws EasyPostException { Address verifiedAddress = vcr.client.address.verify(address.getId()); - assertInstanceOf(Address.class, address); + assertInstanceOf(Address.class, verifiedAddress); assertTrue(verifiedAddress.getId().startsWith("adr_")); assertEquals("388 TOWNSEND ST APT 20", verifiedAddress.getStreet1()); } From 713dbb7a1a98466c86dbd8ab189105b3528010b9 Mon Sep 17 00:00:00 2001 From: nwithan8 Date: Wed, 15 Nov 2023 15:28:17 -0700 Subject: [PATCH 2/2] - Address feedback --- src/test/java/com/easypost/AddressTest.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/easypost/AddressTest.java b/src/test/java/com/easypost/AddressTest.java index 6b67822c7..bc5ae8458 100644 --- a/src/test/java/com/easypost/AddressTest.java +++ b/src/test/java/com/easypost/AddressTest.java @@ -5,6 +5,7 @@ import com.easypost.exception.General.EndOfPaginationError; import com.easypost.model.Address; import com.easypost.model.AddressCollection; +import com.easypost.model.Error; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -14,6 +15,7 @@ import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -81,8 +83,14 @@ public void testCreateVerify() throws EasyPostException { assertEquals("417 MONTGOMERY ST FL 5", address.getStreet1()); assertNotNull(address.getVerifications()); - assertNotNull(address.getVerifications().getZip4().getErrors()); // Should have a error due to second line - assertNotNull(address.getVerifications().getDelivery().getErrors()); + + assertFalse(address.getVerifications().getDelivery().getErrors().isEmpty()); // should have at least one error + Error error = address.getVerifications().getDelivery().getErrors().get(0); + assertEquals("E.SECONDARY_INFORMATION.INVALID", error.getCode()); + + assertFalse(address.getVerifications().getZip4().getErrors().isEmpty()); // should have at least one error + error = address.getVerifications().getZip4().getErrors().get(0); + assertEquals("E.SECONDARY_INFORMATION.INVALID", error.getCode()); } /**