-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathMediaHelperTest.java
More file actions
445 lines (364 loc) · 13.6 KB
/
MediaHelperTest.java
File metadata and controls
445 lines (364 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package com.github.stickerifier.stickerify.media;
import static com.github.stickerifier.stickerify.ResourceHelper.loadResource;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MATROSKA_FORMAT;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_IMAGE_FILE_SIZE;
import static com.github.stickerifier.stickerify.media.MediaConstraints.MAX_VIDEO_FILE_SIZE;
import static com.github.stickerifier.stickerify.media.MediaConstraints.VP9_CODEC;
import static java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import com.github.stickerifier.stickerify.exception.MediaException;
import com.github.stickerifier.stickerify.junit.ClearTempFiles;
import com.github.stickerifier.stickerify.junit.Tags;
import com.github.stickerifier.stickerify.process.ProcessHelper;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import java.io.File;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
@Tag(Tags.MEDIA)
@ClearTempFiles
class MediaHelperTest {
@Test
@Tag(Tags.IMAGE)
void resizeRectangularImage() throws Exception {
var jpgImage = loadResource("big.jpg");
var result = MediaHelper.convert(jpgImage);
assertImageConsistency(result, 512, 341);
}
private static void assertImageConsistency(@Nullable File image, int expectedWidth, int expectedHeight) throws Exception {
assertNotNull(image);
var mediaInfo = MediaHelper.retrieveMultimediaInfo(image);
var imageInfo = mediaInfo.video();
assertNotNull(imageInfo);
var formatInfo = mediaInfo.format();
assertNotNull(formatInfo);
var actualExtension = getExtension(image);
assertAll("Image validation failed",
() -> assertThat("image's extension must be webp", actualExtension, is(equalTo(".webp"))),
() -> assertThat("image's width is not correct", imageInfo.width(), is(equalTo(expectedWidth))),
() -> assertThat("image's height is not correct", imageInfo.height(), is(equalTo(expectedHeight))),
() -> assertThat("image size should not exceed 512 KB", formatInfo.size(), is(lessThanOrEqualTo(MAX_IMAGE_FILE_SIZE)))
);
}
private static String getExtension(File file) {
return file.getName().substring(file.getName().lastIndexOf('.'));
}
@Test
@Tag(Tags.IMAGE)
void resizeSmallImage() throws Exception {
var pngImage = loadResource("small_image.png");
var result = MediaHelper.convert(pngImage);
assertImageConsistency(result, 512, 512);
}
@Test
@Tag(Tags.IMAGE)
void noImageConversionNeeded() throws Exception {
var pngImage = loadResource("valid.png");
var result = MediaHelper.convert(pngImage);
assertThat(result, is(nullValue()));
}
@Test
@Tag(Tags.IMAGE)
void resizeStaticWebpImage() throws Exception {
var webpImage = loadResource("static.webp");
var result = MediaHelper.convert(webpImage);
assertImageConsistency(result, 256, 512);
}
@Test
@Tag(Tags.IMAGE)
void resizeFaviconImage() throws Exception {
var faviconImage = loadResource("favicon.ico");
var result = MediaHelper.convert(faviconImage);
assertImageConsistency(result, 512, 512);
}
@Test
@Tag(Tags.IMAGE)
void resizeTiffImage() throws Exception {
var tiffImage = loadResource("valid.tiff");
var result = MediaHelper.convert(tiffImage);
assertImageConsistency(result, 512, 342);
}
@Test
@Tag(Tags.IMAGE)
void resizePsdImage() throws Exception {
var psdImage = loadResource("valid.psd");
var result = MediaHelper.convert(psdImage);
assertImageConsistency(result, 512, 384);
}
@Test
@Tag(Tags.IMAGE)
void resizeDetailedImage() throws Exception {
var detailedImage = loadResource("detailed.jpg");
var result = MediaHelper.convert(detailedImage);
assertImageConsistency(result, 512, 512);
}
@Test
@Tag(Tags.IMAGE)
void resizeSvgImage() throws Exception {
assumeSvgSupport();
var svgImage = loadResource("valid.svg");
var result = MediaHelper.convert(svgImage);
assertImageConsistency(result, 512, 512);
}
private static void assumeSvgSupport() throws Exception {
var decoders = ProcessHelper.executeCommand("ffmpeg", "-v", "quiet", "-hide_banner", "-decoders");
var supportsSvg = decoders.contains("(codec svg)");
assumeTrue(supportsSvg, "FFmpeg was not compiled with SVG support");
}
@Test
@Tag(Tags.VIDEO)
void convertLongMovVideo() throws Exception {
var movVideo = loadResource("long.mov");
var result = MediaHelper.convert(movVideo);
assertVideoConsistency(result, 512, 288, 29.97003F, 2.969F);
}
private static void assertVideoConsistency(@Nullable File video, int expectedWidth, int expectedHeight, float expectedFrameRate, float expectedDuration) throws Exception {
assertNotNull(video);
var mediaInfo = MediaHelper.retrieveMultimediaInfo(video);
var videoInfo = mediaInfo.video();
assertNotNull(videoInfo);
var formatInfo = mediaInfo.format();
assertNotNull(formatInfo);
assertNotNull(formatInfo.duration());
var actualExtension = getExtension(video);
assertAll("Video validation failed",
() -> assertThat("video's extension must be webm", actualExtension, is(equalTo(".webm"))),
() -> assertThat("video's width is not correct", videoInfo.width(), is(equalTo(expectedWidth))),
() -> assertThat("video's height is not correct", videoInfo.height(), is(equalTo(expectedHeight))),
() -> assertThat("video's frame rate is not correct", videoInfo.frameRate(), is(equalTo(expectedFrameRate))),
() -> assertThat("video must be encoded with the VP9 codec", videoInfo.codec(), is(equalTo(VP9_CODEC))),
() -> assertThat("video's duration is not correct", formatInfo.duration(), is(equalTo(expectedDuration))),
() -> assertThat("video's format must be matroska", formatInfo.format(), startsWith(MATROSKA_FORMAT)),
() -> assertThat("video must have no audio stream", mediaInfo.audio(), is(nullValue())),
() -> assertThat("video size should not exceed 256 KB", formatInfo.size(), is(lessThanOrEqualTo(MAX_VIDEO_FILE_SIZE)))
);
}
@Test
@Tag(Tags.VIDEO)
void convertMp4WithAudio() throws Exception {
var mp4Video = loadResource("video_with_audio.mp4");
var result = MediaHelper.convert(mp4Video);
assertVideoConsistency(result, 512, 288, 29.97003F, 2.969F);
}
@Test
@Tag(Tags.VIDEO)
void convertM4vWithAudio() throws Exception {
var m4vVideo = loadResource("video_with_audio.m4v");
var result = MediaHelper.convert(m4vVideo);
assertVideoConsistency(result, 512, 214, 23.976025F, 2.962F);
}
@Test
@Tag(Tags.VIDEO)
void convertShortAndLowFpsVideo() throws Exception {
var webmVideo = loadResource("short_low_fps.webm");
var result = MediaHelper.convert(webmVideo);
assertVideoConsistency(result, 512, 288, 10F, 1.0F);
}
@Test
@Tag(Tags.VIDEO)
void resizeSmallWebmVideo() throws Exception {
var webmVideo = loadResource("small_video_sticker.webm");
var result = MediaHelper.convert(webmVideo);
assertVideoConsistency(result, 512, 212, 30F, 2.6F);
}
@Test
@Tag(Tags.VIDEO)
void convertVerticalWebmVideo() throws Exception {
var webmVideo = loadResource("vertical_video_sticker.webm");
var result = MediaHelper.convert(webmVideo);
assertVideoConsistency(result, 288, 512, 30F, 2.0F);
}
@Test
@Tag(Tags.VIDEO)
void convertGifVideo() throws Exception {
var gifVideo = loadResource("valid.gif");
var result = MediaHelper.convert(gifVideo);
assertVideoConsistency(result, 512, 274, 10F, 1.0F);
}
@Test
@Tag(Tags.VIDEO)
void convertAviVideo() throws Exception {
var aviVideo = loadResource("valid.avi");
var result = MediaHelper.convert(aviVideo);
assertVideoConsistency(result, 512, 512, 30F, 2.966F);
}
@Test
@Tag(Tags.VIDEO)
void noVideoConversionNeeded() throws Exception {
var webmVideo = loadResource("no_conversion_needed.webm");
var result = MediaHelper.convert(webmVideo);
assertThat(result, is(nullValue()));
}
@Test
@Tag(Tags.VIDEO)
void resizeAnimatedWebpVideo() {
var webpVideo = loadResource("animated.webp");
var ex = assertThrows(MediaException.class, () -> MediaHelper.convert(webpVideo));
assertThat(ex.getMessage(), equalTo("The file with image/webp MIME type is not supported"));
}
@Test
@Tag(Tags.ANIMATED_STICKER)
void noAnimatedStickerConversionNeeded() throws Exception {
var animatedSticker = loadResource("animated_sticker.tgs");
var result = MediaHelper.convert(animatedSticker);
assertThat(result, is(nullValue()));
}
@Test
@Tag(Tags.ANIMATED_STICKER)
void noLowFpsAnimatedStickerConversionNeeded() throws Exception {
var animatedSticker = loadResource("low_fps_animated_sticker.tgs");
var result = MediaHelper.convert(animatedSticker);
assertThat(result, is(nullValue()));
}
@Test
@Tag(Tags.ANIMATED_STICKER)
void nonCompliantAnimatedSticker() {
var animatedSticker = loadResource("non_compliant_animated_sticker.tgs");
var ex = assertThrows(MediaException.class, () -> MediaHelper.convert(animatedSticker));
assertThat(ex.getMessage(), equalTo("The file with application/gzip MIME type is not supported"));
}
@Test
@Tag(Tags.ANIMATED_STICKER)
void unsupportedGzipArchive() {
var archive = loadResource("unsupported_archive.gz");
var ex = assertThrows(MediaException.class, () -> MediaHelper.convert(archive));
assertThat(ex.getMessage(), equalTo("The file with application/gzip MIME type is not supported"));
}
@Test
@Tag(Tags.UNSUPPORTED_FILE)
void unsupportedFile() {
var document = loadResource("document.txt");
var ex = assertThrows(MediaException.class, () -> MediaHelper.convert(document));
assertThat(ex.getMessage(), equalTo("The file with text/plain MIME type is not supported"));
}
@Nested
@Tag(Tags.CONCURRENT)
@DisplayName("Concurrently convert")
@EnabledIfEnvironmentVariable(named = "CI", matches = "true")
class ConcurrencyTest {
@Test
@Tag(Tags.VIDEO)
@DisplayName("mov videos")
void concurrentMovVideoConversions() {
var movVideo = loadResource("long.mov");
executeConcurrentConversionsOf(movVideo);
}
private static void executeConcurrentConversionsOf(File inputFile) {
final int concurrentRequests = 50;
var failedConversions = new AtomicInteger(0);
var failureReasons = ConcurrentHashMap.newKeySet();
try (var executor = newVirtualThreadPerTaskExecutor()) {
IntStream.range(0, concurrentRequests).forEach(_ -> executor.execute(() -> {
try {
MediaHelper.convert(inputFile);
} catch (Throwable e) {
failedConversions.incrementAndGet();
failureReasons.add(e.getMessage());
}
}));
}
int failures = failedConversions.get();
var errorMessage = "Unable to process %d concurrent requests: %d conversions failed: %s".formatted(concurrentRequests, failures, failureReasons);
assertThat(errorMessage, failures, is(equalTo(0)));
}
@Test
@Tag(Tags.VIDEO)
@DisplayName("mp4 videos")
void concurrentMp4VideoConversions() {
var mp4Video = loadResource("video_with_audio.mp4");
executeConcurrentConversionsOf(mp4Video);
}
@Test
@Tag(Tags.VIDEO)
@DisplayName("m4v videos")
void concurrentM4vVideoConversions() {
var m4vVideo = loadResource("video_with_audio.m4v");
executeConcurrentConversionsOf(m4vVideo);
}
@Test
@Tag(Tags.VIDEO)
@DisplayName("webm videos")
void concurrentWebmVideoConversions() {
var webmVideo = loadResource("small_video_sticker.webm");
executeConcurrentConversionsOf(webmVideo);
}
@Test
@Tag(Tags.VIDEO)
@DisplayName("avi videos")
void concurrentAviVideoConversions() {
var aviVideo = loadResource("valid.avi");
executeConcurrentConversionsOf(aviVideo);
}
@Test
@Tag(Tags.VIDEO)
@DisplayName("gif videos")
void concurrentGifVideoConversions() {
var gifVideo = loadResource("valid.gif");
executeConcurrentConversionsOf(gifVideo);
}
@Test
@Tag(Tags.IMAGE)
@DisplayName("webp images")
void concurrentWebpImageConversions() {
var webpImage = loadResource("static.webp");
executeConcurrentConversionsOf(webpImage);
}
@Test
@Tag(Tags.IMAGE)
@DisplayName("jpg images")
void concurrentJpgImageConversions() {
var jpgImage = loadResource("big.jpg");
executeConcurrentConversionsOf(jpgImage);
}
@Test
@Tag(Tags.IMAGE)
@DisplayName("png images")
void concurrentPngImageConversions() {
var pngImage = loadResource("big.png");
executeConcurrentConversionsOf(pngImage);
}
@Test
@Tag(Tags.IMAGE)
@DisplayName("ico images")
void concurrentFaviconImageConversions() {
var faviconImage = loadResource("favicon.ico");
executeConcurrentConversionsOf(faviconImage);
}
@Test
@Tag(Tags.IMAGE)
@DisplayName("tiff images")
void concurrentTiffImageConversions() {
var tiffImage = loadResource("valid.tiff");
executeConcurrentConversionsOf(tiffImage);
}
@Test
@Tag(Tags.IMAGE)
@DisplayName("psd images")
void concurrentPsdImageConversions() {
var psdImage = loadResource("valid.psd");
executeConcurrentConversionsOf(psdImage);
}
@Test
@Tag(Tags.IMAGE)
@DisplayName("svg images")
void concurrentSvgImageConversions() throws Exception {
assumeSvgSupport();
var svgImage = loadResource("valid.svg");
executeConcurrentConversionsOf(svgImage);
}
}
}