private static ProductUpdateCommand createSetTaxesCommand(final TaxCategory taxCategory, final Product product) { return ProductUpdateCommand.of(product, asList(AddPrice.of(MASTER_VARIANT_ID, PRICE), SetTaxCategory.of(taxCategory), Publish.of())); }
@Test public void publishWithPriceScope() throws Exception { withUpdateableProduct(client(), (Product product) -> { final ProductData stagedData = product.getMasterData().getStaged(); final Product publishedProduct = client().executeBlocking(ProductUpdateCommand.of(product, Publish.of())); final ProductData currentData = publishedProduct.getMasterData().getCurrent(); assertThat(stagedData.getMasterVariant().getImages()).hasSize(0); assertThat(stagedData.getMasterVariant().getPrices()).hasSize(0); assertThat(currentData).isEqualTo(stagedData); final PriceDraft expectedPrice = PriceDraft.of(EURO_10); final Image image = createExternalImage(); final List<UpdateAction<Product>> updateActions = asList( AddExternalImage.of(image, MASTER_VARIANT_ID), AddPrice.of(MASTER_VARIANT_ID, expectedPrice), Publish.ofScope(PublishScope.PRICES) ); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(publishedProduct, updateActions)); List<Price> prices = updatedProduct.getMasterData().getCurrent().getMasterVariant().getPrices(); //Verify published price in the current product assertThat(prices).hasSize(1); assertThat(expectedPrice).isEqualTo(PriceDraft.of(prices.get(0))); //Verify that the image has not been published assertThat(updatedProduct.getMasterData().getCurrent().getMasterVariant().getImages()).hasSize(0); return updatedProduct; }); }
@Test public void publishWithAllScope() throws Exception { withUpdateableProduct(client(), (Product product) -> { final ProductData stagedData = product.getMasterData().getStaged(); final Product publishedProduct = client().executeBlocking(ProductUpdateCommand.of(product, Publish.of())); final ProductData currentData = publishedProduct.getMasterData().getCurrent(); assertThat(stagedData.getMasterVariant().getImages()).hasSize(0); assertThat(stagedData.getMasterVariant().getPrices()).hasSize(0); assertThat(currentData).isEqualTo(stagedData); final PriceDraft expectedPrice = PriceDraft.of(EURO_10); final Image image = createExternalImage(); final List<UpdateAction<Product>> updateActions = asList( AddExternalImage.of(image, MASTER_VARIANT_ID), AddPrice.of(MASTER_VARIANT_ID, expectedPrice), Publish.ofScope(PublishScope.ALL) ); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(publishedProduct, updateActions)); List<Price> prices = updatedProduct.getMasterData().getCurrent().getMasterVariant().getPrices(); assertThat(prices).hasSize(1); assertThat(expectedPrice).isEqualTo(PriceDraft.of(prices.get(0))); assertThat(updatedProduct.getMasterData().getCurrent().getMasterVariant().getImages()).isEqualTo(asList(image)); return updatedProduct; }); }
@Test public void publish() throws Exception { withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().isPublished()).isFalse(); final Product publishedProduct = client().executeBlocking(ProductUpdateCommand.of(product, Publish.of())); assertThat(publishedProduct.getMasterData().isPublished()).isTrue(); final Product unpublishedProduct = client().executeBlocking(ProductUpdateCommand.of(publishedProduct, Unpublish.of())); assertThat(unpublishedProduct.getMasterData().isPublished()).isFalse(); return unpublishedProduct; }); }
@Test public void isPublished() throws Exception { withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().isPublished()).isFalse(); checkIsFoundByPublishedFlag(product, false); final Product publishedProduct = client().executeBlocking(ProductUpdateCommand.of(product, Publish.of())); assertThat(publishedProduct.getMasterData().isPublished()).isTrue(); checkIsFoundByPublishedFlag(product, true); final Product unpublishedProduct = client().executeBlocking(ProductUpdateCommand.of(publishedProduct, Unpublish.of())); assertThat(unpublishedProduct.getMasterData().isPublished()).isFalse(); return unpublishedProduct; }); }
@Test public void revertStagedChanges() throws Exception { withUpdateableProduct(client(), product -> { //changing only staged and not current final LocalizedString oldDescriptionOption = product.getMasterData().getStaged().getDescription(); final LocalizedString newDescription = LocalizedString.ofEnglish("new description " + RANDOM.nextInt()); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, asList(Publish.of(), SetDescription.of(newDescription))); final Product updatedProduct = client().executeBlocking(cmd); assertThat(oldDescriptionOption).isNotEqualTo(newDescription); assertThat(updatedProduct.getMasterData().getStaged().getDescription()).isEqualTo(newDescription); assertThat(updatedProduct.getMasterData().getCurrent().getDescription()).isEqualTo(oldDescriptionOption); final Product revertedProduct = client().executeBlocking(ProductUpdateCommand.of(updatedProduct, RevertStagedChanges.of())); assertThat(revertedProduct.getMasterData().getStaged().getDescription()).isEqualTo(oldDescriptionOption); assertThat(revertedProduct.getMasterData().getCurrent().getDescription()).isEqualTo(oldDescriptionOption); return revertedProduct; }); }
public void removeImageByVariantIdWithStaged(final Boolean staged) { final Image image = createExternalImage(); withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final Product productWithImage = client().executeBlocking(ProductUpdateCommand.of(product, AddExternalImage.ofVariantId(MASTER_VARIANT_ID, image, staged))); assertThat(productWithImage.getMasterData().getStaged().getMasterVariant().getImages()).isEqualTo(asList(image)); assertThat(productWithImage.getMasterData().hasStagedChanges()).isEqualTo(staged); final Product publishedProduct = client().executeBlocking(ProductUpdateCommand.of(productWithImage, Publish.of())); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(publishedProduct, RemoveImage.ofVariantId(MASTER_VARIANT_ID, image, staged))); assertThat(updatedProduct.getMasterData().getStaged().getMasterVariant().getImages()).hasSize(0); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
private void withProductOfSku(final String sku, final Function<Product, Product> productProductFunction) { withUpdateableProduct(client(), builder -> { return builder.masterVariant(ProductVariantDraftBuilder.of(builder.getMasterVariant()).sku(sku).build()); }, product -> { final ProductUpdateCommand productUpdateCommand = ProductUpdateCommand.of(product, Publish.of()); final Product publishedProduct = client().executeBlocking(productUpdateCommand); return productProductFunction.apply(publishedProduct); }); }
private static void withTaxedAndPricedProduct(final BlockingSphereClient client, final long centAmount, final double taxRate, final boolean taxIncluded, final Consumer<Product> operator) { final List<TaxRateDraft> taxRateDrafts = singletonList(TaxRateDraftBuilder.of(randomKey(), taxRate, taxIncluded, CountryCode.DE).build()); final TaxCategoryDraft taxCategoryDraft = TaxCategoryDraft.of(randomString(), taxRateDrafts); withTaxCategory(client, taxCategoryDraft, taxCategory -> withProduct(client, product -> { final PriceDraft priceDraft = PriceDraft.of(MoneyImpl.ofCents(centAmount, EUR)).withCountry(DE); final ProductUpdateCommand setPricesCmd = ProductUpdateCommand.of(product, asList( AddPrice.of(MASTER_VARIANT_ID, priceDraft), SetTaxCategory.of(taxCategory), Publish.of())); final Product productWithPrice = client.executeBlocking(setPricesCmd); operator.accept(productWithPrice); }) ); } }
@Test public void withLineItemBySku() { withUpdateableProduct(client(), product -> { final String sku = randomKey(); final Product productWithSku = client().executeBlocking(ProductUpdateCommand.of(product, Arrays.asList(SetSku.of(1, sku), Publish.of()))); final LineItemDraftDsl lineItemBySku = LineItemDraftBuilder.ofSku(sku, 1L).build(); final ShoppingListDraft shoppingListDraft = ShoppingListFixtures.newShoppingListDraftBuilder() .key(DEMO_SHOPPING_LIST_KEY) .plusLineItems(lineItemBySku) .build(); final ShoppingList shoppingList = client().executeBlocking(ShoppingListCreateCommand.of(shoppingListDraft).withExpansionPaths(m -> m.lineItems())); assertThat(shoppingList).isNotNull(); assertThat(shoppingList.getLineItems()) .hasSize(1); return productWithSku; }); } }
public void removeImageBySkuWithStaged(final Boolean staged) { final Image image = createExternalImage(); withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final String sku = product.getMasterData().getStaged().getMasterVariant().getSku(); final Product productWithImage = client().executeBlocking(ProductUpdateCommand.of(product, AddExternalImage.ofSku(sku, image, staged))); assertThat(productWithImage.getMasterData().getStaged().getMasterVariant().getImages()).isEqualTo(asList(image)); assertThat(productWithImage.getMasterData().hasStagedChanges()).isEqualTo(staged); final Product publishedProduct = client().executeBlocking(ProductUpdateCommand.of(productWithImage, Publish.of())); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(publishedProduct, RemoveImage.ofSku(sku, image, staged))); assertThat(updatedProduct.getMasterData().getStaged().getMasterVariant().getImages()).hasSize(0); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
public void addToCategoryWithStaged(final Boolean staged) { withCategory(client(), category -> { withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().getStaged().getCategories()).isEmpty(); assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final String orderHint = "0.123"; final Product productWithCategory = client() .executeBlocking(ProductUpdateCommand.of(product, AddToCategory.of(category, orderHint, staged))); assertThat(productWithCategory.getMasterData().hasStagedChanges()).isEqualTo(staged); final Product publishedProductWithCategory = client().executeBlocking(ProductUpdateCommand.of(productWithCategory, Publish.of())); final Product productWithoutCategory = client() .executeBlocking(ProductUpdateCommand.of(publishedProductWithCategory, RemoveFromCategory.of(category, staged))); assertThat(productWithoutCategory.getMasterData().hasStagedChanges()).isEqualTo(staged); assertThat(productWithoutCategory.getMasterData().getStaged().getCategories()).isEmpty(); return productWithoutCategory; }); }); }
public void setDiscountedPriceWithStaged(final Boolean staged) { final ProductDiscountPredicate predicate = ProductDiscountPredicate.of("1 = 1");//can be used for all products final ProductDiscountDraft productDiscountDraft = ProductDiscountDraft.of(randomSlug(), randomSlug(), predicate, ExternalProductDiscountValue.of(), randomSortOrder(), true); //don't forget that one product discount can be used for multiple products withProductDiscount(client(), productDiscountDraft, externalProductDiscount -> { withProductOfPrices(client(), singletonList(PriceDraft.of(EURO_40)), product -> { final Product publishedProduct = client().executeBlocking(ProductUpdateCommand.of(product, Publish.of())); final Price originalPrice = publishedProduct.getMasterData().getStaged().getMasterVariant().getPrices().get(0); assertThat(originalPrice.getDiscounted()).isNull(); final String priceId = originalPrice.getId(); final SetDiscountedPrice action = SetDiscountedPrice.of(priceId, DiscountedPrice.of(EURO_5, externalProductDiscount.toReference()), staged); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(publishedProduct, action)); final Price stagedPrice = updatedProduct.getMasterData().getStaged().getMasterVariant().getPrices().get(0); assertThat(stagedPrice.getValue()).isEqualTo(EURO_40); assertThat(stagedPrice.getDiscounted().getValue()).isEqualTo(EURO_5); assertThat(stagedPrice.getDiscounted().getDiscount()).isEqualTo(externalProductDiscount.toReference()); final Price currentPrice = updatedProduct.getMasterData().getCurrent().getMasterVariant().getPrices().get(0); if (staged) { assertThat(stagedPrice).isNotEqualTo(currentPrice); } else { assertThat(stagedPrice).isEqualTo(currentPrice); } }); }); }
@Test public void message() { ProductFixtures.withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().isPublished()).isFalse(); final Product publishedProduct = client().executeBlocking(ProductUpdateCommand.of(product, Publish.of())); Query<ProductPublishedMessage> messageQuery = MessageQuery.of() .withPredicates(m -> m.resource().is(product)) .withSort(m -> m.createdAt().sort().desc()) .withExpansionPaths(m -> m.resource()) .withLimit(1L) .forMessageType(ProductPublishedMessage.MESSAGE_HINT); assertEventually(() -> { final PagedQueryResult<ProductPublishedMessage> queryResult = client().executeBlocking(messageQuery); assertThat(queryResult.head()).isPresent(); final ProductPublishedMessage message = queryResult.head().get(); assertThat(message.getResource().getId()).isEqualTo(product.getId()); assertThat(message.getProductProjection().getMasterVariant()).isEqualTo(publishedProduct.getMasterData().getCurrent().getMasterVariant()); assertThat(message.getResource().getObj().getMasterData().getCurrent().getSlug()).isEqualTo(message.getProductProjection().getSlug()); assertThat(message.getRemovedImageUrls().size()).isEqualTo(0); }); return publishedProduct; }); } }
Publish.of() ); client().executeBlocking(ProductUpdateCommand.of(product, productUpdates));
@Test public void message() { ProductFixtures.withUpdateableProduct(client(), product -> { assertThat(product.getMasterData().isPublished()).isFalse(); final Product unpublishedProduct = client().executeBlocking(ProductUpdateCommand.of(product, asList(Publish.of(), Unpublish.of()))); assertEventually(() -> { final PagedQueryResult<ProductUnpublishedMessage> queryResult = client().executeBlocking(MessageQuery.of() .withPredicates(m -> m.resource().is(unpublishedProduct)) .withSort(m -> m.createdAt().sort().desc()) .withExpansionPaths(m -> m.resource()) .withLimit(1L) .forMessageType(ProductUnpublishedMessage.MESSAGE_HINT)); assertThat(queryResult.head()).isPresent(); final ProductUnpublishedMessage message = queryResult.head().get(); assertThat(message.getResource().getId()).as("productId").isEqualTo(product.getId()); assertThat(message.getResource().getObj().getMasterData().isPublished()).isFalse(); }); return unpublishedProduct; }); } }
@Test public void recalculate() throws Exception { withEmptyCartAndProduct(client(), (emptyCart, product) -> { final AddLineItem action = AddLineItem.of(product.getId(), MASTER_VARIANT_ID, 1L); final Cart cartWithLineItem = client().executeBlocking(CartUpdateCommand.of(emptyCart, action)); final Price oldPrice = cartWithLineItem.getLineItems().get(0).getPrice(); final PriceDraft priceDraft = PriceDraft.of(oldPrice).withValue(oldPrice.getValue().multiply(2)); final Product productWithChangedPrice = client().executeBlocking(ProductUpdateCommand.of(product, asList(ChangePrice.of(oldPrice, priceDraft), Publish.of()))); final List<Price> prices = productWithChangedPrice.getMasterData().getCurrent().getMasterVariant().getPrices(); assertThat(prices.stream().map(price -> PriceDraft.of(price)).collect(Collectors.toList())) .as("we updated the price of the product") .isEqualTo(asList(priceDraft)); final LineItem lineItemOfTheChangedProduct = client().executeBlocking(CartByIdGet.of(cartWithLineItem)).getLineItems().get(0); assertThat(lineItemOfTheChangedProduct.getPrice()) .as("the new product price is not automatically propagated to the line item in the cart") .isEqualTo(oldPrice).isNotEqualTo(priceDraft); final Cart recalculatedCart = client().executeBlocking(CartUpdateCommand.of(cartWithLineItem, Recalculate.of())); assertThat(PriceDraft.of(recalculatedCart.getLineItems().get(0).getPrice())) .as("recalculate updated the price of the line item in the cart") .isEqualTo(priceDraft); assertThat(recalculatedCart.getTotalPrice()) .as("recalculate also updated the total price of the cart") .isEqualTo(priceDraft.getValue()).isNotEqualTo(cartWithLineItem.getTotalPrice()); }); }
@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 recalculateAndUpdateProductData() throws Exception { withEmptyCartAndProduct(client(), (emptyCart, product) -> { //create cart with line item final AddLineItem action = AddLineItem.of(product.getId(), MASTER_VARIANT_ID, 1L); final Cart cartWithLineItem = client().executeBlocking(CartUpdateCommand.of(emptyCart, action)); final NamedAttributeAccess<LocalizedEnumValue> colorAttribute = Colors.ATTRIBUTE; final LocalizedEnumValue oldColor = cartWithLineItem.getLineItems().get(0).getVariant().findAttribute(colorAttribute).get(); //update the product final LocalizedEnumValue newValueForColor = Colors.RED; final SetAttribute localizedEnumUpdate = SetAttribute.of(MASTER_VARIANT_ID, colorAttribute, newValueForColor); final Product updatedProduct = client().executeBlocking(ProductUpdateCommand.of(product, asList(localizedEnumUpdate, Publish.of()))); assertThat(updatedProduct.getMasterData().getCurrent().getMasterVariant().findAttribute(colorAttribute)).contains(newValueForColor); //check the line item in the cart, the product data will not be updated final LineItem lineItemOfTheChangedProduct = client().executeBlocking(CartByIdGet.of(cartWithLineItem)).getLineItems().get(0); assertThat(lineItemOfTheChangedProduct.getVariant().findAttribute(colorAttribute)) .as("the new product attribute value is not automatically propagated to the line item in the cart") .contains(oldColor); //use recalculate with updateProductData flag, the product data will be updated final Cart recalculatedCart = client().executeBlocking(CartUpdateCommand.of(cartWithLineItem, Recalculate.of().withUpdateProductData(Boolean.TRUE))); assertThat(recalculatedCart.getLineItems().get(0).getVariant().findAttribute(colorAttribute)) .contains(newValueForColor); }); }
final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, asList(Publish.of(), AddExternalImage.of(image,MASTER_VARIANT_ID))); final Product updatedProduct = client().executeBlocking(cmd);