com.cloudinary.test.AbstractUploaderTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cloudinary-test-common Show documentation
Show all versions of cloudinary-test-common Show documentation
Cloudinary is a cloud service that offers a solution to a web application's entire image management pipeline. Upload images to the cloud. Automatically perform smart image resizing, cropping and conversion without installing any complex software. Integrate Facebook or Twitter profile image extraction in a snap, in any dimension and style to match your websiteâs graphics requirements. Images are seamlessly delivered through a fast CDN, and much much more. This Java library allows to easily integrate with Cloudinary in Java applications.
package com.cloudinary.test;
import com.cloudinary.*;
import com.cloudinary.utils.ObjectUtils;
import com.cloudinary.utils.Rectangle;
import org.cloudinary.json.JSONArray;
import org.junit.*;
import org.junit.rules.TestName;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.zip.ZipInputStream;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeNotNull;
@SuppressWarnings({"rawtypes", "unchecked"})
abstract public class AbstractUploaderTest extends MockableTest {
private static final String ARCHIVE_TAG = "archive_tag_" + String.valueOf(System.currentTimeMillis());
public static final int SRC_TEST_IMAGE_W = 241;
public static final int SRC_TEST_IMAGE_H = 51;
@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, ObjectUtils.asMap("tags", new String [] {SDK_TEST_TAG, ARCHIVE_TAG}));
cloudinary.uploader().upload(SRC_TEST_IMAGE,
ObjectUtils.asMap("tags", new String [] {SDK_TEST_TAG, ARCHIVE_TAG},
"transformation", new Transformation().crop("scale").width(10)));
}
@AfterClass
public static void tearDownClass() {
Api api = MockableTest.cleanUp();
Cloudinary cloudinary = new Cloudinary();
try {
api.deleteResourcesByTag(ARCHIVE_TAG, ObjectUtils.emptyMap());
} catch (Exception ignored) {
}
}
@Rule
public TestName currentTest = new TestName();
@Before
public void setUp() {
System.out.println("Running " + this.getClass().getName() + "." + currentTest.getMethodName());
this.cloudinary = new Cloudinary();
assumeNotNull(cloudinary.config.apiSecret);
}
@Test
public void testUtf8Upload() throws IOException {
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("colors", true, "tags", SDK_TEST_TAG, "public_id", "aåßéƒ"));
assertEquals(result.get("width"), SRC_TEST_IMAGE_W);
assertEquals(result.get("height"), SRC_TEST_IMAGE_H);
assertNotNull(result.get("colors"));
assertNotNull(result.get("predominant"));
Map to_sign = new HashMap();
to_sign.put("public_id", result.get("public_id"));
to_sign.put("version", ObjectUtils.asString(result.get("version")));
String expected_signature = cloudinary.apiSignRequest(to_sign, cloudinary.config.apiSecret);
assertEquals(result.get("signature"), expected_signature);
}
@Test
public void testUpload() throws IOException {
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("colors", true, "tags", SDK_TEST_TAG));
assertEquals(result.get("width"), SRC_TEST_IMAGE_W);
assertEquals(result.get("height"), SRC_TEST_IMAGE_H);
assertNotNull(result.get("colors"));
assertNotNull(result.get("predominant"));
Map to_sign = new HashMap();
to_sign.put("public_id", result.get("public_id"));
to_sign.put("version", ObjectUtils.asString(result.get("version")));
String expected_signature = cloudinary.apiSignRequest(to_sign, cloudinary.config.apiSecret);
assertEquals(result.get("signature"), expected_signature);
}
@Test
public void testUploadUrl() throws IOException {
Map result = cloudinary.uploader().upload(REMOTE_TEST_IMAGE, ObjectUtils.asMap("tags", SDK_TEST_TAG));
assertEquals(result.get("width"), SRC_TEST_IMAGE_W);
assertEquals(result.get("height"), SRC_TEST_IMAGE_H);
Map to_sign = new HashMap();
to_sign.put("public_id", result.get("public_id"));
to_sign.put("version", ObjectUtils.asString(result.get("version")));
String expected_signature = cloudinary.apiSignRequest(to_sign, cloudinary.config.apiSecret);
assertEquals(result.get("signature"), expected_signature);
}
@Test
public void testUploadDataUri() throws IOException {
Map result = cloudinary.uploader().upload("data:image/png;base64,iVBORw0KGgoAA\nAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/dAAAAM0l\nEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A6\nP9/AFGGFyjOXZtQAAAAAElFTkSuQmCC", ObjectUtils.asMap("tags", SDK_TEST_TAG));
assertEquals(result.get("width"), 16);
assertEquals(result.get("height"), 16);
Map to_sign = new HashMap();
to_sign.put("public_id", result.get("public_id"));
to_sign.put("version", ObjectUtils.asString(result.get("version")));
String expected_signature = cloudinary.apiSignRequest(to_sign, cloudinary.config.apiSecret);
assertEquals(result.get("signature"), expected_signature);
}
@Test
public void testUploadUTF8() throws IOException {
Map result = cloudinary.uploader().upload("../cloudinary-test-common/src/main/resources/old_logo.png", ObjectUtils.asMap("public_id", "Plattenkreiss_ñg-é", "tags", SDK_TEST_TAG));
assertEquals(result.get("public_id"), "Plattenkreiss_ñg-é");
cloudinary.uploader().upload(result.get("url"), ObjectUtils.asMap("tags", SDK_TEST_TAG));
}
@Test
public void testRename() throws Exception {
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", SDK_TEST_TAG));
Object publicId = result.get("public_id");
String publicId2 = "folder/" + publicId + "2";
cloudinary.uploader().rename((String) publicId, publicId2, ObjectUtils.emptyMap());
assertNotNull(cloudinary.api().resource(publicId2, ObjectUtils.emptyMap()));
Map result2 = cloudinary.uploader().upload("../cloudinary-test-common/src/main/resources/favicon.ico", ObjectUtils.asMap("tags", SDK_TEST_TAG));
boolean error_found=false;
try {
cloudinary.uploader().rename((String) result2.get("public_id"), publicId2, ObjectUtils.asMap("tags", SDK_TEST_TAG));
} catch(Exception e) {
error_found=true;
}
assertTrue(error_found);
cloudinary.uploader().rename((String) result2.get("public_id"), publicId2, ObjectUtils.asMap("overwrite", true, "tags", SDK_TEST_TAG));
assertEquals(cloudinary.api().resource(publicId2, ObjectUtils.emptyMap()).get("format"), "ico");
}
@Test
public void testUniqueFilename() throws Exception {
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("use_filename", true, "tags", SDK_TEST_TAG));
assertTrue(((String) result.get("public_id")).matches("old_logo_[a-z0-9]{6}"));
result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("use_filename", true, "unique_filename", false, "tags", SDK_TEST_TAG));
assertEquals(result.get("public_id"), "old_logo");
}
@Test
public void testExplicit() throws IOException {
Map result = cloudinary.uploader().explicit("sample", ObjectUtils.asMap("eager", Collections.singletonList(new Transformation().crop("scale").width(2.0)), "type", "upload"));
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 testEager() throws IOException {
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("eager", Collections.singletonList(new Transformation().crop("scale").width(2.0)), "tags", SDK_TEST_TAG));
}
@Test
public void testHeaders() throws IOException {
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("headers", new String[]{"Link: 1"}, "tags", SDK_TEST_TAG));
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("headers", ObjectUtils.asMap("Link", "1"), "tags", SDK_TEST_TAG));
}
@Test
public void testText() throws IOException {
Map result = cloudinary.uploader().text("hello world", ObjectUtils.asMap("tags", SDK_TEST_TAG));
assertTrue(((Integer) result.get("width")) > 1);
assertTrue(((Integer) result.get("height")) > 1);
}
@Test
public void testImageUploadTag() {
String tag = cloudinary.uploader().imageUploadTag("test-field", ObjectUtils.asMap("callback", "http://localhost/cloudinary_cors.html"), ObjectUtils.asMap("htmlattr", "htmlvalue"));
assertTrue(tag.contains("type='file'"));
assertTrue(tag.contains("data-cloudinary-field='test-field'"));
assertTrue(tag.contains("class='cloudinary-fileupload'"));
assertTrue(tag.contains("htmlattr='htmlvalue'"));
tag = cloudinary.uploader().imageUploadTag("test-field", ObjectUtils.asMap("callback", "http://localhost/cloudinary_cors.html"), ObjectUtils.asMap("class", "myclass"));
assertTrue(tag.contains("class='cloudinary-fileupload myclass'"));
}
@Test
public void testSprite() throws IOException {
final String sprite_test_tag = String.format("sprite_test_tag_%d", new java.util.Date().getTime()) ;
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", new String [] {sprite_test_tag, SDK_TEST_TAG}, "public_id", "sprite_test_tag_1"));
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", new String [] {sprite_test_tag, SDK_TEST_TAG}, "public_id", "sprite_test_tag_2"));
Map result = cloudinary.uploader().generateSprite(sprite_test_tag, ObjectUtils.asMap("tags", SDK_TEST_TAG));
assertEquals(2, ((Map) result.get("image_infos")).size());
result = cloudinary.uploader().generateSprite(sprite_test_tag, ObjectUtils.asMap("transformation", "w_100"));
assertTrue(((String) result.get("css_url")).contains("w_100"));
result = cloudinary.uploader().generateSprite(sprite_test_tag, ObjectUtils.asMap("transformation", new Transformation().width(100), "format", "jpg"));
assertTrue(((String) result.get("css_url")).contains("f_jpg,w_100"));
}
@Test
public void testMulti() throws IOException {
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", new String[] {"multi_test_tag", SDK_TEST_TAG}, "public_id", "multi_test_tag_1"));
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", new String[] {"multi_test_tag", SDK_TEST_TAG}, "public_id", "multi_test_tag_2"));
Map result = cloudinary.uploader().multi("multi_test_tag", ObjectUtils.asMap("tags", SDK_TEST_TAG));
assertTrue(((String) result.get("url")).endsWith(".gif"));
result = cloudinary.uploader().multi("multi_test_tag", ObjectUtils.asMap("transformation", "w_100"));
assertTrue(((String) result.get("url")).contains("w_100"));
result = cloudinary.uploader().multi("multi_test_tag", ObjectUtils.asMap("transformation", new Transformation().width(111), "format", "pdf"));
assertTrue(((String) result.get("url")).contains("w_111"));
assertTrue(((String) result.get("url")).endsWith(".pdf"));
}
@Test
public void testTags() throws Exception {
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.emptyMap());
String public_id = (String) result.get("public_id");
Map result2 = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.emptyMap());
String public_id2 = (String) result2.get("public_id");
cloudinary.uploader().addTag("tag1", new String[]{public_id, public_id2}, ObjectUtils.emptyMap());
cloudinary.uploader().addTag("tag2", new String[]{public_id}, ObjectUtils.emptyMap());
List tags = (List) cloudinary.api().resource(public_id, ObjectUtils.emptyMap()).get("tags");
assertEquals(tags, ObjectUtils.asArray(new String[]{"tag1", "tag2"}));
tags = (List) cloudinary.api().resource(public_id2, ObjectUtils.emptyMap()).get("tags");
assertEquals(tags, ObjectUtils.asArray(new String[]{"tag1"}));
cloudinary.uploader().removeTag("tag1", new String[]{public_id}, ObjectUtils.emptyMap());
tags = (List) cloudinary.api().resource(public_id, ObjectUtils.emptyMap()).get("tags");
assertEquals(tags, ObjectUtils.asArray(new String[]{"tag2"}));
cloudinary.uploader().replaceTag("tag3", new String[]{public_id}, ObjectUtils.emptyMap());
tags = (List) cloudinary.api().resource(public_id, ObjectUtils.emptyMap()).get("tags");
assertEquals(tags, ObjectUtils.asArray(new String[]{"tag3"}));
}
@Test
public void testAllowedFormats() throws Exception {
//should allow whitelisted formats if allowed_formats
String[] formats = {"png"};
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("allowed_formats", formats, "tags", SDK_TEST_TAG));
assertEquals(result.get("format"), "png");
}
@Test
public void testAllowedFormatsWithIllegalFormat() throws Exception {
//should prevent non whitelisted formats from being uploaded if allowed_formats is specified
boolean errorFound = false;
String[] formats = {"jpg"};
try {
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("allowed_formats", formats, "tags", SDK_TEST_TAG));
} catch (Exception e) {
errorFound = true;
}
assertTrue(errorFound);
}
@Test
public void testAllowedFormatsWithFormat() throws Exception {
//should allow non whitelisted formats if type is specified and convert to that type
String[] formats = {"jpg"};
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("allowed_formats", formats, "format", "jpg", "tags", SDK_TEST_TAG));
assertEquals("jpg", result.get("format"));
}
@Test
public void testFaceCoordinates() throws Exception {
//should allow sending face coordinates
Coordinates coordinates = new Coordinates();
Rectangle rect1 = new Rectangle(121, 31, 110, 151);
Rectangle rect2 = new Rectangle(120, 30, 109, 150);
coordinates.addRect(rect1);
coordinates.addRect(rect2);
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("face_coordinates", coordinates, "faces", true, "tags", SDK_TEST_TAG));
ArrayList resultFaces = ((ArrayList) result.get("faces"));
assertEquals(2, resultFaces.size());
Object[] resultCoordinates = ((ArrayList) resultFaces.get(0)).toArray();
assertEquals(rect1.x, resultCoordinates[0]);
assertEquals(rect1.y, resultCoordinates[1]);
assertEquals(rect1.width, resultCoordinates[2]);
assertEquals(rect1.height, resultCoordinates[3]);
resultCoordinates = ((ArrayList) resultFaces.get(1)).toArray();
assertEquals(rect2.x, resultCoordinates[0]);
assertEquals(rect2.y, resultCoordinates[1]);
assertEquals(rect2.width, resultCoordinates[2]);
assertEquals(rect2.height, resultCoordinates[3]);
Coordinates differentCoordinates = new Coordinates();
Rectangle rect3 = new Rectangle(122, 32, 111, 152);
differentCoordinates.addRect(rect3);
cloudinary.uploader().explicit((String) result.get("public_id"), ObjectUtils.asMap("face_coordinates", differentCoordinates, "faces", true, "type", "upload"));
Map info = cloudinary.api().resource((String) result.get("public_id"), ObjectUtils.asMap("faces", true));
resultFaces = (ArrayList) info.get("faces");
assertEquals(1, resultFaces.size());
resultCoordinates = ((ArrayList) resultFaces.get(0)).toArray();
assertEquals(rect3.x, resultCoordinates[0]);
assertEquals(rect3.y, resultCoordinates[1]);
assertEquals(rect3.width, resultCoordinates[2]);
assertEquals(rect3.height, resultCoordinates[3]);
}
@Test
public void testCustomCoordinates() throws Exception {
//should allow sending face coordinates
Coordinates coordinates = new Coordinates("121,31,300,151");
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("custom_coordinates", coordinates, "tags", SDK_TEST_TAG));
Map result = cloudinary.api().resource(uploadResult.get("public_id").toString(), ObjectUtils.asMap("coordinates", true));
int[] expected = new int[]{121, 31, SRC_TEST_IMAGE_W, SRC_TEST_IMAGE_H};
Object[] actual = ((ArrayList) ((ArrayList) ((Map) result.get("coordinates")).get("custom")).get(0)).toArray();
for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], actual[i]);
}
coordinates = new Coordinates(new int[]{122, 32, SRC_TEST_IMAGE_W + 100, SRC_TEST_IMAGE_H + 100});
cloudinary.uploader().explicit((String) uploadResult.get("public_id"), ObjectUtils.asMap("custom_coordinates", coordinates, "coordinates", true, "type", "upload"));
result = cloudinary.api().resource(uploadResult.get("public_id").toString(), ObjectUtils.asMap("coordinates", true));
expected = new int[]{122, 32, SRC_TEST_IMAGE_W + 100, SRC_TEST_IMAGE_H + 100};
actual = ((ArrayList) ((ArrayList) ((Map) result.get("coordinates")).get("custom")).get(0)).toArray();
for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], actual[i]);
}
}
@Test
public void testContext() throws Exception {
//should allow sending context
Map context = ObjectUtils.asMap("caption", "some cäption", "alt", "alternativè");
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("context", context, "tags", SDK_TEST_TAG));
Map info = cloudinary.api().resource((String) result.get("public_id"), ObjectUtils.asMap("context", true));
assertEquals(ObjectUtils.asMap("custom", context), info.get("context"));
Map differentContext = ObjectUtils.asMap("caption", "different caption", "alt2", "alternative alternative");
cloudinary.uploader().explicit((String) result.get("public_id"), ObjectUtils.asMap("type", "upload", "context", differentContext));
info = cloudinary.api().resource((String) result.get("public_id"), ObjectUtils.asMap("context", true));
assertEquals(ObjectUtils.asMap("custom", differentContext), info.get("context"));
}
@Test
public void testModerationRequest() throws Exception {
//should support requesting manual moderation
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("moderation", "manual", "tags", SDK_TEST_TAG));
assertEquals("manual", ((List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy