Skip to content

Commit 6fa4783

Browse files
authored
Fix uploader tests cleanup
1 parent 75c141a commit 6fa4783

File tree

1 file changed

+61
-27
lines changed

1 file changed

+61
-27
lines changed

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

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323

2424
@SuppressWarnings({"rawtypes", "unchecked"})
2525
abstract public class AbstractUploaderTest extends MockableTest {
26-
2726
private static final String ARCHIVE_TAG = SDK_TEST_TAG + "_archive";
2827
private static final String UPLOADER_TAG = SDK_TEST_TAG + "_uploader";
2928
public static final int SRC_TEST_IMAGE_W = 241;
3029
public static final int SRC_TEST_IMAGE_H = 51;
30+
private static Map<String,Set<String>> toDelete = new HashMap<>();
3131

3232
@BeforeClass
3333
public static void setUpClass() throws IOException {
@@ -36,10 +36,10 @@ public static void setUpClass() throws IOException {
3636
System.err.println("Please setup environment for Upload test to run");
3737
}
3838

39-
cloudinary.uploader().upload(SRC_TEST_IMAGE, asMap("tags", new String [] {SDK_TEST_TAG, UPLOADER_TAG, ARCHIVE_TAG}));
40-
cloudinary.uploader().upload(SRC_TEST_IMAGE, asMap("tags", new String [] {SDK_TEST_TAG, UPLOADER_TAG, ARCHIVE_TAG}, "resource_type", "raw"));
39+
cloudinary.uploader().upload(SRC_TEST_IMAGE, asMap("tags", new String[]{SDK_TEST_TAG, UPLOADER_TAG, ARCHIVE_TAG}));
40+
cloudinary.uploader().upload(SRC_TEST_IMAGE, asMap("tags", new String[]{SDK_TEST_TAG, UPLOADER_TAG, ARCHIVE_TAG}, "resource_type", "raw"));
4141
cloudinary.uploader().upload(SRC_TEST_IMAGE,
42-
asMap("tags", new String [] {SDK_TEST_TAG, UPLOADER_TAG, ARCHIVE_TAG},
42+
asMap("tags", new String[]{SDK_TEST_TAG, UPLOADER_TAG, ARCHIVE_TAG},
4343
"transformation", new Transformation().crop("scale").width(10)));
4444
}
4545

@@ -58,6 +58,13 @@ public static void tearDownClass() {
5858
api.deleteResourcesByTag(UPLOADER_TAG, ObjectUtils.asMap("resource_type", "raw"));
5959
} catch (Exception ignored) {
6060
}
61+
for (String type : toDelete.keySet()) {
62+
try {
63+
api.deleteResources(toDelete.get(type), Collections.singletonMap("type", type));
64+
}catch ( Exception ignored){}
65+
}
66+
67+
toDelete.clear();
6168
}
6269

6370
@Rule
@@ -70,6 +77,7 @@ public void setUp() {
7077
assumeNotNull(cloudinary.config.apiSecret);
7178
}
7279

80+
7381
@Test
7482
public void testUtf8Upload() throws IOException {
7583

@@ -112,6 +120,7 @@ public void testUpload() throws IOException {
112120
assertEquals(result.get("signature"), expected_signature);
113121
}
114122

123+
115124
@Test
116125
public void testUploadUrl() throws IOException {
117126
Map result = cloudinary.uploader().upload(REMOTE_TEST_IMAGE, asMap("tags", Arrays.asList(SDK_TEST_TAG, UPLOADER_TAG)));
@@ -126,7 +135,7 @@ public void testUploadUrl() throws IOException {
126135

127136
@Test
128137
public void testUploadLargeUrl() throws IOException {
129-
Map result = cloudinary.uploader().uploadLarge(REMOTE_TEST_IMAGE, asMap("tags", SDK_TEST_TAG));
138+
Map result = cloudinary.uploader().uploadLarge(REMOTE_TEST_IMAGE, asMap("tags", Arrays.asList(SDK_TEST_TAG, UPLOADER_TAG)));
130139
assertEquals(result.get("width"), SRC_TEST_IMAGE_W);
131140
assertEquals(result.get("height"), SRC_TEST_IMAGE_H);
132141
Map<String, Object> to_sign = new HashMap<String, Object>();
@@ -186,7 +195,7 @@ public void testUniqueFilename() throws Exception {
186195

187196
@Test
188197
public void testEagerWithStreamingProfile() throws IOException {
189-
Transformation transformation = new EagerTransformation().format("m3u8").streamingProfile("full_hd");
198+
Transformation transformation = new EagerTransformation().format("m3u8").streamingProfile("full_hd");
190199
assertEquals("sp_full_hd/m3u8", transformation.generate());
191200
}
192201

@@ -206,7 +215,7 @@ public void testEager() throws IOException {
206215

207216
@Test
208217
public void testUploadAsync() throws IOException {
209-
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, asMap("transformation", new Transformation().crop("scale").width(2.0), "async", true));
218+
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)));
210219
assertEquals((String) result.get("status"), "pending");
211220
}
212221

@@ -217,12 +226,14 @@ public void testHeaders() throws IOException {
217226
}
218227

219228
@Test
220-
public void testText() throws IOException {
229+
public void testText() throws Exception {
221230
Map result = cloudinary.uploader().text("hello world", asMap("tags", Arrays.asList(SDK_TEST_TAG, UPLOADER_TAG)));
231+
addToDeleteList("text", result.get("public_id").toString());
222232
assertTrue(((Integer) result.get("width")) > 1);
223233
assertTrue(((Integer) result.get("height")) > 1);
224234
}
225235

236+
226237
@Test
227238
public void testImageUploadTag() {
228239
String tag = cloudinary.uploader().imageUploadTag("test-field", asMap("callback", "http://localhost/cloudinary_cors.html"), asMap("htmlattr", "htmlvalue"));
@@ -234,34 +245,35 @@ public void testImageUploadTag() {
234245
assertTrue(tag.contains("class='cloudinary-fileupload myclass'"));
235246
}
236247

248+
237249
@Test
238-
public void testSprite() throws IOException {
250+
public void testSprite() throws Exception {
239251
final String sprite_test_tag = String.format("sprite_test_tag_%d", new java.util.Date().getTime());
240252
cloudinary.uploader().upload(SRC_TEST_IMAGE, asMap("tags", new String[]{sprite_test_tag, SDK_TEST_TAG, UPLOADER_TAG}, "public_id", "sprite_test_tag_1" + SUFFIX));
241253
cloudinary.uploader().upload(SRC_TEST_IMAGE, asMap("tags", new String[]{sprite_test_tag, SDK_TEST_TAG, UPLOADER_TAG}, "public_id", "sprite_test_tag_2" + SUFFIX));
242254
Map result = cloudinary.uploader().generateSprite(sprite_test_tag, asMap("tags", Arrays.asList(SDK_TEST_TAG, UPLOADER_TAG)));
255+
addToDeleteList("sprite", result.get("public_id").toString());
243256
assertEquals(2, ((Map) result.get("image_infos")).size());
244257
result = cloudinary.uploader().generateSprite(sprite_test_tag, asMap("transformation", "w_100"));
258+
addToDeleteList("sprite", result.get("public_id").toString());
245259
assertTrue(((String) result.get("css_url")).contains("w_100"));
246260
result = cloudinary.uploader().generateSprite(sprite_test_tag, asMap("transformation", new Transformation().width(100), "format", "jpg"));
261+
addToDeleteList("sprite", result.get("public_id").toString());
247262
assertTrue(((String) result.get("css_url")).contains("f_jpg,w_100"));
248263
}
249264

250265
@Test
251-
public void testMulti() throws IOException {
266+
public void testMulti() throws Exception {
252267
final String MULTI_TEST_TAG = "multi_test_tag" + SUFFIX;
253268
final Map options = asMap("tags", new String[]{MULTI_TEST_TAG, SDK_TEST_TAG, UPLOADER_TAG});
254269
cloudinary.uploader().upload(SRC_TEST_IMAGE, options);
255270
cloudinary.uploader().upload(SRC_TEST_IMAGE, options);
256271
List<String> ids = new ArrayList<String>();
257272
Map result = cloudinary.uploader().multi(MULTI_TEST_TAG, asMap("transformation", "c_crop,w_0.5"));
258-
ids.add((String) result.get("public_id"));
273+
addToDeleteList("multi", result.get("public_id").toString());
259274
Map pdfResult = cloudinary.uploader().multi(MULTI_TEST_TAG, asMap("transformation", new Transformation().width(111), "format", "pdf"));
260-
ids.add((String) pdfResult.get("public_id"));
261-
try {
262-
cloudinary.api().deleteResources(ids, ObjectUtils.emptyMap());
263-
} catch (Exception ignored) {
264-
}
275+
addToDeleteList("multi", pdfResult.get("public_id").toString());
276+
265277
assertTrue(((String) result.get("url")).endsWith(".gif"));
266278
assertTrue(((String) result.get("url")).contains("w_0.5"));
267279
assertTrue(((String) pdfResult.get("url")).contains("w_111"));
@@ -272,8 +284,10 @@ public void testMulti() throws IOException {
272284
public void testTags() throws Exception {
273285
Map result = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.emptyMap());
274286
String public_id = (String) result.get("public_id");
287+
addToDeleteList("upload", public_id);
275288
Map result2 = cloudinary.uploader().upload(SRC_TEST_IMAGE, ObjectUtils.emptyMap());
276289
String public_id2 = (String) result2.get("public_id");
290+
addToDeleteList("upload", public_id2);
277291
cloudinary.uploader().addTag("tag1", new String[]{public_id, public_id2}, ObjectUtils.emptyMap());
278292
cloudinary.uploader().addTag("tag2", new String[]{public_id}, ObjectUtils.emptyMap());
279293
List<String> tags = (List<String>) cloudinary.api().resource(public_id, ObjectUtils.emptyMap()).get("tags");
@@ -288,10 +302,10 @@ public void testTags() throws Exception {
288302
assertEquals(tags, asArray(new String[]{"tag3"}));
289303
result = cloudinary.uploader().removeAllTags(new String[]{public_id, public_id2, "noSuchId"}, ObjectUtils.emptyMap());
290304
List<String> publicIds = (List<String>) result.get("public_ids");
305+
291306
assertThat(publicIds, containsInAnyOrder(public_id, public_id2)); // = and not containing "noSuchId"
292307
result = cloudinary.api().resource(public_id, ObjectUtils.emptyMap());
293308
assertThat((Map<? extends String, ?>) result, not(hasKey("tags")));
294-
295309
}
296310

297311
@Test
@@ -431,6 +445,8 @@ public void testDetectionRequest() {
431445
}
432446
}
433447

448+
449+
434450
@Test
435451
public void testAutoTaggingRequest() {
436452
//should support requesting auto tagging
@@ -544,23 +560,31 @@ public void testResponsiveBreakpoints() throws Exception {
544560

545561
@Test
546562
public void testCreateArchive() throws Exception {
563+
List<String> toDelete = new ArrayList<>(2);
547564
Map result = cloudinary.uploader().createArchive(new ArchiveParams().tags(new String[]{ARCHIVE_TAG}));
565+
toDelete.add(result.get("public_id").toString());
548566
assertEquals(2, result.get("file_count"));
549567
result = cloudinary.uploader().createArchive(
550568
new ArchiveParams().tags(new String[]{ARCHIVE_TAG}).transformations(
551569
new Transformation[]{new Transformation().width(0.5), new Transformation().width(2.0)}));
570+
toDelete.add(result.get("public_id").toString());
571+
552572
assertEquals(4, result.get("file_count"));
573+
cloudinary.api().deleteResources(toDelete, asMap("resource_type", "raw"));
553574
}
554575

576+
555577
@Test
556578
public void testCreateArchiveRaw() throws Exception {
557579
Map result = cloudinary.uploader().createArchive(new ArchiveParams().tags(new String[]{ARCHIVE_TAG}).resourceType("raw"));
558580
assertEquals(1, result.get("file_count"));
581+
cloudinary.api().deleteResources(Arrays.asList(result.get("public_id").toString()), asMap("resource_type", "raw"));
582+
559583
}
560584

561585
@Test
562586
public void testDownloadArchive() throws Exception {
563-
String result = cloudinary.downloadArchive(new ArchiveParams().tags(new String[]{ARCHIVE_TAG}));
587+
String result = cloudinary.downloadArchive(new ArchiveParams().tags(new String[]{ARCHIVE_TAG}).targetTags(new String[]{UPLOADER_TAG}));
564588
URL url = new java.net.URL(result);
565589
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
566590
ZipInputStream in = new ZipInputStream(new BufferedInputStream(urlConnection.getInputStream()));
@@ -597,17 +621,17 @@ public void testAccessControl() throws ParseException, IOException {
597621
Arrays.asList(acl, token), "tags", Arrays.asList(SDK_TEST_TAG, UPLOADER_TAG)));
598622

599623
assertNotNull(result);
600-
List<Map<String,String>> accessControlResponse = (List<Map<String, String>>) result.get("access_control");
624+
List<Map<String, String>> accessControlResponse = (List<Map<String, String>>) result.get("access_control");
601625
assertNotNull(accessControlResponse);
602-
assertEquals(2, accessControlResponse.size());
626+
assertEquals(2, accessControlResponse.size());
603627

604628
Map<String, String> acr = accessControlResponse.get(0);
605-
assertEquals("anonymous", acr.get("access_type"));
606-
assertEquals("2019-02-22T14:20:57Z", acr.get("start"));
629+
assertEquals("anonymous", acr.get("access_type"));
630+
assertEquals("2019-02-22T14:20:57Z", acr.get("start"));
607631
assertThat(acr, not(hasKey("end")));
608-
632+
609633
acr = accessControlResponse.get(1);
610-
assertEquals("token", acr.get("access_type"));
634+
assertEquals("token", acr.get("access_type"));
611635
assertThat(acr, not(hasKey("start")));
612636
assertThat(acr, not(hasKey("end")));
613637

@@ -618,9 +642,9 @@ public void testAccessControl() throws ParseException, IOException {
618642
accessControlResponse = (List<Map<String, String>>) result.get("access_control");
619643
assertNotNull(accessControlResponse);
620644
acr = accessControlResponse.get(0);
621-
assertEquals(1, accessControlResponse.size());
622-
assertEquals("anonymous", acr.get("access_type"));
623-
assertEquals("2019-02-22T14:20:57Z", acr.get("start"));
645+
assertEquals(1, accessControlResponse.size());
646+
assertEquals("anonymous", acr.get("access_type"));
647+
assertEquals("2019-02-22T14:20:57Z", acr.get("start"));
624648
assertThat(acr, not(hasKey("end")));
625649

626650
String aclString = "[{\"access_type\":\"anonymous\",\"start\":\"2019-02-22 16:20:57 +0200\",\"end\":\"2019-03-22 00:00 +0200\"}]";
@@ -635,4 +659,14 @@ public void testAccessControl() throws ParseException, IOException {
635659
assertEquals("2019-02-22T14:20:57Z", accessControlResponse.get(0).get("start"));
636660
assertEquals("2019-03-21T22:00:00Z", accessControlResponse.get(0).get("end"));
637661
}
662+
663+
private void addToDeleteList(String type, String id){
664+
Set<String> ids = toDelete.get(type);
665+
if (ids == null){
666+
ids = new HashSet<>();
667+
toDelete.put(type, ids);
668+
}
669+
670+
ids.add(id);
671+
}
638672
}

0 commit comments

Comments
 (0)