Adds more tests for api

This commit is contained in:
Guillaume Tâche
2024-08-25 20:45:27 +02:00
parent e93a47f4e2
commit bf68d0a206
13 changed files with 307 additions and 47 deletions

View File

@@ -1,38 +0,0 @@
package com.github.gtache.autosubtitle;
import com.github.gtache.autosubtitle.subtitle.Subtitle;
import com.github.gtache.autosubtitle.translation.Translator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Objects;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class TestTranslator {
private final Translator translator;
private final Subtitle subtitle;
TestTranslator(@Mock final Translator translator, @Mock final Subtitle subtitle) {
this.translator = Objects.requireNonNull(translator);
this.subtitle = Objects.requireNonNull(subtitle);
}
@Test
void testGetLanguage() {
when(translator.getLanguage(subtitle)).thenCallRealMethod();
final var text = "text";
when(subtitle.content()).thenReturn(text);
when(translator.getLanguage(text)).thenReturn(Language.FR);
assertEquals(Language.FR, translator.getLanguage(subtitle));
verify(translator).getLanguage(text);
}
}

View File

@@ -0,0 +1,23 @@
package com.github.gtache.autosubtitle.archive;
import org.junit.jupiter.api.Test;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
class TestArchiver {
@Test
void testIsSupported() {
final var archiver = spy(Archiver.class);
when(archiver.archiveExtension()).thenReturn("test");
assertTrue(archiver.isPathSupported(Paths.get("x.test")));
assertFalse(archiver.isPathSupported(Paths.get("tes")));
assertFalse(archiver.isPathSupported(Paths.get("test")));
assertFalse(archiver.isPathSupported(Paths.get("test.txt")));
}
}

View File

@@ -1,6 +1,5 @@
package com.github.gtache.autosubtitle.com.github.gtache.autosubtitle.subtitle; package com.github.gtache.autosubtitle.subtitle;
import com.github.gtache.autosubtitle.subtitle.Subtitle;
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;

View File

@@ -0,0 +1,26 @@
package com.github.gtache.autosubtitle.subtitle;
import com.github.gtache.autosubtitle.VideoInfo;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import static org.mockito.Mockito.*;
class TestSubtitleImporterExporter {
@Test
void testExportSubtitleCollection() throws IOException {
final var importExporter = spy(SubtitleImporterExporter.class);
final var collection = mock(SubtitleCollection.class);
final var videoInfo = Mockito.mock(VideoInfo.class);
final var file = mock(Path.class);
importExporter.exportSubtitles(collection, videoInfo, file);
verify(importExporter).exportSubtitles(List.of(collection), videoInfo, file);
}
}

View File

@@ -1,9 +1,7 @@
package com.github.gtache.autosubtitle.com.github.gtache.autosubtitle.subtitle.converter; package com.github.gtache.autosubtitle.subtitle.converter;
import com.github.gtache.autosubtitle.subtitle.Subtitle; import com.github.gtache.autosubtitle.subtitle.Subtitle;
import com.github.gtache.autosubtitle.subtitle.SubtitleCollection; import com.github.gtache.autosubtitle.subtitle.SubtitleCollection;
import com.github.gtache.autosubtitle.subtitle.converter.ParseException;
import com.github.gtache.autosubtitle.subtitle.converter.SubtitleConverter;
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.junit.jupiter.api.io.TempDir; import org.junit.jupiter.api.io.TempDir;

View File

@@ -1,12 +1,9 @@
package com.github.gtache.autosubtitle.com.github.gtache.autosubtitle.subtitle.extractor; package com.github.gtache.autosubtitle.subtitle.extractor;
import com.github.gtache.autosubtitle.Audio; import com.github.gtache.autosubtitle.Audio;
import com.github.gtache.autosubtitle.Language; import com.github.gtache.autosubtitle.Language;
import com.github.gtache.autosubtitle.Video; import com.github.gtache.autosubtitle.Video;
import com.github.gtache.autosubtitle.subtitle.SubtitleCollection; import com.github.gtache.autosubtitle.subtitle.SubtitleCollection;
import com.github.gtache.autosubtitle.subtitle.extractor.ExtractException;
import com.github.gtache.autosubtitle.subtitle.extractor.ExtractionModel;
import com.github.gtache.autosubtitle.subtitle.extractor.SubtitleExtractor;
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;

View File

@@ -0,0 +1,82 @@
package com.github.gtache.autosubtitle.translation;
import com.github.gtache.autosubtitle.Language;
import com.github.gtache.autosubtitle.subtitle.Subtitle;
import com.github.gtache.autosubtitle.subtitle.SubtitleCollection;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Objects;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class TestTranslator {
private final Translator<Subtitle> translator;
private final Subtitle subtitle;
private final Language from;
private final Language to;
TestTranslator(@Mock final Translator<Subtitle> translator, @Mock final Subtitle subtitle, @Mock final Language from, @Mock final Language to) {
this.translator = Objects.requireNonNull(translator);
this.subtitle = Objects.requireNonNull(subtitle);
this.from = Objects.requireNonNull(from);
this.to = Objects.requireNonNull(to);
}
@Test
void testGetLanguage() {
when(translator.getLanguage(subtitle)).thenCallRealMethod();
final var text = "text";
when(subtitle.content()).thenReturn(text);
when(translator.getLanguage(text)).thenReturn(Language.FR);
assertEquals(Language.FR, translator.getLanguage(subtitle));
verify(translator).getLanguage(text);
}
@Test
void testTranslate() throws TranslationException {
when(translator.translate(anyString(), any(Language.class))).thenCallRealMethod();
final var text = "text";
when(translator.getLanguage(text)).thenReturn(from);
final var expected = "translated";
when(translator.translate(text, from, to)).thenReturn(expected);
assertEquals(expected, translator.translate(text, to));
verify(translator).translate(text, from, to);
verify(translator).getLanguage(text);
}
@Test
void testTranslateSubtitle() throws TranslationException {
when(translator.translate(any(Subtitle.class), any(Language.class))).thenCallRealMethod();
when(translator.getLanguage(subtitle)).thenReturn(from);
final var expected = mock(Subtitle.class);
when(translator.translate(subtitle, from, to)).thenReturn(expected);
assertEquals(expected, translator.translate(subtitle, to));
verify(translator).translate(subtitle, from, to);
verify(translator).getLanguage(subtitle);
}
@Test
void testTranslateCollection() throws TranslationException {
when(translator.translate(any(SubtitleCollection.class), any(Language.class))).thenCallRealMethod();
final var collection = mock(SubtitleCollection.class);
final var text = "test";
when(collection.text()).thenReturn(text);
when(translator.getLanguage(text)).thenReturn(from);
final var expected = mock(SubtitleCollection.class);
when(translator.translate(collection, from, to)).thenReturn(expected);
assertEquals(expected, translator.translate(collection, to));
verify(translator).translate(collection, from, to);
verify(translator).getLanguage(text);
}
}

View File

@@ -0,0 +1,27 @@
package com.github.gtache.autosubtitle.archive.client;
import com.github.gtache.autosubtitle.archive.Archiver;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
/**
* {@link Archiver} using a remote API
*/
public class RemoteArchiver implements Archiver {
@Override
public void compress(final List<Path> files, final Path destination) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void decompress(final Path archive, final Path destination) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public String archiveExtension() {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,40 @@
package com.github.gtache.autosubtitle.client;
import com.github.gtache.autosubtitle.Audio;
import com.github.gtache.autosubtitle.Video;
import com.github.gtache.autosubtitle.VideoConverter;
import com.github.gtache.autosubtitle.subtitle.SubtitleCollection;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
/**
* {@link VideoConverter} using a remote API
*/
public class RemoteVideoConverter implements VideoConverter {
@Override
public Video addSoftSubtitles(final Video video, final Collection<? extends SubtitleCollection<?>> subtitles) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void addSoftSubtitles(final Video video, final Collection<? extends SubtitleCollection<?>> subtitles, final Path path) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public Video addHardSubtitles(final Video video, final SubtitleCollection<?> subtitles) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public void addHardSubtitles(final Video video, final SubtitleCollection<?> subtitles, final Path path) throws IOException {
throw new UnsupportedOperationException();
}
@Override
public Audio getAudio(final Video video) throws IOException {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,17 @@
package com.github.gtache.autosubtitle.client;
import com.github.gtache.autosubtitle.Video;
import com.github.gtache.autosubtitle.VideoLoader;
import java.io.IOException;
import java.nio.file.Path;
/**
* {@link VideoLoader} using a remote API
*/
public class RemoteVideoLoader implements VideoLoader {
@Override
public Video loadVideo(final Path path) throws IOException {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,29 @@
package com.github.gtache.autosubtitle.subtitle.converter.client;
import com.github.gtache.autosubtitle.VideoInfo;
import com.github.gtache.autosubtitle.subtitle.Subtitle;
import com.github.gtache.autosubtitle.subtitle.SubtitleCollection;
import com.github.gtache.autosubtitle.subtitle.converter.ParseException;
import com.github.gtache.autosubtitle.subtitle.converter.SubtitleConverter;
/**
* {@link SubtitleConverter} using a remote API
*
* @param <T> the type of subtitle
*/
public class RemoteSubtitleConverter<T extends Subtitle> implements SubtitleConverter<T> {
@Override
public String format(final SubtitleCollection<?> collection, final VideoInfo videoInfo) {
throw new UnsupportedOperationException();
}
@Override
public SubtitleCollection<T> parse(final String content) throws ParseException {
throw new UnsupportedOperationException();
}
@Override
public String formatName() {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,26 @@
package com.github.gtache.autosubtitle.subtitle.extractor.client;
import com.github.gtache.autosubtitle.Audio;
import com.github.gtache.autosubtitle.Language;
import com.github.gtache.autosubtitle.Video;
import com.github.gtache.autosubtitle.subtitle.SubtitleCollection;
import com.github.gtache.autosubtitle.subtitle.extractor.ExtractException;
import com.github.gtache.autosubtitle.subtitle.extractor.ExtractionModel;
import com.github.gtache.autosubtitle.subtitle.extractor.SubtitleExtractor;
import com.github.gtache.autosubtitle.subtitle.extractor.impl.AbstractSubtitleExtractor;
/**
* {@link SubtitleExtractor} using a remote API
*/
public class RemoteSubtitleExtractor extends AbstractSubtitleExtractor {
@Override
public SubtitleCollection extract(final Video video, final Language language, final ExtractionModel model) throws ExtractException {
throw new UnsupportedOperationException();
}
@Override
public SubtitleCollection extract(final Audio audio, final Language language, final ExtractionModel model) throws ExtractException {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,34 @@
package com.github.gtache.autosubtitle.translation.client;
import com.github.gtache.autosubtitle.Language;
import com.github.gtache.autosubtitle.subtitle.Subtitle;
import com.github.gtache.autosubtitle.subtitle.SubtitleCollection;
import com.github.gtache.autosubtitle.translation.TranslationException;
import com.github.gtache.autosubtitle.translation.Translator;
/**
* {@link Translator} using a remote API
*
* @param <T> the type of subtitle
*/
public class RemoteTranslator<T extends Subtitle> implements Translator<T> {
@Override
public Language getLanguage(final String text) {
throw new UnsupportedOperationException();
}
@Override
public String translate(final String text, final Language from, final Language to) throws TranslationException {
throw new UnsupportedOperationException();
}
@Override
public T translate(final Subtitle subtitle, final Language from, final Language to) throws TranslationException {
throw new UnsupportedOperationException();
}
@Override
public SubtitleCollection<T> translate(final SubtitleCollection<?> collection, final Language from, final Language to) throws TranslationException {
throw new UnsupportedOperationException();
}
}