feat(mobile): scope filter options to library, shelf, or magic shelf (#2838)
Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
+89
@@ -0,0 +1,89 @@
|
||||
package org.booklore.mobile.controller;
|
||||
|
||||
import org.booklore.mobile.dto.MobileFilterOptions;
|
||||
import org.booklore.mobile.service.MobileBookService;
|
||||
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 org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class MobileFilterControllerTest {
|
||||
|
||||
@Mock
|
||||
private MobileBookService mobileBookService;
|
||||
|
||||
@InjectMocks
|
||||
private MobileFilterController controller;
|
||||
|
||||
@Test
|
||||
void getFilterOptions_noParams_delegatesWithNulls() {
|
||||
MobileFilterOptions expected = buildOptions(List.of("Author A"), List.of("EPUB"), List.of("en"));
|
||||
when(mobileBookService.getFilterOptions(null, null, null)).thenReturn(expected);
|
||||
|
||||
ResponseEntity<MobileFilterOptions> response = controller.getFilterOptions(null, null, null);
|
||||
|
||||
assertEquals(200, response.getStatusCode().value());
|
||||
assertSame(expected, response.getBody());
|
||||
verify(mobileBookService).getFilterOptions(null, null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFilterOptions_withLibraryId_passesLibraryId() {
|
||||
MobileFilterOptions expected = buildOptions(List.of("Author B"), List.of("PDF"), List.of("fr"));
|
||||
when(mobileBookService.getFilterOptions(5L, null, null)).thenReturn(expected);
|
||||
|
||||
ResponseEntity<MobileFilterOptions> response = controller.getFilterOptions(5L, null, null);
|
||||
|
||||
assertEquals(200, response.getStatusCode().value());
|
||||
assertEquals(1, response.getBody().getAuthors().size());
|
||||
assertEquals("Author B", response.getBody().getAuthors().getFirst().getName());
|
||||
verify(mobileBookService).getFilterOptions(5L, null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFilterOptions_withShelfId_passesShelfId() {
|
||||
MobileFilterOptions expected = buildOptions(List.of(), List.of("EPUB"), List.of());
|
||||
when(mobileBookService.getFilterOptions(null, 10L, null)).thenReturn(expected);
|
||||
|
||||
ResponseEntity<MobileFilterOptions> response = controller.getFilterOptions(null, 10L, null);
|
||||
|
||||
assertEquals(200, response.getStatusCode().value());
|
||||
assertTrue(response.getBody().getAuthors().isEmpty());
|
||||
verify(mobileBookService).getFilterOptions(null, 10L, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFilterOptions_withMagicShelfId_passesMagicShelfId() {
|
||||
MobileFilterOptions expected = buildOptions(List.of("Author C"), List.of("MOBI"), List.of("de"));
|
||||
when(mobileBookService.getFilterOptions(null, null, 7L)).thenReturn(expected);
|
||||
|
||||
ResponseEntity<MobileFilterOptions> response = controller.getFilterOptions(null, null, 7L);
|
||||
|
||||
assertEquals(200, response.getStatusCode().value());
|
||||
assertEquals("Author C", response.getBody().getAuthors().getFirst().getName());
|
||||
verify(mobileBookService).getFilterOptions(null, null, 7L);
|
||||
}
|
||||
|
||||
private MobileFilterOptions buildOptions(List<String> authorNames, List<String> fileTypes, List<String> langCodes) {
|
||||
List<MobileFilterOptions.AuthorOption> authors = authorNames.stream()
|
||||
.map(name -> MobileFilterOptions.AuthorOption.builder().name(name).count(1L).build())
|
||||
.toList();
|
||||
List<MobileFilterOptions.LanguageOption> languages = langCodes.stream()
|
||||
.map(code -> MobileFilterOptions.LanguageOption.builder().code(code).label(code).count(1L).build())
|
||||
.toList();
|
||||
return MobileFilterOptions.builder()
|
||||
.authors(authors)
|
||||
.languages(languages)
|
||||
.fileTypes(fileTypes)
|
||||
.readStatuses(List.of("READ", "READING", "UNREAD"))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
+260
@@ -0,0 +1,260 @@
|
||||
package org.booklore.mobile.service;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.Tuple;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import org.booklore.config.security.service.AuthenticationService;
|
||||
import org.booklore.exception.APIException;
|
||||
import org.booklore.mobile.dto.MobileFilterOptions;
|
||||
import org.booklore.mobile.mapper.MobileBookMapper;
|
||||
import org.booklore.model.dto.Book;
|
||||
import org.booklore.model.dto.BookLoreUser;
|
||||
import org.booklore.model.dto.Library;
|
||||
import org.booklore.model.entity.BookLoreUserEntity;
|
||||
import org.booklore.model.entity.ShelfEntity;
|
||||
import org.booklore.model.enums.BookFileType;
|
||||
import org.booklore.repository.BookRepository;
|
||||
import org.booklore.repository.ShelfRepository;
|
||||
import org.booklore.repository.UserBookFileProgressRepository;
|
||||
import org.booklore.repository.UserBookProgressRepository;
|
||||
import org.booklore.service.opds.MagicShelfBookService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@MockitoSettings(strictness = Strictness.LENIENT)
|
||||
class MobileBookServiceFilterOptionsTest {
|
||||
|
||||
@Mock private BookRepository bookRepository;
|
||||
@Mock private UserBookProgressRepository userBookProgressRepository;
|
||||
@Mock private UserBookFileProgressRepository userBookFileProgressRepository;
|
||||
@Mock private ShelfRepository shelfRepository;
|
||||
@Mock private AuthenticationService authenticationService;
|
||||
@Mock private MobileBookMapper mobileBookMapper;
|
||||
@Mock private MagicShelfBookService magicShelfBookService;
|
||||
@Mock private EntityManager entityManager;
|
||||
|
||||
private MobileBookService service;
|
||||
|
||||
private final Long userId = 1L;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
service = new MobileBookService(
|
||||
bookRepository, userBookProgressRepository, userBookFileProgressRepository,
|
||||
shelfRepository, authenticationService, mobileBookMapper,
|
||||
magicShelfBookService, entityManager
|
||||
);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Global (no scoping params)
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
void getFilterOptions_noParams_returnsGlobalOptions() {
|
||||
mockAdminUser();
|
||||
mockJpqlQueries();
|
||||
|
||||
MobileFilterOptions result = service.getFilterOptions(null, null, null);
|
||||
|
||||
assertNotNull(result);
|
||||
assertNotNull(result.getAuthors());
|
||||
assertNotNull(result.getLanguages());
|
||||
assertNotNull(result.getFileTypes());
|
||||
assertFalse(result.getReadStatuses().isEmpty());
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Library scoping
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
void getFilterOptions_withLibraryId_admin_succeeds() {
|
||||
mockAdminUser();
|
||||
mockJpqlQueries();
|
||||
|
||||
MobileFilterOptions result = service.getFilterOptions(5L, null, null);
|
||||
|
||||
assertNotNull(result);
|
||||
verify(entityManager, times(3)).createQuery(anyString(), any(Class.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFilterOptions_withLibraryId_nonAdminWithAccess_succeeds() {
|
||||
mockNonAdminUser(Set.of(5L, 10L));
|
||||
mockJpqlQueries();
|
||||
|
||||
MobileFilterOptions result = service.getFilterOptions(5L, null, null);
|
||||
|
||||
assertNotNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFilterOptions_withLibraryId_nonAdminNoAccess_throwsForbidden() {
|
||||
mockNonAdminUser(Set.of(10L));
|
||||
|
||||
assertThrows(APIException.class, () -> service.getFilterOptions(5L, null, null));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Shelf scoping
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
void getFilterOptions_withShelfId_publicShelf_succeeds() {
|
||||
mockAdminUser();
|
||||
ShelfEntity shelf = ShelfEntity.builder().id(10L).isPublic(true)
|
||||
.user(BookLoreUserEntity.builder().id(99L).build()).build();
|
||||
when(shelfRepository.findById(10L)).thenReturn(Optional.of(shelf));
|
||||
mockJpqlQueries();
|
||||
|
||||
MobileFilterOptions result = service.getFilterOptions(null, 10L, null);
|
||||
|
||||
assertNotNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFilterOptions_withShelfId_ownPrivateShelf_succeeds() {
|
||||
mockAdminUser();
|
||||
ShelfEntity shelf = ShelfEntity.builder().id(10L).isPublic(false)
|
||||
.user(BookLoreUserEntity.builder().id(userId).build()).build();
|
||||
when(shelfRepository.findById(10L)).thenReturn(Optional.of(shelf));
|
||||
mockJpqlQueries();
|
||||
|
||||
MobileFilterOptions result = service.getFilterOptions(null, 10L, null);
|
||||
|
||||
assertNotNull(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFilterOptions_withShelfId_otherPrivateShelf_throwsForbidden() {
|
||||
mockAdminUser();
|
||||
ShelfEntity shelf = ShelfEntity.builder().id(10L).isPublic(false)
|
||||
.user(BookLoreUserEntity.builder().id(99L).build()).build();
|
||||
when(shelfRepository.findById(10L)).thenReturn(Optional.of(shelf));
|
||||
|
||||
assertThrows(APIException.class, () -> service.getFilterOptions(null, 10L, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFilterOptions_withShelfId_notFound_throwsException() {
|
||||
mockAdminUser();
|
||||
when(shelfRepository.findById(10L)).thenReturn(Optional.empty());
|
||||
|
||||
assertThrows(APIException.class, () -> service.getFilterOptions(null, 10L, null));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Magic shelf scoping
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Test
|
||||
void getFilterOptions_withMagicShelfId_emptyResult_returnsEmptyOptions() {
|
||||
mockAdminUser();
|
||||
mockMagicShelfBooks(7L, Collections.emptyList());
|
||||
|
||||
MobileFilterOptions result = service.getFilterOptions(null, null, 7L);
|
||||
|
||||
assertNotNull(result);
|
||||
assertTrue(result.getAuthors().isEmpty());
|
||||
assertTrue(result.getLanguages().isEmpty());
|
||||
assertTrue(result.getFileTypes().isEmpty());
|
||||
assertFalse(result.getReadStatuses().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFilterOptions_withMagicShelfId_withBooks_returnsFilteredOptions() {
|
||||
mockAdminUser();
|
||||
Book book1 = Book.builder().id(100L).build();
|
||||
Book book2 = Book.builder().id(200L).build();
|
||||
mockMagicShelfBooks(7L, List.of(book1, book2));
|
||||
mockJpqlQueries();
|
||||
|
||||
MobileFilterOptions result = service.getFilterOptions(null, null, 7L);
|
||||
|
||||
assertNotNull(result);
|
||||
verify(magicShelfBookService).getBooksByMagicShelfId(eq(userId), eq(7L), eq(0), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFilterOptions_withMagicShelfId_serviceThrows_propagatesException() {
|
||||
mockAdminUser();
|
||||
when(magicShelfBookService.getBooksByMagicShelfId(eq(userId), eq(7L), eq(0), anyInt()))
|
||||
.thenThrow(new RuntimeException("Magic shelf not found"));
|
||||
|
||||
assertThrows(RuntimeException.class, () -> service.getFilterOptions(null, null, 7L));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
private void mockAdminUser() {
|
||||
var permissions = new BookLoreUser.UserPermissions();
|
||||
permissions.setAdmin(true);
|
||||
BookLoreUser user = BookLoreUser.builder()
|
||||
.id(userId)
|
||||
.permissions(permissions)
|
||||
.build();
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
|
||||
}
|
||||
|
||||
private void mockNonAdminUser(Set<Long> libraryIds) {
|
||||
List<Library> assignedLibraries = libraryIds.stream()
|
||||
.map(id -> Library.builder().id(id).build())
|
||||
.toList();
|
||||
var permissions = new BookLoreUser.UserPermissions();
|
||||
permissions.setAdmin(false);
|
||||
BookLoreUser user = BookLoreUser.builder()
|
||||
.id(userId)
|
||||
.permissions(permissions)
|
||||
.assignedLibraries(assignedLibraries)
|
||||
.build();
|
||||
when(authenticationService.getAuthenticatedUser()).thenReturn(user);
|
||||
}
|
||||
|
||||
private void mockMagicShelfBooks(Long magicShelfId, List<Book> books) {
|
||||
var page = new PageImpl<>(books, PageRequest.of(0, Math.max(books.size(), 1)), books.size());
|
||||
when(magicShelfBookService.getBooksByMagicShelfId(eq(userId), eq(magicShelfId), eq(0), anyInt()))
|
||||
.thenReturn(page);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void mockJpqlQueries() {
|
||||
TypedQuery<Tuple> authorQuery = mock(TypedQuery.class);
|
||||
when(authorQuery.setMaxResults(anyInt())).thenReturn(authorQuery);
|
||||
when(authorQuery.setParameter(anyString(), any())).thenReturn(authorQuery);
|
||||
when(authorQuery.getResultList()).thenReturn(Collections.emptyList());
|
||||
|
||||
TypedQuery<Tuple> langQuery = mock(TypedQuery.class);
|
||||
when(langQuery.setParameter(anyString(), any())).thenReturn(langQuery);
|
||||
when(langQuery.getResultList()).thenReturn(Collections.emptyList());
|
||||
|
||||
TypedQuery<BookFileType> ftQuery = mock(TypedQuery.class);
|
||||
when(ftQuery.setParameter(anyString(), any())).thenReturn(ftQuery);
|
||||
when(ftQuery.getResultList()).thenReturn(Collections.emptyList());
|
||||
|
||||
when(entityManager.createQuery(anyString(), eq(Tuple.class)))
|
||||
.thenReturn(authorQuery)
|
||||
.thenReturn(langQuery);
|
||||
when(entityManager.createQuery(anyString(), eq(BookFileType.class)))
|
||||
.thenReturn(ftQuery);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user