-
Notifications
You must be signed in to change notification settings - Fork 39
/
BatchTest.java
170 lines (156 loc) · 5.68 KB
/
BatchTest.java
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
package com.shippo.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;
import com.shippo.Shippo;
import com.shippo.exception.APIConnectionException;
import com.shippo.exception.APIException;
import com.shippo.exception.AuthenticationException;
import com.shippo.exception.InvalidRequestException;
import com.shippo.model.Batch.BatchStatus;
import org.junit.Test;
public class BatchTest extends ShippoTest {
@Test
public void testPurchase() throws AuthenticationException, InvalidRequestException, APIConnectionException,
APIException, TimeoutException {
// GIVEN
Batch batch = createBatchFixture();
// EXPECT
assertEquals(Shippo.apiKeyIsTest, batch.isTest());
// WHEN
String batchId = batch.getId();
waitForBatchStatus(batchId, Batch.BatchStatus.VALID, 60);
Batch purchasedBatch = Batch.purchase(batchId);
// THEN
assertEquals(purchasedBatch.getStatus(), Batch.BatchStatus.PURCHASING);
}
/*
* final String id = "dd442663c24843068977704b1bd7d91d";
*
* @Test
* public void testAll() throws AuthenticationException,
* InvalidRequestException, APIConnectionException, APIException,
* UnsupportedEncodingException, MalformedURLException {
* Batch[] batches = Batch.all();
* Batch batch = null;
* for (Batch b : batches) {
* if (b.getId().equals(id)) {
* batch = b;
* break;
* }
* }
* assertEquals(batch.getStatus(), Batch.BatchStatus.VALID);
* }
*
* @Test
* public void testValidGet()
* throws AuthenticationException, InvalidRequestException,
* APIConnectionException, APIException {
* Batch batch = Batch.get(id, 0, null);
* assertEquals(batch.getStatus(), Batch.BatchStatus.VALID);
* }
*
* @Test
* public void testValidGetParams()
* throws AuthenticationException, InvalidRequestException,
* APIConnectionException, APIException {
* Batch batch = Batch.get(id, 1, Batch.ShipmentStatus.CREATION_SUCCEEDED);
* for (BatchShipment bs : batch.getBatchShipments().getShipments()) {
* if (bs.getStatus() != BatchShipment.Status.VALID) {
* fail(String.format("BatchShipment %s is not VALID. Its status is %s",
* bs.getId(), bs.getStatus()));
* }
* }
* }
*
* @Test(expected = InvalidRequestException.class)
* public void testInvalidGet()
* throws AuthenticationException, InvalidRequestException,
* APIConnectionException, APIException {
* Batch.get("invalid_id", 0, null);
* }
*
* private BatchShipment addShipment()
* throws AuthenticationException, InvalidRequestException,
* APIConnectionException, APIException {
* Shipment shipment = ShipmentTest.createShipmentFixture();
* String[] ids = { (String) shipment.getObjectId() };
* Batch batch = Batch.addShipments(id, ids);
* for (BatchShipment bs : batch.getBatchShipments().getShipments()) {
* if (bs.getShipment().equals(ids[0])) {
* return bs;
* }
* }
* return null;
* }
*
* @Test
* public void testAddShipments()
* throws AuthenticationException, InvalidRequestException,
* APIConnectionException, APIException {
* int beforeAddCount = Batch.get(id, 0, null).getBatchShipments().getCount();
* assertNotNull(addShipment());
* int afterAddCount = Batch.get(id, 0, null).getBatchShipments().getCount();
* assertEquals(beforeAddCount + 1, afterAddCount);
* }
*
* @Test
* public void testRemoveShipments()
* throws AuthenticationException, InvalidRequestException,
* APIConnectionException, APIException {
* BatchShipment batchShipment = addShipment();
* String[] bsObjectIds = { batchShipment.getId() };
* int beforeRemoveCount = Batch.get(id, 0,
* null).getBatchShipments().getCount();
* Batch batch = Batch.removeShipments(id, bsObjectIds);
* for (BatchShipment bs : batch.getBatchShipments().getShipments()) {
* if (bs.getId().equals(bsObjectIds[0])) {
* fail(String.
* format("BatchShipment with object ID %s not removed from batch %s",
* bsObjectIds[0],
* batch.getId()));
* }
* }
* int afterRemoveCount = Batch.get(id, 0, null).getBatchShipments().getCount();
* assertEquals(beforeRemoveCount - 1, afterRemoveCount);
* }
*/
/*
* @Test
* public void testCreate()
* throws AuthenticationException, InvalidRequestException,
* APIConnectionException, APIException {
* assertEquals(createBatch().getStatus(), Batch.BatchStatus.VALIDATING);
* }
*/
private Batch createBatchFixture()
throws AuthenticationException, InvalidRequestException, APIConnectionException, APIException {
Address from = Address.createForPurchase("Undefault New Wu", "Clayton St.", "San Francisco", "94117", "CA",
"USA", "[email protected]");
Address to = Address.createForPurchase("Yo", "965 Mission St", "San Francisco", "94103", "CA", "USA",
Parcel parcel = Parcel.createForShipment(2, 3, 3, "cm", 23, "g");
List<Parcel> parcels = new ArrayList<Parcel>();
parcels.add(parcel);
Shipment shipment = Shipment.createForBatch(from, to, parcels);
BatchShipment[] shipments = { BatchShipment.createForShipment(shipment, null, null) };
return Batch.create("09a25c72f0df461ea8fea8b755356aaf", "usps_priority", LabelFileType.PDF, null, shipments);
}
private void waitForBatchStatus(String batchId, BatchStatus status, int times) throws TimeoutException,
AuthenticationException, InvalidRequestException, APIConnectionException, APIException {
while (times > 0) {
Batch batch = Batch.get(batchId, 0, null);
if (batch.getStatus().equals(status)) {
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
times--;
}
throw new TimeoutException(String.format("Timed out waiting for batch %s to become %s", batchId, status));
}
}