Skip to content

Commit 270ea6e

Browse files
author
Amir Tocker
committed
Fix tests.
1 parent 458c986 commit 270ea6e

File tree

3 files changed

+77
-40
lines changed

3 files changed

+77
-40
lines changed

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,19 @@
2424

2525
@SuppressWarnings({"rawtypes", "unchecked", "JavaDoc"})
2626
abstract public class AbstractApiTest extends MockableTest {
27-
private static final String SRC_TEST_IMAGE = "../cloudinary-test-common/src/main/resources/old_logo.png";
28-
private static final int SUFFIX = new Random().nextInt(99999);
2927
private static final String API_TEST = "api_test_" + SUFFIX;
3028
private static final String API_TEST_1 = API_TEST + "_1";
3129
private static final String API_TEST_2 = API_TEST + "_2";
3230
private static final String API_TEST_3 = API_TEST + "_3";
3331
private static final String API_TEST_5 = API_TEST + "_5";
34-
private static final String SDK_TEST_TAG = "cloudinary_java_test_" + SUFFIX;
3532
public static final String API_TEST_TRANSFORMATION = "api_test_transformation_" + SUFFIX;
3633
public static final String API_TEST_TRANSFORMATION_2 = API_TEST_TRANSFORMATION + "2";
3734
public static final String API_TEST_TRANSFORMATION_3 = API_TEST_TRANSFORMATION + "3";
3835
public static final String API_TEST_UPLOAD_PRESET = "api_test_upload_preset_" + SUFFIX;
3936
public static final String API_TEST_UPLOAD_PRESET_2 = API_TEST_UPLOAD_PRESET + "2";
4037
public static final String API_TEST_UPLOAD_PRESET_3 = API_TEST_UPLOAD_PRESET + "3";
4138
public static final String API_TEST_UPLOAD_PRESET_4 = API_TEST_UPLOAD_PRESET + "4";
42-
private Cloudinary cloudinary;
4339
protected Api api;
44-
private static final String uniqueTag = SDK_TEST_TAG + (new java.util.Date().getTime());
4540

4641
@BeforeClass
4742
public static void setUpClass() throws IOException {
@@ -59,16 +54,11 @@ public static void setUpClass() throws IOException {
5954

6055
@AfterClass
6156
public static void tearDownClass() {
62-
Cloudinary cloudinary = new Cloudinary();
63-
Api api = cloudinary.api();
57+
Api api = MockableTest.cleanUp();
6458
try {
6559
api.deleteResources(Arrays.asList(API_TEST, API_TEST_1, API_TEST_2, API_TEST_3, API_TEST_5), ObjectUtils.emptyMap());
6660
} catch (Exception ignored) {
6761
}
68-
try {
69-
api.deleteResourcesByTag(uniqueTag, ObjectUtils.emptyMap());
70-
} catch (Exception ignored) {
71-
}
7262
try {
7363
api.deleteTransformation(API_TEST_TRANSFORMATION, ObjectUtils.emptyMap());
7464
} catch (Exception ignored) {
@@ -132,19 +122,16 @@ public void test01ResourceTypes() throws Exception {
132122
@Test
133123
public void test02Resources() throws Exception {
134124
// should allow listing resources
135-
Map result = api.resources(ObjectUtils.emptyMap());
136-
final List<Map> resources = (List) result.get("resources");
137-
assertThat(resources, hasItem(allOf(hasEntry("public_id", API_TEST),hasEntry("type", "upload"))));
138-
}
125+
Map resource = preloadResource();
139126

140-
@Test
141-
public void testTimeoutParameter() throws Exception {
142-
// should allow listing resources
143-
Map<String, Object> options = new HashMap<String, Object>();
144-
options.put("timeout", Integer.valueOf(5000));
145-
Map result = api.resources(options);
146-
List<Map> resources = (List) result.get("resources");
147-
assertThat(resources, hasItem(allOf(hasEntry("public_id", API_TEST),hasEntry("type", "upload"))));
127+
final List<Map> resources = new ArrayList<Map>();
128+
String next_cursor = null;
129+
do {
130+
Map result = api.resources(ObjectUtils.asMap("max_results", 500, "next_cursor", next_cursor));
131+
resources.addAll((List) result.get("resources"));
132+
next_cursor = (String) result.get("next_cursor");
133+
} while (next_cursor != null );
134+
assertThat(resources, hasItem(allOf(hasEntry("public_id", (String) resource.get("public_id")),hasEntry("type", "upload"))));
148135
}
149136

150137
@Test
@@ -169,9 +156,10 @@ public void test03ResourcesCursor() throws Exception {
169156
@Test
170157
public void test04ResourcesByType() throws Exception {
171158
// should allow listing resources by type
159+
Map resource = preloadResource();
172160
Map result = api.resources(ObjectUtils.asMap("type", "upload"));
173161
List<Map> resources = (List) result.get("resources");
174-
assertThat(resources, hasItem(hasEntry("public_id", API_TEST)));
162+
assertThat(resources, hasItem(hasEntry("public_id", (String) resource.get("public_id"))));
175163
}
176164

177165
@Test
@@ -280,7 +268,7 @@ public void test09DeleteResources() throws Exception {
280268
cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("public_id", public_id, "tags", SDK_TEST_TAG));
281269
Map resource = api.resource(public_id, ObjectUtils.emptyMap());
282270
assertNotNull(resource);
283-
api.deleteResources(Arrays.asList(API_TEST, API_TEST_2, public_id), ObjectUtils.emptyMap());
271+
api.deleteResources(Arrays.asList(API_TEST_2, public_id), ObjectUtils.emptyMap());
284272
api.resource(public_id, ObjectUtils.emptyMap());
285273
}
286274

@@ -337,9 +325,11 @@ public void test12Transformations() throws Exception {
337325
@Test
338326
public void test13TransformationMetadata() throws Exception {
339327
// should allow getting transformation metadata
340-
Map transformation = api.transformation("c_scale,w_100", ObjectUtils.emptyMap());
328+
final Transformation tr = new Transformation().crop("scale").width(100);
329+
preloadResource(ObjectUtils.asMap("eager", Collections.singletonList(tr)));
330+
Map transformation = api.transformation("c_scale,w_100", ObjectUtils.asMap("max_results", 500));
341331
assertNotNull(transformation);
342-
assertEquals(new Transformation((List<Map>) transformation.get("info")).generate(), new Transformation().crop("scale").width(100).generate());
332+
assertEquals(new Transformation((List<Map>) transformation.get("info")).generate(), tr.generate());
343333
}
344334

345335
@Test

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractUploaderTest.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@
1717
import static org.junit.Assume.assumeNotNull;
1818

1919
@SuppressWarnings({"rawtypes", "unchecked"})
20-
abstract public class AbstractUploaderTest {
20+
abstract public class AbstractUploaderTest extends MockableTest {
2121

22-
public static final String SRC_TEST_IMAGE = "../cloudinary-test-common/src/main/resources/old_logo.png";
23-
public static final String REMOTE_TEST_IMAGE = "http://cloudinary.com/images/old_logo.png";
2422
private static final String ARCHIVE_TAG = "archive_tag_" + String.valueOf(System.currentTimeMillis());
25-
private static final String SDK_TEST_TAG = "cloudinary_java_test";
2623
public static final int SRC_TEST_IMAGE_W = 241;
2724
public static final int SRC_TEST_IMAGE_H = 51;
28-
private Cloudinary cloudinary;
2925

3026
@BeforeClass
3127
public static void setUpClass() throws IOException {
@@ -41,9 +37,13 @@ public static void setUpClass() throws IOException {
4137
}
4238

4339
@AfterClass
44-
public static void tearDownClass() throws Exception {
40+
public static void tearDownClass() {
41+
Api api = MockableTest.cleanUp();
4542
Cloudinary cloudinary = new Cloudinary();
46-
cloudinary.api().deleteResourcesByTag(ARCHIVE_TAG, ObjectUtils.emptyMap());
43+
try {
44+
api.deleteResourcesByTag(ARCHIVE_TAG, ObjectUtils.emptyMap());
45+
} catch (Exception ignored) {
46+
}
4747
}
4848

4949
@Rule
@@ -56,6 +56,20 @@ public void setUp() {
5656
assumeNotNull(cloudinary.config.apiSecret);
5757
}
5858

59+
@Test
60+
public void testUtf8Upload() throws IOException {
61+
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("colors", true, "tags", SDK_TEST_TAG, "public_id", "aåßéƒ"));
62+
assertEquals(result.get("width"), SRC_TEST_IMAGE_W);
63+
assertEquals(result.get("height"), SRC_TEST_IMAGE_H);
64+
assertNotNull(result.get("colors"));
65+
assertNotNull(result.get("predominant"));
66+
Map<String, Object> to_sign = new HashMap<String, Object>();
67+
to_sign.put("public_id", result.get("public_id"));
68+
to_sign.put("version", ObjectUtils.asString(result.get("version")));
69+
String expected_signature = cloudinary.apiSignRequest(to_sign, cloudinary.config.apiSecret);
70+
assertEquals(result.get("signature"), expected_signature);
71+
}
72+
5973
@Test
6074
public void testUpload() throws IOException {
6175
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("colors", true, "tags", SDK_TEST_TAG));
Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,52 @@
11
package com.cloudinary.test;
22

3+
import com.cloudinary.Api;
4+
import com.cloudinary.Cloudinary;
5+
import com.cloudinary.utils.ObjectUtils;
36
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
47

5-
/**
6-
* Created by amir on 01/08/2016.
7-
*/
8+
import java.io.IOException;
9+
import java.util.Map;
10+
import java.util.Random;
11+
812
public class MockableTest {
913

14+
public static final String SRC_TEST_IMAGE = "../cloudinary-test-common/src/main/resources/old_logo.png";
15+
public static final String REMOTE_TEST_IMAGE = "http://cloudinary.com/images/old_logo.png";
16+
protected static final int SUFFIX = new Random().nextInt(99999);
17+
protected static final String SDK_TEST_TAG = "cloudinary_java_test_" + SUFFIX;
18+
protected static final String uniqueTag = SDK_TEST_TAG + (new java.util.Date().getTime());
19+
protected Cloudinary cloudinary;
20+
1021
protected Object getParam(String name){
1122
throw new NotImplementedException();
12-
};
23+
}
1324
protected String getURL(){
1425
throw new NotImplementedException();
15-
};
26+
}
1627
protected String getHttpMethod(){
1728
throw new NotImplementedException();
18-
};
29+
}
30+
31+
protected Map preloadResource() throws IOException {
32+
return preloadResource(ObjectUtils.emptyMap());
33+
}
34+
35+
protected Map preloadResource(Map options) throws IOException {
36+
Map combinedOptions = ObjectUtils.asMap(
37+
"tags", new String[]{SDK_TEST_TAG, uniqueTag},
38+
"transformation", "c_scale,w_100");
39+
combinedOptions.putAll(options);
40+
return cloudinary.uploader().upload("http://res.cloudinary.com/demo/image/upload/sample", combinedOptions);
41+
}
42+
43+
public static Api cleanUp() {
44+
Cloudinary cloudinary = new Cloudinary();
45+
Api api = cloudinary.api();
46+
try {
47+
api.deleteResourcesByTag(uniqueTag, ObjectUtils.emptyMap());
48+
} catch (Exception ignored) {
49+
}
50+
return api;
51+
}
1952
}

0 commit comments

Comments
 (0)