From d64844242c1042b87b3d58d26abe23274fb7125a Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Fri, 11 Apr 2025 14:53:34 -0600 Subject: [PATCH 1/5] feat: route Amazon create request --- CHANGELOG.md | 1 + src/main/java/com/easypost/Constants.java | 16 +++--- .../service/CarrierAccountService.java | 18 ++++--- .../java/com/easypost/CarrierAccountTest.java | 49 ++++++++++++++----- 4 files changed, 60 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ba4ded72..340f480a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - `referralCustomer.addCreditCardFromStripe` - `referralCustomer.addBankAccountFromStripe` - Adds `tracking_codes` param to tracker index endpoint +- Routes `AmazonShippingAccount` to the correct endpoint - Fixes error parsing - Allows for alternative format of `errors` field (previously we deserialized the `errors` field into a list of `Error` objects; however, sometimes the errors are simply a list of strings. This change make the `errors` field a list of `Object` allowing for either the new `FieldError` object or a list of strings. Users will need to check for the type of error returned and handle appropriately) - Removed the unused `Error` model diff --git a/src/main/java/com/easypost/Constants.java b/src/main/java/com/easypost/Constants.java index c36f743b5..53a6c68f5 100644 --- a/src/main/java/com/easypost/Constants.java +++ b/src/main/java/com/easypost/Constants.java @@ -61,13 +61,17 @@ public abstract static class ErrorCodes { } public abstract static class CarrierAccountTypes { - public static final List CARRIER_TYPES_WITH_CUSTOM_WORKFLOW = ImmutableList.of("FedexAccount", - "FedexSmartpostAccount"); - } + public static final List CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOW = ImmutableList.of( + "FedexAccount", "FedexSmartpostAccount" + ); + + public static final List UPS_OAUTH_CARRIER_ACCOUNT_TYPES = ImmutableList.of( + "UpsAccount", "UpsMailInnovationsAccount", "UpsSurepostAccount" + ); - public abstract static class UpsAccountTypes { - public static final List UPS_OAUTH_CARRIER_ACCOUNT_TYPES = ImmutableList.of("UpsAccount", - "UpsMailInnovationsAccount", "UpsSurepostAccount"); + public static final List CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH = ImmutableList.of( + "AmazonShippingAccount" + ); } public abstract static class Http { diff --git a/src/main/java/com/easypost/service/CarrierAccountService.java b/src/main/java/com/easypost/service/CarrierAccountService.java index b40e8ea6a..2d2f008d7 100644 --- a/src/main/java/com/easypost/service/CarrierAccountService.java +++ b/src/main/java/com/easypost/service/CarrierAccountService.java @@ -93,7 +93,7 @@ public CarrierAccount update(String id, final Map params) throws Map wrappedParams = new HashMap(); wrappedParams.put(selectTopLayerKey(type), params); - String endpoint = (Constants.UpsAccountTypes.UPS_OAUTH_CARRIER_ACCOUNT_TYPES.contains(type) + String endpoint = (Constants.CarrierAccountTypes.UPS_OAUTH_CARRIER_ACCOUNT_TYPES.contains(type) ? "ups_oauth_registrations/" : "carrier_accounts/") + id; @@ -121,10 +121,12 @@ public void delete(String id) throws EasyPostException { * @return The endpoint for the carrier account creation request. */ private static String selectCarrierAccountCreationEndpoint(final String carrierAccountType) { - if (Constants.CarrierAccountTypes.CARRIER_TYPES_WITH_CUSTOM_WORKFLOW.contains(carrierAccountType)) { + if (Constants.CarrierAccountTypes.CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_WORKFLOW.contains(carrierAccountType)) { return "carrier_accounts/register"; - } else if (Constants.UpsAccountTypes.UPS_OAUTH_CARRIER_ACCOUNT_TYPES.contains(carrierAccountType)) { + } else if (Constants.CarrierAccountTypes.UPS_OAUTH_CARRIER_ACCOUNT_TYPES.contains(carrierAccountType)) { return "ups_oauth_registrations"; + } else if (Constants.CarrierAccountTypes.CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH.contains(carrierAccountType)) { + return "carrier_accounts/register_oauth"; } else { return "carrier_accounts"; } @@ -143,8 +145,12 @@ private static String selectTopLayerKey(final String carrierAccountType) throws String.format(Constants.ErrorMessages.MISSING_REQUIRED_PARAMETER, "carrier account type")); } - return Constants.UpsAccountTypes.UPS_OAUTH_CARRIER_ACCOUNT_TYPES.contains(carrierAccountType) - ? "ups_oauth_registrations" - : "carrier_account"; + if (Constants.CarrierAccountTypes.UPS_OAUTH_CARRIER_ACCOUNT_TYPES.contains(carrierAccountType)) { + return "ups_oauth_registrations"; + } else if (Constants.CarrierAccountTypes.CARRIER_ACCOUNT_TYPES_WITH_CUSTOM_OAUTH.contains(carrierAccountType)) { + return "carrier_account_oauth_registrations"; + } else { + return "carrier_account"; + } } } diff --git a/src/test/java/com/easypost/CarrierAccountTest.java b/src/test/java/com/easypost/CarrierAccountTest.java index 24f3119eb..1c3d712cc 100644 --- a/src/test/java/com/easypost/CarrierAccountTest.java +++ b/src/test/java/com/easypost/CarrierAccountTest.java @@ -43,9 +43,18 @@ private static CarrierAccount createUpsCarrierAccount() throws EasyPostException data.put("type", "UpsAccount"); data.put("account_number", "123456789"); - CarrierAccount upsAccount = vcr.client.carrierAccount.create(data); - testCarrierAccountId = upsAccount.getId(); // trigger deletion after test - return upsAccount; + CarrierAccount carrierAccount = vcr.client.carrierAccount.create(data); + testCarrierAccountId = carrierAccount.getId(); // trigger deletion after test + return carrierAccount; + } + + private static CarrierAccount createAmazonCarrierAccount() throws EasyPostException { + Map data = new HashMap<>(); + data.put("type", "AmazonShippingAccount"); + + CarrierAccount carrierAccount = vcr.client.carrierAccount.create(data); + testCarrierAccountId = carrierAccount.getId(); // trigger deletion after test + return carrierAccount; } /** @@ -126,11 +135,27 @@ public void testCreateWithCustomWorkflow() throws EasyPostException { public void testCreateWithUPS() throws EasyPostException { vcr.setUpTest("create_with_ups"); - CarrierAccount upsAccount = createUpsCarrierAccount(); + CarrierAccount carrierAccount = createUpsCarrierAccount(); - assertInstanceOf(CarrierAccount.class, upsAccount); - assertTrue(upsAccount.getId().startsWith("ca_")); - assertEquals("UpsAccount", upsAccount.getType()); + assertInstanceOf(CarrierAccount.class, carrierAccount); + assertTrue(carrierAccount.getId().startsWith("ca_")); + assertEquals("UpsAccount", carrierAccount.getType()); + } + + /** + * Test creating an Amazon carrier account. + * + * @throws EasyPostException when the request fails. + */ + @Test + public void testCreateWithAmazon() throws EasyPostException { + // vcr.setUpTest("create_with_amazon"); + + CarrierAccount carrierAccount = createAmazonCarrierAccount(); + + assertInstanceOf(CarrierAccount.class, carrierAccount); + assertTrue(carrierAccount.getId().startsWith("ca_")); + assertEquals("AmazonShippingAccount", carrierAccount.getType()); } /** @@ -197,15 +222,15 @@ public void testUpdate() throws EasyPostException { public void testUpdateUpsAccount() throws EasyPostException { vcr.setUpTest("update_ups"); - CarrierAccount upsAccount = createUpsCarrierAccount(); + CarrierAccount carrierAccount = createUpsCarrierAccount(); Map updateParams = new HashMap<>(); updateParams.put("account_number", "987654321"); - CarrierAccount updatedUpsAccount = vcr.client.carrierAccount.update(upsAccount.getId(), updateParams); + CarrierAccount updatedCarrierAccount = vcr.client.carrierAccount.update(carrierAccount.getId(), updateParams); - assertInstanceOf(CarrierAccount.class, updatedUpsAccount); - assertTrue(updatedUpsAccount.getId().startsWith("ca_")); - assertEquals("UpsAccount", updatedUpsAccount.getType()); + assertInstanceOf(CarrierAccount.class, updatedCarrierAccount); + assertTrue(updatedCarrierAccount.getId().startsWith("ca_")); + assertEquals("UpsAccount", updatedCarrierAccount.getType()); } /** From 43f46737d05178b5f5fc9f81eca5854dfa7988af Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Fri, 11 Apr 2025 15:20:30 -0600 Subject: [PATCH 2/5] fix: coverage --- Makefile | 2 +- pom.xml | 3 ++- src/test/java/com/easypost/CarrierAccountTest.java | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index aea2299e3..6656afa92 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ clean: ## coverage - Test (and build) the project to generate a coverage report coverage: - mvn verify -Dgpg.skip=true -Dcheckstyle.skip=true -Ddependency-check.skip=true -Djavadoc.skip=true jacoco:report + mvn test -Dgpg.skip=true -Dcheckstyle.skip=true -Ddependency-check.skip=true -Djavadoc.skip=true jacoco:report ## checkstyle - Check if project follows CheckStyle rules (must run install-checkstyle first) checkstyle: diff --git a/pom.xml b/pom.xml index 6ad2f5ab2..64ac5f912 100644 --- a/pom.xml +++ b/pom.xml @@ -125,12 +125,13 @@ junit.jupiter.execution.order.random.seed=99 + -javaagent:${settings.localRepository}/net/bytebuddy/byte-buddy-agent/1.12.19/byte-buddy-agent-1.12.19.jar org.jacoco jacoco-maven-plugin - 0.8.12 + 0.8.13 diff --git a/src/test/java/com/easypost/CarrierAccountTest.java b/src/test/java/com/easypost/CarrierAccountTest.java index 1c3d712cc..7eaec7a43 100644 --- a/src/test/java/com/easypost/CarrierAccountTest.java +++ b/src/test/java/com/easypost/CarrierAccountTest.java @@ -149,7 +149,7 @@ public void testCreateWithUPS() throws EasyPostException { */ @Test public void testCreateWithAmazon() throws EasyPostException { - // vcr.setUpTest("create_with_amazon"); + vcr.setUpTest("create_with_amazon"); CarrierAccount carrierAccount = createAmazonCarrierAccount(); From 9472276274181ab4fb53359e3e56f1d89dd391db Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Mon, 14 Apr 2025 14:20:47 -0600 Subject: [PATCH 3/5] chore: cleanup --- .../easypost/service/CarrierAccountService.java | 3 ++- .../java/com/easypost/CarrierAccountTest.java | 16 +++++----------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/easypost/service/CarrierAccountService.java b/src/main/java/com/easypost/service/CarrierAccountService.java index 2d2f008d7..34f2a21f7 100644 --- a/src/main/java/com/easypost/service/CarrierAccountService.java +++ b/src/main/java/com/easypost/service/CarrierAccountService.java @@ -138,8 +138,9 @@ private static String selectCarrierAccountCreationEndpoint(final String carrierA * * @param carrierAccountType The type of carrier account to create. * @return The top-layer key for the carrier account creation/update request. + * @throws MissingParameterError when the request fails. */ - private static String selectTopLayerKey(final String carrierAccountType) throws EasyPostException { + private static String selectTopLayerKey(final String carrierAccountType) throws MissingParameterError { if (carrierAccountType == null) { throw new MissingParameterError( String.format(Constants.ErrorMessages.MISSING_REQUIRED_PARAMETER, "carrier account type")); diff --git a/src/test/java/com/easypost/CarrierAccountTest.java b/src/test/java/com/easypost/CarrierAccountTest.java index 7eaec7a43..98f7e5b6c 100644 --- a/src/test/java/com/easypost/CarrierAccountTest.java +++ b/src/test/java/com/easypost/CarrierAccountTest.java @@ -32,7 +32,6 @@ public final class CarrierAccountTest { private static MockedStatic requestMock = Mockito.mockStatic(Requestor.class); private static CarrierAccount createBasicCarrierAccount() throws EasyPostException { - // This method creates DhlEcsAccount carrier account. CarrierAccount carrierAccount = vcr.client.carrierAccount.create(Fixtures.basicCarrierAccount()); testCarrierAccountId = carrierAccount.getId(); // trigger deletion after test return carrierAccount; @@ -48,15 +47,6 @@ private static CarrierAccount createUpsCarrierAccount() throws EasyPostException return carrierAccount; } - private static CarrierAccount createAmazonCarrierAccount() throws EasyPostException { - Map data = new HashMap<>(); - data.put("type", "AmazonShippingAccount"); - - CarrierAccount carrierAccount = vcr.client.carrierAccount.create(data); - testCarrierAccountId = carrierAccount.getId(); // trigger deletion after test - return carrierAccount; - } - /** * Set up the testing environment for this file. * @@ -151,7 +141,11 @@ public void testCreateWithUPS() throws EasyPostException { public void testCreateWithAmazon() throws EasyPostException { vcr.setUpTest("create_with_amazon"); - CarrierAccount carrierAccount = createAmazonCarrierAccount(); + Map data = new HashMap<>(); + data.put("type", "AmazonShippingAccount"); + + CarrierAccount carrierAccount = vcr.client.carrierAccount.create(data); + testCarrierAccountId = carrierAccount.getId(); // trigger deletion after test assertInstanceOf(CarrierAccount.class, carrierAccount); assertTrue(carrierAccount.getId().startsWith("ca_")); From d00cc77b902acafbe2527c497794a73626d05c21 Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Mon, 14 Apr 2025 15:28:03 -0600 Subject: [PATCH 4/5] fix: mocking in unrelated tests --- .../carrier_account/create_with_amazon.json | 263 ++++++++++++++++++ .../java/com/easypost/CarrierAccountTest.java | 4 +- 2 files changed, 265 insertions(+), 2 deletions(-) create mode 100644 src/test/cassettes/carrier_account/create_with_amazon.json diff --git a/src/test/cassettes/carrier_account/create_with_amazon.json b/src/test/cassettes/carrier_account/create_with_amazon.json new file mode 100644 index 000000000..070044b8d --- /dev/null +++ b/src/test/cassettes/carrier_account/create_with_amazon.json @@ -0,0 +1,263 @@ +[ + { + "recordedAt": 1744665939, + "request": { + "body": "{\n \"carrier_account_oauth_registrations\": {\n \"type\": \"AmazonShippingAccount\"\n }\n}", + "method": "POST", + "headers": { + "Accept-Charset": [ + "UTF-8" + ], + "User-Agent": [ + "REDACTED" + ], + "Content-Type": [ + "application/json" + ] + }, + "uri": "https://api.easypost.com/v2/carrier_accounts/register_oauth" + }, + "response": { + "body": "{\n \"readable\": \"Amazon Shipping\",\n \"credentials\": {},\n \"created_at\": \"2025-04-14T21:25:39Z\",\n \"description\": null,\n \"type\": \"AmazonShippingAccount\",\n \"reference\": null,\n \"updated_at\": \"2025-04-14T21:25:39Z\",\n \"clone\": false,\n \"billing_type\": \"carrier\",\n \"logo\": null,\n \"id\": \"ca_490363c69ed34e26a8b8e74a5238170a\",\n \"fields\": {\n \"credentials\": {}\n },\n \"object\": \"CarrierAccount\"\n}", + "httpVersion": null, + "headers": { + "null": [ + "HTTP/1.1 201 Created" + ], + "content-length": [ + "1432" + ], + "expires": [ + "0" + ], + "x-node": [ + "bigweb54nuq" + ], + "x-frame-options": [ + "SAMEORIGIN" + ], + "x-download-options": [ + "noopen" + ], + "x-permitted-cross-domain-policies": [ + "none" + ], + "x-backend": [ + "easypost" + ], + "pragma": [ + "no-cache" + ], + "strict-transport-security": [ + "max-age\u003d31536000; includeSubDomains; preload" + ], + "x-xss-protection": [ + "1; mode\u003dblock" + ], + "x-content-type-options": [ + "nosniff" + ], + "x-ep-request-uuid": [ + "1a43adb967fd7d53e2bc3d1c003c98d6" + ], + "x-proxied": [ + "intlb4nuq 284c5d344a", + "extlb1nuq 99aac35317" + ], + "referrer-policy": [ + "strict-origin-when-cross-origin" + ], + "x-runtime": [ + "0.074536" + ], + "content-type": [ + "application/json; charset\u003dutf-8" + ], + "x-version-label": [ + "easypost-202504111751-33c61fb414-master" + ], + "cache-control": [ + "private, no-cache, no-store" + ] + }, + "status": { + "code": 201, + "message": "Created" + }, + "uri": "https://api.easypost.com/v2/carrier_accounts/register_oauth" + }, + "duration": 185 + }, + { + "recordedAt": 1744665939, + "request": { + "body": "", + "method": "GET", + "headers": { + "Accept-Charset": [ + "UTF-8" + ], + "User-Agent": [ + "REDACTED" + ] + }, + "uri": "https://api.easypost.com/v2/carrier_accounts/ca_490363c69ed34e26a8b8e74a5238170a" + }, + "response": { + "body": "{\n \"readable\": \"Amazon Shipping\",\n \"credentials\": {},\n \"created_at\": \"2025-04-14T21:25:39Z\",\n \"description\": null,\n \"type\": \"AmazonShippingAccount\",\n \"reference\": null,\n \"updated_at\": \"2025-04-14T21:25:39Z\",\n \"clone\": false,\n \"billing_type\": \"carrier\",\n \"logo\": null,\n \"id\": \"ca_490363c69ed34e26a8b8e74a5238170a\",\n \"fields\": {\n \"credentials\": {}\n },\n \"object\": \"CarrierAccount\"\n}", + "httpVersion": null, + "headers": { + "null": [ + "HTTP/1.1 200 OK" + ], + "content-length": [ + "1432" + ], + "expires": [ + "0" + ], + "x-node": [ + "bigweb34nuq" + ], + "x-frame-options": [ + "SAMEORIGIN" + ], + "x-download-options": [ + "noopen" + ], + "x-permitted-cross-domain-policies": [ + "none" + ], + "x-backend": [ + "easypost" + ], + "pragma": [ + "no-cache" + ], + "strict-transport-security": [ + "max-age\u003d31536000; includeSubDomains; preload" + ], + "x-xss-protection": [ + "1; mode\u003dblock" + ], + "x-content-type-options": [ + "nosniff" + ], + "x-ep-request-uuid": [ + "1a43adb967fd7d53e2bc3d1d003c991e" + ], + "x-proxied": [ + "intlb4nuq 284c5d344a", + "extlb1nuq 99aac35317" + ], + "referrer-policy": [ + "strict-origin-when-cross-origin" + ], + "x-runtime": [ + "0.032996" + ], + "content-type": [ + "application/json; charset\u003dutf-8" + ], + "x-version-label": [ + "easypost-202504111751-33c61fb414-master" + ], + "cache-control": [ + "private, no-cache, no-store" + ] + }, + "status": { + "code": 200, + "message": "OK" + }, + "uri": "https://api.easypost.com/v2/carrier_accounts/ca_490363c69ed34e26a8b8e74a5238170a" + }, + "duration": 140 + }, + { + "recordedAt": 1744665939, + "request": { + "body": "", + "method": "DELETE", + "headers": { + "Accept-Charset": [ + "UTF-8" + ], + "User-Agent": [ + "REDACTED" + ] + }, + "uri": "https://api.easypost.com/v2/carrier_accounts/ca_490363c69ed34e26a8b8e74a5238170a" + }, + "response": { + "body": "{}", + "httpVersion": null, + "headers": { + "null": [ + "HTTP/1.1 200 OK" + ], + "content-length": [ + "2" + ], + "expires": [ + "0" + ], + "x-node": [ + "bigweb34nuq" + ], + "x-frame-options": [ + "SAMEORIGIN" + ], + "x-download-options": [ + "noopen" + ], + "x-permitted-cross-domain-policies": [ + "none" + ], + "x-backend": [ + "easypost" + ], + "pragma": [ + "no-cache" + ], + "strict-transport-security": [ + "max-age\u003d31536000; includeSubDomains; preload" + ], + "x-xss-protection": [ + "1; mode\u003dblock" + ], + "x-content-type-options": [ + "nosniff" + ], + "x-ep-request-uuid": [ + "1a43adb967fd7d53e2bc3d35003c994c" + ], + "x-proxied": [ + "intlb4nuq 284c5d344a", + "extlb1nuq 99aac35317" + ], + "referrer-policy": [ + "strict-origin-when-cross-origin" + ], + "x-runtime": [ + "0.052300" + ], + "content-type": [ + "application/json; charset\u003dutf-8" + ], + "x-version-label": [ + "easypost-202504111751-33c61fb414-master" + ], + "cache-control": [ + "private, no-cache, no-store" + ] + }, + "status": { + "code": 200, + "message": "OK" + }, + "uri": "https://api.easypost.com/v2/carrier_accounts/ca_490363c69ed34e26a8b8e74a5238170a" + }, + "duration": 164 + } +] \ No newline at end of file diff --git a/src/test/java/com/easypost/CarrierAccountTest.java b/src/test/java/com/easypost/CarrierAccountTest.java index 98f7e5b6c..18b672912 100644 --- a/src/test/java/com/easypost/CarrierAccountTest.java +++ b/src/test/java/com/easypost/CarrierAccountTest.java @@ -29,8 +29,6 @@ public final class CarrierAccountTest { private static TestUtils.VCR vcr; - private static MockedStatic requestMock = Mockito.mockStatic(Requestor.class); - private static CarrierAccount createBasicCarrierAccount() throws EasyPostException { CarrierAccount carrierAccount = vcr.client.carrierAccount.create(Fixtures.basicCarrierAccount()); testCarrierAccountId = carrierAccount.getId(); // trigger deletion after test @@ -284,6 +282,8 @@ public void testCarrierFieldsJsonDeserialization() { */ @Test public void testCarrierFieldsJsonSerialization() { + MockedStatic requestMock = Mockito.mockStatic(Requestor.class); + String carrierAccountJson = "[{\"id\":\"ca_123\",\"object\":\"CarrierAccount\",\"fields\":{\"credentials\":" + "{\"account_number\":{\"visibility\":\"visible\",\"label\":\"DHL Account Number\"," + "\"value\":\"123456\"},\"country\":{\"visibility\":\"visible\",\"label\":" + From ed70e8dc23071567757a28516852a416b62b025c Mon Sep 17 00:00:00 2001 From: Justintime50 <39606064+Justintime50@users.noreply.github.com> Date: Mon, 14 Apr 2025 15:29:30 -0600 Subject: [PATCH 5/5] chore: put back pom --- pom.xml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 64ac5f912..6ad2f5ab2 100644 --- a/pom.xml +++ b/pom.xml @@ -125,13 +125,12 @@ junit.jupiter.execution.order.random.seed=99 - -javaagent:${settings.localRepository}/net/bytebuddy/byte-buddy-agent/1.12.19/byte-buddy-agent-1.12.19.jar org.jacoco jacoco-maven-plugin - 0.8.13 + 0.8.12