private static void testResult(final ProductProjectionSearch search, final Consumer<List<String>> testFilter, final Consumer<List<TermStats>> testColors, final Consumer<List<TermStats>> testSizes) { assertEventually(Duration.ofSeconds(45), Duration.ofMillis(200), () -> { final PagedSearchResult<ProductProjection> result = executeSearch(search); testFilter.accept(toIds(result.getResults())); testColors.accept(result.getFacetResult(COLOR_FACETED_SEARCH).getTerms()); testSizes.accept(result.getFacetResult(SIZE_FACETED_SEARCH).getTerms()); }); } }
@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 filteredFacetResult() throws Exception { testPagedSearchResult(result -> { assertThat(result.getFacetResult(FILTERED_FACET_EXPR)).isEqualTo(FILTERED_FACET_RESULT); assertThat(result.getFilteredFacetResult("non-existent")).isNull(); assertThatThrownBy(() -> result.getFilteredFacetResult(TERM_FACET_EXPR.resultPath())) .isInstanceOf(IllegalArgumentException.class); assertThatThrownBy(() -> result.getFilteredFacetResult(RANGE_FACET_EXPR.resultPath())) .isInstanceOf(IllegalArgumentException.class); }); }
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 termFacetResult() throws Exception { testPagedSearchResult(result -> { assertThat(result.getFacetResult(TERM_FACET_EXPR)).isEqualTo(TERM_FACET_RESULT); assertThat(result.getTermFacetResult("non-existent")).isNull(); assertThatThrownBy(() -> result.getTermFacetResult(RANGE_FACET_EXPR.resultPath())) .isInstanceOf(IllegalArgumentException.class); assertThatThrownBy(() -> result.getTermFacetResult(FILTERED_FACET_EXPR.resultPath())) .isInstanceOf(IllegalArgumentException.class); }); }
private static Set<ProductProjection> resultToProductSet(final PagedSearchResult<ProductProjection> result) { return result.getResults().stream().collect(toSet()); } }
public Optional<FilteredFacetResult> getFilteredFacetResult(final FilteredFacetExpression<T, ?> facetExpression) { return getFacetResult(facetExpression.resultPath()).map(facetResult -> { if (facetResult instanceof FilteredFacetResult) { return (FilteredFacetResult) facetResult; } else { // TODO Use another type of exception once exception task is finished throw new RuntimeException("Facet result is not of type FilterFacetResult: " + facetResult); } }); } }
@Test public void rangeFacetResult() throws Exception { testPagedSearchResult(result -> { assertThat(result.getFacetResult(RANGE_FACET_EXPR)).isEqualTo(RANGE_FACET_RESULT); assertThat(result.getRangeFacetResult("non-existent")).isNull(); assertThatThrownBy(() -> result.getRangeFacetResult(TERM_FACET_EXPR.resultPath())) .isInstanceOf(IllegalArgumentException.class); assertThatThrownBy(() -> result.getRangeFacetResult(FILTERED_FACET_EXPR.resultPath())) .isInstanceOf(IllegalArgumentException.class); }); }
@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"); }); }); }
@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 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))); }); }); }
@Override public boolean matches(final PagedSearchResult<ProductProjection> value) { return value.getTotal() > 0; } };
@Test public void parsesFilteredFacetResults() throws Exception { final FilteredFacetResult filteredFacet = searchResult.getFilteredFacetResult(FILTERED_FACET_KEY); assertThat(filteredFacet.getCount()).isEqualTo(2); }
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 searchByFullLocale() { final Locale austria = Locale.forLanguageTag("de-AT"); final String marille = "Marille"; final LocalizedString apricot = LocalizedString .of(GERMAN, "Aprikose") .plus(austria, marille) .plus(ENGLISH, "apricot"); withProduct(client(), productBuilder -> productBuilder.name(apricot), product -> { final ProductProjectionSearch austrianSearchRequest = ProductProjectionSearch.ofStaged() .withText(austria, marille) .plusQueryFilters(productModel -> productModel.id().is(product.getId())); final ProductProjectionSearch germanSearchRequest = austrianSearchRequest.withText(GERMAN, marille); assertEventually(() -> { final List<ProductProjection> results = client().executeBlocking(austrianSearchRequest).getResults(); assertThat(results).isNotEmpty(); assertThat(results.get(0).getId()).isEqualTo(product.getId()); assertThat(client().executeBlocking(germanSearchRequest).head()) .as("word is for austria defined and should not be found for just 'de'") .isEmpty(); }); }); } }
@Test public void parsesTermFacetResultsProductCounts() throws Exception { final TermFacetResult termFacet = searchResult.getTermFacetResult(TERM_FACET_COUNTING_PRODUCTS_KEY); assertThat(termFacet.getTerms()).hasSize(2); assertThat(termFacet.getTerms().get(0).getProductCount()).isEqualTo(372); assertThat(termFacet.getTerms().get(1).getProductCount()).isEqualTo(195); }
@Test public void parsesRangeFacetResultsProductCounts() throws Exception { final RangeFacetResult rangeFacet = searchResult.getRangeFacetResult(RANGE_FACET_COUNTING_PRODUCTS_KEY); assertThat(rangeFacet.getRanges()).hasSize(2); assertThat(rangeFacet.getRanges().get(0).getProductCount()).isEqualTo(2302); assertThat(rangeFacet.getRanges().get(1).getProductCount()).isEqualTo(978); } }
private static void testResultWithRange(final RangeFacetedSearchExpression<ProductProjection> facetedSearchExpr, final Consumer<List<String>> testFilter, final Consumer<SimpleRangeStats> rangeStats) { final PagedSearchResult<ProductProjection> result = executeFacetedSearch(facetedSearchExpr, testFilter); rangeStats.accept(result.getRangeStatsOfAllRanges(facetedSearchExpr)); }
protected Result renderGoodHealthStatus(final PagedSearchResult<ProductProjection> productResult) { final boolean containsProducts = !productResult.getResults().isEmpty(); if (!containsProducts) { throw new RuntimeException("Cannot find any product!"); } return ok(healthReportAsJson(true)); }
@SuppressWarnings("unchecked") public <V> Optional<TermFacetResult<V>> getTermFacetResult(final TermFacetExpression<T, V> facetExpression) { return getFacetResult(facetExpression.resultPath()).map(facetResult -> { if (facetResult instanceof TermFacetResult) { return (TermFacetResult) facetResult; } else { // TODO Use another type of exception once exception task is finished throw new RuntimeException("Facet result is not of type TermFacetResult: " + facetResult); } }); }