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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
package com.p4square.ccbapi;
import com.p4square.ccbapi.exception.CCBErrorResponseException;
import com.p4square.ccbapi.exception.CCBRetryableErrorException;
import com.p4square.ccbapi.model.*;
import org.junit.Before;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.time.LocalDate;
import java.util.Collections;
import java.util.Map;
import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.*;
/**
* Tests for the CCBAPIClient.
*/
public class CCBAPIClientTest {
private HTTPInterface mockHttpClient;
private CCBAPIClient client;
@Before
public void setUp() throws Exception {
mockHttpClient = EasyMock.mock(HTTPInterface.class);
client = new TestCCBAPIClient(new URI("https://localhost:8080/api.php"), mockHttpClient);
}
@Test
public void testClose() throws Exception {
// Set expectation.
mockHttpClient.close();
EasyMock.replay(mockHttpClient);
// Test close.
client.close();
// Verify results.
EasyMock.verify(mockHttpClient);
}
@Test
public void testGetCustomFieldLabelsSuccess() throws Exception {
// Set expectation.
URI expectedURI = new URI("https://localhost:8080/api.php?srv=custom_field_labels");
InputStream is = getClass().getResourceAsStream("model/ccb_custom_field_labels_response.xml");
EasyMock.expect(mockHttpClient.sendPostRequest(expectedURI, null))
.andReturn(is);
EasyMock.replay(mockHttpClient);
// Test custom_field_labels.
GetCustomFieldLabelsResponse response = client.getCustomFieldLabels();
// Verify results.
EasyMock.verify(mockHttpClient);
assertNull(response.getErrors());
assertEquals(27, response.getCustomFields().size());
assertEquals("udf_ind_text_1", response.getCustomFields().get(0).getName());
assertEquals("Favorite Movie", response.getCustomFields().get(0).getLabel());
assertEquals(false, response.getCustomFields().get(0).isAdminOnly());
}
@Test
public void testGetCustomFieldLabelsError() throws Exception {
// Set expectation.
URI expectedURI = new URI("https://localhost:8080/api.php?srv=custom_field_labels");
InputStream is = getClass().getResourceAsStream("model/ccb_error_response.xml");
EasyMock.expect(mockHttpClient.sendPostRequest(expectedURI, null))
.andReturn(is);
EasyMock.replay(mockHttpClient);
try {
// Call getCustomFieldLabels() and expect an exception.
client.getCustomFieldLabels();
fail("No exception thrown.");
} catch (CCBErrorResponseException e) {
// Assert error was received.
EasyMock.verify(mockHttpClient);
assertEquals(1, e.getErrors().size());
assertEquals(2, e.getErrors().get(0).getNumber());
}
}
@Test(expected = CCBRetryableErrorException.class)
public void testGetCustomFieldLabelsTimeout() throws Exception {
// Setup mocks.
EasyMock.expect(mockHttpClient.sendPostRequest(EasyMock.anyObject(URI.class), EasyMock.anyObject()))
.andThrow(new CCBRetryableErrorException("Retryable error"));
EasyMock.replay(mockHttpClient);
// Call getCustomFieldLabels() and expect an exception.
client.getCustomFieldLabels();
}
@Test(expected = IOException.class)
public void testGetCustomFieldLabelsException() throws Exception {
// Setup mocks.
EasyMock.expect(mockHttpClient.sendPostRequest(EasyMock.anyObject(URI.class), EasyMock.anyObject()))
.andThrow(new IOException());
EasyMock.replay(mockHttpClient);
// Call getCustomFieldLabels() and expect an exception.
client.getCustomFieldLabels();
}
@Test
public void testGetIndividualProfilesById() throws Exception {
// Set expectation.
URI expectedURI = new URI("https://localhost:8080/api.php?srv=individual_profile_from_id&individual_id=48");
InputStream is = getClass().getResourceAsStream("model/ccb_individual_profile_response.xml");
EasyMock.expect(mockHttpClient.sendPostRequest(expectedURI, null))
.andReturn(is);
EasyMock.replay(mockHttpClient);
// Test individual_profile_from_id.
GetIndividualProfilesRequest request = new GetIndividualProfilesRequest().withIndividualId(48);
GetIndividualProfilesResponse response = client.getIndividualProfiles(request);
// Verify results.
EasyMock.verify(mockHttpClient);
assertNull(response.getErrors());
assertEquals(1, response.getIndividuals().size());
assertEquals(48, response.getIndividuals().get(0).getId());
}
@Test
public void testGetIndividualProfilesByLogin() throws Exception {
// Set expectation.
URI expectedURI = new URI("https://localhost:8080/api.php?"
+ "srv=individual_profile_from_login_password&password=pass&login=user");
InputStream is = getClass().getResourceAsStream("model/ccb_individual_profile_response.xml");
EasyMock.expect(mockHttpClient.sendPostRequest(expectedURI, null))
.andReturn(is);
EasyMock.replay(mockHttpClient);
// Test individual_profile_from_login_password.
GetIndividualProfilesRequest request = new GetIndividualProfilesRequest()
.withLoginPassword("user", "pass".toCharArray());
GetIndividualProfilesResponse response = client.getIndividualProfiles(request);
// Verify results.
EasyMock.verify(mockHttpClient);
assertNull(response.getErrors());
assertEquals(1, response.getIndividuals().size());
assertEquals(48, response.getIndividuals().get(0).getId());
}
@Test
public void testGetIndividualProfilesByRoutingAndAccount() throws Exception {
// Set expectation.
URI expectedURI = new URI("https://localhost:8080/api.php?"
+ "srv=individual_profile_from_micr&account_number=4567&routing_number=1234");
InputStream is = getClass().getResourceAsStream("model/ccb_individual_profile_response.xml");
EasyMock.expect(mockHttpClient.sendPostRequest(expectedURI, null))
.andReturn(is);
EasyMock.replay(mockHttpClient);
// Test individual_profile_from_micr.
GetIndividualProfilesRequest request = new GetIndividualProfilesRequest().withMICR("1234", "4567");
GetIndividualProfilesResponse response = client.getIndividualProfiles(request);
// Verify results.
EasyMock.verify(mockHttpClient);
assertNull(response.getErrors());
assertEquals(1, response.getIndividuals().size());
assertEquals(48, response.getIndividuals().get(0).getId());
}
@Test
public void testGetAllIndividualProfiles() throws Exception {
// Set expectation.
URI expectedURI = new URI("https://localhost:8080/api.php?srv=individual_profiles");
InputStream is = getClass().getResourceAsStream("model/ccb_individual_profile_response.xml");
EasyMock.expect(mockHttpClient.sendPostRequest(expectedURI, null))
.andReturn(is);
EasyMock.replay(mockHttpClient);
// Test individual_profiles without any options.
GetIndividualProfilesRequest request = new GetIndividualProfilesRequest();
GetIndividualProfilesResponse response = client.getIndividualProfiles(request);
// Verify results.
EasyMock.verify(mockHttpClient);
assertNull(response.getErrors());
assertEquals(1, response.getIndividuals().size());
assertEquals(48, response.getIndividuals().get(0).getId());
}
@Test
public void testGetAllIndividualProfilesWithOptions() throws Exception {
// Set expectation.
URI expectedURI = new URI("https://localhost:8080/api.php?srv=individual_profiles"
+ "&per_page=15&include_inactive=true&modified_since=2016-03-19&page=5");
InputStream is = getClass().getResourceAsStream("model/ccb_individual_profile_response.xml");
EasyMock.expect(mockHttpClient.sendPostRequest(expectedURI, null))
.andReturn(is);
EasyMock.replay(mockHttpClient);
// Test individual_profiles with all the options.
GetIndividualProfilesRequest request = new GetIndividualProfilesRequest()
.withModifiedSince(LocalDate.parse("2016-03-19"))
.withIncludeInactive(true)
.withPage(5)
.withPerPage(15);
GetIndividualProfilesResponse response = client.getIndividualProfiles(request);
// Verify results.
EasyMock.verify(mockHttpClient);
assertNull(response.getErrors());
assertEquals(1, response.getIndividuals().size());
assertEquals(48, response.getIndividuals().get(0).getId());
}
@Test
public void testUpdateIndividual() throws Exception {
// Set expectation.
URI expectedURI = new URI("https://localhost:8080/api.php?srv=update_individual&individual_id=48");
InputStream is = getClass().getResourceAsStream("model/ccb_individual_profile_response.xml");
EasyMock.expect(mockHttpClient.sendPostRequest(EasyMock.eq(expectedURI),
EasyMock.aryEq("legal_first_name=Larabar".getBytes("UTF-8"))))
.andReturn(is);
EasyMock.replay(mockHttpClient);
UpdateIndividualProfileRequest request = new UpdateIndividualProfileRequest()
.withIndividualId(48)
.withLegalFirstName("Larabar");
// Test update_individual.
UpdateIndividualProfileResponse response = client.updateIndividualProfile(request);
// Verify results.
EasyMock.verify(mockHttpClient);
assertNull(response.getErrors());
assertEquals(1, response.getIndividuals().size());
assertEquals(48, response.getIndividuals().get(0).getId());
}
/**
* This test ensures {@link CCBAPIClient#updateIndividualProfile(UpdateIndividualProfileRequest)}
* throws an exception if the individualId property isn't set on the request.
*/
@Test(expected = IllegalArgumentException.class)
public void testUpdateIndividualWithoutIndividualId() throws Exception {
// Set expectation.
UpdateIndividualProfileRequest request = new UpdateIndividualProfileRequest()
.withLegalFirstName("Larabar");
// Test update_individual.
UpdateIndividualProfileResponse response = client.updateIndividualProfile(request);
// Expect exception.
}
/**
* Simple extension of CCBAPIClient to swap out the HTTPInterface with a mock.
*/
private final class TestCCBAPIClient extends CCBAPIClient {
public TestCCBAPIClient(final URI apiUri, final HTTPInterface mockHttpClient) {
super(apiUri, mockHttpClient);
}
}
}
|