-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathBillingTest.java
More file actions
132 lines (112 loc) · 5.41 KB
/
Copy pathBillingTest.java
File metadata and controls
132 lines (112 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.easypost;
import com.easypost.exception.EasyPostException;
import com.easypost.http.Requestor;
import com.easypost.http.Requestor.RequestMethod;
import com.easypost.model.PaymentMethod;
import com.easypost.model.PaymentMethodObject;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public final class BillingTest {
private static TestUtils.VCR vcr;
private String jsonResponse = "{\"id\":\"cust_...\",\"object\":\"PaymentMethods\",\"primary_" +
"payment_method\":{\"id\":\"pm_...\",\"disabled_at\":null,\"object\":\"CreditCard\",\"na" +
"me\":null,\"last4\":\"4242\",\"exp_month\":1,\"exp_year\":2025,\"brand\":\"Visa\"},\"secondar" +
"y_payment_method\":{\"id\":\"pm_...\",\"disabled_at\":null,\"object\":\"BankAccount\",\"name\":nu" +
"ll,\"last4\":\"4444\",\"exp_month\":1,\"exp_year\":2025,\"brand\":\"Mastercard\"}}";
private PaymentMethod paymentMethod = Constants.Http.GSON.fromJson(jsonResponse, PaymentMethod.class);
private static MockedStatic<Requestor> requestMock = Mockito.mockStatic(Requestor.class);
/**
* Setup the testing environment for this file.
*
* @throws EasyPostException when the request fails.
*/
@BeforeAll
public static void setup() throws EasyPostException {
vcr = new TestUtils.VCR("billing", TestUtils.ApiKey.PRODUCTION);
}
/**
* Release the static mock once it has been used.
*/
@AfterAll
public static void cleanup() {
requestMock.close();
}
/**
* Test deleting a payment method.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testDeletePaymentMethod() throws EasyPostException {
requestMock.when(() -> Requestor.request(
RequestMethod.GET, "payment_methods", null, PaymentMethod.class, vcr.client))
.thenReturn(paymentMethod);
PaymentMethodObject paymentMethodObject =
vcr.client.billing.retrievePaymentMethods().getSecondaryPaymentMethod();
requestMock.when(() -> Requestor.request(RequestMethod.GET,
paymentMethodObject.getEndpoint() + "/" + paymentMethodObject.getId(),
null, PaymentMethod.class,
vcr.client)).thenReturn(null);
assertDoesNotThrow(() -> vcr.client.billing.deletePaymentMethod(PaymentMethod.Priority.SECONDARY));
}
/**
* Test funding a wallet.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testFundWallet() throws EasyPostException {
requestMock.when(() -> Requestor.request(
RequestMethod.GET, "payment_methods", null, PaymentMethod.class, vcr.client))
.thenReturn(paymentMethod);
PaymentMethodObject paymentMethodObject = vcr.client.billing.retrievePaymentMethods().getPrimaryPaymentMethod();
requestMock.when(() -> Requestor.request(RequestMethod.GET,
paymentMethodObject.getEndpoint() + "/" + paymentMethodObject.getId() + "/charges", null,
PaymentMethod.class, vcr.client)).thenReturn(paymentMethod);
assertDoesNotThrow(() -> vcr.client.billing.fundWallet("2000"));
}
/**
* Test retrieving all payment methods.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testRetrievePaymentMethods() throws EasyPostException {
requestMock.when(() -> Requestor.request(
RequestMethod.GET, "payment_methods", null, PaymentMethod.class, vcr.client))
.thenReturn(paymentMethod);
PaymentMethod paymentMethods = vcr.client.billing.retrievePaymentMethods();
assertNotNull(paymentMethods.getPrimaryPaymentMethod());
assertNotNull(paymentMethods.getSecondaryPaymentMethod());
}
/**
* Test determining a payment method type by its object type.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testDeterminePaymentMethodTypeByObjectType() throws EasyPostException {
requestMock.when(() -> Requestor.request(
RequestMethod.GET, "payment_methods", null, PaymentMethod.class, vcr.client))
.thenReturn(paymentMethod);
// Should be a credit card with "CreditCard" object type and "pm_" prefix
PaymentMethodObject creditCard =
vcr.client.billing.retrievePaymentMethods().getPrimaryPaymentMethod();
assertTrue(creditCard.getId().startsWith("pm_"));
assertEquals("CreditCard", creditCard.getObject());
assertEquals(PaymentMethodObject.PaymentMethodType.CREDIT_CARD, creditCard.getType());
// Should be a bank account with "BankAccount" object type and "pm_" prefix
PaymentMethodObject bankAccount =
vcr.client.billing.retrievePaymentMethods().getSecondaryPaymentMethod();
assertTrue(bankAccount.getId().startsWith("pm_"));
assertEquals("BankAccount", bankAccount.getObject());
assertEquals(PaymentMethodObject.PaymentMethodType.BANK_ACCOUNT, bankAccount.getType());
}
}