|
| 1 | +package com.adityachandel.booklore.service.library; |
| 2 | + |
| 3 | +import com.adityachandel.booklore.model.entity.BookEntity; |
| 4 | +import com.adityachandel.booklore.model.entity.LibraryEntity; |
| 5 | +import com.adityachandel.booklore.model.entity.LibraryPathEntity; |
| 6 | +import com.adityachandel.booklore.model.enums.LibraryScanMode; |
| 7 | +import com.adityachandel.booklore.repository.BookAdditionalFileRepository; |
| 8 | +import com.adityachandel.booklore.repository.LibraryRepository; |
| 9 | +import com.adityachandel.booklore.service.NotificationService; |
| 10 | +import com.adityachandel.booklore.task.options.RescanLibraryContext; |
| 11 | +import jakarta.persistence.EntityManager; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 15 | +import org.junit.jupiter.api.io.TempDir; |
| 16 | +import org.mockito.Mock; |
| 17 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Optional; |
| 25 | + |
| 26 | +import static org.mockito.ArgumentMatchers.any; |
| 27 | +import static org.mockito.Mockito.*; |
| 28 | + |
| 29 | +@ExtendWith(MockitoExtension.class) |
| 30 | +class LibraryProcessingServiceRegressionTest { |
| 31 | + |
| 32 | + @Mock |
| 33 | + private LibraryRepository libraryRepository; |
| 34 | + @Mock |
| 35 | + private NotificationService notificationService; |
| 36 | + @Mock |
| 37 | + private BookAdditionalFileRepository bookAdditionalFileRepository; |
| 38 | + @Mock |
| 39 | + private LibraryFileProcessorRegistry fileProcessorRegistry; |
| 40 | + @Mock |
| 41 | + private BookRestorationService bookRestorationService; |
| 42 | + @Mock |
| 43 | + private BookDeletionService bookDeletionService; |
| 44 | + @Mock |
| 45 | + private LibraryFileHelper libraryFileHelper; |
| 46 | + @Mock |
| 47 | + private EntityManager entityManager; |
| 48 | + @Mock |
| 49 | + private LibraryFileProcessor libraryFileProcessor; |
| 50 | + |
| 51 | + private LibraryProcessingService libraryProcessingService; |
| 52 | + |
| 53 | + @BeforeEach |
| 54 | + void setUp() { |
| 55 | + libraryProcessingService = new LibraryProcessingService( |
| 56 | + libraryRepository, |
| 57 | + notificationService, |
| 58 | + bookAdditionalFileRepository, |
| 59 | + fileProcessorRegistry, |
| 60 | + bookRestorationService, |
| 61 | + bookDeletionService, |
| 62 | + libraryFileHelper, |
| 63 | + entityManager |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void rescanLibrary_shouldThrowException_whenBookHasNoFiles(@TempDir Path tempDir) throws IOException { |
| 69 | + long libraryId = 1L; |
| 70 | + Path accessiblePath = tempDir.resolve("accessible"); |
| 71 | + Files.createDirectory(accessiblePath); |
| 72 | + |
| 73 | + LibraryEntity libraryEntity = new LibraryEntity(); |
| 74 | + libraryEntity.setId(libraryId); |
| 75 | + libraryEntity.setName("Test Library"); |
| 76 | + libraryEntity.setScanMode(LibraryScanMode.FILE_AS_BOOK); |
| 77 | + |
| 78 | + LibraryPathEntity pathEntity = new LibraryPathEntity(); |
| 79 | + pathEntity.setId(10L); |
| 80 | + pathEntity.setPath(accessiblePath.toString()); |
| 81 | + libraryEntity.setLibraryPaths(List.of(pathEntity)); |
| 82 | + |
| 83 | + BookEntity bookWithNoFiles = new BookEntity(); |
| 84 | + bookWithNoFiles.setId(1L); |
| 85 | + bookWithNoFiles.setLibraryPath(pathEntity); |
| 86 | + bookWithNoFiles.setBookFiles(Collections.emptyList()); // Empty files list |
| 87 | + |
| 88 | + libraryEntity.setBookEntities(List.of(bookWithNoFiles)); |
| 89 | + |
| 90 | + when(libraryRepository.findById(libraryId)).thenReturn(Optional.of(libraryEntity)); |
| 91 | + when(fileProcessorRegistry.getProcessor(libraryEntity)).thenReturn(libraryFileProcessor); |
| 92 | + // We need at least one file so it doesn't think the library is offline |
| 93 | + when(libraryFileHelper.getLibraryFiles(libraryEntity, libraryFileProcessor)).thenReturn(List.of( |
| 94 | + com.adityachandel.booklore.model.dto.settings.LibraryFile.builder() |
| 95 | + .libraryPathEntity(pathEntity) |
| 96 | + .fileName("other.epub") |
| 97 | + .fileSubPath("") |
| 98 | + .build() |
| 99 | + )); |
| 100 | + |
| 101 | + RescanLibraryContext context = RescanLibraryContext.builder().libraryId(libraryId).build(); |
| 102 | + |
| 103 | + // Should not throw exception anymore |
| 104 | + libraryProcessingService.rescanLibrary(context); |
| 105 | + |
| 106 | + // Verify that the book with no files (ID 1) was detected as deleted |
| 107 | + verify(bookDeletionService).processDeletedLibraryFiles( |
| 108 | + argThat(list -> list.contains(1L)), |
| 109 | + any() |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
0 commit comments