From 03311b76eedd84ad42e2b7b858828071783ad6cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= <127139797+balazs-szucs@users.noreply.github.com> Date: Thu, 1 Jan 2026 08:49:31 +0100 Subject: [PATCH] feat(library): improve detection of new files by using unique key and add tests for subpath handling (#2078) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Balázs Szücs --- .../library/LibraryProcessingService.java | 32 +- .../library/LibraryProcessingServiceTest.java | 276 ++++++++++++++++++ 2 files changed, 301 insertions(+), 7 deletions(-) create mode 100644 booklore-api/src/test/java/com/adityachandel/booklore/service/library/LibraryProcessingServiceTest.java diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java index e4c719ab2..64daab00d 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java @@ -46,7 +46,7 @@ public class LibraryProcessingService { LibraryFileProcessor processor = fileProcessorRegistry.getProcessor(libraryEntity); try { List libraryFiles = libraryFileHelper.getLibraryFiles(libraryEntity, processor); - processor.processLibraryFiles(libraryFiles, libraryEntity); + processor.processLibraryFiles(detectNewBookPaths(libraryFiles, libraryEntity), libraryEntity); notificationService.sendMessage(Topic.LOG, LogNotification.info("Finished processing library: " + libraryEntity.getName())); } catch (IOException e) { log.error("Failed to process library {}: {}", libraryEntity.getName(), e.getMessage(), e); @@ -96,21 +96,39 @@ public class LibraryProcessingService { } protected List detectNewBookPaths(List libraryFiles, LibraryEntity libraryEntity) { - Set existingFullPaths = libraryEntity.getBookEntities().stream() - .map(BookEntity::getFullFilePath) + Set existingKeys = libraryEntity.getBookEntities().stream() + .map(this::generateUniqueKey) .collect(Collectors.toSet()); - Set additionalFilePaths = bookAdditionalFileRepository.findByLibraryId(libraryEntity.getId()).stream() - .map(BookAdditionalFileEntity::getFullFilePath) + Set additionalFileKeys = bookAdditionalFileRepository.findByLibraryId(libraryEntity.getId()).stream() + .map(this::generateUniqueKey) .collect(Collectors.toSet()); - existingFullPaths.addAll(additionalFilePaths); + existingKeys.addAll(additionalFileKeys); return libraryFiles.stream() - .filter(file -> !existingFullPaths.contains(file.getFullPath())) + .filter(file -> !existingKeys.contains(generateUniqueKey(file))) .collect(Collectors.toList()); } + private String generateUniqueKey(BookEntity book) { + return generateKey(book.getLibraryPath().getId(), book.getFileSubPath(), book.getFileName()); + } + + private String generateUniqueKey(BookAdditionalFileEntity file) { + // Additional files inherit library path from their parent book + return generateKey(file.getBook().getLibraryPath().getId(), file.getFileSubPath(), file.getFileName()); + } + + private String generateUniqueKey(LibraryFile file) { + return generateKey(file.getLibraryPathEntity().getId(), file.getFileSubPath(), file.getFileName()); + } + + private String generateKey(Long libraryPathId, String subPath, String fileName) { + String safeSubPath = (subPath == null) ? "" : subPath; + return libraryPathId + ":" + safeSubPath + ":" + fileName; + } + protected List detectDeletedAdditionalFiles(List libraryFiles, LibraryEntity libraryEntity) { Set currentFileNames = libraryFiles.stream() .map(LibraryFile::getFileName) diff --git a/booklore-api/src/test/java/com/adityachandel/booklore/service/library/LibraryProcessingServiceTest.java b/booklore-api/src/test/java/com/adityachandel/booklore/service/library/LibraryProcessingServiceTest.java new file mode 100644 index 000000000..5838cab7d --- /dev/null +++ b/booklore-api/src/test/java/com/adityachandel/booklore/service/library/LibraryProcessingServiceTest.java @@ -0,0 +1,276 @@ +package com.adityachandel.booklore.service.library; + +import com.adityachandel.booklore.model.dto.settings.LibraryFile; +import com.adityachandel.booklore.model.entity.BookAdditionalFileEntity; +import com.adityachandel.booklore.model.entity.BookEntity; +import com.adityachandel.booklore.model.entity.LibraryEntity; +import com.adityachandel.booklore.model.entity.LibraryPathEntity; +import com.adityachandel.booklore.model.enums.LibraryScanMode; +import com.adityachandel.booklore.repository.BookAdditionalFileRepository; +import com.adityachandel.booklore.repository.LibraryRepository; +import com.adityachandel.booklore.service.NotificationService; +import jakarta.persistence.EntityManager; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class LibraryProcessingServiceTest { + + @Mock + private LibraryRepository libraryRepository; + @Mock + private NotificationService notificationService; + @Mock + private BookAdditionalFileRepository bookAdditionalFileRepository; + @Mock + private LibraryFileProcessorRegistry fileProcessorRegistry; + @Mock + private BookRestorationService bookRestorationService; + @Mock + private BookDeletionService bookDeletionService; + @Mock + private LibraryFileHelper libraryFileHelper; + @Mock + private EntityManager entityManager; + @Mock + private LibraryFileProcessor libraryFileProcessor; + + private LibraryProcessingService libraryProcessingService; + + @BeforeEach + void setUp() { + libraryProcessingService = new LibraryProcessingService( + libraryRepository, + notificationService, + bookAdditionalFileRepository, + fileProcessorRegistry, + bookRestorationService, + bookDeletionService, + libraryFileHelper, + entityManager + ); + } + + @Test + void processLibrary_shouldOnlyProcessNewFiles() throws IOException { + long libraryId = 1L; + LibraryEntity libraryEntity = new LibraryEntity(); + libraryEntity.setId(libraryId); + libraryEntity.setName("Test Library"); + libraryEntity.setScanMode(LibraryScanMode.FILE_AS_BOOK); + + LibraryPathEntity pathEntity = new LibraryPathEntity(); + pathEntity.setId(10L); + pathEntity.setPath("/library"); + + BookEntity existingBook = new BookEntity(); + existingBook.setLibraryPath(pathEntity); + existingBook.setFileSubPath(""); + existingBook.setFileName("book1.epub"); + libraryEntity.setBookEntities(List.of(existingBook)); + + when(libraryRepository.findById(libraryId)).thenReturn(Optional.of(libraryEntity)); + when(fileProcessorRegistry.getProcessor(libraryEntity)).thenReturn(libraryFileProcessor); + + // Library files found on disk (1 existing, 1 new) + LibraryFile existingFile = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(pathEntity) + .fileSubPath("") + .fileName("book1.epub") + .build(); + + LibraryFile newFile = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(pathEntity) + .fileSubPath("") + .fileName("book2.epub") + .build(); + + when(libraryFileHelper.getLibraryFiles(libraryEntity, libraryFileProcessor)).thenReturn(List.of(existingFile, newFile)); + when(bookAdditionalFileRepository.findByLibraryId(libraryId)).thenReturn(Collections.emptyList()); + + libraryProcessingService.processLibrary(libraryId); + + ArgumentCaptor> captor = ArgumentCaptor.forClass(List.class); + verify(libraryFileProcessor).processLibraryFiles(captor.capture(), eq(libraryEntity)); + + List processedFiles = captor.getValue(); + + assertThat(processedFiles).hasSize(1); + assertThat(processedFiles.get(0).getFileName()).isEqualTo("book2.epub"); + } + + @Test + void processLibrary_noNewFiles_shouldProcessNothing() throws IOException { + long libraryId = 1L; + LibraryEntity libraryEntity = new LibraryEntity(); + libraryEntity.setId(libraryId); + libraryEntity.setName("Test Library"); + libraryEntity.setScanMode(LibraryScanMode.FILE_AS_BOOK); + + LibraryPathEntity pathEntity = new LibraryPathEntity(); + pathEntity.setId(10L); + pathEntity.setPath("/library"); + + BookEntity existingBook = new BookEntity(); + existingBook.setLibraryPath(pathEntity); + existingBook.setFileSubPath(""); + existingBook.setFileName("book1.epub"); + libraryEntity.setBookEntities(List.of(existingBook)); + + when(libraryRepository.findById(libraryId)).thenReturn(Optional.of(libraryEntity)); + when(fileProcessorRegistry.getProcessor(libraryEntity)).thenReturn(libraryFileProcessor); + + LibraryFile existingFile = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(pathEntity) + .fileSubPath("") + .fileName("book1.epub") + .build(); + + when(libraryFileHelper.getLibraryFiles(libraryEntity, libraryFileProcessor)).thenReturn(List.of(existingFile)); + when(bookAdditionalFileRepository.findByLibraryId(libraryId)).thenReturn(Collections.emptyList()); + + libraryProcessingService.processLibrary(libraryId); + + ArgumentCaptor> captor = ArgumentCaptor.forClass(List.class); + verify(libraryFileProcessor).processLibraryFiles(captor.capture(), eq(libraryEntity)); + + assertThat(captor.getValue()).isEmpty(); + } + + @Test + void processLibrary_allNewFiles_shouldProcessAll() throws IOException { + long libraryId = 1L; + LibraryEntity libraryEntity = new LibraryEntity(); + libraryEntity.setId(libraryId); + libraryEntity.setName("Test Library"); + libraryEntity.setScanMode(LibraryScanMode.FILE_AS_BOOK); + libraryEntity.setBookEntities(Collections.emptyList()); // No existing books + + LibraryPathEntity pathEntity = new LibraryPathEntity(); + pathEntity.setId(10L); + pathEntity.setPath("/library"); + + when(libraryRepository.findById(libraryId)).thenReturn(Optional.of(libraryEntity)); + when(fileProcessorRegistry.getProcessor(libraryEntity)).thenReturn(libraryFileProcessor); + + LibraryFile newFile1 = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(pathEntity) + .fileSubPath("") + .fileName("book1.epub") + .build(); + LibraryFile newFile2 = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(pathEntity) + .fileSubPath("") + .fileName("book2.epub") + .build(); + + when(libraryFileHelper.getLibraryFiles(libraryEntity, libraryFileProcessor)).thenReturn(List.of(newFile1, newFile2)); + when(bookAdditionalFileRepository.findByLibraryId(libraryId)).thenReturn(Collections.emptyList()); + + libraryProcessingService.processLibrary(libraryId); + + ArgumentCaptor> captor = ArgumentCaptor.forClass(List.class); + verify(libraryFileProcessor).processLibraryFiles(captor.capture(), eq(libraryEntity)); + + assertThat(captor.getValue()).hasSize(2); + } + + @Test + void processLibrary_newFileInSubdirectory_shouldProcess() throws IOException { + long libraryId = 1L; + LibraryEntity libraryEntity = new LibraryEntity(); + libraryEntity.setId(libraryId); + libraryEntity.setName("Test Library"); + libraryEntity.setScanMode(LibraryScanMode.FILE_AS_BOOK); + libraryEntity.setBookEntities(Collections.emptyList()); + + LibraryPathEntity pathEntity = new LibraryPathEntity(); + pathEntity.setId(10L); + pathEntity.setPath("/library"); + + when(libraryRepository.findById(libraryId)).thenReturn(Optional.of(libraryEntity)); + when(fileProcessorRegistry.getProcessor(libraryEntity)).thenReturn(libraryFileProcessor); + + LibraryFile newFileInSub = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(pathEntity) + .fileSubPath("fantasy") + .fileName("book1.epub") + .build(); + + when(libraryFileHelper.getLibraryFiles(libraryEntity, libraryFileProcessor)).thenReturn(List.of(newFileInSub)); + when(bookAdditionalFileRepository.findByLibraryId(libraryId)).thenReturn(Collections.emptyList()); + + libraryProcessingService.processLibrary(libraryId); + + ArgumentCaptor> captor = ArgumentCaptor.forClass(List.class); + verify(libraryFileProcessor).processLibraryFiles(captor.capture(), eq(libraryEntity)); + + List processedFiles = captor.getValue(); + assertThat(processedFiles).hasSize(1); + assertThat(processedFiles.get(0).getFileName()).isEqualTo("book1.epub"); + assertThat(processedFiles.get(0).getFileSubPath()).isEqualTo("fantasy"); + } + + @Test + void processLibrary_additionalFile_shouldNotProcess() throws IOException { + long libraryId = 1L; + LibraryEntity libraryEntity = new LibraryEntity(); + libraryEntity.setId(libraryId); + libraryEntity.setName("Test Library"); + libraryEntity.setScanMode(LibraryScanMode.FILE_AS_BOOK); + libraryEntity.setBookEntities(Collections.emptyList()); + + LibraryPathEntity pathEntity = new LibraryPathEntity(); + pathEntity.setId(10L); + pathEntity.setPath("/library"); + + when(libraryRepository.findById(libraryId)).thenReturn(Optional.of(libraryEntity)); + when(fileProcessorRegistry.getProcessor(libraryEntity)).thenReturn(libraryFileProcessor); + + // A file that exists as an additional file (e.g. cover.jpg, or a misidentified book) + LibraryFile additionalFileAsLibraryFile = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(pathEntity) + .fileSubPath("") + .fileName("extra.pdf") + .build(); + + BookEntity parentBook = new BookEntity(); + parentBook.setLibraryPath(pathEntity); + + BookAdditionalFileEntity additionalFileEntity = new BookAdditionalFileEntity(); + additionalFileEntity.setBook(parentBook); // Links to library path + additionalFileEntity.setFileSubPath(""); + additionalFileEntity.setFileName("extra.pdf"); + + when(libraryFileHelper.getLibraryFiles(libraryEntity, libraryFileProcessor)).thenReturn(List.of(additionalFileAsLibraryFile)); + when(bookAdditionalFileRepository.findByLibraryId(libraryId)).thenReturn(List.of(additionalFileEntity)); + + libraryProcessingService.processLibrary(libraryId); + + ArgumentCaptor> captor = ArgumentCaptor.forClass(List.class); + verify(libraryFileProcessor).processLibraryFiles(captor.capture(), eq(libraryEntity)); + + assertThat(captor.getValue()).isEmpty(); + } +}