fix(reader): resolve epub preview runtime and audiobook download failures (#463)
* fix(reader): resolve epub preview runtime and audiobook download failures * fix(files): load book library path for additional file downloads * fix(files): scope additional file operations to book-owned non-primary files
This commit is contained in:
+1
-1
@@ -115,4 +115,4 @@ ARG BOOKLORE_PORT=6060
|
|||||||
EXPOSE ${BOOKLORE_PORT}
|
EXPOSE ${BOOKLORE_PORT}
|
||||||
|
|
||||||
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
|
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
|
||||||
CMD ["java", "--enable-native-access=ALL-UNNAMED", "-jar", "/app/app.jar"]
|
CMD ["java", "--enable-native-access=ALL-UNNAMED", "--enable-preview", "-jar", "/app/app.jar"]
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ public class AdditionalFileController {
|
|||||||
public ResponseEntity<Resource> downloadAdditionalFile(
|
public ResponseEntity<Resource> downloadAdditionalFile(
|
||||||
@PathVariable Long bookId,
|
@PathVariable Long bookId,
|
||||||
@PathVariable Long fileId) throws IOException {
|
@PathVariable Long fileId) throws IOException {
|
||||||
return additionalFileService.downloadAdditionalFile(fileId);
|
return additionalFileService.downloadAdditionalFile(bookId, fileId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Operation(
|
@Operation(
|
||||||
@@ -98,7 +98,7 @@ public class AdditionalFileController {
|
|||||||
public ResponseEntity<Void> deleteAdditionalFile(
|
public ResponseEntity<Void> deleteAdditionalFile(
|
||||||
@PathVariable Long bookId,
|
@PathVariable Long bookId,
|
||||||
@PathVariable Long fileId) {
|
@PathVariable Long fileId) {
|
||||||
additionalFileService.deleteAdditionalFile(fileId);
|
additionalFileService.deleteAdditionalFile(bookId, fileId);
|
||||||
return ResponseEntity.noContent().build();
|
return ResponseEntity.noContent().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,20 @@ public interface BookAdditionalFileRepository extends JpaRepository<BookFileEnti
|
|||||||
@Query("SELECT bf FROM BookFileEntity bf WHERE bf.book.library.id = :libraryId")
|
@Query("SELECT bf FROM BookFileEntity bf WHERE bf.book.library.id = :libraryId")
|
||||||
List<BookFileEntity> findByLibraryId(@Param("libraryId") Long libraryId);
|
List<BookFileEntity> findByLibraryId(@Param("libraryId") Long libraryId);
|
||||||
|
|
||||||
|
@Query("""
|
||||||
|
SELECT DISTINCT bf FROM BookFileEntity bf
|
||||||
|
LEFT JOIN FETCH bf.book b
|
||||||
|
LEFT JOIN FETCH b.libraryPath
|
||||||
|
LEFT JOIN FETCH b.library
|
||||||
|
LEFT JOIN FETCH b.bookFiles
|
||||||
|
WHERE bf.id = :id
|
||||||
|
AND b.id = :bookId
|
||||||
|
""")
|
||||||
|
Optional<BookFileEntity> findByIdAndBookIdWithBookAndLibraryPath(
|
||||||
|
@Param("id") Long id,
|
||||||
|
@Param("bookId") Long bookId
|
||||||
|
);
|
||||||
|
|
||||||
@Modifying
|
@Modifying
|
||||||
@Transactional
|
@Transactional
|
||||||
@Query("""
|
@Query("""
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import org.booklore.model.dto.BookFile;
|
|||||||
import org.booklore.model.entity.BookEntity;
|
import org.booklore.model.entity.BookEntity;
|
||||||
import org.booklore.model.entity.BookFileEntity;
|
import org.booklore.model.entity.BookFileEntity;
|
||||||
import org.booklore.repository.BookAdditionalFileRepository;
|
import org.booklore.repository.BookAdditionalFileRepository;
|
||||||
import org.booklore.repository.BookRepository;
|
|
||||||
import org.booklore.service.monitoring.MonitoringRegistrationService;
|
import org.booklore.service.monitoring.MonitoringRegistrationService;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -39,7 +38,6 @@ public class AdditionalFileService {
|
|||||||
private static final Pattern NON_ASCII = Pattern.compile("[^\\x00-\\x7F]");
|
private static final Pattern NON_ASCII = Pattern.compile("[^\\x00-\\x7F]");
|
||||||
|
|
||||||
private final BookAdditionalFileRepository additionalFileRepository;
|
private final BookAdditionalFileRepository additionalFileRepository;
|
||||||
private final BookRepository bookRepository;
|
|
||||||
private final AdditionalFileMapper additionalFileMapper;
|
private final AdditionalFileMapper additionalFileMapper;
|
||||||
private final MonitoringRegistrationService monitoringRegistrationService;
|
private final MonitoringRegistrationService monitoringRegistrationService;
|
||||||
|
|
||||||
@@ -54,14 +52,15 @@ public class AdditionalFileService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteAdditionalFile(Long fileId) {
|
public void deleteAdditionalFile(Long bookId, Long fileId) {
|
||||||
Optional<BookFileEntity> fileOpt = additionalFileRepository.findById(fileId);
|
Optional<BookFileEntity> fileOpt = additionalFileRepository.findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId);
|
||||||
if (fileOpt.isEmpty()) {
|
if (fileOpt.isEmpty()) {
|
||||||
throw new IllegalArgumentException("Additional file not found with id: " + fileId);
|
throw new IllegalArgumentException("Additional file not found with id: " + fileId);
|
||||||
}
|
}
|
||||||
|
|
||||||
BookFileEntity file = fileOpt.get();
|
BookFileEntity file = fileOpt.get();
|
||||||
BookEntity book = file.getBook();
|
BookEntity book = file.getBook();
|
||||||
|
validateAdditionalFile(file, book);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
monitoringRegistrationService.unregisterSpecificPath(file.getFullFilePath().getParent());
|
monitoringRegistrationService.unregisterSpecificPath(file.getFullFilePath().getParent());
|
||||||
@@ -96,13 +95,14 @@ public class AdditionalFileService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResponseEntity<Resource> downloadAdditionalFile(Long fileId) throws IOException {
|
public ResponseEntity<Resource> downloadAdditionalFile(Long bookId, Long fileId) throws IOException {
|
||||||
Optional<BookFileEntity> fileOpt = additionalFileRepository.findById(fileId);
|
Optional<BookFileEntity> fileOpt = additionalFileRepository.findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId);
|
||||||
if (fileOpt.isEmpty()) {
|
if (fileOpt.isEmpty()) {
|
||||||
return ResponseEntity.notFound().build();
|
return ResponseEntity.notFound().build();
|
||||||
}
|
}
|
||||||
|
|
||||||
BookFileEntity file = fileOpt.get();
|
BookFileEntity file = fileOpt.get();
|
||||||
|
validateAdditionalFile(file, file.getBook());
|
||||||
Path filePath = file.getFullFilePath();
|
Path filePath = file.getFullFilePath();
|
||||||
|
|
||||||
if (!Files.exists(filePath)) {
|
if (!Files.exists(filePath)) {
|
||||||
@@ -159,4 +159,10 @@ public class AdditionalFileService {
|
|||||||
.header(HttpHeaders.CONTENT_LENGTH, String.valueOf(zipBytes.length))
|
.header(HttpHeaders.CONTENT_LENGTH, String.valueOf(zipBytes.length))
|
||||||
.body(resource);
|
.body(resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void validateAdditionalFile(BookFileEntity file, BookEntity book) {
|
||||||
|
if (book != null && book.getPrimaryBookFile() != null && file.getId().equals(book.getPrimaryBookFile().getId())) {
|
||||||
|
throw new IllegalArgumentException("Primary book file cannot be processed as an additional file: " + file.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import org.booklore.model.entity.BookEntity;
|
|||||||
import org.booklore.model.entity.BookFileEntity;
|
import org.booklore.model.entity.BookFileEntity;
|
||||||
import org.booklore.model.entity.LibraryPathEntity;
|
import org.booklore.model.entity.LibraryPathEntity;
|
||||||
import org.booklore.repository.BookAdditionalFileRepository;
|
import org.booklore.repository.BookAdditionalFileRepository;
|
||||||
import org.booklore.repository.BookRepository;
|
|
||||||
import org.booklore.service.file.AdditionalFileService;
|
import org.booklore.service.file.AdditionalFileService;
|
||||||
import org.booklore.service.monitoring.MonitoringRegistrationService;
|
import org.booklore.service.monitoring.MonitoringRegistrationService;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
@@ -26,6 +25,7 @@ import org.springframework.http.ResponseEntity;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -39,9 +39,6 @@ class AdditionalFileServiceTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private BookAdditionalFileRepository additionalFileRepository;
|
private BookAdditionalFileRepository additionalFileRepository;
|
||||||
|
|
||||||
@Mock
|
|
||||||
private BookRepository bookRepository;
|
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private AdditionalFileMapper additionalFileMapper;
|
private AdditionalFileMapper additionalFileMapper;
|
||||||
|
|
||||||
@@ -77,6 +74,7 @@ class AdditionalFileServiceTest {
|
|||||||
fileEntity.setFileName("test-file.pdf");
|
fileEntity.setFileName("test-file.pdf");
|
||||||
fileEntity.setFileSubPath(".");
|
fileEntity.setFileSubPath(".");
|
||||||
fileEntity.setBookFormat(true);
|
fileEntity.setBookFormat(true);
|
||||||
|
bookEntity.setBookFiles(new ArrayList<>(List.of(fileEntity)));
|
||||||
|
|
||||||
additionalFile = mock(BookFile.class);
|
additionalFile = mock(BookFile.class);
|
||||||
}
|
}
|
||||||
@@ -149,92 +147,100 @@ class AdditionalFileServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void deleteAdditionalFile_WhenFileNotFound_ShouldThrowException() {
|
void deleteAdditionalFile_WhenFileNotFound_ShouldThrowException() {
|
||||||
|
Long bookId = 100L;
|
||||||
Long fileId = 1L;
|
Long fileId = 1L;
|
||||||
when(additionalFileRepository.findById(fileId)).thenReturn(Optional.empty());
|
when(additionalFileRepository.findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
IllegalArgumentException exception = assertThrows(
|
IllegalArgumentException exception = assertThrows(
|
||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> additionalFileService.deleteAdditionalFile(fileId)
|
() -> additionalFileService.deleteAdditionalFile(bookId, fileId)
|
||||||
);
|
);
|
||||||
|
|
||||||
assertEquals("Additional file not found with id: 1", exception.getMessage());
|
assertEquals("Additional file not found with id: 1", exception.getMessage());
|
||||||
verify(additionalFileRepository).findById(fileId);
|
verify(additionalFileRepository).findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId);
|
||||||
verify(additionalFileRepository, never()).delete(any());
|
verify(additionalFileRepository, never()).delete(any());
|
||||||
verify(monitoringRegistrationService, never()).unregisterSpecificPath(any());
|
verify(monitoringRegistrationService, never()).unregisterSpecificPath(any());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void deleteAdditionalFile_WhenFileExists_ShouldDeleteSuccessfully() {
|
void deleteAdditionalFile_WhenFileExists_ShouldDeleteSuccessfully() {
|
||||||
|
Long bookId = 100L;
|
||||||
Long fileId = 1L;
|
Long fileId = 1L;
|
||||||
Path parentPath = fileEntity.getFullFilePath().getParent();
|
Path parentPath = fileEntity.getFullFilePath().getParent();
|
||||||
|
BookFileEntity primaryFile = createBookFile(2L, "primary.epub");
|
||||||
|
bookEntity.setBookFiles(new ArrayList<>(List.of(primaryFile, fileEntity)));
|
||||||
|
|
||||||
when(additionalFileRepository.findById(fileId)).thenReturn(Optional.of(fileEntity));
|
when(additionalFileRepository.findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId)).thenReturn(Optional.of(fileEntity));
|
||||||
|
|
||||||
try (MockedStatic<Files> filesMock = mockStatic(Files.class)) {
|
try (MockedStatic<Files> filesMock = mockStatic(Files.class)) {
|
||||||
filesMock.when(() -> Files.deleteIfExists(fileEntity.getFullFilePath())).thenReturn(true);
|
filesMock.when(() -> Files.deleteIfExists(fileEntity.getFullFilePath())).thenReturn(true);
|
||||||
|
|
||||||
additionalFileService.deleteAdditionalFile(fileId);
|
additionalFileService.deleteAdditionalFile(bookId, fileId);
|
||||||
|
|
||||||
verify(additionalFileRepository).findById(fileId);
|
verify(additionalFileRepository).findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId);
|
||||||
verify(monitoringRegistrationService).unregisterSpecificPath(parentPath);
|
verify(monitoringRegistrationService).unregisterSpecificPath(parentPath);
|
||||||
filesMock.verify(() -> Files.deleteIfExists(fileEntity.getFullFilePath()));
|
filesMock.verify(() -> Files.deleteIfExists(fileEntity.getFullFilePath()));
|
||||||
verify(additionalFileRepository).delete(fileEntity);
|
verify(additionalFileRepository).delete(fileEntity);
|
||||||
verify(bookRepository, never()).save(any());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void deleteAdditionalFile_WhenIOExceptionOccurs_ShouldStillDeleteFromRepository() {
|
void deleteAdditionalFile_WhenIOExceptionOccurs_ShouldStillDeleteFromRepository() {
|
||||||
|
Long bookId = 100L;
|
||||||
Long fileId = 1L;
|
Long fileId = 1L;
|
||||||
Path parentPath = fileEntity.getFullFilePath().getParent();
|
Path parentPath = fileEntity.getFullFilePath().getParent();
|
||||||
|
BookFileEntity primaryFile = createBookFile(2L, "primary.epub");
|
||||||
|
bookEntity.setBookFiles(new ArrayList<>(List.of(primaryFile, fileEntity)));
|
||||||
|
|
||||||
when(additionalFileRepository.findById(fileId)).thenReturn(Optional.of(fileEntity));
|
when(additionalFileRepository.findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId)).thenReturn(Optional.of(fileEntity));
|
||||||
|
|
||||||
try (MockedStatic<Files> filesMock = mockStatic(Files.class)) {
|
try (MockedStatic<Files> filesMock = mockStatic(Files.class)) {
|
||||||
filesMock.when(() -> Files.deleteIfExists(fileEntity.getFullFilePath())).thenThrow(new IOException("File access error"));
|
filesMock.when(() -> Files.deleteIfExists(fileEntity.getFullFilePath())).thenThrow(new IOException("File access error"));
|
||||||
|
|
||||||
additionalFileService.deleteAdditionalFile(fileId);
|
additionalFileService.deleteAdditionalFile(bookId, fileId);
|
||||||
|
|
||||||
verify(additionalFileRepository).findById(fileId);
|
verify(additionalFileRepository).findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId);
|
||||||
verify(monitoringRegistrationService).unregisterSpecificPath(parentPath);
|
verify(monitoringRegistrationService).unregisterSpecificPath(parentPath);
|
||||||
filesMock.verify(() -> Files.deleteIfExists(fileEntity.getFullFilePath()));
|
filesMock.verify(() -> Files.deleteIfExists(fileEntity.getFullFilePath()));
|
||||||
verify(additionalFileRepository).delete(fileEntity);
|
verify(additionalFileRepository).delete(fileEntity);
|
||||||
verify(bookRepository, never()).save(any());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void deleteAdditionalFile_WhenEntityRelationshipsMissing_ShouldThrowIllegalStateException() {
|
void deleteAdditionalFile_WhenEntityRelationshipsMissing_ShouldThrowIllegalStateException() {
|
||||||
|
Long bookId = 100L;
|
||||||
Long fileId = 1L;
|
Long fileId = 1L;
|
||||||
BookFileEntity invalidEntity = new BookFileEntity();
|
BookFileEntity invalidEntity = new BookFileEntity();
|
||||||
invalidEntity.setId(fileId);
|
invalidEntity.setId(fileId);
|
||||||
|
|
||||||
when(additionalFileRepository.findById(fileId)).thenReturn(Optional.of(invalidEntity));
|
when(additionalFileRepository.findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId)).thenReturn(Optional.of(invalidEntity));
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(
|
||||||
IllegalStateException.class,
|
IllegalStateException.class,
|
||||||
() -> additionalFileService.deleteAdditionalFile(fileId)
|
() -> additionalFileService.deleteAdditionalFile(bookId, fileId)
|
||||||
);
|
);
|
||||||
|
|
||||||
verify(additionalFileRepository).findById(fileId);
|
verify(additionalFileRepository).findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId);
|
||||||
verify(additionalFileRepository, never()).delete(any());
|
verify(additionalFileRepository, never()).delete(any());
|
||||||
verify(monitoringRegistrationService, never()).unregisterSpecificPath(any());
|
verify(monitoringRegistrationService, never()).unregisterSpecificPath(any());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void downloadAdditionalFile_WhenFileNotFound_ShouldReturnNotFound() throws IOException {
|
void downloadAdditionalFile_WhenFileNotFound_ShouldReturnNotFound() throws IOException {
|
||||||
|
Long bookId = 100L;
|
||||||
Long fileId = 1L;
|
Long fileId = 1L;
|
||||||
when(additionalFileRepository.findById(fileId)).thenReturn(Optional.empty());
|
when(additionalFileRepository.findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
ResponseEntity<Resource> result = additionalFileService.downloadAdditionalFile(fileId);
|
ResponseEntity<Resource> result = additionalFileService.downloadAdditionalFile(bookId, fileId);
|
||||||
|
|
||||||
assertEquals(HttpStatus.NOT_FOUND, result.getStatusCode());
|
assertEquals(HttpStatus.NOT_FOUND, result.getStatusCode());
|
||||||
assertNull(result.getBody());
|
assertNull(result.getBody());
|
||||||
verify(additionalFileRepository).findById(fileId);
|
verify(additionalFileRepository).findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void downloadAdditionalFile_WhenPhysicalFileNotExists_ShouldReturnNotFound() throws IOException {
|
void downloadAdditionalFile_WhenPhysicalFileNotExists_ShouldReturnNotFound() throws IOException {
|
||||||
|
Long bookId = 100L;
|
||||||
Long fileId = 1L;
|
Long fileId = 1L;
|
||||||
|
|
||||||
BookFileEntity entityWithNonExistentFile = new BookFileEntity();
|
BookFileEntity entityWithNonExistentFile = new BookFileEntity();
|
||||||
@@ -242,31 +248,36 @@ class AdditionalFileServiceTest {
|
|||||||
entityWithNonExistentFile.setBook(bookEntity);
|
entityWithNonExistentFile.setBook(bookEntity);
|
||||||
entityWithNonExistentFile.setFileName("non-existent.pdf");
|
entityWithNonExistentFile.setFileName("non-existent.pdf");
|
||||||
entityWithNonExistentFile.setFileSubPath(".");
|
entityWithNonExistentFile.setFileSubPath(".");
|
||||||
|
BookFileEntity primaryFile = createBookFile(2L, "primary.epub");
|
||||||
|
bookEntity.setBookFiles(new ArrayList<>(List.of(primaryFile, entityWithNonExistentFile)));
|
||||||
|
|
||||||
when(additionalFileRepository.findById(fileId)).thenReturn(Optional.of(entityWithNonExistentFile));
|
when(additionalFileRepository.findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId)).thenReturn(Optional.of(entityWithNonExistentFile));
|
||||||
|
|
||||||
try (MockedStatic<Files> filesMock = mockStatic(Files.class)) {
|
try (MockedStatic<Files> filesMock = mockStatic(Files.class)) {
|
||||||
Path actualPath = entityWithNonExistentFile.getFullFilePath();
|
Path actualPath = entityWithNonExistentFile.getFullFilePath();
|
||||||
filesMock.when(() -> Files.exists(actualPath)).thenReturn(false);
|
filesMock.when(() -> Files.exists(actualPath)).thenReturn(false);
|
||||||
|
|
||||||
ResponseEntity<Resource> result = additionalFileService.downloadAdditionalFile(fileId);
|
ResponseEntity<Resource> result = additionalFileService.downloadAdditionalFile(bookId, fileId);
|
||||||
|
|
||||||
assertEquals(HttpStatus.NOT_FOUND, result.getStatusCode());
|
assertEquals(HttpStatus.NOT_FOUND, result.getStatusCode());
|
||||||
assertNull(result.getBody());
|
assertNull(result.getBody());
|
||||||
verify(additionalFileRepository).findById(fileId);
|
verify(additionalFileRepository).findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId);
|
||||||
filesMock.verify(() -> Files.exists(actualPath));
|
filesMock.verify(() -> Files.exists(actualPath));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void downloadAdditionalFile_WhenFileExists_ShouldReturnFileResource() throws Exception {
|
void downloadAdditionalFile_WhenFileExists_ShouldReturnFileResource() throws Exception {
|
||||||
|
Long bookId = 100L;
|
||||||
Long fileId = 1L;
|
Long fileId = 1L;
|
||||||
when(additionalFileRepository.findById(fileId)).thenReturn(Optional.of(fileEntity));
|
BookFileEntity primaryFile = createBookFile(2L, "primary.epub");
|
||||||
|
bookEntity.setBookFiles(new ArrayList<>(List.of(primaryFile, fileEntity)));
|
||||||
|
when(additionalFileRepository.findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId)).thenReturn(Optional.of(fileEntity));
|
||||||
|
|
||||||
try (MockedStatic<Files> filesMock = mockStatic(Files.class)) {
|
try (MockedStatic<Files> filesMock = mockStatic(Files.class)) {
|
||||||
filesMock.when(() -> Files.exists(fileEntity.getFullFilePath())).thenReturn(true);
|
filesMock.when(() -> Files.exists(fileEntity.getFullFilePath())).thenReturn(true);
|
||||||
|
|
||||||
ResponseEntity<Resource> result = additionalFileService.downloadAdditionalFile(fileId);
|
ResponseEntity<Resource> result = additionalFileService.downloadAdditionalFile(bookId, fileId);
|
||||||
|
|
||||||
assertEquals(HttpStatus.OK, result.getStatusCode());
|
assertEquals(HttpStatus.OK, result.getStatusCode());
|
||||||
assertNotNull(result.getBody());
|
assertNotNull(result.getBody());
|
||||||
@@ -274,24 +285,67 @@ class AdditionalFileServiceTest {
|
|||||||
assertTrue(result.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION).contains("test-file.pdf"));
|
assertTrue(result.getHeaders().getFirst(HttpHeaders.CONTENT_DISPOSITION).contains("test-file.pdf"));
|
||||||
assertEquals(MediaType.APPLICATION_OCTET_STREAM, result.getHeaders().getContentType());
|
assertEquals(MediaType.APPLICATION_OCTET_STREAM, result.getHeaders().getContentType());
|
||||||
|
|
||||||
verify(additionalFileRepository).findById(fileId);
|
verify(additionalFileRepository).findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId);
|
||||||
filesMock.verify(() -> Files.exists(fileEntity.getFullFilePath()));
|
filesMock.verify(() -> Files.exists(fileEntity.getFullFilePath()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void downloadAdditionalFile_WhenEntityRelationshipsMissing_ShouldThrowIllegalStateException() {
|
void downloadAdditionalFile_WhenEntityRelationshipsMissing_ShouldThrowIllegalStateException() {
|
||||||
|
Long bookId = 100L;
|
||||||
Long fileId = 1L;
|
Long fileId = 1L;
|
||||||
BookFileEntity invalidEntity = new BookFileEntity();
|
BookFileEntity invalidEntity = new BookFileEntity();
|
||||||
invalidEntity.setId(fileId);
|
invalidEntity.setId(fileId);
|
||||||
|
|
||||||
when(additionalFileRepository.findById(fileId)).thenReturn(Optional.of(invalidEntity));
|
when(additionalFileRepository.findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId)).thenReturn(Optional.of(invalidEntity));
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(
|
||||||
IllegalStateException.class,
|
IllegalStateException.class,
|
||||||
() -> additionalFileService.downloadAdditionalFile(fileId)
|
() -> additionalFileService.downloadAdditionalFile(bookId, fileId)
|
||||||
);
|
);
|
||||||
|
|
||||||
verify(additionalFileRepository).findById(fileId);
|
verify(additionalFileRepository).findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void deleteAdditionalFile_WhenFileIsPrimaryBookFile_ShouldThrowException() {
|
||||||
|
Long bookId = 100L;
|
||||||
|
Long fileId = 1L;
|
||||||
|
bookEntity.setBookFiles(new ArrayList<>(List.of(fileEntity)));
|
||||||
|
when(additionalFileRepository.findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId)).thenReturn(Optional.of(fileEntity));
|
||||||
|
|
||||||
|
IllegalArgumentException exception = assertThrows(
|
||||||
|
IllegalArgumentException.class,
|
||||||
|
() -> additionalFileService.deleteAdditionalFile(bookId, fileId)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals("Primary book file cannot be processed as an additional file: 1", exception.getMessage());
|
||||||
|
verify(additionalFileRepository, never()).delete(any());
|
||||||
|
verify(monitoringRegistrationService, never()).unregisterSpecificPath(any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void downloadAdditionalFile_WhenFileIsPrimaryBookFile_ShouldThrowException() {
|
||||||
|
Long bookId = 100L;
|
||||||
|
Long fileId = 1L;
|
||||||
|
bookEntity.setBookFiles(new ArrayList<>(List.of(fileEntity)));
|
||||||
|
when(additionalFileRepository.findByIdAndBookIdWithBookAndLibraryPath(fileId, bookId)).thenReturn(Optional.of(fileEntity));
|
||||||
|
|
||||||
|
IllegalArgumentException exception = assertThrows(
|
||||||
|
IllegalArgumentException.class,
|
||||||
|
() -> additionalFileService.downloadAdditionalFile(bookId, fileId)
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals("Primary book file cannot be processed as an additional file: 1", exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
private BookFileEntity createBookFile(Long id, String fileName) {
|
||||||
|
BookFileEntity entity = new BookFileEntity();
|
||||||
|
entity.setId(id);
|
||||||
|
entity.setBook(bookEntity);
|
||||||
|
entity.setFileName(fileName);
|
||||||
|
entity.setFileSubPath(".");
|
||||||
|
entity.setBookFormat(true);
|
||||||
|
return entity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user