feat(api): add paged books random sort (#2451)
This commit is contained in:
@@ -13,14 +13,14 @@ class CursorCodecTest {
|
||||
|
||||
@Test
|
||||
void roundTripsBasicState() {
|
||||
CursorState state = new CursorState(80, 40, "seriesName,-seriesNumber", "abc123def456");
|
||||
CursorState state = new CursorState(80, 40, "seriesName,-seriesNumber", "abc123def456", 123);
|
||||
CursorState decoded = codec.decode(codec.encode(state));
|
||||
assertEquals(state, decoded);
|
||||
}
|
||||
|
||||
@Test
|
||||
void producesUrlSafeUnpaddedToken() {
|
||||
CursorState state = new CursorState(12345, 40, "title", "hash00000000");
|
||||
CursorState state = new CursorState(12345, 40, "title", "hash00000000", 0);
|
||||
String cursor = codec.encode(state);
|
||||
assertEquals(cursor.indexOf('+'), -1);
|
||||
assertEquals(cursor.indexOf('/'), -1);
|
||||
@@ -41,13 +41,13 @@ class CursorCodecTest {
|
||||
|
||||
@Test
|
||||
void verifyParamsMatchPassesOnEqualHash() {
|
||||
CursorState state = new CursorState(0, 20, "title", "samehash0000");
|
||||
CursorState state = new CursorState(0, 20, "title", "samehash0000", 0);
|
||||
assertDoesNotThrow(() -> codec.verifyParamsMatch(state, "samehash0000"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void verifyParamsMatchThrowsOnDifferentHash() {
|
||||
CursorState state = new CursorState(0, 20, "title", "samehash0000");
|
||||
CursorState state = new CursorState(0, 20, "title", "samehash0000", 0);
|
||||
assertThrows(APIException.class, () -> codec.verifyParamsMatch(state, "otherhash000"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ class LinksBuilderTest {
|
||||
private final LinksBuilder builder = new LinksBuilder(codec);
|
||||
|
||||
private LinksBuilder.Context context(long offset, int limit, long total, String preservedQuery) {
|
||||
CursorState base = new CursorState(offset, limit, "title", "hash00000000");
|
||||
CursorState base = new CursorState(offset, limit, "title", "hash00000000", 0);
|
||||
return new LinksBuilder.Context("/api/v1/books/page", "/api/v1/books/facets", preservedQuery, offset, limit, total, base);
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ class LinksBuilderTest {
|
||||
void nullPreservedQueryStillProducesValidLinks() {
|
||||
List<Link> links = builder.build(new LinksBuilder.Context(
|
||||
"/api/v1/books/page", "/api/v1/books/facets", null, 0, 20, 5,
|
||||
new CursorState(0, 20, "title", "hash00000000")));
|
||||
new CursorState(0, 20, "title", "hash00000000", 0)));
|
||||
assertTrue(hasRel(links, "self"));
|
||||
Link self = withRel(links, "self");
|
||||
assertTrue(self.href().startsWith("/api/v1/books/page?cursor="));
|
||||
|
||||
@@ -7,6 +7,8 @@ import jakarta.persistence.criteria.Root;
|
||||
import org.booklore.exception.APIException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Captor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
@@ -18,6 +20,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@MockitoSettings(strictness = Strictness.LENIENT)
|
||||
@@ -30,6 +33,12 @@ class SortRegistryTest {
|
||||
@Mock
|
||||
private CriteriaBuilder cb;
|
||||
|
||||
@Mock
|
||||
SortOrderBuilder<Object> mockSortOrderBuilder;
|
||||
|
||||
@Captor
|
||||
ArgumentCaptor<SortContext<Object>> sortContextCaptor;
|
||||
|
||||
@Test
|
||||
void registersAndExposesKeysInOrder() {
|
||||
SortRegistry<Object> registry = new SortRegistry<>()
|
||||
@@ -49,32 +58,44 @@ class SortRegistryTest {
|
||||
.register("id", ctx -> List.of(idOrder));
|
||||
|
||||
List<Order> orders = registry.toOrders(
|
||||
List.of(new SortTerm("title", false), new SortTerm("id", false)), root, query, cb, 7L);
|
||||
List.of(new SortTerm("title", false), new SortTerm("id", false)),
|
||||
root,
|
||||
query,
|
||||
cb,
|
||||
7L,
|
||||
null
|
||||
);
|
||||
|
||||
assertEquals(List.of(titleOrder, idOrder), orders);
|
||||
}
|
||||
|
||||
@Test
|
||||
void passesDirectionAndUserIdToBuilder() {
|
||||
boolean[] sawDescending = {false};
|
||||
Long[] sawUser = {null};
|
||||
SortRegistry<Object> registry = new SortRegistry<>().register("title", ctx -> {
|
||||
sawDescending[0] = ctx.descending();
|
||||
sawUser[0] = ctx.userId();
|
||||
return List.of(mock(Order.class));
|
||||
});
|
||||
void passesParametersToBuilder() {
|
||||
SortRegistry<Object> registry = new SortRegistry<>().register("title", mockSortOrderBuilder);
|
||||
|
||||
registry.toOrders(List.of(new SortTerm("title", true)), root, query, cb, 42L);
|
||||
registry.toOrders(List.of(new SortTerm("title", true)), root, query, cb, 42L, 123);
|
||||
|
||||
assertTrue(sawDescending[0]);
|
||||
assertEquals(42L, sawUser[0]);
|
||||
verify(mockSortOrderBuilder).toOrders(sortContextCaptor.capture());
|
||||
|
||||
var context = sortContextCaptor.getValue();
|
||||
|
||||
assertTrue(context.descending());
|
||||
assertEquals(42L, context.userId());
|
||||
assertEquals(123, context.randomSeed());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownKeyThrows() {
|
||||
SortRegistry<Object> registry = new SortRegistry<>().register("id", ascOn("id"));
|
||||
assertThrows(APIException.class,
|
||||
() -> registry.toOrders(List.of(new SortTerm("bogus", false)), root, query, cb, null));
|
||||
() -> registry.toOrders(
|
||||
List.of(new SortTerm("bogus", false)),
|
||||
root,
|
||||
query,
|
||||
cb,
|
||||
null,
|
||||
null
|
||||
));
|
||||
}
|
||||
|
||||
private SortOrderBuilder<Object> ascOn(String attribute) {
|
||||
|
||||
@@ -152,7 +152,7 @@ class BookBrowseRegistryTest {
|
||||
private List<Long> sortedIds(String sortString, Long userId) {
|
||||
List<SortTerm> terms = SortParser.parse(sortString, sortRegistry.registry().keys());
|
||||
Specification<BookEntity> spec = (root, query, cb) -> {
|
||||
query.orderBy(sortRegistry.registry().toOrders(terms, root, query, cb, userId));
|
||||
query.orderBy(sortRegistry.registry().toOrders(terms, root, query, cb, userId, null));
|
||||
return cb.conjunction();
|
||||
};
|
||||
return bookRepository.findAll(spec).stream().map(BookEntity::getId).toList();
|
||||
|
||||
@@ -4,6 +4,7 @@ import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import org.booklore.BookloreApplication;
|
||||
import org.booklore.browse.BrowsePage;
|
||||
import org.booklore.browse.CursorCodec;
|
||||
import org.booklore.browse.Link;
|
||||
import org.booklore.exception.APIException;
|
||||
import org.booklore.config.security.service.AuthenticationService;
|
||||
@@ -205,6 +206,17 @@ class BookBrowseServiceTest {
|
||||
.doesNotContainAnyElementsOf(page0.content().stream().map(Book::getId).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void generatesRandomSeedForCursor() {
|
||||
var page = browse("random", null, null, null, 0, 2);
|
||||
String cursor = page.page().cursor();
|
||||
|
||||
var codec = new CursorCodec();
|
||||
var values = codec.decode(cursor);
|
||||
|
||||
assertThat(values.randomSeed()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void cursorWithConflictingFacetsIsRejected() {
|
||||
book("Alpha", List.of("Horror"));
|
||||
@@ -215,13 +227,28 @@ class BookBrowseServiceTest {
|
||||
.hasMessageContaining("does not match");
|
||||
}
|
||||
|
||||
@Test
|
||||
void randomSortIsSameWithSameCursor() {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
book(String.format("Book%02d", i), List.of());
|
||||
}
|
||||
em.flush();
|
||||
|
||||
var response = browse("random", null, null, null, 0, 20);
|
||||
|
||||
var ids = response.content().stream().map(Book::getId).toList();
|
||||
|
||||
assertThat(browse("random", null, null, response.page().cursor(), 0, 20)
|
||||
.content().stream().map(Book::getId).toList()).isEqualTo(ids);
|
||||
|
||||
assertThat(browse("random", null, null, response.page().cursor(), 0, 20)
|
||||
.content().stream().map(Book::getId).toList()).isEqualTo(ids);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deferredSortKeyIsRejected() {
|
||||
book("Alpha", List.of());
|
||||
em.flush();
|
||||
assertThatThrownBy(() -> browse("random", null, null, null, 0, 20))
|
||||
.isInstanceOfSatisfying(APIException.class, e -> assertThat(e.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST))
|
||||
.hasMessageContaining("Unknown sort key");
|
||||
assertThatThrownBy(() -> browse("fileSizeKb", null, null, null, 0, 20))
|
||||
.isInstanceOfSatisfying(APIException.class, e -> assertThat(e.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST))
|
||||
.hasMessageContaining("Unknown sort key");
|
||||
|
||||
Reference in New Issue
Block a user