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)); + } }