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); }
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(); }
protected static PagedSearchResult<ProductProjection> executeSearch(final ProductProjectionSearch search) { final List<String> ids = asList(product1.getId(), product2.getId(), product3.getId()); final ProductProjectionSearch sphereRequest = search.plusQueryFilters(product -> product.id().isIn(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(); }); }); }
@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()); }); }
@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); }); }
protected static PagedSearchResult<ProductProjection> executeSearch(final ProductProjectionSearch search) { final List<String> ids = asList(product1.getId(), product2.getId()); final ProductProjectionSearch sphereRequest = search.plusQueryFilters(productModel -> productModel.id().isIn(ids)); return client().executeBlocking(sphereRequest); }
@Test public void sortByRestockableInDays() { withProductOfRestockableInDaysAndChannel(client(), 4, null, product4 -> { withProductOfRestockableInDaysAndChannel(client(), 9, null, product9 -> { final ProductProjectionSearch request = ProductProjectionSearch.ofStaged() .plusQueryFilters(m -> m.id().isIn(asList(product4.getId(), product9.getId()))) .plusSort(m -> m.allVariants().availability().restockableInDays().asc()); assertEventually(() -> { final PagedSearchResult<ProductProjection> res = client().executeBlocking(request); assertThat(res.getResults()).hasSize(2); assertThat(res.getResults().get(0).getId()) .isEqualTo(product4.getId()); }); }); }); }
@Test public void sortByAvailableQuantity() { withProductOfStock(client(), 10, productWith10Items -> { withProductOfStock(client(), 5, productWith5Items -> { final ProductProjectionSearch request = ProductProjectionSearch.ofStaged() .plusQueryFilters(m -> m.id().isIn(asList(productWith5Items.getId(), productWith10Items.getId()))) .plusSort(m -> m.allVariants().availability().availableQuantity().asc()); assertEventually(() -> { final PagedSearchResult<ProductProjection> res = client().executeBlocking(request); assertThat(res.getResults()).hasSize(2); assertThat(res.getResults().get(0).getId()) .isEqualTo(productWith5Items.getId()); }); }); }); }
@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 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 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 sortByAvailableQuantityWithSupplyChannel() { withChannelOfRole(client(), ChannelRole.INVENTORY_SUPPLY, channel -> { withProductOfStockAndChannel(client(), 10, channel, productWith10Items -> { withProductOfStockAndChannel(client(), 5, channel, productWith5Items -> { final ProductProjectionSearch request = ProductProjectionSearch.ofStaged() .plusQueryFilters(m -> m.id().isIn(asList(productWith5Items.getId(), productWith10Items.getId()))) .plusSort(m -> m.allVariants().availability().channels().channelId(channel.getId()).availableQuantity().asc()); assertEventually(() -> { final PagedSearchResult<ProductProjection> res = client().executeBlocking(request); assertThat(res.getResults()).hasSize(2); assertThat(res.getResults().get(0).getId()) .isEqualTo(productWith5Items.getId()); }); }); }); }); }
@Test public void createPublishedProduct() { withEmptyProductType(client(), randomKey(), productType -> { final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().build(); final LocalizedString name = randomSlug(); final LocalizedString slug = randomSlug(); final ProductDraft productDraft = ProductDraftBuilder.of(productType, name, slug, masterVariant) .publish(true) .build(); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); assertThat(product.getMasterData().isPublished()).isTrue(); assertThat(product.getMasterData().getCurrent().getSlug()).isEqualTo(slug); assertEventually(() -> { final ProductProjectionSearch search = ProductProjectionSearch.ofCurrent() .withQueryFilters(m -> m.id().is(product.getId())); final PagedSearchResult<ProductProjection> searchResult = client().executeBlocking(search); assertThat(searchResult.getResults()).hasSize(1); }); unpublishAndDelete(product); }); }
private void sortCheck(final List<PriceDraft> prices1, final List<PriceDraft> prices2, final SortExpression<ProductProjection> sortExpression) { withProductOfPrices(prices1, product1 -> { withProductOfPrices(prices2, product2 -> { final ProductProjectionSearch search = ProductProjectionSearch.ofStaged() .withPriceSelection(PriceSelection.of(EUR).withPriceCountry(DE)) .plusQueryFilters(m -> m.id().isIn(asList(product1.getId(), product2.getId()))) .plusSort(sortExpression); assertEventually(() -> { final PagedSearchResult<ProductProjection> result = client().executeBlocking(search); assertThat(result.getResults()).extracting(ResourceView::getId) .as("product1 with small price included").containsExactly(product1.getId(), product2.getId()); }); }); }); }
private void checkFilter(final UnaryOperator<ProductDraftBuilder> productDraftBuilderUnaryOperator, final Function<ProductProjectionFilterSearchModel, ExistsAndMissingFilterSearchModelSupport<ProductProjection>> dsl) { withProduct(client(), productDraftBuilderUnaryOperator, productWith -> { withProduct(client(), builder -> builder.masterVariant(ProductVariantDraftBuilder.of(builder.getMasterVariant()).sku(null).build()), productWithout -> { assertThat(productWithout.getMasterData().getStaged().getMasterVariant().getSku()).as("sku for productWithout is missing").isNull(); final ProductProjectionSearch baseRequest = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> { final List<String> productIds = asList(productWith.getId(), productWithout.getId()); return m.id().isIn(productIds); }); final ProductProjectionSearch exists = baseRequest.plusQueryFilters(m -> dsl.apply(m).exists()); final ProductProjectionSearch missing = baseRequest.plusQueryFilters(m -> dsl.apply(m).missing()); assertEventually(() -> { assertThat(client().executeBlocking(exists).getResults()).hasSize(1).extracting(s -> s.getId()).as("exists").containsExactly(productWith.getId()); assertThat(client().executeBlocking(missing).getResults()).hasSize(1).extracting(s -> s.getId()).as("missing").containsExactly(productWithout.getId()); }); }); }); } }
@Test public void searchForIsOnStockWithChannel() { withChannelOfRole(client(), ChannelRole.INVENTORY_SUPPLY, channel -> { withProductOfStockAndChannel(client(), 2, channel, product -> { final ProductProjectionSearch request = ProductProjectionSearch.ofStaged() .plusQueryFilters(m -> m.id().is(product.getId())) .plusQueryFilters(m -> m.allVariants().availability() .channels().channelId(channel.getId()).isOnStock().is(true)); assertEventually(() -> { final PagedSearchResult<ProductProjection> res = client().executeBlocking(request); assertThat(res.getResults()).hasSize(1); }); }); }); }
@Test public void is() { final List<String> categoryIds1 = getCategoryIds("A", "B-1", "C-2-2"); final List<String> categoryIds2 = getCategoryIds("A", "B-1"); final List<String> categoryIds3 = getCategoryIds("A", "B", "C"); withProductInCategories(client(), categoryIds1, (Product product1) -> { withProductInCategories(client(), categoryIds2, (Product product2) -> { withProductInCategories(client(), categoryIds3, (Product product3) -> { assertEventually(() -> { final ProductProjectionSearch request = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.categories().id().containsAny(getCategoryIds("B-1"))) .plusQueryFilters(m -> m.id().isIn(asList(product1.getId(), product2.getId(), product3.getId()))); assertThat(client().executeBlocking(request)).has(onlyProducts(product1, product2)); }); }); }); }); }
@Test public void containsAnyIncludingSubtrees() { final List<String> categoryIds1 = getCategoryIds("A", "B-1", "C-2-2"); final List<String> categoryIds2 = getCategoryIds("A", "B-1"); final List<String> categoryIds3 = getCategoryIds("A", "B", "C"); withProductInCategories(client(), categoryIds1, (Product product1) -> { withProductInCategories(client(), categoryIds2, (Product product2) -> { withProductInCategories(client(), categoryIds3, (Product product3) -> { assertEventually(() -> { final ProductProjectionSearch request = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.categories().id().containsAnyIncludingSubtrees(getCategoryIds("C-2"))) .plusQueryFilters(m -> m.id().isIn(asList(product1.getId(), product2.getId(), product3.getId()))); assertThat(client().executeBlocking(request)).has(onlyProducts(product1)); }); }); }); }); }
@Test public void containsAllIncludingSubtrees() { final List<String> categoryIds1 = getCategoryIds("B-1", "C-2-2"); final List<String> categoryIds2 = getCategoryIds("B-1-2", "C-1-3"); final List<String> categoryIds3 = getCategoryIds("B-3", "C"); withProductInCategories(client(), categoryIds1, (Product product1) -> { withProductInCategories(client(), categoryIds2, (Product product2) -> { withProductInCategories(client(), categoryIds3, (Product product3) -> { assertEventually(() -> { final ProductProjectionSearch request = ProductProjectionSearch.ofStaged() .withQueryFilters(m -> m.categories().id().containsAllIncludingSubtrees(getCategoryIds("C", "B-1"))) .plusQueryFilters(m -> m.id().isIn(asList(product1.getId(), product2.getId(), product3.getId()))); assertThat(client().executeBlocking(request)).has(onlyProducts(product1, product2)); }); }); }); }); }