-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathTrackerTest.java
More file actions
246 lines (193 loc) · 7.95 KB
/
TrackerTest.java
File metadata and controls
246 lines (193 loc) · 7.95 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
package com.easypost;
import com.easypost.exception.EasyPostException;
import com.easypost.exception.General.EndOfPaginationError;
import com.easypost.model.Tracker;
import com.easypost.model.TrackerCollection;
import com.google.common.collect.ImmutableList;
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.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public final class TrackerTest {
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("tracker", TestUtils.ApiKey.TEST);
}
/**
* Test creating a tracker.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testCreate() throws EasyPostException {
vcr.setUpTest("create");
Tracker tracker = createBasicTracker();
assertInstanceOf(Tracker.class, tracker);
assertTrue(tracker.getId().startsWith("trk_"));
assertEquals("pre_transit", tracker.getStatus());
}
/**
* Create a tracker.
*
* @return Tracker object
*/
private static Tracker createBasicTracker() throws EasyPostException {
Map<String, Object> params = new HashMap<>();
params.put("carrier", Fixtures.usps());
params.put("tracking_code", "EZ1000000001");
return vcr.client.tracker.create(params);
}
/**
* Test retrieving a tracker.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testRetrieve() throws EasyPostException {
vcr.setUpTest("retrieve");
Tracker tracker = createBasicTracker();
Tracker retrievedTracker = vcr.client.tracker.retrieve(tracker.getId());
assertInstanceOf(Tracker.class, tracker);
assertTrue(retrievedTracker.getId().startsWith("trk_"));
assertEquals(tracker.getId(), retrievedTracker.getId());
}
/**
* Test retrieving all trackers.
*
* @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());
TrackerCollection trackers = vcr.client.tracker.all(params);
List<Tracker> trackersList = trackers.getTrackers();
assertTrue(trackersList.size() <= Fixtures.pageSize());
assertNotNull(trackers.getHasMore());
assertTrue(trackersList.stream().allMatch(tracker -> tracker != null));
}
/**
* Test the parameter handoff when retrieving all trackers.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testAllParameterHandOff() throws EasyPostException {
vcr.setUpTest("all_parameter_hand_off");
String trackingCode = "something";
String carrier = "something else";
Map<String, Object> params = new HashMap<String, Object>();
params.put("page_size", Fixtures.pageSize());
params.put("tracking_code", trackingCode);
params.put("carrier", carrier);
TrackerCollection trackerCollection = vcr.client.tracker.all(params);
assertEquals(trackingCode, trackerCollection.getTrackingCode());
assertEquals(carrier, trackerCollection.getCarrier());
}
/**
* 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());
TrackerCollection collection = vcr.client.tracker.all(params);
try {
TrackerCollection nextPage = vcr.client.tracker.getNextPage(collection, Fixtures.pageSize());
assertNotNull(nextPage);
String firstIdOfFirstPage = collection.getTrackers().get(0).getId();
String firstIdOfSecondPage = nextPage.getTrackers().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 the parameter handoff when constructing the next page parameter map.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testGetNextPageParameterHandOff() throws EasyPostException {
vcr.setUpTest("get_next_page_parameter_hand_off");
String trackingCode = "something";
String carrier = "something else";
Map<String, Object> params = new HashMap<String, Object>();
params.put("page_size", Fixtures.pageSize());
params.put("tracking_code", trackingCode);
params.put("carrier", carrier);
TrackerCollection trackerCollection = vcr.client.tracker.all(params);
// Can't access protected method directly, need to make a temporary extended class, yay
// Downside, TrackerCollection and Tracker are no longer final because they need to be extended
final class ExtendedTrackerCollection extends TrackerCollection {
ExtendedTrackerCollection(TrackerCollection trackerCollection) {
setTrackingCode(trackerCollection.getTrackingCode());
setCarrier(trackerCollection.getCarrier());
}
@Override
public List<Tracker> getTrackers() {
final class ExtendedTracker extends Tracker {
@Override
public String getId() {
return "trk_123";
}
}
return new ArrayList<Tracker>(ImmutableList.of(new ExtendedTracker()));
}
public Map<String, Object> getNextPageParams() throws EndOfPaginationError {
return super.buildNextPageParameters(getTrackers(), null);
}
}
ExtendedTrackerCollection extendedShipmentCollection = new ExtendedTrackerCollection(trackerCollection);
Map<String, Object> nextPageParams = extendedShipmentCollection.getNextPageParams();
assertEquals(trackingCode, nextPageParams.get("tracking_code"));
assertEquals(carrier, nextPageParams.get("carrier"));
}
/**
* Test retrieving a batch of trackers.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testRetrieveBatch() throws EasyPostException {
vcr.setUpTest("retrieve_batch");
Tracker tracker = createBasicTracker();
Map<String, Object> params = new HashMap<>();
params.put("tracking_codes", ImmutableList.of(tracker.getTrackingCode()));
TrackerCollection trackers = vcr.client.tracker.retrieveBatch(params);
List<Tracker> trackersList = trackers.getTrackers();
assertTrue(trackersList.stream().allMatch(singleTracker -> singleTracker != null));
}
/**
* Test deleting a tracker.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testDelete() throws EasyPostException {
vcr.setUpTest("delete");
Tracker tracker = createBasicTracker();
// Nothing gets returned here, simply ensure no error gets raised
vcr.client.tracker.delete(tracker.getId());
}
}