fix(api): book files cannot be left in a detached state (#2389)
This commit is contained in:
@@ -100,8 +100,8 @@ public class BookFileDetachmentService {
|
||||
sourceBook.getBookFiles().remove(targetFile);
|
||||
newBook.getBookFiles().add(targetFile);
|
||||
|
||||
bookRepository.saveAndFlush(sourceBook);
|
||||
newBook = bookRepository.saveAndFlush(newBook);
|
||||
bookRepository.saveAndFlush(sourceBook);
|
||||
|
||||
// Only save changes to the target file after the shift in relationship
|
||||
// via source & new book or else we get a hibernate error.
|
||||
|
||||
@@ -20,6 +20,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.InOrder;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
@@ -187,6 +188,35 @@ class BookFileDetachmentServiceTest {
|
||||
verify(bookFileRepository).saveAndFlush(any(BookFileEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void detachSupplementaryFile_CorrectOrder() {
|
||||
BookEntity oldBook = createBook(1L);
|
||||
BookEntity newBook = createBook(2L);
|
||||
createBookFile(10L, oldBook, true, BookFileType.EPUB);
|
||||
BookFileEntity suppFile = createBookFile(11L, oldBook, false, BookFileType.PDF);
|
||||
suppFile.setFileName("notes.txt");
|
||||
|
||||
when(bookRepository.findByIdWithBookFiles(1L)).thenReturn(Optional.of(oldBook));
|
||||
when(bookRepository.saveAndFlush(any(BookEntity.class))).thenAnswer(inv -> {
|
||||
BookEntity saved = inv.getArgument(0);
|
||||
if (saved.getId() == null) {
|
||||
saved.setId(2L);
|
||||
}
|
||||
return saved;
|
||||
});
|
||||
|
||||
setupMocksForGetUpdatedBook();
|
||||
when(bookRepository.findByIdWithBookFiles(2L)).thenReturn(Optional.of(newBook));
|
||||
|
||||
service.detachBookFile(1L, 11L, false);
|
||||
|
||||
//create inOrder object passing any mocks that need to be verified in order
|
||||
InOrder inOrder = inOrder(bookRepository);
|
||||
|
||||
inOrder.verify(bookRepository).saveAndFlush(newBook);
|
||||
inOrder.verify(bookRepository).saveAndFlush(oldBook);
|
||||
}
|
||||
|
||||
@Test
|
||||
void detachPrimaryFile_alternativeGetsPromoted() {
|
||||
BookEntity book = createBook(1L);
|
||||
|
||||
Reference in New Issue
Block a user