public ProductProjectionFilterSearchModel filter() { return new ProductProjectionFilterSearchModel(null, null); }
default ProductProjectionSearch bySlug(final Locale locale, final String slug) { return withQueryFilters(m -> m.slug().locale(locale).is(slug)); }
public RangeTermFilterSearchModel<ProductProjection, ZonedDateTime> createdAt() { return datetimeSearchModel("createdAt").filtered(); }
@Test public void filtersByAnyTerm() throws Exception { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .plusQueryFilters(productModel -> productModel.allVariants().attribute().ofNumber(ATTR_NAME_SIZE).isIn(asList(valueOf(36), valueOf(38)))); testResultIds(search, resultIds -> assertThat(resultIds).containsOnly(product1.getId(), product2.getId())); }
@Test public void searchForIsNotOnStock() { withProductOfStock(client(), 0, product -> { final ProductProjectionSearch request = ProductProjectionSearch.ofStaged() .plusQueryFilters(m -> m.id().is(product.getId())) .plusQueryFilters(m -> m.allVariants().availability().isOnStock().is(false)); assertEventually(() -> { final PagedSearchResult<ProductProjection> res = client().executeBlocking(request); assertThat(res.getResults()).hasSize(0); }); }); }
@Test public void categoriesMissing() { withCategory(client(), category -> { withProduct(client(), builder -> builder.categories(singleton(category.toReference())), productWithCategories -> { withProduct(client(), productWithoutCategories -> { final List<String> productIds = asList(productWithCategories.getId(), productWithoutCategories.getId()); final ProductProjectionSearch missing = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.id().isIn(productIds))//for test isolation .plusQueryFilters(m -> m.categories().missing()); assertEventually(() -> { final List<ProductProjection> results = client().executeBlocking(missing).getResults(); assertThat(results) .hasSize(1) .extracting(s -> s.getId()) .containsExactly(productWithoutCategories.getId()) .doesNotContain(productWithCategories.getId()); }); }); }); }); }
@Test public void categorySubtree() { final List<String> expressionsWithoutSubtrees = ProductProjectionSearch.ofStaged().withQueryFilters(m -> m.categories().id().isIn(asList("id1", "id2"))).queryFilters().stream().map(e -> e.expression()).collect(Collectors.toList()); assertThat(expressionsWithoutSubtrees.get(0)).isEqualTo("categories.id:\"id1\",\"id2\""); final List<FilterExpression<ProductProjection>> filterExpressions = ProductProjectionSearch.ofStaged().withQueryFilters(m -> m.categories().id().isInSubtreeOrInCategory(asList("id1", "id2"), asList("id3", "id4"))).queryFilters(); final List<String> collect = filterExpressions.stream().map(e -> e.expression()).collect(Collectors.toList()); final String expected = "categories.id:subtree(\"id1\"),subtree(\"id2\"),\"id3\",\"id4\""; assertThat(collect).hasSize(1); assertThat(collect.get(0)).isEqualTo(expected); }
protected static PagedSearchResult<ProductProjection> executeEvilSearch(final ProductProjectionSearch search) { final List<String> ids = asList(evilProduct1.getId(), evilProduct2.getId()); final ProductProjectionSearch sphereRequest = search.plusQueryFilters(product -> product.id().containsAny(ids)); return client().executeBlocking(sphereRequest); }
@Test public void forId() { final Function<ProductProjectionFilterSearchModel, ExistsAndMissingFilterSearchModelSupport<ProductProjection>> dsl = m -> m.state(); withProduct(client(), product -> { final ProductProjectionSearch baseRequest = ProductProjectionSearch.ofStaged() .withQueryFilters(m1 -> { final List<String> productIds = singletonList(product.getId()); return m1.id().isIn(productIds); }); final ProductProjectionSearch exists = baseRequest.plusQueryFilters(m -> m.id().exists()); final ProductProjectionSearch missing = baseRequest.plusQueryFilters(m -> m.id().missing()); assertEventually(() -> { assertThat(client().executeBlocking(exists).getResults()).hasSize(1) .extracting(s -> s.getId()).as("exists").containsExactly(product.getId()); assertThat(client().executeBlocking(missing).getResults()).isEmpty(); }); }); }
public LocalizedStringFilterSearchModel<ProductProjection> name() { return localizedStringFilterSearchModel("name"); }
@Test public void filterByProductKey() { final List<String> keys = Stream.of(product1, product2) .map(product -> product.getKey()) .collect(toList()); final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .plusQueryFilters(m -> m.key().isIn(keys)); assertEventually(Duration.ofSeconds(45), Duration.ofMillis(200), () -> { final List<ProductProjection> results = client().executeBlocking(search).getResults(); assertThat(results) .extracting(m -> m.getKey()) .containsOnly(keys.get(0), keys.get(1)); }); }
@Test public void canAccessLastModifiedAt() throws Exception { assertThat(FACET_MODEL.lastModifiedAt().allTerms().expression()).isEqualTo("lastModifiedAt"); assertThat(FILTER_MODEL.lastModifiedAt().is(dateTime("2001-09-11T22:05:09.203+00:00"))).extracting(expression()).containsExactly("lastModifiedAt:\"2001-09-11T22:05:09.203Z\""); assertThat(SORT_MODEL.lastModifiedAt().asc().expression()).isEqualTo("lastModifiedAt asc"); }
@Test public void canAccessCreatedAt() throws Exception { assertThat(FACET_MODEL.createdAt().allTerms().expression()).isEqualTo("createdAt"); assertThat(FILTER_MODEL.createdAt().is(dateTime("2001-09-11T22:05:09.203+00:00"))).extracting(expression()).containsExactly("createdAt:\"2001-09-11T22:05:09.203Z\""); assertThat(SORT_MODEL.createdAt().desc().expression()).isEqualTo("createdAt desc"); }
@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 searchForIsOnStock() { withProductOfStock(client(), 2, product -> { final ProductProjectionSearch request = ProductProjectionSearch.ofStaged() .plusQueryFilters(m -> m.id().is(product.getId())) .plusQueryFilters(m -> m.allVariants().availability().isOnStock().is(true)); assertEventually(() -> { final PagedSearchResult<ProductProjection> res = client().executeBlocking(request); assertThat(res.getResults()).hasSize(1); }); }); }
@Test public void categoriesExists() { withCategory(client(), category -> { withProduct(client(), builder -> builder.categories(singleton(category.toReference())), productWithCategories -> { withProduct(client(), productWithoutCategories -> { final List<String> productIds = asList(productWithCategories.getId(), productWithoutCategories.getId()); final ProductProjectionSearch exists = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.id().isIn(productIds))//for test isolation .plusQueryFilters(m -> m.categories().exists()); assertEventually(() -> { final List<ProductProjection> results = client().executeBlocking(exists).getResults(); assertThat(results) .hasSize(1) .extracting(s -> s.getId()) .containsExactly(productWithCategories.getId()) .doesNotContain(productWithoutCategories.getId()); }); }); }); }); }
@Test public void existFilter() { final List<FilterExpression<ProductProjection>> exists = FILTER_MODEL.categories().exists(); assertThat(exists).extracting(expression()).containsExactly("categories:exists"); }
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(); }
public LocalizedStringFilterSearchModel<ProductProjection> name() { return localizedStringFilterSearchModel("name"); }
@Test public void canAccessSku() throws Exception { final String sku = "sku-test"; assertThat(FILTER_MODEL.allVariants().sku().is(sku)).extracting(expression()).containsExactly("variants.sku:\"" + sku +"\""); assertThat(SORT_MODEL.allVariants().sku().asc().expression()).isEqualTo("variants.sku asc"); }