|
12 | 12 | import org.mockito.Mockito;
|
13 | 13 |
|
14 | 14 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
15 | 16 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
| 17 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 18 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
16 | 19 |
|
17 | 20 | public final class BillingTest {
|
18 | 21 | private static TestUtils.VCR vcr;
|
19 | 22 | private String jsonResponse = "{\"id\":\"cust_...\",\"object\":\"PaymentMethods\",\"primary_" +
|
20 |
| - "payment_method\":{\"id\":\"card_...\",\"disabled_at\":null,\"object\":\"CreditCard\",\"na" + |
| 23 | + "payment_method\":{\"id\":\"pm_...\",\"disabled_at\":null,\"object\":\"CreditCard\",\"na" + |
21 | 24 | "me\":null,\"last4\":\"4242\",\"exp_month\":1,\"exp_year\":2025,\"brand\":\"Visa\"},\"secondar" +
|
22 |
| - "y_payment_method\":{\"id\":\"card_...\",\"disabled_at\":null,\"object\":\"CreditCard\",\"name\":nu" + |
| 25 | + "y_payment_method\":{\"id\":\"pm_...\",\"disabled_at\":null,\"object\":\"BankAccount\",\"name\":nu" + |
23 | 26 | "ll,\"last4\":\"4444\",\"exp_month\":1,\"exp_year\":2025,\"brand\":\"Mastercard\"}}";
|
24 | 27 | private PaymentMethod paymentMethod = Constants.Http.GSON.fromJson(jsonResponse, PaymentMethod.class);
|
| 28 | + |
| 29 | + private String jsonResponseLegacyPrefixes = "{\"id\":\"cust_...\",\"object\":\"PaymentMethods\",\"primary_" + |
| 30 | + "payment_method\":{\"id\":\"card_...\",\"disabled_at\":null,\"object\":null,\"na" + |
| 31 | + "me\":null,\"last4\":\"4242\",\"exp_month\":1,\"exp_year\":2025,\"brand\":\"Visa\"},\"secondar" + |
| 32 | + "y_payment_method\":{\"id\":\"bank_...\",\"disabled_at\":null,\"object\":null,\"name\":nu" + |
| 33 | + "ll,\"last4\":\"4444\",\"exp_month\":1,\"exp_year\":2025,\"brand\":\"Mastercard\"}}"; |
| 34 | + |
| 35 | + private PaymentMethod paymentMethodLegacyPrefixes = |
| 36 | + Constants.Http.GSON.fromJson(jsonResponseLegacyPrefixes, PaymentMethod.class); |
| 37 | + |
25 | 38 | private static MockedStatic<Requestor> requestMock = Mockito.mockStatic(Requestor.class);
|
26 | 39 |
|
27 | 40 | /**
|
@@ -100,4 +113,56 @@ public void testRetrievePaymentMethods() throws EasyPostException {
|
100 | 113 | assertNotNull(paymentMethods.getPrimaryPaymentMethod());
|
101 | 114 | assertNotNull(paymentMethods.getSecondaryPaymentMethod());
|
102 | 115 | }
|
| 116 | + |
| 117 | + /** |
| 118 | + * Test determining a payment method type by its object type. |
| 119 | + * |
| 120 | + * @throws EasyPostException when the request fails. |
| 121 | + */ |
| 122 | + @Test |
| 123 | + public void testDeterminePaymentMethodTypeByObjectType() throws EasyPostException { |
| 124 | + requestMock.when(() -> Requestor.request( |
| 125 | + RequestMethod.GET, "payment_methods", null, PaymentMethod.class, vcr.client)) |
| 126 | + .thenReturn(paymentMethod); |
| 127 | + |
| 128 | + // Should be a credit card with "CreditCard" object type and "pm_" prefix |
| 129 | + PaymentMethodObject creditCard = |
| 130 | + vcr.client.billing.retrievePaymentMethods().getPrimaryPaymentMethod(); |
| 131 | + assertTrue(creditCard.getId().startsWith("pm_")); |
| 132 | + assertEquals("CreditCard", creditCard.getObject()); |
| 133 | + assertEquals(PaymentMethodObject.PaymentMethodType.CREDIT_CARD, creditCard.getType()); |
| 134 | + |
| 135 | + // Should be a bank account with "BankAccount" object type and "pm_" prefix |
| 136 | + PaymentMethodObject bankAccount = |
| 137 | + vcr.client.billing.retrievePaymentMethods().getSecondaryPaymentMethod(); |
| 138 | + assertTrue(bankAccount.getId().startsWith("pm_")); |
| 139 | + assertEquals("BankAccount", bankAccount.getObject()); |
| 140 | + assertEquals(PaymentMethodObject.PaymentMethodType.BANK_ACCOUNT, bankAccount.getType()); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Test determining a payment method type by its legacy prefix. |
| 145 | + * |
| 146 | + * @throws EasyPostException when the request fails. |
| 147 | + */ |
| 148 | + @Test |
| 149 | + public void testDeterminePaymentMethodTypeByLegacyPrefix() throws EasyPostException { |
| 150 | + requestMock.when(() -> Requestor.request( |
| 151 | + RequestMethod.GET, "payment_methods", null, PaymentMethod.class, vcr.client)) |
| 152 | + .thenReturn(paymentMethodLegacyPrefixes); |
| 153 | + |
| 154 | + // Should be a credit card with null object type and "card_" prefix |
| 155 | + PaymentMethodObject creditCard = |
| 156 | + vcr.client.billing.retrievePaymentMethods().getPrimaryPaymentMethod(); |
| 157 | + assertTrue(creditCard.getId().startsWith("card_")); |
| 158 | + assertNull(creditCard.getObject()); |
| 159 | + assertEquals(PaymentMethodObject.PaymentMethodType.CREDIT_CARD, creditCard.getType()); |
| 160 | + |
| 161 | + // Should be a bank account with null object type and "bank_" prefix |
| 162 | + PaymentMethodObject bankAccount = |
| 163 | + vcr.client.billing.retrievePaymentMethods().getSecondaryPaymentMethod(); |
| 164 | + assertTrue(bankAccount.getId().startsWith("bank_")); |
| 165 | + assertNull(bankAccount.getObject()); |
| 166 | + assertEquals(PaymentMethodObject.PaymentMethodType.BANK_ACCOUNT, bankAccount.getType()); |
| 167 | + } |
103 | 168 | }
|
0 commit comments