2
2
3
3
import com .easypost .exception .API .InvalidRequestError ;
4
4
import com .easypost .exception .EasyPostException ;
5
+ import com .easypost .http .Requestor ;
5
6
import com .easypost .model .CarrierAccount ;
6
7
import com .easypost .model .CarrierType ;
8
+ import com .easypost .model .Pickup ;
9
+ import com .easypost .model .Shipment ;
7
10
import com .google .common .collect .ImmutableMap ;
8
11
import org .junit .jupiter .api .AfterEach ;
9
12
import org .junit .jupiter .api .BeforeAll ;
10
13
import org .junit .jupiter .api .Test ;
14
+ import org .mockito .MockedStatic ;
15
+ import org .mockito .Mockito ;
11
16
12
17
import java .util .HashMap ;
13
18
import java .util .List ;
@@ -23,6 +28,8 @@ public final class CarrierAccountTest {
23
28
24
29
private static TestUtils .VCR vcr ;
25
30
31
+ private static MockedStatic <Requestor > requestMock = Mockito .mockStatic (Requestor .class );
32
+
26
33
/**
27
34
* Set up the testing environment for this file.
28
35
*
@@ -179,4 +186,58 @@ public void testTypes() throws EasyPostException {
179
186
assertInstanceOf (List .class , types );
180
187
assertTrue (types .stream ().allMatch (type -> type != null ));
181
188
}
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
+ }
182
243
}
0 commit comments