feat(bookdrop): add ability to poll the bookdrop folder (#2768)
* feat(bookdrop): add ability to poll the bookdrop folder * fix(bookdrop): Modify bookdrop scan behavior to only enqueue newly seen files to reduce duplicate log messages
This commit is contained in:
+26
-1
@@ -1,6 +1,7 @@
|
||||
package org.booklore.service.bookdrop;
|
||||
|
||||
import org.booklore.config.AppProperties;
|
||||
import org.booklore.repository.BookdropFileRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
@@ -9,6 +10,7 @@ import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardWatchEventKinds;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@@ -16,6 +18,7 @@ class BookdropMonitoringServiceTest {
|
||||
|
||||
private AppProperties appProperties;
|
||||
private BookdropEventHandlerService eventHandler;
|
||||
private BookdropFileRepository bookdropFileRepository;
|
||||
private BookdropMonitoringService monitoringService;
|
||||
|
||||
@TempDir
|
||||
@@ -25,9 +28,10 @@ class BookdropMonitoringServiceTest {
|
||||
void setUp() {
|
||||
appProperties = mock(AppProperties.class);
|
||||
eventHandler = mock(BookdropEventHandlerService.class);
|
||||
bookdropFileRepository = mock(BookdropFileRepository.class);
|
||||
|
||||
when(appProperties.getBookdropFolder()).thenReturn(tempDir.toString());
|
||||
monitoringService = new BookdropMonitoringService(appProperties, eventHandler);
|
||||
monitoringService = new BookdropMonitoringService(appProperties, eventHandler, bookdropFileRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -49,6 +53,8 @@ class BookdropMonitoringServiceTest {
|
||||
Path invalidFileInSubdir = subDir.resolve("._another.epub");
|
||||
Files.createFile(invalidFileInSubdir);
|
||||
|
||||
when(bookdropFileRepository.findAllFilePathsIn(anyList())).thenReturn(List.of());
|
||||
|
||||
monitoringService.start();
|
||||
|
||||
monitoringService.stop();
|
||||
@@ -60,4 +66,23 @@ class BookdropMonitoringServiceTest {
|
||||
verify(eventHandler, never()).enqueueFile(eq(hiddenFile), any());
|
||||
verify(eventHandler, never()).enqueueFile(eq(invalidFileInSubdir), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void scanExistingBookdropFiles_ShouldSkipFilesAlreadyTrackedInDatabase() throws IOException {
|
||||
Path alreadyTracked = tempDir.resolve("already-tracked.epub");
|
||||
Files.createFile(alreadyTracked);
|
||||
|
||||
Path newFile = tempDir.resolve("new-file.epub");
|
||||
Files.createFile(newFile);
|
||||
|
||||
when(bookdropFileRepository.findAllFilePathsIn(anyList()))
|
||||
.thenReturn(List.of(alreadyTracked.toAbsolutePath().toString()));
|
||||
|
||||
monitoringService.start();
|
||||
|
||||
monitoringService.stop();
|
||||
|
||||
verify(eventHandler, never()).enqueueFile(eq(alreadyTracked), eq(StandardWatchEventKinds.ENTRY_CREATE));
|
||||
verify(eventHandler).enqueueFile(eq(newFile), eq(StandardWatchEventKinds.ENTRY_CREATE));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package org.booklore.task.tasks;
|
||||
|
||||
import org.booklore.exception.APIException;
|
||||
import org.booklore.model.dto.BookLoreUser;
|
||||
import org.booklore.model.dto.request.TaskCreateRequest;
|
||||
import org.booklore.model.dto.response.TaskCreateResponse;
|
||||
import org.booklore.model.enums.TaskType;
|
||||
import org.booklore.service.bookdrop.BookdropMonitoringService;
|
||||
import org.booklore.task.TaskStatus;
|
||||
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 static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class BookdropPeriodicScanTaskTest {
|
||||
|
||||
@Mock
|
||||
private BookdropMonitoringService bookdropMonitoringService;
|
||||
|
||||
@InjectMocks
|
||||
private BookdropPeriodicScanTask task;
|
||||
|
||||
private BookLoreUser user;
|
||||
private TaskCreateRequest request;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
user = BookLoreUser.builder()
|
||||
.permissions(new BookLoreUser.UserPermissions())
|
||||
.build();
|
||||
request = new TaskCreateRequest();
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatePermissions_shouldThrowException_whenUserCannotAccessTaskManager() {
|
||||
user.getPermissions().setCanAccessTaskManager(false);
|
||||
assertThrows(APIException.class, () -> task.validatePermissions(user, request));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatePermissions_shouldPass_whenUserCanAccessTaskManager() {
|
||||
user.getPermissions().setCanAccessTaskManager(true);
|
||||
assertDoesNotThrow(() -> task.validatePermissions(user, request));
|
||||
}
|
||||
|
||||
@Test
|
||||
void execute_shouldTriggerBookdropRescan() {
|
||||
TaskCreateResponse response = task.execute(request);
|
||||
|
||||
assertEquals(TaskType.BOOKDROP_PERIODIC_SCANNING, response.getTaskType());
|
||||
assertEquals(TaskStatus.COMPLETED, response.getStatus());
|
||||
verify(bookdropMonitoringService).rescanBookdropFolder();
|
||||
}
|
||||
|
||||
@Test
|
||||
void execute_shouldReturnFailed_whenRescanThrows() {
|
||||
doThrow(new RuntimeException("rescan failed")).when(bookdropMonitoringService).rescanBookdropFolder();
|
||||
|
||||
TaskCreateResponse response = task.execute(request);
|
||||
|
||||
assertEquals(TaskType.BOOKDROP_PERIODIC_SCANNING, response.getTaskType());
|
||||
assertEquals(TaskStatus.FAILED, response.getStatus());
|
||||
verify(bookdropMonitoringService).rescanBookdropFolder();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user