fix(cover): apply bulk cover actions to audiobook covers (#2623)
This commit is contained in:
@@ -32,6 +32,8 @@ import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@@ -97,6 +99,29 @@ class BookCoverServiceTest {
|
||||
.build();
|
||||
}
|
||||
|
||||
private void runAsyncInline() {
|
||||
doAnswer(inv -> {
|
||||
inv.<Runnable>getArgument(0).run();
|
||||
return null;
|
||||
}).when(taskExecutor).execute(any(Runnable.class));
|
||||
}
|
||||
|
||||
private void runTransactionsInline() {
|
||||
when(transactionTemplate.execute(any())).thenAnswer(inv -> {
|
||||
var callback = inv.getArgument(0, TransactionCallback.class);
|
||||
return callback.doInTransaction(null);
|
||||
});
|
||||
}
|
||||
|
||||
private MultipartFile validPngFile() throws Exception {
|
||||
MultipartFile file = mock(MultipartFile.class);
|
||||
when(file.isEmpty()).thenReturn(false);
|
||||
when(file.getSize()).thenReturn(1024L);
|
||||
when(file.getInputStream()).thenReturn(new ByteArrayInputStream(new byte[]{(byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}));
|
||||
when(file.getBytes()).thenReturn(new byte[]{1, 2, 3});
|
||||
return file;
|
||||
}
|
||||
|
||||
@Nested
|
||||
class BookNotFound {
|
||||
|
||||
@@ -688,23 +713,44 @@ class BookCoverServiceTest {
|
||||
class BulkRegenerateCoversForBooks {
|
||||
|
||||
@Test
|
||||
void delegatesToAsyncExecutorWithUnlockedBooks() {
|
||||
BookEntity unlocked = buildBook(1L, false);
|
||||
unlocked.setBookFiles(Set.of(BookFileEntity.builder().bookType(BookFileType.EPUB).isBookFormat(true).build()));
|
||||
unlocked.setLibrary(LibraryEntity.builder().build());
|
||||
BookEntity locked = buildBook(2L, true);
|
||||
void regeneratesBothBooksInMixedBookAndAudiobookSelection() {
|
||||
BookEntity ebook = buildBook(1L, false);
|
||||
BookFileEntity ebookFile = BookFileEntity.builder()
|
||||
.bookType(BookFileType.EPUB)
|
||||
.isBookFormat(true)
|
||||
.build();
|
||||
ebook.setBookFiles(Set.of(ebookFile));
|
||||
BookEntity audiobook = buildBookWithAudiobookLock(2L, false);
|
||||
audiobook.setBookFiles(Set.of(BookFileEntity.builder()
|
||||
.bookType(BookFileType.AUDIOBOOK)
|
||||
.isBookFormat(true)
|
||||
.build()));
|
||||
|
||||
BookFileProcessor ebookProcessor = mock(BookFileProcessor.class);
|
||||
BookFileProcessor audiobookProcessor = mock(BookFileProcessor.class);
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(1L, 2L)))
|
||||
.thenReturn(List.of(unlocked, locked));
|
||||
|
||||
doAnswer(inv -> {
|
||||
inv.<Runnable>getArgument(0).run();
|
||||
return null;
|
||||
}).when(taskExecutor).execute(any(Runnable.class));
|
||||
.thenReturn(List.of(ebook, audiobook));
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(ebook));
|
||||
when(bookRepository.findByIdWithBookFiles(2L)).thenReturn(Optional.of(audiobook));
|
||||
when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(ebookProcessor);
|
||||
when(processorRegistry.getProcessorOrThrow(BookFileType.AUDIOBOOK)).thenReturn(audiobookProcessor);
|
||||
when(ebookProcessor.generateCover(ebook, ebookFile)).thenReturn(true);
|
||||
when(audiobookProcessor.generateAudiobookCover(audiobook)).thenReturn(true);
|
||||
when(bookRepository.findCoverUpdateInfoByIds(any())).thenReturn(List.of());
|
||||
runAsyncInline();
|
||||
runTransactionsInline();
|
||||
|
||||
service.regenerateCoversForBooks(Set.of(1L, 2L));
|
||||
|
||||
verify(taskExecutor).execute(any(Runnable.class));
|
||||
verify(ebookProcessor).generateCover(ebook, ebookFile);
|
||||
verify(audiobookProcessor).generateAudiobookCover(audiobook);
|
||||
verify(bookRepository).save(ebook);
|
||||
verify(bookRepository).save(audiobook);
|
||||
assertThat(ebook.getMetadata().getCoverUpdatedOn()).isNotNull();
|
||||
assertThat(audiobook.getMetadata().getAudiobookCoverUpdatedOn()).isNotNull();
|
||||
assertThat(audiobook.getAudiobookCoverHash()).isNotNull();
|
||||
assertThat(audiobook.getMetadata().getCoverUpdatedOn()).isNull();
|
||||
assertThat(audiobook.getBookCoverHash()).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -712,21 +758,39 @@ class BookCoverServiceTest {
|
||||
class BulkGenerateCustomCoversForBooks {
|
||||
|
||||
@Test
|
||||
void delegatesToAsyncExecutorWithUnlockedBooks() {
|
||||
BookEntity unlocked = buildBook(1L, false);
|
||||
BookEntity locked = buildBook(2L, true);
|
||||
void generatesTheCorrectCoverShapeForMixedBookAndAudiobookSelection() {
|
||||
when(appProperties.isLocalStorage()).thenReturn(false);
|
||||
BookEntity ebook = buildBook(1L, false);
|
||||
ebook.setBookFiles(Set.of(BookFileEntity.builder()
|
||||
.bookType(BookFileType.EPUB)
|
||||
.isBookFormat(true)
|
||||
.build()));
|
||||
BookEntity audiobook = buildBookWithAudiobookLock(2L, false);
|
||||
audiobook.setBookFiles(Set.of(BookFileEntity.builder()
|
||||
.bookType(BookFileType.AUDIOBOOK)
|
||||
.isBookFormat(true)
|
||||
.build()));
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(1L, 2L)))
|
||||
.thenReturn(List.of(unlocked, locked));
|
||||
|
||||
doAnswer(inv -> {
|
||||
inv.<Runnable>getArgument(0).run();
|
||||
return null;
|
||||
}).when(taskExecutor).execute(any(Runnable.class));
|
||||
.thenReturn(List.of(ebook, audiobook));
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(ebook));
|
||||
when(bookRepository.findByIdWithBookFiles(2L)).thenReturn(Optional.of(audiobook));
|
||||
when(coverImageGenerator.generateCover("Test Book", null)).thenReturn(new byte[]{1});
|
||||
when(coverImageGenerator.generateSquareCover("Test Audiobook", null)).thenReturn(new byte[]{2});
|
||||
when(bookRepository.findCoverUpdateInfoByIds(any())).thenReturn(List.of());
|
||||
runAsyncInline();
|
||||
runTransactionsInline();
|
||||
|
||||
service.generateCustomCoversForBooks(Set.of(1L, 2L));
|
||||
|
||||
verify(taskExecutor).execute(any(Runnable.class));
|
||||
verify(coverImageGenerator).generateCover("Test Book", null);
|
||||
verify(coverImageGenerator).generateSquareCover("Test Audiobook", null);
|
||||
verify(fileService).createThumbnailFromBytes(eq(1L), any());
|
||||
verify(fileService).createAudiobookThumbnailFromBytes(eq(2L), any());
|
||||
verify(fileService, never()).createThumbnailFromBytes(eq(2L), any());
|
||||
verify(fileService, never()).createAudiobookThumbnailFromBytes(eq(1L), any());
|
||||
verify(bookRepository).save(ebook);
|
||||
verify(bookRepository).save(audiobook);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -734,30 +798,30 @@ class BookCoverServiceTest {
|
||||
class BulkUpdateCoverFromFileForBooks {
|
||||
|
||||
@Test
|
||||
void processesOnlyUnlockedBooks() throws Exception {
|
||||
void appliesUploadedCoverToAudiobookSlotForAudiobookOnlyBooks() throws Exception {
|
||||
when(appSettingService.getAppSettings()).thenReturn(appSettings);
|
||||
when(appSettings.getMaxFileUploadSizeInMb()).thenReturn(5);
|
||||
|
||||
BookEntity unlocked = buildBook(1L, false);
|
||||
BookEntity locked = buildBook(2L, true);
|
||||
BookEntity audiobook = buildBookWithAudiobookLock(1L, false);
|
||||
audiobook.setBookFiles(Set.of(BookFileEntity.builder()
|
||||
.bookType(BookFileType.AUDIOBOOK)
|
||||
.isBookFormat(true)
|
||||
.build()));
|
||||
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(1L, 2L)))
|
||||
.thenReturn(List.of(unlocked, locked));
|
||||
when(bookQueryService.findAllWithMetadataByIds(Set.of(1L))).thenReturn(List.of(audiobook));
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(audiobook));
|
||||
when(bookRepository.findCoverUpdateInfoByIds(any())).thenReturn(List.of());
|
||||
MultipartFile file = validPngFile();
|
||||
runAsyncInline();
|
||||
runTransactionsInline();
|
||||
|
||||
MultipartFile file = mock(MultipartFile.class);
|
||||
when(file.isEmpty()).thenReturn(false);
|
||||
when(file.getSize()).thenReturn(1024L);
|
||||
when(file.getInputStream()).thenReturn(new ByteArrayInputStream(new byte[]{(byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}));
|
||||
when(file.getBytes()).thenReturn(new byte[]{1, 2, 3});
|
||||
service.updateCoverFromFileForBooks(Set.of(1L), file);
|
||||
|
||||
doAnswer(inv -> {
|
||||
inv.<Runnable>getArgument(0).run();
|
||||
return null;
|
||||
}).when(taskExecutor).execute(any(Runnable.class));
|
||||
|
||||
service.updateCoverFromFileForBooks(Set.of(1L, 2L), file);
|
||||
|
||||
verify(taskExecutor).execute(any(Runnable.class));
|
||||
verify(fileService).createAudiobookThumbnailFromBytes(eq(1L), any());
|
||||
verify(fileService, never()).createThumbnailFromBytes(eq(1L), any());
|
||||
verify(bookRepository).save(audiobook);
|
||||
assertThat(audiobook.getMetadata().getAudiobookCoverUpdatedOn()).isNotNull();
|
||||
assertThat(audiobook.getAudiobookCoverHash()).isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -781,13 +845,10 @@ class BookCoverServiceTest {
|
||||
});
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(book));
|
||||
when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(processor);
|
||||
when(processor.generateCover(book)).thenReturn(true);
|
||||
when(processor.generateCover(book, ebookFile)).thenReturn(true);
|
||||
when(bookRepository.findCoverUpdateInfoByIds(any())).thenReturn(List.of());
|
||||
|
||||
doAnswer(inv -> {
|
||||
inv.<Runnable>getArgument(0).run();
|
||||
return null;
|
||||
}).when(taskExecutor).execute(any(Runnable.class));
|
||||
runAsyncInline();
|
||||
|
||||
service.regenerateCovers(false);
|
||||
|
||||
@@ -805,10 +866,7 @@ class BookCoverServiceTest {
|
||||
|
||||
when(bookQueryService.getAllFullBookEntitiesWithFiles()).thenReturn(List.of(locked));
|
||||
|
||||
doAnswer(inv -> {
|
||||
inv.<Runnable>getArgument(0).run();
|
||||
return null;
|
||||
}).when(taskExecutor).execute(any(Runnable.class));
|
||||
runAsyncInline();
|
||||
|
||||
service.regenerateCovers(false);
|
||||
|
||||
@@ -840,13 +898,10 @@ class BookCoverServiceTest {
|
||||
});
|
||||
when(bookRepository.findByIdWithBookFiles(2L)).thenReturn(Optional.of(withoutCover));
|
||||
when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(processor);
|
||||
when(processor.generateCover(withoutCover)).thenReturn(true);
|
||||
when(processor.generateCover(withoutCover, ebookFile2)).thenReturn(true);
|
||||
when(bookRepository.findCoverUpdateInfoByIds(any())).thenReturn(List.of());
|
||||
|
||||
doAnswer(inv -> {
|
||||
inv.<Runnable>getArgument(0).run();
|
||||
return null;
|
||||
}).when(taskExecutor).execute(any(Runnable.class));
|
||||
runAsyncInline();
|
||||
|
||||
service.regenerateCovers(true);
|
||||
|
||||
@@ -863,10 +918,7 @@ class BookCoverServiceTest {
|
||||
|
||||
when(bookQueryService.getAllFullBookEntitiesWithFiles()).thenReturn(List.of(book));
|
||||
|
||||
doAnswer(inv -> {
|
||||
inv.<Runnable>getArgument(0).run();
|
||||
return null;
|
||||
}).when(taskExecutor).execute(any(Runnable.class));
|
||||
runAsyncInline();
|
||||
|
||||
service.regenerateCovers(false);
|
||||
|
||||
@@ -885,10 +937,7 @@ class BookCoverServiceTest {
|
||||
|
||||
when(bookQueryService.getAllFullBookEntitiesWithFiles()).thenReturn(List.of(book));
|
||||
|
||||
doAnswer(inv -> {
|
||||
inv.<Runnable>getArgument(0).run();
|
||||
return null;
|
||||
}).when(taskExecutor).execute(any(Runnable.class));
|
||||
runAsyncInline();
|
||||
|
||||
service.regenerateCovers(false);
|
||||
|
||||
@@ -904,12 +953,8 @@ class BookCoverServiceTest {
|
||||
BookEntity book = BookEntity.builder().id(1L).metadata(null).build();
|
||||
when(bookQueryService.findAllWithMetadataByIds(any())).thenReturn(List.of(book));
|
||||
|
||||
// Test getUnlockedBookCoverInfos via updateCoverFromFileForBooks (async)
|
||||
doAnswer(inv -> {
|
||||
inv.<Runnable>getArgument(0).run();
|
||||
return null;
|
||||
}).when(taskExecutor).execute(any(Runnable.class));
|
||||
|
||||
runAsyncInline();
|
||||
|
||||
MultipartFile file = mock(MultipartFile.class);
|
||||
when(file.isEmpty()).thenReturn(false);
|
||||
when(file.getSize()).thenReturn(1024L);
|
||||
@@ -1086,6 +1131,29 @@ class BookCoverServiceTest {
|
||||
verify(notificationService).sendMessage(any(), eq(List.of(projection)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendsNotificationAfterCommitNotBefore() {
|
||||
BookEntity book = buildBook(1L, false);
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(book));
|
||||
BookCoverUpdateProjection projection = mock(BookCoverUpdateProjection.class);
|
||||
when(bookRepository.findCoverUpdateInfoByIds(List.of(1L))).thenReturn(List.of(projection));
|
||||
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
try {
|
||||
service.updateCoverFromUrl(1L, "https://example.com/cover.jpg");
|
||||
|
||||
verify(notificationService, never()).sendMessage(any(), anyList());
|
||||
|
||||
List<TransactionSynchronization> syncs = TransactionSynchronizationManager.getSynchronizations();
|
||||
assertThat(syncs).hasSize(1);
|
||||
|
||||
syncs.forEach(TransactionSynchronization::afterCommit);
|
||||
verify(notificationService).sendMessage(any(), eq(List.of(projection)));
|
||||
} finally {
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotSendNotificationWhenNoUpdates() {
|
||||
BookEntity book = buildBook(1L, false);
|
||||
|
||||
Reference in New Issue
Block a user