Fix database lockout caused by duplicate files in library (#1102)
This commit is contained in:
+2
-1
@@ -27,7 +27,8 @@ import java.util.function.Supplier;
|
||||
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.*;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class BookServiceDeleteBooksTest {
|
||||
|
||||
+511
@@ -0,0 +1,511 @@
|
||||
package com.adityachandel.booklore.service.fileprocessor;
|
||||
|
||||
import com.adityachandel.booklore.mapper.BookMapper;
|
||||
import com.adityachandel.booklore.model.DuplicateFileInfo;
|
||||
import com.adityachandel.booklore.model.FileProcessResult;
|
||||
import com.adityachandel.booklore.model.dto.Book;
|
||||
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
||||
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.BookFileType;
|
||||
import com.adityachandel.booklore.model.enums.FileProcessStatus;
|
||||
import com.adityachandel.booklore.repository.BookAdditionalFileRepository;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.service.BookCreatorService;
|
||||
import com.adityachandel.booklore.service.FileFingerprint;
|
||||
import com.adityachandel.booklore.service.metadata.MetadataMatchService;
|
||||
import com.adityachandel.booklore.util.FileService;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.*;
|
||||
|
||||
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.*;
|
||||
|
||||
class AbstractFileProcessorTest {
|
||||
|
||||
@Mock
|
||||
BookRepository bookRepository;
|
||||
@Mock
|
||||
BookAdditionalFileRepository bookAdditionalFileRepository;
|
||||
@Mock
|
||||
BookCreatorService bookCreatorService;
|
||||
@Mock
|
||||
BookMapper bookMapper;
|
||||
@Mock
|
||||
MetadataMatchService metadataMatchService;
|
||||
@Mock
|
||||
FileService fileService;
|
||||
@Mock
|
||||
EntityManager entityManager;
|
||||
|
||||
TestFileProcessor processor;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
processor = new TestFileProcessor(
|
||||
bookRepository,
|
||||
bookAdditionalFileRepository,
|
||||
bookCreatorService,
|
||||
bookMapper,
|
||||
fileService,
|
||||
metadataMatchService
|
||||
);
|
||||
|
||||
// Inject EntityManager via reflection
|
||||
try {
|
||||
var field = AbstractFileProcessor.class.getDeclaredField("entityManager");
|
||||
field.setAccessible(true);
|
||||
field.set(processor, entityManager);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void processFile_shouldReturnDuplicate_whenDuplicateFoundByFileService() {
|
||||
// Given
|
||||
LibraryFile libraryFile = createMockLibraryFile();
|
||||
Book duplicateBook = createMockBook(1L, "file.pdf");
|
||||
|
||||
try (MockedStatic<FileFingerprint> fingerprintMock = mockStatic(FileFingerprint.class)) {
|
||||
fingerprintMock.when(() -> FileFingerprint.generateHash(any())).thenReturn("hash1");
|
||||
|
||||
when(fileService.checkForDuplicateAndUpdateMetadataIfNeeded(
|
||||
eq(libraryFile), eq("hash1"), eq(bookRepository), eq(bookAdditionalFileRepository), eq(bookMapper)))
|
||||
.thenReturn(Optional.of(duplicateBook));
|
||||
|
||||
BookEntity bookEntity = createMockBookEntity(1L, "file.pdf", "hash1", "sub", libraryFile.getLibraryPathEntity());
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(bookEntity));
|
||||
|
||||
// When
|
||||
FileProcessResult result = processor.processFile(libraryFile);
|
||||
|
||||
// Then
|
||||
assertThat(result.getStatus()).isEqualTo(FileProcessStatus.DUPLICATE);
|
||||
assertThat(result.getDuplicate()).isNotNull();
|
||||
assertThat(result.getDuplicate().getBookId()).isEqualTo(1L);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void processFile_shouldReturnDuplicate_whenBookFoundByFileNameAndLibraryId() {
|
||||
// Given
|
||||
LibraryFile libraryFile = createMockLibraryFile();
|
||||
BookEntity existingEntity = createMockBookEntity(2L, "file.pdf", "hash2", "sub", libraryFile.getLibraryPathEntity());
|
||||
Book existingBook = createMockBook(2L, "file.pdf");
|
||||
|
||||
try (MockedStatic<FileFingerprint> fingerprintMock = mockStatic(FileFingerprint.class)) {
|
||||
fingerprintMock.when(() -> FileFingerprint.generateHash(any())).thenReturn("hash2");
|
||||
|
||||
when(fileService.checkForDuplicateAndUpdateMetadataIfNeeded(any(), any(), any(), any(), any()))
|
||||
.thenReturn(Optional.empty());
|
||||
when(bookRepository.findBookByFileNameAndLibraryId("file.pdf", 1L))
|
||||
.thenReturn(Optional.of(existingEntity));
|
||||
when(bookMapper.toBook(existingEntity)).thenReturn(existingBook);
|
||||
|
||||
// When
|
||||
FileProcessResult result = processor.processFile(libraryFile);
|
||||
|
||||
// Then
|
||||
assertThat(result.getStatus()).isEqualTo(FileProcessStatus.DUPLICATE);
|
||||
assertThat(result.getBook()).isEqualTo(existingBook);
|
||||
assertThat(result.getDuplicate()).isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void processFile_shouldReturnNew_whenNoDuplicateFound() {
|
||||
// Given
|
||||
LibraryFile libraryFile = createMockLibraryFile();
|
||||
BookEntity newEntity = createMockBookEntity(3L, "file.pdf", "hash3", "sub", libraryFile.getLibraryPathEntity());
|
||||
Book newBook = createMockBook(3L, "file.pdf");
|
||||
|
||||
try (MockedStatic<FileFingerprint> fingerprintMock = mockStatic(FileFingerprint.class)) {
|
||||
fingerprintMock.when(() -> FileFingerprint.generateHash(any())).thenReturn("hash3");
|
||||
|
||||
when(fileService.checkForDuplicateAndUpdateMetadataIfNeeded(any(), any(), any(), any(), any()))
|
||||
.thenReturn(Optional.empty());
|
||||
when(bookRepository.findBookByFileNameAndLibraryId("file.pdf", 1L))
|
||||
.thenReturn(Optional.empty());
|
||||
when(metadataMatchService.calculateMatchScore(any())).thenReturn(85F);
|
||||
when(bookMapper.toBook(newEntity)).thenReturn(newBook);
|
||||
|
||||
processor.setProcessNewFileResult(newEntity);
|
||||
|
||||
// When
|
||||
FileProcessResult result = processor.processFile(libraryFile);
|
||||
|
||||
// Then
|
||||
assertThat(result.getStatus()).isEqualTo(FileProcessStatus.NEW);
|
||||
assertThat(result.getBook()).isEqualTo(newBook);
|
||||
assertThat(result.getDuplicate()).isNull();
|
||||
verify(bookCreatorService).saveConnections(newEntity);
|
||||
verify(metadataMatchService).calculateMatchScore(newEntity);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void processFile_shouldReturnUpdated_whenDuplicateFoundWithDifferentMetadata() {
|
||||
// Given
|
||||
LibraryFile libraryFile = createMockLibraryFile();
|
||||
libraryFile.setFileSubPath("new-sub");
|
||||
|
||||
Book duplicateBook = createMockBook(1L, "file.pdf");
|
||||
BookEntity existingEntity = createMockBookEntity(1L, "file.pdf", "hash1", "old-sub", libraryFile.getLibraryPathEntity());
|
||||
Book updatedBook = createMockBook(1L, "file.pdf");
|
||||
|
||||
try (MockedStatic<FileFingerprint> fingerprintMock = mockStatic(FileFingerprint.class)) {
|
||||
fingerprintMock.when(() -> FileFingerprint.generateHash(any())).thenReturn("hash1");
|
||||
|
||||
when(fileService.checkForDuplicateAndUpdateMetadataIfNeeded(any(), any(), any(), any(), any()))
|
||||
.thenReturn(Optional.of(duplicateBook));
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(existingEntity));
|
||||
when(bookMapper.toBook(existingEntity)).thenReturn(updatedBook);
|
||||
|
||||
// When
|
||||
FileProcessResult result = processor.processFile(libraryFile);
|
||||
|
||||
// Then
|
||||
assertThat(result.getStatus()).isEqualTo(FileProcessStatus.UPDATED);
|
||||
assertThat(result.getBook()).isEqualTo(updatedBook);
|
||||
assertThat(existingEntity.getFileSubPath()).isEqualTo("new-sub");
|
||||
verify(entityManager).flush();
|
||||
verify(entityManager).detach(existingEntity);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void processFile_shouldReturnUpdated_whenDuplicateFoundWithDifferentFileName() {
|
||||
// Given
|
||||
LibraryFile libraryFile = createMockLibraryFile();
|
||||
libraryFile.setFileName("new-file.pdf");
|
||||
|
||||
Book duplicateBook = createMockBook(1L, "old-file.pdf");
|
||||
BookEntity existingEntity = createMockBookEntity(1L, "old-file.pdf", "hash1", "sub", libraryFile.getLibraryPathEntity());
|
||||
Book updatedBook = createMockBook(1L, "new-file.pdf");
|
||||
|
||||
try (MockedStatic<FileFingerprint> fingerprintMock = mockStatic(FileFingerprint.class)) {
|
||||
fingerprintMock.when(() -> FileFingerprint.generateHash(any())).thenReturn("hash1");
|
||||
|
||||
when(fileService.checkForDuplicateAndUpdateMetadataIfNeeded(any(), any(), any(), any(), any()))
|
||||
.thenReturn(Optional.of(duplicateBook));
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(existingEntity));
|
||||
when(bookMapper.toBook(existingEntity)).thenReturn(updatedBook);
|
||||
|
||||
// When
|
||||
FileProcessResult result = processor.processFile(libraryFile);
|
||||
|
||||
// Then
|
||||
assertThat(result.getStatus()).isEqualTo(FileProcessStatus.UPDATED);
|
||||
assertThat(result.getBook()).isEqualTo(updatedBook);
|
||||
assertThat(existingEntity.getFileName()).isEqualTo("new-file.pdf");
|
||||
verify(entityManager).flush();
|
||||
verify(entityManager).detach(existingEntity);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void processFile_shouldReturnUpdated_whenDuplicateFoundWithDifferentLibraryPath() {
|
||||
// Given
|
||||
LibraryEntity library = LibraryEntity.builder().id(2L).build();
|
||||
LibraryPathEntity newLibraryPath = LibraryPathEntity.builder()
|
||||
.id(2L)
|
||||
.library(library)
|
||||
.path("/new-path")
|
||||
.build();
|
||||
|
||||
LibraryFile libraryFile = LibraryFile.builder()
|
||||
.fileName("file.pdf")
|
||||
.fileSubPath("sub")
|
||||
.bookFileType(BookFileType.PDF)
|
||||
.libraryEntity(library)
|
||||
.libraryPathEntity(newLibraryPath)
|
||||
.build();
|
||||
|
||||
Book duplicateBook = createMockBook(1L, "file.pdf");
|
||||
BookEntity existingEntity = createMockBookEntity(1L, "file.pdf", "hash1", "sub",
|
||||
LibraryPathEntity.builder().id(1L).library(LibraryEntity.builder().id(1L).build()).path("/old-path").build());
|
||||
Book updatedBook = createMockBook(1L, "file.pdf");
|
||||
|
||||
try (MockedStatic<FileFingerprint> fingerprintMock = mockStatic(FileFingerprint.class)) {
|
||||
fingerprintMock.when(() -> FileFingerprint.generateHash(any())).thenReturn("hash1");
|
||||
|
||||
when(fileService.checkForDuplicateAndUpdateMetadataIfNeeded(any(), any(), any(), any(), any()))
|
||||
.thenReturn(Optional.of(duplicateBook));
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(existingEntity));
|
||||
when(bookMapper.toBook(existingEntity)).thenReturn(updatedBook);
|
||||
|
||||
// When
|
||||
FileProcessResult result = processor.processFile(libraryFile);
|
||||
|
||||
// Then
|
||||
assertThat(result.getStatus()).isEqualTo(FileProcessStatus.UPDATED);
|
||||
assertThat(result.getBook()).isEqualTo(updatedBook);
|
||||
assertThat(existingEntity.getLibraryPath()).isEqualTo(newLibraryPath);
|
||||
verify(entityManager).flush();
|
||||
verify(entityManager).detach(existingEntity);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void processFile_shouldReturnDuplicate_whenDuplicateFoundWithSameMetadata() {
|
||||
// Given
|
||||
LibraryFile libraryFile = createMockLibraryFile();
|
||||
Book duplicateBook = createMockBook(1L, "file.pdf");
|
||||
BookEntity existingEntity = createMockBookEntity(1L, "file.pdf", "hash1", "sub", libraryFile.getLibraryPathEntity());
|
||||
|
||||
try (MockedStatic<FileFingerprint> fingerprintMock = mockStatic(FileFingerprint.class)) {
|
||||
fingerprintMock.when(() -> FileFingerprint.generateHash(any())).thenReturn("hash1");
|
||||
|
||||
when(fileService.checkForDuplicateAndUpdateMetadataIfNeeded(any(), any(), any(), any(), any()))
|
||||
.thenReturn(Optional.of(duplicateBook));
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(existingEntity));
|
||||
|
||||
// When
|
||||
FileProcessResult result = processor.processFile(libraryFile);
|
||||
|
||||
// Then
|
||||
assertThat(result.getStatus()).isEqualTo(FileProcessStatus.DUPLICATE);
|
||||
assertThat(result.getBook()).isEqualTo(duplicateBook);
|
||||
assertThat(result.getDuplicate()).isNotNull();
|
||||
assertThat(result.getDuplicate().getBookId()).isEqualTo(1L);
|
||||
verify(entityManager, never()).flush();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void processFile_shouldReturnDuplicateFromFallback_whenDuplicateBookNotFoundInRepository() {
|
||||
// Given
|
||||
LibraryFile libraryFile = createMockLibraryFile();
|
||||
Book duplicateBook = createMockBook(1L, "file.pdf");
|
||||
|
||||
try (MockedStatic<FileFingerprint> fingerprintMock = mockStatic(FileFingerprint.class)) {
|
||||
fingerprintMock.when(() -> FileFingerprint.generateHash(any())).thenReturn("hash1");
|
||||
|
||||
when(fileService.checkForDuplicateAndUpdateMetadataIfNeeded(any(), any(), any(), any(), any()))
|
||||
.thenReturn(Optional.of(duplicateBook));
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.empty());
|
||||
|
||||
// When
|
||||
FileProcessResult result = processor.processFile(libraryFile);
|
||||
|
||||
// Then
|
||||
assertThat(result.getStatus()).isEqualTo(FileProcessStatus.DUPLICATE);
|
||||
assertThat(result.getBook()).isEqualTo(duplicateBook);
|
||||
assertThat(result.getDuplicate()).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void processFile_shouldUpdateHashEvenWhenOtherMetadataUnchanged() {
|
||||
// Given
|
||||
LibraryFile libraryFile = createMockLibraryFile();
|
||||
Book duplicateBook = createMockBook(1L, "file.pdf");
|
||||
BookEntity existingEntity = createMockBookEntity(1L, "file.pdf", "old-hash", "sub", libraryFile.getLibraryPathEntity());
|
||||
|
||||
try (MockedStatic<FileFingerprint> fingerprintMock = mockStatic(FileFingerprint.class)) {
|
||||
fingerprintMock.when(() -> FileFingerprint.generateHash(any())).thenReturn("new-hash");
|
||||
|
||||
when(fileService.checkForDuplicateAndUpdateMetadataIfNeeded(any(), any(), any(), any(), any()))
|
||||
.thenReturn(Optional.of(duplicateBook));
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(existingEntity));
|
||||
when(bookMapper.toBook(existingEntity)).thenReturn(duplicateBook);
|
||||
|
||||
// When
|
||||
FileProcessResult result = processor.processFile(libraryFile);
|
||||
|
||||
// Then
|
||||
assertThat(existingEntity.getCurrentHash()).isEqualTo("new-hash");
|
||||
verify(entityManager).detach(existingEntity);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void processFile_shouldHandleNullFileSubPath() {
|
||||
// Given
|
||||
LibraryEntity library = LibraryEntity.builder().id(1L).build();
|
||||
LibraryPathEntity libraryPath = LibraryPathEntity.builder()
|
||||
.id(1L)
|
||||
.library(library)
|
||||
.path("/tmp")
|
||||
.build();
|
||||
|
||||
LibraryFile libraryFile = LibraryFile.builder()
|
||||
.fileName("file.pdf")
|
||||
.fileSubPath("") // Use empty string instead of null to avoid NPE in Paths.get
|
||||
.bookFileType(BookFileType.PDF)
|
||||
.libraryEntity(library)
|
||||
.libraryPathEntity(libraryPath)
|
||||
.build();
|
||||
|
||||
BookEntity newEntity = createMockBookEntity(3L, "file.pdf", "hash3", "", libraryFile.getLibraryPathEntity());
|
||||
Book newBook = createMockBook(3L, "file.pdf");
|
||||
|
||||
try (MockedStatic<FileFingerprint> fingerprintMock = mockStatic(FileFingerprint.class)) {
|
||||
fingerprintMock.when(() -> FileFingerprint.generateHash(any())).thenReturn("hash3");
|
||||
|
||||
when(fileService.checkForDuplicateAndUpdateMetadataIfNeeded(any(), any(), any(), any(), any()))
|
||||
.thenReturn(Optional.empty());
|
||||
when(bookRepository.findBookByFileNameAndLibraryId("file.pdf", 1L))
|
||||
.thenReturn(Optional.empty());
|
||||
when(metadataMatchService.calculateMatchScore(any())).thenReturn(90F);
|
||||
when(bookMapper.toBook(newEntity)).thenReturn(newBook);
|
||||
|
||||
processor.setProcessNewFileResult(newEntity);
|
||||
|
||||
// When
|
||||
FileProcessResult result = processor.processFile(libraryFile);
|
||||
|
||||
// Then
|
||||
assertThat(result.getStatus()).isEqualTo(FileProcessStatus.NEW);
|
||||
assertThat(result.getBook()).isEqualTo(newBook);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void processFile_shouldHandleMultipleMetadataChangesSimultaneously() {
|
||||
// Given
|
||||
LibraryEntity newLibrary = LibraryEntity.builder().id(2L).build();
|
||||
LibraryPathEntity newLibraryPath = LibraryPathEntity.builder()
|
||||
.id(2L)
|
||||
.library(newLibrary)
|
||||
.path("/new-path")
|
||||
.build();
|
||||
|
||||
LibraryFile libraryFile = LibraryFile.builder()
|
||||
.fileName("new-file.pdf")
|
||||
.fileSubPath("new-sub")
|
||||
.bookFileType(BookFileType.PDF)
|
||||
.libraryEntity(newLibrary)
|
||||
.libraryPathEntity(newLibraryPath)
|
||||
.build();
|
||||
|
||||
Book duplicateBook = createMockBook(1L, "old-file.pdf");
|
||||
BookEntity existingEntity = createMockBookEntity(1L, "old-file.pdf", "hash1", "old-sub",
|
||||
LibraryPathEntity.builder().id(1L).library(LibraryEntity.builder().id(1L).build()).build());
|
||||
Book updatedBook = createMockBook(1L, "new-file.pdf");
|
||||
|
||||
try (MockedStatic<FileFingerprint> fingerprintMock = mockStatic(FileFingerprint.class)) {
|
||||
fingerprintMock.when(() -> FileFingerprint.generateHash(any())).thenReturn("hash1");
|
||||
|
||||
when(fileService.checkForDuplicateAndUpdateMetadataIfNeeded(any(), any(), any(), any(), any()))
|
||||
.thenReturn(Optional.of(duplicateBook));
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(existingEntity));
|
||||
when(bookMapper.toBook(existingEntity)).thenReturn(updatedBook);
|
||||
|
||||
// When
|
||||
FileProcessResult result = processor.processFile(libraryFile);
|
||||
|
||||
// Then
|
||||
assertThat(result.getStatus()).isEqualTo(FileProcessStatus.UPDATED);
|
||||
assertThat(existingEntity.getFileName()).isEqualTo("new-file.pdf");
|
||||
assertThat(existingEntity.getFileSubPath()).isEqualTo("new-sub");
|
||||
assertThat(existingEntity.getLibraryPath()).isEqualTo(newLibraryPath);
|
||||
verify(entityManager).flush();
|
||||
verify(entityManager).detach(existingEntity);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDuplicateInfo_shouldCreateCorrectDuplicateInfo() {
|
||||
// Given
|
||||
LibraryFile libraryFile = createMockLibraryFile();
|
||||
Book book = createMockBook(1L, "file.pdf");
|
||||
|
||||
try (MockedStatic<FileFingerprint> fingerprintMock = mockStatic(FileFingerprint.class)) {
|
||||
fingerprintMock.when(() -> FileFingerprint.generateHash(any())).thenReturn("hash1");
|
||||
|
||||
when(fileService.checkForDuplicateAndUpdateMetadataIfNeeded(any(), any(), any(), any(), any()))
|
||||
.thenReturn(Optional.of(book));
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(
|
||||
createMockBookEntity(1L, "file.pdf", "hash1", "sub", libraryFile.getLibraryPathEntity())));
|
||||
|
||||
// When
|
||||
FileProcessResult result = processor.processFile(libraryFile);
|
||||
|
||||
// Then
|
||||
DuplicateFileInfo duplicateInfo = result.getDuplicate();
|
||||
assertThat(duplicateInfo).isNotNull();
|
||||
assertThat(duplicateInfo.getBookId()).isEqualTo(1L);
|
||||
assertThat(duplicateInfo.getFileName()).isEqualTo("file.pdf");
|
||||
assertThat(duplicateInfo.getFullPath()).contains("/tmp", "sub", "file.pdf");
|
||||
}
|
||||
}
|
||||
|
||||
// Helper methods
|
||||
private LibraryFile createMockLibraryFile() {
|
||||
LibraryEntity library = LibraryEntity.builder().id(1L).build();
|
||||
LibraryPathEntity libraryPath = LibraryPathEntity.builder()
|
||||
.id(1L)
|
||||
.library(library)
|
||||
.path("/tmp")
|
||||
.build();
|
||||
|
||||
return LibraryFile.builder()
|
||||
.fileName("file.pdf")
|
||||
.fileSubPath("sub")
|
||||
.bookFileType(BookFileType.PDF)
|
||||
.libraryEntity(library)
|
||||
.libraryPathEntity(libraryPath)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Book createMockBook(Long id, String fileName) {
|
||||
return Book.builder()
|
||||
.id(id)
|
||||
.fileName(fileName)
|
||||
.fileSubPath("sub")
|
||||
.build();
|
||||
}
|
||||
|
||||
private BookEntity createMockBookEntity(Long id, String fileName, String hash, String subPath, LibraryPathEntity libraryPath) {
|
||||
return BookEntity.builder()
|
||||
.id(id)
|
||||
.fileName(fileName)
|
||||
.currentHash(hash)
|
||||
.fileSubPath(subPath)
|
||||
.libraryPath(libraryPath)
|
||||
.build();
|
||||
}
|
||||
|
||||
// Test implementation of AbstractFileProcessor
|
||||
static class TestFileProcessor extends AbstractFileProcessor {
|
||||
private BookEntity processNewFileResult;
|
||||
|
||||
public TestFileProcessor(BookRepository bookRepository,
|
||||
BookAdditionalFileRepository bookAdditionalFileRepository,
|
||||
BookCreatorService bookCreatorService,
|
||||
BookMapper bookMapper,
|
||||
FileService fileService,
|
||||
MetadataMatchService metadataMatchService) {
|
||||
super(bookRepository, bookAdditionalFileRepository, bookCreatorService, bookMapper, fileService, metadataMatchService);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BookEntity processNewFile(LibraryFile libraryFile) {
|
||||
return processNewFileResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BookFileType> getSupportedTypes() {
|
||||
return List.of(BookFileType.PDF);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean generateCover(BookEntity bookEntity) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setProcessNewFileResult(BookEntity entity) {
|
||||
this.processNewFileResult = entity;
|
||||
}
|
||||
}
|
||||
}
|
||||
+70
-58
@@ -1,6 +1,8 @@
|
||||
package com.adityachandel.booklore.service.library;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.Book;
|
||||
import com.adityachandel.booklore.model.FileProcessResult;
|
||||
import com.adityachandel.booklore.model.enums.FileProcessStatus;
|
||||
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
||||
import com.adityachandel.booklore.model.entity.LibraryEntity;
|
||||
import com.adityachandel.booklore.model.entity.LibraryPathEntity;
|
||||
@@ -58,7 +60,7 @@ class FileAsBookProcessorTest {
|
||||
LibraryPathEntity libraryPathEntity = new LibraryPathEntity();
|
||||
libraryPathEntity.setPath("/library/path");
|
||||
List<LibraryFile> libraryFiles = new ArrayList<>();
|
||||
|
||||
|
||||
LibraryFile file1 = LibraryFile.builder()
|
||||
.libraryEntity(libraryEntity)
|
||||
.libraryPathEntity(libraryPathEntity)
|
||||
@@ -66,7 +68,7 @@ class FileAsBookProcessorTest {
|
||||
.fileSubPath("books")
|
||||
.bookFileType(BookFileType.EPUB)
|
||||
.build();
|
||||
|
||||
|
||||
LibraryFile file2 = LibraryFile.builder()
|
||||
.libraryEntity(libraryEntity)
|
||||
.libraryPathEntity(libraryPathEntity)
|
||||
@@ -74,30 +76,32 @@ class FileAsBookProcessorTest {
|
||||
.fileSubPath("books")
|
||||
.bookFileType(BookFileType.PDF)
|
||||
.build();
|
||||
|
||||
|
||||
libraryFiles.add(file1);
|
||||
libraryFiles.add(file2);
|
||||
|
||||
|
||||
Book book1 = Book.builder()
|
||||
.fileName("book1.epub")
|
||||
.title("Book 1")
|
||||
.bookType(BookFileType.EPUB)
|
||||
.build();
|
||||
|
||||
|
||||
Book book2 = Book.builder()
|
||||
.fileName("book2.pdf")
|
||||
.title("Book 2")
|
||||
.bookType(BookFileType.PDF)
|
||||
.build();
|
||||
|
||||
|
||||
when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(bookFileProcessor);
|
||||
when(processorRegistry.getProcessorOrThrow(BookFileType.PDF)).thenReturn(bookFileProcessor);
|
||||
when(bookFileProcessor.processFile(file1)).thenReturn(book1);
|
||||
when(bookFileProcessor.processFile(file2)).thenReturn(book2);
|
||||
|
||||
when(bookFileProcessor.processFile(file1))
|
||||
.thenReturn(new FileProcessResult(book1, FileProcessStatus.NEW, null));
|
||||
when(bookFileProcessor.processFile(file2))
|
||||
.thenReturn(new FileProcessResult(book2, FileProcessStatus.NEW, null));
|
||||
|
||||
// When
|
||||
fileAsBookProcessor.processLibraryFiles(libraryFiles, libraryEntity);
|
||||
|
||||
|
||||
// Then
|
||||
verify(bookEventBroadcaster, times(2)).broadcastBookAddEvent(bookCaptor.capture());
|
||||
|
||||
@@ -113,7 +117,7 @@ class FileAsBookProcessorTest {
|
||||
LibraryPathEntity libraryPathEntity = new LibraryPathEntity();
|
||||
libraryPathEntity.setPath("/library/path");
|
||||
List<LibraryFile> libraryFiles = new ArrayList<>();
|
||||
|
||||
|
||||
LibraryFile validFile = LibraryFile.builder()
|
||||
.libraryEntity(libraryEntity)
|
||||
.libraryPathEntity(libraryPathEntity)
|
||||
@@ -121,7 +125,7 @@ class FileAsBookProcessorTest {
|
||||
.fileSubPath("books")
|
||||
.bookFileType(BookFileType.EPUB)
|
||||
.build();
|
||||
|
||||
|
||||
LibraryFile invalidFile = LibraryFile.builder()
|
||||
.libraryEntity(libraryEntity)
|
||||
.libraryPathEntity(libraryPathEntity)
|
||||
@@ -129,22 +133,23 @@ class FileAsBookProcessorTest {
|
||||
.fileSubPath("docs")
|
||||
.bookFileType(null)
|
||||
.build();
|
||||
|
||||
|
||||
libraryFiles.add(validFile);
|
||||
libraryFiles.add(invalidFile);
|
||||
|
||||
|
||||
Book book = Book.builder()
|
||||
.fileName("book.epub")
|
||||
.title("Valid Book")
|
||||
.bookType(BookFileType.EPUB)
|
||||
.build();
|
||||
|
||||
|
||||
when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(bookFileProcessor);
|
||||
when(bookFileProcessor.processFile(validFile)).thenReturn(book);
|
||||
|
||||
when(bookFileProcessor.processFile(validFile))
|
||||
.thenReturn(new FileProcessResult(book, FileProcessStatus.NEW, null));
|
||||
|
||||
// When
|
||||
fileAsBookProcessor.processLibraryFiles(libraryFiles, libraryEntity);
|
||||
|
||||
|
||||
// Then
|
||||
verify(bookEventBroadcaster, times(1)).broadcastBookAddEvent(book);
|
||||
verify(processorRegistry, times(1)).getProcessorOrThrow(any());
|
||||
@@ -155,10 +160,10 @@ class FileAsBookProcessorTest {
|
||||
// Given
|
||||
LibraryEntity libraryEntity = new LibraryEntity();
|
||||
List<LibraryFile> libraryFiles = new ArrayList<>();
|
||||
|
||||
|
||||
// When
|
||||
fileAsBookProcessor.processLibraryFiles(libraryFiles, libraryEntity);
|
||||
|
||||
|
||||
// Then
|
||||
verify(bookEventBroadcaster, never()).broadcastBookAddEvent(any());
|
||||
verify(processorRegistry, never()).getProcessorOrThrow(any());
|
||||
@@ -170,7 +175,7 @@ class FileAsBookProcessorTest {
|
||||
LibraryEntity libraryEntity = new LibraryEntity();
|
||||
LibraryPathEntity libraryPathEntity = new LibraryPathEntity();
|
||||
libraryPathEntity.setPath("/library/path");
|
||||
|
||||
|
||||
LibraryFile libraryFile = LibraryFile.builder()
|
||||
.libraryEntity(libraryEntity)
|
||||
.libraryPathEntity(libraryPathEntity)
|
||||
@@ -178,10 +183,10 @@ class FileAsBookProcessorTest {
|
||||
.fileSubPath("docs")
|
||||
.bookFileType(null)
|
||||
.build();
|
||||
|
||||
|
||||
// When
|
||||
Book result = fileAsBookProcessor.processLibraryFile(libraryFile);
|
||||
|
||||
FileProcessResult result = fileAsBookProcessor.processLibraryFile(libraryFile);
|
||||
|
||||
// Then
|
||||
assertThat(result).isNull();
|
||||
verify(processorRegistry, never()).getProcessorOrThrow(any());
|
||||
@@ -193,7 +198,7 @@ class FileAsBookProcessorTest {
|
||||
LibraryEntity libraryEntity = new LibraryEntity();
|
||||
LibraryPathEntity libraryPathEntity = new LibraryPathEntity();
|
||||
libraryPathEntity.setPath("/library/path");
|
||||
|
||||
|
||||
LibraryFile libraryFile = LibraryFile.builder()
|
||||
.libraryEntity(libraryEntity)
|
||||
.libraryPathEntity(libraryPathEntity)
|
||||
@@ -201,21 +206,24 @@ class FileAsBookProcessorTest {
|
||||
.fileSubPath("books")
|
||||
.bookFileType(BookFileType.EPUB)
|
||||
.build();
|
||||
|
||||
|
||||
Book expectedBook = Book.builder()
|
||||
.fileName("book.epub")
|
||||
.title("Test Book")
|
||||
.bookType(BookFileType.EPUB)
|
||||
.build();
|
||||
|
||||
|
||||
when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(bookFileProcessor);
|
||||
when(bookFileProcessor.processFile(libraryFile)).thenReturn(expectedBook);
|
||||
|
||||
when(bookFileProcessor.processFile(libraryFile))
|
||||
.thenReturn(new FileProcessResult(expectedBook, FileProcessStatus.NEW, null));
|
||||
|
||||
// When
|
||||
Book result = fileAsBookProcessor.processLibraryFile(libraryFile);
|
||||
|
||||
FileProcessResult result = fileAsBookProcessor.processLibraryFile(libraryFile);
|
||||
|
||||
// Then
|
||||
assertThat(result).isEqualTo(expectedBook);
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.getBook()).isEqualTo(expectedBook);
|
||||
|
||||
verify(processorRegistry).getProcessorOrThrow(BookFileType.EPUB);
|
||||
verify(bookFileProcessor).processFile(libraryFile);
|
||||
}
|
||||
@@ -226,7 +234,7 @@ class FileAsBookProcessorTest {
|
||||
LibraryEntity libraryEntity = new LibraryEntity();
|
||||
LibraryPathEntity libraryPathEntity = new LibraryPathEntity();
|
||||
libraryPathEntity.setPath("/library/path");
|
||||
|
||||
|
||||
LibraryFile libraryFile = LibraryFile.builder()
|
||||
.libraryEntity(libraryEntity)
|
||||
.libraryPathEntity(libraryPathEntity)
|
||||
@@ -234,13 +242,13 @@ class FileAsBookProcessorTest {
|
||||
.fileSubPath("books")
|
||||
.bookFileType(BookFileType.PDF)
|
||||
.build();
|
||||
|
||||
|
||||
when(processorRegistry.getProcessorOrThrow(BookFileType.PDF)).thenReturn(bookFileProcessor);
|
||||
when(bookFileProcessor.processFile(libraryFile)).thenReturn(null);
|
||||
|
||||
|
||||
// When
|
||||
Book result = fileAsBookProcessor.processLibraryFile(libraryFile);
|
||||
|
||||
FileProcessResult result = fileAsBookProcessor.processLibraryFile(libraryFile);
|
||||
|
||||
// Then
|
||||
assertThat(result).isNull();
|
||||
}
|
||||
@@ -252,7 +260,7 @@ class FileAsBookProcessorTest {
|
||||
LibraryPathEntity libraryPathEntity = new LibraryPathEntity();
|
||||
libraryPathEntity.setPath("/library/path");
|
||||
List<LibraryFile> libraryFiles = new ArrayList<>();
|
||||
|
||||
|
||||
LibraryFile file = LibraryFile.builder()
|
||||
.libraryEntity(libraryEntity)
|
||||
.libraryPathEntity(libraryPathEntity)
|
||||
@@ -260,15 +268,15 @@ class FileAsBookProcessorTest {
|
||||
.fileSubPath("books")
|
||||
.bookFileType(BookFileType.EPUB)
|
||||
.build();
|
||||
|
||||
|
||||
libraryFiles.add(file);
|
||||
|
||||
|
||||
when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(bookFileProcessor);
|
||||
when(bookFileProcessor.processFile(file)).thenReturn(null);
|
||||
|
||||
|
||||
// When
|
||||
fileAsBookProcessor.processLibraryFiles(libraryFiles, libraryEntity);
|
||||
|
||||
|
||||
// Then
|
||||
verify(bookEventBroadcaster, never()).broadcastBookAddEvent(any());
|
||||
}
|
||||
@@ -280,7 +288,7 @@ class FileAsBookProcessorTest {
|
||||
LibraryPathEntity libraryPathEntity = new LibraryPathEntity();
|
||||
libraryPathEntity.setPath("/library/path");
|
||||
List<LibraryFile> libraryFiles = new ArrayList<>();
|
||||
|
||||
|
||||
LibraryFile epubFile = LibraryFile.builder()
|
||||
.libraryEntity(libraryEntity)
|
||||
.libraryPathEntity(libraryPathEntity)
|
||||
@@ -288,7 +296,7 @@ class FileAsBookProcessorTest {
|
||||
.fileSubPath("books")
|
||||
.bookFileType(BookFileType.EPUB)
|
||||
.build();
|
||||
|
||||
|
||||
LibraryFile pdfFile = LibraryFile.builder()
|
||||
.libraryEntity(libraryEntity)
|
||||
.libraryPathEntity(libraryPathEntity)
|
||||
@@ -296,7 +304,7 @@ class FileAsBookProcessorTest {
|
||||
.fileSubPath("books")
|
||||
.bookFileType(BookFileType.PDF)
|
||||
.build();
|
||||
|
||||
|
||||
LibraryFile cbzFile = LibraryFile.builder()
|
||||
.libraryEntity(libraryEntity)
|
||||
.libraryPathEntity(libraryPathEntity)
|
||||
@@ -304,7 +312,7 @@ class FileAsBookProcessorTest {
|
||||
.fileSubPath("comics")
|
||||
.bookFileType(BookFileType.CBX)
|
||||
.build();
|
||||
|
||||
|
||||
LibraryFile cbrFile = LibraryFile.builder()
|
||||
.libraryEntity(libraryEntity)
|
||||
.libraryPathEntity(libraryPathEntity)
|
||||
@@ -312,44 +320,48 @@ class FileAsBookProcessorTest {
|
||||
.fileSubPath("comics")
|
||||
.bookFileType(BookFileType.CBX)
|
||||
.build();
|
||||
|
||||
|
||||
libraryFiles.add(epubFile);
|
||||
libraryFiles.add(pdfFile);
|
||||
libraryFiles.add(cbzFile);
|
||||
libraryFiles.add(cbrFile);
|
||||
|
||||
|
||||
Book epubBook = Book.builder()
|
||||
.fileName("book.epub")
|
||||
.bookType(BookFileType.EPUB)
|
||||
.build();
|
||||
|
||||
|
||||
Book pdfBook = Book.builder()
|
||||
.fileName("book.pdf")
|
||||
.bookType(BookFileType.PDF)
|
||||
.build();
|
||||
|
||||
|
||||
Book cbzBook = Book.builder()
|
||||
.fileName("comic.cbz")
|
||||
.bookType(BookFileType.CBX)
|
||||
.build();
|
||||
|
||||
|
||||
Book cbrBook = Book.builder()
|
||||
.fileName("comic.cbr")
|
||||
.bookType(BookFileType.CBX)
|
||||
.build();
|
||||
|
||||
|
||||
when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(bookFileProcessor);
|
||||
when(processorRegistry.getProcessorOrThrow(BookFileType.PDF)).thenReturn(bookFileProcessor);
|
||||
when(processorRegistry.getProcessorOrThrow(BookFileType.CBX)).thenReturn(bookFileProcessor);
|
||||
|
||||
when(bookFileProcessor.processFile(epubFile)).thenReturn(epubBook);
|
||||
when(bookFileProcessor.processFile(pdfFile)).thenReturn(pdfBook);
|
||||
when(bookFileProcessor.processFile(cbzFile)).thenReturn(cbzBook);
|
||||
when(bookFileProcessor.processFile(cbrFile)).thenReturn(cbrBook);
|
||||
|
||||
when(bookFileProcessor.processFile(epubFile))
|
||||
.thenReturn(new FileProcessResult(epubBook, FileProcessStatus.NEW, null));
|
||||
when(bookFileProcessor.processFile(pdfFile))
|
||||
.thenReturn(new FileProcessResult(pdfBook, FileProcessStatus.NEW, null));
|
||||
when(bookFileProcessor.processFile(cbzFile))
|
||||
.thenReturn(new FileProcessResult(cbzBook, FileProcessStatus.NEW, null));
|
||||
when(bookFileProcessor.processFile(cbrFile))
|
||||
.thenReturn(new FileProcessResult(cbrBook, FileProcessStatus.NEW, null));
|
||||
|
||||
// When
|
||||
fileAsBookProcessor.processLibraryFiles(libraryFiles, libraryEntity);
|
||||
|
||||
|
||||
// Then
|
||||
verify(bookEventBroadcaster, times(4)).broadcastBookAddEvent(any(Book.class));
|
||||
}
|
||||
|
||||
+5
-3
@@ -1,10 +1,12 @@
|
||||
package com.adityachandel.booklore.service.library;
|
||||
|
||||
import com.adityachandel.booklore.model.FileProcessResult;
|
||||
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.FileProcessStatus;
|
||||
import com.adityachandel.booklore.model.enums.LibraryScanMode;
|
||||
import com.adityachandel.booklore.service.event.BookEventBroadcaster;
|
||||
import com.adityachandel.booklore.service.event.AdminEventBroadcaster;
|
||||
@@ -118,7 +120,7 @@ class FolderAsBookFileProcessorTest {
|
||||
when(bookFileProcessorRegistry.getProcessorOrThrow(BookFileType.PDF))
|
||||
.thenReturn(mockBookFileProcessor);
|
||||
when(mockBookFileProcessor.processFile(any(LibraryFile.class)))
|
||||
.thenReturn(createdBook);
|
||||
.thenReturn(new FileProcessResult(createdBook, FileProcessStatus.NEW, null));
|
||||
when(bookRepository.getReferenceById(createdBook.getId()))
|
||||
.thenReturn(bookEntity);
|
||||
when(bookAdditionalFileRepository.findByLibraryPath_IdAndFileSubPathAndFileName(anyLong(), anyString(), anyString()))
|
||||
@@ -230,7 +232,7 @@ class FolderAsBookFileProcessorTest {
|
||||
when(bookFileProcessorRegistry.getProcessorOrThrow(BookFileType.EPUB))
|
||||
.thenReturn(mockBookFileProcessor);
|
||||
when(mockBookFileProcessor.processFile(argThat(file -> file.getFileName().equals("book.epub"))))
|
||||
.thenReturn(createdBook);
|
||||
.thenReturn(new FileProcessResult(createdBook, FileProcessStatus.NEW, null));
|
||||
when(bookRepository.getReferenceById(createdBook.getId()))
|
||||
.thenReturn(bookEntity);
|
||||
when(bookAdditionalFileRepository.findByLibraryPath_IdAndFileSubPathAndFileName(anyLong(), anyString(), anyString()))
|
||||
@@ -275,7 +277,7 @@ class FolderAsBookFileProcessorTest {
|
||||
when(bookFileProcessorRegistry.getProcessorOrThrow(BookFileType.PDF))
|
||||
.thenReturn(mockBookFileProcessor);
|
||||
when(mockBookFileProcessor.processFile(argThat(file -> file.getFileName().equals("book.pdf"))))
|
||||
.thenReturn(createdBook);
|
||||
.thenReturn(new FileProcessResult(createdBook, FileProcessStatus.NEW, null));
|
||||
when(bookRepository.getReferenceById(createdBook.getId()))
|
||||
.thenReturn(bookEntity);
|
||||
when(bookAdditionalFileRepository.findByLibraryPath_IdAndFileSubPathAndFileName(anyLong(), anyString(), anyString()))
|
||||
|
||||
+34
-29
@@ -4,11 +4,13 @@ import com.adityachandel.booklore.config.AppProperties;
|
||||
import com.adityachandel.booklore.exception.APIException;
|
||||
import com.adityachandel.booklore.exception.ApiError;
|
||||
import com.adityachandel.booklore.mapper.AdditionalFileMapperImpl;
|
||||
import com.adityachandel.booklore.model.FileProcessResult;
|
||||
import com.adityachandel.booklore.model.dto.Book;
|
||||
import com.adityachandel.booklore.model.dto.settings.AppSettings;
|
||||
import com.adityachandel.booklore.model.entity.LibraryEntity;
|
||||
import com.adityachandel.booklore.model.entity.LibraryPathEntity;
|
||||
import com.adityachandel.booklore.model.enums.BookFileType;
|
||||
import com.adityachandel.booklore.model.enums.FileProcessStatus;
|
||||
import com.adityachandel.booklore.model.websocket.Topic;
|
||||
import com.adityachandel.booklore.repository.BookAdditionalFileRepository;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
@@ -110,11 +112,11 @@ class FileUploadServiceTest {
|
||||
Files.write(tempDir.resolve("dup.epub"), new byte[]{1, 2, 3});
|
||||
|
||||
assertThatExceptionOfType(APIException.class)
|
||||
.isThrownBy(() -> service.uploadFileBookDrop(file))
|
||||
.satisfies(ex -> {
|
||||
assertThat(ex.getStatus()).isEqualTo(ApiError.FILE_ALREADY_EXISTS.getStatus());
|
||||
assertThat(ex.getMessage()).isEqualTo(ApiError.FILE_ALREADY_EXISTS.getMessage());
|
||||
});
|
||||
.isThrownBy(() -> service.uploadFileBookDrop(file))
|
||||
.satisfies(ex -> {
|
||||
assertThat(ex.getStatus()).isEqualTo(ApiError.FILE_ALREADY_EXISTS.getStatus());
|
||||
assertThat(ex.getMessage()).isEqualTo(ApiError.FILE_ALREADY_EXISTS.getMessage());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -122,11 +124,11 @@ class FileUploadServiceTest {
|
||||
MockMultipartFile file = new MockMultipartFile("file", "bad.txt", "text/plain", "x".getBytes());
|
||||
|
||||
assertThatExceptionOfType(APIException.class)
|
||||
.isThrownBy(() -> service.uploadFileBookDrop(file))
|
||||
.satisfies(ex -> {
|
||||
assertThat(ex.getStatus()).isEqualTo(ApiError.INVALID_FILE_FORMAT.getStatus());
|
||||
assertThat(ex.getMessage()).contains("Invalid file format, only pdf and epub are supported");
|
||||
});
|
||||
.isThrownBy(() -> service.uploadFileBookDrop(file))
|
||||
.satisfies(ex -> {
|
||||
assertThat(ex.getStatus()).isEqualTo(ApiError.INVALID_FILE_FORMAT.getStatus());
|
||||
assertThat(ex.getMessage()).contains("Invalid file format, only pdf and epub are supported");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -138,11 +140,11 @@ class FileUploadServiceTest {
|
||||
when(appSettingService.getAppSettings()).thenReturn(small);
|
||||
|
||||
assertThatExceptionOfType(APIException.class)
|
||||
.isThrownBy(() -> service.uploadFileBookDrop(file))
|
||||
.satisfies(ex -> {
|
||||
assertThat(ex.getStatus()).isEqualTo(ApiError.FILE_TOO_LARGE.getStatus());
|
||||
assertThat(ex.getMessage()).contains("1");
|
||||
});
|
||||
.isThrownBy(() -> service.uploadFileBookDrop(file))
|
||||
.satisfies(ex -> {
|
||||
assertThat(ex.getStatus()).isEqualTo(ApiError.FILE_TOO_LARGE.getStatus());
|
||||
assertThat(ex.getMessage()).contains("1");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -151,8 +153,8 @@ class FileUploadServiceTest {
|
||||
when(libraryRepository.findById(42L)).thenReturn(Optional.empty());
|
||||
|
||||
assertThatExceptionOfType(APIException.class)
|
||||
.isThrownBy(() -> service.uploadFile(file, 42L, 1L))
|
||||
.satisfies(ex -> assertThat(ex.getStatus()).isEqualTo(ApiError.LIBRARY_NOT_FOUND.getStatus()));
|
||||
.isThrownBy(() -> service.uploadFile(file, 42L, 1L))
|
||||
.satisfies(ex -> assertThat(ex.getStatus()).isEqualTo(ApiError.LIBRARY_NOT_FOUND.getStatus()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -164,8 +166,8 @@ class FileUploadServiceTest {
|
||||
when(libraryRepository.findById(42L)).thenReturn(Optional.of(lib));
|
||||
|
||||
assertThatExceptionOfType(APIException.class)
|
||||
.isThrownBy(() -> service.uploadFile(file, 42L, 99L))
|
||||
.satisfies(ex -> assertThat(ex.getStatus()).isEqualTo(ApiError.INVALID_LIBRARY_PATH.getStatus()));
|
||||
.isThrownBy(() -> service.uploadFile(file, 42L, 99L))
|
||||
.satisfies(ex -> assertThat(ex.getStatus()).isEqualTo(ApiError.INVALID_LIBRARY_PATH.getStatus()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -185,11 +187,11 @@ class FileUploadServiceTest {
|
||||
when(libraryRepository.findById(1L)).thenReturn(Optional.of(lib));
|
||||
|
||||
assertThatExceptionOfType(APIException.class)
|
||||
.isThrownBy(() -> service.uploadFile(file, 1L, 1L))
|
||||
.satisfies(ex -> {
|
||||
assertThat(ex.getStatus()).isEqualTo(ApiError.FILE_READ_ERROR.getStatus());
|
||||
assertThat(ex.getMessage()).contains("Error reading files from path");
|
||||
});
|
||||
.isThrownBy(() -> service.uploadFile(file, 1L, 1L))
|
||||
.satisfies(ex -> {
|
||||
assertThat(ex.getStatus()).isEqualTo(ApiError.FILE_READ_ERROR.getStatus());
|
||||
assertThat(ex.getMessage()).contains("Error reading files from path");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -206,17 +208,20 @@ class FileUploadServiceTest {
|
||||
when(libraryRepository.findById(7L)).thenReturn(Optional.of(lib));
|
||||
|
||||
BookFileProcessor proc = mock(BookFileProcessor.class);
|
||||
Book stubBook = Book.builder().build();
|
||||
stubBook.setTitle("X");
|
||||
FileProcessResult fileProcessResult = FileProcessResult.builder()
|
||||
.book(Book.builder().build())
|
||||
.status(FileProcessStatus.NEW)
|
||||
.build();
|
||||
|
||||
when(processorRegistry.getProcessorOrThrow(BookFileType.CBX)).thenReturn(proc);
|
||||
when(proc.processFile(any())).thenReturn(stubBook);
|
||||
when(proc.processFile(any())).thenReturn(fileProcessResult);
|
||||
|
||||
Book result = service.uploadFile(file, 7L, 2L);
|
||||
|
||||
assertThat(result).isSameAs(stubBook);
|
||||
assertThat(result).isSameAs(fileProcessResult.getBook());
|
||||
Path moved = tempDir.resolve("book.cbz");
|
||||
assertThat(Files.exists(moved)).isTrue();
|
||||
verify(notificationService).sendMessage(eq(Topic.BOOK_ADD), same(stubBook));
|
||||
verify(notificationService).sendMessage(eq(Topic.BOOK_ADD), same(fileProcessResult.getBook()));
|
||||
verifyNoInteractions(pdfMetadataExtractor, epubMetadataExtractor);
|
||||
}
|
||||
}
|
||||
|
||||
+12
-6
@@ -2,13 +2,11 @@ package com.adityachandel.booklore.util.builder;
|
||||
|
||||
import com.adityachandel.booklore.mapper.BookMapper;
|
||||
import com.adityachandel.booklore.mapper.BookMapperImpl;
|
||||
import com.adityachandel.booklore.model.FileProcessResult;
|
||||
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.model.enums.*;
|
||||
import com.adityachandel.booklore.repository.BookAdditionalFileRepository;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.service.FileFingerprint;
|
||||
@@ -74,7 +72,7 @@ public class LibraryTestBuilder {
|
||||
lenient().when(bookFileProcessorMock.processFile(any(LibraryFile.class)))
|
||||
.then(invocation -> {
|
||||
LibraryFile libraryFile = invocation.getArgument(0);
|
||||
return processFile(libraryFile);
|
||||
return processFileResult(libraryFile);
|
||||
});
|
||||
lenient().when(bookRepositoryMock.getReferenceById(anyLong()))
|
||||
.thenAnswer(invocation -> {
|
||||
@@ -275,7 +273,15 @@ public class LibraryTestBuilder {
|
||||
return hexString.toString();
|
||||
}
|
||||
|
||||
private Book processFile(LibraryFile libraryFile) {
|
||||
private FileProcessResult processFileResult(LibraryFile libraryFile) {
|
||||
var book = processBook(libraryFile);
|
||||
return FileProcessResult.builder()
|
||||
.book(book)
|
||||
.status(FileProcessStatus.NEW)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Book processBook(LibraryFile libraryFile) {
|
||||
var hash = computeFileHash(libraryFile.getFullPath());
|
||||
|
||||
long id = libraryFiles.indexOf(libraryFile) + 1L;
|
||||
|
||||
Reference in New Issue
Block a user