Implement Bookdrop: Watch folder for file drops and auto-process uploads
This commit is contained in:
committed by
Aditya Chandel
parent
5064706b8f
commit
177528e640
+127
@@ -0,0 +1,127 @@
|
||||
package com.adityachandel.booklore.service;
|
||||
|
||||
import com.adityachandel.booklore.config.AppProperties;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.repository.BookdropFileRepository;
|
||||
import com.adityachandel.booklore.repository.LibraryRepository;
|
||||
import com.adityachandel.booklore.service.bookdrop.BookDropService;
|
||||
import com.adityachandel.booklore.service.bookdrop.BookdropNotificationService;
|
||||
import com.adityachandel.booklore.service.fileprocessor.CbxProcessor;
|
||||
import com.adityachandel.booklore.service.fileprocessor.EpubProcessor;
|
||||
import com.adityachandel.booklore.service.fileprocessor.PdfProcessor;
|
||||
import com.adityachandel.booklore.service.metadata.MetadataRefreshService;
|
||||
import com.adityachandel.booklore.service.bookdrop.BookdropMonitoringService;
|
||||
import com.adityachandel.booklore.service.monitoring.MonitoringService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.core.io.PathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class BookDropServiceTest {
|
||||
|
||||
private BookdropFileRepository bookdropFileRepository;
|
||||
private LibraryRepository libraryRepository;
|
||||
private BookRepository bookRepository;
|
||||
private MonitoringService monitoringService;
|
||||
private BookdropMonitoringService bookdropMonitoringService;
|
||||
private NotificationService notificationService;
|
||||
private MetadataRefreshService metadataRefreshService;
|
||||
private BookdropNotificationService bookdropNotificationService;
|
||||
private PdfProcessor pdfProcessor;
|
||||
private EpubProcessor epubProcessor;
|
||||
private CbxProcessor cbxProcessor;
|
||||
private AppProperties appProperties;
|
||||
|
||||
private BookDropService bookDropService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
bookdropFileRepository = mock(BookdropFileRepository.class);
|
||||
libraryRepository = mock(LibraryRepository.class);
|
||||
bookRepository = mock(BookRepository.class);
|
||||
monitoringService = mock(MonitoringService.class);
|
||||
bookdropMonitoringService = mock(BookdropMonitoringService.class);
|
||||
notificationService = mock(NotificationService.class);
|
||||
metadataRefreshService = mock(MetadataRefreshService.class);
|
||||
bookdropNotificationService = mock(BookdropNotificationService.class);
|
||||
pdfProcessor = mock(PdfProcessor.class);
|
||||
epubProcessor = mock(EpubProcessor.class);
|
||||
cbxProcessor = mock(CbxProcessor.class);
|
||||
appProperties = mock(AppProperties.class);
|
||||
|
||||
bookDropService = new BookDropService(
|
||||
bookdropFileRepository, libraryRepository, bookRepository,
|
||||
monitoringService, bookdropMonitoringService,
|
||||
notificationService, metadataRefreshService,
|
||||
bookdropNotificationService,
|
||||
pdfProcessor, epubProcessor, cbxProcessor,
|
||||
appProperties
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void discardAllFiles_shouldDeleteFilesDirsAndNotify() throws Exception {
|
||||
Path bookdropPath = Paths.get("/tmp/bookdrop");
|
||||
when(appProperties.getBookdropFolder()).thenReturn(bookdropPath.toString());
|
||||
|
||||
Files.createDirectories(bookdropPath);
|
||||
Path testFile = bookdropPath.resolve("testfile.txt");
|
||||
Files.createFile(testFile);
|
||||
|
||||
when(bookdropFileRepository.count()).thenReturn(1L);
|
||||
|
||||
Path tempCoverDir = Paths.get("/tmp/config/bookdrop_temp");
|
||||
when(appProperties.getPathConfig()).thenReturn("/tmp/config");
|
||||
Files.createDirectories(tempCoverDir);
|
||||
Path tempCover = tempCoverDir.resolve("1.jpg");
|
||||
Files.createFile(tempCover);
|
||||
|
||||
bookDropService.discardAllFiles();
|
||||
|
||||
verify(bookdropFileRepository).deleteAll();
|
||||
verify(bookdropNotificationService).sendBookdropFileSummaryNotification();
|
||||
verify(bookdropMonitoringService).pauseMonitoring();
|
||||
verify(bookdropMonitoringService).resumeMonitoring();
|
||||
|
||||
Files.deleteIfExists(testFile);
|
||||
Files.deleteIfExists(bookdropPath);
|
||||
Files.deleteIfExists(tempCover);
|
||||
Files.deleteIfExists(tempCoverDir);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getBookdropCover_shouldReturnResourceIfExists() throws Exception {
|
||||
when(appProperties.getPathConfig()).thenReturn("/tmp/config");
|
||||
long bookdropId = 123L;
|
||||
Path coverPath = Paths.get("/tmp/config/bookdrop_temp", bookdropId + ".jpg");
|
||||
File coverFile = coverPath.toFile();
|
||||
|
||||
coverFile.getParentFile().mkdirs();
|
||||
coverFile.createNewFile();
|
||||
|
||||
Resource resource = bookDropService.getBookdropCover(bookdropId);
|
||||
|
||||
assertThat(resource).isInstanceOf(PathResource.class);
|
||||
assertThat(((PathResource) resource).getFile().getName()).isEqualTo(bookdropId + ".jpg");
|
||||
|
||||
coverFile.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getBookdropCover_shouldReturnNullIfNotExists() {
|
||||
when(appProperties.getPathConfig()).thenReturn("/tmp/config");
|
||||
long bookdropId = 99999L;
|
||||
|
||||
Resource resource = bookDropService.getBookdropCover(bookdropId);
|
||||
|
||||
assertThat(resource).isNull();
|
||||
}
|
||||
}
|
||||
+183
@@ -0,0 +1,183 @@
|
||||
package com.adityachandel.booklore.service;
|
||||
|
||||
import com.adityachandel.booklore.exception.APIException;
|
||||
import com.adityachandel.booklore.model.dto.BookMetadata;
|
||||
import com.adityachandel.booklore.model.dto.settings.AppSettings;
|
||||
import com.adityachandel.booklore.model.entity.BookdropFileEntity;
|
||||
import com.adityachandel.booklore.model.enums.MetadataProvider;
|
||||
import com.adityachandel.booklore.repository.BookdropFileRepository;
|
||||
import com.adityachandel.booklore.service.appsettings.AppSettingService;
|
||||
import com.adityachandel.booklore.service.bookdrop.BookdropMetadataService;
|
||||
import com.adityachandel.booklore.service.metadata.MetadataRefreshService;
|
||||
import com.adityachandel.booklore.service.metadata.extractor.CbxMetadataExtractor;
|
||||
import com.adityachandel.booklore.service.metadata.extractor.EpubMetadataExtractor;
|
||||
import com.adityachandel.booklore.service.metadata.extractor.PdfMetadataExtractor;
|
||||
import com.adityachandel.booklore.util.FileService;
|
||||
import com.adityachandel.booklore.util.ImageUtils;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.io.File;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static com.adityachandel.booklore.model.entity.BookdropFileEntity.Status.PENDING_REVIEW;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class BookdropMetadataServiceTest {
|
||||
|
||||
@Mock
|
||||
private BookdropFileRepository bookdropFileRepository;
|
||||
@Mock
|
||||
private AppSettingService appSettingService;
|
||||
@Mock
|
||||
private ObjectMapper objectMapper;
|
||||
@Mock
|
||||
private EpubMetadataExtractor epubMetadataExtractor;
|
||||
@Mock
|
||||
private PdfMetadataExtractor pdfMetadataExtractor;
|
||||
@Mock
|
||||
private CbxMetadataExtractor cbxMetadataExtractor;
|
||||
@Mock
|
||||
private MetadataRefreshService metadataRefreshService;
|
||||
@Mock
|
||||
private ImageUtils imageUtils;
|
||||
@Mock
|
||||
private FileService fileService;
|
||||
|
||||
@InjectMocks
|
||||
private BookdropMetadataService bookdropMetadataService;
|
||||
|
||||
private BookdropFileEntity sampleFile;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
sampleFile = new BookdropFileEntity();
|
||||
sampleFile.setId(1L);
|
||||
sampleFile.setFileName("book.epub");
|
||||
sampleFile.setFilePath("/tmp/book.epub");
|
||||
}
|
||||
|
||||
@Test
|
||||
void attachInitialMetadata_shouldExtractAndSaveMetadata() throws Exception {
|
||||
BookMetadata metadata = BookMetadata.builder().title("Test Book").build();
|
||||
|
||||
when(bookdropFileRepository.findById(1L)).thenReturn(Optional.of(sampleFile));
|
||||
when(epubMetadataExtractor.extractMetadata(any(File.class))).thenReturn(metadata);
|
||||
when(objectMapper.writeValueAsString(metadata)).thenReturn("{\"title\":\"Test Book\"}");
|
||||
when(bookdropFileRepository.save(any())).thenReturn(sampleFile);
|
||||
|
||||
BookdropFileEntity result = bookdropMetadataService.attachInitialMetadata(1L);
|
||||
|
||||
assertThat(result).isNotNull();
|
||||
assertThat(result.getOriginalMetadata()).contains("Test Book");
|
||||
assertThat(result.getUpdatedAt()).isBeforeOrEqualTo(Instant.now());
|
||||
verify(bookdropFileRepository).save(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void attachInitialMetadata_shouldThrowWhenFileMissing() {
|
||||
when(bookdropFileRepository.findById(99L)).thenReturn(Optional.empty());
|
||||
|
||||
org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> bookdropMetadataService.attachInitialMetadata(99L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void attachFetchedMetadata_shouldUpdateEntityWithFetchedData() throws Exception {
|
||||
sampleFile.setOriginalMetadata("{\"title\":\"Old Book\"}");
|
||||
AppSettings settings = new AppSettings();
|
||||
BookMetadata fetched = BookMetadata.builder().title("New Title").build();
|
||||
|
||||
when(bookdropFileRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
when(bookdropFileRepository.findById(1L)).thenReturn(Optional.of(sampleFile));
|
||||
when(appSettingService.getAppSettings()).thenReturn(settings);
|
||||
when(metadataRefreshService.prepareProviders(any())).thenReturn(List.of());
|
||||
when(objectMapper.readValue(sampleFile.getOriginalMetadata(), BookMetadata.class)).thenReturn(fetched);
|
||||
when(metadataRefreshService.fetchMetadataForBook(any(), any())).thenReturn(Map.of());
|
||||
when(metadataRefreshService.buildFetchMetadata(any(), any(), any())).thenReturn(fetched);
|
||||
when(objectMapper.writeValueAsString(fetched)).thenReturn("{\"title\":\"New Title\"}");
|
||||
|
||||
BookdropFileEntity result = bookdropMetadataService.attachFetchedMetadata(1L);
|
||||
|
||||
assertThat(result.getFetchedMetadata()).contains("New Title");
|
||||
assertThat(result.getStatus()).isEqualTo(PENDING_REVIEW);
|
||||
verify(bookdropFileRepository).save(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void attachInitialMetadata_shouldHandleNullCoverGracefully() throws Exception {
|
||||
BookMetadata metadata = BookMetadata.builder().title("No Cover Book").build();
|
||||
|
||||
when(bookdropFileRepository.findById(1L)).thenReturn(Optional.of(sampleFile));
|
||||
when(epubMetadataExtractor.extractMetadata(any(File.class))).thenReturn(metadata);
|
||||
when(objectMapper.writeValueAsString(metadata)).thenReturn("{\"title\":\"No Cover Book\"}");
|
||||
when(epubMetadataExtractor.extractCover(any(File.class))).thenReturn(null);
|
||||
when(bookdropFileRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
|
||||
BookdropFileEntity result = bookdropMetadataService.attachInitialMetadata(1L);
|
||||
|
||||
assertThat(result.getOriginalMetadata()).contains("No Cover Book");
|
||||
verify(imageUtils, never()).saveImage(any(), any(), anyInt(), anyInt());
|
||||
verify(bookdropFileRepository).save(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractInitialMetadata_shouldThrowForUnsupportedFileExtension() {
|
||||
sampleFile.setFileName("book.txt");
|
||||
sampleFile.setFilePath("/tmp/book.txt");
|
||||
|
||||
when(bookdropFileRepository.findById(sampleFile.getId())).thenReturn(Optional.of(sampleFile));
|
||||
|
||||
assertThatThrownBy(() -> {
|
||||
bookdropMetadataService.attachInitialMetadata(sampleFile.getId());
|
||||
}).isInstanceOf(APIException.class)
|
||||
.hasMessageContaining("Invalid file format");
|
||||
}
|
||||
|
||||
@Test
|
||||
void attachFetchedMetadata_shouldSleepIfGoodreadsIncluded() throws Exception {
|
||||
sampleFile.setOriginalMetadata("{\"title\":\"Book\"}");
|
||||
AppSettings settings = new AppSettings();
|
||||
BookMetadata fetched = BookMetadata.builder().title("Fetched Book").build();
|
||||
|
||||
when(bookdropFileRepository.findById(1L)).thenReturn(Optional.of(sampleFile));
|
||||
when(appSettingService.getAppSettings()).thenReturn(settings);
|
||||
when(metadataRefreshService.prepareProviders(any())).thenReturn(List.of(MetadataProvider.GoodReads));
|
||||
when(objectMapper.readValue(anyString(), eq(BookMetadata.class))).thenReturn(fetched);
|
||||
when(metadataRefreshService.fetchMetadataForBook(any(), any())).thenReturn(Map.of());
|
||||
when(metadataRefreshService.buildFetchMetadata(any(), any(), any())).thenReturn(fetched);
|
||||
when(objectMapper.writeValueAsString(fetched)).thenReturn("{\"title\":\"Fetched Book\"}");
|
||||
when(bookdropFileRepository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
|
||||
BookdropFileEntity result = bookdropMetadataService.attachFetchedMetadata(1L);
|
||||
|
||||
assertThat(result.getFetchedMetadata()).contains("Fetched Book");
|
||||
assertThat(result.getStatus()).isEqualTo(PENDING_REVIEW);
|
||||
verify(bookdropFileRepository).save(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void attachFetchedMetadata_shouldThrowOnJsonProcessingError() throws Exception {
|
||||
sampleFile.setOriginalMetadata("{invalidJson}");
|
||||
|
||||
when(bookdropFileRepository.findById(1L)).thenReturn(Optional.of(sampleFile));
|
||||
when(appSettingService.getAppSettings()).thenReturn(new AppSettings());
|
||||
when(objectMapper.readValue(anyString(), eq(BookMetadata.class)))
|
||||
.thenThrow(new JsonProcessingException("Invalid JSON") {
|
||||
});
|
||||
|
||||
assertThatThrownBy(() -> bookdropMetadataService.attachFetchedMetadata(1L))
|
||||
.isInstanceOf(JsonProcessingException.class);
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.adityachandel.booklore.service;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.BookdropFileNotification;
|
||||
import com.adityachandel.booklore.model.entity.BookdropFileEntity;
|
||||
import com.adityachandel.booklore.model.websocket.Topic;
|
||||
import com.adityachandel.booklore.repository.BookdropFileRepository;
|
||||
import com.adityachandel.booklore.service.bookdrop.BookdropNotificationService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class BookdropNotificationServiceTest {
|
||||
|
||||
private BookdropFileRepository bookdropFileRepository;
|
||||
private NotificationService notificationService;
|
||||
|
||||
private BookdropNotificationService bookdropNotificationService;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
bookdropFileRepository = mock(BookdropFileRepository.class);
|
||||
notificationService = mock(NotificationService.class);
|
||||
|
||||
bookdropNotificationService = new BookdropNotificationService(bookdropFileRepository, notificationService);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendBookdropFileSummaryNotification_shouldSendCorrectNotification() {
|
||||
long pendingCount = 5L;
|
||||
long totalCount = 20L;
|
||||
|
||||
when(bookdropFileRepository.countByStatus(BookdropFileEntity.Status.PENDING_REVIEW)).thenReturn(pendingCount);
|
||||
when(bookdropFileRepository.count()).thenReturn(totalCount);
|
||||
|
||||
bookdropNotificationService.sendBookdropFileSummaryNotification();
|
||||
|
||||
ArgumentCaptor<BookdropFileNotification> captor = ArgumentCaptor.forClass(BookdropFileNotification.class);
|
||||
verify(notificationService).sendMessage(eq(Topic.BOOKDROP_FILE), captor.capture());
|
||||
|
||||
BookdropFileNotification sentNotification = captor.getValue();
|
||||
|
||||
assertThat(sentNotification.getPendingCount()).isEqualTo((int) pendingCount);
|
||||
assertThat(sentNotification.getTotalCount()).isEqualTo((int) totalCount);
|
||||
assertThat(Instant.parse(sentNotification.getLastUpdatedAt())).isBeforeOrEqualTo(Instant.now());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendBookdropFileSummaryNotification_shouldSendEvenIfCountsAreZero() {
|
||||
when(bookdropFileRepository.countByStatus(BookdropFileEntity.Status.PENDING_REVIEW)).thenReturn(0L);
|
||||
when(bookdropFileRepository.count()).thenReturn(0L);
|
||||
|
||||
bookdropNotificationService.sendBookdropFileSummaryNotification();
|
||||
|
||||
verify(notificationService).sendMessage(eq(Topic.BOOKDROP_FILE), any(BookdropFileNotification.class));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user