All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.cloudinary.test.AbstractSearchTest Maven / Gradle / Ivy
package com.cloudinary.test;
import com.cloudinary.Cloudinary;
import com.cloudinary.Search;
import com.cloudinary.utils.ObjectUtils;
import org.junit.*;
import org.junit.rules.TestName;
import java.lang.reflect.Field;
import java.util.*;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeNotNull;
@SuppressWarnings({"rawtypes", "unchecked", "JavaDoc"})
abstract public class AbstractSearchTest extends MockableTest {
@Rule
public TestName currentTest = new TestName();
private static final String SEARCH_TAG = "search_test_tag_" + SUFFIX;
public static final String[] UPLOAD_TAGS = {SDK_TEST_TAG, SEARCH_TAG};
private static final String SEARCH_TEST = "search_test_" + SUFFIX;
private static final String SEARCH_TEST_1 = SEARCH_TEST + "_1";
private static final String SEARCH_TEST_2 = SEARCH_TEST + "_2";
private static String SEARCH_TEST_ASSET_ID_1;
@BeforeClass
public static void setUpClass() throws Exception {
Cloudinary cloudinary = new Cloudinary();
Map options = ObjectUtils.asMap("public_id", SEARCH_TEST, "tags", UPLOAD_TAGS, "context", "stage=in_review");
cloudinary.api().deleteResourcesByTag(SEARCH_TAG, null);
cloudinary.uploader().upload(SRC_TEST_IMAGE, options);
options = ObjectUtils.asMap("public_id", SEARCH_TEST_1, "tags", UPLOAD_TAGS, "context", "stage=new");
SEARCH_TEST_ASSET_ID_1 = cloudinary.uploader().upload(SRC_TEST_IMAGE, options).get("asset_id").toString();
options = ObjectUtils.asMap("public_id", SEARCH_TEST_2, "tags", UPLOAD_TAGS, "context", "stage=validated");
cloudinary.uploader().upload(SRC_TEST_IMAGE, options);
try {
Thread.sleep(5000); //wait for search indexing
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@AfterClass
public static void tearDownClass() throws Exception {
Cloudinary cloudinary = new Cloudinary();
cloudinary.api().deleteResourcesByTag(SEARCH_TAG, null);
}
@Before
public void setUp() {
System.out.println("Running " + this.getClass().getName() + "." + currentTest.getMethodName());
this.cloudinary = new Cloudinary();
assumeNotNull(cloudinary.config.apiSecret);
}
@Test
public void shouldFindResourcesByTag() throws Exception {
Map result = cloudinary.search().expression(String.format("tags:%s", SEARCH_TAG)).execute();
List resources = (List) result.get("resources");
assertEquals(3, resources.size());
}
@Test
public void shouldFindResourceByPublicId() throws Exception {
Map result = cloudinary.search().expression(String.format("public_id:%s", SEARCH_TEST_1)).execute();
List resources = (List) result.get("resources");
assertEquals(1, resources.size());
}
@Test
public void shouldFindResourceByAssetId() throws Exception {
Map result = cloudinary.search().expression(String.format("asset_id:%s", SEARCH_TEST_ASSET_ID_1)).execute();
List resources = (List) result.get("resources");
assertEquals(1, resources.size());
}
@Test
public void testShouldNotDuplicateValues() throws Exception {
Search request = cloudinary.search().maxResults(1).
sortBy("created_at", "asc")
.sortBy("created_at", "desc")
.sortBy("public_id", "asc")
.aggregate("format")
.aggregate("format")
.aggregate("resource_type")
.withField("context")
.withField("context")
.withField("tags");
Field[] fields = Search.class.getDeclaredFields();
for(Field field : fields) {
if(field.getName() == "aggregateParam") {
field.setAccessible(true);
ArrayList aggregateList = (ArrayList) field.get(request);
Set testSet = new HashSet(aggregateList);
assertTrue(aggregateList.size() == testSet.size());
}
if (field.getName() == "withFieldParam") {
field.setAccessible(true);
ArrayList withFieldList = (ArrayList) field.get(request);
Set testSet = new HashSet(withFieldList);
assertTrue(withFieldList.size() == testSet.size());
}
if (field.getName() == "sortByParam") {
field.setAccessible(true);
ArrayList> sortByList = (ArrayList>) field.get(request);
Set> testSet = new HashSet>(sortByList);
assertTrue(sortByList.size() == testSet.size());
}
}
}
@Test
public void shouldPaginateResourcesLimitedByTagAndOrderdByAscendingPublicId() throws Exception {
List resources;
Map result = cloudinary.search().maxResults(1).expression(String.format("tags:%s", SEARCH_TAG)).sortBy("public_id", "asc").execute();
resources = (List) result.get("resources");
assertEquals(1, resources.size());
assertEquals(3, result.get("total_count"));
assertEquals(SEARCH_TEST, resources.get(0).get("public_id"));
result = cloudinary.search().maxResults(1).expression(String.format("tags:%s", SEARCH_TAG)).sortBy("public_id", "asc")
.nextCursor(ObjectUtils.asString(result.get("next_cursor"))).execute();
resources = (List) result.get("resources");
assertEquals(1, resources.size());
assertEquals(3, result.get("total_count"));
assertEquals(SEARCH_TEST_1, resources.get(0).get("public_id"));
result = cloudinary.search().maxResults(1).expression(String.format("tags:%s", SEARCH_TAG)).sortBy("public_id", "asc")
.nextCursor(ObjectUtils.asString(result.get("next_cursor"))).execute();
resources = (List) result.get("resources");
assertEquals(1, resources.size());
assertEquals(3, result.get("total_count"));
assertEquals(SEARCH_TEST_2, resources.get(0).get("public_id"));
assertNull(result.get("next_cursor"));
}
}