@Test public void testExplicit() throws IOException { Map result = cloudinary.uploader().explicit("sample", asMap("eager", Collections.singletonList(new Transformation().crop("scale").width(2.0)), "type", "upload", "moderation", "manual")); String url = cloudinary.url().transformation(new Transformation().crop("scale").width(2.0)).format("jpg").version(result.get("version")).generate("sample"); String eagerUrl = (String) ((Map) ((List) result.get("eager")).get(0)).get("url"); String cloudName = cloudinary.config.cloudName; assertEquals(eagerUrl.substring(eagerUrl.indexOf(cloudName)), url.substring(url.indexOf(cloudName))); }
@Test public void testUploadAsync() throws IOException { Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, asMap("transformation", new Transformation().crop("scale").width(2.0), "async", true, "tags", Arrays.asList(SDK_TEST_TAG, UPLOADER_TAG))); assertEquals((String) result.get("status"), "pending"); }
@Test public void test12Transformations() throws Exception { // should allow listing transformations final Transformation listTest = new Transformation().width(25).crop("scale").overlay(new TextLayer().text(SUFFIX + "_testListTransformations").fontFamily("Arial").fontSize(60)); preloadResource(ObjectUtils.asMap("tags", UPLOAD_TAGS, "eager", Collections.singletonList(listTest))); Map result = api.transformations(ObjectUtils.asMap("max_results", 500)); Map transformation = findByAttr((List<Map>) result.get("transformations"), "name", listTest.generate()); assertNotNull(transformation); assertTrue((Boolean) transformation.get("used")); }
public void testDeleteAllResources() throws Exception { // should allow deleting all resources cloudinary.uploader().upload(SRC_TEST_IMAGE, 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)); result = api.resource(API_TEST_5, ObjectUtils.emptyMap()); // assertEquals(0, ((org.cloudinary.json.JSONArray) // result.get("derived")).size()); }
@BeforeClass public static void setUpClass() throws IOException { Cloudinary cloudinary = new Cloudinary(); if (cloudinary.config.apiSecret == null) { System.err.println("Please setup environment for Upload test to run"); } cloudinary.uploader().upload(SRC_TEST_IMAGE, asMap("tags", new String[]{SDK_TEST_TAG, UPLOADER_TAG, ARCHIVE_TAG})); cloudinary.uploader().upload(SRC_TEST_IMAGE, asMap("tags", new String[]{SDK_TEST_TAG, UPLOADER_TAG, ARCHIVE_TAG}, "resource_type", "raw")); cloudinary.uploader().upload(SRC_TEST_IMAGE, asMap("tags", new String[]{SDK_TEST_TAG, UPLOADER_TAG, ARCHIVE_TAG}, "transformation", new Transformation().crop("scale").width(10))); }
@Test public void testCreate() throws Exception { ApiResponse result = api.createStreamingProfile(CREATE_PROFILE_NAME, null, Collections.singletonList(ObjectUtils.asMap( "transformation", new Transformation().crop("limit").width(1200).height(1200).bitRate("5m") )), ObjectUtils.emptyMap()); assertTrue(result.containsKey("data")); Map profile = (Map) result.get("data"); assertThat(profile, (Matcher) hasEntry("name", (Object) CREATE_PROFILE_NAME)); }
@Test public void testBaseTransformations() { // should support base transformation Transformation transformation = new Transformation().x(100).y(100).crop("fill").chain().crop("crop").width(100); String result = cloudinary.url().transformation(transformation).generate("test"); assertEquals("100", transformation.getHtmlWidth()); assertEquals(DEFAULT_UPLOAD_PATH + "c_fill,x_100,y_100/c_crop,w_100/test", result); }
@Test public void testShouldSupportIhIw() { String trans = new Transformation().width("iw").height("ih").crop("crop").generate(); assertEquals("c_crop,h_ih,w_iw", trans); }
@Test(expected = NotFound.class) public void test16aTransformationDelete() throws Exception { // should allow deleting named transformation api.createTransformation(API_TEST_TRANSFORMATION_2, new Transformation().crop("scale").width(103).generate(), ObjectUtils.emptyMap()); api.transformation(API_TEST_TRANSFORMATION_2, ObjectUtils.emptyMap()); ApiResponse res = api.deleteTransformation(API_TEST_TRANSFORMATION_2, ObjectUtils.emptyMap()); assertEquals("deleted", res.get("message")); api.transformation(API_TEST_TRANSFORMATION_2, ObjectUtils.emptyMap()); }
@Test public void testUploadAsync() throws Exception { if (cloudinary.config.apiSecret == null) return; JSONObject result = new JSONObject(cloudinary.uploader().upload(getAssetStream(TEST_IMAGE), ObjectUtils.asMap("transformation", new Transformation().crop("scale").width(2.0), "async", true))); assertEquals(result.getString("status"), "pending"); }
@Test public void testBaseTransformationArray() { // should support array of base transformations Transformation transformation = new Transformation().x(100).y(100).width(200).crop("fill").chain().radius(10).chain().crop("crop").width(100); String result = cloudinary.url().transformation(transformation).generate("test"); assertEquals("100", transformation.getHtmlWidth()); assertEquals(DEFAULT_UPLOAD_PATH + "c_fill,w_200,x_100,y_100/r_10/c_crop,w_100/test", result); }
@Test public void endIf2() throws Exception { Transformation transformation = new Transformation().ifCondition().width("gt", 100).and().width("lt", 200).then().width(50).crop("scale").endIf(); assertEquals("should serialize to 'if_end'", "if_w_gt_100_and_w_lt_200/c_scale,w_50/if_end", transformation.toString()); transformation = new Transformation().ifCondition().width("gt", 100).and().width("lt", 200).then().width(50).crop("scale").endIf(); assertEquals("force the if clause to be chained", "if_w_gt_100_and_w_lt_200/c_scale,w_50/if_end", transformation.toString()); transformation = new Transformation().ifCondition().width("gt", 100).and().width("lt", 200).then().width(50).crop("scale").ifElse().width(100).crop("crop").endIf(); assertEquals("force the if_else clause to be chained", "if_w_gt_100_and_w_lt_200/c_scale,w_50/if_else/c_crop,w_100/if_end", transformation.toString()); }
@Test public void withLiteral() throws Exception { Transformation transformation = new Transformation().ifCondition("w_lt_200").crop("fill").height(120).width(80); assertEquals("should include the if parameter as the first component in the transformation string", "if_w_lt_200,c_fill,h_120,w_80", transformation.toString()); transformation = new Transformation().crop("fill").height(120).ifCondition("w_lt_200").width(80); assertEquals("should include the if parameter as the first component in the transformation string", "if_w_lt_200,c_fill,h_120,w_80", transformation.toString()); String chained = "[{if: \"w_lt_200\",crop: \"fill\",height: 120, width: 80}, {if: \"w_gt_400\",crop: \"fit\",width: 150,height: 150},{effect: \"sepia\"}]"; List transformations = ObjectUtils.toList(new JSONArray(chained)); transformation = new Transformation(transformations); assertEquals("should allow multiple conditions when chaining transformations", "if_w_lt_200,c_fill,h_120,w_80/if_w_gt_400,c_fit,h_150,w_150/e_sepia", transformation.toString()); }
@Test public void testSupportStringInterpolation() { Transformation t = new Transformation() .crop("scale") .overlay(new TextLayer().text( "$(start)Hello $(name)$(ext), $(no ) $( no)$(end)" ).fontFamily("Arial").fontSize(18)); assertEquals("c_scale,l_text:Arial_18:$(start)Hello%20$(name)$(ext)%252C%20%24%28no%20%29%20%24%28%20no%29$(end)", t.generate()); } }
@Test public void testArrayShouldDefineASetOfVariables() { // using methods Transformation t = new Transformation(); t.ifCondition("face_count > 2") .variables(variable("$z", 5), variable("$foo", "$z * 2")) .crop("scale") .width("$foo * 200"); assertEquals("if_fc_gt_2,$z_5,$foo_$z_mul_2,c_scale,w_$foo_mul_200", t.generate()); }
@Test @TestCaseName("{method}: {0}") @Parameters public void testAutoGravity(String value, String serialized) { Transformation transformation = new Transformation().crop("crop").gravity(value).width(0.5f); String result = cloudinary.url().transformation(transformation).generate("test"); assertEquals(DEFAULT_UPLOAD_PATH + "c_crop," + serialized + ",w_0.5/test", result); }
@Test public void testNoEmptyTransformation() { // should not include empty transformations Transformation transformation = new Transformation().chain().x(100).y(100).crop("fill").chain(); String result = cloudinary.url().transformation(transformation).generate("test"); assertEquals(DEFAULT_UPLOAD_PATH + "c_fill,x_100,y_100/test", result); }
@Test public void testSignedUrl() { // should correctly sign a url String expected = DEFAULT_UPLOAD_PATH + "s--Ai4Znfl3--/c_crop,h_20,w_10/v1234/image.jpg"; String actual = cloudinary.url().version(1234).transformation(new Transformation().crop("crop").width(10).height(20)).signed(true) .generate("image.jpg"); assertEquals(expected, actual); expected = DEFAULT_UPLOAD_PATH + "s----SjmNDA--/v1234/image.jpg"; actual = cloudinary.url().version(1234).signed(true).generate("image.jpg"); assertEquals(expected, actual); expected = DEFAULT_UPLOAD_PATH + "s--Ai4Znfl3--/c_crop,h_20,w_10/image.jpg"; actual = cloudinary.url().transformation(new Transformation().crop("crop").width(10).height(20)).signed(true).generate("image.jpg"); assertEquals(expected, actual); }
@Test public void testVariable(){ // using strings Transformation t = new Transformation(); t.variable("$foo", 10) .chain() .ifCondition(faceCount().gt(2)) .crop("scale") .width(new Condition("$foo * 200 / faceCount")) .endIf(); assertEquals("$foo_10/if_fc_gt_2/c_scale,w_$foo_mul_200_div_fc/if_end", t.generate()); }
@Test public void testCrop() { Transformation transformation = new Transformation().width(100).height(101); String result = cloudinary.url().transformation(transformation).generate("test"); assertEquals(DEFAULT_UPLOAD_PATH + "h_101,w_100/test", result); assertEquals("101", transformation.getHtmlHeight()); assertEquals("100", transformation.getHtmlWidth()); transformation = new Transformation().width(100).height(101).crop("crop"); result = cloudinary.url().transformation(transformation).generate("test"); assertEquals(DEFAULT_UPLOAD_PATH + "c_crop,h_101,w_100/test", result); }