FlatStrategyPriceCalculator implements StrategyPriceCalculator { @Override public BigDecimal getPrice(Carrier carrier, Map<Purchasable, Long> items) { BigDecimal price = carrier.getPerShipping() != null ? carrier.getPerShipping() : BigDecimal.ZERO; Long numberOfItems = 0l; for (Long number : items.values()) { numberOfItems += number; } if (numberOfItems == 0) { return BigDecimal.ZERO; } BigDecimal perItem = carrier.getPerItem() != null ? carrier.getPerItem() : BigDecimal.ZERO; price = price.add(perItem.multiply(BigDecimal.valueOf(numberOfItems))); return price; }  @Override BigDecimal getPrice(Carrier carrier, Map<Purchasable, Long> items);  }
@Test public void testFlatStrategyPriceCalculation() throws ComponentLookupException { Carrier carrier = new Carrier(); carrier.setPerShipping(BigDecimal.TEN); carrier.setPerItem(BigDecimal.ONE); Map<Purchasable, Long> items = Maps.newHashMap(); Product product1 = new Product(); product1.setPrice(BigDecimal.valueOf(25)); Product product2 = new Product(); product2.setPrice(BigDecimal.valueOf((100))); items.put(product1, 3l); BigDecimal price = this.componentManager.getComponentUnderTest().getPrice(carrier, items); Assert.assertEquals(BigDecimal.valueOf(13), price); items.put(product1, 4l); price = this.componentManager.getComponentUnderTest().getPrice(carrier, items); Assert.assertEquals(BigDecimal.valueOf(14), price); items.put(product2, 3l); price = this.componentManager.getComponentUnderTest().getPrice(carrier, items); Assert.assertEquals(BigDecimal.valueOf(17), price); }
@Test public void testFlatStrategyPriceCalculationWhenCartIsEmpty() throws ComponentLookupException { Carrier carrier = new Carrier(); carrier.setPerShipping(BigDecimal.TEN); carrier.setPerItem(BigDecimal.ONE); Map<Purchasable, Long> items = Maps.newHashMap(); BigDecimal price = this.componentManager.getComponentUnderTest().getPrice(carrier, items); Assert.assertEquals(BigDecimal.ZERO, price); }