From f11f43fffaf38b4637c3009453cfdd902ff885fb Mon Sep 17 00:00:00 2001 From: Amir Tocker Date: Wed, 8 Feb 2017 00:39:05 +0200 Subject: [PATCH] Fix listing direction test. --- .../com/cloudinary/test/AbstractApiTest.java | 62 +++++++++++-------- .../cloudinary/test/AbstractUploaderTest.java | 4 ++ 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java b/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java index 3176e980..1ea4b207 100644 --- a/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java +++ b/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java @@ -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 @@ -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 { @@ -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 resources = (List) result.get("resources"); - result = api.resourcesByTag(uniqueTag, ObjectUtils.asMap("type", "upload", "direction", -1)); + ArrayList resourceIds = new ArrayList(); + 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 resourcesDesc = (List) result.get("resources"); - Collections.reverse(resources); - assertEquals(resources, resourcesDesc); + ArrayList resourceIdsDesc = new ArrayList(); + for (Map resource : resourcesDesc) { + resourceIdsDesc.add((String) resource.get("public_id")); + } + Collections.reverse(resourceIds); + assertEquals(resourceIds, resourceIdsDesc); } @Ignore @@ -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 resources = (List) listResources.get("resources"); assertEquals(response.get("public_id"), resources.get(0).get("public_id")); @@ -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 derived = (List) resource.get("derived"); @@ -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()); @@ -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()); @@ -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)); @@ -446,8 +456,8 @@ 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) apiResult.get("moderation")).get(0)).get("status")); } @@ -455,7 +465,7 @@ public void testManualModeration() throws Exception { 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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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}; @@ -601,9 +611,9 @@ public void testListByModerationUpdate() throws Exception { // "should support listing by moderation kind and value List 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)); @@ -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")); @@ -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()); diff --git a/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractUploaderTest.java b/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractUploaderTest.java index cf9068fb..2fb82f67 100644 --- a/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractUploaderTest.java +++ b/cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractUploaderTest.java @@ -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