Book additional files support library (3/3) (#872)

* feat(api/model): add new entity BookAdditionalFileEntity

* feat(api/db): add book additional file repository

* feat(api/db): add migration

* test(api/db): add book additional file repository test

* test(api/db): add book additional file repository tests for hash uniqueness

* feat(api/domain): add support to additional file model

* feat(api): add additional files controller

* refactor(api): move addAdditionalFile to FileUploadService as uploadAdditionalFile method

* feat(service): search book by additional file

* feat(services): process deleted additional files with ability to promote alternative formats to book instead of deleting them

* refactor(util): use common code to resolve patter for entity and domain object

* feat(service): move additional files

* test(service): test move additional files along with book itself

* feat(ui/domain): add alternativeFormats and supplementaryFiles to book model

* feat(ui/domain): add download additional file method to book service

* refactor(api/domain): extract FileInfo interface with common fields

Allow to share the same interface thet is implemented by AdditionFile and Book

* feat(ui): show multiple download options

* feat(ui/domain): add delete additional file method to book service

* feat(ui): add delete additional file ui

* feat(ui): add additional-file-uploader.component

* feat(ui/domain): add uploadAdditionalFile to the service

* feat(ui): add Upload File menu item

* feat(ui): show supplementary files in download menu item

* feat(ui): show supplementary files in delete file menu item

* feat(ui): book card allow to select single file to download or delete

* feat(api/domain): add scan mode and default book format to the library

* feat(ui): select scan mode and default book format

* feat(api): create/update library settings

* refactor(services): get processor based on scan mode

* feat(services): read all files if processor supports supplimentary files

* fix(services): detectNewBookPaths should check additional files as well

* feat(services): implement folder as book file processor

* test(service): add tests for FolderAsBookFileProcessor

* feat(services): allow follow links for search library files

* test(library): add tests for folder as book file processor

* refactor(library): use Path instead of String for directory path

* feat(library): sort directories before processing

* feat(library): reuse library file book type

* fix(library): do not add additional format if it is used in another book

* test(library): test same hashes files

* refactor(library): simplify FolderAsBookFileProcessor

* test(library): add test with deep folder structure

* test(library): add additional files to existing book

---------

Co-authored-by: Aditya Chandel <8075870+adityachandelgit@users.noreply.github.com>
This commit is contained in:
Alexander Puzynia
2025-08-27 08:20:29 -06:00
committed by GitHub
co-authored by Aditya Chandel
parent f725ececf5
commit 694ee11540
20 changed files with 1692 additions and 31 deletions
@@ -3,6 +3,7 @@ package com.adityachandel.booklore.repository;
import com.adityachandel.booklore.model.entity.*;
import com.adityachandel.booklore.model.enums.AdditionalFileType;
import com.adityachandel.booklore.model.enums.BookFileType;
import com.adityachandel.booklore.model.enums.LibraryScanMode;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -66,6 +67,7 @@ class BookAdditionalFileRepositoryTest {
testLibrary = LibraryEntity.builder()
.name("Test Library")
.icon("book")
.scanMode(LibraryScanMode.FILE_AS_BOOK)
.watch(false)
.build();
testLibrary = entityManager.persistAndFlush(testLibrary);
@@ -0,0 +1,316 @@
package com.adityachandel.booklore.service.library;
import com.adityachandel.booklore.model.entity.BookAdditionalFileEntity;
import com.adityachandel.booklore.model.enums.BookFileType;
import com.adityachandel.booklore.repository.BookAdditionalFileRepository;
import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.service.FileFingerprint;
import com.adityachandel.booklore.service.NotificationService;
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessor;
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessorRegistry;
import com.adityachandel.booklore.util.FileUtils;
import com.adityachandel.booklore.util.builder.LibraryTestBuilder;
import static com.adityachandel.booklore.util.builder.LibraryTestBuilderAssert.assertThat;
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;
import org.mockito.*;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class FolderAsBookFileProcessorExampleTest {
@Mock
private BookRepository bookRepository;
@Mock
private BookAdditionalFileRepository bookAdditionalFileRepository;
@Mock
private NotificationService notificationService;
@Mock
private BookFileProcessorRegistry bookFileProcessorRegistry;
@Mock
private BookFileProcessor mockBookFileProcessor;
@InjectMocks
private FolderAsBookFileProcessor processor;
@Captor
private ArgumentCaptor<BookAdditionalFileEntity> additionalFileCaptor;
private MockedStatic<FileUtils> fileUtilsMock;
private MockedStatic<FileFingerprint> fileFingerprintMock;
private LibraryTestBuilder libraryTestBuilder;
@BeforeEach
void setUp() {
fileUtilsMock = mockStatic(FileUtils.class);
fileFingerprintMock = mockStatic(FileFingerprint.class);
libraryTestBuilder = new LibraryTestBuilder(fileUtilsMock, fileFingerprintMock, bookFileProcessorRegistry, mockBookFileProcessor, bookRepository, bookAdditionalFileRepository);
}
@AfterEach
void tearDown() {
fileUtilsMock.close();
fileFingerprintMock.close();
}
@Test
void processLibraryFiles_shouldCreateNewBookFromDirectory() {
// Given
libraryTestBuilder
.addDefaultLibrary()
.addLibraryFile("/101/Accounting", "Accounting 101.pdf")
.addLibraryFile("/101/Anatomy", "Anatomy 101.pdf");
// When
processor.processLibraryFiles(libraryTestBuilder.getLibraryFiles(), libraryTestBuilder.getLibraryEntity());
// Then
assertThat(libraryTestBuilder)
.hasBooks("Accounting 101", "Anatomy 101")
.hasNoAdditionalFiles();
}
@Test
void processLibraryFiles_shouldCreateNewBookFromDirectoryWithAdditionalBookFormats() {
// Given
libraryTestBuilder
.addDefaultLibrary()
.addLibraryFile("/101/Accounting", "Accounting 101.pdf")
.addLibraryFile("/101/Accounting", "Accounting 101.epub")
.addLibraryFile("/101/Anatomy", "Anatomy 101.pdf");
// When
processor.processLibraryFiles(libraryTestBuilder.getLibraryFiles(), libraryTestBuilder.getLibraryEntity());
// Then
assertThat(libraryTestBuilder)
.hasBooks("Accounting 101", "Anatomy 101")
.bookHasAdditionalFormats("Accounting 101", BookFileType.PDF)
.bookHasNoSupplementaryFiles("Accounting 101")
.bookHasNoAdditionalFiles("Anatomy 101");
}
@Test
void processLibraryFiles_shouldCreateNewBookFromDirectoryWithAdditionalBookFormatsAndSupplementaryFiles() {
// Given
libraryTestBuilder
.addDefaultLibrary()
.addLibraryFile("/101/Accounting", "Accounting 101.pdf")
.addLibraryFile("/101/Accounting", "Accounting 101.epub")
.addLibraryFile("/101/Accounting", "Accounting 101.zip")
.addLibraryFile("/101/Anatomy", "Anatomy 101.pdf");
// When
processor.processLibraryFiles(libraryTestBuilder.getLibraryFiles(), libraryTestBuilder.getLibraryEntity());
// Then
assertThat(libraryTestBuilder)
.hasBooks("Accounting 101", "Anatomy 101")
.bookHasAdditionalFormats("Accounting 101", BookFileType.PDF)
.bookHasSupplementaryFiles("Accounting 101", "Accounting 101.zip")
.bookHasNoAdditionalFiles("Anatomy 101");
}
@Test
void processLibraryFiles_shouldCreateNewBookFromDirectoryWithSameSupplementaryFilesByHash() {
// Given
String supplementaryFileHash = "hash-accounting-101";
libraryTestBuilder
.addDefaultLibrary()
.addLibraryFile("/101/Accounting", "Accounting 101.pdf")
.addLibraryFile(
"/101/Accounting",
"Accounting 101.zip",
supplementaryFileHash)
.addLibraryFile("/101/Accounting 2nd Edition", "Accounting 101 2nd Edition.pdf")
.addLibraryFile(
"/101/Accounting 2nd Edition",
"Accounting 101 2nd Edition.zip",
supplementaryFileHash);
// When
processor.processLibraryFiles(libraryTestBuilder.getLibraryFiles(), libraryTestBuilder.getLibraryEntity());
// Then
assertThat(libraryTestBuilder)
.hasBooks("Accounting 101", "Accounting 101 2nd Edition")
.bookHasSupplementaryFiles("Accounting 101", "Accounting 101.zip")
.bookHasSupplementaryFiles("Accounting 101 2nd Edition", "Accounting 101 2nd Edition.zip");
}
@Test
void processLibraryFiles_shouldIgnoreAdditionalFormatWithTheSameHashUsedInOtherBook() {
// Given
String additionalFormatHash = "hash-accounting-101";
libraryTestBuilder
.addDefaultLibrary()
.addLibraryFile("/101/Accounting", "Accounting 101.epub")
.addLibraryFile(
"/101/Accounting",
"Accounting 101.pdf",
additionalFormatHash)
.addLibraryFile("/101/Accounting 2nd Edition", "Accounting 101 2nd Edition.epub")
.addLibraryFile(
"/101/Accounting 2nd Edition",
"Accounting 101 2nd Edition.pdf",
additionalFormatHash);
// When
processor.processLibraryFiles(libraryTestBuilder.getLibraryFiles(), libraryTestBuilder.getLibraryEntity());
// Then
assertThat(libraryTestBuilder)
.hasBooks("Accounting 101", "Accounting 101 2nd Edition")
.bookHasAdditionalFormats("Accounting 101", BookFileType.PDF)
.bookHasNoAdditionalFiles("Accounting 101 2nd Edition");
}
@Test
void processLibraryFiles_shouldAddAdditionalFormatsToExistingBook() {
// Given
libraryTestBuilder
.addDefaultLibrary()
.addBook("/101/Accounting", "Accounting 101.pdf")
.addLibraryFile("/101/Accounting", "Accounting 101.epub");
// When
processor.processLibraryFiles(libraryTestBuilder.getLibraryFiles(), libraryTestBuilder.getLibraryEntity());
// Then
assertThat(libraryTestBuilder)
.hasBooks("Accounting 101")
.bookHasAdditionalFormats("Accounting 101", BookFileType.EPUB)
.bookHasNoSupplementaryFiles("Accounting 101");
}
@Test
void processLibraryFiles_shouldAddSupplementaryFilesToExistingBook() {
// Given
libraryTestBuilder
.addDefaultLibrary()
.addBook("/101/Accounting", "Accounting 101.pdf")
.addLibraryFile("/101/Accounting", "sources.zip");
// When
processor.processLibraryFiles(libraryTestBuilder.getLibraryFiles(), libraryTestBuilder.getLibraryEntity());
// Then
assertThat(libraryTestBuilder)
.hasBooks("Accounting 101")
.bookHasNoAdditionalFormats("Accounting 101")
.bookHasSupplementaryFiles("Accounting 101", "sources.zip");
}
@Test
void processLibraryFiles_shouldAddAdditionalFilesToExistingBook() {
// Given
libraryTestBuilder
.addDefaultLibrary()
.addBook("/101/Accounting", "Accounting 101.pdf")
.addLibraryFile("/101/Accounting", "Accounting 101.epub")
.addLibraryFile("/101/Accounting", "sources.zip");
// When
processor.processLibraryFiles(libraryTestBuilder.getLibraryFiles(), libraryTestBuilder.getLibraryEntity());
// Then
assertThat(libraryTestBuilder)
.hasBooks("Accounting 101")
.bookHasAdditionalFormats("Accounting 101", BookFileType.EPUB)
.bookHasSupplementaryFiles("Accounting 101", "sources.zip");
}
@Test
void processLibraryFiles_shouldProcessDeepSubfolders() {
var javaSourcesSameHash = "hash-java-sources";
libraryTestBuilder
.addDefaultLibrary()
// Basic books
.addLibraryFile("/Basic/101/Accounting", "Accounting 101.epub")
.addLibraryFile("/Basic/101/Accounting", "Accounting 101.pdf")
.addLibraryFile("/Basic/101/Anatomy", "Anatomy 101.pdf")
.addLibraryFile("/Basic/How-To/Repair", "How to Repair.epub")
.addLibraryFile("/Basic/How-To/Repair", "How to Repair.pdf")
// Software Engineering books
.addLibraryFile("/Software Engineering/Java/Design Patterns", "Design Patterns.pdf")
.addLibraryFile("/Software Engineering/Java/Design Patterns", "Design Patterns.epub")
.addLibraryFile("/Software Engineering/Java/Design Patterns", "Design Patterns.zip", javaSourcesSameHash)
.addLibraryFile("/Software Engineering/Java/Effective Java", "Effective Java.pdf")
.addLibraryFile("/Software Engineering/Java/Effective Java", "Effective Java.epub")
.addLibraryFile("/Software Engineering/Java/Effective Java", "Effective Java.zip", javaSourcesSameHash)
.addLibraryFile("/Software Engineering/Python/AI/Machine Learning/Pytorch", "PyTorch for Machine Learning.pdf")
.addLibraryFile("/Software Engineering/Python/AI/Machine Learning/Pytorch", "PyTorch for Machine Learning.epub")
.addLibraryFile("/Software Engineering/Python/AI/Machine Learning/Pytorch", "sources.zip")
.addLibraryFile("/Software Engineering/Python/AI/Machine Learning/TensorFlow", "TensorFlow for Machine Learning.pdf")
.addLibraryFile("/Software Engineering/Python/AI/Machine Learning/TensorFlow", "TensorFlow for Machine Learning.epub")
.addLibraryFile("/Software Engineering/Python/AI/Machine Learning/TensorFlow", "sources.zip")
.addLibraryFile("/Software Engineering/Python/Flask/Flask Web Development", "Flask Web Development.pdf")
.addLibraryFile("/Software Engineering/Python/Flask/Flask Web Development", "Flask Web Development.epub")
// Comics Marvel
.addLibraryFile("/Comics/Marvel/Batman/Volume 1", "Batman v1.cbr")
.addLibraryFile("/Comics/Marvel/Batman/Volume 2", "Batman v2.cbr")
.addLibraryFile("/Comics/Marvel/Spiderman/Volume 1", "Spiderman v1.cbz")
.addLibraryFile("/Comics/Marvel/Spiderman/Volume 2", "Spiderman v2.cbz")
// Comics DC
.addLibraryFile("/Comics/DC/Superman/Volume 1", "Superman v1.cbr")
.addLibraryFile("/Comics/DC/Superman/Volume 1", "Poster.jpg")
.addLibraryFile("/Comics/DC/Superman/Volume 2", "Superman v2.cbr")
// Manga
.addLibraryFile("/Manga/One Piece/Volume 1", "One Piece v1.cbz")
.addLibraryFile("/Manga/One Piece/Volume 2", "One Piece v2.cbz")
.addLibraryFile("/Manga/Naruto/Volume 1", "Naruto v1.cbr")
.addLibraryFile("/Manga/Naruto/Volume 2", "Naruto v2.cbr");
// When
processor.processLibraryFiles(libraryTestBuilder.getLibraryFiles(), libraryTestBuilder.getLibraryEntity());
// Then
assertThat(libraryTestBuilder)
.hasBooks(
"Accounting 101", "Anatomy 101", "How to Repair",
"Design Patterns", "Effective Java",
"PyTorch for Machine Learning", "TensorFlow for Machine Learning",
"Flask Web Development",
"Batman v1", "Batman v2",
"Spiderman v1", "Spiderman v2",
"Superman v1", "Superman v2",
"One Piece v1", "One Piece v2",
"Naruto v1", "Naruto v2")
// Basic books
.bookHasAdditionalFormats("Accounting 101", BookFileType.PDF)
.bookHasNoSupplementaryFiles("Anatomy 101")
.bookHasAdditionalFormats("How to Repair", BookFileType.PDF)
// Software Engineering books
.bookHasAdditionalFormats("Design Patterns", BookFileType.PDF)
.bookHasSupplementaryFiles("Design Patterns", "Design Patterns.zip")
.bookHasAdditionalFormats("Effective Java", BookFileType.PDF)
.bookHasSupplementaryFiles("Effective Java", "Effective Java.zip")
.bookHasAdditionalFormats("PyTorch for Machine Learning", BookFileType.PDF)
.bookHasSupplementaryFiles("PyTorch for Machine Learning", "sources.zip")
.bookHasAdditionalFormats("TensorFlow for Machine Learning", BookFileType.PDF)
.bookHasSupplementaryFiles("TensorFlow for Machine Learning", "sources.zip")
.bookHasAdditionalFormats("Flask Web Development", BookFileType.PDF)
// Comics Marvel
.bookHasNoAdditionalFiles("Batman v1")
.bookHasNoAdditionalFiles("Batman v2")
.bookHasNoAdditionalFiles("Spiderman v1")
.bookHasNoAdditionalFiles("Spiderman v2")
// Comics DC
.bookHasSupplementaryFiles("Superman v1", "Poster.jpg")
.bookHasNoAdditionalFiles("Superman v2")
// Manga
.bookHasNoAdditionalFiles("One Piece v1")
.bookHasNoAdditionalFiles("One Piece v2")
.bookHasNoAdditionalFiles("Naruto v1")
.bookHasNoAdditionalFiles("Naruto v2");
}
}
@@ -0,0 +1,40 @@
package com.adityachandel.booklore.service.library;
import com.adityachandel.booklore.model.enums.LibraryScanMode;
import com.adityachandel.booklore.repository.BookAdditionalFileRepository;
import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.service.NotificationService;
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessorRegistry;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
class FolderAsBookFileProcessorSimpleTest {
@Test
void getScanMode_shouldReturnFolderAsBook() {
BookRepository bookRepository = mock(BookRepository.class);
BookAdditionalFileRepository bookAdditionalFileRepository = mock(BookAdditionalFileRepository.class);
NotificationService notificationService = mock(NotificationService.class);
BookFileProcessorRegistry bookFileProcessorRegistry = mock(BookFileProcessorRegistry.class);
FolderAsBookFileProcessor processor = new FolderAsBookFileProcessor(
bookRepository, bookAdditionalFileRepository, notificationService, bookFileProcessorRegistry);
assertThat(processor.getScanMode()).isEqualTo(LibraryScanMode.FOLDER_AS_BOOK);
}
@Test
void supportsSupplementaryFiles_shouldReturnTrue() {
BookRepository bookRepository = mock(BookRepository.class);
BookAdditionalFileRepository bookAdditionalFileRepository = mock(BookAdditionalFileRepository.class);
NotificationService notificationService = mock(NotificationService.class);
BookFileProcessorRegistry bookFileProcessorRegistry = mock(BookFileProcessorRegistry.class);
FolderAsBookFileProcessor processor = new FolderAsBookFileProcessor(
bookRepository, bookAdditionalFileRepository, notificationService, bookFileProcessorRegistry);
assertThat(processor.supportsSupplementaryFiles()).isTrue();
}
}
@@ -0,0 +1,419 @@
package com.adityachandel.booklore.service.library;
import com.adityachandel.booklore.model.dto.Book;
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
import com.adityachandel.booklore.model.entity.*;
import com.adityachandel.booklore.model.enums.AdditionalFileType;
import com.adityachandel.booklore.model.enums.BookFileType;
import com.adityachandel.booklore.model.enums.LibraryScanMode;
import com.adityachandel.booklore.model.websocket.LogNotification;
import com.adityachandel.booklore.model.websocket.Topic;
import com.adityachandel.booklore.repository.BookAdditionalFileRepository;
import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.service.FileFingerprint;
import com.adityachandel.booklore.service.NotificationService;
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessor;
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessorRegistry;
import com.adityachandel.booklore.util.FileUtils;
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;
import org.mockito.*;
import org.mockito.junit.jupiter.MockitoExtension;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class FolderAsBookFileProcessorTest {
@Mock
private BookRepository bookRepository;
@Mock
private BookAdditionalFileRepository bookAdditionalFileRepository;
@Mock
private NotificationService notificationService;
@Mock
private BookFileProcessorRegistry bookFileProcessorRegistry;
@Mock
private BookFileProcessor mockBookFileProcessor;
@InjectMocks
private FolderAsBookFileProcessor processor;
@Captor
private ArgumentCaptor<BookAdditionalFileEntity> additionalFileCaptor;
private MockedStatic<FileUtils> fileUtilsMock;
private MockedStatic<FileFingerprint> fileFingerprintMock;
@BeforeEach
void setUp() {
fileUtilsMock = mockStatic(FileUtils.class);
fileFingerprintMock = mockStatic(FileFingerprint.class);
// Setup common FileUtils mocks for all tests
fileUtilsMock.when(() -> FileUtils.getFileSizeInKb(any(Path.class))).thenReturn(100L);
fileFingerprintMock.when(() -> FileFingerprint.generateHash(any(Path.class)))
.then(invocation -> {
MessageDigest digest = MessageDigest.getInstance("MD5");
Path path = invocation.getArgument(0);
byte[] hash = digest.digest(path.toString().getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
});
}
@AfterEach
void tearDown() {
fileUtilsMock.close();
fileFingerprintMock.close();
}
@Test
void getScanMode_shouldReturnFolderAsBook() {
assertThat(processor.getScanMode()).isEqualTo(LibraryScanMode.FOLDER_AS_BOOK);
}
@Test
void supportsSupplementaryFiles_shouldReturnTrue() {
assertThat(processor.supportsSupplementaryFiles()).isTrue();
}
@Test
void processLibraryFiles_shouldCreateNewBookFromDirectory() {
// Given
LibraryEntity libraryEntity = createLibraryEntity();
List<LibraryFile> libraryFiles = createLibraryFilesInSameDirectory();
Book createdBook = Book.builder()
.id(1L)
.fileName("book.pdf")
.bookType(BookFileType.PDF)
.build();
BookEntity bookEntity = createBookEntity(1L, "book.pdf", "books");
when(bookRepository.findAllByLibraryPathIdAndFileSubPathStartingWith(anyLong(), anyString()))
.thenReturn(new ArrayList<>());
when(bookFileProcessorRegistry.getProcessorOrThrow(BookFileType.PDF))
.thenReturn(mockBookFileProcessor);
when(mockBookFileProcessor.processFile(any(LibraryFile.class)))
.thenReturn(createdBook);
when(bookRepository.getReferenceById(createdBook.getId()))
.thenReturn(bookEntity);
when(bookAdditionalFileRepository.findByLibraryPath_IdAndFileSubPathAndFileName(anyLong(), anyString(), anyString()))
.thenReturn(Optional.empty());
// When
processor.processLibraryFiles(libraryFiles, libraryEntity);
// Then
verify(mockBookFileProcessor).processFile(any(LibraryFile.class));
verify(notificationService).sendMessage(Topic.BOOK_ADD, createdBook);
verify(notificationService).sendMessage(eq(Topic.LOG), any(LogNotification.class));
verify(bookAdditionalFileRepository, times(2)).save(additionalFileCaptor.capture());
List<BookAdditionalFileEntity> capturedFiles = additionalFileCaptor.getAllValues();
assertThat(capturedFiles).hasSize(2);
assertThat(capturedFiles).extracting(BookAdditionalFileEntity::getFileName)
.containsExactlyInAnyOrder("book.epub", "cover.jpg");
assertThat(capturedFiles).extracting(BookAdditionalFileEntity::getAdditionalFileType)
.containsExactly(AdditionalFileType.ALTERNATIVE_FORMAT, AdditionalFileType.SUPPLEMENTARY);
}
@Test
void processLibraryFiles_shouldProcessExistingBook() {
// Given
LibraryEntity libraryEntity = createLibraryEntity();
List<LibraryFile> libraryFiles = createLibraryFilesInSameDirectory()
.stream()
.filter(f -> !f.getFileName().equals("book.pdf"))
.toList();
BookEntity existingBook = createBookEntity(1L, "book.pdf", "books");
when(bookRepository.findAllByLibraryPathIdAndFileSubPathStartingWith(anyLong(), eq("books")))
.thenReturn(List.of(existingBook));
when(bookAdditionalFileRepository.findByLibraryPath_IdAndFileSubPathAndFileName(anyLong(), anyString(), anyString()))
.thenReturn(Optional.empty());
// When
processor.processLibraryFiles(libraryFiles, libraryEntity);
// Then
verify(mockBookFileProcessor, never()).processFile(any());
verify(notificationService, never()).sendMessage(eq(Topic.BOOK_ADD), any());
verify(bookAdditionalFileRepository, times(2)).save(additionalFileCaptor.capture());
List<BookAdditionalFileEntity> capturedFiles = additionalFileCaptor.getAllValues();
assertThat(capturedFiles).hasSize(2);
assertThat(capturedFiles).extracting(BookAdditionalFileEntity::getFileName)
.containsExactlyInAnyOrder("book.epub", "cover.jpg");
}
@Test
void processLibraryFiles_shouldProcessFilesWithParentBook() {
// Given
LibraryEntity libraryEntity = createLibraryEntity();
List<LibraryFile> libraryFiles = List.of(
createLibraryFile("file1.txt", "books/chapter1"),
createLibraryFile("file2.txt", "books/chapter1")
);
BookEntity parentBook = createBookEntity(1L, "book.pdf", "books");
when(bookRepository.findAllByLibraryPathIdAndFileSubPathStartingWith(anyLong(), eq("books/chapter1")))
.thenReturn(new ArrayList<>());
when(bookRepository.findAllByLibraryPathIdAndFileSubPathStartingWith(anyLong(), eq("books")))
.thenReturn(List.of(parentBook));
when(bookAdditionalFileRepository.findByLibraryPath_IdAndFileSubPathAndFileName(anyLong(), anyString(), anyString()))
.thenReturn(Optional.empty());
// When
processor.processLibraryFiles(libraryFiles, libraryEntity);
// Then
verify(mockBookFileProcessor, never()).processFile(any());
verify(bookAdditionalFileRepository, times(2)).save(additionalFileCaptor.capture());
List<BookAdditionalFileEntity> capturedFiles = additionalFileCaptor.getAllValues();
assertThat(capturedFiles).hasSize(2);
assertThat(capturedFiles).extracting(BookAdditionalFileEntity::getAdditionalFileType)
.containsOnly(AdditionalFileType.SUPPLEMENTARY);
}
@Test
void processLibraryFiles_shouldRespectDefaultBookFormat() {
// Given
LibraryEntity libraryEntity = createLibraryEntity();
libraryEntity.setDefaultBookFormat(BookFileType.EPUB);
List<LibraryFile> libraryFiles = List.of(
createLibraryFile("book.pdf", "books", BookFileType.PDF),
createLibraryFile("book.epub", "books", BookFileType.EPUB),
createLibraryFile("book.cbz", "books", BookFileType.CBX)
);
Book createdBook = Book.builder()
.id(1L)
.fileName("book.epub")
.bookType(BookFileType.EPUB)
.build();
BookEntity bookEntity = createBookEntity(1L, "book.epub", "books");
when(bookRepository.findAllByLibraryPathIdAndFileSubPathStartingWith(anyLong(), anyString()))
.thenReturn(new ArrayList<>());
when(bookFileProcessorRegistry.getProcessorOrThrow(BookFileType.EPUB))
.thenReturn(mockBookFileProcessor);
when(mockBookFileProcessor.processFile(argThat(file -> file.getFileName().equals("book.epub"))))
.thenReturn(createdBook);
when(bookRepository.getReferenceById(createdBook.getId()))
.thenReturn(bookEntity);
when(bookAdditionalFileRepository.findByLibraryPath_IdAndFileSubPathAndFileName(anyLong(), anyString(), anyString()))
.thenReturn(Optional.empty());
// When
processor.processLibraryFiles(libraryFiles, libraryEntity);
// Then
verify(mockBookFileProcessor).processFile(argThat(file -> file.getFileName().equals("book.epub")));
verify(bookAdditionalFileRepository, times(2)).save(additionalFileCaptor.capture());
List<BookAdditionalFileEntity> capturedFiles = additionalFileCaptor.getAllValues();
assertThat(capturedFiles).extracting(BookAdditionalFileEntity::getFileName)
.containsExactlyInAnyOrder("book.pdf", "book.cbz");
}
@Test
void processLibraryFiles_shouldUseDefaultPriorityWhenNoDefaultFormat() {
// Given
LibraryEntity libraryEntity = createLibraryEntity();
libraryEntity.setDefaultBookFormat(null);
List<LibraryFile> libraryFiles = List.of(
createLibraryFile("book.epub", "books", BookFileType.EPUB),
createLibraryFile("book.cbz", "books", BookFileType.CBX),
createLibraryFile("book.pdf", "books", BookFileType.PDF)
);
Book createdBook = Book.builder()
.id(1L)
.fileName("book.pdf")
.bookType(BookFileType.PDF)
.build();
BookEntity bookEntity = createBookEntity(1L, "book.pdf", "books");
when(bookRepository.findAllByLibraryPathIdAndFileSubPathStartingWith(anyLong(), anyString()))
.thenReturn(new ArrayList<>());
when(bookFileProcessorRegistry.getProcessorOrThrow(BookFileType.PDF))
.thenReturn(mockBookFileProcessor);
when(mockBookFileProcessor.processFile(argThat(file -> file.getFileName().equals("book.pdf"))))
.thenReturn(createdBook);
when(bookRepository.getReferenceById(createdBook.getId()))
.thenReturn(bookEntity);
when(bookAdditionalFileRepository.findByLibraryPath_IdAndFileSubPathAndFileName(anyLong(), anyString(), anyString()))
.thenReturn(Optional.empty());
// When
processor.processLibraryFiles(libraryFiles, libraryEntity);
// Then
verify(mockBookFileProcessor).processFile(argThat(file -> file.getFileName().equals("book.pdf")));
}
@Test
void processLibraryFiles_shouldSkipExistingAdditionalFiles() {
// Given
LibraryEntity libraryEntity = createLibraryEntity();
List<LibraryFile> libraryFiles = createLibraryFilesInSameDirectory()
.stream()
.filter(f -> !f.getFileName().equals("book.pdf"))
.toList();
BookEntity existingBook = createBookEntity(1L, "book.pdf", "books");
BookAdditionalFileEntity existingAdditionalFile = BookAdditionalFileEntity.builder()
.id(1L)
.book(existingBook)
.fileName("book.epub")
.fileSubPath("books")
.additionalFileType(AdditionalFileType.ALTERNATIVE_FORMAT)
.build();
when(bookRepository.findAllByLibraryPathIdAndFileSubPathStartingWith(anyLong(), eq("books")))
.thenReturn(List.of(existingBook));
when(bookAdditionalFileRepository.findByLibraryPath_IdAndFileSubPathAndFileName(anyLong(), eq("books"), eq("book.epub")))
.thenReturn(Optional.of(existingAdditionalFile));
when(bookAdditionalFileRepository.findByLibraryPath_IdAndFileSubPathAndFileName(anyLong(), eq("books"), eq("cover.jpg")))
.thenReturn(Optional.empty());
// When
processor.processLibraryFiles(libraryFiles, libraryEntity);
// Then
verify(bookAdditionalFileRepository, times(1)).save(additionalFileCaptor.capture());
BookAdditionalFileEntity capturedFile = additionalFileCaptor.getValue();
assertThat(capturedFile.getFileName()).isEqualTo("cover.jpg");
}
@Test
void processLibraryFiles_shouldHandleEmptyDirectory() {
// Given
LibraryEntity libraryEntity = createLibraryEntity();
List<LibraryFile> libraryFiles = List.of(
createLibraryFile("readme.txt", "docs"),
createLibraryFile("notes.txt", "docs")
);
when(bookRepository.findAllByLibraryPathIdAndFileSubPathStartingWith(anyLong(), anyString()))
.thenReturn(new ArrayList<>());
// When
processor.processLibraryFiles(libraryFiles, libraryEntity);
// Then
verify(mockBookFileProcessor, never()).processFile(any());
verify(notificationService, never()).sendMessage(eq(Topic.BOOK_ADD), any());
verify(bookAdditionalFileRepository, never()).save(any());
}
@Test
void processLibraryFiles_shouldHandleProcessorError() {
// Given
LibraryEntity libraryEntity = createLibraryEntity();
List<LibraryFile> libraryFiles = List.of(
createLibraryFile("book.pdf", "books", BookFileType.PDF)
);
when(bookRepository.findAllByLibraryPathIdAndFileSubPathStartingWith(anyLong(), anyString()))
.thenReturn(new ArrayList<>());
when(bookFileProcessorRegistry.getProcessorOrThrow(BookFileType.PDF))
.thenReturn(mockBookFileProcessor);
when(mockBookFileProcessor.processFile(any(LibraryFile.class)))
.thenThrow(new RuntimeException("Processing error"));
// When
processor.processLibraryFiles(libraryFiles, libraryEntity);
// Then
verify(notificationService, never()).sendMessage(eq(Topic.BOOK_ADD), any());
verify(bookAdditionalFileRepository, never()).save(any());
}
// Helper methods
private LibraryEntity createLibraryEntity() {
LibraryEntity library = new LibraryEntity();
library.setId(1L);
library.setName("Test Library");
library.setScanMode(LibraryScanMode.FOLDER_AS_BOOK);
LibraryPathEntity path = new LibraryPathEntity();
path.setId(1L);
path.setPath("/test/library");
library.setLibraryPaths(List.of(path));
return library;
}
private List<LibraryFile> createLibraryFilesInSameDirectory() {
return List.of(
createLibraryFile("book.pdf", "books", BookFileType.PDF),
createLibraryFile("book.epub", "books", BookFileType.EPUB),
createLibraryFile("cover.jpg", "books")
);
}
private LibraryFile createLibraryFile(String fileName, String subPath) {
return createLibraryFile(fileName, subPath, null);
}
private LibraryFile createLibraryFile(String fileName, String subPath, BookFileType bookFileType) {
LibraryPathEntity libraryPath = new LibraryPathEntity();
libraryPath.setId(1L);
libraryPath.setPath("/test/library");
return LibraryFile.builder()
.libraryEntity(new LibraryEntity())
.libraryPathEntity(libraryPath)
.fileName(fileName)
.fileSubPath(subPath)
.bookFileType(bookFileType)
.build();
}
private BookEntity createBookEntity(Long id, String fileName, String subPath) {
BookEntity book = new BookEntity();
book.setId(id);
book.setFileName(fileName);
book.setFileSubPath(subPath);
book.setBookType(BookFileType.PDF);
book.setAddedOn(Instant.now());
LibraryPathEntity libraryPath = new LibraryPathEntity();
libraryPath.setId(1L);
libraryPath.setPath("/test/library");
book.setLibraryPath(libraryPath);
return book;
}
}
@@ -0,0 +1,341 @@
package com.adityachandel.booklore.util.builder;
import com.adityachandel.booklore.mapper.BookMapper;
import com.adityachandel.booklore.mapper.BookMapperImpl;
import com.adityachandel.booklore.model.dto.Book;
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
import com.adityachandel.booklore.model.entity.*;
import com.adityachandel.booklore.model.enums.AdditionalFileType;
import com.adityachandel.booklore.model.enums.BookFileExtension;
import com.adityachandel.booklore.model.enums.BookFileType;
import com.adityachandel.booklore.model.enums.LibraryScanMode;
import com.adityachandel.booklore.repository.BookAdditionalFileRepository;
import com.adityachandel.booklore.repository.BookRepository;
import com.adityachandel.booklore.service.FileFingerprint;
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessor;
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessorRegistry;
import com.adityachandel.booklore.util.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.jetbrains.annotations.NotNull;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.lenient;
/**
* Test builder for creating Library-related test objects.
* Provides fluent API for creating LibraryEntity and Library DTO objects with sensible defaults.
*/
public class LibraryTestBuilder {
public static final String DEFAULT_LIBRARY_NAME = "Test Library";
public static final String DEFAULT_LIBRARY_PATH = "/library/books";
private static final BookMapper BOOK_MAPPER = new BookMapperImpl();
private Long libraryId = 1L;
private final List<LibraryEntity> libraries = new ArrayList<>();
private final Map<String, LibraryEntity> libraryMap = new HashMap<>();
// DTO-specific fields
private final List<LibraryFile> libraryFiles = new ArrayList<>();
private final Map<Path, String> libraryFileHashes = new HashMap<>();
private final Map<Long, BookEntity> bookRepository = new HashMap<>();
private final Map<String, BookEntity> bookMap = new HashMap<>();
private final Map<Long, BookAdditionalFileEntity> bookAdditionalFileRepository = new HashMap<>();
public LibraryTestBuilder(MockedStatic<FileUtils> fileUtilsMock,
MockedStatic<FileFingerprint> fileFingerprintMock,
BookFileProcessorRegistry bookFileProcessorRegistry,
BookFileProcessor bookFileProcessorMock,
BookRepository bookRepositoryMock,
BookAdditionalFileRepository bookAdditionalFileRepositoryMock) {
fileUtilsMock.when(() -> FileUtils.getFileSizeInKb(any(Path.class))).thenReturn(100L);
fileFingerprintMock.when(() -> FileFingerprint.generateHash(any(Path.class)))
.then(invocation -> {
Path path = invocation.getArgument(0);
return computeFileHash(path);
});
lenient().when(bookFileProcessorRegistry.getProcessorOrThrow(any(BookFileType.class)))
.thenReturn(bookFileProcessorMock);
lenient().when(bookFileProcessorMock.processFile(any(LibraryFile.class)))
.then(invocation -> {
LibraryFile libraryFile = invocation.getArgument(0);
return processFile(libraryFile);
});
lenient().when(bookRepositoryMock.getReferenceById(anyLong()))
.thenAnswer(invocation -> {
Long bookId = invocation.getArgument(0);
return getBookById(bookId);
});
when(bookRepositoryMock.findAllByLibraryPathIdAndFileSubPathStartingWith(anyLong(), any(String.class)))
.thenAnswer(invocation -> {
Long libraryPathId = invocation.getArgument(0);
String fileSubPath = invocation.getArgument(1);
return bookRepository.values()
.stream()
.filter(book -> book.getLibraryPath().getId().equals(libraryPathId) &&
book.getFileSubPath().startsWith(fileSubPath))
.toList();
});
// lenient is used to avoid strict stubbing issues,
// the builder does not know when the save method will be called
lenient().when(bookAdditionalFileRepositoryMock.save(any(BookAdditionalFileEntity.class)))
.thenAnswer(invocation -> {
BookAdditionalFileEntity additionalFile = invocation.getArgument(0);
return saveBookAdditionalFile(additionalFile);
});
}
/**
* Creates a default library with a single path.
*/
public LibraryTestBuilder addDefaultLibrary() {
return addLibrary(DEFAULT_LIBRARY_NAME)
.addPath(DEFAULT_LIBRARY_PATH);
}
public LibraryTestBuilder addLibrary(String name) {
LibraryEntity library = new LibraryEntity();
library.setId(libraryId++);
library.setName(name);
library.setScanMode(LibraryScanMode.FOLDER_AS_BOOK);
library.setDefaultBookFormat(BookFileType.EPUB);
library.setLibraryPaths(new ArrayList<>());
libraries.add(library);
libraryMap.put(name, library);
return this;
}
public LibraryEntity getLibraryEntity() {
if (libraries.isEmpty()) {
throw new IllegalStateException("No library available. Please add a library first.");
}
return getLibraryEntity(DEFAULT_LIBRARY_NAME);
}
public LibraryEntity getLibraryEntity(String libraryName) {
return libraryMap.get(libraryName);
}
public List<LibraryFile> getLibraryFiles() {
if (libraryFiles.isEmpty()) {
throw new IllegalStateException("No library files available. Please add a library file first.");
}
return List.copyOf(libraryFiles);
}
public List<BookEntity> getBookEntities() {
if (bookRepository.isEmpty()) {
throw new IllegalStateException("No book entities available. Please process a file first.");
}
return new ArrayList<>(bookRepository.values());
}
public BookEntity getBookEntity(String bookTitle) {
if (bookMap.isEmpty()) {
throw new IllegalStateException("No book entities available. Please process a file first.");
}
if (!bookMap.containsKey(bookTitle)) {
throw new IllegalStateException("No book found with title: " + bookTitle);
}
return bookMap.get(bookTitle);
}
public List<BookAdditionalFileEntity> getBookAdditionalFiles() {
return new ArrayList<>(bookAdditionalFileRepository.values());
}
public LibraryTestBuilder addPath(String path) {
if (libraries.isEmpty()) {
throw new IllegalStateException("No library available to add a path. Please add a library first.");
}
var library = libraries.getLast();
var id = libraries
.stream()
.map(l -> l.getLibraryPaths()
.stream()
.map(LibraryPathEntity::getId)
.max(Long::compareTo)
.orElse(0L))
.max(Long::compareTo)
.orElse(0L)+ 1;
LibraryPathEntity libraryPath = LibraryPathEntity.builder()
.id(id)
.path(path)
.build();
library.getLibraryPaths().add(libraryPath);
return this;
}
public LibraryTestBuilder addBook(String fileSubPath, String fileName) {
fileSubPath = removeLeadingSlash(fileSubPath);
long id = bookRepository.size() + 1L;
BookMetadataEntity metadata = BookMetadataEntity.builder()
.title(FilenameUtils.removeExtension(fileName))
.bookId(id)
.build();
String hash = computeFileHash(Path.of(fileSubPath, fileName));
BookEntity bookEntity = BookEntity.builder()
.id(id)
.fileName(fileName)
.fileSubPath(fileSubPath)
.bookType(getBookFileType(fileName))
.fileSizeKb(1024L)
.library(getLibraryEntity())
.libraryPath(getLibraryEntity().getLibraryPaths().getLast())
.addedOn(java.time.Instant.now())
.initialHash(hash)
.currentHash(hash)
.metadata(metadata)
.additionalFiles(new ArrayList<>())
.build();
bookRepository.put(bookEntity.getId(), bookEntity);
bookMap.put(metadata.getTitle(), bookEntity);
return this;
}
public LibraryTestBuilder addLibraryFile(String fileSubPath, String fileName, String hash) {
addLibraryFile(fileSubPath, fileName);
var lastLibraryFiles = libraryFiles.getLast();
Path filePath = Path.of(lastLibraryFiles.getLibraryPathEntity().getPath(), fileSubPath, fileName);
if (libraryFileHashes.containsKey(filePath)) {
throw new IllegalArgumentException("File with the same path and name already exists: " + fileSubPath + "/" + fileName);
}
libraryFileHashes.put(filePath, hash);
return this;
}
public LibraryTestBuilder addLibraryFile(String fileSubPath, String fileName) {
if (libraries.isEmpty()) {
throw new IllegalStateException("No library available to add a book. Please add a library first.");
}
var library = libraries.getLast();
if (library.getLibraryPaths().isEmpty()) {
throw new IllegalStateException("No library path available to add a book. Please add a path first.");
}
var libraryPath = library.getLibraryPaths().getLast();
// Really don't want to check if there is subpath in library paths
var libraryFile = LibraryFile.builder()
.libraryPathEntity(libraryPath)
.fileSubPath(fileSubPath)
.fileName(fileName)
.bookFileType(getBookFileType(fileName))
.build();
libraryFiles.add(libraryFile);
return this;
}
private @NotNull String computeFileHash(Path path) {
if (libraryFileHashes.containsKey(path)) {
return libraryFileHashes.get(path);
}
MessageDigest digest;
try {
digest = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
byte[] hash = digest.digest(path.getFileName().toString().getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
}
private Book processFile(LibraryFile libraryFile) {
var hash = computeFileHash(libraryFile.getFullPath());
long id = libraryFiles.indexOf(libraryFile) + 1L;
BookMetadataEntity metadata = BookMetadataEntity.builder()
.title(FilenameUtils.removeExtension(libraryFile.getFileName()))
.bookId(id)
.build();
BookEntity bookEntity = BookEntity.builder()
.id(id) // Simple ID generation based on index
.fileName(libraryFile.getFileName())
.fileSubPath(libraryFile.getFileSubPath())
.bookType(libraryFile.getBookFileType())
.fileSizeKb(1024L)
.library(libraryFile.getLibraryPathEntity().getLibrary())
.libraryPath(libraryFile.getLibraryPathEntity())
.addedOn(java.time.Instant.now())
.initialHash(hash)
.currentHash(hash)
.metadata(metadata)
.additionalFiles(new ArrayList<>())
.build();
bookRepository.put(bookEntity.getId(), bookEntity);
bookMap.put(metadata.getTitle(), bookEntity);
return BOOK_MAPPER.toBook(bookEntity);
}
private BookEntity getBookById(Long bookId) {
if (!bookRepository.containsKey(bookId)) {
throw new IllegalStateException("No book found with ID: " + bookId);
}
return bookRepository.get(bookId);
}
private @NotNull BookAdditionalFileEntity saveBookAdditionalFile(BookAdditionalFileEntity additionalFile) {
if (additionalFile.getId() != null) {
throw new IllegalArgumentException("ID must be null for new additional files");
}
// Do not allow files with duplicate hashes for alternative formats only
if (additionalFile.getAdditionalFileType() == AdditionalFileType.ALTERNATIVE_FORMAT &&
bookAdditionalFileRepository.values()
.stream()
.anyMatch(existingFile -> existingFile.getCurrentHash()
.equals(additionalFile.getCurrentHash()))) {
throw new IllegalArgumentException("File with the same hash already exists: " + additionalFile.getCurrentHash());
}
additionalFile.setId((long) bookAdditionalFileRepository.size() + 1);
bookAdditionalFileRepository.put(additionalFile.getId(), additionalFile);
return additionalFile;
}
private static BookFileType getBookFileType(String fileName) {
var extension = BookFileExtension.fromFileName(fileName);
return extension.map(BookFileExtension::getType).orElse(null);
}
private static String removeLeadingSlash(String path) {
return path.startsWith("/") ? path.substring(1) : path;
}
}
@@ -0,0 +1,121 @@
package com.adityachandel.booklore.util.builder;
import com.adityachandel.booklore.model.entity.BookAdditionalFileEntity;
import com.adityachandel.booklore.model.enums.AdditionalFileType;
import com.adityachandel.booklore.model.enums.BookFileExtension;
import com.adityachandel.booklore.model.enums.BookFileType;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.Assertions;
import java.util.Optional;
public class LibraryTestBuilderAssert extends AbstractAssert<LibraryTestBuilderAssert, LibraryTestBuilder> {
protected LibraryTestBuilderAssert(LibraryTestBuilder libraryTestBuilder) {
super(libraryTestBuilder, LibraryTestBuilderAssert.class);
}
public static LibraryTestBuilderAssert assertThat(LibraryTestBuilder actual) {
return new LibraryTestBuilderAssert(actual);
}
public LibraryTestBuilderAssert hasBooks(String ...expectedBookTitles) {
Assertions.assertThat(actual.getBookEntities())
.extracting(bookEntity -> bookEntity.getMetadata().getTitle())
.containsExactlyInAnyOrder(expectedBookTitles);
return this;
}
public LibraryTestBuilderAssert hasNoAdditionalFiles() {
var additionalFiles = actual.getBookAdditionalFiles();
Assertions.assertThat(additionalFiles)
.isEmpty();
return this;
}
public LibraryTestBuilderAssert bookHasAdditionalFormats(String bookTitle, BookFileType ...additionalFormatTypes) {
var book = actual.getBookEntity(bookTitle);
Assertions.assertThat(book)
.describedAs("Book with title '%s' should exist", bookTitle)
.isNotNull();
Assertions.assertThat(book.getAdditionalFiles()
.stream()
.filter(a -> a.getAdditionalFileType() == AdditionalFileType.ALTERNATIVE_FORMAT)
.map(BookAdditionalFileEntity::getFileName)
.map(BookFileExtension::fromFileName)
.filter(Optional::isPresent)
.map(Optional::get)
.map(BookFileExtension::getType))
.describedAs("Book '%s' should have additional formats: %s", bookTitle, additionalFormatTypes)
.containsExactlyInAnyOrder(additionalFormatTypes);
return this;
}
public LibraryTestBuilderAssert bookHasSupplementaryFiles(String bookTitle, String ...supplementaryFiles) {
var book = actual.getBookEntity(bookTitle);
Assertions.assertThat(book)
.describedAs("Book with title '%s' should exist", bookTitle)
.isNotNull();
Assertions.assertThat(book.getAdditionalFiles()
.stream()
.filter(a -> a.getAdditionalFileType() == AdditionalFileType.SUPPLEMENTARY)
.map(BookAdditionalFileEntity::getFileName))
.describedAs("Book '%s' should have supplementary files", bookTitle)
.containsExactlyInAnyOrder(supplementaryFiles);
var additionalFiles = actual.getBookAdditionalFiles();
Assertions.assertThat(additionalFiles)
.describedAs("Book '%s' should have supplementary files", bookTitle)
.anyMatch(a -> a.getAdditionalFileType() == AdditionalFileType.SUPPLEMENTARY &&
a.getBook().getId().equals(book.getId()) &&
a.getFileName().equals(supplementaryFiles[0]));
return this;
}
public LibraryTestBuilderAssert bookHasNoAdditionalFormats(String bookTitle) {
var book = actual.getBookEntity(bookTitle);
Assertions.assertThat(book)
.describedAs("Book with title '%s' should exist", bookTitle)
.isNotNull();
Assertions.assertThat(book.getAdditionalFiles()
.stream()
.filter(a -> a.getAdditionalFileType() == AdditionalFileType.ALTERNATIVE_FORMAT))
.describedAs("Book '%s' should have no additional formats", bookTitle)
.isEmpty();
return this;
}
public LibraryTestBuilderAssert bookHasNoSupplementaryFiles(String bookTitle) {
var book = actual.getBookEntity(bookTitle);
Assertions.assertThat(book)
.describedAs("Book with title '%s' should exist", bookTitle)
.isNotNull();
Assertions.assertThat(book.getAdditionalFiles())
.describedAs("Book '%s' should have no supplementary files", bookTitle)
.noneMatch(a -> a.getAdditionalFileType() == AdditionalFileType.SUPPLEMENTARY);
return this;
}
public LibraryTestBuilderAssert bookHasNoAdditionalFiles(String bookTitle) {
var book = actual.getBookEntity(bookTitle);
Assertions.assertThat(book)
.describedAs("Book with title '%s' should exist", bookTitle)
.isNotNull();
Assertions.assertThat(book.getAdditionalFiles())
.describedAs("Book '%s' should have no additional files", bookTitle)
.isEmpty();
return this;
}
}