public CompletionStage<Result> showHealth() throws IOException { final ProductProjectionSearch productRequest = ProductProjectionSearch.ofCurrent().withLimit(1); return sphere.execute(productRequest) .thenApplyAsync(this::renderGoodHealthStatus, HttpExecution.defaultContext()) .exceptionally(this::renderBadHealthStatus) .thenApply(result -> result.as(Http.MimeTypes.JSON)); }
@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 allowsExpansion() throws Exception { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged().withLimit(1L); testResult(search, result -> { final List<ProductProjection> results = result.getResults(); assertThat(results).isNotEmpty(); assertThat(results.get(0).getProductType().getObj()).isNull(); }); testResult(search.withExpansionPaths(product -> { return product.productType(); }), result -> { final List<ProductProjection> results = result.getResults(); assertThat(results).isNotEmpty(); assertThat(results.get(0).getProductType().getObj()).isEqualTo(productType); }); }
@Test public void resultsArePaginated() throws Exception { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .plusQueryFilters(productModel -> productModel.allVariants().attribute().ofString(ATTR_NAME_COLOR).isIn(asList("blue", "red"))) .withSort(sort -> sort.name().locale(ENGLISH).desc()) .withOffset(1L) .withLimit(1L); testResultIds(search, resultIds -> assertThat(resultIds).containsOnly(product2.getId())); }
/** * Gets the products belonging to the given categories, up to {@code numProducts}, most expensive products first. * @param categoryIds the category IDs to get products from * @param numProducts the number of products the returned set should contain * @return the products related to these categories */ private CompletionStage<Set<ProductProjection>> productsFromCategoryIds(final Iterable<String> categoryIds, final int numProducts) { final ProductProjectionSearch request = ProductProjectionSearch.ofCurrent() .withLimit(numProducts) .withQueryFilters(product -> product.categories().id().containsAny(categoryIds)) .withSort(product -> product.allVariants().price().desc()) .withPriceSelection(PriceUtils.createPriceSelection(userContext)); return sphereClient.execute(request) .whenCompleteAsync((result, t) -> logProductRequest(LOGGER, request, result), HttpExecution.defaultContext()) .thenApply(SunriseProductRecommendation::resultToProductSet); }