test(unit): improve test coverage
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package com.ddf.vodsystem.services.media;
|
package com.ddf.vodsystem.services.media;
|
||||||
|
|
||||||
|
import com.ddf.vodsystem.dto.CommandOutput;
|
||||||
import com.ddf.vodsystem.dto.ProgressTracker;
|
import com.ddf.vodsystem.dto.ProgressTracker;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
@@ -8,11 +10,14 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.anyList;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
class RemuxServiceTest {
|
class RemuxServiceTest {
|
||||||
@@ -25,24 +30,57 @@ class RemuxServiceTest {
|
|||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
remuxService = new RemuxService(commandRunner);
|
remuxService = new RemuxService(commandRunner);
|
||||||
|
Thread.interrupted();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void clearInterruptFlag() {
|
||||||
|
Thread.interrupted();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void remux_buildsExpectedFfmpegCommand() throws Exception {
|
void remux_success_completesWithCommandOutput() throws Exception {
|
||||||
File input = new File("videos/input.ts");
|
File input = new File("videos/input.ts");
|
||||||
File output = new File("videos/output.mp4");
|
File output = new File("videos/output.mp4");
|
||||||
|
CommandOutput commandOutput = new CommandOutput();
|
||||||
|
when(commandRunner.run(anyList(), any())).thenReturn(commandOutput);
|
||||||
|
|
||||||
remuxService.remux(input, output, new ProgressTracker(), 60f);
|
var future = remuxService.remux(input, output, new ProgressTracker(), 60f);
|
||||||
|
|
||||||
verify(commandRunner).run(eq(List.of(
|
assertThat(future.get()).isSameAs(commandOutput);
|
||||||
"ffmpeg",
|
}
|
||||||
"-progress", "pipe:1",
|
|
||||||
"-y",
|
@Test
|
||||||
"-i", input.getAbsolutePath(),
|
void remux_ioFailure_completesExceptionally() throws Exception {
|
||||||
"-c:v", "h264",
|
when(commandRunner.run(anyList(), any())).thenThrow(new IOException("ffmpeg failed"));
|
||||||
"-c:a", "aac",
|
|
||||||
"-f", "mp4",
|
var future = remuxService.remux(
|
||||||
output.getAbsolutePath()
|
new File("videos/input.ts"),
|
||||||
)), any());
|
new File("videos/output.mp4"),
|
||||||
|
new ProgressTracker(),
|
||||||
|
60f
|
||||||
|
);
|
||||||
|
|
||||||
|
assertThatThrownBy(future::get)
|
||||||
|
.isInstanceOf(ExecutionException.class)
|
||||||
|
.hasCauseInstanceOf(IOException.class);
|
||||||
|
assertThat(Thread.currentThread().isInterrupted()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void remux_interruption_completesExceptionallyAndRestoresInterruptFlag() throws Exception {
|
||||||
|
when(commandRunner.run(anyList(), any())).thenThrow(new InterruptedException("interrupted"));
|
||||||
|
|
||||||
|
var future = remuxService.remux(
|
||||||
|
new File("videos/input.ts"),
|
||||||
|
new File("videos/output.mp4"),
|
||||||
|
new ProgressTracker(),
|
||||||
|
60f
|
||||||
|
);
|
||||||
|
|
||||||
|
assertThatThrownBy(future::get)
|
||||||
|
.isInstanceOf(ExecutionException.class)
|
||||||
|
.hasCauseInstanceOf(InterruptedException.class);
|
||||||
|
assertThat(Thread.currentThread().isInterrupted()).isTrue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,21 @@
|
|||||||
package com.ddf.vodsystem.services.media;
|
package com.ddf.vodsystem.services.media;
|
||||||
|
|
||||||
|
import com.ddf.vodsystem.dto.CommandOutput;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.List;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyList;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
class ThumbnailServiceTest {
|
class ThumbnailServiceTest {
|
||||||
@@ -22,21 +28,55 @@ class ThumbnailServiceTest {
|
|||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
thumbnailService = new ThumbnailService(commandRunner);
|
thumbnailService = new ThumbnailService(commandRunner);
|
||||||
|
Thread.interrupted();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void clearInterruptFlag() {
|
||||||
|
Thread.interrupted();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createThumbnail_buildsExpectedFfmpegCommand() throws Exception {
|
void createThumbnail_success_completesWithCommandOutput() throws Exception {
|
||||||
Path input = Path.of("videos/input.mp4");
|
Path input = Path.of("videos/input.mp4");
|
||||||
Path output = Path.of("thumbnails/output.jpg");
|
Path output = Path.of("thumbnails/output.jpg");
|
||||||
|
CommandOutput commandOutput = new CommandOutput();
|
||||||
|
when(commandRunner.run(anyList())).thenReturn(commandOutput);
|
||||||
|
|
||||||
thumbnailService.createThumbnail(input, output, 12.5f);
|
var future = thumbnailService.createThumbnail(input, output, 12.5f);
|
||||||
|
|
||||||
verify(commandRunner).run(List.of(
|
assertThat(future.get()).isSameAs(commandOutput);
|
||||||
"ffmpeg",
|
}
|
||||||
"-ss", "12.5",
|
|
||||||
"-i", input.toAbsolutePath().toString(),
|
@Test
|
||||||
"-frames:v", "1",
|
void createThumbnail_ioFailure_completesExceptionally() throws Exception {
|
||||||
output.toAbsolutePath().toString()
|
when(commandRunner.run(anyList())).thenThrow(new IOException("ffmpeg failed"));
|
||||||
));
|
|
||||||
|
var future = thumbnailService.createThumbnail(
|
||||||
|
Path.of("videos/input.mp4"),
|
||||||
|
Path.of("thumbnails/output.jpg"),
|
||||||
|
12.5f
|
||||||
|
);
|
||||||
|
|
||||||
|
assertThatThrownBy(future::get)
|
||||||
|
.isInstanceOf(ExecutionException.class)
|
||||||
|
.hasCauseInstanceOf(IOException.class);
|
||||||
|
assertThat(Thread.currentThread().isInterrupted()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createThumbnail_interruption_completesExceptionallyAndRestoresInterruptFlag() throws Exception {
|
||||||
|
when(commandRunner.run(anyList())).thenThrow(new InterruptedException("interrupted"));
|
||||||
|
|
||||||
|
var future = thumbnailService.createThumbnail(
|
||||||
|
Path.of("videos/input.mp4"),
|
||||||
|
Path.of("thumbnails/output.jpg"),
|
||||||
|
12.5f
|
||||||
|
);
|
||||||
|
|
||||||
|
assertThatThrownBy(future::get)
|
||||||
|
.isInstanceOf(ExecutionException.class)
|
||||||
|
.hasCauseInstanceOf(InterruptedException.class);
|
||||||
|
assertThat(Thread.currentThread().isInterrupted()).isTrue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user