@Override public boolean matches(final PagedSearchResult<ProductProjection> value) { return value.getTotal() > 0; } };
public static void logProductRequest(final Logger logger, final ProductProjectionSearch request, final PagedSearchResult<ProductProjection> result) { final HttpRequestIntent httpRequest = request.httpRequestIntent(); final String requestBody = printableRequestBody(httpRequest) .map(body -> " with body {" + body + "}") .orElse(""); logger.debug("Fetched {} out of {} products with request {} {}", result.getCount(), result.getTotal(), httpRequest.getHttpMethod(), httpRequest.getPath() + requestBody); }
@Test public void paginationExample() { final long offset = 10; final long limit = 25; final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .withOffset(offset) .withLimit(limit); testResult(search, result -> { final long remainingProducts = max(result.getTotal() - offset, 0); final long expectedProducts = min(limit, remainingProducts); assertThat(result.getCount()).as("size").isEqualTo(expectedProducts); assertThat(result.getOffset()).as("offset").isEqualTo(offset); }); }
@Test public void filterByEvilCharacterWord() throws Exception { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .plusQueryFilters(productModel -> productModel.allVariants().attribute().ofString(ATTR_NAME_EVIL).is(EVIL_CHARACTER_WORD)); assertEventually(Duration.ofSeconds(60), Duration.ofMillis(200), () -> { final PagedSearchResult<ProductProjection> result = executeEvilSearch(search); assertThat(result.getTotal()).as("total").isEqualTo(1); }); }
@Test public void id() { final ProductProjectionSearch baseRequest = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.id().isIn(getAllIds())); final ProductProjectionSearch asc = baseRequest.withSort(m -> m.id().asc()); final ProductProjectionSearch desc = baseRequest.withSort(m -> m.id().desc()); assertEventually(() -> { final PagedSearchResult<ProductProjection> ascResult = client().executeBlocking(asc); assertThat(ascResult.getTotal()).isEqualTo(getAllIds().size()); final PagedSearchResult<ProductProjection> descResult = client().executeBlocking(desc); assertThat(descResult.getTotal()).isEqualTo(getAllIds().size()); final LinkedList<ProductProjection> reversedDesc = new LinkedList<>(descResult.getResults()); Collections.reverse(reversedDesc); assertThat(ascResult.getResults()).isEqualTo(reversedDesc); }); }
@Test public void score() { final ProductProjectionSearch baseRequest = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.id().isIn(getAllIds())); final ProductProjectionSearch asc = baseRequest.withSort(m -> m.score().asc()); assertEventually(() -> { final PagedSearchResult<ProductProjection> ascResult = client().executeBlocking(asc); assertThat(ascResult.getTotal()).isEqualTo(getAllIds().size()); }); }