@Override default Reference<Product> toReference() { return Product.reference(getId()); }
@Override default Reference<Product> toReference() { return Product.reference(getId()); }
@Override default Reference<Product> toReference() { return Product.reference(getId()); }
@Override default Reference<Product> toReference() { return Product.reference(getId()); }
@Override default Reference<Product> toReference() { return Product.reference(getId()); }
default Optional<ProductVariant> findVariant(final VariantIdentifier identifier){ return getId().equals(identifier.getProductId()) ? Optional.ofNullable(getVariant(identifier.getVariantId())) : Optional.empty(); }
default Optional<ProductVariant> findVariant(final ByIdVariantIdentifier identifier){ return getId().equals(identifier.getProductId()) ? Optional.ofNullable(getVariant(identifier.getVariantId())) : Optional.empty(); }
default Optional<ProductVariant> findVariant(final ByIdVariantIdentifier identifier){ return getId().equals(identifier.getProductId()) ? Optional.ofNullable(getVariant(identifier.getVariantId())) : Optional.empty(); }
default Versioned<Product> toProductVersioned() { return Versioned.of(getId(), getVersion()); } }
default Versioned<Product> toProductVersioned() { return Versioned.of(getId(), getVersion()); }
private Set<String> ids(final PagedQueryResult<ProductProjection> res) { return res.getResults().stream().map(p -> p.getId()).collect(toSet()); }
@Test public void execution() { final String key = randomKey(); ProductFixtures.withProduct(client(), builder -> builder.key(key), product -> { final ProductProjectionByKeyGet request = ProductProjectionByKeyGet.ofStaged(key); final ProductProjection productProjection = client().executeBlocking(request); assertThat(productProjection.getId()).isEqualTo(product.getId()); }); } }
@Test public void execution() throws Exception { withProduct(client(), "getProductProjectionById", product -> { final ProductProjectionType projectionType = STAGED; final String productId = product.getId(); final ProductProjectionByIdGet sphereRequest = ProductProjectionByIdGet.of(productId, projectionType); final ProductProjection productProjection = client().executeBlocking(sphereRequest); final String fetchedProjectionId = productProjection.getId(); assertThat(fetchedProjectionId).isEqualTo(productId); assertThat(productProjection.getCategories()).isEqualTo(product.getMasterData().get(projectionType).getCategories()); }); } }
@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 sortByRestockableInDaysWithSupplyChannel() { withChannelOfRole(client(), ChannelRole.INVENTORY_SUPPLY, channel -> { withProductOfRestockableInDaysAndChannel(client(), 4, channel, product4 -> { withProductOfRestockableInDaysAndChannel(client(), 9, channel, product9 -> { final ProductProjectionSearch request = ProductProjectionSearch.ofStaged() .plusQueryFilters(m -> m.id().isIn(asList(product4.getId(), product9.getId()))) .plusSort(m -> m.allVariants().availability().channels().channelId(channel.getId()).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 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 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(); }); }); }
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 queryByReviewRating() { withProduct(client(), product -> { withReview(client(), b -> b.target(product).rating(1), review1 -> { withReview(client(), b -> b.target(product).rating(3), review2 -> { assertEventually(Duration.ofSeconds(60), Duration.ofMillis(200), () -> { final ProductProjectionQuery query = ProductProjectionQuery.ofStaged() .withPredicates(m -> m.reviewRatingStatistics().averageRating().is(2.0)) .plusPredicates(m -> m.reviewRatingStatistics().count().is(2)) .plusPredicates(m -> m.reviewRatingStatistics().lowestRating().is(1)) .plusPredicates(m -> m.reviewRatingStatistics().highestRating().is(3)) .plusPredicates(m -> m.is(product)); final List<ProductProjection> results = client().executeBlocking(query).getResults(); assertThat(results).hasSize(1); assertThat(results.get(0).getId()).isEqualTo(product.getId()); }); }); }); }); }