-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathUserTest.java
More file actions
236 lines (191 loc) · 6.75 KB
/
Copy pathUserTest.java
File metadata and controls
236 lines (191 loc) · 6.75 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
package com.easypost;
import com.easypost.exception.EasyPostException;
import com.easypost.exception.General.EndOfPaginationError;
import com.easypost.model.Brand;
import com.easypost.model.ChildUserCollection;
import com.easypost.model.User;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
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 UserTest {
private static String testUserId = null;
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("user", TestUtils.ApiKey.PRODUCTION);
}
/**
* Clean up test attributes after each unit test.
*/
@AfterEach
public void cleanup() {
if (testUserId != null) {
try {
User user = vcr.client.user.retrieve(testUserId);
vcr.client.user.delete(user.getId());
testUserId = null;
} catch (Exception e) {
// in case we try to delete something that's already been deleted
}
}
}
/**
* Test creating a child user.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testCreate() throws EasyPostException {
vcr.setUpTest("create");
User user = createUser();
assertInstanceOf(User.class, user);
assertTrue(user.getId().startsWith("user_"));
assertEquals("Test User", user.getName());
}
/**
* Create a user.
*
* @return User object
*/
private static User createUser() throws EasyPostException {
Map<String, Object> params = new HashMap<>();
params.put("name", "Test User");
User user = vcr.client.user.create(params);
testUserId = user.getId(); // trigger deletion after test
return user;
}
/**
* Test retrieving a user.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testRetrieve() throws EasyPostException {
vcr.setUpTest("retrieve");
User authenticatedUser = retrieveMe();
String userId = authenticatedUser.getId();
User user = vcr.client.user.retrieve(userId);
assertInstanceOf(User.class, user);
assertTrue(user.getId().startsWith("user_"));
assertEquals(userId, user.getId());
}
/**
* Retrieve current user.
*
* @return User object
*/
private static User retrieveMe() throws EasyPostException {
return vcr.client.user.retrieveMe();
}
/**
* Test retrieving user itself.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testRetrieveMe() throws EasyPostException {
vcr.setUpTest("retrieve_me");
User user = vcr.client.user.retrieveMe();
assertInstanceOf(User.class, user);
assertTrue(user.getId().startsWith("user_"));
}
/**
* Test updating a user.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testUpdate() throws EasyPostException {
vcr.setUpTest("update");
User user = createUser();
String testName = "New Name";
Map<String, Object> params = new HashMap<>();
params.put("name", testName);
User updatedUser = vcr.client.user.update(user.getId(), params);
assertInstanceOf(User.class, updatedUser);
assertTrue(updatedUser.getId().startsWith("user_"));
assertEquals(testName, updatedUser.getName());
}
/**
* Test deleting a user.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testDelete() throws EasyPostException {
vcr.setUpTest("delete");
User user = createUser();
assertDoesNotThrow(() -> vcr.client.user.delete(user.getId()));
}
/**
* Test updating a brand.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testUpdateBrand() throws EasyPostException {
vcr.setUpTest("update_brand");
User user = createUser();
String color = "#123456";
Map<String, Object> params = new HashMap<>();
params.put("color", color);
Brand brand = vcr.client.user.updateBrand(user.getId(), params);
assertInstanceOf(Brand.class, brand);
assertTrue(brand.getId().startsWith("brd_"));
assertEquals(color, brand.getColor());
}
/**
* Test retrieving a paginated list of children.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testAllChildren() throws EasyPostException {
vcr.setUpTest("all_children");
Map<String, Object> params = new HashMap<>();
params.put("page_size", Fixtures.pageSize());
ChildUserCollection children = vcr.client.user.allChildren(params);
List<User> childrenList = children.getChildren();
assertTrue(childrenList.size() <= Fixtures.pageSize());
assertNotNull(children.getHasMore());
assertTrue(childrenList.stream().allMatch(children_user -> children_user != null));
}
/**
* Test retrieving the next page of child users.
*
* @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());
ChildUserCollection collection = vcr.client.user.allChildren(params);
try {
ChildUserCollection nextPage = vcr.client.user.getNextPage(collection, Fixtures.pageSize());
assertNotNull(nextPage);
String firstIdOfFirstPage = collection.getChildren().get(0).getId();
String firstIdOfSecondPage = nextPage.getChildren().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();
}
}
}