Skip to content

Commit 791a6b4

Browse files
authored
[fix] Invalid Fields structure (#312)
- Fix invalid schema for carrier account field/fields - Add missing logo property for CarrierAccount
1 parent 1cc10ab commit 791a6b4

File tree

6 files changed

+72
-10
lines changed

6 files changed

+72
-10
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## Next Release
4+
5+
- Fix `Fields` serialization bug causing carrier account operations to fail
6+
37
## v7.2.0 (2024-04-10)
48

59
- Adds `refund` function in Insurance service for requesting a refund for a standalone insurance

src/main/java/com/easypost/model/CarrierAccount.java

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public final class CarrierAccount extends EasyPostResource {
88
private String type;
99
private Fields fields;
1010
private boolean clone;
11+
private String logo;
1112
private String readable;
1213
private String description;
1314
private String reference;

src/main/java/com/easypost/model/Field.java

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
@Getter
66
public class Field extends EasyPostResource {
7-
private String key;
87
private String visibility;
98
private String label;
109
private String value;

src/main/java/com/easypost/model/Fields.java

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
import lombok.Getter;
44

5+
import java.util.Map;
6+
57
@Getter
68
public class Fields extends EasyPostResource {
9+
private Map<String, Field> credentials;
10+
private Map<String, Field> testCredentials;
11+
private boolean autoLink;
12+
private boolean customWorkflow;
713
}

src/test/java/com/easypost/BillingTest.java

-9
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@ public final class BillingTest {
2525
"ll,\"last4\":\"4444\",\"exp_month\":1,\"exp_year\":2025,\"brand\":\"Mastercard\"}}";
2626
private PaymentMethod paymentMethod = Constants.Http.GSON.fromJson(jsonResponse, PaymentMethod.class);
2727

28-
private String jsonResponseLegacyPrefixes = "{\"id\":\"cust_...\",\"object\":\"PaymentMethods\",\"primary_" +
29-
"payment_method\":{\"id\":\"card_...\",\"disabled_at\":null,\"object\":null,\"na" +
30-
"me\":null,\"last4\":\"4242\",\"exp_month\":1,\"exp_year\":2025,\"brand\":\"Visa\"},\"secondar" +
31-
"y_payment_method\":{\"id\":\"bank_...\",\"disabled_at\":null,\"object\":null,\"name\":nu" +
32-
"ll,\"last4\":\"4444\",\"exp_month\":1,\"exp_year\":2025,\"brand\":\"Mastercard\"}}";
33-
34-
private PaymentMethod paymentMethodLegacyPrefixes =
35-
Constants.Http.GSON.fromJson(jsonResponseLegacyPrefixes, PaymentMethod.class);
36-
3728
private static MockedStatic<Requestor> requestMock = Mockito.mockStatic(Requestor.class);
3829

3930
/**

src/test/java/com/easypost/CarrierAccountTest.java

+61
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
import com.easypost.exception.API.InvalidRequestError;
44
import com.easypost.exception.EasyPostException;
5+
import com.easypost.http.Requestor;
56
import com.easypost.model.CarrierAccount;
67
import com.easypost.model.CarrierType;
8+
import com.easypost.model.Pickup;
9+
import com.easypost.model.Shipment;
710
import com.google.common.collect.ImmutableMap;
811
import org.junit.jupiter.api.AfterEach;
912
import org.junit.jupiter.api.BeforeAll;
1013
import org.junit.jupiter.api.Test;
14+
import org.mockito.MockedStatic;
15+
import org.mockito.Mockito;
1116

1217
import java.util.HashMap;
1318
import java.util.List;
@@ -23,6 +28,8 @@ public final class CarrierAccountTest {
2328

2429
private static TestUtils.VCR vcr;
2530

31+
private static MockedStatic<Requestor> requestMock = Mockito.mockStatic(Requestor.class);
32+
2633
/**
2734
* Set up the testing environment for this file.
2835
*
@@ -179,4 +186,58 @@ public void testTypes() throws EasyPostException {
179186
assertInstanceOf(List.class, types);
180187
assertTrue(types.stream().allMatch(type -> type != null));
181188
}
189+
190+
/**
191+
* Test that the CarrierAccount fields are correctly deserialized from the API response.
192+
* None of the demo carrier accounts used in the above tests have credentials or test credentials fields,
193+
* so we need to use some mock data.
194+
*/
195+
@Test
196+
public void testCarrierFieldsJsonDeserialization() {
197+
String carrierAccountJson = "[{\"id\":\"ca_123\",\"object\":\"CarrierAccount\"," +
198+
"\"fields\":{\"credentials\":{\"account_number\":{\"visibility\":\"visible\"," +
199+
"\"label\":\"DHL Account Number\",\"value\":\"123456\"},\"country\":{\"visibility\":\"visible\"," +
200+
"\"label\":\"Account Country Code (2 Letter)\",\"value\":\"US\"},\"site_id\":{\"visibility\":" +
201+
"\"visible\",\"label\":\"Site ID (Optional)\",\"value\": null },\"password\":{\"visibility\":" +
202+
"\"password\",\"label\":\"Password (Optional)\",\"value\":\"\"},\"is_reseller\":{\"visibility\":" +
203+
"\"checkbox\",\"label\":\"Reseller Account? (check if yes)\",\"value\":null}}}}]";
204+
CarrierAccount[] carrierAccounts = Constants.Http.GSON.fromJson(carrierAccountJson, CarrierAccount[].class);
205+
206+
CarrierAccount carrierAccount = carrierAccounts[0];
207+
assertEquals("ca_123", carrierAccount.getId());
208+
assertEquals("CarrierAccount", carrierAccount.getObject());
209+
assertEquals("DHL Account Number",
210+
carrierAccount.getFields().getCredentials().get("account_number").getLabel());
211+
}
212+
213+
/**
214+
* Test that the CarrierAccount fields are correctly serialized to the API request.
215+
*/
216+
@Test
217+
public void testCarrierFieldsJsonSerialization() {
218+
String carrierAccountJson = "[{\"id\":\"ca_123\",\"object\":\"CarrierAccount\",\"fields\":{\"credentials\":" +
219+
"{\"account_number\":{\"visibility\":\"visible\",\"label\":\"DHL Account Number\"," +
220+
"\"value\":\"123456\"},\"country\":{\"visibility\":\"visible\",\"label\":" +
221+
"\"Account Country Code (2 Letter)\",\"value\":\"US\"},\"site_id\":{\"visibility\":\"visible\"," +
222+
"\"label\":\"Site ID (Optional)\",\"value\": null },\"password\":{\"visibility\":\"password\"," +
223+
"\"label\":\"Password (Optional)\",\"value\":\"\"},\"is_reseller\":{\"visibility\":\"checkbox\"," +
224+
"\"label\":\"Reseller Account? (check if yes)\",\"value\":null}}}}]";
225+
CarrierAccount[] carrierAccounts = Constants.Http.GSON.fromJson(carrierAccountJson, CarrierAccount[].class);
226+
CarrierAccount carrierAccount = carrierAccounts[0];
227+
228+
// Prepare a parameter set for creating a pickup, using the carrier account object
229+
Map<String, Object> pickupData = Fixtures.basicPickup();
230+
pickupData.put("shipment", new Shipment());
231+
pickupData.put("carrier_accounts", new CarrierAccount[] { carrierAccount });
232+
233+
// Avoid making a real request to the API, interested in pre-request serialization, not interested in response
234+
requestMock.when(() -> Requestor.request(Requestor.RequestMethod.POST, "pickups", pickupData, Shipment.class,
235+
vcr.client)).thenReturn(new Pickup());
236+
237+
// This will throw an exception if the carrier account fields could not be serialized properly
238+
assertDoesNotThrow(() -> vcr.client.pickup.create(pickupData));
239+
240+
// Close mock
241+
requestMock.close();
242+
}
182243
}

0 commit comments

Comments
 (0)