Automatically add newly added books to Kobo shelf (#1826)
This commit is contained in:
+268
@@ -0,0 +1,268 @@
|
||||
package com.adityachandel.booklore.service.kobo;
|
||||
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
import com.adityachandel.booklore.model.entity.BookLoreUserEntity;
|
||||
import com.adityachandel.booklore.model.entity.KoboUserSettingsEntity;
|
||||
import com.adityachandel.booklore.model.entity.ShelfEntity;
|
||||
import com.adityachandel.booklore.model.enums.ShelfType;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.repository.KoboUserSettingsRepository;
|
||||
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.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class KoboAutoShelfServiceTest {
|
||||
|
||||
@Mock
|
||||
private KoboUserSettingsRepository koboUserSettingsRepository;
|
||||
|
||||
@Mock
|
||||
private ShelfRepository shelfRepository;
|
||||
|
||||
@Mock
|
||||
private BookRepository bookRepository;
|
||||
|
||||
@Mock
|
||||
private KoboCompatibilityService koboCompatibilityService;
|
||||
|
||||
@InjectMocks
|
||||
private KoboAutoShelfService koboAutoShelfService;
|
||||
|
||||
private BookEntity testBook;
|
||||
private BookLoreUserEntity testUser1;
|
||||
private BookLoreUserEntity testUser2;
|
||||
private ShelfEntity koboShelf1;
|
||||
private ShelfEntity koboShelf2;
|
||||
private KoboUserSettingsEntity settings1;
|
||||
private KoboUserSettingsEntity settings2;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
testBook = BookEntity.builder()
|
||||
.id(1L)
|
||||
.shelves(new HashSet<>())
|
||||
.build();
|
||||
|
||||
testUser1 = BookLoreUserEntity.builder()
|
||||
.id(100L)
|
||||
.build();
|
||||
|
||||
testUser2 = BookLoreUserEntity.builder()
|
||||
.id(200L)
|
||||
.build();
|
||||
|
||||
koboShelf1 = ShelfEntity.builder()
|
||||
.id(10L)
|
||||
.name(ShelfType.KOBO.getName())
|
||||
.user(testUser1)
|
||||
.build();
|
||||
|
||||
koboShelf2 = ShelfEntity.builder()
|
||||
.id(20L)
|
||||
.name(ShelfType.KOBO.getName())
|
||||
.user(testUser2)
|
||||
.build();
|
||||
|
||||
settings1 = KoboUserSettingsEntity.builder()
|
||||
.userId(100L)
|
||||
.autoAddToShelf(true)
|
||||
.syncEnabled(true)
|
||||
.build();
|
||||
|
||||
settings2 = KoboUserSettingsEntity.builder()
|
||||
.userId(200L)
|
||||
.autoAddToShelf(true)
|
||||
.syncEnabled(true)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoAddBookToKoboShelves_withNullBookId_shouldReturnEarly() {
|
||||
koboAutoShelfService.autoAddBookToKoboShelves(null);
|
||||
|
||||
verify(bookRepository, never()).findById(anyLong());
|
||||
verify(koboUserSettingsRepository, never()).findByAutoAddToShelfTrueAndSyncEnabledTrue();
|
||||
verify(shelfRepository, never()).findByUserIdInAndName(anyList(), anyString());
|
||||
verify(bookRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoAddBookToKoboShelves_withNonExistentBook_shouldReturnEarly() {
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.empty());
|
||||
|
||||
koboAutoShelfService.autoAddBookToKoboShelves(1L);
|
||||
|
||||
verify(bookRepository).findById(1L);
|
||||
verify(koboUserSettingsRepository, never()).findByAutoAddToShelfTrueAndSyncEnabledTrue();
|
||||
verify(shelfRepository, never()).findByUserIdInAndName(anyList(), anyString());
|
||||
verify(bookRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoAddBookToKoboShelves_withIncompatibleBook_shouldReturnEarly() {
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(testBook));
|
||||
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(false);
|
||||
|
||||
koboAutoShelfService.autoAddBookToKoboShelves(1L);
|
||||
|
||||
verify(bookRepository).findById(1L);
|
||||
verify(koboCompatibilityService).isBookSupportedForKobo(testBook);
|
||||
verify(koboUserSettingsRepository, never()).findByAutoAddToShelfTrueAndSyncEnabledTrue();
|
||||
verify(shelfRepository, never()).findByUserIdInAndName(anyList(), anyString());
|
||||
verify(bookRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoAddBookToKoboShelves_withNoEligibleUsers_shouldReturnEarly() {
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(testBook));
|
||||
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
||||
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
||||
.thenReturn(Collections.emptyList());
|
||||
|
||||
koboAutoShelfService.autoAddBookToKoboShelves(1L);
|
||||
|
||||
verify(bookRepository).findById(1L);
|
||||
verify(koboCompatibilityService).isBookSupportedForKobo(testBook);
|
||||
verify(koboUserSettingsRepository).findByAutoAddToShelfTrueAndSyncEnabledTrue();
|
||||
verify(shelfRepository, never()).findByUserIdInAndName(anyList(), anyString());
|
||||
verify(bookRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoAddBookToKoboShelves_successfully_shouldAddBookToShelves() {
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(testBook));
|
||||
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
||||
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
||||
.thenReturn(List.of(settings1, settings2));
|
||||
when(shelfRepository.findByUserIdInAndName(List.of(100L, 200L), ShelfType.KOBO.getName()))
|
||||
.thenReturn(List.of(koboShelf1, koboShelf2));
|
||||
|
||||
koboAutoShelfService.autoAddBookToKoboShelves(1L);
|
||||
|
||||
verify(bookRepository).findById(1L);
|
||||
verify(koboCompatibilityService).isBookSupportedForKobo(testBook);
|
||||
verify(koboUserSettingsRepository).findByAutoAddToShelfTrueAndSyncEnabledTrue();
|
||||
verify(shelfRepository).findByUserIdInAndName(List.of(100L, 200L), ShelfType.KOBO.getName());
|
||||
verify(bookRepository).save(testBook);
|
||||
|
||||
assert testBook.getShelves().contains(koboShelf1);
|
||||
assert testBook.getShelves().contains(koboShelf2);
|
||||
assert testBook.getShelves().size() == 2;
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoAddBookToKoboShelves_withOneUserMissingShelf_shouldAddOnlyToExistingShelves() {
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(testBook));
|
||||
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
||||
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
||||
.thenReturn(List.of(settings1, settings2));
|
||||
when(shelfRepository.findByUserIdInAndName(List.of(100L, 200L), ShelfType.KOBO.getName()))
|
||||
.thenReturn(List.of(koboShelf1));
|
||||
|
||||
koboAutoShelfService.autoAddBookToKoboShelves(1L);
|
||||
|
||||
verify(bookRepository).save(testBook);
|
||||
assert testBook.getShelves().contains(koboShelf1);
|
||||
assert !testBook.getShelves().contains(koboShelf2);
|
||||
assert testBook.getShelves().size() == 1;
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoAddBookToKoboShelves_withBookAlreadyOnShelf_shouldNotDuplicate() {
|
||||
testBook.getShelves().add(koboShelf1);
|
||||
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(testBook));
|
||||
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
||||
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
||||
.thenReturn(List.of(settings1, settings2));
|
||||
when(shelfRepository.findByUserIdInAndName(List.of(100L, 200L), ShelfType.KOBO.getName()))
|
||||
.thenReturn(List.of(koboShelf1, koboShelf2));
|
||||
|
||||
koboAutoShelfService.autoAddBookToKoboShelves(1L);
|
||||
|
||||
verify(bookRepository).save(testBook);
|
||||
assert testBook.getShelves().contains(koboShelf1);
|
||||
assert testBook.getShelves().contains(koboShelf2);
|
||||
assert testBook.getShelves().size() == 2;
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoAddBookToKoboShelves_withBookOnAllShelves_shouldNotSave() {
|
||||
testBook.getShelves().add(koboShelf1);
|
||||
testBook.getShelves().add(koboShelf2);
|
||||
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(testBook));
|
||||
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
||||
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
||||
.thenReturn(List.of(settings1, settings2));
|
||||
when(shelfRepository.findByUserIdInAndName(List.of(100L, 200L), ShelfType.KOBO.getName()))
|
||||
.thenReturn(List.of(koboShelf1, koboShelf2));
|
||||
|
||||
koboAutoShelfService.autoAddBookToKoboShelves(1L);
|
||||
|
||||
verify(bookRepository, never()).save(any());
|
||||
assert testBook.getShelves().size() == 2;
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoAddBookToKoboShelves_withNoKoboShelvesFound_shouldNotSave() {
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(testBook));
|
||||
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
||||
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
||||
.thenReturn(List.of(settings1, settings2));
|
||||
when(shelfRepository.findByUserIdInAndName(List.of(100L, 200L), ShelfType.KOBO.getName()))
|
||||
.thenReturn(Collections.emptyList());
|
||||
|
||||
koboAutoShelfService.autoAddBookToKoboShelves(1L);
|
||||
|
||||
verify(shelfRepository).findByUserIdInAndName(List.of(100L, 200L), ShelfType.KOBO.getName());
|
||||
verify(bookRepository, never()).save(any());
|
||||
assert testBook.getShelves().isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoAddBookToKoboShelves_withSingleUser_shouldAddToSingleShelf() {
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(testBook));
|
||||
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
||||
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
||||
.thenReturn(List.of(settings1));
|
||||
when(shelfRepository.findByUserIdInAndName(List.of(100L), ShelfType.KOBO.getName()))
|
||||
.thenReturn(List.of(koboShelf1));
|
||||
|
||||
koboAutoShelfService.autoAddBookToKoboShelves(1L);
|
||||
|
||||
verify(bookRepository).save(testBook);
|
||||
assert testBook.getShelves().contains(koboShelf1);
|
||||
assert testBook.getShelves().size() == 1;
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoAddBookToKoboShelves_withNullShelves_shouldInitializeAndAdd() {
|
||||
testBook = BookEntity.builder()
|
||||
.id(1L)
|
||||
.shelves(null)
|
||||
.build();
|
||||
|
||||
when(bookRepository.findById(1L)).thenReturn(Optional.of(testBook));
|
||||
when(koboCompatibilityService.isBookSupportedForKobo(testBook)).thenReturn(true);
|
||||
when(koboUserSettingsRepository.findByAutoAddToShelfTrueAndSyncEnabledTrue())
|
||||
.thenReturn(List.of(settings1));
|
||||
when(shelfRepository.findByUserIdInAndName(List.of(100L), ShelfType.KOBO.getName()))
|
||||
.thenReturn(List.of(koboShelf1));
|
||||
|
||||
try {
|
||||
koboAutoShelfService.autoAddBookToKoboShelves(1L);
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
+279
@@ -0,0 +1,279 @@
|
||||
package com.adityachandel.booklore.service.kobo;
|
||||
|
||||
import com.adityachandel.booklore.config.security.service.AuthenticationService;
|
||||
import com.adityachandel.booklore.model.dto.BookLoreUser;
|
||||
import com.adityachandel.booklore.model.dto.KoboSyncSettings;
|
||||
import com.adityachandel.booklore.model.dto.Shelf;
|
||||
import com.adityachandel.booklore.model.dto.request.ShelfCreateRequest;
|
||||
import com.adityachandel.booklore.model.entity.KoboUserSettingsEntity;
|
||||
import com.adityachandel.booklore.model.entity.ShelfEntity;
|
||||
import com.adityachandel.booklore.model.enums.IconType;
|
||||
import com.adityachandel.booklore.model.enums.ShelfType;
|
||||
import com.adityachandel.booklore.repository.KoboUserSettingsRepository;
|
||||
import com.adityachandel.booklore.service.ShelfService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.*;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class KoboSettingsServiceTest {
|
||||
|
||||
@Mock
|
||||
private KoboUserSettingsRepository repository;
|
||||
@Mock
|
||||
private AuthenticationService authenticationService;
|
||||
@Mock
|
||||
private ShelfService shelfService;
|
||||
|
||||
@InjectMocks
|
||||
private KoboSettingsService service;
|
||||
|
||||
private BookLoreUser user;
|
||||
private KoboUserSettingsEntity settingsEntity;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
user = BookLoreUser.builder().id(1L).build();
|
||||
settingsEntity = KoboUserSettingsEntity.builder()
|
||||
.id(10L)
|
||||
.userId(1L)
|
||||
.token("token")
|
||||
.syncEnabled(true)
|
||||
.autoAddToShelf(true)
|
||||
.progressMarkAsReadingThreshold(0.5f)
|
||||
.progressMarkAsFinishedThreshold(0.9f)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCurrentUserSettings_existingSettings() {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
|
||||
when(repository.findByUserId(1L)).thenReturn(Optional.of(settingsEntity));
|
||||
|
||||
KoboSyncSettings dto = service.getCurrentUserSettings();
|
||||
|
||||
assertEquals(settingsEntity.getId(), dto.getId());
|
||||
assertEquals(settingsEntity.getUserId().toString(), dto.getUserId());
|
||||
assertEquals(settingsEntity.getToken(), dto.getToken());
|
||||
assertTrue(dto.isSyncEnabled());
|
||||
assertTrue(dto.isAutoAddToShelf());
|
||||
assertEquals(settingsEntity.getProgressMarkAsReadingThreshold(), dto.getProgressMarkAsReadingThreshold());
|
||||
assertEquals(settingsEntity.getProgressMarkAsFinishedThreshold(), dto.getProgressMarkAsFinishedThreshold());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCurrentUserSettings_noSettings_createsDefault() {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
|
||||
when(repository.findByUserId(1L)).thenReturn(Optional.empty());
|
||||
when(repository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
when(shelfService.getShelf(eq(1L), eq(ShelfType.KOBO.getName()))).thenReturn(Optional.empty());
|
||||
doReturn(Shelf.builder().id(100L).build()).when(shelfService).createShelf(any(ShelfCreateRequest.class));
|
||||
|
||||
KoboSyncSettings dto = service.getCurrentUserSettings();
|
||||
|
||||
assertEquals(user.getId().toString(), dto.getUserId());
|
||||
assertNotNull(dto.getToken());
|
||||
assertFalse(dto.isSyncEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createOrUpdateToken_existingSettings() {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
|
||||
when(repository.findByUserId(1L)).thenReturn(Optional.of(settingsEntity));
|
||||
when(repository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
when(shelfService.getShelf(eq(1L), eq(ShelfType.KOBO.getName()))).thenReturn(Optional.of(ShelfEntity.builder().id(100L).build()));
|
||||
|
||||
KoboSyncSettings dto = service.createOrUpdateToken();
|
||||
|
||||
assertEquals(settingsEntity.getUserId().toString(), dto.getUserId());
|
||||
assertNotNull(dto.getToken());
|
||||
assertNotEquals("token", dto.getToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createOrUpdateToken_noSettings_createsNew() {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
|
||||
when(repository.findByUserId(1L)).thenReturn(Optional.empty());
|
||||
when(repository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
when(shelfService.getShelf(eq(1L), eq(ShelfType.KOBO.getName()))).thenReturn(Optional.empty());
|
||||
doReturn(Shelf.builder().id(100L).build()).when(shelfService).createShelf(any(ShelfCreateRequest.class));
|
||||
|
||||
KoboSyncSettings dto = service.createOrUpdateToken();
|
||||
|
||||
assertEquals(user.getId().toString(), dto.getUserId());
|
||||
assertNotNull(dto.getToken());
|
||||
assertFalse(dto.isSyncEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateSettings_disableSync_deletesShelf() {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
|
||||
when(repository.findByUserId(1L)).thenReturn(Optional.of(settingsEntity));
|
||||
Shelf shelf = Shelf.builder().id(100L).build();
|
||||
when(shelfService.getUserKoboShelf()).thenReturn(shelf);
|
||||
when(repository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
|
||||
KoboSyncSettings update = new KoboSyncSettings();
|
||||
update.setSyncEnabled(false);
|
||||
update.setAutoAddToShelf(false);
|
||||
|
||||
KoboSyncSettings dto = service.updateSettings(update);
|
||||
|
||||
verify(shelfService).deleteShelf(100L);
|
||||
assertFalse(dto.isSyncEnabled());
|
||||
assertFalse(dto.isAutoAddToShelf());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateSettings_enableSync_createsShelf() {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
|
||||
settingsEntity.setSyncEnabled(false);
|
||||
when(repository.findByUserId(1L)).thenReturn(Optional.of(settingsEntity));
|
||||
when(shelfService.getUserKoboShelf()).thenReturn(null);
|
||||
when(repository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
when(shelfService.getShelf(eq(1L), eq(ShelfType.KOBO.getName()))).thenReturn(Optional.empty());
|
||||
doReturn(Shelf.builder().id(100L).build()).when(shelfService).createShelf(any(ShelfCreateRequest.class));
|
||||
|
||||
KoboSyncSettings update = new KoboSyncSettings();
|
||||
update.setSyncEnabled(true);
|
||||
update.setAutoAddToShelf(true);
|
||||
|
||||
KoboSyncSettings dto = service.updateSettings(update);
|
||||
|
||||
verify(shelfService).createShelf(any(ShelfCreateRequest.class));
|
||||
assertTrue(dto.isSyncEnabled());
|
||||
assertTrue(dto.isAutoAddToShelf());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateSettings_updatesThresholds() {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
|
||||
when(repository.findByUserId(1L)).thenReturn(Optional.of(settingsEntity));
|
||||
when(repository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
|
||||
KoboSyncSettings update = new KoboSyncSettings();
|
||||
update.setSyncEnabled(true);
|
||||
update.setAutoAddToShelf(true);
|
||||
update.setProgressMarkAsReadingThreshold(0.7f);
|
||||
update.setProgressMarkAsFinishedThreshold(0.95f);
|
||||
|
||||
KoboSyncSettings dto = service.updateSettings(update);
|
||||
|
||||
assertEquals((Float)0.7f, dto.getProgressMarkAsReadingThreshold());
|
||||
assertEquals((Float)0.95f, dto.getProgressMarkAsFinishedThreshold());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateSettings_nullThresholds_shouldNotChangeExisting() {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
|
||||
when(repository.findByUserId(1L)).thenReturn(Optional.of(settingsEntity));
|
||||
when(repository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
|
||||
Float originalReading = settingsEntity.getProgressMarkAsReadingThreshold();
|
||||
Float originalFinished = settingsEntity.getProgressMarkAsFinishedThreshold();
|
||||
|
||||
KoboSyncSettings update = new KoboSyncSettings();
|
||||
update.setSyncEnabled(true);
|
||||
update.setAutoAddToShelf(true);
|
||||
update.setProgressMarkAsReadingThreshold(null);
|
||||
update.setProgressMarkAsFinishedThreshold(null);
|
||||
|
||||
KoboSyncSettings dto = service.updateSettings(update);
|
||||
|
||||
assertEquals(originalReading, dto.getProgressMarkAsReadingThreshold());
|
||||
assertEquals(originalFinished, dto.getProgressMarkAsFinishedThreshold());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCurrentUserSettings_settingsWithNullToken_shouldReturnDtoWithNullToken() {
|
||||
settingsEntity.setToken(null);
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
|
||||
when(repository.findByUserId(1L)).thenReturn(Optional.of(settingsEntity));
|
||||
|
||||
KoboSyncSettings dto = service.getCurrentUserSettings();
|
||||
|
||||
assertNull(dto.getToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCurrentUserSettings_noAuthenticatedUser_shouldThrowException() {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(null);
|
||||
assertThrows(NullPointerException.class, () -> service.getCurrentUserSettings());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateSettings_getUserKoboShelfReturnsNull_shouldNotThrow() {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
|
||||
when(repository.findByUserId(1L)).thenReturn(Optional.of(settingsEntity));
|
||||
when(shelfService.getUserKoboShelf()).thenReturn(null);
|
||||
when(repository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
|
||||
KoboSyncSettings update = new KoboSyncSettings();
|
||||
update.setSyncEnabled(false);
|
||||
update.setAutoAddToShelf(false);
|
||||
|
||||
assertDoesNotThrow(() -> service.updateSettings(update));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ensureKoboShelfExists_doesNotCreateIfExists() throws Exception {
|
||||
when(shelfService.getShelf(eq(1L), eq(ShelfType.KOBO.getName()))).thenReturn(Optional.of(ShelfEntity.builder().id(100L).build()));
|
||||
|
||||
var method = service.getClass().getDeclaredMethod("ensureKoboShelfExists", Long.class);
|
||||
method.setAccessible(true);
|
||||
assertDoesNotThrow(() -> method.invoke(service, 1L));
|
||||
verify(shelfService, never()).createShelf(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ensureKoboShelfExists_createsIfMissing() throws Exception {
|
||||
when(shelfService.getShelf(eq(1L), eq(ShelfType.KOBO.getName()))).thenReturn(Optional.empty());
|
||||
doReturn(Shelf.builder().id(100L).build()).when(shelfService).createShelf(any(ShelfCreateRequest.class));
|
||||
|
||||
var method = service.getClass().getDeclaredMethod("ensureKoboShelfExists", Long.class);
|
||||
method.setAccessible(true);
|
||||
assertDoesNotThrow(() -> method.invoke(service, 1L));
|
||||
verify(shelfService).createShelf(any(ShelfCreateRequest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ensureKoboShelfExists_idempotentIfCalledTwice() throws Exception {
|
||||
when(shelfService.getShelf(eq(1L), eq(ShelfType.KOBO.getName())))
|
||||
.thenReturn(Optional.empty())
|
||||
.thenReturn(Optional.of(ShelfEntity.builder().id(100L).build()));
|
||||
doReturn(Shelf.builder().id(100L).build()).when(shelfService).createShelf(any(ShelfCreateRequest.class));
|
||||
|
||||
var method = service.getClass().getDeclaredMethod("ensureKoboShelfExists", Long.class);
|
||||
method.setAccessible(true);
|
||||
|
||||
method.invoke(service, 1L);
|
||||
method.invoke(service, 1L);
|
||||
|
||||
verify(shelfService, times(1)).createShelf(any(ShelfCreateRequest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createOrUpdateToken_multipleCalls_generateDifferentTokens() {
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
|
||||
when(repository.findByUserId(1L))
|
||||
.thenReturn(Optional.empty())
|
||||
.thenReturn(Optional.of(settingsEntity));
|
||||
when(repository.save(any())).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
when(shelfService.getShelf(eq(1L), eq(ShelfType.KOBO.getName()))).thenReturn(Optional.empty());
|
||||
doReturn(Shelf.builder().id(100L).build()).when(shelfService).createShelf(any(ShelfCreateRequest.class));
|
||||
|
||||
KoboSyncSettings dto1 = service.createOrUpdateToken();
|
||||
// Simulate a new call with an existing entity
|
||||
KoboSyncSettings dto2 = service.createOrUpdateToken();
|
||||
|
||||
assertNotEquals(dto1.getToken(), dto2.getToken());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user