private ProductProjectionQuery(final ProductProjectionType productProjectionType) { super(ProductProjectionsEndpoint.ENDPOINT.endpoint(), asList(QueryParameter.of("staged", stagedQueryParameterValue(productProjectionType))), resultTypeReference()); }
default ProductProjectionQuery byProductType(final Referenceable<ProductType> productType) { return withPredicates(m -> m.productType().is(productType)); }
static ProductProjectionQuery ofCurrent() { return of(ProductProjectionType.CURRENT); }
public QueryDsl<ProductProjection> byProductType(final Referenceable<ProductType> productType) { return withPredicate(model().productType().is(productType)); }
@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; }); } ); }
private void checkOneResult(final Product product, final QueryPredicate<ProductProjection> predicate) { final PagedQueryResult<ProductProjection> queryResult = client().executeBlocking(ProductProjectionQuery.of(STAGED).withPredicates(predicate)); assertThat(ids(queryResult)).containsOnly(product.getId()); }
@Test public void queryByName() throws Exception { with2products("queryByName", (p1, p2) ->{ final Query<ProductProjection> query1 = ProductProjectionQuery.of(STAGED) .withPredicates(m -> m.name().lang(ENGLISH).is(en(p1.getMasterData().getStaged().getName()))) .withSort(m -> m.createdAt().sort().desc()) .withLimit(1L); assertThat(ids(client().executeBlocking(query1))).containsOnly(p1.getId()); }); }
@Test public void execution2() throws Exception { withProduct(client(), product -> { final String slug = product.getMasterData().getStaged().getSlug().get(Locale.ENGLISH); final ProductProjectionQuery normalSphereRequest = ProductProjectionQuery.ofStaged() .withPredicates(m -> m.slug().locale(Locale.ENGLISH).is(slug)) .plusExpansionPaths(m -> m.productType()); final JsonNodeSphereRequest jsonNodeSphereRequest = JsonNodeSphereRequest.of(normalSphereRequest); assertThat(normalSphereRequest.httpRequestIntent()) .as("a JsonNodeSphereRequest creates the same request to the platform, but differs in the response") .isEqualTo(jsonNodeSphereRequest.httpRequestIntent()); final PagedQueryResult<ProductProjection> productProjectionPagedSearchResult = client().executeBlocking(normalSphereRequest); final JsonNode jsonNode = client().executeBlocking(jsonNodeSphereRequest);//all will be returned as JSON assertThat(jsonNode.get("results").get(0).get("productType").get("obj").get("description").asText()) .as("the expansion paths are honored") .isEqualTo("a 'T' shaped cloth"); }); } }
@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 queryByHasStagedChanges() throws Exception { withProduct(client(), product -> { final Product updated = client().executeBlocking(ProductUpdateCommand.of(product, ChangeName.of(randomSlug()))); final ProductProjectionQuery query = ProductProjectionQuery.of(STAGED) .withPredicates(m -> m.hasStagedChanges().is(true)) .withSort(m -> m.createdAt().sort().desc()); final PagedQueryResult<ProductProjection> pagedQueryResult = client().executeBlocking(query); assertThat(ids(pagedQueryResult)).contains(updated.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); }
private static List<ProductProjection> queryForCategoryAndSort(final String categoryId) { final ProductProjectionQuery query = ProductProjectionQuery.ofStaged() .withPredicates(productModel -> productModel.categories().id().is(categoryId)) .withSort(productModel -> productModel.categoryOrderHints().category(categoryId).asc()); return client().executeBlocking(query).getResults(); }
@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 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()); }); }); }); }); }
withProductInCategory(client(), jeansCategory, jeansProduct -> { withProductInCategory(client(), tshirtCategory, tshirtProduct -> { final ProductProjectionQuery sphereRequest = ProductProjectionQuery.ofStaged().withPredicates(m -> m.categories().isIn(asList(mensClothingCategory))); final PagedQueryResult<ProductProjection> resultForParentCategory = .isEmpty(); final ProductProjectionQuery query = ProductProjectionQuery.ofStaged().withPredicates(m -> m.categories().isIn(asList(tshirtCategory, jeansCategory))); assertThat(query.predicates().get(0)) .isEqualTo(QueryPredicate.of(format("categories(id in (\"%s\", \"%s\"))", tshirtCategory.getId(), jeansCategory.getId()))); final PagedQueryResult<ProductProjection> resultForDirectCategories =
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); }
ProductProjectionQueryImpl(final ProductProjectionType productProjectionType){ super(ProductProjectionEndpoint.ENDPOINT.endpoint(), ProductProjectionQuery.resultTypeReference(), ProductProjectionQueryModel.of(), ProductProjectionExpansionModel.of(), ProductProjectionQueryImpl::new, additionalParametersOf(productProjectionType)); }
@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 queryBySlug() throws Exception { with2products("queryBySlug", (p1, p2) ->{ final Query<ProductProjection> query1 = ProductProjectionQuery.of(STAGED).bySlug(ENGLISH, p1.getMasterData().getStaged().getSlug().get(ENGLISH)); assertThat(ids(client().executeBlocking(query1))).containsOnly(p1.getId()); }); }
public static ProductProjectionQuery of(final ProductProjectionType productProjectionType) { return new ProductProjectionQuery(productProjectionType); }