refactor(api): use Path for FileUtils.getBookFullPath (#209)
* refactor(api): use `Path` for FileUtils.getBookFullPath * test: update tests for new behavior
This commit is contained in:
@@ -59,7 +59,7 @@ public class BookDownloadService {
|
||||
if (primaryFile == null) {
|
||||
throw ApiError.FAILED_TO_DOWNLOAD_FILE.createException(bookId);
|
||||
}
|
||||
Path file = Paths.get(FileUtils.getBookFullPath(bookEntity)).toAbsolutePath().normalize();
|
||||
Path file = FileUtils.getBookFullPath(bookEntity).toAbsolutePath().normalize();
|
||||
|
||||
if (!Files.exists(file)) {
|
||||
throw ApiError.FAILED_TO_DOWNLOAD_FILE.createException(bookId);
|
||||
@@ -258,7 +258,7 @@ public class BookDownloadService {
|
||||
int compressionPercentage = koboSettings.getConversionImageCompressionPercentage();
|
||||
Path tempDir = null;
|
||||
try {
|
||||
File inputFile = new File(FileUtils.getBookFullPath(bookEntity));
|
||||
File inputFile = FileUtils.getBookFullPath(bookEntity).toFile();
|
||||
File fileToSend = inputFile;
|
||||
|
||||
if (convertCbxToEpub || convertEpubToKepub) {
|
||||
|
||||
@@ -338,7 +338,7 @@ public class BookService {
|
||||
.orElseThrow(() -> ApiError.FILE_NOT_FOUND.createException("No file of type " + bookType + " found for book"));
|
||||
filePath = bookFile.getFullFilePath().toString();
|
||||
} else {
|
||||
filePath = FileUtils.getBookFullPath(bookEntity);
|
||||
filePath = FileUtils.getBookFullPath(bookEntity).toString();
|
||||
}
|
||||
File file = new File(filePath);
|
||||
if (!file.exists()) {
|
||||
@@ -362,7 +362,7 @@ public class BookService {
|
||||
.orElseThrow(() -> ApiError.FILE_NOT_FOUND.createException("No file of type " + bookType + " found for book"));
|
||||
filePath = bookFile.getFullFilePath().toString();
|
||||
} else {
|
||||
filePath = FileUtils.getBookFullPath(bookEntity);
|
||||
filePath = FileUtils.getBookFullPath(bookEntity).toString();
|
||||
}
|
||||
|
||||
Path path = Paths.get(filePath);
|
||||
|
||||
@@ -96,7 +96,7 @@ public class SendEmailV2Service {
|
||||
helper.setTo(recipientEmail);
|
||||
helper.setSubject("Your Book from Booklore: " + book.getMetadata().getTitle());
|
||||
helper.setText(generateEmailBody(book.getMetadata().getTitle()));
|
||||
File bookFile = new File(FileUtils.getBookFullPath(book, bookFileEntity));
|
||||
File bookFile = FileUtils.getBookFullPath(book, bookFileEntity).toFile();
|
||||
helper.addAttachment(bookFile.getName(), bookFile);
|
||||
dynamicMailSender.send(message);
|
||||
log.info("Book sent successfully to {}", recipientEmail);
|
||||
|
||||
+1
-1
@@ -160,7 +160,7 @@ public class AudiobookProcessor extends AbstractFileProcessor implements BookFil
|
||||
.map(java.nio.file.Path::toFile)
|
||||
.orElse(null);
|
||||
} else {
|
||||
return new File(FileUtils.getBookFullPath(bookEntity));
|
||||
return FileUtils.getBookFullPath(bookEntity).toFile();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ public class Azw3Processor extends AbstractFileProcessor implements BookFileProc
|
||||
@Override
|
||||
public boolean generateCover(BookEntity bookEntity, BookFileEntity bookFile) {
|
||||
try {
|
||||
File azw3File = new File(FileUtils.getBookFullPath(bookEntity, bookFile));
|
||||
File azw3File = FileUtils.getBookFullPath(bookEntity, bookFile).toFile();
|
||||
byte[] coverData = azw3MetadataExtractor.extractCover(azw3File);
|
||||
|
||||
if (coverData == null || coverData.length == 0) {
|
||||
|
||||
@@ -33,6 +33,7 @@ import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.io.InputStream;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -65,7 +66,7 @@ public class CbxProcessor extends AbstractFileProcessor implements BookFileProce
|
||||
@Override
|
||||
public BookEntity processNewFile(LibraryFile libraryFile) {
|
||||
BookEntity bookEntity = bookCreatorService.createShellBook(libraryFile, BookFileType.CBX);
|
||||
bookEntity.getPrimaryBookFile().setArchiveType(ArchiveUtils.detectArchiveType(new File(FileUtils.getBookFullPath(bookEntity))));
|
||||
bookEntity.getPrimaryBookFile().setArchiveType(ArchiveUtils.detectArchiveType(FileUtils.getBookFullPath(bookEntity).toFile()));
|
||||
boolean coverGenerated = generateCover(bookEntity);
|
||||
if (!coverGenerated) {
|
||||
var folder = getBookFolderForCoverFallback(libraryFile);
|
||||
@@ -88,7 +89,7 @@ public class CbxProcessor extends AbstractFileProcessor implements BookFileProce
|
||||
|
||||
@Override
|
||||
public boolean generateCover(BookEntity bookEntity, BookFileEntity bookFile) {
|
||||
File file = new File(FileUtils.getBookFullPath(bookEntity, bookFile));
|
||||
File file = FileUtils.getBookFullPath(bookEntity, bookFile).toFile();
|
||||
try {
|
||||
Optional<BufferedImage> imageOptional = extractImagesFromArchive(file);
|
||||
if (imageOptional.isPresent()) {
|
||||
@@ -251,7 +252,7 @@ public class CbxProcessor extends AbstractFileProcessor implements BookFileProce
|
||||
|
||||
private void extractAndSetMetadata(BookEntity bookEntity) {
|
||||
try {
|
||||
BookMetadata extracted = cbxMetadataExtractor.extractMetadata(new File(FileUtils.getBookFullPath(bookEntity)));
|
||||
BookMetadata extracted = cbxMetadataExtractor.extractMetadata(FileUtils.getBookFullPath(bookEntity).toFile());
|
||||
if (extracted == null) {
|
||||
// Fallback to filename-derived title
|
||||
setMetadata(bookEntity);
|
||||
|
||||
@@ -73,7 +73,7 @@ public class EpubProcessor extends AbstractFileProcessor implements BookFileProc
|
||||
@Override
|
||||
public boolean generateCover(BookEntity bookEntity, BookFileEntity bookFile) {
|
||||
try {
|
||||
File epubFile = new File(FileUtils.getBookFullPath(bookEntity, bookFile));
|
||||
File epubFile = FileUtils.getBookFullPath(bookEntity, bookFile).toFile();
|
||||
byte[] coverData = epubMetadataExtractor.extractCover(epubFile);
|
||||
|
||||
if (coverData == null) {
|
||||
|
||||
@@ -71,7 +71,7 @@ public class Fb2Processor extends AbstractFileProcessor implements BookFileProce
|
||||
@Override
|
||||
public boolean generateCover(BookEntity bookEntity, BookFileEntity bookFile) {
|
||||
try {
|
||||
File fb2File = new File(FileUtils.getBookFullPath(bookEntity, bookFile));
|
||||
File fb2File = FileUtils.getBookFullPath(bookEntity, bookFile).toFile();
|
||||
byte[] coverData = fb2MetadataExtractor.extractCover(fb2File);
|
||||
|
||||
if (coverData == null || coverData.length == 0) {
|
||||
|
||||
@@ -72,7 +72,7 @@ public class MobiProcessor extends AbstractFileProcessor implements BookFileProc
|
||||
@Override
|
||||
public boolean generateCover(BookEntity bookEntity, BookFileEntity bookFile) {
|
||||
try {
|
||||
File mobiFile = new File(FileUtils.getBookFullPath(bookEntity, bookFile));
|
||||
File mobiFile = FileUtils.getBookFullPath(bookEntity, bookFile).toFile();
|
||||
byte[] coverData = mobiMetadataExtractor.extractCover(mobiFile);
|
||||
|
||||
if (coverData == null || coverData.length == 0) {
|
||||
|
||||
@@ -74,7 +74,7 @@ public class PdfProcessor extends AbstractFileProcessor implements BookFileProce
|
||||
|
||||
@Override
|
||||
public boolean generateCover(BookEntity bookEntity, BookFileEntity bookFile) {
|
||||
File pdfFile = new File(FileUtils.getBookFullPath(bookEntity, bookFile));
|
||||
File pdfFile = FileUtils.getBookFullPath(bookEntity, bookFile).toFile();
|
||||
try (RandomAccessReadBufferedFile randomAccessRead = new RandomAccessReadBufferedFile(pdfFile);
|
||||
PDDocument pdf = Loader.loadPDF(randomAccessRead)) {
|
||||
return generateCoverImageAndSave(bookEntity.getId(), pdf);
|
||||
@@ -104,7 +104,7 @@ public class PdfProcessor extends AbstractFileProcessor implements BookFileProce
|
||||
|
||||
private void extractAndSetMetadata(BookEntity bookEntity) {
|
||||
try {
|
||||
BookMetadata extracted = pdfMetadataExtractor.extractMetadata(new File(FileUtils.getBookFullPath(bookEntity)));
|
||||
BookMetadata extracted = pdfMetadataExtractor.extractMetadata(FileUtils.getBookFullPath(bookEntity).toFile());
|
||||
|
||||
if (StringUtils.isNotBlank(extracted.getTitle())) {
|
||||
bookEntity.getMetadata().setTitle(truncate(extracted.getTitle(), 1000));
|
||||
|
||||
@@ -197,7 +197,7 @@ public class BookMetadataService {
|
||||
log.info("Unsupported operation for book ID {} - no file or not CBX type", bookId);
|
||||
return null;
|
||||
}
|
||||
return cbxMetadataExtractor.extractMetadata(new File(FileUtils.getBookFullPath(bookEntity)));
|
||||
return cbxMetadataExtractor.extractMetadata(FileUtils.getBookFullPath(bookEntity).toFile());
|
||||
}
|
||||
|
||||
public BookMetadata getFileMetadata(long bookId) {
|
||||
@@ -207,7 +207,7 @@ public class BookMetadataService {
|
||||
if (primaryFile == null) {
|
||||
throw ApiError.GENERIC_BAD_REQUEST.createException("Book has no file to extract metadata from");
|
||||
}
|
||||
return metadataExtractorFactory.extractMetadata(primaryFile.getBookType(), new File(FileUtils.getBookFullPath(bookEntity)));
|
||||
return metadataExtractorFactory.extractMetadata(primaryFile.getBookType(), FileUtils.getBookFullPath(bookEntity).toFile());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
||||
@@ -144,8 +144,7 @@ public class CbxReaderService {
|
||||
.orElseThrow(() -> ApiError.FILE_NOT_FOUND.createException("No file of type " + bookType + " found for book"));
|
||||
return bookFile.getFullFilePath();
|
||||
}
|
||||
String bookFullPath = FileUtils.getBookFullPath(bookEntity);
|
||||
return Path.of(bookFullPath);
|
||||
return FileUtils.getBookFullPath(bookEntity);
|
||||
}
|
||||
|
||||
private void validatePageRequest(Long bookId, int page, List<String> imageEntries) throws FileNotFoundException {
|
||||
|
||||
@@ -217,8 +217,7 @@ public class EpubReaderService {
|
||||
.orElseThrow(() -> ApiError.FILE_NOT_FOUND.createException("No file of type " + bookType + " found for book"));
|
||||
return bookFile.getFullFilePath();
|
||||
}
|
||||
String bookFullPath = FileUtils.getBookFullPath(bookEntity);
|
||||
return Path.of(bookFullPath);
|
||||
return FileUtils.getBookFullPath(bookEntity);
|
||||
}
|
||||
|
||||
private CachedEpubMetadata getCachedMetadata(Path epubPath) throws IOException {
|
||||
|
||||
@@ -113,8 +113,7 @@ public class PdfReaderService {
|
||||
.orElseThrow(() -> ApiError.FILE_NOT_FOUND.createException("No file of type " + bookType + " found for book"));
|
||||
return bookFile.getFullFilePath();
|
||||
}
|
||||
String bookFullPath = FileUtils.getBookFullPath(bookEntity);
|
||||
return Path.of(bookFullPath);
|
||||
return FileUtils.getBookFullPath(bookEntity);
|
||||
}
|
||||
|
||||
private void validatePageRequest(Long bookId, int page, int pageCount) throws FileNotFoundException {
|
||||
|
||||
@@ -24,27 +24,18 @@ public class FileUtils {
|
||||
|
||||
private final String FILE_NOT_FOUND_MESSAGE = "File does not exist: ";
|
||||
|
||||
public String getBookFullPath(BookEntity bookEntity) {
|
||||
public Path getBookFullPath(BookEntity bookEntity) {
|
||||
BookFileEntity bookFile = bookEntity.getPrimaryBookFile();
|
||||
if (bookFile == null || bookEntity.getLibraryPath() == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Path.of(bookEntity.getLibraryPath().getPath(), bookFile.getFileSubPath(), bookFile.getFileName())
|
||||
.normalize()
|
||||
.toString()
|
||||
.replace("\\", "/");
|
||||
return getBookFullPath(bookEntity, bookFile);
|
||||
}
|
||||
|
||||
public String getBookFullPath(BookEntity bookEntity, BookFileEntity bookFile) {
|
||||
public Path getBookFullPath(BookEntity bookEntity, BookFileEntity bookFile) {
|
||||
if (bookFile == null || bookEntity.getLibraryPath() == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Path.of(bookEntity.getLibraryPath().getPath(), bookFile.getFileSubPath(), bookFile.getFileName())
|
||||
.normalize()
|
||||
.toString()
|
||||
.replace("\\", "/");
|
||||
.normalize();
|
||||
}
|
||||
|
||||
public String getBookFullPath(Book book) {
|
||||
@@ -60,7 +51,7 @@ public class FileUtils {
|
||||
}
|
||||
|
||||
public Long getFileSizeInKb(BookEntity bookEntity) {
|
||||
Path filePath = Path.of(getBookFullPath(bookEntity));
|
||||
Path filePath = getBookFullPath(bookEntity);
|
||||
return getFileSizeInKb(filePath);
|
||||
}
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ class Rar5FallbackIntegrationTest {
|
||||
|
||||
try (var fileUtilsStatic = org.mockito.Mockito.mockStatic(org.booklore.util.FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> org.booklore.util.FileUtils.getBookFullPath(book))
|
||||
.thenReturn(cbrCopy.toString());
|
||||
.thenReturn(cbrCopy);
|
||||
|
||||
CbxReaderService readerService = new CbxReaderService(mockRepo);
|
||||
List<Integer> pages = readerService.getAvailablePages(99L);
|
||||
@@ -96,7 +96,7 @@ class Rar5FallbackIntegrationTest {
|
||||
|
||||
try (var fileUtilsStatic = org.mockito.Mockito.mockStatic(org.booklore.util.FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> org.booklore.util.FileUtils.getBookFullPath(book))
|
||||
.thenReturn(cbrCopy.toString());
|
||||
.thenReturn(cbrCopy);
|
||||
|
||||
CbxReaderService readerService = new CbxReaderService(mockRepo);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
|
||||
@@ -81,6 +81,9 @@ class BookServiceTest {
|
||||
|
||||
private BookLoreUser testUser;
|
||||
|
||||
private Path epubPath;
|
||||
private Path pdfPath;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
BookLoreUser.UserPermissions perms = new BookLoreUser.UserPermissions();
|
||||
@@ -90,6 +93,9 @@ class BookServiceTest {
|
||||
.permissions(perms)
|
||||
.assignedLibraries(List.of())
|
||||
.isDefaultPassword(false).build();
|
||||
|
||||
epubPath = Path.of("/tmp/library/book.epub");
|
||||
pdfPath = Path.of("/tmp/library/book.pdf");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -125,7 +131,7 @@ class BookServiceTest {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(testUser);
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsMock = mockStatic(FileUtils.class)) {
|
||||
fileUtilsMock.when(() -> FileUtils.getBookFullPath(entity)).thenReturn("/tmp/library/book.epub");
|
||||
fileUtilsMock.when(() -> FileUtils.getBookFullPath(entity)).thenReturn(epubPath);
|
||||
List<Book> result = bookService.getBooksByIds(Set.of(2L), false);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
@@ -153,7 +159,7 @@ class BookServiceTest {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(testUser);
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsMock = mockStatic(FileUtils.class)) {
|
||||
fileUtilsMock.when(() -> FileUtils.getBookFullPath(entity)).thenReturn("/tmp/library/book.pdf");
|
||||
fileUtilsMock.when(() -> FileUtils.getBookFullPath(entity)).thenReturn(pdfPath);
|
||||
Book result = bookService.getBook(3L, true);
|
||||
assertEquals(3L, result.getId());
|
||||
verify(bookRepository).findByIdWithBookFiles(3L);
|
||||
@@ -329,7 +335,7 @@ class BookServiceTest {
|
||||
Path path = Paths.get("/tmp/bookcontent.txt");
|
||||
Files.write(path, "hello".getBytes());
|
||||
try (MockedStatic<FileUtils> fileUtilsMock = mockStatic(FileUtils.class)) {
|
||||
fileUtilsMock.when(() -> FileUtils.getBookFullPath(entity)).thenReturn(path.toString());
|
||||
fileUtilsMock.when(() -> FileUtils.getBookFullPath(entity)).thenReturn(path);
|
||||
ResponseEntity<Resource> response = bookService.getBookContent(10L);
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertArrayEquals("hello".getBytes(), response.getBody().getInputStream().readAllBytes());
|
||||
@@ -350,7 +356,7 @@ class BookServiceTest {
|
||||
entity.setId(12L);
|
||||
when(bookRepository.findByIdWithBookFiles(12L)).thenReturn(Optional.of(entity));
|
||||
try (MockedStatic<FileUtils> fileUtilsMock = mockStatic(FileUtils.class)) {
|
||||
fileUtilsMock.when(() -> FileUtils.getBookFullPath(entity)).thenReturn("/tmp/nonexistentfile.txt");
|
||||
fileUtilsMock.when(() -> FileUtils.getBookFullPath(entity)).thenReturn(Path.of("/tmp/nonexistentfile.txt"));
|
||||
assertThrows(APIException.class, () -> bookService.getBookContent(12L));
|
||||
}
|
||||
}
|
||||
@@ -476,12 +482,12 @@ class BookServiceTest {
|
||||
// `FileUtils.getBookFullPath` calls `book.getPrimaryBookFile()`.
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsMock = mockStatic(FileUtils.class)) {
|
||||
fileUtilsMock.when(() -> FileUtils.getBookFullPath(entity)).thenReturn("/tmp/library/book.epub");
|
||||
fileUtilsMock.when(() -> FileUtils.getBookFullPath(entity)).thenReturn(epubPath);
|
||||
|
||||
bookService.streamBookContent(15L, null, request, response);
|
||||
|
||||
verify(bookRepository).findByIdWithBookFiles(15L);
|
||||
verify(fileStreamingService).streamWithRangeSupport(eq(Paths.get("/tmp/library/book.epub")), eq("application/epub+zip"), eq(request), eq(response));
|
||||
verify(fileStreamingService).streamWithRangeSupport(eq(epubPath), eq("application/epub+zip"), eq(request), eq(response));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -122,7 +123,7 @@ class SendEmailV2ServiceTest {
|
||||
});
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsMock = mockStatic(FileUtils.class)) {
|
||||
fileUtilsMock.when(() -> FileUtils.getBookFullPath(book)).thenReturn("/library/books/test-book.epub");
|
||||
fileUtilsMock.when(() -> FileUtils.getBookFullPath(book)).thenReturn(Path.of("/library/books/test-book.epub"));
|
||||
|
||||
sendEmailV2Service.emailBookQuick(10L);
|
||||
|
||||
@@ -192,7 +193,7 @@ class SendEmailV2ServiceTest {
|
||||
});
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsMock = mockStatic(FileUtils.class)) {
|
||||
fileUtilsMock.when(() -> FileUtils.getBookFullPath(book)).thenReturn("/library/books/test-book.epub");
|
||||
fileUtilsMock.when(() -> FileUtils.getBookFullPath(book)).thenReturn(Path.of("/library/books/test-book.epub"));
|
||||
|
||||
sendEmailV2Service.emailBook(request);
|
||||
|
||||
@@ -225,7 +226,7 @@ class SendEmailV2ServiceTest {
|
||||
});
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsMock = mockStatic(FileUtils.class)) {
|
||||
fileUtilsMock.when(() -> FileUtils.getBookFullPath(book)).thenReturn("/library/books/test-book.epub");
|
||||
fileUtilsMock.when(() -> FileUtils.getBookFullPath(book)).thenReturn(Path.of("/library/books/test-book.epub"));
|
||||
|
||||
sendEmailV2Service.emailBook(request);
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ class CbxReaderServiceTest {
|
||||
void testGetAvailablePages_CBZ_Success() throws Exception {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath);
|
||||
|
||||
ZipArchiveEntry entry1 = new ZipArchiveEntry("1.jpg");
|
||||
ZipArchiveEntry entry2 = new ZipArchiveEntry("2.png");
|
||||
@@ -94,7 +94,7 @@ class CbxReaderServiceTest {
|
||||
void testGetAvailablePages_CBZ_Fallback_Success() throws Exception {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath);
|
||||
|
||||
// Unicode enabled -> throws Exception
|
||||
ZipFile zipFileFail = mock(ZipFile.class);
|
||||
@@ -141,7 +141,7 @@ class CbxReaderServiceTest {
|
||||
void testStreamPageImage_CBZ_Success() throws Exception {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath);
|
||||
|
||||
ZipArchiveEntry entry1 = new ZipArchiveEntry("1.jpg");
|
||||
Enumeration<ZipArchiveEntry> entries = Collections.enumeration(List.of(entry1));
|
||||
@@ -180,7 +180,7 @@ class CbxReaderServiceTest {
|
||||
void testGetAvailablePages_CB7_Success() throws Exception {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cb7Path.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cb7Path);
|
||||
|
||||
SevenZArchiveEntry entry1 = mock(SevenZArchiveEntry.class);
|
||||
when(entry1.getName()).thenReturn("1.jpg");
|
||||
@@ -209,7 +209,7 @@ class CbxReaderServiceTest {
|
||||
void testGetAvailablePages_CBR_Success() throws Exception {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbrPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbrPath);
|
||||
|
||||
FileHeader header = mock(FileHeader.class);
|
||||
when(header.isDirectory()).thenReturn(false);
|
||||
@@ -232,7 +232,7 @@ class CbxReaderServiceTest {
|
||||
void testStreamPageImage_CBR_Success() throws Exception {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbrPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbrPath);
|
||||
|
||||
FileHeader header = mock(FileHeader.class);
|
||||
when(header.isDirectory()).thenReturn(false);
|
||||
@@ -261,7 +261,7 @@ class CbxReaderServiceTest {
|
||||
void testStreamPageImage_PageOutOfRange_Throws() throws Exception {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath);
|
||||
|
||||
ZipArchiveEntry entry1 = new ZipArchiveEntry("1.jpg");
|
||||
Enumeration<ZipArchiveEntry> entries = Collections.enumeration(List.of(entry1));
|
||||
@@ -292,7 +292,7 @@ class CbxReaderServiceTest {
|
||||
Files.deleteIfExists(unknownPath);
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(unknownPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(unknownPath);
|
||||
|
||||
Files.createFile(unknownPath);
|
||||
Files.setLastModifiedTime(unknownPath, FileTime.fromMillis(System.currentTimeMillis()));
|
||||
@@ -305,7 +305,7 @@ class CbxReaderServiceTest {
|
||||
void testStreamPageImage_EntryNotFound_Throws() throws Exception {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(cbzPath);
|
||||
|
||||
ZipArchiveEntry entry1 = new ZipArchiveEntry("1.jpg");
|
||||
Enumeration<ZipArchiveEntry> entries = Collections.enumeration(List.of(entry1));
|
||||
|
||||
@@ -110,7 +110,7 @@ class EpubReaderServiceTest {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath);
|
||||
|
||||
ZipFile zipFile = createMockZipFile();
|
||||
ZipFile.Builder builder = createMockZipFileBuilder(zipFile);
|
||||
@@ -149,7 +149,7 @@ class EpubReaderServiceTest {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath);
|
||||
|
||||
ZipFile zipFile = createMockZipFile();
|
||||
ZipFile.Builder builder = createMockZipFileBuilder(zipFile);
|
||||
@@ -177,7 +177,7 @@ class EpubReaderServiceTest {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath);
|
||||
|
||||
byte[] chapterContent = "<html><body>Chapter 1 content</body></html>".getBytes(StandardCharsets.UTF_8);
|
||||
ZipFile zipFile = createMockZipFileWithStreamableEntry("OEBPS/chapter1.xhtml", chapterContent);
|
||||
@@ -212,7 +212,7 @@ class EpubReaderServiceTest {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath);
|
||||
|
||||
ZipFile zipFile = createMockZipFile();
|
||||
ZipFile.Builder builder = createMockZipFileBuilder(zipFile);
|
||||
@@ -238,7 +238,7 @@ class EpubReaderServiceTest {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath);
|
||||
|
||||
ZipFile zipFile = createMockZipFile();
|
||||
ZipFile.Builder builder = createMockZipFileBuilder(zipFile);
|
||||
@@ -264,7 +264,7 @@ class EpubReaderServiceTest {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath);
|
||||
|
||||
byte[] containerContent = CONTAINER_XML.getBytes(StandardCharsets.UTF_8);
|
||||
ZipFile zipFile = createMockZipFileWithStreamableEntry("META-INF/container.xml", containerContent);
|
||||
@@ -299,7 +299,7 @@ class EpubReaderServiceTest {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath);
|
||||
|
||||
ZipFile zipFile = createMockZipFile();
|
||||
ZipFile.Builder builder = createMockZipFileBuilder(zipFile);
|
||||
@@ -325,7 +325,7 @@ class EpubReaderServiceTest {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath);
|
||||
|
||||
ZipFile zipFile = createMockZipFile();
|
||||
ZipFile.Builder builder = createMockZipFileBuilder(zipFile);
|
||||
@@ -353,7 +353,7 @@ class EpubReaderServiceTest {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath);
|
||||
|
||||
ZipFile zipFile = createMockZipFileWithSizes();
|
||||
ZipFile.Builder builder = createMockZipFileBuilder(zipFile);
|
||||
@@ -375,7 +375,7 @@ class EpubReaderServiceTest {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath);
|
||||
|
||||
ZipFile zipFile = createMockZipFile();
|
||||
ZipFile.Builder builder = createMockZipFileBuilder(zipFile);
|
||||
@@ -396,7 +396,7 @@ class EpubReaderServiceTest {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath);
|
||||
|
||||
ZipFile zipFile = createMockZipFile();
|
||||
ZipFile.Builder builder = createMockZipFileBuilder(zipFile);
|
||||
@@ -424,7 +424,7 @@ class EpubReaderServiceTest {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath);
|
||||
|
||||
ZipFile zipFile = createMockZipFile();
|
||||
ZipFile.Builder builder = createMockZipFileBuilder(zipFile);
|
||||
@@ -453,7 +453,7 @@ class EpubReaderServiceTest {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath);
|
||||
|
||||
ZipFile zipFile = createMockZipFile();
|
||||
ZipFile.Builder builder = createMockZipFileBuilder(zipFile);
|
||||
@@ -494,7 +494,7 @@ class EpubReaderServiceTest {
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(bookEntity));
|
||||
|
||||
try (MockedStatic<FileUtils> fileUtilsStatic = mockStatic(FileUtils.class)) {
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath.toString());
|
||||
fileUtilsStatic.when(() -> FileUtils.getBookFullPath(bookEntity)).thenReturn(epubPath);
|
||||
|
||||
byte[] chapterContent = "<html><body>Chapter 1 content</body></html>".getBytes(StandardCharsets.UTF_8);
|
||||
ZipFile zipFile = createMockZipFileWithStreamableEntry("OEBPS/chapter1.xhtml", chapterContent);
|
||||
|
||||
@@ -42,10 +42,9 @@ class FileUtilsTest {
|
||||
|
||||
BookEntity book = createBookEntity(libraryPath, subPath, fileName);
|
||||
|
||||
String fullPath = FileUtils.getBookFullPath(book);
|
||||
Path fullPath = FileUtils.getBookFullPath(book);
|
||||
|
||||
String expected = libraryPath.resolve(subPath).resolve(fileName)
|
||||
.toString().replace("\\", "/");
|
||||
Path expected = libraryPath.resolve(subPath).resolve(fileName);
|
||||
|
||||
assertEquals(expected, fullPath);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user