Skip to content

Commit f443e85

Browse files
nitzanjAmir Tocker
authored andcommitted
Adapt tests to method-level parallel testing.
1 parent c063e8c commit f443e85

File tree

6 files changed

+99
-107
lines changed

6 files changed

+99
-107
lines changed

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

Lines changed: 66 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,15 @@ abstract public class AbstractApiTest extends MockableTest {
3535
public static final String API_TEST_UPLOAD_PRESET_2 = API_TEST_UPLOAD_PRESET + "2";
3636
public static final String API_TEST_UPLOAD_PRESET_3 = API_TEST_UPLOAD_PRESET + "3";
3737
public static final String API_TEST_UPLOAD_PRESET_4 = API_TEST_UPLOAD_PRESET + "4";
38-
public static final String[] UPLOAD_TAGS = {SDK_TEST_TAG, SDK_TEST_TAG + "_api", uniqueTag};
38+
public static final String API_TAG = SDK_TEST_TAG + "_api";
39+
public static final String DIRECTION_TAG = SDK_TEST_TAG + "_api_resource_direction";
40+
public static final String[] UPLOAD_TAGS = {SDK_TEST_TAG, API_TAG};
3941
public static final String EXPLICIT_TRANSFORMATION_NAME = "c_scale,l_text:Arial_60:" + SUFFIX + ",w_100";
4042
public static final Transformation EXPLICIT_TRANSFORMATION = new Transformation().width(100).crop("scale").overlay(new TextLayer().text(SUFFIX).fontFamily("Arial").fontSize(60));
43+
public static final String UPDATE_TRANSFORMATION_NAME = "c_scale,l_text:Arial_60:" + SUFFIX + "_update,w_100";
44+
public static final Transformation UPDATE_TRANSFORMATION = new Transformation().width(100).crop("scale").overlay(new TextLayer().text(SUFFIX + "_update").fontFamily("Arial").fontSize(60));
45+
public static final String DELETE_TRANSFORMATION_NAME = "c_scale,l_text:Arial_60:" + SUFFIX + "_delete,w_100";
46+
public static final Transformation DELETE_TRANSFORMATION = new Transformation().width(100).crop("scale").overlay(new TextLayer().text(SUFFIX + "_delete").fontFamily("Arial").fontSize(60));
4147
public static final String TEST_KEY = "test-key" + SUFFIX;
4248

4349
protected Api api;
@@ -49,26 +55,39 @@ public static void setUpClass() throws IOException {
4955
System.err.println("Please setup environment for Upload test to run");
5056
return;
5157
}
52-
Map options = ObjectUtils.asMap("public_id", API_TEST, "tags", UPLOAD_TAGS, "context", "key=value", "eager",
58+
59+
List<String> uploadAndDirectionTag = new ArrayList<String>(Arrays.asList(UPLOAD_TAGS));
60+
uploadAndDirectionTag.add(DIRECTION_TAG);
61+
62+
Map options = ObjectUtils.asMap("public_id", API_TEST, "tags", uploadAndDirectionTag, "context", "key=value", "eager",
5363
Collections.singletonList(EXPLICIT_TRANSFORMATION));
5464
cloudinary.uploader().upload(SRC_TEST_IMAGE, options);
65+
5566
options.put("public_id", API_TEST_1);
5667
cloudinary.uploader().upload(SRC_TEST_IMAGE, options);
68+
options.remove("public_id");
69+
70+
options.put("eager", Collections.singletonList(UPDATE_TRANSFORMATION));
71+
cloudinary.uploader().upload(SRC_TEST_IMAGE, options);
72+
73+
options.put("eager", Collections.singletonList(DELETE_TRANSFORMATION));
74+
cloudinary.uploader().upload(SRC_TEST_IMAGE, options);
5775

5876
String context1 = TEST_KEY + "=alt";
5977
String context2 = TEST_KEY + "=alternate";
60-
options = ObjectUtils.asMap("public_id", "context_1" + SUFFIX, "tags", UPLOAD_TAGS, "context", context1);
78+
79+
options = ObjectUtils.asMap("public_id", "context_1" + SUFFIX, "tags", uploadAndDirectionTag, "context", context1);
6180
cloudinary.uploader().upload(SRC_TEST_IMAGE, options);
62-
options = ObjectUtils.asMap("public_id", "context_2" + SUFFIX, "tags", UPLOAD_TAGS, "context", context2);
81+
82+
options = ObjectUtils.asMap("public_id", "context_2" + SUFFIX, "tags", uploadAndDirectionTag, "context", context2);
6383
cloudinary.uploader().upload(SRC_TEST_IMAGE, options);
6484
}
6585

6686
@AfterClass
6787
public static void tearDownClass() {
68-
Api api = MockableTest.cleanUp();
88+
Api api = new Cloudinary().api();
6989
try {
70-
// api.deleteResources(Arrays.asList(API_TEST, API_TEST_1, API_TEST_2, API_TEST_3, API_TEST_5), ObjectUtils.emptyMap());
71-
api.deleteResourcesByTag(uniqueTag, ObjectUtils.emptyMap());
90+
api.deleteResourcesByTag(API_TAG, ObjectUtils.emptyMap());
7291
} catch (Exception ignored) {
7392
}
7493
try {
@@ -135,7 +154,7 @@ public void test01ResourceTypes() throws Exception {
135154
@Test
136155
public void test02Resources() throws Exception {
137156
// should allow listing resources
138-
Map resource = preloadResource();
157+
Map resource = preloadResource(ObjectUtils.asMap("tags", UPLOAD_TAGS));
139158

140159
final List<Map> resources = new ArrayList<Map>();
141160
String next_cursor = null;
@@ -169,8 +188,8 @@ public void test03ResourcesCursor() throws Exception {
169188
@Test
170189
public void test04ResourcesByType() throws Exception {
171190
// should allow listing resources by type
172-
Map resource = preloadResource();
173-
Map result = api.resources(ObjectUtils.asMap("type", "upload"));
191+
Map resource = preloadResource(ObjectUtils.asMap("tags", UPLOAD_TAGS));
192+
Map result = api.resources(ObjectUtils.asMap("type", "upload", "max_results", 500));
174193
List<Map> resources = (List) result.get("resources");
175194
assertThat(resources, hasItem(hasEntry("public_id", (String) resource.get("public_id"))));
176195
}
@@ -185,19 +204,19 @@ public void test05ResourcesByPrefix() throws Exception {
185204
// resources = (List<Map<? extends String, ?>>) result.get("resources");
186205
assertThat(resources, hasItem(allOf(hasEntry("public_id", API_TEST), hasEntry("type", "upload"))));
187206
assertThat(resources, hasItem(hasEntry("context", ObjectUtils.asMap("custom", ObjectUtils.asMap("key", "value")))));
188-
assertThat(resources, hasItem(hasEntry(equalTo("tags"), hasItem(SDK_TEST_TAG))));
207+
assertThat(resources, hasItem(hasEntry(equalTo("tags"), hasItem(API_TAG))));
189208
}
190209

191210
@Test
192211
public void testResourcesListingDirection() throws Exception {
193212
// should allow listing resources in both directions
194-
Map result = api.resourcesByTag(uniqueTag, ObjectUtils.asMap("type", "upload", "direction", "asc", "max_results", 500));
213+
Map result = api.resourcesByTag(DIRECTION_TAG, ObjectUtils.asMap("type", "upload", "direction", "asc", "max_results", 500));
195214
List<Map> resources = (List<Map>) result.get("resources");
196215
ArrayList<String> resourceIds = new ArrayList<String>();
197216
for (Map resource : resources) {
198217
resourceIds.add((String) resource.get("public_id"));
199218
}
200-
result = api.resourcesByTag(uniqueTag, ObjectUtils.asMap("type", "upload", "direction", -1, "max_results", 500));
219+
result = api.resourcesByTag(DIRECTION_TAG, ObjectUtils.asMap("type", "upload", "direction", -1, "max_results", 500));
201220
List<Map> resourcesDesc = (List<Map>) result.get("resources");
202221
ArrayList<String> resourceIdsDesc = new ArrayList<String>();
203222
for (Map resource : resourcesDesc) {
@@ -232,15 +251,15 @@ public void testResourcesByPublicIds() throws Exception {
232251
boolean found = false;
233252
for (Map r : resources) {
234253
ArrayList tags = (ArrayList) r.get("tags");
235-
found = found || tags.contains(SDK_TEST_TAG);
254+
found = found || tags.contains(API_TAG);
236255
}
237256
assertTrue(found);
238257
}
239258

240259
@Test
241260
public void test06ResourcesTag() throws Exception {
242261
// should allow listing resources by tag
243-
Map result = api.resourcesByTag(SDK_TEST_TAG, ObjectUtils.asMap("tags", true, "context", true));
262+
Map result = api.resourcesByTag(API_TAG, ObjectUtils.asMap("tags", true, "context", true, "max_results", 500));
244263
Map resource = findByAttr((List<Map>) result.get("resources"), "public_id", API_TEST);
245264
assertNotNull(resource);
246265
resource = findByAttr((List<Map>) result.get("resources"), "context", ObjectUtils.asMap("custom", ObjectUtils.asMap("key", "value")));
@@ -249,7 +268,7 @@ public void test06ResourcesTag() throws Exception {
249268
boolean found = false;
250269
for (Map r : resources) {
251270
ArrayList tags = (ArrayList) r.get("tags");
252-
found = found || tags.contains(SDK_TEST_TAG);
271+
found = found || tags.contains(API_TAG);
253272
}
254273
assertTrue(found);
255274
}
@@ -338,15 +357,15 @@ public void test10Tags() throws Exception {
338357
// should allow listing tags
339358
Map result = api.tags(ObjectUtils.asMap("max_results", 500));
340359
List<String> tags = (List<String>) result.get("tags");
341-
assertThat(tags, hasItem(SDK_TEST_TAG));
360+
assertThat(tags, hasItem(API_TAG));
342361
}
343362

344363
@Test
345364
public void test11TagsPrefix() throws Exception {
346365
// should allow listing tag by prefix
347-
Map result = api.tags(ObjectUtils.asMap("prefix", SDK_TEST_TAG.substring(0, SDK_TEST_TAG.length() - 1)));
366+
Map result = api.tags(ObjectUtils.asMap("prefix", API_TAG.substring(0, API_TAG.length() - 1)));
348367
List<String> tags = (List<String>) result.get("tags");
349-
assertThat(tags, hasItem(SDK_TEST_TAG));
368+
assertThat(tags, hasItem(API_TAG));
350369
result = api.tags(ObjectUtils.asMap("prefix", "api_test_no_such_tag"));
351370
tags = (List<String>) result.get("tags");
352371
assertEquals(0, tags.size());
@@ -365,7 +384,7 @@ public void test12Transformations() throws Exception {
365384
@Test
366385
public void test13TransformationMetadata() throws Exception {
367386
// should allow getting transformation metadata
368-
preloadResource(ObjectUtils.asMap("eager", Collections.singletonList(EXPLICIT_TRANSFORMATION)));
387+
preloadResource(ObjectUtils.asMap("tags", UPLOAD_TAGS, "eager", Collections.singletonList(EXPLICIT_TRANSFORMATION)));
369388
Map transformation = api.transformation(EXPLICIT_TRANSFORMATION_NAME, ObjectUtils.asMap("max_results", 500));
370389
assertNotNull(transformation);
371390
assertEquals(new Transformation((List<Map>) transformation.get("info")).generate(), EXPLICIT_TRANSFORMATION.generate());
@@ -374,12 +393,12 @@ public void test13TransformationMetadata() throws Exception {
374393
@Test
375394
public void test14TransformationUpdate() throws Exception {
376395
// should allow updating transformation allowed_for_strict
377-
api.updateTransformation(EXPLICIT_TRANSFORMATION_NAME, ObjectUtils.asMap("allowed_for_strict", true), ObjectUtils.emptyMap());
378-
Map transformation = api.transformation(EXPLICIT_TRANSFORMATION_NAME, ObjectUtils.emptyMap());
396+
api.updateTransformation(UPDATE_TRANSFORMATION_NAME, ObjectUtils.asMap("allowed_for_strict", true), ObjectUtils.emptyMap());
397+
Map transformation = api.transformation(UPDATE_TRANSFORMATION_NAME, ObjectUtils.emptyMap());
379398
assertNotNull(transformation);
380399
assertEquals(transformation.get("allowed_for_strict"), true);
381-
api.updateTransformation(EXPLICIT_TRANSFORMATION_NAME, ObjectUtils.asMap("allowed_for_strict", false), ObjectUtils.emptyMap());
382-
transformation = api.transformation(EXPLICIT_TRANSFORMATION_NAME, ObjectUtils.emptyMap());
400+
api.updateTransformation(UPDATE_TRANSFORMATION_NAME, ObjectUtils.asMap("allowed_for_strict", false), ObjectUtils.emptyMap());
401+
transformation = api.transformation(UPDATE_TRANSFORMATION_NAME, ObjectUtils.emptyMap());
383402
assertNotNull(transformation);
384403
assertEquals(transformation.get("allowed_for_strict"), false);
385404
}
@@ -407,24 +426,23 @@ public void test15aTransformationUnsafeUpdate() throws Exception {
407426
assertEquals(transformation.get("used"), false);
408427
}
409428

410-
@Test
429+
@Test(expected = NotFound.class)
411430
public void test16aTransformationDelete() throws Exception {
412431
// should allow deleting named transformation
413432
api.createTransformation(API_TEST_TRANSFORMATION_2, new Transformation().crop("scale").width(103).generate(), ObjectUtils.emptyMap());
414433
api.transformation(API_TEST_TRANSFORMATION_2, ObjectUtils.emptyMap());
415-
api.deleteTransformation(API_TEST_TRANSFORMATION_2, ObjectUtils.emptyMap());
416-
}
417-
418-
@Test(expected = NotFound.class)
419-
public void test16bTransformationDelete() throws Exception {
434+
ApiResponse res = api.deleteTransformation(API_TEST_TRANSFORMATION_2, ObjectUtils.emptyMap());
435+
assertEquals("deleted", res.get("message"));
420436
api.transformation(API_TEST_TRANSFORMATION_2, ObjectUtils.emptyMap());
421437
}
422438

423-
@Test
439+
@Test(expected = NotFound.class)
424440
public void test17aTransformationDeleteImplicit() throws Exception {
425441
// should allow deleting implicit transformation
426-
api.transformation(EXPLICIT_TRANSFORMATION_NAME, ObjectUtils.emptyMap());
427-
api.deleteTransformation(EXPLICIT_TRANSFORMATION_NAME, ObjectUtils.emptyMap());
442+
api.transformation(DELETE_TRANSFORMATION_NAME, ObjectUtils.emptyMap());
443+
ApiResponse res = api.deleteTransformation(DELETE_TRANSFORMATION_NAME, ObjectUtils.emptyMap());
444+
assertEquals("deleted", res.get("message"));
445+
api.deleteTransformation(DELETE_TRANSFORMATION_NAME, ObjectUtils.emptyMap());
428446
}
429447

430448
@Test
@@ -439,15 +457,6 @@ public void test20ResourcesContext() throws Exception {
439457
assertEquals(1, resources.size());
440458
}
441459

442-
/**
443-
* @throws Exception
444-
* @expectedException \Cloudinary\Api\NotFound
445-
*/
446-
@Test(expected = NotFound.class)
447-
public void test17bTransformationDeleteImplicit() throws Exception {
448-
api.transformation(EXPLICIT_TRANSFORMATION_NAME, ObjectUtils.emptyMap());
449-
}
450-
451460
@Test
452461
public void test18Usage() throws Exception {
453462
// should support usage API call
@@ -574,10 +583,12 @@ public void testListUploadPresets() throws Exception {
574583
api.createUploadPreset(ObjectUtils.asMap("name", API_TEST_UPLOAD_PRESET_2, "folder", "folder2"));
575584
api.createUploadPreset(ObjectUtils.asMap("name", API_TEST_UPLOAD_PRESET_3, "folder", "folder3"));
576585

577-
ArrayList presets = (ArrayList) (api.uploadPresets(ObjectUtils.emptyMap()).get("presets"));
578-
assertEquals(((Map) presets.get(0)).get("name"), API_TEST_UPLOAD_PRESET_3);
579-
assertEquals(((Map) presets.get(1)).get("name"), API_TEST_UPLOAD_PRESET_2);
580-
assertEquals(((Map) presets.get(2)).get("name"), API_TEST_UPLOAD_PRESET);
586+
ArrayList<Map> presets = (ArrayList) (api.uploadPresets(ObjectUtils.emptyMap()).get("presets"));
587+
588+
assertThat(presets, hasItem(hasEntry("name", API_TEST_UPLOAD_PRESET)));
589+
assertThat(presets, hasItem(hasEntry("name", API_TEST_UPLOAD_PRESET_2)));
590+
assertThat(presets, hasItem(hasEntry("name", API_TEST_UPLOAD_PRESET_3)));
591+
581592
api.deleteUploadPreset(API_TEST_UPLOAD_PRESET, ObjectUtils.emptyMap());
582593
api.deleteUploadPreset(API_TEST_UPLOAD_PRESET_2, ObjectUtils.emptyMap());
583594
api.deleteUploadPreset(API_TEST_UPLOAD_PRESET_3, ObjectUtils.emptyMap());
@@ -751,7 +762,7 @@ public void testUploadMapping() throws Exception {
751762

752763
@Test
753764
public void testPublishByIds() throws Exception {
754-
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", uniqueTag, "type", "authenticated"));
765+
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", UPLOAD_TAGS, "type", "authenticated"));
755766
String publicId = (String) response.get("public_id");
756767
response = cloudinary.api().publishByIds(Arrays.asList(publicId), null);
757768
List published = (List) response.get("published");
@@ -765,7 +776,7 @@ public void testPublishByIds() throws Exception {
765776

766777
@Test
767778
public void testPublishWithType() throws Exception {
768-
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", uniqueTag, "type", "authenticated"));
779+
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", UPLOAD_TAGS, "type", "authenticated"));
769780
String publicId = (String) response.get("public_id");
770781

771782
// publish with wrong type - verify publish fails
@@ -794,7 +805,7 @@ public void testPublishWithType() throws Exception {
794805

795806
@Test
796807
public void testPublishByPrefix() throws Exception {
797-
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", uniqueTag, "type", "authenticated"));
808+
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", UPLOAD_TAGS, "type", "authenticated"));
798809
String publicId = (String) response.get("public_id");
799810
response = cloudinary.api().publishByPrefix(publicId.substring(0, publicId.length() - 2), null);
800811
List published = (List) response.get("published");
@@ -808,9 +819,9 @@ public void testPublishByPrefix() throws Exception {
808819

809820
@Test
810821
public void testPublishByTag() throws Exception {
811-
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", Arrays.asList(uniqueTag, uniqueTag + "1"), "type", "authenticated"));
822+
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", Arrays.asList(API_TAG, API_TAG + "1"), "type", "authenticated"));
812823
String publicId = (String) response.get("public_id");
813-
response = cloudinary.api().publishByTag(uniqueTag + "1", null);
824+
response = cloudinary.api().publishByTag(API_TAG + "1", null);
814825
List published = (List) response.get("published");
815826
assertNotNull(published);
816827
assertEquals(published.size(), 1);
@@ -822,7 +833,7 @@ public void testPublishByTag() throws Exception {
822833

823834
@Test
824835
public void testUpdateResourcesAccessModeByIds() throws Exception {
825-
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", uniqueTag, "access_mode", "authenticated"));
836+
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", UPLOAD_TAGS, "access_mode", "authenticated"));
826837
String publicId = (String) response.get("public_id");
827838
assertEquals(response.get("access_mode"), "authenticated");
828839
response = cloudinary.api().updateResourcesAccessModeByIds("public", Arrays.asList(publicId), null);
@@ -837,7 +848,7 @@ public void testUpdateResourcesAccessModeByIds() throws Exception {
837848

838849
@Test
839850
public void testUpdateResourcesAccessModeByPrefix() throws Exception {
840-
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", uniqueTag, "access_mode", "authenticated"));
851+
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", UPLOAD_TAGS, "access_mode", "authenticated"));
841852
String publicId = (String) response.get("public_id");
842853
assertEquals(response.get("access_mode"), "authenticated");
843854
response = cloudinary.api().updateResourcesAccessModeByPrefix("public", publicId.substring(0, publicId.length() - 2), null);
@@ -852,10 +863,10 @@ public void testUpdateResourcesAccessModeByPrefix() throws Exception {
852863

853864
@Test
854865
public void testUpdateResourcesAccessModeByTag() throws Exception {
855-
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", Arrays.asList(uniqueTag, uniqueTag + "2"), "access_mode", "authenticated"));
866+
Map response = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.asMap("tags", Arrays.asList(API_TAG, API_TAG + "2"), "access_mode", "authenticated"));
856867
String publicId = (String) response.get("public_id");
857868
assertEquals(response.get("access_mode"), "authenticated");
858-
response = cloudinary.api().updateResourcesAccessModeByTag("public", uniqueTag + "2", null);
869+
response = cloudinary.api().updateResourcesAccessModeByTag("public", API_TAG + "2", null);
859870
List updated = (List) response.get("updated");
860871
assertNotNull(updated);
861872
assertEquals(updated.size(), 1);

0 commit comments

Comments
 (0)