@Nullable default ProductData get(final ProductProjectionType productProjectionType) { return productProjectionType == CURRENT ? getCurrent() : getStaged(); } }
@Nullable default ProductData get(final ProductProjectionType productProjectionType) { return productProjectionType == CURRENT ? getCurrent() : getStaged(); } }
default Optional<ProductData> get(final ProductProjectionType productProjectionType) { return productProjectionType == CURRENT ? getCurrent() : Optional.of(getStaged()); } }
@Nullable default ProductData get(final ProductProjectionType productProjectionType) { return productProjectionType == CURRENT ? getCurrent() : getStaged(); } }
default Optional<ProductData> get(final ProductProjectionType productProjectionType) { return productProjectionType == CURRENT ? getCurrent() : Optional.of(getStaged()); } }
public void setSkuWithStaged(final Boolean staged) { final String oldSku = randomKey(); withProductOfSku(oldSku, (Product product) -> { assertThat(product.getMasterData().getStaged().getMasterVariant().getSku()).isEqualTo(oldSku); assertThat(product.getMasterData().getCurrent().getMasterVariant().getSku()).isEqualTo(oldSku); assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final String newSku = randomKey(); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, SetSku.of(MASTER_VARIANT_ID, newSku, staged)); final Product updatedProduct = client().executeBlocking(cmd); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isEqualTo(staged); return updatedProduct; }); }
@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; }); }
@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 setSku() throws Exception { final String oldSku = randomKey(); withProductOfSku(oldSku, (Product product) -> { assertThat(product.getMasterData().getStaged().getMasterVariant().getSku()).isEqualTo(oldSku); assertThat(product.getMasterData().getCurrent().getMasterVariant().getSku()).isEqualTo(oldSku); assertThat(product.getMasterData().hasStagedChanges()).isFalse(); final String newSku = randomKey(); final ProductUpdateCommand cmd = ProductUpdateCommand.of(product, SetSku.of(MASTER_VARIANT_ID, newSku)); final Product updatedProduct = client().executeBlocking(cmd); assertThat(updatedProduct.getMasterData().getStaged().getMasterVariant().getSku()) .as("update action updates SKU in staged") .isEqualTo(newSku); assertThat(updatedProduct.getMasterData().getCurrent().getMasterVariant().getSku()) .as("update action updates NOT directly in current") .isEqualTo(oldSku) .isNotEqualTo(newSku); assertThat(updatedProduct.getMasterData().hasStagedChanges()).isTrue(); 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 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; }); } }
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 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 createPublishedProduct() { withEmptyProductType(client(), randomKey(), productType -> { final ProductVariantDraft masterVariant = ProductVariantDraftBuilder.of().build(); final LocalizedString name = randomSlug(); final LocalizedString slug = randomSlug(); final ProductDraft productDraft = ProductDraftBuilder.of(productType, name, slug, masterVariant) .publish(true) .build(); final Product product = client().executeBlocking(ProductCreateCommand.of(productDraft)); assertThat(product.getMasterData().isPublished()).isTrue(); assertThat(product.getMasterData().getCurrent().getSlug()).isEqualTo(slug); assertEventually(() -> { final ProductProjectionSearch search = ProductProjectionSearch.ofCurrent() .withQueryFilters(m -> m.id().is(product.getId())); final PagedSearchResult<ProductProjection> searchResult = client().executeBlocking(search); assertThat(searchResult.getResults()).hasSize(1); }); unpublishAndDelete(product); }); }
@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); }); }