@BeforeClass public static void setUpClass() throws IOException { Cloudinary cloudinary = new Cloudinary(); if (cloudinary.getStringConfig("api_secret") == null) { System.err.println("Please setup environment for Upload test to run"); return; } Api api = cloudinary.api(); try { api.deleteResources( Arrays.asList("api_test", "api_test1", "api_test2", "api_test3", "api_test5"), Cloudinary.emptyMap()); } catch (Exception e) { } try { api.deleteTransformation("api_test_transformation", Cloudinary.emptyMap()); } catch (Exception e) { } try { api.deleteTransformation("api_test_transformation2", Cloudinary.emptyMap()); } catch (Exception e) { } try { api.deleteTransformation("api_test_transformation3", Cloudinary.emptyMap()); } catch (Exception e) { } try { api.deleteUploadPreset("api_test_upload_preset", Cloudinary.emptyMap()); } catch (Exception e) { } try { api.deleteUploadPreset("api_test_upload_preset2", Cloudinary.emptyMap()); } catch (Exception e) { } try { api.deleteUploadPreset("api_test_upload_preset3", Cloudinary.emptyMap()); } catch (Exception e) { } try { api.deleteUploadPreset("api_test_upload_preset4", Cloudinary.emptyMap()); } catch (Exception e) { } Map options = Cloudinary.asMap( "public_id", "api_test", "tags", new String[] {"api_test_tag", uniqueTag}, "context", "key=value", "eager", Collections.singletonList(new Transformation().width(100).crop("scale"))); cloudinary.uploader().upload("src/test/resources/logo.png", options); options.put("public_id", "api_test1"); cloudinary.uploader().upload("src/test/resources/logo.png", options); }
@Test public void testListByModerationUpdate() throws Exception { // "should support listing by moderation kind and value Map result1 = cloudinary .uploader() .upload("src/test/resources/logo.png", Cloudinary.asMap("moderation", "manual")); Map result2 = cloudinary .uploader() .upload("src/test/resources/logo.png", Cloudinary.asMap("moderation", "manual")); Map result3 = cloudinary .uploader() .upload("src/test/resources/logo.png", Cloudinary.asMap("moderation", "manual")); api.update( (String) result1.get("public_id"), Cloudinary.asMap("moderation_status", "approved")); api.update( (String) result2.get("public_id"), Cloudinary.asMap("moderation_status", "rejected")); Map approved = api.resourcesByModeration("manual", "approved", Cloudinary.asMap("max_results", 1000)); Map rejected = api.resourcesByModeration("manual", "rejected", Cloudinary.asMap("max_results", 1000)); Map pending = api.resourcesByModeration("manual", "pending", Cloudinary.asMap("max_results", 1000)); assertNotNull( findByAttr( (List<Map>) approved.get("resources"), "public_id", (String) result1.get("public_id"))); assertNull( findByAttr( (List<Map>) approved.get("resources"), "public_id", (String) result2.get("public_id"))); assertNull( findByAttr( (List<Map>) approved.get("resources"), "public_id", (String) result2.get("public_id"))); assertNotNull( findByAttr( (List<Map>) rejected.get("resources"), "public_id", (String) result2.get("public_id"))); assertNull( findByAttr( (List<Map>) rejected.get("resources"), "public_id", (String) result1.get("public_id"))); assertNull( findByAttr( (List<Map>) rejected.get("resources"), "public_id", (String) result3.get("public_id"))); assertNotNull( findByAttr( (List<Map>) pending.get("resources"), "public_id", (String) result3.get("public_id"))); assertNull( findByAttr( (List<Map>) pending.get("resources"), "public_id", (String) result1.get("public_id"))); assertNull( findByAttr( (List<Map>) pending.get("resources"), "public_id", (String) result2.get("public_id"))); }
@Test 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/resources/logo.png", Cloudinary.emptyMap()); cloudinary .api() .update( uploadResult.get("public_id").toString(), Cloudinary.asMap("custom_coordinates", coordinates)); Map result = cloudinary .api() .resource( uploadResult.get("public_id").toString(), Cloudinary.asMap("coordinates", true)); long[] expected = new long[] {121L, 31L, 110L, 151L}; Object[] actual = ((org.json.simple.JSONArray) ((org.json.simple.JSONArray) ((Map) result.get("coordinates")).get("custom")) .get(0)) .toArray(); for (int i = 0; i < expected.length; i++) { assertEquals(expected[i], actual[i]); } }
@Test(expected = Api.NotFound.class) public void test09aDeleteResourcesByPrefix() throws Exception { // should allow deleting resources cloudinary .uploader() .upload("src/test/resources/logo.png", Cloudinary.asMap("public_id", "api_test_by_prefix")); Map resource = api.resource("api_test_by_prefix", Cloudinary.emptyMap()); assertNotNull(resource); api.deleteResourcesByPrefix("api_test_by", Cloudinary.emptyMap()); api.resource("api_test_by_prefix", Cloudinary.emptyMap()); }
@Test public void testDetectionUpdate() { // should support requesting detection try { Map uploadResult = cloudinary.uploader().upload("src/test/resources/logo.png", Cloudinary.emptyMap()); api.update((String) uploadResult.get("public_id"), Cloudinary.asMap("detection", "illegal")); } catch (Exception e) { assertTrue(e instanceof com.cloudinary.Api.BadRequest); assertTrue(e.getMessage().matches("^Illegal value(.*)")); } }
@Test(expected = Api.NotFound.class) public void test09DeleteResources() throws Exception { // should allow deleting resources cloudinary .uploader() .upload("src/test/resources/logo.png", Cloudinary.asMap("public_id", "api_test3")); Map resource = api.resource("api_test3", Cloudinary.emptyMap()); assertNotNull(resource); api.deleteResources( Arrays.asList("apit_test", "api_test2", "api_test3"), Cloudinary.emptyMap()); api.resource("api_test3", Cloudinary.emptyMap()); }
@Test public void testManualModeration() throws Exception { // should support setting manual moderation status Map uploadResult = cloudinary .uploader() .upload("src/test/resources/logo.png", Cloudinary.asMap("moderation", "manual")); Map apiResult = api.update( (String) uploadResult.get("public_id"), Cloudinary.asMap("moderation_status", "approved")); assertEquals( "approved", ((Map) ((List<Map>) apiResult.get("moderation")).get(0)).get("status")); }
@Test public void testResourcesListingStartAt() throws Exception { // should allow listing resources by start date - make sure your clock is set correctly!!! Thread.sleep(2000L); java.util.Date startAt = new java.util.Date(); Thread.sleep(2000L); Map response = cloudinary.uploader().upload("src/test/resources/logo.png", Cloudinary.emptyMap()); List<Map> resources = (List<Map>) api.resources( Cloudinary.asMap("type", "upload", "start_at", startAt, "direction", "asc")) .get("resources"); assertEquals(response.get("public_id"), resources.get(0).get("public_id")); }
// This test must be last because it deletes (potentially) all dependent transformations which // some tests rely on. // Add @Test if you really want to test it - This test deletes derived resources! public void testDeleteAllResources() throws Exception { // should allow deleting all resources cloudinary .uploader() .upload( "src/test/resources/logo.png", Cloudinary.asMap( "public_id", "api_test5", "eager", Collections.singletonList(new Transformation().crop("scale").width(2.0)))); Map result = api.resource("api_test5", Cloudinary.emptyMap()); assertEquals(1, ((org.json.simple.JSONArray) result.get("derived")).size()); api.deleteAllResources(Cloudinary.asMap("keep_original", true)); result = api.resource("api_test5", Cloudinary.emptyMap()); // assertEquals(0, ((org.json.simple.JSONArray) result.get("derived")).size()); }
@Test public void test08DeleteDerived() throws Exception { // should allow deleting derived resource cloudinary .uploader() .upload( "src/test/resources/logo.png", Cloudinary.asMap( "public_id", "api_test3", "eager", Collections.singletonList(new Transformation().width(101).crop("scale")))); Map resource = api.resource("api_test3", Cloudinary.emptyMap()); assertNotNull(resource); List<Map> derived = (List<Map>) resource.get("derived"); assertEquals(derived.size(), 1); String derived_resource_id = (String) derived.get(0).get("id"); api.deleteDerivedResources(Arrays.asList(derived_resource_id), Cloudinary.emptyMap()); resource = api.resource("api_test3", Cloudinary.emptyMap()); assertNotNull(resource); derived = (List<Map>) resource.get("derived"); assertEquals(derived.size(), 0); }