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); }); }
private Price searchWith(final Product product, final PriceSelection selection) { final ProductProjectionSearch searchRequest = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.id().is(product.getId()))//to limit the test scope .withPriceSelection(selection);//price selection config final PagedSearchResult<ProductProjection> searchResult = client().executeBlocking(searchRequest); assertThat(searchResult.getCount()).isEqualTo(1); return searchResult.head().get().getMasterVariant().getPrice(); }
@Test public void selectAPriceByCurrency() { final List<PriceDraft> prices = asList(PriceDraft.of(EURO_30), PriceDraft.of(USD_20)); withProductOfPrices(prices, product -> { final ProductProjectionSearch searchRequest = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.id().is(product.getId()))//to limit the test scope .withPriceSelection(PriceSelection.of(EUR));//price selection config assertEventually(() -> { final PagedSearchResult<ProductProjection> result = client().executeBlocking(searchRequest); assertThat(result.getCount()).isEqualTo(1); final ProductVariant masterVariant = result.getResults().get(0).getMasterVariant(); assertThat(masterVariant.getPrice()).isNotNull().has(price(PriceDraft.of(EURO_30))); }); }); }
@Test public void searchByCount() { final ProductProjectionSearch projectionSearch = ProductProjectionSearch.ofStaged()//in prod it would be current .withResultFilters(m -> m.reviewRatingStatistics().count().isGreaterThanOrEqualTo(new BigDecimal(2))) .plusResultFilters(m -> m.reviewRatingStatistics().highestRating().isGreaterThanOrEqualTo(new BigDecimal(2))) .plusResultFilters(m -> m.reviewRatingStatistics().lowestRating().isGreaterThanOrEqualTo(new BigDecimal(0))) .plusFacets(m -> m.reviewRatingStatistics().count().allRanges()) .plusFacets(m -> m.reviewRatingStatistics().highestRating().allRanges()) .plusFacets(m -> m.reviewRatingStatistics().lowestRating().allRanges()) ; assertEventually(Duration.ofSeconds(60), Duration.ofMillis(100), () -> { softAssert(soft -> { final PagedSearchResult<ProductProjection> res = client().executeBlocking(projectionSearch); soft.assertThat(res.getCount()).isGreaterThanOrEqualTo(3); final RangeFacetResult countFacets = (RangeFacetResult) res.getFacetResult("reviewRatingStatistics.count"); soft.assertThat(countFacets.getRanges().get(1).getSum()).as("count facets").isEqualTo("60.0"); final RangeFacetResult lowestRatingFacets = (RangeFacetResult) res.getFacetResult("reviewRatingStatistics.lowestRating"); soft.assertThat(lowestRatingFacets.getRanges().get(1).getSum()).as("lowestRating facets").isEqualTo("0.0"); final RangeFacetResult highestRatingFacets = (RangeFacetResult) res.getFacetResult("reviewRatingStatistics.highestRating"); soft.assertThat(highestRatingFacets.getRanges().get(1).getSum()).as("highestRating facets").isEqualTo("14.0"); }); }); }