Skip to content

Commit

Permalink
- Linting
Browse files Browse the repository at this point in the history
  • Loading branch information
nwithan8 committed Apr 12, 2024
1 parent 1c374d8 commit 682f1c6
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/test/java/com/easypost/CarrierAccountTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.easypost.http.Requestor;
import com.easypost.model.CarrierAccount;
import com.easypost.model.CarrierType;
import com.easypost.model.PaymentMethod;
import com.easypost.model.Pickup;
import com.easypost.model.Shipment;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -191,37 +190,52 @@ public void testTypes() throws EasyPostException {

/**
* Test that the CarrierAccount fields are correctly deserialized from the API response.
* None of the demo carrier accounts used in the above tests have credentials or test credentials fields, so we need to use some mock data.
* None of the demo carrier accounts used in the above tests have credentials or test credentials fields,
* so we need to use some mock data.
*/
@Test
public void testCarrierFieldsJsonDeserialization() {
String carrierAccountJson = "[{\"id\":\"ca_123\",\"object\":\"CarrierAccount\",\"fields\":{\"credentials\":{\"account_number\":{\"visibility\":\"visible\",\"label\":\"DHL Account Number\",\"value\":\"123456\"},\"country\":{\"visibility\":\"visible\",\"label\":\"Account Country Code (2 Letter)\",\"value\":\"US\"},\"site_id\":{\"visibility\":\"visible\",\"label\":\"Site ID (Optional)\",\"value\": null },\"password\":{\"visibility\":\"password\",\"label\":\"Password (Optional)\",\"value\":\"\"},\"is_reseller\":{\"visibility\":\"checkbox\",\"label\":\"Reseller Account? (check if yes)\",\"value\":null}}}}]";
String carrierAccountJson = "[{\"id\":\"ca_123\",\"object\":\"CarrierAccount\"," +
"\"fields\":{\"credentials\":{\"account_number\":{\"visibility\":\"visible\"," +
"\"label\":\"DHL Account Number\",\"value\":\"123456\"},\"country\":{\"visibility\":\"visible\"," +
"\"label\":\"Account Country Code (2 Letter)\",\"value\":\"US\"},\"site_id\":{\"visibility\":" +
"\"visible\",\"label\":\"Site ID (Optional)\",\"value\": null },\"password\":{\"visibility\":" +
"\"password\",\"label\":\"Password (Optional)\",\"value\":\"\"},\"is_reseller\":{\"visibility\":" +
"\"checkbox\",\"label\":\"Reseller Account? (check if yes)\",\"value\":null}}}}]";
CarrierAccount[] carrierAccounts = Constants.Http.GSON.fromJson(carrierAccountJson, CarrierAccount[].class);

CarrierAccount carrierAccount = carrierAccounts[0];
assertEquals("ca_123", carrierAccount.getId());
assertEquals("CarrierAccount", carrierAccount.getObject());
assertEquals("DHL Account Number", carrierAccount.getFields().getCredentials().get("account_number").getLabel());
assertEquals("DHL Account Number",
carrierAccount.getFields().getCredentials().get("account_number").getLabel());
}

/**
* Test that the CarrierAccount fields are correctly serialized to the API request.
*/
@Test
public void testCarrierFieldsJsonSerialization() throws EasyPostException {
String carrierAccountJson = "[{\"id\":\"ca_123\",\"object\":\"CarrierAccount\",\"fields\":{\"credentials\":{\"account_number\":{\"visibility\":\"visible\",\"label\":\"DHL Account Number\",\"value\":\"123456\"},\"country\":{\"visibility\":\"visible\",\"label\":\"Account Country Code (2 Letter)\",\"value\":\"US\"},\"site_id\":{\"visibility\":\"visible\",\"label\":\"Site ID (Optional)\",\"value\": null },\"password\":{\"visibility\":\"password\",\"label\":\"Password (Optional)\",\"value\":\"\"},\"is_reseller\":{\"visibility\":\"checkbox\",\"label\":\"Reseller Account? (check if yes)\",\"value\":null}}}}]";
String carrierAccountJson = "[{\"id\":\"ca_123\",\"object\":\"CarrierAccount\",\"fields\":{\"credentials\":" +
"{\"account_number\":{\"visibility\":\"visible\",\"label\":\"DHL Account Number\"," +
"\"value\":\"123456\"},\"country\":{\"visibility\":\"visible\",\"label\":" +
"\"Account Country Code (2 Letter)\",\"value\":\"US\"},\"site_id\":{\"visibility\":\"visible\"," +
"\"label\":\"Site ID (Optional)\",\"value\": null },\"password\":{\"visibility\":\"password\"," +
"\"label\":\"Password (Optional)\",\"value\":\"\"},\"is_reseller\":{\"visibility\":\"checkbox\"," +
"\"label\":\"Reseller Account? (check if yes)\",\"value\":null}}}}]";
CarrierAccount[] carrierAccounts = Constants.Http.GSON.fromJson(carrierAccountJson, CarrierAccount[].class);
CarrierAccount carrierAccount = carrierAccounts[0];

// Prepare a parameter set for creating a pickup, using the carrier account object
Shipment shipment = vcr.client.shipment.create(Fixtures.oneCallBuyShipment());
Map<String, Object> pickupData = Fixtures.basicPickup();
pickupData.put("shipment", shipment);
pickupData.put("carrier_accounts", new CarrierAccount[]{carrierAccount});
pickupData.put("carrier_accounts", new CarrierAccount[] { carrierAccount });

// Avoid making a real request to the API, interested in pre-request serialization, not interested in response
requestMock.when(() -> Requestor.request(
Requestor.RequestMethod.POST, "pickups", pickupData, Shipment.class, vcr.client)).thenReturn(new Pickup());
requestMock.when(() -> Requestor.request(Requestor.RequestMethod.POST, "pickups",
pickupData, Shipment.class,
vcr.client)).thenReturn(new Pickup());

// This will throw an exception if the carrier account fields could not be serialized properly
assertDoesNotThrow(() -> vcr.client.pickup.create(pickupData));
Expand Down

0 comments on commit 682f1c6

Please sign in to comment.