Skip to content

Commit

Permalink
Fix listing direction test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir Tocker committed Feb 7, 2017
1 parent da35b2b commit f11f43f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ abstract public class AbstractApiTest extends MockableTest {
public static final String API_TEST_UPLOAD_PRESET_2 = API_TEST_UPLOAD_PRESET + "2";
public static final String API_TEST_UPLOAD_PRESET_3 = API_TEST_UPLOAD_PRESET + "3";
public static final String API_TEST_UPLOAD_PRESET_4 = API_TEST_UPLOAD_PRESET + "4";
public static final String[] UPLOAD_TAGS = {SDK_TEST_TAG, uniqueTag};
protected Api api;

@BeforeClass
Expand All @@ -61,7 +62,8 @@ public static void setUpClass() throws IOException {
public static void tearDownClass() {
Api api = MockableTest.cleanUp();
try {
api.deleteResources(Arrays.asList(API_TEST, API_TEST_1, API_TEST_2, API_TEST_3, API_TEST_5), ObjectUtils.emptyMap());
// api.deleteResources(Arrays.asList(API_TEST, API_TEST_1, API_TEST_2, API_TEST_3, API_TEST_5), ObjectUtils.emptyMap());
api.deleteResourcesByTag(uniqueTag, ObjectUtils.emptyMap());
} catch (Exception ignored) {
}
try {
Expand Down Expand Up @@ -183,12 +185,20 @@ public void test05ResourcesByPrefix() throws Exception {
@Test
public void testResourcesListingDirection() throws Exception {
// should allow listing resources in both directions
Map result = api.resourcesByTag(uniqueTag, ObjectUtils.asMap("type", "upload", "direction", "asc"));
Map result = api.resourcesByTag(uniqueTag, ObjectUtils.asMap("type", "upload", "direction", "asc", "max_results", 500));
List<Map> resources = (List<Map>) result.get("resources");
result = api.resourcesByTag(uniqueTag, ObjectUtils.asMap("type", "upload", "direction", -1));
ArrayList<String> resourceIds = new ArrayList<String>();
for (Map resource : resources) {
resourceIds.add((String) resource.get("public_id"));
}
result = api.resourcesByTag(uniqueTag, ObjectUtils.asMap("type", "upload", "direction", -1, "max_results", 500));
List<Map> resourcesDesc = (List<Map>) result.get("resources");
Collections.reverse(resources);
assertEquals(resources, resourcesDesc);
ArrayList<String> resourceIdsDesc = new ArrayList<String>();
for (Map resource : resourcesDesc) {
resourceIdsDesc.add((String) resource.get("public_id"));
}
Collections.reverse(resourceIds);
assertEquals(resourceIds, resourceIdsDesc);
}

@Ignore
Expand All @@ -198,7 +208,7 @@ public void testResourcesListingStartAt() throws Exception {
Thread.sleep(2000L);
java.util.Date startAt = new java.util.Date();
Thread.sleep(2000L);
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", SDK_TEST_TAG));
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", UPLOAD_TAGS));
ApiResponse listResources = api.resources(ObjectUtils.asMap("type", "upload", "start_at", startAt, "direction", "asc"));
List<Map> resources = (List<Map>) listResources.get("resources");
assertEquals(response.get("public_id"), resources.get(0).get("public_id"));
Expand Down Expand Up @@ -252,7 +262,7 @@ public void test07ResourceMetadata() throws Exception {
public void test08DeleteDerived() throws Exception {
// should allow deleting derived resource
cloudinary.uploader().upload(SRC_TEST_IMAGE,
ObjectUtils.asMap("public_id", API_TEST_3, "tags", SDK_TEST_TAG, "eager", Collections.singletonList(new Transformation().width(101).crop("scale"))));
ObjectUtils.asMap("public_id", API_TEST_3, "tags", UPLOAD_TAGS, "eager", Collections.singletonList(new Transformation().width(101).crop("scale"))));
Map resource = api.resource(API_TEST_3, ObjectUtils.emptyMap());
assertNotNull(resource);
List<Map> derived = (List<Map>) resource.get("derived");
Expand All @@ -269,7 +279,7 @@ public void test08DeleteDerived() throws Exception {
public void test09DeleteResources() throws Exception {
// should allow deleting resources
String public_id = "api_,test3";
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("public_id", public_id, "tags", SDK_TEST_TAG));
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("public_id", public_id, "tags", UPLOAD_TAGS));
Map resource = api.resource(public_id, ObjectUtils.emptyMap());
assertNotNull(resource);
api.deleteResources(Arrays.asList(API_TEST_2, public_id), ObjectUtils.emptyMap());
Expand All @@ -279,7 +289,7 @@ public void test09DeleteResources() throws Exception {
@Test(expected = NotFound.class)
public void test09aDeleteResourcesByPrefix() throws Exception {
// should allow deleting resources
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("public_id", "api_test_by_prefix", "tags", SDK_TEST_TAG));
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("public_id", "api_test_by_prefix", "tags", UPLOAD_TAGS));
Map resource = api.resource("api_test_by_prefix", ObjectUtils.emptyMap());
assertNotNull(resource);
api.deleteResourcesByPrefix("api_test_by", ObjectUtils.emptyMap());
Expand Down Expand Up @@ -434,7 +444,7 @@ public void test19Ping() throws Exception {
public void testDeleteAllResources() throws Exception {
// should allow deleting all resources
cloudinary.uploader().upload(SRC_TEST_IMAGE,
ObjectUtils.asMap("public_id", API_TEST_5, "tags", SDK_TEST_TAG, "eager", Collections.singletonList(new Transformation().crop("scale").width(2.0))));
ObjectUtils.asMap("public_id", API_TEST_5, "tags", UPLOAD_TAGS, "eager", Collections.singletonList(new Transformation().crop("scale").width(2.0))));
Map result = api.resource(API_TEST_5, ObjectUtils.emptyMap());
assertEquals(1, ((org.cloudinary.json.JSONArray) result.get("derived")).length());
api.deleteAllResources(ObjectUtils.asMap("keep_original", true));
Expand All @@ -446,16 +456,16 @@ public void testDeleteAllResources() throws Exception {
@Test
public void testManualModeration() throws Exception {
// should support setting manual moderation status
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("moderation", "manual", "tags", SDK_TEST_TAG));
Map apiResult = api.update((String) uploadResult.get("public_id"), ObjectUtils.asMap("moderation_status", "approved", "tags", SDK_TEST_TAG));
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("moderation", "manual", "tags", UPLOAD_TAGS));
Map apiResult = api.update((String) uploadResult.get("public_id"), ObjectUtils.asMap("moderation_status", "approved", "tags", UPLOAD_TAGS));
assertEquals("approved", ((Map) ((List<Map>) apiResult.get("moderation")).get(0)).get("status"));
}

@Test
public void testOcrUpdate() {
// should support requesting ocr info
try {
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap( "tags", SDK_TEST_TAG));
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap( "tags", UPLOAD_TAGS));
api.update((String) uploadResult.get("public_id"), ObjectUtils.asMap("ocr", "illegal"));
} catch (Exception e) {
assertTrue(e instanceof BadRequest);
Expand All @@ -467,7 +477,7 @@ public void testOcrUpdate() {
public void testRawConvertUpdate() {
// should support requesting raw conversion
try {
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap( "tags", SDK_TEST_TAG));
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap( "tags", UPLOAD_TAGS));
api.update((String) uploadResult.get("public_id"), ObjectUtils.asMap("raw_convert", "illegal"));
} catch (Exception e) {
assertTrue(e instanceof BadRequest);
Expand All @@ -479,7 +489,7 @@ public void testRawConvertUpdate() {
public void testCategorizationUpdate() {
// should support requesting categorization
try {
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap( "tags", SDK_TEST_TAG));
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap( "tags", UPLOAD_TAGS));
api.update((String) uploadResult.get("public_id"), ObjectUtils.asMap("categorization", "illegal"));
} catch (Exception e) {
assertTrue(e instanceof BadRequest);
Expand All @@ -491,7 +501,7 @@ public void testCategorizationUpdate() {
public void testDetectionUpdate() {
// should support requesting detection
try {
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap( "tags", SDK_TEST_TAG));
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap( "tags", UPLOAD_TAGS));
api.update((String) uploadResult.get("public_id"), ObjectUtils.asMap("detection", "illegal"));
} catch (Exception e) {
assertTrue(e instanceof BadRequest);
Expand All @@ -503,7 +513,7 @@ public void testDetectionUpdate() {
public void testSimilaritySearchUpdate() {
// should support requesting similarity search
try {
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap( "tags", SDK_TEST_TAG));
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap( "tags", UPLOAD_TAGS));
api.update((String) uploadResult.get("public_id"), ObjectUtils.asMap("similarity_search", "illegal"));
} catch (Exception e) {
assertTrue(e instanceof BadRequest);
Expand All @@ -515,7 +525,7 @@ public void testSimilaritySearchUpdate() {
public void testUpdateCustomCoordinates() throws IOException, Exception {
// should update custom coordinates
Coordinates coordinates = new Coordinates("121,31,110,151");
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap( "tags", SDK_TEST_TAG));
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap( "tags", UPLOAD_TAGS));
cloudinary.api().update(uploadResult.get("public_id").toString(), ObjectUtils.asMap("custom_coordinates", coordinates));
Map result = cloudinary.api().resource(uploadResult.get("public_id").toString(), ObjectUtils.asMap("coordinates", true));
int[] expected = new int[]{121, 31, 110, 151};
Expand Down Expand Up @@ -601,9 +611,9 @@ public void testListByModerationUpdate() throws Exception {
// "should support listing by moderation kind and value
List<Map> resources;

Map result1 = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("moderation", "manual", "tags", SDK_TEST_TAG));
Map result2 = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("moderation", "manual", "tags", SDK_TEST_TAG));
Map result3 = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("moderation", "manual", "tags", SDK_TEST_TAG));
Map result1 = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("moderation", "manual", "tags", UPLOAD_TAGS));
Map result2 = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("moderation", "manual", "tags", UPLOAD_TAGS));
Map result3 = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("moderation", "manual", "tags", UPLOAD_TAGS));
api.update((String) result1.get("public_id"), ObjectUtils.asMap("moderation_status", "approved"));
api.update((String) result2.get("public_id"), ObjectUtils.asMap("moderation_status", "rejected"));
Map approved = api.resourcesByModeration("manual", "approved", ObjectUtils.asMap("max_results", 1000));
Expand Down Expand Up @@ -632,10 +642,10 @@ public void testListByModerationUpdate() throws Exception {
// @Test
public void testFolderApi() throws Exception {
// should allow deleting all resources
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("public_id", "test_folder1/item", "tags", SDK_TEST_TAG));
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("public_id", "test_folder2/item", "tags", SDK_TEST_TAG));
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("public_id", "test_folder1/test_subfolder1/item", "tags", SDK_TEST_TAG));
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("public_id", "test_folder1/test_subfolder2/item", "tags", SDK_TEST_TAG));
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("public_id", "test_folder1/item", "tags", UPLOAD_TAGS));
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("public_id", "test_folder2/item", "tags", UPLOAD_TAGS));
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("public_id", "test_folder1/test_subfolder1/item", "tags", UPLOAD_TAGS));
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("public_id", "test_folder1/test_subfolder2/item", "tags", UPLOAD_TAGS));
Map result = api.rootFolders(null);
assertEquals("test_folder1", ((Map) ((org.cloudinary.json.JSONArray) result.get("folders")).get(0)).get("name"));
assertEquals("test_folder2", ((Map) ((org.cloudinary.json.JSONArray) result.get("folders")).get(1)).get("name"));
Expand All @@ -654,7 +664,7 @@ public void testFolderApi() throws Exception {
public void testRestore() throws Exception {
// should support restoring resources
cloudinary.uploader().upload(SRC_TEST_IMAGE,
ObjectUtils.asMap("public_id", "api_test_restore", "backup", true, "tags", SDK_TEST_TAG));
ObjectUtils.asMap("public_id", "api_test_restore", "backup", true, "tags", UPLOAD_TAGS));
Map resource = api.resource("api_test_restore", ObjectUtils.emptyMap());
assertEquals(resource.get("bytes"), 3381);
api.deleteResources(Collections.singletonList("api_test_restore"), ObjectUtils.emptyMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public static void tearDownClass() {
api.deleteResourcesByTag(ARCHIVE_TAG, ObjectUtils.emptyMap());
} catch (Exception ignored) {
}
try {
api.deleteResourcesByTag(SDK_TEST_TAG, ObjectUtils.emptyMap());
} catch (Exception ignored) {
}
}

@Rule
Expand Down

0 comments on commit f11f43f

Please sign in to comment.