test(unit): improve test coverage

This commit is contained in:
2026-07-12 19:06:10 +01:00
parent bb6f5e7823
commit f9b055b3b5
2 changed files with 105 additions and 27 deletions

View File

@@ -1,6 +1,8 @@
package com.ddf.vodsystem.services.media;
import com.ddf.vodsystem.dto.CommandOutput;
import com.ddf.vodsystem.dto.ProgressTracker;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -8,11 +10,14 @@ import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
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.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class RemuxServiceTest {
@@ -25,24 +30,57 @@ class RemuxServiceTest {
@BeforeEach
void setUp() {
remuxService = new RemuxService(commandRunner);
Thread.interrupted();
}
@AfterEach
void clearInterruptFlag() {
Thread.interrupted();
}
@Test
void remux_buildsExpectedFfmpegCommand() throws Exception {
void remux_success_completesWithCommandOutput() throws Exception {
File input = new File("videos/input.ts");
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(
"ffmpeg",
"-progress", "pipe:1",
"-y",
"-i", input.getAbsolutePath(),
"-c:v", "h264",
"-c:a", "aac",
"-f", "mp4",
output.getAbsolutePath()
)), any());
assertThat(future.get()).isSameAs(commandOutput);
}
@Test
void remux_ioFailure_completesExceptionally() throws Exception {
when(commandRunner.run(anyList(), any())).thenThrow(new IOException("ffmpeg failed"));
var future = remuxService.remux(
new File("videos/input.ts"),
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();
}
}