-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathAddressTest.java
More file actions
350 lines (288 loc) · 12.9 KB
/
AddressTest.java
File metadata and controls
350 lines (288 loc) · 12.9 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package com.easypost;
import com.easypost.exception.API.InvalidRequestError;
import com.easypost.exception.EasyPostException;
import com.easypost.exception.General.EndOfPaginationError;
import com.easypost.model.Address;
import com.easypost.model.AddressCollection;
import com.easypost.model.AddressDetail;
import com.easypost.model.AddressVerificationFieldError;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public final class AddressTest {
private static TestUtils.VCR vcr;
/**
* Set up the testing environment for this file.
*
* @throws EasyPostException when the request fails.
*/
@BeforeAll
public static void setup() throws EasyPostException {
vcr = new TestUtils.VCR("address", TestUtils.ApiKey.TEST);
}
/**
* Create a basic address.
*
* @return basic Address object
* @throws EasyPostException if an exception is thrown.
*/
public Address createBasicAddress() throws EasyPostException {
return vcr.client.address.create(Fixtures.caAddress1());
}
/**
* Test creating an address.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testCreate() throws EasyPostException {
vcr.setUpTest("create");
Address address = createBasicAddress();
assertInstanceOf(Address.class, address);
assertTrue(address.getId().startsWith("adr_"));
assertEquals("Address", address.getObject());
assertEquals("388 Townsend St", address.getStreet1());
}
/**
* Test creating an address with the verify param.
* We purposefully pass in slightly incorrect data to get the corrected address
* back once verified.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testCreateVerify() throws EasyPostException {
vcr.setUpTest("create_verify");
Map<String, Object> addressData = Fixtures.incorrectAddress();
// Creating normally (without specifying "verify") will make the address, perform no verifications
Address address = vcr.client.address.create(addressData);
assertInstanceOf(Address.class, address);
assertNull(address.getVerifications().getDelivery());
assertNull(address.getVerifications().getZip4());
// Creating with verify would make the address and perform verifications
// internally, we're just checking for the presence of "verify" in the dictionary, so the value doesn't matter
addressData.put("verify", true);
address = vcr.client.address.create(addressData);
assertInstanceOf(Address.class, address);
assertEquals(false, address.getVerifications().getDelivery().getSuccess());
assertInstanceOf(AddressDetail.class, address.getVerifications().getDelivery().getDetails());
AddressVerificationFieldError addressVerificationFieldErrorGetDelivery = address.getVerifications()
.getDelivery()
.getErrors()
.get(0);
assertEquals("E.ADDRESS.NOT_FOUND", addressVerificationFieldErrorGetDelivery.getCode());
assertEquals("address", addressVerificationFieldErrorGetDelivery.getField());
assertNull(addressVerificationFieldErrorGetDelivery.getSuggestion());
assertEquals("Address not found", addressVerificationFieldErrorGetDelivery.getMessage());
assertEquals(false, address.getVerifications().getZip4().getSuccess());
assertNull(address.getVerifications().getZip4().getDetails());
AddressVerificationFieldError addressVerificationFieldErrorZip4 = address.getVerifications()
.getZip4()
.getErrors()
.get(0);
assertEquals("E.ADDRESS.NOT_FOUND", addressVerificationFieldErrorZip4.getCode());
assertEquals("address", addressVerificationFieldErrorZip4.getField());
assertNull(addressVerificationFieldErrorZip4.getSuggestion());
assertEquals("Address not found", addressVerificationFieldErrorZip4.getMessage());
}
/**
* Test creating an address with the verify_strict param.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testCreateVerifyStrict() throws EasyPostException {
vcr.setUpTest("create_verify_strict");
Map<String, Object> addressData = Fixtures.caAddress1();
addressData.put("verify_strict", true);
Address address = vcr.client.address.create(addressData);
assertInstanceOf(Address.class, address);
assertTrue(address.getId().startsWith("adr_"));
assertEquals("388 TOWNSEND ST APT 20", address.getStreet1());
}
/**
* Test creating an address with the verify param.
* We purposefully pass in slightly incorrect data to get the corrected address
* back once verified.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testCreateVerifyArray() throws EasyPostException {
vcr.setUpTest("create_verify_array");
Map<String, Object> addressData = Fixtures.incorrectAddress();
// Creating normally (without specifying "verify") will make the address, perform no verifications
Address address = vcr.client.address.create(addressData);
assertInstanceOf(Address.class, address);
assertNull(address.getVerifications().getDelivery());
assertNull(address.getVerifications().getZip4());
// Creating with verify would make the address and perform verifications
// internally, we're just checking for the presence of "verify" in the dictionary, so the value doesn't matter
List<Boolean> verificationList = new ArrayList<>();
verificationList.add(true);
addressData.put("verify", verificationList);
address = vcr.client.address.create(addressData);
assertInstanceOf(Address.class, address);
assertNotNull(address.getVerifications().getDelivery());
assertNotNull(address.getVerifications().getZip4());
}
/**
* Test retrieving an address.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testRetrieve() throws EasyPostException {
vcr.setUpTest("retrieve");
Address address = createBasicAddress();
Address retrievedAddress = vcr.client.address.retrieve(address.getId());
assertInstanceOf(Address.class, retrievedAddress);
assertTrue(address.equals(retrievedAddress));
}
/**
* Test retrieving all addresses.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testAll() throws EasyPostException {
vcr.setUpTest("all");
Map<String, Object> params = new HashMap<>();
params.put("page_size", Fixtures.pageSize());
AddressCollection addresses = vcr.client.address.all(params);
List<Address> addressesList = addresses.getAddresses();
assertTrue(addressesList.size() <= Fixtures.pageSize());
assertNotNull(addresses.getHasMore());
assertTrue(addressesList.stream().allMatch(address -> address != null));
}
/**
* Test retrieving the next page.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testGetNextPage() throws EasyPostException {
vcr.setUpTest("get_next_page");
Map<String, Object> params = new HashMap<>();
params.put("page_size", Fixtures.pageSize());
AddressCollection collection = vcr.client.address.all(params);
try {
AddressCollection nextPage = vcr.client.address.getNextPage(collection, Fixtures.pageSize());
assertNotNull(nextPage);
String firstIdOfFirstPage = collection.getAddresses().get(0).getId();
String firstIdOfSecondPage = nextPage.getAddresses().get(0).getId();
assertNotEquals(firstIdOfFirstPage, firstIdOfSecondPage);
} catch (EndOfPaginationError e) { // There's no next page, that's not a failure
assertTrue(true);
} catch (Exception e) { // Any other exception is a failure
fail();
}
}
/**
* Test creating a verified address.
* We purposefully pass in slightly incorrect data to get the corrected address
* back once verified.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testCreateAndVerify() throws EasyPostException {
vcr.setUpTest("create_and_verify");
Map<String, Object> addressData = Fixtures.caAddress1();
Address address = vcr.client.address.createAndVerify(addressData);
assertInstanceOf(Address.class, address);
assertTrue(address.getId().startsWith("adr_"));
assertEquals("388 TOWNSEND ST APT 20", address.getStreet1());
}
/**
* Test verifying an existing address.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testVerify() throws EasyPostException {
vcr.setUpTest("verify");
Address address = createBasicAddress();
Address verifiedAddress = vcr.client.address.verify(address.getId());
assertInstanceOf(Address.class, verifiedAddress);
assertTrue(verifiedAddress.getId().startsWith("adr_"));
assertEquals("388 TOWNSEND ST APT 20", verifiedAddress.getStreet1());
}
/**
* Test creating invalid address creation to see if the error has correct properties.
*
* @throws EasyPostException if an exception is thrown.
*/
@Test
public void testInvalidAddressCreation() throws EasyPostException {
vcr.setUpTest("error_address_creation");
Map<String, Object> params = new HashMap<>();
InvalidRequestError exception =
assertThrows(InvalidRequestError.class, () -> vcr.client.address.createAndVerify(params));
assertEquals("PARAMETER.REQUIRED", exception.getCode());
assertEquals(422, exception.getStatusCode());
assertEquals("Missing required parameter.", exception.getMessage());
}
/**
* Test creating an address with the verify_carrier param.
* We purposefully pass in slightly incorrect data to get the corrected address
* back once verified.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testCreateVerifyCarrier() throws EasyPostException {
vcr.setUpTest("create_verify_carrier");
Map<String, Object> addressData = Fixtures.incorrectAddress();
addressData.put("verify", true);
addressData.put("verify_carrier", "UPS");
Address address = vcr.client.address.create(addressData);
assertInstanceOf(Address.class, address);
AddressVerificationFieldError addressVerificationFieldErrorGetDelivery = address.getVerifications()
.getDelivery()
.getErrors()
.get(0);
AddressVerificationFieldError addressVerificationFieldErrorZip4 = address.getVerifications()
.getZip4()
.getErrors()
.get(0);
assertEquals("Address not found", addressVerificationFieldErrorGetDelivery.getMessage());
assertEquals("Address not found", addressVerificationFieldErrorZip4.getMessage());
}
/**
* Test creating and verifying an address with the verify_carrier param.
* We purposefully pass in slightly incorrect data to get the corrected address
* back once verified.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testCreateAndVerifyCarrier() throws EasyPostException {
vcr.setUpTest("create_and_verify_carrier");
Map<String, Object> addressData = Fixtures.incorrectAddress();
addressData.put("verify_carrier", "UPS");
Address address = vcr.client.address.createAndVerify(addressData);
assertInstanceOf(Address.class, address);
AddressVerificationFieldError addressVerificationFieldErrorGetDelivery = address.getVerifications()
.getDelivery()
.getErrors()
.get(0);
AddressVerificationFieldError addressVerificationFieldErrorZip4 = address.getVerifications()
.getZip4()
.getErrors()
.get(0);
assertEquals("Address not found", addressVerificationFieldErrorGetDelivery.getMessage());
assertEquals("Address not found", addressVerificationFieldErrorZip4.getMessage());
}
}