DefaultDeliveryCostCalculationStrategy implements DeliveryCostCalculationStrategy { @Override public Total calculate(final MutableShoppingCart cart) { cart.removeShipping(); if (!cart.getCarrierSlaId().isEmpty()) { Total total = null; final Set<Long> uniqueCarrierSlaIds = new HashSet<>(cart.getCarrierSlaId().values()); final Set<DeliveryCostCalculationStrategy> strategyExecutionPlan = new HashSet<>(); for (final Long carrierSlaId : uniqueCarrierSlaIds) { final CarrierSla carrierSla = carrierSlaService.getById(carrierSlaId); if (carrierSla != null) { final DeliveryCostCalculationStrategy strategy = subStrategies.get(carrierSla.getSlaType()); if (strategy == null) { LOG.error("CarrierSla.slaType [{}] is not mapped to DeliveryCostCalculationStrategy", carrierSla.getSlaType()); } else { strategyExecutionPlan.add(strategy); } } } for (final DeliveryCostCalculationStrategy strategy : strategyExecutionPlan) { final Total subTotal = strategy.calculate(cart); if (subTotal != null) { total = total == null ? subTotal : total.add(subTotal); } } return total; } return null; } DefaultDeliveryCostCalculationStrategy(final CarrierSlaService carrierSlaService,
                                                  final Map<String, DeliveryCostCalculationStrategy> subStrategies); @Override Total calculate(final MutableShoppingCart cart);  }
@Test public void testCalculateNoneOf() throws Exception { final CarrierSlaService carrierSlaService = context.mock(CarrierSlaService.class, "carrierSlaService"); final MutableShoppingCart cart = context.mock(MutableShoppingCart.class, "cart"); final DeliveryCostCalculationStrategy strategy = context.mock(DeliveryCostCalculationStrategy.class, "strategy"); context.checking(new Expectations() {{ allowing(cart).getCarrierSlaId(); will(returnValue(Collections.emptyMap())); oneOf(cart).removeShipping(); }}); final Total delTotal = new DefaultDeliveryCostCalculationStrategy(carrierSlaService, Collections.singletonMap("S", strategy)).calculate(cart); assertNull(delTotal); context.assertIsSatisfied(); }
@Test public void testCalculateSingleCarrierNotFound() throws Exception { final CarrierSlaService carrierSlaService = context.mock(CarrierSlaService.class, "carrierSlaService"); final MutableShoppingCart cart = context.mock(MutableShoppingCart.class, "cart"); final DeliveryCostCalculationStrategy strategy = context.mock(DeliveryCostCalculationStrategy.class, "strategy"); context.checking(new Expectations() {{ allowing(cart).getCarrierSlaId(); will(returnValue(Collections.singletonMap("Main", 123L))); oneOf(carrierSlaService).getById(123L); will(returnValue(null)); oneOf(cart).removeShipping(); }}); final Total delTotal = new DefaultDeliveryCostCalculationStrategy(carrierSlaService, Collections.singletonMap("S", strategy)).calculate(cart); assertNull(delTotal); context.assertIsSatisfied(); }
@Test public void testCalculateStrategyoneOf() throws Exception { final CarrierSlaService carrierSlaService = context.mock(CarrierSlaService.class, "carrierSlaService"); final MutableShoppingCart cart = context.mock(MutableShoppingCart.class, "cart"); final CarrierSla carrierSla = context.mock(CarrierSla.class, "carrierSla"); final DeliveryCostCalculationStrategy strategy = context.mock(DeliveryCostCalculationStrategy.class, "strategy"); final Total strategyTotal = context.mock(Total.class, "strategyTotal"); context.checking(new Expectations() {{ allowing(cart).getCarrierSlaId(); will(returnValue(Collections.singletonMap("Main", 123L))); oneOf(carrierSlaService).getById(123L); will(returnValue(carrierSla)); oneOf(carrierSla).getSlaType(); will(returnValue("S")); oneOf(cart).removeShipping(); oneOf(strategy).calculate(cart); will(returnValue(strategyTotal)); }}); final Total delTotal = new DefaultDeliveryCostCalculationStrategy(carrierSlaService, Collections.singletonMap("S", strategy)).calculate(cart); assertSame(strategyTotal, delTotal); context.assertIsSatisfied(); }
@Test public void testCalculateStrategyMulti() throws Exception { final CarrierSlaService carrierSlaService = context.mock(CarrierSlaService.class, "carrierSlaService"); final MutableShoppingCart cart = context.mock(MutableShoppingCart.class, "cart"); final CarrierSla carrierSla = context.mock(CarrierSla.class, "carrierSla"); final CarrierSla carrierSlaB = context.mock(CarrierSla.class, "carrierSlaB"); final DeliveryCostCalculationStrategy strategy = context.mock(DeliveryCostCalculationStrategy.class, "strategy"); final DeliveryCostCalculationStrategy strategyB = context.mock(DeliveryCostCalculationStrategy.class, "strategyB"); final Total strategyTotal = context.mock(Total.class, "strategyTotal"); final Total strategyTotalB = context.mock(Total.class, "strategyTotalB"); final Total strategyTotalAll = context.mock(Total.class, "strategyTotalAll"); final Map<String, Long> carriers = new LinkedHashMap<>(); carriers.put("Main", 123L); carriers.put("Backorder", 234L); context.checking(new Expectations() {{ allowing(cart).getCarrierSlaId(); will(returnValue(carriers)); oneOf(carrierSlaService).getById(123L); will(returnValue(carrierSla)); oneOf(carrierSla).getSlaType(); will(returnValue("S")); oneOf(carrierSlaService).getById(234L); will(returnValue(carrierSlaB)); oneOf(carrierSlaB).getSlaType(); will(returnValue("B")); oneOf(cart).removeShipping(); oneOf(strategy).calculate(cart); will(returnValue(strategyTotal)); oneOf(strategyB).calculate(cart); will(returnValue(strategyTotalB)); allowing(strategyTotal).add(strategyTotalB); will(returnValue(strategyTotalAll)); allowing(strategyTotalB).add(strategyTotal); will(returnValue(strategyTotalAll)); }}); final Map<String, DeliveryCostCalculationStrategy> strategies = new HashMap<>(); strategies.put("S", strategy); strategies.put("B", strategyB); final Total delTotal = new DefaultDeliveryCostCalculationStrategy(carrierSlaService, strategies).calculate(cart); assertSame(strategyTotalAll, delTotal); context.assertIsSatisfied(); }
@Test public void testCalculateStrategyMultiWithUnavailable() throws Exception { final CarrierSlaService carrierSlaService = context.mock(CarrierSlaService.class, "carrierSlaService"); final MutableShoppingCart cart = context.mock(MutableShoppingCart.class, "cart"); final CarrierSla carrierSla = context.mock(CarrierSla.class, "carrierSla"); final CarrierSla carrierSlaB = context.mock(CarrierSla.class, "carrierSlaB"); final DeliveryCostCalculationStrategy strategy = context.mock(DeliveryCostCalculationStrategy.class, "strategy"); final DeliveryCostCalculationStrategy strategyB = context.mock(DeliveryCostCalculationStrategy.class, "strategyB"); final Total strategyTotal = null; final Total strategyTotalB = context.mock(Total.class, "strategyTotalB"); final Map<String, Long> carriers = new LinkedHashMap<>(); carriers.put("Main", 123L); carriers.put("Backorder", 234L); context.checking(new Expectations() {{ allowing(cart).getCarrierSlaId(); will(returnValue(carriers)); oneOf(carrierSlaService).getById(123L); will(returnValue(carrierSla)); oneOf(carrierSla).getSlaType(); will(returnValue("S")); oneOf(carrierSlaService).getById(234L); will(returnValue(carrierSlaB)); oneOf(carrierSlaB).getSlaType(); will(returnValue("B")); oneOf(cart).removeShipping(); oneOf(strategy).calculate(cart); will(returnValue(strategyTotal)); oneOf(strategyB).calculate(cart); will(returnValue(strategyTotalB)); }}); final Map<String, DeliveryCostCalculationStrategy> strategies = new HashMap<>(); strategies.put("S", strategy); strategies.put("B", strategyB); final Total delTotal = new DefaultDeliveryCostCalculationStrategy(carrierSlaService, strategies).calculate(cart); assertSame(strategyTotalB, delTotal); context.assertIsSatisfied(); }
@Test public void testCalculateStrategyMultiWithUnavailableB() throws Exception { final CarrierSlaService carrierSlaService = context.mock(CarrierSlaService.class, "carrierSlaService"); final MutableShoppingCart cart = context.mock(MutableShoppingCart.class, "cart"); final CarrierSla carrierSla = context.mock(CarrierSla.class, "carrierSla"); final CarrierSla carrierSlaB = context.mock(CarrierSla.class, "carrierSlaB"); final DeliveryCostCalculationStrategy strategy = context.mock(DeliveryCostCalculationStrategy.class, "strategy"); final DeliveryCostCalculationStrategy strategyB = context.mock(DeliveryCostCalculationStrategy.class, "strategyB"); final Total strategyTotal = context.mock(Total.class, "strategyTotal"); final Total strategyTotalB = null; final Map<String, Long> carriers = new LinkedHashMap<>(); carriers.put("Main", 123L); carriers.put("Backorder", 234L); context.checking(new Expectations() {{ allowing(cart).getCarrierSlaId(); will(returnValue(carriers)); oneOf(carrierSlaService).getById(123L); will(returnValue(carrierSla)); oneOf(carrierSla).getSlaType(); will(returnValue("S")); oneOf(carrierSlaService).getById(234L); will(returnValue(carrierSlaB)); oneOf(carrierSlaB).getSlaType(); will(returnValue("B")); oneOf(cart).removeShipping(); oneOf(strategy).calculate(cart); will(returnValue(strategyTotal)); oneOf(strategyB).calculate(cart); will(returnValue(strategyTotalB)); }}); final Map<String, DeliveryCostCalculationStrategy> strategies = new HashMap<>(); strategies.put("S", strategy); strategies.put("B", strategyB); final Total delTotal = new DefaultDeliveryCostCalculationStrategy(carrierSlaService, strategies).calculate(cart); assertSame(strategyTotal, delTotal); context.assertIsSatisfied(); }
@Test public void testCalculateNoStrategy() throws Exception { final CarrierSlaService carrierSlaService = context.mock(CarrierSlaService.class, "carrierSlaService"); final MutableShoppingCart cart = context.mock(MutableShoppingCart.class, "cart"); final CarrierSla carrierSla = context.mock(CarrierSla.class, "carrierSla"); context.checking(new Expectations() {{ allowing(cart).getCarrierSlaId(); will(returnValue(Collections.singletonMap("Main", 123L))); oneOf(carrierSlaService).getById(123L); will(returnValue(carrierSla)); allowing(carrierSla).getSlaType(); will(returnValue("S")); oneOf(cart).removeShipping(); }}); final Total delTotal = new DefaultDeliveryCostCalculationStrategy(carrierSlaService, Collections.EMPTY_MAP).calculate(cart); assertNull(delTotal); context.assertIsSatisfied(); }