Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public BookFileEntity getPrimaryBookFile() {
}
return bookFiles
.stream()
.filter(BookFileEntity::isBook)
.min(Comparator.comparingLong(BookFileEntity::getId))
.orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public void deleteAdditionalFile(Long bookId, Long fileId) {

BookFileEntity file = fileOpt.get();
BookEntity book = file.getBook();
validateAdditionalFile(file, book);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are removing this we should move this out of the AdditionalFileService & stop using the additionalFileRepository for it, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess so yeah. depends on if it needs to happen here or a separate PR


try {
monitoringRegistrationService.unregisterSpecificPath(file.getFullFilePath().getParent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void deleteAdditionalFile_WhenFileNotFound_ShouldThrowException() {
}

@Test
void deleteAdditionalFile_WhenFileExists_ShouldDeleteSuccessfully() {
void deleteAdditionalFile_WhenFileIsAlternativeFormat_ShouldDeleteSuccessfully() {
Long bookId = 100L;
Long fileId = 1L;
Path parentPath = fileEntity.getFullFilePath().getParent();
Expand Down Expand Up @@ -309,20 +309,54 @@ void downloadAdditionalFile_WhenEntityRelationshipsMissing_ShouldThrowIllegalSta
}

@Test
void deleteAdditionalFile_WhenFileIsPrimaryBookFile_ShouldThrowException() {
void deleteAdditionalFile_WhenPrimaryHasAlternativeAndSupplementaryFile_ShouldDeleteAndPromoteReadableAlternative() {
Comment thread
alexhb1 marked this conversation as resolved.
Long bookId = 100L;
Long fileId = 1L;
bookEntity.setBookFiles(Set.of(fileEntity));
BookFileEntity supplementaryFile = createBookFile(2L, "notes.txt");
supplementaryFile.setBookFormat(false);
BookFileEntity alternativeFormat = createBookFile(3L, "alternative.epub");
bookEntity.setBookFiles(new HashSet<>(Set.of(fileEntity, supplementaryFile, alternativeFormat)));
Path parentPath = fileEntity.getFullFilePath().getParent();
assertEquals(fileEntity, bookEntity.getPrimaryBookFile());

when(additionalFileRepository.findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId)).thenReturn(Optional.of(fileEntity));

IllegalArgumentException exception = assertThrows(
IllegalArgumentException.class,
() -> additionalFileService.deleteAdditionalFile(bookId, fileId)
);
try (MockedStatic<Files> filesMock = mockStatic(Files.class)) {
filesMock.when(() -> Files.deleteIfExists(fileEntity.getFullFilePath())).thenReturn(true);

assertEquals("Primary book file cannot be processed as an additional file: 1", exception.getMessage());
verify(additionalFileRepository, never()).delete(any());
verify(monitoringRegistrationService, never()).unregisterSpecificPath(any());
additionalFileService.deleteAdditionalFile(bookId, fileId);

verify(monitoringRegistrationService).unregisterSpecificPath(parentPath);
filesMock.verify(() -> Files.deleteIfExists(fileEntity.getFullFilePath()));
verify(additionalFileRepository).delete(fileEntity);
}

assertEquals(alternativeFormat, bookEntity.getPrimaryBookFile());
}

@Test
void deleteAdditionalFile_WhenFileIsSupplementary_ShouldDeleteSuccessfully() {
Long bookId = 100L;
Long fileId = 1L;
fileEntity.setBookFormat(false);
BookFileEntity readableFormat = createBookFile(0L, "book.epub");
bookEntity.setBookFiles(new HashSet<>(Set.of(fileEntity, readableFormat)));
Path parentPath = fileEntity.getFullFilePath().getParent();
assertEquals(readableFormat, bookEntity.getPrimaryBookFile());

when(additionalFileRepository.findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId)).thenReturn(Optional.of(fileEntity));

try (MockedStatic<Files> filesMock = mockStatic(Files.class)) {
filesMock.when(() -> Files.deleteIfExists(fileEntity.getFullFilePath())).thenReturn(true);

additionalFileService.deleteAdditionalFile(bookId, fileId);

verify(monitoringRegistrationService).unregisterSpecificPath(parentPath);
filesMock.verify(() -> Files.deleteIfExists(fileEntity.getFullFilePath()));
verify(additionalFileRepository).delete(fileEntity);
}

assertEquals(readableFormat, bookEntity.getPrimaryBookFile());
}

@Test
Expand Down
Loading