5
5
import com .cloudinary .api .ApiResponse ;
6
6
import com .cloudinary .provisioning .Account ;
7
7
import org .junit .*;
8
+ import org .junit .rules .ExpectedException ;
8
9
import org .junit .rules .TestName ;
9
10
10
11
import java .util .*;
@@ -29,6 +30,8 @@ public static void setUpClass() {
29
30
30
31
@ Rule
31
32
public TestName currentTest = new TestName ();
33
+ @ Rule
34
+ public ExpectedException expectedException = ExpectedException .none ();
32
35
33
36
@ Before
34
37
public void setUp () throws Exception {
@@ -180,6 +183,63 @@ public void testGetUsers() throws Exception {
180
183
deleteUser (user2Id );
181
184
}
182
185
186
+ @ Test
187
+ public void testGetPendingUsers () throws Exception {
188
+ String id = createUser (Account .Role .BILLING ).get ("id" ).toString ();
189
+
190
+ ApiResponse pending = account .users (true , Collections .singletonList (id ), null , null , null );
191
+ assertEquals (1 , ((ArrayList ) pending .get ("users" )).size ());
192
+
193
+ ApiResponse notPending = account .users (false , Collections .singletonList (id ), null , null , null );
194
+ assertEquals (0 , ((ArrayList ) notPending .get ("users" )).size ());
195
+
196
+ ApiResponse all = account .users (null , Collections .singletonList (id ), null , null , null );
197
+ assertEquals (1 , ((ArrayList ) all .get ("users" )).size ());
198
+ }
199
+
200
+ @ Test
201
+ public void testGetUsersByPrefix () throws Exception {
202
+ final long timeMillis = System .currentTimeMillis ();
203
+ final String userName = String .format ("SDK TEST Get Users By Prefix %d" , timeMillis );
204
+ final String userEmail = String .format ("sdk-test-get-users-by-prefix+%d@cloudinary.com" , timeMillis );
205
+
206
+ createUser (userName ,
207
+ userEmail ,
208
+ Account .Role .BILLING ,
209
+ Collections .<String >emptyList ());
210
+
211
+ ApiResponse userByPrefix = account .users (true , null , userName .substring (0 , userName .length () - 1 ), null , null );
212
+ assertEquals (1 , ((ArrayList ) userByPrefix .get ("users" )).size ());
213
+
214
+ ApiResponse userByNonExistingPrefix = account .users (true , null , userName + "zzz" , null , null );
215
+ assertEquals (0 , ((ArrayList ) userByNonExistingPrefix .get ("users" )).size ());
216
+ }
217
+
218
+ @ Test
219
+ public void testGetUsersBySubAccountIds () throws Exception {
220
+ ApiResponse subAccount = createSubAccount ();
221
+ final String subAccountId = subAccount .get ("id" ).toString ();
222
+
223
+ final long timeMillis = System .currentTimeMillis ();
224
+ final String userName = String .format ("SDK TEST Get Users By Sub Account Ids %d" , timeMillis );
225
+ final String userEmail = String .format ("sdk-test-get-users-by-sub-account-ids+%d@cloudinary.com" , timeMillis );
226
+
227
+ createUser (userName ,
228
+ userEmail ,
229
+ Account .Role .BILLING ,
230
+ Collections .singletonList (subAccountId ));
231
+
232
+ ApiResponse usersBySubAccount = account .users (true , null , userName , subAccountId , null );
233
+ assertEquals (1 , ((ArrayList ) usersBySubAccount .get ("users" )).size ());
234
+ }
235
+
236
+ @ Test
237
+ public void testGetUsersThrowsWhenSubAccountIdDoesntExist () throws Exception {
238
+ final String subAccountId = randomLetters ();
239
+ expectedException .expectMessage ("Cannot find sub account with id " + subAccountId );
240
+ account .users (true , null , null , subAccountId , null );
241
+ }
242
+
183
243
@ Test
184
244
public void testCreateUser () throws Exception {
185
245
ApiResponse createResult = createSubAccount ();
@@ -294,6 +354,7 @@ private ApiResponse createGroup() throws Exception {
294
354
ApiResponse userGroup = account .createUserGroup (name );
295
355
createdGroupIds .add (userGroup .get ("id" ).toString ());
296
356
return userGroup ;
357
+
297
358
}
298
359
299
360
private ApiResponse createUser (Account .Role role ) throws Exception {
@@ -310,7 +371,11 @@ private ApiResponse createUser(List<String> subAccountsIds) throws Exception {
310
371
311
372
private ApiResponse createUser (List <String > subAccountsIds , Account .Role role ) throws Exception {
312
373
String email = String .format ("%s@%s.com" , randomLetters (), randomLetters ());
313
- ApiResponse user = account .createUser ("TestUserJava" +new Date ().toString (), email , role , subAccountsIds , null );
374
+ return createUser ("TestName" , email , role , subAccountsIds );
375
+ }
376
+
377
+ private ApiResponse createUser (final String name , String email , Account .Role role , List <String > subAccountsIds ) throws Exception {
378
+ ApiResponse user = account .createUser (name , email , role , subAccountsIds , null );
314
379
createdUserIds .add (user .get ("id" ).toString ());
315
380
return user ;
316
381
}
@@ -336,6 +401,7 @@ private static String randomLetters() {
336
401
for (int i = 0 ; i < 10 ; i ++) {
337
402
sb .append ((char ) ('a' + rand .nextInt ('z' - 'a' + 1 )));
338
403
}
404
+
339
405
return sb .toString ();
340
406
}
341
407
}
0 commit comments