@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(); }
@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 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 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); }
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); }
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);  }