@Test public void testCalculatePriceRegisteredTaxInclusive() throws Exception { final TaxProvider taxProvider = context.mock(TaxProvider.class, "taxProvider"); final DeliveryCostCalculationStrategy deliveryCostCalculationStrategy = context.mock(DeliveryCostCalculationStrategy.class, "deliveryCostCalculationStrategy"); final PromotionContextFactory promotionContextFactory = context.mock(PromotionContextFactory.class, "promotionContextFactory"); final CustomerService customerService = context.mock(CustomerService.class, "customerService"); final ShopService shopService = context.mock(ShopService.class, "shopService"); final ShoppingCart currentCart = context.mock(ShoppingCart.class, "currentCart"); final ShoppingContext currentCartCtx = context.mock(ShoppingContext.class, "currentCartCtx"); final Shop shop = context.mock(Shop.class, "shop"); final Customer customer = context.mock(Customer.class, "customer"); final PromotionContext promotionContext = context.mock(PromotionContext.class, "promotionContext"); final TaxProvider.Tax tax = context.mock(TaxProvider.Tax.class, "tax"); final Total deliveryCost = new TotalImpl(); final Total itemsTotal = new TotalImpl(); context.checking(new Expectations() {{ allowing(currentCart).getCurrentLocale(); will(returnValue("en")); allowing(currentCart).getCurrencyCode(); will(returnValue("EUR")); allowing(currentCart).getShoppingContext(); will(returnValue(currentCartCtx)); allowing(currentCartCtx).getCustomerName(); will(returnValue("Bob Doe")); allowing(currentCartCtx).getShopId(); will(returnValue(123L)); allowing(currentCartCtx).getShopCode(); will(returnValue("SHOP10")); allowing(currentCartCtx).getCustomerShopId(); will(returnValue(123L)); allowing(currentCartCtx).getCustomerShopCode(); will(returnValue("SHOP10")); allowing(currentCartCtx).getCountryCode(); will(returnValue("GB")); allowing(currentCartCtx).getStateCode(); will(returnValue("GB-GB")); allowing(currentCartCtx).getCustomerEmail(); will(returnValue("bob.doe@somewhere.com")); allowing(currentCartCtx).getCustomerShops(); will(returnValue(Collections.singletonList("SHOP10"))); allowing(currentCartCtx).getLatestViewedSkus(); will(returnValue(null)); allowing(currentCartCtx).getLatestViewedCategories(); will(returnValue(null)); allowing(currentCartCtx).getResolvedIp(); will(returnValue("127.0.0.1")); allowing(shopService).getById(123L); will(returnValue(shop)); allowing(customerService).getCustomerByEmail("bob.doe@somewhere.com", shop); will(returnValue(customer)); allowing(promotionContextFactory).getInstance("SHOP10", "EUR"); will(returnValue(promotionContext)); allowing(promotionContext).applyItemPromo(with(customer), with(any(MutableShoppingCart.class))); allowing(taxProvider).determineTax("SHOP10", "EUR", "GB", "GB-GB", "SKU0001"); will(returnValue(tax)); allowing(tax).getCode(); will(returnValue("VAT")); allowing(tax).getRate(); will(returnValue(new BigDecimal("20"))); allowing(tax).isExcluded(); will(returnValue(false)); allowing(deliveryCostCalculationStrategy).calculate(with(any(MutableShoppingCart.class))); will(returnValue(deliveryCost)); allowing(promotionContext).applyOrderPromo(with(customer), with(any(MutableShoppingCart.class)), with(any(Total.class))); will(returnValue(itemsTotal)); allowing(promotionContext).applyShippingPromo(with(customer), with(any(MutableShoppingCart.class)), with(any(Total.class))); }}); final AmountCalculationStrategy strategy = new DefaultAmountCalculationStrategy(taxProvider, deliveryCostCalculationStrategy, promotionContextFactory, customerService, shopService); final ShoppingCartCalculator calculator = new ShoppingCartCalculatorImpl(strategy); final ShoppingCartCalculator.PriceModel model = calculator.calculatePrice(currentCart, "Main", "SKU0001", new BigDecimal("9.99")); assertNotNull(model); assertEquals("9.99", model.getGrossPrice().toPlainString()); assertEquals("8.32", model.getNetPrice().toPlainString()); assertEquals("1.67", model.getTaxAmount().toPlainString()); assertEquals("VAT", model.getTaxCode()); assertEquals("20", model.getTaxRate().toPlainString()); assertFalse(model.isTaxExclusive()); context.assertIsSatisfied(); }
@Override public PriceModel calculatePrice(final ShoppingCart currentCart, final String supplier, final String skuCode, final BigDecimal minimalPrice) { final MutableShoppingCart cart = createNewCart(currentCart, true, false, true); cart.addProductSkuToCart(supplier, skuCode, skuCode, BigDecimal.ONE, "calculator", false, false); cart.setProductSkuPrice(supplier, skuCode, minimalPrice, minimalPrice); cart.recalculate(); PriceModel model = null; for (final CartItem item : cart.getCartItemList()) { if (item.getProductSkuCode().equals(skuCode) && (item.getSupplierCode() != null && item.getSupplierCode().equals(supplier)) || item.getSupplierCode() == null && supplier == null) { model = new DefaultPriceModel( item.getGrossPrice(), item.getNetPrice(), item.getTaxCode(), item.getTaxRate(), item.isTaxExclusiveOfPrice()); } } return model; }
ShoppingCartCalculatorImpl implements ShoppingCartCalculator { @Override public PriceModel calculatePrice(final ShoppingCart currentCart, final String supplier, final String skuCode, final BigDecimal minimalPrice) { final MutableShoppingCart cart = createNewCart(currentCart, true, false, true); cart.addProductSkuToCart(supplier, skuCode, skuCode, BigDecimal.ONE, "calculator", false, false); cart.setProductSkuPrice(supplier, skuCode, minimalPrice, minimalPrice); cart.recalculate(); PriceModel model = null; for (final CartItem item : cart.getCartItemList()) { if (item.getProductSkuCode().equals(skuCode) && (item.getSupplierCode() != null && item.getSupplierCode().equals(supplier)) || item.getSupplierCode() == null && supplier == null) { model = new DefaultPriceModel( item.getGrossPrice(), item.getNetPrice(), item.getTaxCode(), item.getTaxRate(), item.isTaxExclusiveOfPrice()); } } return model; } }
ShoppingCartCalculatorImpl implements ShoppingCartCalculator { @Override public PriceModel calculatePrice(final ShoppingCart currentCart, final String supplier, final String skuCode, final BigDecimal minimalPrice) { final MutableShoppingCart cart = createNewCart(currentCart, true, false, true); cart.addProductSkuToCart(supplier, skuCode, skuCode, BigDecimal.ONE, "calculator", false, false); cart.setProductSkuPrice(supplier, skuCode, minimalPrice, minimalPrice); cart.recalculate(); PriceModel model = null; for (final CartItem item : cart.getCartItemList()) { if (item.getProductSkuCode().equals(skuCode) && (item.getSupplierCode() != null && item.getSupplierCode().equals(supplier)) || item.getSupplierCode() == null && supplier == null) { model = new DefaultPriceModel( item.getGrossPrice(), item.getNetPrice(), item.getTaxCode(), item.getTaxRate(), item.isTaxExclusiveOfPrice()); } } return model; } ShoppingCartCalculatorImpl(final AmountCalculationStrategy calculationStrategy); }
ShoppingCartCalculatorImpl implements ShoppingCartCalculator { @Override public PriceModel calculatePrice(final ShoppingCart currentCart, final String supplier, final String skuCode, final BigDecimal minimalPrice) { final MutableShoppingCart cart = createNewCart(currentCart, true, false, true); cart.addProductSkuToCart(supplier, skuCode, skuCode, BigDecimal.ONE, "calculator", false, false); cart.setProductSkuPrice(supplier, skuCode, minimalPrice, minimalPrice); cart.recalculate(); PriceModel model = null; for (final CartItem item : cart.getCartItemList()) { if (item.getProductSkuCode().equals(skuCode) && (item.getSupplierCode() != null && item.getSupplierCode().equals(supplier)) || item.getSupplierCode() == null && supplier == null) { model = new DefaultPriceModel( item.getGrossPrice(), item.getNetPrice(), item.getTaxCode(), item.getTaxRate(), item.isTaxExclusiveOfPrice()); } } return model; } ShoppingCartCalculatorImpl(final AmountCalculationStrategy calculationStrategy); @Override PriceModel calculatePrice(final ShoppingCart currentCart,
                                     final String supplier,
                                     final String skuCode,
                                     final BigDecimal minimalPrice); }
ShoppingCartCalculatorImpl implements ShoppingCartCalculator { @Override public PriceModel calculatePrice(final ShoppingCart currentCart, final String supplier, final String skuCode, final BigDecimal minimalPrice) { final MutableShoppingCart cart = createNewCart(currentCart, true, false, true); cart.addProductSkuToCart(supplier, skuCode, skuCode, BigDecimal.ONE, "calculator", false, false); cart.setProductSkuPrice(supplier, skuCode, minimalPrice, minimalPrice); cart.recalculate(); PriceModel model = null; for (final CartItem item : cart.getCartItemList()) { if (item.getProductSkuCode().equals(skuCode) && (item.getSupplierCode() != null && item.getSupplierCode().equals(supplier)) || item.getSupplierCode() == null && supplier == null) { model = new DefaultPriceModel( item.getGrossPrice(), item.getNetPrice(), item.getTaxCode(), item.getTaxRate(), item.isTaxExclusiveOfPrice()); } } return model; } ShoppingCartCalculatorImpl(final AmountCalculationStrategy calculationStrategy); @Override PriceModel calculatePrice(final ShoppingCart currentCart,
                                     final String supplier,
                                     final String skuCode,
                                     final BigDecimal minimalPrice);  }