SetSkuPriceEventCommandImpl extends AbstractCartCommandImpl { @Override public void execute(final MutableShoppingCart shoppingCart, final Map<String, Object> parameters) { if (parameters.containsKey(getCmdKey())) { final String skuCode = (String) parameters.get(getCmdKey()); final String supplier = (String) parameters.get(CMD_P_SUPPLIER); final String price = (String) parameters.get(CMD_SETPRICE_P_PRICE); BigDecimal offer; try { offer = new BigDecimal(price); } catch (Exception exp) { LOG.error("Can not set price {} for product sku dto with code {}", price, skuCode); return; } final String auth = (String) parameters.get(CMD_SETPRICE_P_AUTH); if (StringUtils.isBlank(auth)) { LOG.error("Can not set price {} for product sku dto with code {} because no auth code supplied", price, skuCode); return; } boolean matched = false; for (final CartItem item : shoppingCart.getCartItemList()) { if (ShoppingCartUtils.isCartItem(item, supplier, skuCode)) { matched = true; if (MoneyUtils.isFirstBiggerThanSecond(item.getSalePrice(), offer)) { if (!shoppingCart.setProductSkuOffer(supplier, skuCode, offer, auth)) { LOG.warn("Can not set price to sku with code {} ", skuCode); } else { recalculate(shoppingCart); markDirty(shoppingCart); } } else { if (!(shoppingCart.setProductSkuPrice(supplier, skuCode, offer, offer) && shoppingCart.setProductSkuOffer(supplier, skuCode, offer, auth))) { LOG.warn("Can not set price to sku with code {} ", skuCode); } else { recalculate(shoppingCart); markDirty(shoppingCart); } } } } if (!matched) { LOG.warn("Can not locate product sku dto with code {} in cart {}", skuCode, shoppingCart.getGuid()); } } } SetSkuPriceEventCommandImpl(final ShoppingCartCommandRegistry registry); @Override String getCmdKey(); @Override void execute(final MutableShoppingCart shoppingCart, final Map<String, Object> parameters);  }
@Test public void testExecute() 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, "CC_TEST1"); params.put(ShoppingCartCommand.CMD_P_QTY, "5"); commands.execute(shoppingCart, (Map) params); assertEquals("90.00", shoppingCart.getTotal().getListSubTotal().toPlainString()); assertEquals("90.00", shoppingCart.getTotal().getSubTotal().toPlainString()); params.clear(); params.put(ShoppingCartCommand.CMD_SETPRICE, "CC_TEST1"); params.put(ShoppingCartCommand.CMD_SETPRICE_P_PRICE, "15.00"); params.put(ShoppingCartCommand.CMD_SETPRICE_P_AUTH, "CC0001"); commands.execute(shoppingCart, (Map) params); assertEquals("90.00", shoppingCart.getTotal().getListSubTotal().toPlainString()); assertEquals("75.00", shoppingCart.getTotal().getSubTotal().toPlainString()); params.clear(); params.put(ShoppingCartCommand.CMD_SETPRICE, "CC_TEST1"); params.put(ShoppingCartCommand.CMD_SETPRICE_P_PRICE, "30.00"); params.put(ShoppingCartCommand.CMD_SETPRICE_P_AUTH, "CC0001"); commands.execute(shoppingCart, (Map) params); assertEquals("150.00", shoppingCart.getTotal().getListSubTotal().toPlainString()); assertEquals("150.00", shoppingCart.getTotal().getSubTotal().toPlainString()); }