@Test public void testExecuteProductWithOptions() throws Exception { MutableShoppingCart shoppingCart = new ShoppingCartImpl(); shoppingCart.initialise(ctx().getBean("amountCalculationStrategy", AmountCalculationStrategy.class)); final ShoppingCartCommandFactory commands = ctx().getBean("shoppingCartCommandFactory", ShoppingCartCommandFactory.class); commands.execute(shoppingCart, (Map) singletonMap(ShoppingCartCommand.CMD_SETSHOP, 10)); commands.execute(shoppingCart, (Map) singletonMap(ShoppingCartCommand.CMD_CHANGECURRENCY, "EUR")); assertEquals(MoneyUtils.ZERO, shoppingCart.getTotal().getSubTotal()); Map<String, String> params = new HashMap<>(); params.put(ShoppingCartCommand.CMD_ADDTOCART, "NOPROD-SKU"); commands.execute(shoppingCart, (Map) params); params.clear(); params.put(ShoppingCartCommand.CMD_ADDTOCART, "001_CFG01"); params.put(ShoppingCartCommand.CMD_P_ITEM_GROUP, "001_CFG01-A"); params.put("MATERIAL", "001_CFG_OPT1_A"); commands.execute(shoppingCart, (Map) params); params.clear(); params.put(ShoppingCartCommand.CMD_ADDTOCART, "001_CFG01"); params.put(ShoppingCartCommand.CMD_P_ITEM_GROUP, "001_CFG01-B"); params.put("MATERIAL", "001_CFG_OPT1_B"); commands.execute(shoppingCart, (Map) params); assertEquals("45049.99", shoppingCart.getTotal().getSubTotal().toPlainString()); assertEquals(3, shoppingCart.getCartItemsCount()); assertEquals(5, shoppingCart.getCartItemList().size()); params.clear(); params.put(ShoppingCartCommand.CMD_REMOVEALLSKU, "001_CFG01"); params.put(ShoppingCartCommand.CMD_P_ITEM_GROUP, "001_CFG01-A"); commands.execute(shoppingCart, (Map) params); assertEquals("25049.99", shoppingCart.getTotal().getSubTotal().toPlainString()); assertEquals(2, shoppingCart.getCartItemsCount()); assertEquals(3, shoppingCart.getCartItemList().size()); params.clear(); params.put(ShoppingCartCommand.CMD_REMOVEALLSKU, "001_CFG01"); params.put(ShoppingCartCommand.CMD_P_ITEM_GROUP, "001_CFG01-B"); commands.execute(shoppingCart, (Map) params); assertEquals("49.99", shoppingCart.getTotal().getSubTotal().toPlainString()); assertEquals(1, shoppingCart.getCartItemsCount()); assertEquals(1, shoppingCart.getCartItemList().size()); }
@Override protected void execute(final MutableShoppingCart shoppingCart, final ProductSku productSku, final String skuCode, final String supplier, final String itemGroup, final BigDecimal qty, final Map<String, Object> parameters) { if (StringUtils.isNotBlank(itemGroup)) { final List<String> allInGroup = shoppingCart.getCartItemList().stream() .filter(item -> itemGroup.equals(item.getItemGroup())) .map(CartItem::getProductSkuCode).collect(Collectors.toList()); for (final String sku : allInGroup) { if(!shoppingCart.removeCartItem(supplier, sku, itemGroup)) { LOG.warn("Cannot remove all skus with code {}:{} from cart", sku, itemGroup); return; } } recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else if(shoppingCart.removeCartItem(supplier, skuCode, itemGroup)) { recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else { LOG.warn("Cannot remove all skus with code {} from cart", skuCode); } }
RemoveAllSkuFromCartCommandImpl extends AbstractSkuCartCommandImpl { @Override protected void execute(final MutableShoppingCart shoppingCart, final ProductSku productSku, final String skuCode, final String supplier, final String itemGroup, final BigDecimal qty, final Map<String, Object> parameters) { if (StringUtils.isNotBlank(itemGroup)) { final List<String> allInGroup = shoppingCart.getCartItemList().stream() .filter(item -> itemGroup.equals(item.getItemGroup())) .map(CartItem::getProductSkuCode).collect(Collectors.toList()); for (final String sku : allInGroup) { if(!shoppingCart.removeCartItem(supplier, sku, itemGroup)) { LOG.warn("Cannot remove all skus with code {}:{} from cart", sku, itemGroup); return; } } recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else if(shoppingCart.removeCartItem(supplier, skuCode, itemGroup)) { recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else { LOG.warn("Cannot remove all skus with code {} from cart", skuCode); } } }
RemoveAllSkuFromCartCommandImpl extends AbstractSkuCartCommandImpl { @Override protected void execute(final MutableShoppingCart shoppingCart, final ProductSku productSku, final String skuCode, final String supplier, final String itemGroup, final BigDecimal qty, final Map<String, Object> parameters) { if (StringUtils.isNotBlank(itemGroup)) { final List<String> allInGroup = shoppingCart.getCartItemList().stream() .filter(item -> itemGroup.equals(item.getItemGroup())) .map(CartItem::getProductSkuCode).collect(Collectors.toList()); for (final String sku : allInGroup) { if(!shoppingCart.removeCartItem(supplier, sku, itemGroup)) { LOG.warn("Cannot remove all skus with code {}:{} from cart", sku, itemGroup); return; } } recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else if(shoppingCart.removeCartItem(supplier, skuCode, itemGroup)) { recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else { LOG.warn("Cannot remove all skus with code {} from cart", skuCode); } } RemoveAllSkuFromCartCommandImpl(final ShoppingCartCommandRegistry registry,
                                           final PriceResolver priceResolver,
                                           final PricingPolicyProvider pricingPolicyProvider,
                                           final ProductService productService,
                                           final ShopService shopService); }
RemoveAllSkuFromCartCommandImpl extends AbstractSkuCartCommandImpl { @Override protected void execute(final MutableShoppingCart shoppingCart, final ProductSku productSku, final String skuCode, final String supplier, final String itemGroup, final BigDecimal qty, final Map<String, Object> parameters) { if (StringUtils.isNotBlank(itemGroup)) { final List<String> allInGroup = shoppingCart.getCartItemList().stream() .filter(item -> itemGroup.equals(item.getItemGroup())) .map(CartItem::getProductSkuCode).collect(Collectors.toList()); for (final String sku : allInGroup) { if(!shoppingCart.removeCartItem(supplier, sku, itemGroup)) { LOG.warn("Cannot remove all skus with code {}:{} from cart", sku, itemGroup); return; } } recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else if(shoppingCart.removeCartItem(supplier, skuCode, itemGroup)) { recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else { LOG.warn("Cannot remove all skus with code {} from cart", skuCode); } } RemoveAllSkuFromCartCommandImpl(final ShoppingCartCommandRegistry registry,
                                           final PriceResolver priceResolver,
                                           final PricingPolicyProvider pricingPolicyProvider,
                                           final ProductService productService,
                                           final ShopService shopService); @Override String getCmdKey(); }
RemoveAllSkuFromCartCommandImpl extends AbstractSkuCartCommandImpl { @Override protected void execute(final MutableShoppingCart shoppingCart, final ProductSku productSku, final String skuCode, final String supplier, final String itemGroup, final BigDecimal qty, final Map<String, Object> parameters) { if (StringUtils.isNotBlank(itemGroup)) { final List<String> allInGroup = shoppingCart.getCartItemList().stream() .filter(item -> itemGroup.equals(item.getItemGroup())) .map(CartItem::getProductSkuCode).collect(Collectors.toList()); for (final String sku : allInGroup) { if(!shoppingCart.removeCartItem(supplier, sku, itemGroup)) { LOG.warn("Cannot remove all skus with code {}:{} from cart", sku, itemGroup); return; } } recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else if(shoppingCart.removeCartItem(supplier, skuCode, itemGroup)) { recalculatePricesInCart(shoppingCart); markDirty(shoppingCart); } else { LOG.warn("Cannot remove all skus with code {} from cart", skuCode); } } RemoveAllSkuFromCartCommandImpl(final ShoppingCartCommandRegistry registry,
                                           final PriceResolver priceResolver,
                                           final PricingPolicyProvider pricingPolicyProvider,
                                           final ProductService productService,
                                           final ShopService shopService); @Override String getCmdKey();  }