@Test public void productAttributeReferenceExpansion() throws Exception { final ProductProjectionQuery query = ProductProjectionQuery.ofCurrent() .withExpansionPaths(m -> m.masterVariant().attributes().value()); assertThat(query.expansionPaths().get(0).toSphereExpand()) .isEqualTo("masterVariant.attributes[*].value"); } }
@Test public void useExpansionPathsOfOtherRequest() { final ProductProjectionQuery query = ProductProjectionQuery.ofCurrent().withExpansionPaths(m -> m.categories()); final ProductProjectionSearch search = ProductProjectionSearch.ofCurrent().withExpansionPaths(query); assertThat(query.expansionPaths()) .hasSize(1) .as("reuse expansion spec from other request") .isEqualTo(search.expansionPaths()); } }
@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 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 queryByProductType() throws Exception { with2products("queryByProductType", (p1, p2) ->{ final Query<ProductProjection> query = ProductProjectionQuery.of(STAGED) .byProductType(p1.getProductType()) .withExpansionPaths(m -> m.productType()); final PagedQueryResult<ProductProjection> queryResult = client().executeBlocking(query); assertThat(queryResult.head().get().getProductType()).is(expanded()); assertThat(ids(queryResult)).containsOnly(p1.getId()); }); }
@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 queryByCategory() throws Exception { withCategory(client(), cat3 -> withCategory(client(), cat1 -> withCategory(client(), cat2 -> with2products("queryByCategory", (p1, p2) -> { final Category cat1WithParent = client().executeBlocking(CategoryUpdateCommand.of(cat1, asList(ChangeParent.of(cat3)))); final Product productWithCat1 = client().executeBlocking(ProductUpdateCommand.of(p1, AddToCategory.of(cat1WithParent))); final Query<ProductProjection> query = ProductProjectionQuery.of(STAGED) .withPredicates(m -> m.categories().isIn(asList(cat1, cat2))) .withExpansionPaths(m -> m.categories().parent()); final PagedQueryResult<ProductProjection> queryResult = client().executeBlocking(query); assertThat(ids(queryResult)).containsOnly(productWithCat1.getId()); final Reference<Category> cat1Loaded = queryResult.head().get().getCategories().stream().findAny().get(); assertThat(cat1Loaded).as("cat of product is expanded").is(expanded()); final Reference<Category> parent = cat1Loaded.getObj().getParent(); assertThat(parent).as("parent of cat is expanded").is(expanded()); }) ) ) ); }
@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()); }) ); }