public QueryPredicate<ProductProjection> is(final Referenceable<Product> product) { return id().is(product.toReference().getId()); } }
private static QueryResult doQuery() { List<ProductProjection> sortedFromSearchForCategory1 = searchForCategoryAndSort(category1Id); List<ProductProjection> sortedFromQueryForCategory1 = queryForCategoryAndSort(category1Id); List<ProductProjection> sortedFromSearchForCategory2 = searchForCategoryAndSort(category2Id); List<ProductProjection> sortedFromQueryForCategory2 = queryForCategoryAndSort(category2Id); final List<String> ids = products.stream().map(p -> p.getId()).collect(toList()); final ProductProjectionQuery productProjectionQuery = ProductProjectionQuery.ofStaged() .withPredicates(productModel -> productModel.id().isIn(ids)); List<ProductProjection> productProjections = client().executeBlocking(productProjectionQuery).getResults(); return new QueryResult(productProjections, sortedFromSearchForCategory1, sortedFromSearchForCategory2, sortedFromQueryForCategory1, sortedFromQueryForCategory2); }
.withPredicates(m -> m.id().is(product.getId())) .plusExpansionPaths(m -> m.masterVariant().attributes().valueSet()) .plusExpansionPaths(m -> m.productType());
@Test public void variantIdentifierIsAvailable() throws Exception { withProduct(client(), product -> { final Query<ProductProjection> query = ProductProjectionQuery.of(STAGED) .withPredicates(m -> m.id().is(product.getId())); final ProductProjection productProjection = client().executeBlocking(query).head().get(); final ByIdVariantIdentifier identifier = productProjection.getMasterVariant().getIdentifier(); assertThat(identifier).isEqualTo(ByIdVariantIdentifier.of(product.getId(), 1)); }); }
@Test public void expandChannelInPrice() throws Exception { ChannelFixtures.withChannelOfRole(client(), ChannelRole.INVENTORY_SUPPLY, channel -> { withUpdateablePricedProduct(client(), PriceDraft.of(MoneyImpl.of(new BigDecimal("12.34"), EUR)).withCountry(DE).withChannel(channel), product -> { final Query<ProductProjection> query = ProductProjectionQuery.of(STAGED) .withPredicates(m -> m.id().is(product.getId())) .withExpansionPaths(m -> m.masterVariant().prices().channel()); final List<Price> prices = client().executeBlocking(query).head().get().getMasterVariant().getPrices(); assertThat(prices .stream() .anyMatch(price -> Optional.ofNullable(price.getChannel()).map(channelRef -> channelRef.getObj() != null).orElse(false))) .isTrue(); return product; }); }); }
@Test public void expandCustomerGroupInPrice() throws Exception { withCustomerGroup(client(), customerGroup -> { withUpdateablePricedProduct(client(), PriceDraft.of(MoneyImpl.of(new BigDecimal("12.34"), EUR)).withCountry(DE).withCustomerGroup(customerGroup), product -> { final Query<ProductProjection> query = ProductProjectionQuery.of(STAGED) .withPredicates(m -> m.id().is(product.getId())) .withExpansionPaths(m -> m.masterVariant().prices().customerGroup()); final List<Price> prices = client().executeBlocking(query).head().get().getMasterVariant().getPrices(); assertThat(prices .stream() .anyMatch(price -> Optional.ofNullable(price.getCustomerGroup()).map(customerGroupReference -> customerGroupReference.getObj() != null).orElse(false))) .isTrue(); return product; }); } ); }
@Test public void selectAPriceByCurrencyInProductProjectionQuery() { final List<PriceDraft> prices = asList(PriceDraft.of(EURO_30), PriceDraft.of(USD_20)); withProductOfPrices(prices, product -> { final ProductProjectionQuery request = ProductProjectionQuery.ofStaged() .withPredicates(m -> m.id().is(product.getId()))//to limit the test scope .withPriceSelection(PriceSelection.of(EUR));//price selection config final PagedQueryResult<ProductProjection> result = client().executeBlocking(request); 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 queryById() throws Exception { with2products("queryById", (p1, p2) -> { final Query<ProductProjection> query1 = ProductProjectionQuery.of(STAGED).withPredicates(m -> m.id().isIn(asList(p1.getId(), p2.getId()))); assertThat(ids(client().executeBlocking(query1))).containsOnly(p1.getId(), p2.getId()); final Query<ProductProjection> query = ProductProjectionQuery.of(STAGED).withPredicates(m -> m.id().is(p1.getId())); assertThat(ids(client().executeBlocking(query))).containsOnly(p1.getId()); }); }
@Test public void showProductAttributeTableWithDefaultFormatter() throws Exception { final List<String> attrNamesToShow = asList(COLOR_ATTR_NAME, SIZE_ATTR_NAME, MATCHING_PRODUCTS_ATTR_NAME, LAUNDRY_SYMBOLS_ATTR_NAME, RRP_ATTR_NAME, AVAILABLE_SINCE_ATTR_NAME); final Product product = createProduct(); final ProductProjectionQuery query = ProductProjectionQuery.ofStaged() .withPredicates(m -> m.id().is(product.getId())) .plusExpansionPaths(m -> m.masterVariant().attributes().valueSet()) .plusExpansionPaths(m -> m.productType()); final ProductProjection productProjection = client().executeBlocking(query).head().get(); final List<ProductType> productTypes = Collections.singletonList(productProjection.getProductType().getObj()); final List<Locale> locales = Collections.singletonList(ENGLISH); final DefaultProductAttributeFormatter formatter = DefaultProductAttributeFormatterDemo.createFormatter(productTypes, locales); DefaultProductAttributeFormatterDemo.example(attrNamesToShow, productProjection, formatter); }
@Test public void differentiateBetweenCurrentAndStaged() throws Exception { withUpdateableProduct(client(), product -> { final Product publishedProduct = client().executeBlocking(ProductUpdateCommand.of(product, Publish.of())); final Product mixedDataProduct = client().executeBlocking(ProductUpdateCommand.of(publishedProduct, ChangeName.of(randomSlug()))); final LocalizedString nameInCurrent = mixedDataProduct.getMasterData().getCurrent().getName(); final LocalizedString nameInStaged = mixedDataProduct.getMasterData().getStaged().getName(); final ProductProjectionQuery stagedQuery = ProductProjectionQuery.of(STAGED).withPredicates(m -> m.id().is(product.getId())); assertThat(client().executeBlocking(stagedQuery).head().get().getName()).isEqualTo(nameInStaged); final ProductProjectionQuery currentQuery = ProductProjectionQuery.of(CURRENT).withPredicates(m -> m.id().is(product.getId())); assertThat(client().executeBlocking(currentQuery).head().get().getName()).isEqualTo(nameInCurrent); return mixedDataProduct; }); }
@Test public void expandProductReferencesInProductAttributes() throws Exception { withProductWithProductReference(client(), (product, referencedProduct) -> { final Query<ProductProjection> query = ProductProjectionQuery.ofStaged() .withPredicates(m -> m.id().is(product.getId())) .withExpansionPaths(m -> m.masterVariant().attributes().value()) .toQuery(); final ProductProjection productProjection = client().executeBlocking(query).head().get(); final NamedAttributeAccess<Reference<Product>> namedAttributeAccess = AttributeAccess.ofProductReference().ofName("productreference"); final Reference<Product> productReference = productProjection.getMasterVariant().findAttribute(namedAttributeAccess).get(); final Product expandedReferencedProduct = productReference.getObj(); assertThat(expandedReferencedProduct.getId()).isEqualTo(referencedProduct.getId()); }); }
@Test public void expandTaxCategory() throws Exception { TaxCategoryFixtures.withTransientTaxCategory(client(), taxCategory -> withProduct(client(), product -> { final Product productWithTaxCategory = client().executeBlocking(ProductUpdateCommand.of(product, SetTaxCategory.of(taxCategory))); final ProductProjectionQuery query = ProductProjectionQuery.of(STAGED) .withPredicates(m -> m.id().is(productWithTaxCategory.getId())) .withExpansionPaths(m -> m.taxCategory()); final PagedQueryResult<ProductProjection> pagedQueryResult = client().executeBlocking(query); assertThat(pagedQueryResult.head().get().getTaxCategory()).is(expanded()); }) ); }