private ProductQuery(){ super(ProductsEndpoint.ENDPOINT.endpoint(), resultTypeReference()); }
public static ProductQueryBuilder of() { return new ProductQueryBuilder(ProductQuery.of()); }
default ProductQuery byProductType(final Referenceable<ProductType> productType) { return withPredicates(m -> m.productType().is(productType)); }
public QueryDsl<Product> byProductType(final Referenceable<ProductType> productType) { return withPredicate(model().productType().is(productType)); }
private ProductQuery query(final Product product) { return ProductQuery.of().withPredicates(m -> m.id().is(product.getId())); } }
private void checkIsFoundByPublishedFlag(final Product product, final boolean value) { final Optional<Product> productFromQuery = client().executeBlocking(ProductQuery.of() .withPredicates(m -> { return m.masterData().isPublished().is(value); }) .plusPredicates(m -> m.id().is(product.getId()))).head(); assertThat(productFromQuery.get().getId()).isEqualTo(product.getId()); }
@Test public void useIdPredicateInsteadOfOffset() throws Exception { final ProductQuery seedQuery = ProductQuery.of() //the original predicate, which queries products for a certain product type //the idea works also for no predicate to get all products .withPredicates(m -> m.productType().is(productType)) //important, we sort by id, otherwise id > $lastId would not make sense .withSort(m -> m.id().sort().asc()) .withLimit(PAGE_SIZE) .withFetchTotal(false);//saves also resources and time final CompletionStage<List<Product>> resultStage = findNext(seedQuery, seedQuery, new LinkedList<>()); final List<Product> actualProducts = resultStage.toCompletableFuture().join(); assertThat(actualProducts).hasSize(createdProducts.size()); //!!! the underlying database has a different algorithm to sort by ID, it is a UUID, which differs from String sorting final List<Product> javaSortedActual = actualProducts.stream().sorted(BY_ID_COMPARATOR).collect(toList()); assertThat(javaSortedActual).isEqualTo(createdProducts); }
@AfterClass @BeforeClass public static void deleteProductsAndProductType() { final List<ProductType> productTypes = client().executeBlocking(ProductTypeQuery.of().byName(PRODUCT_TYPE_NAME)).getResults(); if (!productTypes.isEmpty()) { final ProductQuery productQuery = ProductQuery.of() .withPredicates(m -> m.productType().isIn(productTypes)) .withLimit(500L); client().executeBlocking(productQuery).getResults().forEach(p -> client().executeBlocking(ProductDeleteCommand.of(p))); productTypes.forEach(p -> client().executeBlocking(ProductTypeDeleteCommand.of(p))); } }
@Test public void expandVariants() { CustomerGroupFixtures.withB2cCustomerGroup(client(), customerGroup -> ProductFixtures.withProductType(client(), randomString(), productType -> withProduct(client(), new VariantsCottonTShirtProductDraftSupplier(productType, randomString(), customerGroup), product -> { final PagedQueryResult<Product> result = client().executeBlocking(ProductQuery.of() .withPredicates(m -> m.id().is(product.getId())) .withExpansionPaths(m -> m.masterData().staged().variants().prices().customerGroup()) .withLimit(1L)); final Price priceWithCustomerGroup = result.head().get().getMasterData().getStaged().getVariants().get(0).getPrices().stream() .filter(price -> Objects.equals(price.getCustomerGroup(), customerGroup.toReference())) .findFirst().get(); assertThat(priceWithCustomerGroup.getCustomerGroup().getObj()).isNotNull().isEqualTo(customerGroup); }) ) ); }
@Test public void selectAPriceByCurrencyInProductQuery() { final List<PriceDraft> prices = asList(PriceDraft.of(EURO_30), PriceDraft.of(USD_20)); withProductOfPrices(prices, product -> { final ProductQuery request = ProductQuery.of() .withPredicates(m -> m.id().is(product.getId()))//to limit the test scope .withPriceSelection(PriceSelection.of(EUR));//price selection config final PagedQueryResult<Product> result = client().executeBlocking(request); assertThat(result.getCount()).isEqualTo(1); final ProductVariant masterVariant = result.getResults().get(0).getMasterData().getStaged().getMasterVariant(); assertThat(masterVariant.getPrice()).isNotNull().has(price(PriceDraft.of(EURO_30))); }); }
@Test public void productType() throws Exception { final Consumer<Product> user = product -> { final Query<Product> query = ProductQuery.of(). bySlug(ProductProjectionType.CURRENT, Locale.ENGLISH, englishSlugOf(product.getMasterData().getStaged())). withExpansionPaths(ProductExpansionModel.of().productType()). toQuery(); final PagedQueryResult<Product> queryResult = client().executeBlocking(query); final Reference<ProductType> productTypeReference = queryResult.head().get().getProductType(); assertThat(productTypeReference).is(expanded()); }; withProduct(client(), "productTypeReferenceExpansion", user); }
public static void withUpdateableProduct(final BlockingSphereClient client, final Supplier<? extends ProductDraft> creator, final Function<Product, Product> user) { final ProductDraft productDraft = creator.get(); final String slug = englishSlugOf(productDraft); final PagedQueryResult<Product> pagedQueryResult = client.executeBlocking(ProductQuery.of().bySlug(ProductProjectionType.CURRENT, Locale.ENGLISH, slug)); delete(client, pagedQueryResult.getResults()); final Product product = client.executeBlocking(ProductCreateCommand.of(productDraft)); final Product possiblyUpdateProduct = user.apply(product); delete(client, possiblyUpdateProduct); }
@Test public void queryBySku() { final String sku = "sku2000"; final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of() .sku(sku) .attributes(Sizes.ATTRIBUTE.draftOf(Sizes.S), Colors.ATTRIBUTE.draftOf(Colors.GREEN)) .build(); final ProductDraft productDraft = ProductDraftBuilder.of(productType, en("foo"), en("foo-slug"), masterVariant).build(); client().executeBlocking(ProductCreateCommand.of(productDraft)); final PagedQueryResult<Product> result = client().executeBlocking(ProductQuery.of().bySku(sku, STAGED)); assertThat(result.getResults()).hasSize(1); assertThat(result.getResults().get(0).getMasterData().getStaged().getMasterVariant().getSku()).contains(sku); assertThat(result.getResults().get(0).getMasterData().getStaged().getMasterVariant().findAttribute(Colors.ATTRIBUTE)).contains(Colors.GREEN); assertThat(result.getResults().get(0).getMasterData().getStaged().getMasterVariant().findAttribute(Sizes.ATTRIBUTE)).contains(Sizes.S); }
public static void withUpdateableProductType(final BlockingSphereClient client, final Supplier<ProductTypeDraft> creator, final UnaryOperator<ProductType> user) { final SphereInternalLogger logger = SphereInternalLogger.getLogger("product-types.fixtures"); final ProductTypeDraft productTypeDraft = creator.get(); final String name = productTypeDraft.getName(); final PagedQueryResult<ProductType> queryResult = client.executeBlocking(ProductTypeQuery.of().byName(name)); queryResult.getResults().forEach(productType -> { final PagedQueryResult<Product> pagedQueryResult = client.executeBlocking(ProductQuery.of().byProductType(productType)); delete(client, pagedQueryResult.getResults()); client.executeBlocking(ProductTypeDeleteCommand.of(productType)); }); final ProductType productType = client.executeBlocking(ProductTypeCreateCommand.of(productTypeDraft)); logger.debug(() -> "created product type " + productType.getName() + " " + productType.getId()); final ProductType updated = user.apply(productType); logger.debug(() -> "attempt to delete product type " + productType.getName() + " " + productType.getId()); try { client.executeBlocking(ProductTypeDeleteCommand.of(updated)); } catch (final Exception e) { final PagedQueryResult<Product> pagedQueryResult = client.executeBlocking(ProductQuery.of().byProductType(productType)); delete(client, pagedQueryResult.getResults()); client.executeBlocking(ProductTypeDeleteCommand.of(productType)); } }
public static ProductQuery of() { return new ProductQuery(); }
protected SphereRequest<PagedQueryResult<Product>> queryObjectForName(final String name) { return ProductQuery.of().withPredicates(m -> m.masterData().current().name().lang(ENGLISH).is(name)); }
@Test public void queryByTiersWithMinimumQuantity() { withProduct(client(), product -> { final ProductQuery productQuery = ProductQuery.of() .withPredicates(m -> m.masterData().current().variants().prices().tiers().minimumQuantity().isGreaterThan(5)) .plusPredicates(m -> m.is(product)); final List<Product> results = client().executeBlocking(productQuery).getResults(); assertThat(results).hasSize(0); }); }
public static void deleteProductsProductTypeAndProductDiscounts(final BlockingSphereClient client, final ProductType productType) { client.executeBlocking(ProductDiscountQuery.of().withLimit(500L)).getResults() .forEach(discount -> client.executeBlocking(ProductDiscountDeleteCommand.of(discount))); if (productType != null) { QueryPredicate<Product> ofProductType = ProductQueryModel.of().productType().is(productType); ProductQuery productsOfProductTypeQuery = ProductQuery.of().withPredicates(ofProductType).withLimit(500L); do { final List<Product> products = client.executeBlocking(productsOfProductTypeQuery).getResults(); final List<Product> unpublishedProducts = products.stream().map( product -> { if (product.getMasterData().isPublished()) { return client.executeBlocking(ProductUpdateCommand.of(product, Unpublish.of())); } else { return product; } } ).collect(toList()); final List<CompletionStage<Product>> stages = new LinkedList<>(); unpublishedProducts.forEach( product -> { final CompletionStage<Product> completionStage = client.execute(ProductDeleteCommand.of(product)); stages.add(completionStage); } ); stages.forEach(stage -> SphereClientUtils.blockingWait(stage, 30, TimeUnit.SECONDS)); deleteProductType(client, productType); } while (client.executeBlocking(productsOfProductTypeQuery).getCount() > 0); } }
public QueryDsl<Product> byProductType(final Referenceable<ProductType> productType) { return withPredicate(model().productType().is(productType)); }
@Test public void taxCategory() throws Exception { TaxCategoryFixtures.withTransientTaxCategory(client(), taxCategory -> withProduct(client(), product -> { final Product productWithTaxCategory = client().executeBlocking(ProductUpdateCommand.of(product, SetTaxCategory.of(taxCategory))); assertThat(productWithTaxCategory.getTaxCategory()).isNotNull(); final Query<Product> query = ProductQuery.of(). bySlug(ProductProjectionType.CURRENT, Locale.ENGLISH, englishSlugOf(product.getMasterData().getStaged())). withExpansionPaths(ProductExpansionModel.of().taxCategory()). toQuery(); final PagedQueryResult<Product> queryResult = client().executeBlocking(query); final Reference<TaxCategory> productTypeReference = firstOf(queryResult).getTaxCategory(); assertThat(productTypeReference).is(expanded()); }) ); } }