Support File Move Operations with Watcher Refactor for Accurate Tracking
This commit is contained in:
committed by
Aditya Chandel
parent
1c9c09628a
commit
841e359b92
+524
@@ -0,0 +1,524 @@
|
||||
package com.adityachandel.booklore;
|
||||
|
||||
import com.adityachandel.booklore.mapper.BookMapper;
|
||||
import com.adityachandel.booklore.model.dto.request.FileMoveRequest;
|
||||
import com.adityachandel.booklore.model.entity.*;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.service.BookQueryService;
|
||||
import com.adityachandel.booklore.service.NotificationService;
|
||||
import com.adityachandel.booklore.service.file.FileMoveService;
|
||||
import com.adityachandel.booklore.service.monitoring.MonitoringService;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class FileMoveServiceMoveFilesTest {
|
||||
|
||||
@Mock
|
||||
private BookQueryService bookQueryService;
|
||||
|
||||
@Mock
|
||||
private BookRepository bookRepository;
|
||||
|
||||
@Mock
|
||||
private BookMapper bookMapper;
|
||||
|
||||
@Mock
|
||||
private NotificationService notificationService;
|
||||
|
||||
@Mock
|
||||
private MonitoringService monitoringService;
|
||||
|
||||
@InjectMocks
|
||||
private FileMoveService fileMoveService;
|
||||
|
||||
@TempDir
|
||||
Path tempLibraryRoot;
|
||||
|
||||
private BookEntity createBookWithFile(Path libraryRoot, String fileSubPath, String fileName) throws IOException {
|
||||
LibraryEntity library = LibraryEntity.builder()
|
||||
.id(42L)
|
||||
.name("Test Library")
|
||||
.build();
|
||||
|
||||
LibraryPathEntity libraryPathEntity = LibraryPathEntity.builder()
|
||||
.path(libraryRoot.toString())
|
||||
.library(library)
|
||||
.build();
|
||||
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("Test Book")
|
||||
.authors(new HashSet<>(List.of(new AuthorEntity(1L, "Author Name", new ArrayList<>()))))
|
||||
.publishedDate(LocalDate.of(2020, 1, 1))
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.id(1L)
|
||||
.fileName(fileName)
|
||||
.fileSubPath(fileSubPath)
|
||||
.metadata(metadata)
|
||||
.libraryPath(libraryPathEntity)
|
||||
.build();
|
||||
|
||||
Path oldFilePath = book.getFullFilePath();
|
||||
Files.createDirectories(oldFilePath.getParent());
|
||||
Files.createFile(oldFilePath);
|
||||
|
||||
return book;
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMoveFiles_successfulMove() throws IOException {
|
||||
String fileName = "testbook.epub";
|
||||
String fileSubPath = "oldfolder";
|
||||
|
||||
BookEntity book = createBookWithFile(tempLibraryRoot, fileSubPath, fileName);
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(1L))).thenReturn(List.of(book));
|
||||
|
||||
FileMoveRequest request = new FileMoveRequest();
|
||||
request.setBookIds(Set.of(1L));
|
||||
request.setPattern("NewFolder/{title}");
|
||||
|
||||
Path oldFilePath = book.getFullFilePath();
|
||||
|
||||
fileMoveService.moveFiles(request);
|
||||
|
||||
Path expectedNewPath = tempLibraryRoot.resolve("NewFolder").resolve("Test Book.epub");
|
||||
assertThat(Files.exists(expectedNewPath))
|
||||
.withFailMessage("Expected the file to be moved to %s", expectedNewPath)
|
||||
.isTrue();
|
||||
|
||||
assertThat(Files.exists(oldFilePath))
|
||||
.withFailMessage("Original file path should no longer exist: %s", oldFilePath)
|
||||
.isFalse();
|
||||
|
||||
Files.deleteIfExists(expectedNewPath);
|
||||
Files.deleteIfExists(expectedNewPath.getParent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMoveFiles_skipsNonexistentFile() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("NoFile")
|
||||
.build();
|
||||
|
||||
LibraryPathEntity libraryPathEntity = LibraryPathEntity.builder()
|
||||
.path(tempLibraryRoot.toString())
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.id(2L)
|
||||
.fileName("nofile.epub")
|
||||
.fileSubPath("subfolder")
|
||||
.metadata(metadata)
|
||||
.libraryPath(libraryPathEntity)
|
||||
.build();
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(2L))).thenReturn(List.of(book));
|
||||
|
||||
FileMoveRequest request = new FileMoveRequest();
|
||||
request.setBookIds(Set.of(2L));
|
||||
request.setPattern("Moved/{title}");
|
||||
|
||||
fileMoveService.moveFiles(request);
|
||||
|
||||
Path expectedNewPath = tempLibraryRoot.resolve("Moved").resolve("NoFile.epub");
|
||||
assertThat(Files.exists(expectedNewPath))
|
||||
.withFailMessage("No file should be created for nonexistent source")
|
||||
.isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMoveFiles_skipsBookWithoutLibraryPath() throws IOException {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("MissingLibrary")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.id(3L)
|
||||
.fileName("missinglibrary.epub")
|
||||
.fileSubPath("subfolder")
|
||||
.metadata(metadata)
|
||||
.libraryPath(null)
|
||||
.build();
|
||||
|
||||
Path fakeOldFile = tempLibraryRoot.resolve("subfolder").resolve("missinglibrary.epub");
|
||||
Files.createDirectories(fakeOldFile.getParent());
|
||||
Files.createFile(fakeOldFile);
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(3L))).thenReturn(List.of(book));
|
||||
|
||||
FileMoveRequest request = new FileMoveRequest();
|
||||
request.setBookIds(Set.of(3L));
|
||||
request.setPattern("Moved/{title}");
|
||||
|
||||
fileMoveService.moveFiles(request);
|
||||
|
||||
Path expectedNewPath = tempLibraryRoot.resolve("Moved").resolve("MissingLibrary.epub");
|
||||
assertThat(Files.exists(expectedNewPath))
|
||||
.withFailMessage("File should not be moved if library path is missing")
|
||||
.isFalse();
|
||||
assertThat(Files.exists(fakeOldFile))
|
||||
.withFailMessage("Original file should remain when library path is missing")
|
||||
.isTrue();
|
||||
|
||||
Files.deleteIfExists(fakeOldFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMoveFiles_handlesEmptyPattern() throws IOException {
|
||||
String fileName = "emptypattern.epub";
|
||||
String fileSubPath = "folder";
|
||||
|
||||
BookEntity book = createBookWithFile(tempLibraryRoot, fileSubPath, fileName);
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(4L))).thenReturn(List.of(book));
|
||||
|
||||
FileMoveRequest request = new FileMoveRequest();
|
||||
request.setBookIds(Set.of(4L));
|
||||
request.setPattern("");
|
||||
|
||||
Path oldFilePath = book.getFullFilePath();
|
||||
|
||||
fileMoveService.moveFiles(request);
|
||||
|
||||
Path expectedNewPath = tempLibraryRoot.resolve(fileName);
|
||||
assertThat(Files.exists(expectedNewPath))
|
||||
.withFailMessage("File should be moved to the original filename path when pattern is empty")
|
||||
.isTrue();
|
||||
|
||||
assertThat(Files.exists(oldFilePath))
|
||||
.withFailMessage("Original file path should no longer exist")
|
||||
.isFalse();
|
||||
|
||||
Files.deleteIfExists(expectedNewPath);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMoveFiles_removesIllegalCharactersFromPath() throws IOException {
|
||||
String fileName = "badchars.epub";
|
||||
String fileSubPath = "folder";
|
||||
|
||||
BookEntity book = createBookWithFile(tempLibraryRoot, fileSubPath, fileName);
|
||||
book.getMetadata().setTitle("Bad|Title:Test*?");
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(5L))).thenReturn(List.of(book));
|
||||
|
||||
FileMoveRequest request = new FileMoveRequest();
|
||||
request.setBookIds(Set.of(5L));
|
||||
request.setPattern("{title}");
|
||||
|
||||
Path oldFilePath = book.getFullFilePath();
|
||||
|
||||
fileMoveService.moveFiles(request);
|
||||
|
||||
Path expectedNewPath = tempLibraryRoot.resolve("BadTitleTest.epub");
|
||||
assertThat(Files.exists(expectedNewPath))
|
||||
.withFailMessage("Illegal characters should be removed from generated path")
|
||||
.isTrue();
|
||||
|
||||
assertThat(Files.exists(oldFilePath))
|
||||
.withFailMessage("Original file path should no longer exist")
|
||||
.isFalse();
|
||||
|
||||
Files.deleteIfExists(expectedNewPath);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMoveFiles_deletesEmptyOldDirectories() throws IOException {
|
||||
String fileName = "deleteold.epub";
|
||||
String fileSubPath = "folder/emptydir";
|
||||
|
||||
BookEntity book = createBookWithFile(tempLibraryRoot, fileSubPath, fileName);
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(6L))).thenReturn(List.of(book));
|
||||
|
||||
FileMoveRequest request = new FileMoveRequest();
|
||||
request.setBookIds(Set.of(6L));
|
||||
request.setPattern("NewFolder/{title}");
|
||||
|
||||
Path oldDir = tempLibraryRoot.resolve(fileSubPath).normalize();
|
||||
assertThat(Files.exists(oldDir))
|
||||
.withFailMessage("Precondition failed: old directory must exist before move")
|
||||
.isTrue();
|
||||
|
||||
fileMoveService.moveFiles(request);
|
||||
|
||||
Path expectedNewPath = tempLibraryRoot.resolve("NewFolder").resolve("Test Book.epub");
|
||||
assertThat(Files.exists(expectedNewPath))
|
||||
.withFailMessage("Expected file to be moved to new location")
|
||||
.isTrue();
|
||||
|
||||
assertThat(Files.exists(oldDir))
|
||||
.withFailMessage("Expected old empty directory to be deleted after move")
|
||||
.isFalse();
|
||||
|
||||
assertThat(Files.exists(oldDir.getParent()))
|
||||
.withFailMessage("Expected parent directory of old directory to be deleted if empty")
|
||||
.isFalse();
|
||||
|
||||
Files.deleteIfExists(expectedNewPath);
|
||||
Files.deleteIfExists(expectedNewPath.getParent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMoveFiles_keepsOldDirectoryIfNotEmpty() throws IOException {
|
||||
String fileName = "keepold.epub";
|
||||
String fileSubPath = "folder/notempty";
|
||||
|
||||
BookEntity book = createBookWithFile(tempLibraryRoot, fileSubPath, fileName);
|
||||
|
||||
Path oldDir = tempLibraryRoot.resolve(fileSubPath).normalize();
|
||||
Files.createDirectories(oldDir);
|
||||
|
||||
Path otherFile = oldDir.resolve("otherfile.txt");
|
||||
Files.createFile(otherFile);
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(7L))).thenReturn(List.of(book));
|
||||
|
||||
FileMoveRequest request = new FileMoveRequest();
|
||||
request.setBookIds(Set.of(7L));
|
||||
request.setPattern("NewFolder/{title}");
|
||||
|
||||
Path oldFilePath = book.getFullFilePath();
|
||||
|
||||
fileMoveService.moveFiles(request);
|
||||
|
||||
Path expectedNewPath = tempLibraryRoot.resolve("NewFolder").resolve("Test Book.epub");
|
||||
|
||||
assertThat(Files.exists(expectedNewPath))
|
||||
.withFailMessage("Expected file to be moved to new location")
|
||||
.isTrue();
|
||||
assertThat(Files.exists(oldFilePath))
|
||||
.withFailMessage("Original file path should no longer exist")
|
||||
.isFalse();
|
||||
|
||||
assertThat(Files.exists(oldDir))
|
||||
.withFailMessage("Old directory should remain if it contains other files")
|
||||
.isTrue();
|
||||
assertThat(Files.exists(otherFile))
|
||||
.withFailMessage("Other files in old directory should remain intact")
|
||||
.isTrue();
|
||||
|
||||
Files.deleteIfExists(otherFile);
|
||||
Files.deleteIfExists(expectedNewPath);
|
||||
Files.deleteIfExists(expectedNewPath.getParent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMoveFiles_handlesMultipleBooks() throws IOException {
|
||||
BookEntity book1 = createBookWithFile(tempLibraryRoot, "folder1", "book1.epub");
|
||||
book1.getMetadata().setTitle("Book One");
|
||||
|
||||
BookEntity book2 = createBookWithFile(tempLibraryRoot, "folder2", "book2.epub");
|
||||
book2.getMetadata().setTitle("Book Two");
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(8L, 9L))).thenReturn(List.of(book1, book2));
|
||||
|
||||
FileMoveRequest request = new FileMoveRequest();
|
||||
request.setBookIds(Set.of(8L, 9L));
|
||||
request.setPattern("Moved/{title}");
|
||||
|
||||
Path oldPath1 = book1.getFullFilePath();
|
||||
Path oldPath2 = book2.getFullFilePath();
|
||||
|
||||
fileMoveService.moveFiles(request);
|
||||
|
||||
Path expectedPath1 = tempLibraryRoot.resolve("Moved").resolve("Book One.epub");
|
||||
Path expectedPath2 = tempLibraryRoot.resolve("Moved").resolve("Book Two.epub");
|
||||
|
||||
assertThat(Files.exists(expectedPath1))
|
||||
.withFailMessage("Expected first book to be moved to new location")
|
||||
.isTrue();
|
||||
assertThat(Files.exists(expectedPath2))
|
||||
.withFailMessage("Expected second book to be moved to new location")
|
||||
.isTrue();
|
||||
|
||||
assertThat(Files.exists(oldPath1))
|
||||
.withFailMessage("Original path for first book should no longer exist")
|
||||
.isFalse();
|
||||
assertThat(Files.exists(oldPath2))
|
||||
.withFailMessage("Original path for second book should no longer exist")
|
||||
.isFalse();
|
||||
|
||||
Files.deleteIfExists(expectedPath1);
|
||||
Files.deleteIfExists(expectedPath2);
|
||||
Files.deleteIfExists(expectedPath1.getParent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMoveFiles_skipsBookWithNullFileName() throws IOException {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("NullFileName")
|
||||
.build();
|
||||
|
||||
LibraryPathEntity libraryPathEntity = LibraryPathEntity.builder()
|
||||
.path(tempLibraryRoot.toString())
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.id(10L)
|
||||
.fileName(null)
|
||||
.fileSubPath("folder")
|
||||
.metadata(metadata)
|
||||
.libraryPath(libraryPathEntity)
|
||||
.build();
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(10L))).thenReturn(List.of(book));
|
||||
|
||||
FileMoveRequest request = new FileMoveRequest();
|
||||
request.setBookIds(Set.of(10L));
|
||||
request.setPattern("Moved/{title}");
|
||||
|
||||
fileMoveService.moveFiles(request);
|
||||
|
||||
Path expectedNewPath = tempLibraryRoot.resolve("Moved").resolve("NullFileName");
|
||||
assertThat(Files.exists(expectedNewPath))
|
||||
.withFailMessage("File should not be moved if filename is null")
|
||||
.isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMoveFiles_skipsBookWithEmptyFileName() throws IOException {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("EmptyFileName")
|
||||
.build();
|
||||
|
||||
LibraryPathEntity libraryPathEntity = LibraryPathEntity.builder()
|
||||
.path(tempLibraryRoot.toString())
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.id(11L)
|
||||
.fileName("")
|
||||
.fileSubPath("folder")
|
||||
.metadata(metadata)
|
||||
.libraryPath(libraryPathEntity)
|
||||
.build();
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(11L))).thenReturn(List.of(book));
|
||||
|
||||
FileMoveRequest request = new FileMoveRequest();
|
||||
request.setBookIds(Set.of(11L));
|
||||
request.setPattern("Moved/{title}");
|
||||
|
||||
fileMoveService.moveFiles(request);
|
||||
|
||||
Path expectedNewPath = tempLibraryRoot.resolve("Moved").resolve("EmptyFileName");
|
||||
assertThat(Files.exists(expectedNewPath))
|
||||
.withFailMessage("File should not be moved if filename is empty")
|
||||
.isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMoveFiles_skipsBookWithNullFileSubPath() throws IOException {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("NullSubPath")
|
||||
.build();
|
||||
|
||||
LibraryPathEntity libraryPathEntity = LibraryPathEntity.builder()
|
||||
.path(tempLibraryRoot.toString())
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.id(12L)
|
||||
.fileName("file.epub")
|
||||
.fileSubPath(null)
|
||||
.metadata(metadata)
|
||||
.libraryPath(libraryPathEntity)
|
||||
.build();
|
||||
|
||||
Path oldFile = tempLibraryRoot.resolve("file.epub");
|
||||
Files.createFile(oldFile);
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(12L))).thenReturn(List.of(book));
|
||||
|
||||
FileMoveRequest request = new FileMoveRequest();
|
||||
request.setBookIds(Set.of(12L));
|
||||
request.setPattern("Moved/{title}");
|
||||
|
||||
fileMoveService.moveFiles(request);
|
||||
|
||||
Path expectedNewPath = tempLibraryRoot.resolve("Moved").resolve("NullSubPath.epub");
|
||||
assertThat(Files.exists(expectedNewPath))
|
||||
.withFailMessage("File should not be moved if fileSubPath is null")
|
||||
.isFalse();
|
||||
|
||||
Files.deleteIfExists(oldFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMoveFiles_skipsBookWithNullMetadata() throws IOException {
|
||||
LibraryPathEntity libraryPathEntity = LibraryPathEntity.builder()
|
||||
.path(tempLibraryRoot.toString())
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.id(13L)
|
||||
.fileName("file.epub")
|
||||
.fileSubPath("folder")
|
||||
.metadata(null)
|
||||
.libraryPath(libraryPathEntity)
|
||||
.build();
|
||||
|
||||
Path oldFile = tempLibraryRoot.resolve("folder").resolve("file.epub");
|
||||
Files.createDirectories(oldFile.getParent());
|
||||
Files.createFile(oldFile);
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(13L))).thenReturn(List.of(book));
|
||||
|
||||
FileMoveRequest request = new FileMoveRequest();
|
||||
request.setBookIds(Set.of(13L));
|
||||
request.setPattern("Moved/{title}");
|
||||
|
||||
fileMoveService.moveFiles(request);
|
||||
|
||||
Path expectedNewPath = tempLibraryRoot.resolve("Moved").resolve(".epub");
|
||||
assertThat(Files.exists(expectedNewPath))
|
||||
.withFailMessage("File should not be moved if metadata is null")
|
||||
.isFalse();
|
||||
|
||||
Files.deleteIfExists(oldFile);
|
||||
}
|
||||
|
||||
@Disabled
|
||||
@Test
|
||||
void testMoveFiles_patternWithUnknownPlaceholder() throws IOException {
|
||||
BookEntity book = createBookWithFile(tempLibraryRoot, "folder", "unknown.epub");
|
||||
book.getMetadata().setTitle("UnknownPlaceholder");
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(14L))).thenReturn(List.of(book));
|
||||
|
||||
FileMoveRequest request = new FileMoveRequest();
|
||||
request.setBookIds(Set.of(14L));
|
||||
request.setPattern("Moved/{title}/{foo}");
|
||||
|
||||
fileMoveService.moveFiles(request);
|
||||
|
||||
Path expectedNewPath = tempLibraryRoot.resolve("Moved").resolve("UnknownPlaceholder").resolve("{foo}.epub");
|
||||
assertThat(Files.exists(expectedNewPath))
|
||||
.withFailMessage("Unknown placeholders should remain in the generated path")
|
||||
.isTrue();
|
||||
|
||||
Files.deleteIfExists(expectedNewPath);
|
||||
Files.deleteIfExists(expectedNewPath.getParent());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,406 @@
|
||||
package com.adityachandel.booklore;
|
||||
|
||||
import com.adityachandel.booklore.model.entity.AuthorEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
|
||||
import com.adityachandel.booklore.service.BookQueryService;
|
||||
import com.adityachandel.booklore.service.file.FileMoveService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class FileMoveServiceTest {
|
||||
|
||||
@Mock
|
||||
private BookQueryService bookQueryService;
|
||||
|
||||
@InjectMocks
|
||||
private FileMoveService fileMoveService;
|
||||
|
||||
@Test
|
||||
void testGeneratePathWithAllFieldsPresent() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("Dune")
|
||||
.authors(new HashSet<>(List.of(new AuthorEntity(1L, "Frank Herbert", new ArrayList<>())))
|
||||
)
|
||||
.publishedDate(LocalDate.of(1965, 8, 1))
|
||||
.seriesName("Dune Saga")
|
||||
.seriesNumber(1.0F)
|
||||
.language("English")
|
||||
.publisher("Chilton Books")
|
||||
.isbn13("9780441172719")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("Dune.epub")
|
||||
.build();
|
||||
|
||||
String pattern = "/{series}/{seriesIndex} - {title} by {authors} ({year})";
|
||||
String result = fileMoveService.generatePathFromPattern(book, pattern);
|
||||
|
||||
assertThat(result).isEqualTo("/Dune Saga/1 - Dune by Frank Herbert (1965).epub");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGeneratePathWithMissingMetadata() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder().build();
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("unknown.pdf")
|
||||
.build();
|
||||
|
||||
String result = fileMoveService.generatePathFromPattern(
|
||||
book,
|
||||
"/{title}_{authors}_{year}_{series}_{seriesIndex}_{language}_{publisher}_{isbn}"
|
||||
);
|
||||
assertThat(result).isEqualTo("/Untitled_______.pdf");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGeneratePathWithMultipleAuthors() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("Good Omens")
|
||||
.authors(new HashSet<>(List.of(
|
||||
new AuthorEntity(1L, "Neil Gaiman", new ArrayList<>()),
|
||||
new AuthorEntity(2L, "Terry Pratchett", new ArrayList<>())
|
||||
)))
|
||||
.publishedDate(LocalDate.of(1990, 5, 1))
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("goodomens.mobi")
|
||||
.build();
|
||||
|
||||
String pattern = "/{title} - {authors} ({year})";
|
||||
String result = fileMoveService.generatePathFromPattern(book, pattern);
|
||||
|
||||
assertThat(result)
|
||||
.isIn(
|
||||
"/Good Omens - Neil Gaiman, Terry Pratchett (1990).mobi",
|
||||
"/Good Omens - Terry Pratchett, Neil Gaiman (1990).mobi"
|
||||
); // Set order is non-deterministic
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGeneratePathWithNoExtension() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("The Nameless Book")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("namelessbook")
|
||||
.build();
|
||||
|
||||
String result = fileMoveService.generatePathFromPattern(book, "/{title}");
|
||||
assertThat(result).isEqualTo("/The Nameless Book");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGeneratePathWithIsbn10Fallback() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("Test Book")
|
||||
.isbn10("1234567890")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("testbook.pdf")
|
||||
.build();
|
||||
|
||||
String result = fileMoveService.generatePathFromPattern(book, "/{isbn}");
|
||||
assertThat(result).isEqualTo("/1234567890.pdf");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGeneratePathWithEmptyFileName() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("No File")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName(null)
|
||||
.build();
|
||||
|
||||
String result = fileMoveService.generatePathFromPattern(book, "/{title}");
|
||||
assertThat(result).isEqualTo("/No File");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGeneratePathWithEmptyPattern() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("Empty Pattern Book")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("empty.txt")
|
||||
.build();
|
||||
|
||||
String result = fileMoveService.generatePathFromPattern(book, "");
|
||||
assertThat(result).isEqualTo("empty.txt");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGeneratePathWithEmptyFileNameString() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("Ghost File")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("") // explicitly empty
|
||||
.build();
|
||||
|
||||
String result = fileMoveService.generatePathFromPattern(book, "/{title}");
|
||||
assertThat(result).isEqualTo("/Ghost File");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGeneratePathWithUnknownPlaceholder() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("Weird Format")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("weird.pdf")
|
||||
.build();
|
||||
|
||||
String pattern = "/{title}/{unknown}";
|
||||
String result = fileMoveService.generatePathFromPattern(book, pattern);
|
||||
|
||||
assertThat(result).isEqualTo("/Weird Format/{unknown}.pdf");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGeneratePathWithDotOnlyFilename() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("Dotfile")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName(".gitignore")
|
||||
.build();
|
||||
|
||||
String result = fileMoveService.generatePathFromPattern(book, "/{title}");
|
||||
assertThat(result).isEqualTo("/Dotfile.gitignore");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGeneratePathWithOptionalBlockIncluded() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("Book Title")
|
||||
.seriesName("Series")
|
||||
.seriesNumber(3.5F)
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("book.pdf")
|
||||
.build();
|
||||
|
||||
String pattern = "<{series}/><{seriesIndex}. >{title}";
|
||||
String result = fileMoveService.generatePathFromPattern(book, pattern);
|
||||
|
||||
assertThat(result).isEqualTo("Series/3.5. Book Title.pdf");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGeneratePathWithOptionalBlockExcluded() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("Book Title")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("book.pdf")
|
||||
.build();
|
||||
|
||||
String pattern = "<{series}/><{seriesIndex}. >{title}";
|
||||
String result = fileMoveService.generatePathFromPattern(book, pattern);
|
||||
|
||||
assertThat(result).isEqualTo("Book Title.pdf");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOptionalBlockWithPartialMissingData() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("Solo")
|
||||
.seriesName("Lone")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("solo.pdf")
|
||||
.build();
|
||||
|
||||
String pattern = "<{series}/><{seriesIndex}. >{title}";
|
||||
String result = fileMoveService.generatePathFromPattern(book, pattern);
|
||||
|
||||
assertThat(result).isEqualTo("Lone/Solo.pdf");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPatternWithMultipleOptionalBlocks() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("Layered")
|
||||
.seriesName("Stack")
|
||||
.seriesNumber(3F)
|
||||
.language("English")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("layered.epub")
|
||||
.build();
|
||||
|
||||
String pattern = "<{series}/><{seriesIndex}. ><{language}/>{title}";
|
||||
String result = fileMoveService.generatePathFromPattern(book, pattern);
|
||||
|
||||
assertThat(result).isEqualTo("Stack/3. English/Layered.epub");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIllegalCharactersAreRemoved() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("Inva|id:Name*?")
|
||||
.authors(new HashSet<>(List.of(new AuthorEntity(1L, "Au<thor>", new ArrayList<>()))))
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("bad?.pdf")
|
||||
.build();
|
||||
|
||||
String pattern = "/{title}_{authors}";
|
||||
String result = fileMoveService.generatePathFromPattern(book, pattern);
|
||||
|
||||
assertThat(result).isEqualTo("/InvaidName_Author.pdf");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testControlCharactersAreStripped() {
|
||||
String titleWithControlChars = "Bad\u0000Name\u0007Here";
|
||||
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title(titleWithControlChars)
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("control.epub")
|
||||
.build();
|
||||
|
||||
String result = fileMoveService.generatePathFromPattern(book, "/{title}");
|
||||
assertThat(result).isEqualTo("/BadNameHere.epub");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWhitespaceIsCollapsed() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title(" Too Many Spaces ")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("space.mobi")
|
||||
.build();
|
||||
|
||||
String result = fileMoveService.generatePathFromPattern(book, "/{title}");
|
||||
assertThat(result).isEqualTo("/Too Many Spaces.mobi");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUnknownPlaceholdersAreUnchanged() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("Valid Book")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("valid.pdf")
|
||||
.build();
|
||||
|
||||
String pattern = "/{title}/{foo}/{bar}";
|
||||
String result = fileMoveService.generatePathFromPattern(book, pattern);
|
||||
|
||||
assertThat(result).isEqualTo("/Valid Book/{foo}/{bar}.pdf");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEmptyOptionalBlockIsIgnored() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("Optional")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("opt.epub")
|
||||
.build();
|
||||
|
||||
String result = fileMoveService.generatePathFromPattern(book, "<>");
|
||||
assertThat(result).isEqualTo("opt.epub");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUnicodeCharactersArePreservedAndSanitized() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("𝔘𝔫𝔦𝔠𝔬𝔡𝔢 – 漢字 – हिंदी")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("unicode.azw3")
|
||||
.build();
|
||||
|
||||
String result = fileMoveService.generatePathFromPattern(book, "/{title}");
|
||||
assertThat(result).isEqualTo("/𝔘𝔫𝔦𝔠𝔬𝔡𝔢 – 漢字 – हिंदी.azw3");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOptionalBlockDoesNotLeakSurroundingChars() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("LeakTest")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName("leaky.fb2")
|
||||
.build();
|
||||
|
||||
String pattern = "<({series}) >{title}";
|
||||
String result = fileMoveService.generatePathFromPattern(book, pattern);
|
||||
|
||||
assertThat(result).isEqualTo("LeakTest.fb2");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExtensionWithDotOnly() {
|
||||
BookMetadataEntity metadata = BookMetadataEntity.builder()
|
||||
.title("DotDot")
|
||||
.build();
|
||||
|
||||
BookEntity book = BookEntity.builder()
|
||||
.metadata(metadata)
|
||||
.fileName(".")
|
||||
.build();
|
||||
|
||||
String result = fileMoveService.generatePathFromPattern(book, "/{title}");
|
||||
assertThat(result).isEqualTo("/DotDot");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
package com.adityachandel.booklore;
|
||||
|
||||
import com.adityachandel.booklore.model.entity.AuthorEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
|
||||
import com.adityachandel.booklore.util.PathPatternResolver;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class PathPatternResolverTest {
|
||||
|
||||
private BookEntity createBook(String title, List<String> authors, LocalDate date,
|
||||
String series, Float seriesNum, String lang,
|
||||
String publisher, String isbn13, String isbn10,
|
||||
String fileName) {
|
||||
BookEntity book = mock(BookEntity.class);
|
||||
BookMetadataEntity metadata = mock(BookMetadataEntity.class);
|
||||
|
||||
when(book.getMetadata()).thenReturn(metadata);
|
||||
when(book.getFileName()).thenReturn(fileName);
|
||||
|
||||
when(metadata.getTitle()).thenReturn(title);
|
||||
|
||||
if (authors == null) {
|
||||
when(metadata.getAuthors()).thenReturn(null);
|
||||
} else {
|
||||
LinkedHashSet<AuthorEntity> authorEntities = authors.stream().map(name -> {
|
||||
AuthorEntity a = new AuthorEntity();
|
||||
a.setName(name);
|
||||
return a;
|
||||
}).collect(Collectors.toCollection(LinkedHashSet::new));
|
||||
when(metadata.getAuthors()).thenReturn(authorEntities);
|
||||
}
|
||||
|
||||
when(metadata.getPublishedDate()).thenReturn(date);
|
||||
when(metadata.getSeriesName()).thenReturn(series);
|
||||
when(metadata.getSeriesNumber()).thenReturn(seriesNum);
|
||||
when(metadata.getLanguage()).thenReturn(lang);
|
||||
when(metadata.getPublisher()).thenReturn(publisher);
|
||||
when(metadata.getIsbn13()).thenReturn(isbn13);
|
||||
when(metadata.getIsbn10()).thenReturn(isbn10);
|
||||
|
||||
return book;
|
||||
}
|
||||
|
||||
@Test void emptyPattern_returnsOnlyExtension() {
|
||||
var book = createBook("Title", List.of("Author"), LocalDate.now(), null, null, null, null, null, null, "file.pdf");
|
||||
assertThat(PathPatternResolver.resolvePattern(book, "")).isEqualTo("file.pdf");
|
||||
}
|
||||
|
||||
@Test void optionalBlockMissingPlaceholder_removed() {
|
||||
var book = createBook(null, null, null, null, null, null, null, null, null, "f.epub");
|
||||
assertThat(PathPatternResolver.resolvePattern(book, "<{series}/>")).isEqualTo("f.epub");
|
||||
}
|
||||
|
||||
@Test void multipleOptionalBlocks_partialValues() {
|
||||
var book = createBook("Title", List.of("Author"), LocalDate.of(2020, 1, 1),
|
||||
"Series", 1f, null, null, null, null, "f.epub");
|
||||
String p = "<{series}/><{seriesIndex}. ><{language}/>{title}";
|
||||
assertThat(PathPatternResolver.resolvePattern(book, p)).isEqualTo("Series/1. Title.epub");
|
||||
}
|
||||
|
||||
@Test void placeholdersOutsideOptional_replacedWithEmpty() {
|
||||
var book = createBook("Title", null, LocalDate.of(2020, 1, 1), null, null, null, null, null, null, "file.cbz");
|
||||
String p = "{title} - {authors} - {publisher}";
|
||||
assertThat(PathPatternResolver.resolvePattern(book, p)).isEqualTo("Title - - .cbz");
|
||||
}
|
||||
|
||||
@Test void seriesNumber_decimalAndInteger() {
|
||||
var book1 = createBook("Title", List.of("Author"), LocalDate.now(), "Series", 3.5f, null, null, null, null, "f.epub");
|
||||
var book2 = createBook("Title", List.of("Author"), LocalDate.now(), "Series", 3f, null, null, null, null, "f.epub");
|
||||
String p = "<{series}/><{seriesIndex}. >{title}";
|
||||
assertThat(PathPatternResolver.resolvePattern(book1, p)).isEqualTo("Series/3.5. Title.epub");
|
||||
assertThat(PathPatternResolver.resolvePattern(book2, p)).isEqualTo("Series/3. Title.epub");
|
||||
}
|
||||
|
||||
@Test void sanitizes_illegalCharsAndWhitespace() {
|
||||
var book = createBook(" Ti:tle/<>|*? ", List.of("Au:thor|?*"), LocalDate.of(2000, 1, 1),
|
||||
"Se:ries", 1f, "Lang<>", "Pub|?", "123:456", "654|321", "file?.pdf");
|
||||
String p = "{title} - {authors} - {series} - {language} - {publisher} - {isbn}";
|
||||
String result = PathPatternResolver.resolvePattern(book, p);
|
||||
assertThat(result).doesNotContain(":", "/", "*", "?", "<", ">", "|")
|
||||
.contains("Title").contains("Author").contains("Series");
|
||||
}
|
||||
|
||||
@Test void noExtension_returnsNoDot() {
|
||||
var book = createBook("Title", List.of("Author"), LocalDate.now(), null, null, null, null, null, null, "filenameWithoutExt");
|
||||
assertThat(PathPatternResolver.resolvePattern(book, "{title}")).isEqualTo("Title");
|
||||
}
|
||||
|
||||
@Test void complexPattern_allPlaceholdersPresent() {
|
||||
var book = createBook("Complex Title", List.of("Author One"), LocalDate.of(2010, 5, 5),
|
||||
"Complex Series", 12.5f, "English", "Publisher", "ISBN13", "ISBN10", "complex.cbz");
|
||||
String p = "<{series}/><{seriesIndex}. ><{language}/><{publisher}/>{title} - {authors} - {year} - {isbn}";
|
||||
assertThat(PathPatternResolver.resolvePattern(book, p))
|
||||
.isEqualTo("Complex Series/12.5. English/Publisher/Complex Title - Author One - 2010 - ISBN13.cbz");
|
||||
}
|
||||
|
||||
@Test void missingAllMetadata_returnsExtensionOnly() {
|
||||
var b = createBook(null, null, null, null, null, null, null, null, null, "f.pdf");
|
||||
assertThat(PathPatternResolver.resolvePattern(b, "")).isEqualTo("f.pdf");
|
||||
}
|
||||
|
||||
@Test void optionalBlockWithEmptyValue_removesBlock() {
|
||||
var b = createBook(null, null, null, null, null, null, null, null, null, "f.epub");
|
||||
assertThat(PathPatternResolver.resolvePattern(b, "<{series}/><{language}/>")).isEqualTo("f.epub");
|
||||
}
|
||||
|
||||
@Test void placeholderWithEmptyValue_outsideOptional_replacedByEmpty() {
|
||||
var b = createBook("T", null, null, null, null, null, null, null, null, "f.epub");
|
||||
String p = "{title} - {authors} - {language}";
|
||||
assertThat(PathPatternResolver.resolvePattern(b, p)).isEqualTo("T - - .epub");
|
||||
}
|
||||
|
||||
@Test void complexPattern_someMissingOptionalBlocksSkipped() {
|
||||
var b = createBook("T", List.of("A"), null, null, null, "en", null, null, null, "f.epub");
|
||||
String p = "<{series}/><{seriesIndex}. ><{language}/>{title} - {authors}";
|
||||
assertThat(PathPatternResolver.resolvePattern(b, p)).isEqualTo("en/T - A.epub");
|
||||
}
|
||||
|
||||
@Test void seriesNumberDecimals_andIntegersHandledProperly() {
|
||||
var b1 = createBook("T", List.of("A"), null, "S", 1f, null, null, null, null, "f.epub");
|
||||
var b2 = createBook("T", List.of("A"), null, "S", 1.5f, null, null, null, null, "f.epub");
|
||||
String p = "<{series}/><{seriesIndex}. >{title}";
|
||||
assertThat(PathPatternResolver.resolvePattern(b1, p)).isEqualTo("S/1. T.epub");
|
||||
assertThat(PathPatternResolver.resolvePattern(b2, p)).isEqualTo("S/1.5. T.epub");
|
||||
}
|
||||
|
||||
@Test void sanitize_removesIllegalCharacters() {
|
||||
var b = createBook("Ti:tle<>", List.of("Au:thor|?*"), null, "Se:ries", 1f,
|
||||
"La<>ng", "Pub|?", "123:456", "654|321", "f?.pdf");
|
||||
String p = "{title}-{authors}-{series}-{language}-{publisher}-{isbn}";
|
||||
String result = PathPatternResolver.resolvePattern(b, p);
|
||||
assertThat(result).doesNotContain(":", "/", "*", "?", "<", ">", "|")
|
||||
.contains("Title").contains("Author").contains("Series");
|
||||
}
|
||||
|
||||
@Test void noFileExtension_returnsNoDot() {
|
||||
var b = createBook("Title", List.of("Author"), null, null, null, null, null, null, null, "fileNoExt");
|
||||
assertThat(PathPatternResolver.resolvePattern(b, "{title}")).isEqualTo("Title");
|
||||
}
|
||||
|
||||
@Test void patternWithOnlyExtension_returnsExtensionOnly() {
|
||||
var book = createBook(null, null, null, null, null, null, null, null, null, "sample.pdf");
|
||||
assertThat(PathPatternResolver.resolvePattern(book, ".{extension}")).isEqualTo(".pdf");
|
||||
}
|
||||
|
||||
@Test void patternWithExtensionAndFilenamePlaceholder_works() {
|
||||
var book = createBook("Sample", null, null, null, null, null, null, null, null, "original.epub");
|
||||
assertThat(PathPatternResolver.resolvePattern(book, "{currentFilename}")).isEqualTo("original.epub");
|
||||
}
|
||||
|
||||
@Test void allPlaceholdersMissing_yieldsJustExtension() {
|
||||
var book = createBook(null, null, null, null, null, null, null, null, null, "file.cbz");
|
||||
String pattern = "{title}-{authors}-{series}-{year}-{language}-{publisher}-{isbn}";
|
||||
assertThat(PathPatternResolver.resolvePattern(book, pattern)).isEqualTo("Untitled------.cbz");
|
||||
}
|
||||
|
||||
@Test void patternWithBackslashes_isSanitized() {
|
||||
var book = createBook("Ti\\tle", List.of("Au\\thor"), null, null, null, null, null, null, null, "f.pdf");
|
||||
assertThat(PathPatternResolver.resolvePattern(book, "{title}/{authors}")).isEqualTo("Title/Author.pdf");
|
||||
}
|
||||
|
||||
@Test void optionalBlockWithMixedResolvedAndEmptyValues_skippedEntirely() {
|
||||
var book = createBook("Title", null, null, null, null, null, null, null, null, "file.epub");
|
||||
String pattern = "<{authors} - {series}/>{title}";
|
||||
assertThat(PathPatternResolver.resolvePattern(book, pattern)).isEqualTo("Title.epub");
|
||||
}
|
||||
|
||||
@Test void placeholderWithWhitespace_trimmedAndSanitized() {
|
||||
var book = createBook(" My Book ", List.of(" John Doe "), null, null, null, null, null, null, null, "book.pdf");
|
||||
String pattern = "{title} - {authors}";
|
||||
assertThat(PathPatternResolver.resolvePattern(book, pattern)).isEqualTo("My Book - John Doe.pdf");
|
||||
}
|
||||
|
||||
@Test void multipleAuthors_concatenatedProperly() {
|
||||
var book = createBook("Book", List.of("Alice", "Bob", "Carol"), null, null, null, null, null, null, null, "book.pdf");
|
||||
String pattern = "{title} - {authors}";
|
||||
assertThat(PathPatternResolver.resolvePattern(book, pattern)).isEqualTo("Book - Alice, Bob, Carol.pdf");
|
||||
}
|
||||
|
||||
@Test void patternWithOnlyOptional_allEmpty_resolvesToFilename() {
|
||||
var book = createBook(null, null, null, null, null, null, null, null, null, "fallback.epub");
|
||||
String pattern = "<{series}/><{language}/>";
|
||||
assertThat(PathPatternResolver.resolvePattern(book, pattern)).isEqualTo("fallback.epub");
|
||||
}
|
||||
|
||||
@Test void invalidFilename_noExtension_stillResolves() {
|
||||
var book = createBook("A", List.of("B"), null, null, null, null, null, null, null, "noExt");
|
||||
String pattern = "{title}-{authors}";
|
||||
assertThat(PathPatternResolver.resolvePattern(book, pattern)).isEqualTo("A-B");
|
||||
}
|
||||
|
||||
@Test void patternEndsWithSlash_slashIsPreserved() {
|
||||
var book = createBook("T", List.of("A"), null, "S", 1f, null, null, null, null, "b.pdf");
|
||||
String pattern = "<{series}/><{seriesIndex}/>{title}/";
|
||||
assertThat(PathPatternResolver.resolvePattern(book, pattern)).isEqualTo("S/1/T/.pdf");
|
||||
}
|
||||
|
||||
@Test void extensionDotEscaped_doubleDotsNotAdded() {
|
||||
var book = createBook("X", List.of("Y"), null, null, null, null, null, null, null, "book.mobi");
|
||||
String pattern = "{title}.{extension}";
|
||||
assertThat(PathPatternResolver.resolvePattern(book, pattern)).isEqualTo("X.mobi");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user