-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAbacatePayTest.java
More file actions
130 lines (95 loc) · 4.59 KB
/
Copy pathAbacatePayTest.java
File metadata and controls
130 lines (95 loc) · 4.59 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
package com.abacatepay;
import com.abacatepay.clients.AbacatePayClient;
import com.abacatepay.model.AbacatePayBilling;
import com.abacatepay.model.IAbacatePayBilling;
import com.abacatepay.model.billing.CreateBillingData;
import com.abacatepay.model.billing.CreateBillingResponse;
import com.abacatepay.model.billing.ListBillingResponse;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import static org.mockito.Mockito.*;
class AbacatePayTest {
@Mock
private AbacatePayClient mockClient;
private AbacatePay abacatePay;
@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.openMocks(this);
abacatePay = new AbacatePay("apiKey");
IAbacatePayBilling realBilling = new AbacatePayBilling(mockClient);
setClientMock(abacatePay, realBilling);
}
private void setClientMock(AbacatePay abacatePay, IAbacatePayBilling realBilling) throws Exception {
Field clientField = AbacatePay.class.getDeclaredField("billing");
clientField.setAccessible(true);
clientField.set(abacatePay, realBilling);
}
@Test
void shouldReturnCreateBillingResponseOnSuccess() {
CreateBillingData data = CreateBillingData.builder().build();
CreateBillingResponse expectedResponse = new CreateBillingResponse();
when(mockClient.create(data)).thenReturn(expectedResponse);
CreateBillingResponse result = abacatePay.billing().create(data);
verify(mockClient, atMostOnce()).create(data);
Assertions.assertEquals(expectedResponse, result, "Should return the expected response");
}
@Test
void shouldCreateBillingReturnErrorResponseWhenClientFails() {
CreateBillingData data = CreateBillingData.builder().build();
String errorMessage = "Internal Server Error";
when(mockClient.create(data)).thenThrow(new IllegalArgumentException(errorMessage));
CreateBillingResponse result = abacatePay.billing().create(data);
verify(mockClient, atMostOnce()).create(data);
Assertions.assertNotNull(result, "Should not return null");
Assertions.assertEquals(errorMessage, result.getError());
}
@Test
void shouldReturnBillingListResponseOnSucess() {
ListBillingResponse expectedResponse = new ListBillingResponse();
when(mockClient.list()).thenReturn(expectedResponse);
ListBillingResponse result = abacatePay.billing().list();
verify(mockClient, atMostOnce()).list();
Assertions.assertEquals(expectedResponse, result, "Should return the expected response");
}
@Test
void shouldListBillingReturnErrorResponseWhenClientFails() {
String errorMessage = "Internal Server Error";
when(mockClient.list()).thenThrow(new IllegalArgumentException(errorMessage));
ListBillingResponse result = abacatePay.billing().list();
verify(mockClient, atMostOnce()).list();
Assertions.assertNotNull(result, "Should not return null");
Assertions.assertEquals(errorMessage, result.getError());
}
@Test
void shouldThrowExceptionWhenApiKeyIsMissing() {
IllegalArgumentException exception = Assertions.assertThrows(IllegalArgumentException.class, () -> {
new AbacatePay("");
});
Assertions.assertEquals("API key not provided", exception.getMessage());
}
@Test
void testResolveVersionDirectly() throws Exception {
Method method = AbacatePay.class.getDeclaredMethod("resolveVersion");
method.setAccessible(true);
String version = (String) method.invoke(null);
Assertions.assertNotNull(version);
Assertions.assertEquals("dev", version, "local environment should return 'dev' as version");
}
@Test
void testBuildUserAgentDirectly() throws Exception {
Method method = AbacatePay.class.getDeclaredMethod("buildUserAgent");
method.setAccessible(true);
String userAgent = (String) method.invoke(null);
String currentJava = System.getProperty("java.version");
String currentOs = System.getProperty("os.name");
Assertions.assertNotNull(userAgent);
Assertions.assertTrue(userAgent.startsWith("abacatepay-java-sdk/"), "Should start with the SDK name");
Assertions.assertTrue(userAgent.contains("Java/" + currentJava), "Should contain the Java tag version");
Assertions.assertTrue(userAgent.contains(currentOs), "Should contain the OS name");
}
}