Skip to content

Commit 349ec6d

Browse files
author
RTLcoil
authored
Add handling of "max_results"/"next_cursor" parameters (#203)
1 parent 84b9d5a commit 349ec6d

File tree

5 files changed

+118
-2
lines changed

5 files changed

+118
-2
lines changed

cloudinary-core/src/main/java/com/cloudinary/Api.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,17 @@ public ApiResponse createUploadPreset(Map options) throws Exception {
259259
public ApiResponse rootFolders(Map options) throws Exception {
260260
if (options == null)
261261
options = ObjectUtils.emptyMap();
262-
return callApi(HttpMethod.GET, Arrays.asList("folders"), ObjectUtils.emptyMap(), options);
262+
return callApi(HttpMethod.GET, Arrays.asList("folders"),
263+
extractParams(options, Arrays.asList("max_results", "next_cursor")),
264+
options);
263265
}
264266

265267
public ApiResponse subFolders(String ofFolderPath, Map options) throws Exception {
266268
if (options == null)
267269
options = ObjectUtils.emptyMap();
268-
return callApi(HttpMethod.GET, Arrays.asList("folders", ofFolderPath), ObjectUtils.emptyMap(), options);
270+
return callApi(HttpMethod.GET, Arrays.asList("folders", ofFolderPath),
271+
extractParams(options, Arrays.asList("max_results", "next_cursor")),
272+
options);
269273
}
270274

271275
//Creates an empty folder
@@ -659,4 +663,16 @@ public ApiResponse deleteMetadataField(String fieldExternalId) throws Exception
659663
List<String> uri = Arrays.asList("metadata_fields", fieldExternalId);
660664
return callApi(HttpMethod.DELETE, uri, Collections.<String, Object>emptyMap(), Collections.emptyMap());
661665
}
666+
667+
private Map<String, ?> extractParams(Map options, List<String> keys) {
668+
Map<String, Object> result = new HashMap<String, Object>();
669+
for (String key : keys) {
670+
Object option = options.get(key);
671+
672+
if (option != null) {
673+
result.put(key, option);
674+
}
675+
}
676+
return result;
677+
}
662678
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.cloudinary.test;
2+
3+
public class FoldersApiTest extends AbstractFoldersApiTest {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.cloudinary.test;
2+
3+
public class FoldersApiTest extends AbstractFoldersApiTest {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.cloudinary.test;
2+
3+
public class FoldersApiTest extends AbstractFoldersApiTest {
4+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.cloudinary.test;
2+
3+
import com.cloudinary.Api;
4+
import com.cloudinary.Cloudinary;
5+
import com.cloudinary.api.ApiResponse;
6+
import com.cloudinary.utils.ObjectUtils;
7+
import org.junit.Before;
8+
import org.junit.Rule;
9+
import org.junit.Test;
10+
import org.junit.rules.TestName;
11+
12+
import java.util.List;
13+
14+
import static org.junit.Assert.*;
15+
import static org.junit.Assume.assumeNotNull;
16+
17+
@SuppressWarnings({"rawtypes"})
18+
abstract public class AbstractFoldersApiTest extends MockableTest {
19+
protected Api api;
20+
21+
@Rule
22+
public TestName currentTest = new TestName();
23+
24+
@Before
25+
public void setUp() {
26+
System.out.println("Running " + this.getClass().getName() + "." + currentTest.getMethodName());
27+
this.cloudinary = new Cloudinary();
28+
assumeNotNull(cloudinary.config.apiSecret);
29+
this.api = cloudinary.api();
30+
}
31+
32+
@Test
33+
public void testRootFolderWithParams() throws Exception {
34+
String rootFolder1Name = "rootFolderWithParamsTest1" + SUFFIX;
35+
assertTrue((Boolean) api.createFolder(rootFolder1Name, null).get("success"));
36+
37+
String rootFolder2Name = "rootFolderWithParamsTest2" + SUFFIX;
38+
assertTrue((Boolean) api.createFolder(rootFolder2Name, null).get("success"));
39+
40+
Thread.sleep(500);
41+
42+
ApiResponse rootResponse1 = api.rootFolders(ObjectUtils.asMap("max_results", 1));
43+
List rootFolders1 = (List) rootResponse1.get("folders");
44+
assertNotNull(rootFolders1);
45+
assertEquals(1, rootFolders1.size());
46+
47+
String nextCursor = (String) rootResponse1.get("next_cursor");
48+
assertNotNull(nextCursor);
49+
50+
ApiResponse rootResponse2 = api.rootFolders(ObjectUtils.asMap("max_results", 1, "next_cursor", nextCursor));
51+
List folders2 = (List) rootResponse2.get("folders");
52+
assertNotNull(folders2);
53+
assertEquals(1, folders2.size());
54+
55+
assertTrue(((List) api.deleteFolder(rootFolder1Name, null).get("deleted")).contains(rootFolder1Name));
56+
assertTrue(((List) api.deleteFolder(rootFolder2Name, null).get("deleted")).contains(rootFolder2Name));
57+
}
58+
59+
@Test
60+
public void testSubFolderWithParams() throws Exception {
61+
String rootFolderName = "subfolderWithParamsTest" + SUFFIX;
62+
assertTrue((Boolean) api.createFolder(rootFolderName, null).get("success"));
63+
64+
String subFolder1Name = rootFolderName + "/subfolder1" + SUFFIX;
65+
assertTrue((Boolean) api.createFolder(subFolder1Name, null).get("success"));
66+
67+
String subFolder2Name = rootFolderName + "/subfolder2" + SUFFIX;
68+
assertTrue((Boolean) api.createFolder(subFolder2Name, null).get("success"));
69+
70+
Thread.sleep(500);
71+
72+
ApiResponse response = api.subFolders(rootFolderName, ObjectUtils.asMap("max_results", 1));
73+
List folders = (List) response.get("folders");
74+
assertNotNull(folders);
75+
assertEquals(1, folders.size());
76+
77+
String nextCursor = (String) response.get("next_cursor");
78+
assertNotNull(nextCursor);
79+
80+
ApiResponse response2 = api.subFolders(rootFolderName, ObjectUtils.asMap("max_results", 1, "next_cursor", nextCursor));
81+
List folders2 = (List) response2.get("folders");
82+
assertNotNull(folders2);
83+
assertEquals(1, folders2.size());
84+
85+
ApiResponse result = api.deleteFolder(rootFolderName, null);
86+
assertTrue(((List) result.get("deleted")).contains(rootFolderName));
87+
}
88+
}

0 commit comments

Comments
 (0)