Skip to content

Commit a5e275e

Browse files
authored
Add Search folders functionality
1 parent be017b7 commit a5e275e

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public Search search() {
5959
return new Search(this);
6060
}
6161

62+
public SearchFolders searchFolders() {
63+
return new SearchFolders(this);
64+
}
65+
6266
public static void registerUploaderStrategy(String className) {
6367
if (!UPLOAD_STRATEGIES.contains(className)) {
6468
UPLOAD_STRATEGIES.add(className);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
public class Search {
1212

13-
private final Api api;
13+
protected final Api api;
1414
private ArrayList<HashMap<String, Object>> sortByParam;
1515
private ArrayList<String> aggregateParam;
1616
private ArrayList<String> withFieldParam;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.cloudinary;
2+
3+
import com.cloudinary.api.ApiResponse;
4+
import com.cloudinary.utils.ObjectUtils;
5+
6+
import java.util.Arrays;
7+
import java.util.Map;
8+
9+
public class SearchFolders extends Search {
10+
11+
public SearchFolders(Cloudinary cloudinary) {
12+
super(cloudinary);
13+
}
14+
15+
public ApiResponse execute() throws Exception {
16+
Map<String, String> options = ObjectUtils.asMap("content_type", "json");
17+
return this.api.callApi(Api.HttpMethod.POST, Arrays.asList("folders", "search"), this.toQuery(), options);
18+
}
19+
}

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractSearchTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import java.lang.reflect.Field;
1010
import java.util.*;
1111

12+
import static org.hamcrest.Matchers.hasEntry;
13+
import static org.hamcrest.Matchers.hasItem;
14+
import static org.hamcrest.core.AllOf.allOf;
1215
import static org.junit.Assert.*;
1316
import static org.junit.Assume.assumeNotNull;
1417

@@ -19,6 +22,7 @@ abstract public class AbstractSearchTest extends MockableTest {
1922
private static final String SEARCH_TAG = "search_test_tag_" + SUFFIX;
2023
public static final String[] UPLOAD_TAGS = {SDK_TEST_TAG, SEARCH_TAG};
2124
private static final String SEARCH_TEST = "search_test_" + SUFFIX;
25+
private static final String SEARCH_FOLDER = "search_folder_" + SUFFIX;
2226
private static final String SEARCH_TEST_1 = SEARCH_TEST + "_1";
2327
private static final String SEARCH_TEST_2 = SEARCH_TEST + "_2";
2428
private static String SEARCH_TEST_ASSET_ID_1;
@@ -44,6 +48,11 @@ public static void setUpClass() throws Exception {
4448
public static void tearDownClass() throws Exception {
4549
Cloudinary cloudinary = new Cloudinary();
4650
cloudinary.api().deleteResourcesByTag(SEARCH_TAG, null);
51+
try {
52+
cloudinary.api().deleteFolder(SEARCH_FOLDER, null);
53+
} catch (Exception e){
54+
System.err.println(e.getMessage());
55+
}
4756
}
4857

4958
@Before
@@ -60,6 +69,14 @@ public void shouldFindResourcesByTag() throws Exception {
6069
assertEquals(3, resources.size());
6170
}
6271

72+
@Test
73+
public void shouldFindFolders() throws Exception {
74+
cloudinary.api().createFolder(SEARCH_FOLDER, null);
75+
Map result = cloudinary.searchFolders().expression(String.format("name:%s", SEARCH_FOLDER)).execute();
76+
final List<Map> folders = (List) result.get("folders");
77+
assertThat(folders, hasItem(hasEntry("name", SEARCH_FOLDER)));
78+
}
79+
6380
@Test
6481
public void shouldFindResourceByPublicId() throws Exception {
6582
Map result = cloudinary.search().expression(String.format("public_id:%s", SEARCH_TEST_1)).execute();

0 commit comments

Comments
 (0)