Fix/kobo library snapshot (#2051)
* Fix Kobo snapshot ownership and clean existing rows * Add Kobo snapshot ownership and clean existing rows test.
This commit is contained in:
+18
-1
@@ -1,6 +1,8 @@
|
|||||||
package com.adityachandel.booklore.service.kobo;
|
package com.adityachandel.booklore.service.kobo;
|
||||||
|
|
||||||
import com.adityachandel.booklore.mapper.BookEntityToKoboSnapshotBookMapper;
|
import com.adityachandel.booklore.mapper.BookEntityToKoboSnapshotBookMapper;
|
||||||
|
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||||
|
import com.adityachandel.booklore.model.entity.BookLoreUserEntity;
|
||||||
import com.adityachandel.booklore.model.entity.KoboDeletedBookProgressEntity;
|
import com.adityachandel.booklore.model.entity.KoboDeletedBookProgressEntity;
|
||||||
import com.adityachandel.booklore.model.entity.KoboSnapshotBookEntity;
|
import com.adityachandel.booklore.model.entity.KoboSnapshotBookEntity;
|
||||||
import com.adityachandel.booklore.model.entity.ShelfEntity;
|
import com.adityachandel.booklore.model.entity.ShelfEntity;
|
||||||
@@ -117,7 +119,10 @@ public class KoboLibrarySnapshotService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<KoboSnapshotBookEntity> mapBooksToKoboSnapshotBook(ShelfEntity shelf, KoboLibrarySnapshotEntity snapshot) {
|
private List<KoboSnapshotBookEntity> mapBooksToKoboSnapshotBook(ShelfEntity shelf, KoboLibrarySnapshotEntity snapshot) {
|
||||||
|
Long userId = snapshot.getUserId();
|
||||||
|
|
||||||
return shelf.getBookEntities().stream()
|
return shelf.getBookEntities().stream()
|
||||||
|
.filter(book -> isBookOwnedByUser(book, userId))
|
||||||
.filter(koboCompatibilityService::isBookSupportedForKobo)
|
.filter(koboCompatibilityService::isBookSupportedForKobo)
|
||||||
.map(book -> {
|
.map(book -> {
|
||||||
KoboSnapshotBookEntity snapshotBook = mapper.toKoboSnapshotBook(book);
|
KoboSnapshotBookEntity snapshotBook = mapper.toKoboSnapshotBook(book);
|
||||||
@@ -127,8 +132,20 @@ public class KoboLibrarySnapshotService {
|
|||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isBookOwnedByUser(BookEntity book, Long userId) {
|
||||||
|
if (book == null || book.getLibrary() == null || book.getLibrary().getUsers() == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return book.getLibrary()
|
||||||
|
.getUsers()
|
||||||
|
.stream()
|
||||||
|
.map(BookLoreUserEntity::getId)
|
||||||
|
.anyMatch(id -> Objects.equals(id, userId));
|
||||||
|
}
|
||||||
|
|
||||||
public void deleteById(String id) {
|
public void deleteById(String id) {
|
||||||
koboLibrarySnapshotRepository.deleteById(id);
|
koboLibrarySnapshotRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
DELETE ksb
|
||||||
|
FROM kobo_library_snapshot_book ksb
|
||||||
|
JOIN kobo_library_snapshot ks ON ks.id = ksb.snapshot_id
|
||||||
|
LEFT JOIN book b ON b.id = ksb.book_id
|
||||||
|
LEFT JOIN user_library_mapping ulm
|
||||||
|
ON ulm.library_id = b.library_id
|
||||||
|
AND ulm.user_id = ks.user_id
|
||||||
|
WHERE ulm.user_id IS NULL;
|
||||||
+138
@@ -0,0 +1,138 @@
|
|||||||
|
package com.adityachandel.booklore.service.kobo;
|
||||||
|
|
||||||
|
import com.adityachandel.booklore.mapper.BookEntityToKoboSnapshotBookMapper;
|
||||||
|
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||||
|
import com.adityachandel.booklore.model.entity.BookLoreUserEntity;
|
||||||
|
import com.adityachandel.booklore.model.entity.KoboLibrarySnapshotEntity;
|
||||||
|
import com.adityachandel.booklore.model.entity.KoboSnapshotBookEntity;
|
||||||
|
import com.adityachandel.booklore.model.entity.LibraryEntity;
|
||||||
|
import com.adityachandel.booklore.model.entity.ShelfEntity;
|
||||||
|
import com.adityachandel.booklore.model.enums.ShelfType;
|
||||||
|
import com.adityachandel.booklore.repository.KoboDeletedBookProgressRepository;
|
||||||
|
import com.adityachandel.booklore.repository.KoboLibrarySnapshotRepository;
|
||||||
|
import com.adityachandel.booklore.repository.KoboSnapshotBookRepository;
|
||||||
|
import com.adityachandel.booklore.repository.ShelfRepository;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.doAnswer;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class KoboLibrarySnapshotServiceTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private KoboLibrarySnapshotRepository koboLibrarySnapshotRepository;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private KoboSnapshotBookRepository koboSnapshotBookRepository;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private ShelfRepository shelfRepository;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private BookEntityToKoboSnapshotBookMapper mapper;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private KoboDeletedBookProgressRepository koboDeletedBookProgressRepository;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private KoboCompatibilityService koboCompatibilityService;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private KoboLibrarySnapshotService service;
|
||||||
|
|
||||||
|
private BookLoreUserEntity owner;
|
||||||
|
private BookLoreUserEntity otherUser;
|
||||||
|
private BookEntity ownersBook;
|
||||||
|
private BookEntity otherUsersBook;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
owner = BookLoreUserEntity.builder().id(1L).build();
|
||||||
|
otherUser = BookLoreUserEntity.builder().id(2L).build();
|
||||||
|
|
||||||
|
LibraryEntity ownersLibrary = LibraryEntity.builder()
|
||||||
|
.users(List.of(owner))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
LibraryEntity othersLibrary = LibraryEntity.builder()
|
||||||
|
.users(List.of(otherUser))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
ownersBook = BookEntity.builder()
|
||||||
|
.id(101L)
|
||||||
|
.library(ownersLibrary)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
otherUsersBook = BookEntity.builder()
|
||||||
|
.id(202L)
|
||||||
|
.library(othersLibrary)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void create_shouldIncludeOnlyBooksOwnedBySnapshotUser() {
|
||||||
|
ShelfEntity shelf = ShelfEntity.builder()
|
||||||
|
.name(ShelfType.KOBO.getName())
|
||||||
|
.bookEntities(Set.of(ownersBook, otherUsersBook))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
when(shelfRepository.findByUserIdAndName(eq(owner.getId()), eq(ShelfType.KOBO.getName())))
|
||||||
|
.thenReturn(Optional.of(shelf));
|
||||||
|
|
||||||
|
when(koboCompatibilityService.isBookSupportedForKobo(any())).thenReturn(true);
|
||||||
|
|
||||||
|
doAnswer(invocation -> {
|
||||||
|
BookEntity book = invocation.getArgument(0);
|
||||||
|
return KoboSnapshotBookEntity.builder().bookId(book.getId()).build();
|
||||||
|
}).when(mapper).toKoboSnapshotBook(any(BookEntity.class));
|
||||||
|
|
||||||
|
ArgumentCaptor<KoboLibrarySnapshotEntity> snapshotCaptor = ArgumentCaptor.forClass(KoboLibrarySnapshotEntity.class);
|
||||||
|
when(koboLibrarySnapshotRepository.save(snapshotCaptor.capture()))
|
||||||
|
.thenAnswer(invocation -> invocation.getArgument(0));
|
||||||
|
|
||||||
|
KoboLibrarySnapshotEntity created = service.create(owner.getId());
|
||||||
|
|
||||||
|
assertThat(created.getUserId()).isEqualTo(owner.getId());
|
||||||
|
assertThat(created.getBooks()).extracting(KoboSnapshotBookEntity::getBookId)
|
||||||
|
.containsExactly(ownersBook.getId());
|
||||||
|
assertThat(created.getBooks()).allMatch(book -> created.equals(book.getSnapshot()));
|
||||||
|
|
||||||
|
KoboLibrarySnapshotEntity saved = snapshotCaptor.getValue();
|
||||||
|
assertThat(saved.getBooks()).extracting(KoboSnapshotBookEntity::getBookId)
|
||||||
|
.containsExactly(ownersBook.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void create_shouldSkipOwnedBooksThatAreIncompatibleWithKobo() {
|
||||||
|
ShelfEntity shelf = ShelfEntity.builder()
|
||||||
|
.name(ShelfType.KOBO.getName())
|
||||||
|
.bookEntities(Set.of(ownersBook))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
when(shelfRepository.findByUserIdAndName(eq(owner.getId()), eq(ShelfType.KOBO.getName())))
|
||||||
|
.thenReturn(Optional.of(shelf));
|
||||||
|
|
||||||
|
when(koboCompatibilityService.isBookSupportedForKobo(ownersBook)).thenReturn(false);
|
||||||
|
|
||||||
|
when(koboLibrarySnapshotRepository.save(any(KoboLibrarySnapshotEntity.class)))
|
||||||
|
.thenAnswer(invocation -> invocation.getArgument(0));
|
||||||
|
|
||||||
|
KoboLibrarySnapshotEntity created = service.create(owner.getId());
|
||||||
|
|
||||||
|
assertThat(created.getBooks()).isEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user