Expose Magic Shelves through the OPDS feed so clients can access them (#1683)
This commit is contained in:
+48
-21
@@ -57,10 +57,8 @@ class OpdsBookServiceTest {
|
||||
}
|
||||
|
||||
private OpdsUserDetails legacyUserDetails() {
|
||||
OpdsUser opdsUser = new OpdsUser();
|
||||
opdsUser.setUsername("legacy");
|
||||
opdsUser.setPassword(null);
|
||||
return new OpdsUserDetails(null, opdsUser);
|
||||
OpdsUserV2 v2 = OpdsUserV2.builder().userId(999L).username("legacy").build();
|
||||
return new OpdsUserDetails(v2);
|
||||
}
|
||||
|
||||
private OpdsUserDetails v2UserDetails(Long userId, boolean isAdmin, Set<Long> libraryIds) {
|
||||
@@ -81,17 +79,14 @@ class OpdsBookServiceTest {
|
||||
}
|
||||
when(user.getAssignedLibraries()).thenReturn(libraries);
|
||||
|
||||
return new OpdsUserDetails(v2, null);
|
||||
return new OpdsUserDetails(v2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAccessibleLibraries_returnsAllLibraries_whenNoUserDetails() {
|
||||
List<Library> libraries = List.of(Library.builder().id(1L).name("Lib1").watch(false).build());
|
||||
when(libraryService.getAllLibraries()).thenReturn(libraries);
|
||||
|
||||
void getAccessibleLibraries_returnsEmptyList_whenUserIdIsNull() {
|
||||
List<Library> result = opdsBookService.getAccessibleLibraries(null);
|
||||
|
||||
assertThat(result).isEqualTo(libraries);
|
||||
assertThat(result).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -107,7 +102,7 @@ class OpdsBookServiceTest {
|
||||
List<Library> assigned = List.of(Library.builder().id(2L).watch(false).build());
|
||||
when(user.getAssignedLibraries()).thenReturn(assigned);
|
||||
|
||||
List<Library> result = opdsBookService.getAccessibleLibraries(details);
|
||||
List<Library> result = opdsBookService.getAccessibleLibraries(details.getOpdsUserV2().getUserId());
|
||||
|
||||
assertThat(result).isEqualTo(assigned);
|
||||
}
|
||||
@@ -118,7 +113,7 @@ class OpdsBookServiceTest {
|
||||
List<Library> allLibs = List.of(Library.builder().id(1L).watch(false).build());
|
||||
when(libraryService.getAllLibraries()).thenReturn(allLibs);
|
||||
|
||||
List<Library> result = opdsBookService.getAccessibleLibraries(details);
|
||||
List<Library> result = opdsBookService.getAccessibleLibraries(details.getOpdsUserV2().getUserId());
|
||||
|
||||
assertThat(result).isEqualTo(allLibs);
|
||||
}
|
||||
@@ -126,18 +121,41 @@ class OpdsBookServiceTest {
|
||||
@Test
|
||||
void getBooksPage_legacyUser_delegatesToLegacyMethod() {
|
||||
OpdsUserDetails details = legacyUserDetails();
|
||||
BookLoreUserEntity entity = mock(BookLoreUserEntity.class);
|
||||
var permissionsEntity = mock(com.adityachandel.booklore.model.entity.UserPermissionsEntity.class);
|
||||
when(permissionsEntity.isPermissionAccessOpds()).thenReturn(true);
|
||||
when(permissionsEntity.isPermissionAdmin()).thenReturn(false);
|
||||
when(entity.getPermissions()).thenReturn(permissionsEntity);
|
||||
when(userRepository.findById(999L)).thenReturn(Optional.of(entity));
|
||||
|
||||
BookLoreUser user = mock(BookLoreUser.class);
|
||||
BookLoreUser.UserPermissions perms = mock(BookLoreUser.UserPermissions.class);
|
||||
when(bookLoreUserTransformer.toDTO(entity)).thenReturn(user);
|
||||
when(user.getPermissions()).thenReturn(perms);
|
||||
when(perms.isAdmin()).thenReturn(false);
|
||||
when(user.getAssignedLibraries()).thenReturn(List.of(Library.builder().id(1L).watch(false).build()));
|
||||
when(user.getId()).thenReturn(999L);
|
||||
|
||||
// Mock shelf access
|
||||
ShelfEntity shelf = mock(ShelfEntity.class);
|
||||
BookLoreUserEntity shelfUser = mock(BookLoreUserEntity.class);
|
||||
when(shelfUser.getId()).thenReturn(999L);
|
||||
when(shelf.getUser()).thenReturn(shelfUser);
|
||||
when(shelfRepository.findById(2L)).thenReturn(Optional.of(shelf));
|
||||
|
||||
when(bookOpdsRepository.findBookIds(any())).thenReturn(Page.empty());
|
||||
when(bookOpdsRepository.findRecentBookIds(any())).thenReturn(Page.empty());
|
||||
when(bookOpdsRepository.findBookIdsByLibraryIds(anySet(), any())).thenReturn(Page.empty());
|
||||
when(bookOpdsRepository.findBookIdsByShelfId(anyLong(), any())).thenReturn(Page.empty());
|
||||
when(bookOpdsRepository.findBookIdsByMetadataSearch(anyString(), any())).thenReturn(Page.empty());
|
||||
when(bookOpdsRepository.findBookIdsByMetadataSearchAndLibraryIds(anyString(), anySet(), any())).thenReturn(Page.empty());
|
||||
when(bookOpdsRepository.findAllWithMetadataByIds(anyList())).thenReturn(List.of());
|
||||
when(bookOpdsRepository.findAllWithMetadataByIdsAndLibraryIds(anyList(), anySet())).thenReturn(List.of());
|
||||
when(bookOpdsRepository.findAllWithMetadataByIdsAndShelfId(anyList(), anyLong())).thenReturn(List.of());
|
||||
when(bookOpdsRepository.findAllWithFullMetadataByIds(anyList())).thenReturn(List.of());
|
||||
when(bookOpdsRepository.findAllWithFullMetadataByIdsAndLibraryIds(anyList(), anySet())).thenReturn(List.of());
|
||||
|
||||
opdsBookService.getBooksPage(details, "q", 1L, 2L, 0, 10);
|
||||
opdsBookService.getBooksPage(details.getOpdsUserV2().getUserId(), "q", 1L, 2L, 0, 10);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -165,6 +183,7 @@ class OpdsBookServiceTest {
|
||||
when(bookLoreUserTransformer.toDTO(entity)).thenReturn(user);
|
||||
when(user.getPermissions()).thenReturn(perms);
|
||||
when(perms.isAdmin()).thenReturn(true);
|
||||
when(user.getId()).thenReturn(1L);
|
||||
|
||||
ShelfEntity shelf = mock(ShelfEntity.class);
|
||||
BookLoreUserEntity shelfUser = mock(BookLoreUserEntity.class);
|
||||
@@ -172,16 +191,24 @@ class OpdsBookServiceTest {
|
||||
when(shelf.getUser()).thenReturn(shelfUser);
|
||||
when(shelfRepository.findById(anyLong())).thenReturn(Optional.of(shelf));
|
||||
|
||||
opdsBookService.getBooksPage(details, "q", 1L, 2L, 0, 10);
|
||||
opdsBookService.getBooksPage(details.getOpdsUserV2().getUserId(), "q", 1L, 2L, 0, 10);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRecentBooksPage_returnsRecentBooks_forLegacyUser() {
|
||||
OpdsUserDetails details = legacyUserDetails();
|
||||
BookLoreUserEntity entity = mock(BookLoreUserEntity.class);
|
||||
when(userRepository.findById(999L)).thenReturn(Optional.of(entity));
|
||||
BookLoreUser user = mock(BookLoreUser.class);
|
||||
when(bookLoreUserTransformer.toDTO(entity)).thenReturn(user);
|
||||
BookLoreUser.UserPermissions perms = mock(BookLoreUser.UserPermissions.class);
|
||||
when(user.getPermissions()).thenReturn(perms);
|
||||
when(perms.isAdmin()).thenReturn(true);
|
||||
|
||||
when(bookOpdsRepository.findRecentBookIds(any())).thenReturn(Page.empty());
|
||||
when(bookOpdsRepository.findAllWithMetadataByIds(anyList())).thenReturn(List.of());
|
||||
|
||||
opdsBookService.getRecentBooksPage(details, 0, 10);
|
||||
opdsBookService.getRecentBooksPage(details.getOpdsUserV2().getUserId(), 0, 10);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -205,7 +232,7 @@ class OpdsBookServiceTest {
|
||||
when(bookOpdsRepository.findAllWithMetadataByIdsAndLibraryIds(anyList(), anySet())).thenReturn(List.of(bookEntity));
|
||||
when(bookMapper.toBook(bookEntity)).thenReturn(book);
|
||||
|
||||
Page<Book> result = opdsBookService.getRecentBooksPage(details, 0, 10);
|
||||
Page<Book> result = opdsBookService.getRecentBooksPage(details.getOpdsUserV2().getUserId(), 0, 10);
|
||||
|
||||
assertThat(result.getContent()).hasSize(1);
|
||||
assertThat(result.getContent().getFirst().getShelves()).allMatch(shelf -> shelf.getUserId().equals(2L));
|
||||
@@ -265,7 +292,7 @@ class OpdsBookServiceTest {
|
||||
OpdsUserDetails details = v2UserDetails(1L, true, Set.of(1L));
|
||||
OpdsBookService spy = Mockito.spy(opdsBookService);
|
||||
List<Library> libs = List.of(Library.builder().id(1L).watch(false).build());
|
||||
doReturn(libs).when(spy).getAccessibleLibraries(details);
|
||||
doReturn(libs).when(spy).getAccessibleLibraries(details.getOpdsUserV2().getUserId());
|
||||
|
||||
when(bookOpdsRepository.findRandomBookIdsByLibraryIds(anyList())).thenReturn(List.of(1L, 2L));
|
||||
BookEntity entity = mock(BookEntity.class);
|
||||
@@ -273,7 +300,7 @@ class OpdsBookServiceTest {
|
||||
Book book = Book.builder().id(1L).build();
|
||||
when(bookMapper.toBook(entity)).thenReturn(book);
|
||||
|
||||
List<Book> result = spy.getRandomBooks(details, 1);
|
||||
List<Book> result = spy.getRandomBooks(details.getOpdsUserV2().getUserId(), 1);
|
||||
|
||||
assertThat(result).hasSize(1);
|
||||
}
|
||||
@@ -282,9 +309,9 @@ class OpdsBookServiceTest {
|
||||
void getRandomBooks_returnsEmpty_whenNoLibraries() {
|
||||
OpdsUserDetails details = v2UserDetails(1L, false, Set.of());
|
||||
OpdsBookService spy = Mockito.spy(opdsBookService);
|
||||
doReturn(List.of()).when(spy).getAccessibleLibraries(details);
|
||||
doReturn(List.of()).when(spy).getAccessibleLibraries(details.getOpdsUserV2().getUserId());
|
||||
|
||||
List<Book> result = spy.getRandomBooks(details, 1);
|
||||
List<Book> result = spy.getRandomBooks(details.getOpdsUserV2().getUserId(), 1);
|
||||
|
||||
assertThat(result).isEmpty();
|
||||
}
|
||||
@@ -300,7 +327,7 @@ class OpdsBookServiceTest {
|
||||
when(userRepository.findById(1L)).thenReturn(Optional.of(entity));
|
||||
|
||||
assertThatThrownBy(() ->
|
||||
opdsBookService.getBooksPage(v2UserDetails(1L, false, Set.of()), null, null, null, 0, 10)
|
||||
opdsBookService.getBooksPage(1L, null, null, null, 0, 10)
|
||||
).hasMessageContaining("You are not allowed to access this resource");
|
||||
}
|
||||
|
||||
|
||||
+64
-40
@@ -8,6 +8,7 @@ import com.adityachandel.booklore.model.dto.Library;
|
||||
import com.adityachandel.booklore.model.dto.OpdsUserV2;
|
||||
import com.adityachandel.booklore.model.entity.ShelfEntity;
|
||||
import com.adityachandel.booklore.model.enums.BookFileType;
|
||||
import com.adityachandel.booklore.service.MagicShelfService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -26,9 +27,12 @@ import static org.mockito.Mockito.*;
|
||||
class OpdsFeedServiceTest {
|
||||
|
||||
private static final Instant FIXED_INSTANT = Instant.parse("2025-01-01T12:00:00Z");
|
||||
private static final Long TEST_USER_ID = 42L;
|
||||
|
||||
private AuthenticationService authenticationService;
|
||||
private OpdsBookService opdsBookService;
|
||||
private MagicShelfService magicShelfService;
|
||||
private MagicShelfBookService magicShelfBookService;
|
||||
private OpdsFeedService opdsFeedService;
|
||||
private HttpServletRequest request;
|
||||
|
||||
@@ -36,10 +40,21 @@ class OpdsFeedServiceTest {
|
||||
void setUp() {
|
||||
authenticationService = mock(AuthenticationService.class);
|
||||
opdsBookService = mock(OpdsBookService.class);
|
||||
opdsFeedService = new OpdsFeedService(authenticationService, opdsBookService);
|
||||
magicShelfService = mock(MagicShelfService.class);
|
||||
magicShelfBookService = mock(MagicShelfBookService.class);
|
||||
opdsFeedService = new OpdsFeedService(authenticationService, opdsBookService, magicShelfService, magicShelfBookService);
|
||||
request = mock(HttpServletRequest.class);
|
||||
}
|
||||
|
||||
private OpdsUserDetails mockAuthenticatedUser() {
|
||||
OpdsUserDetails userDetails = mock(OpdsUserDetails.class);
|
||||
OpdsUserV2 v2 = mock(OpdsUserV2.class);
|
||||
when(userDetails.getOpdsUserV2()).thenReturn(v2);
|
||||
when(v2.getUserId()).thenReturn(TEST_USER_ID);
|
||||
when(authenticationService.getOpdsUser()).thenReturn(userDetails);
|
||||
return userDetails;
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateRootNavigation_shouldContainAllSections() {
|
||||
String xml = opdsFeedService.generateRootNavigation(request);
|
||||
@@ -47,6 +62,7 @@ class OpdsFeedServiceTest {
|
||||
assertThat(xml).contains("Recently Added");
|
||||
assertThat(xml).contains("Libraries");
|
||||
assertThat(xml).contains("Shelves");
|
||||
assertThat(xml).contains("Magic Shelves");
|
||||
assertThat(xml).contains("Surprise Me");
|
||||
assertThat(xml).contains("<?xml");
|
||||
assertThat(xml).contains("</feed>");
|
||||
@@ -54,23 +70,22 @@ class OpdsFeedServiceTest {
|
||||
|
||||
@Test
|
||||
void generateLibrariesNavigation_shouldListLibraries() {
|
||||
OpdsUserDetails userDetails = mock(OpdsUserDetails.class);
|
||||
when(authenticationService.getOpdsUser()).thenReturn(userDetails);
|
||||
mockAuthenticatedUser();
|
||||
|
||||
Library lib = Library.builder().id(1L).name("Test Library").watch(false).build();
|
||||
when(opdsBookService.getAccessibleLibraries(userDetails)).thenReturn(List.of(lib));
|
||||
when(opdsBookService.getAccessibleLibraries(TEST_USER_ID)).thenReturn(List.of(lib));
|
||||
|
||||
String xml = opdsFeedService.generateLibrariesNavigation(request);
|
||||
assertThat(xml).contains("Test Library");
|
||||
assertThat(xml).contains("urn:booklore:library:1");
|
||||
assertThat(xml).contains("</feed>");
|
||||
verify(opdsBookService).getAccessibleLibraries(TEST_USER_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateLibrariesNavigation_shouldHandleNoLibraries() {
|
||||
OpdsUserDetails userDetails = mock(OpdsUserDetails.class);
|
||||
when(authenticationService.getOpdsUser()).thenReturn(userDetails);
|
||||
when(opdsBookService.getAccessibleLibraries(userDetails)).thenReturn(Collections.emptyList());
|
||||
mockAuthenticatedUser();
|
||||
when(opdsBookService.getAccessibleLibraries(TEST_USER_ID)).thenReturn(Collections.emptyList());
|
||||
|
||||
String xml = opdsFeedService.generateLibrariesNavigation(request);
|
||||
assertThat(xml).contains("</feed>");
|
||||
@@ -78,30 +93,22 @@ class OpdsFeedServiceTest {
|
||||
|
||||
@Test
|
||||
void generateShelvesNavigation_shouldListShelves() {
|
||||
OpdsUserDetails userDetails = mock(OpdsUserDetails.class);
|
||||
OpdsUserV2 v2 = mock(OpdsUserV2.class);
|
||||
when(userDetails.getOpdsUserV2()).thenReturn(v2);
|
||||
when(v2.getUserId()).thenReturn(42L);
|
||||
when(authenticationService.getOpdsUser()).thenReturn(userDetails);
|
||||
mockAuthenticatedUser();
|
||||
|
||||
ShelfEntity shelfEntity = ShelfEntity.builder().id(5L).name("Favorites").build();
|
||||
when(opdsBookService.getUserShelves(42L)).thenReturn(Collections.singletonList(shelfEntity));
|
||||
when(opdsBookService.getUserShelves(TEST_USER_ID)).thenReturn(Collections.singletonList(shelfEntity));
|
||||
|
||||
String xml = opdsFeedService.generateShelvesNavigation(request);
|
||||
assertThat(xml).contains("Favorites");
|
||||
assertThat(xml).contains("urn:booklore:shelf:5");
|
||||
assertThat(xml).contains("</feed>");
|
||||
verify(opdsBookService).getUserShelves(TEST_USER_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateShelvesNavigation_shouldHandleNoShelves() {
|
||||
OpdsUserDetails userDetails = mock(OpdsUserDetails.class);
|
||||
OpdsUserV2 v2 = mock(OpdsUserV2.class);
|
||||
when(userDetails.getOpdsUserV2()).thenReturn(v2);
|
||||
when(v2.getUserId()).thenReturn(42L);
|
||||
when(authenticationService.getOpdsUser()).thenReturn(userDetails);
|
||||
|
||||
when(opdsBookService.getUserShelves(42L)).thenReturn(Collections.emptyList());
|
||||
mockAuthenticatedUser();
|
||||
when(opdsBookService.getUserShelves(TEST_USER_ID)).thenReturn(Collections.emptyList());
|
||||
|
||||
String xml = opdsFeedService.generateShelvesNavigation(request);
|
||||
assertThat(xml).contains("</feed>");
|
||||
@@ -112,15 +119,16 @@ class OpdsFeedServiceTest {
|
||||
when(authenticationService.getOpdsUser()).thenReturn(null);
|
||||
String xml = opdsFeedService.generateShelvesNavigation(request);
|
||||
assertThat(xml).contains("</feed>");
|
||||
verify(opdsBookService, never()).getUserShelves(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateCatalogFeed_shouldReturnFeedWithBooks() {
|
||||
OpdsUserDetails userDetails = mock(OpdsUserDetails.class);
|
||||
when(authenticationService.getOpdsUser()).thenReturn(userDetails);
|
||||
mockAuthenticatedUser();
|
||||
|
||||
when(request.getParameter("libraryId")).thenReturn(null);
|
||||
when(request.getParameter("shelfId")).thenReturn(null);
|
||||
when(request.getParameter("magicShelfId")).thenReturn(null);
|
||||
when(request.getParameter("q")).thenReturn(null);
|
||||
when(request.getParameter("page")).thenReturn(null);
|
||||
when(request.getParameter("size")).thenReturn(null);
|
||||
@@ -143,9 +151,7 @@ class OpdsFeedServiceTest {
|
||||
.build();
|
||||
|
||||
Page<Book> page = new PageImpl<>(List.of(book), PageRequest.of(0, 50), 1);
|
||||
when(opdsBookService.getBooksPage(eq(userDetails), any(), any(), any(), eq(0), eq(50))).thenReturn(page);
|
||||
when(opdsBookService.getShelfName(any())).thenReturn("Shelf Name");
|
||||
when(opdsBookService.getLibraryName(any())).thenReturn("Library Name");
|
||||
when(opdsBookService.getBooksPage(eq(TEST_USER_ID), any(), any(), any(), eq(0), eq(50))).thenReturn(page);
|
||||
|
||||
String xml = opdsFeedService.generateCatalogFeed(request);
|
||||
assertThat(xml).contains("Book Title");
|
||||
@@ -154,12 +160,12 @@ class OpdsFeedServiceTest {
|
||||
assertThat(xml).contains("urn:booklore:book:10");
|
||||
assertThat(xml).contains("application/epub+zip");
|
||||
assertThat(xml).contains("</feed>");
|
||||
verify(opdsBookService).getBooksPage(TEST_USER_ID, null, null, null, 0, 50);
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateCatalogFeed_shouldHandleEmptyPage() {
|
||||
OpdsUserDetails userDetails = mock(OpdsUserDetails.class);
|
||||
when(authenticationService.getOpdsUser()).thenReturn(userDetails);
|
||||
mockAuthenticatedUser();
|
||||
|
||||
when(request.getParameter(anyString())).thenReturn(null);
|
||||
when(request.getRequestURI()).thenReturn("/api/v1/opds/catalog");
|
||||
@@ -174,8 +180,7 @@ class OpdsFeedServiceTest {
|
||||
|
||||
@Test
|
||||
void generateRecentFeed_shouldReturnFeedWithBooks() {
|
||||
OpdsUserDetails userDetails = mock(OpdsUserDetails.class);
|
||||
when(authenticationService.getOpdsUser()).thenReturn(userDetails);
|
||||
mockAuthenticatedUser();
|
||||
|
||||
when(request.getParameter("page")).thenReturn(null);
|
||||
when(request.getParameter("size")).thenReturn(null);
|
||||
@@ -190,18 +195,18 @@ class OpdsFeedServiceTest {
|
||||
.build();
|
||||
|
||||
Page<Book> page = new PageImpl<>(List.of(book), PageRequest.of(0, 50), 1);
|
||||
when(opdsBookService.getRecentBooksPage(eq(userDetails), eq(0), eq(50))).thenReturn(page);
|
||||
when(opdsBookService.getRecentBooksPage(eq(TEST_USER_ID), eq(0), eq(50))).thenReturn(page);
|
||||
|
||||
String xml = opdsFeedService.generateRecentFeed(request);
|
||||
assertThat(xml).contains("Recent Book");
|
||||
assertThat(xml).contains("application/pdf");
|
||||
assertThat(xml).contains("</feed>");
|
||||
verify(opdsBookService).getRecentBooksPage(TEST_USER_ID, 0, 50);
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateRecentFeed_shouldHandleEmptyPage() {
|
||||
OpdsUserDetails userDetails = mock(OpdsUserDetails.class);
|
||||
when(authenticationService.getOpdsUser()).thenReturn(userDetails);
|
||||
mockAuthenticatedUser();
|
||||
|
||||
when(request.getParameter(anyString())).thenReturn(null);
|
||||
when(request.getRequestURI()).thenReturn("/api/v1/opds/recent");
|
||||
@@ -216,8 +221,7 @@ class OpdsFeedServiceTest {
|
||||
|
||||
@Test
|
||||
void generateSurpriseFeed_shouldReturnFeedWithBooks() {
|
||||
OpdsUserDetails userDetails = mock(OpdsUserDetails.class);
|
||||
when(authenticationService.getOpdsUser()).thenReturn(userDetails);
|
||||
mockAuthenticatedUser();
|
||||
|
||||
Book book = Book.builder()
|
||||
.id(12L)
|
||||
@@ -226,20 +230,19 @@ class OpdsFeedServiceTest {
|
||||
.metadata(BookMetadata.builder().title("Surprise Book").build())
|
||||
.build();
|
||||
|
||||
when(opdsBookService.getRandomBooks(userDetails, 25)).thenReturn(List.of(book));
|
||||
when(opdsBookService.getRandomBooks(TEST_USER_ID, 25)).thenReturn(List.of(book));
|
||||
|
||||
String xml = opdsFeedService.generateSurpriseFeed(request);
|
||||
assertThat(xml).contains("Surprise Book");
|
||||
assertThat(xml).contains("urn:booklore:book:12");
|
||||
assertThat(xml).contains("</feed>");
|
||||
verify(opdsBookService).getRandomBooks(TEST_USER_ID, 25);
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateSurpriseFeed_shouldHandleNoBooks() {
|
||||
OpdsUserDetails userDetails = mock(OpdsUserDetails.class);
|
||||
when(authenticationService.getOpdsUser()).thenReturn(userDetails);
|
||||
|
||||
when(opdsBookService.getRandomBooks(userDetails, 25)).thenReturn(Collections.emptyList());
|
||||
mockAuthenticatedUser();
|
||||
when(opdsBookService.getRandomBooks(TEST_USER_ID, 25)).thenReturn(Collections.emptyList());
|
||||
|
||||
String xml = opdsFeedService.generateSurpriseFeed(request);
|
||||
assertThat(xml).contains("</feed>");
|
||||
@@ -255,7 +258,6 @@ class OpdsFeedServiceTest {
|
||||
|
||||
@Test
|
||||
void escapeXml_shouldEscapeSpecialCharacters() throws Exception {
|
||||
// Use reflection to access private method
|
||||
var method = OpdsFeedService.class.getDeclaredMethod("escapeXml", String.class);
|
||||
method.setAccessible(true);
|
||||
String input = "a&b<c>d\"e'f";
|
||||
@@ -281,4 +283,26 @@ class OpdsFeedServiceTest {
|
||||
assertThat(invalid).isEqualTo(42L);
|
||||
assertThat(missing).isEqualTo(42L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getUserId_shouldReturnUserId() throws Exception {
|
||||
mockAuthenticatedUser();
|
||||
|
||||
var method = OpdsFeedService.class.getDeclaredMethod("getUserId");
|
||||
method.setAccessible(true);
|
||||
Long userId = (Long) method.invoke(opdsFeedService);
|
||||
|
||||
assertThat(userId).isEqualTo(TEST_USER_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getUserId_shouldReturnNullWhenNotAuthenticated() throws Exception {
|
||||
when(authenticationService.getOpdsUser()).thenReturn(null);
|
||||
|
||||
var method = OpdsFeedService.class.getDeclaredMethod("getUserId");
|
||||
method.setAccessible(true);
|
||||
Long userId = (Long) method.invoke(opdsFeedService);
|
||||
|
||||
assertThat(userId).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user