55import com .cloudinary .api .ApiResponse ;
66import com .cloudinary .provisioning .Account ;
77import org .junit .*;
8+ import org .junit .rules .ExpectedException ;
89import org .junit .rules .TestName ;
910
1011import java .util .*;
@@ -29,6 +30,8 @@ public static void setUpClass() {
2930
3031 @ Rule
3132 public TestName currentTest = new TestName ();
33+ @ Rule
34+ public ExpectedException expectedException = ExpectedException .none ();
3235
3336 @ Before
3437 public void setUp () throws Exception {
@@ -180,6 +183,63 @@ public void testGetUsers() throws Exception {
180183 deleteUser (user2Id );
181184 }
182185
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+%[email protected] " ,
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+%[email protected] " ,
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+
183243 @ Test
184244 public void testCreateUser () throws Exception {
185245 ApiResponse createResult = createSubAccount ();
@@ -294,6 +354,7 @@ private ApiResponse createGroup() throws Exception {
294354 ApiResponse userGroup = account .createUserGroup (name );
295355 createdGroupIds .add (userGroup .get ("id" ).toString ());
296356 return userGroup ;
357+
297358 }
298359
299360 private ApiResponse createUser (Account .Role role ) throws Exception {
@@ -310,7 +371,11 @@ private ApiResponse createUser(List<String> subAccountsIds) throws Exception {
310371
311372 private ApiResponse createUser (List <String > subAccountsIds , Account .Role role ) throws Exception {
312373 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 );
314379 createdUserIds .add (user .get ("id" ).toString ());
315380 return user ;
316381 }
@@ -336,6 +401,7 @@ private static String randomLetters() {
336401 for (int i = 0 ; i < 10 ; i ++) {
337402 sb .append ((char ) ('a' + rand .nextInt ('z' - 'a' + 1 )));
338403 }
404+
339405 return sb .toString ();
340406 }
341407}
0 commit comments