@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());
});
}