From 878bc44138315a819b30a789ec8b76d439484558 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: Wed, 28 Jan 2026 00:38:30 +0100 Subject: [PATCH] fix(library-processing): re-fetch library entity after clearing entity manager during rescan (#2507) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(library-processing): re-fetch library entity after clearing entity manager during rescan Ensure the latest library entity state is retrieved after clearing the entity manager in rescanLibrary. Add test to verify fresh entity is used for subsequent processing. Signed-off-by: Balázs Szücs * test(library-processing): update rescan test to verify grouping service usage and remove obsolete assertions Signed-off-by: Balázs Szücs --------- Signed-off-by: Balázs Szücs --- .../library/LibraryProcessingService.java | 3 + .../library/LibraryProcessingServiceTest.java | 63 +++++++++++++++++-- 2 files changed, 61 insertions(+), 5 deletions(-) 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 07d405a2b..9ad8bd443 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 @@ -98,6 +98,9 @@ public class LibraryProcessingService { } bookRestorationService.restoreDeletedBooks(libraryFiles); entityManager.clear(); + // Re-fetch library entity to get fresh state after entity manager was cleared + libraryEntity = libraryRepository.findById(context.getLibraryId()) + .orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(context.getLibraryId())); List newFiles = detectNewBookPaths(libraryFiles, libraryEntity); 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 index ed63da732..3f29c302e 100644 --- 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 @@ -22,11 +22,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -511,4 +507,61 @@ class LibraryProcessingServiceTest { verify(bookDeletionService, never()).processDeletedLibraryFiles(any(), any()); } + + @Test + void rescanLibrary_shouldRefetchLibraryAfterEntityManagerClear(@TempDir Path tempDir) throws IOException { + + long libraryId = 1L; + Path accessiblePath = tempDir.resolve("accessible"); + Files.createDirectory(accessiblePath); + + LibraryPathEntity pathEntity = new LibraryPathEntity(); + pathEntity.setId(10L); + pathEntity.setPath(accessiblePath.toString()); + + BookEntity existingBook = new BookEntity(); + existingBook.setId(1L); + existingBook.setLibraryPath(pathEntity); + BookFileEntity existingBookFile = new BookFileEntity(); + existingBookFile.setBook(existingBook); + existingBook.setBookFiles(new ArrayList<>(List.of(existingBookFile))); + existingBook.getPrimaryBookFile().setFileSubPath(""); + existingBook.getPrimaryBookFile().setFileName("book (with parens).epub"); + + // First call returns entity with one book, second call returns fresh entity with same book + LibraryEntity libraryEntity = new LibraryEntity(); + libraryEntity.setId(libraryId); + libraryEntity.setName("Test Library"); + libraryEntity.setLibraryPaths(new ArrayList<>(List.of(pathEntity))); + libraryEntity.setBookEntities(new ArrayList<>(List.of(existingBook))); + + LibraryEntity freshLibraryEntity = new LibraryEntity(); + freshLibraryEntity.setId(libraryId); + freshLibraryEntity.setName("Test Library"); + freshLibraryEntity.setLibraryPaths(new ArrayList<>(List.of(pathEntity))); + freshLibraryEntity.setBookEntities(new ArrayList<>(List.of(existingBook))); + + LibraryFile fileOnDisk = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(pathEntity) + .fileSubPath("") + .fileName("book (with parens).epub") + .build(); + + when(libraryRepository.findById(libraryId)) + .thenReturn(Optional.of(libraryEntity)) + .thenReturn(Optional.of(freshLibraryEntity)); // Second call returns fresh entity + when(libraryFileHelper.getLibraryFiles(any(LibraryEntity.class))).thenReturn(List.of(fileOnDisk)); + when(bookAdditionalFileRepository.findByLibraryId(libraryId)).thenReturn(Collections.emptyList()); + when(bookGroupingService.groupForRescan(anyList(), any(LibraryEntity.class))) + .thenReturn(new BookGroupingService.GroupingResult(Collections.emptyMap(), Collections.emptyMap())); + + RescanLibraryContext context = RescanLibraryContext.builder().libraryId(libraryId).build(); + + libraryProcessingService.rescanLibrary(context); + + verify(libraryRepository, times(2)).findById(libraryId); + + verify(bookGroupingService).groupForRescan(eq(Collections.emptyList()), any(LibraryEntity.class)); + } }