@Test public void testCalculateSingleCarrierNotFound() throws Exception { final CarrierSlaService carrierSlaService = context.mock(CarrierSlaService.class, "carrierSlaService"); final MutableShoppingCart cart = context.mock(MutableShoppingCart.class, "cart"); context.checking(new Expectations() {{ allowing(cart).getCarrierSlaId(); will(returnValue(Collections.singletonMap("Main", 123L))); oneOf(carrierSlaService).getById(123L); will(returnValue(null)); }}); final Total delTotal = new ExternalDeliveryCostCalculationStrategy(carrierSlaService).calculate(cart); assertNull(delTotal); context.assertIsSatisfied(); }
@Override public Total calculate(final MutableShoppingCart cart) { 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 && CarrierSla.EXTERNAL.equals(carrierSla.getSlaType())) { final Object strategy = applicationContext.getBean(carrierSla.getScript()); if (strategy instanceof DeliveryCostCalculationStrategy) { strategyExecutionPlan.add((DeliveryCostCalculationStrategy) strategy); } else { LOG.error("CarrierSla.script [{}] is not a bean of type DeliveryCostCalculationStrategy", carrierSla.getScript()); } } } 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; }
ExternalDeliveryCostCalculationStrategy implements DeliveryCostCalculationStrategy, ApplicationContextAware { @Override public Total calculate(final MutableShoppingCart cart) { 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 && CarrierSla.EXTERNAL.equals(carrierSla.getSlaType())) { final Object strategy = applicationContext.getBean(carrierSla.getScript()); if (strategy instanceof DeliveryCostCalculationStrategy) { strategyExecutionPlan.add((DeliveryCostCalculationStrategy) strategy); } else { LOG.error("CarrierSla.script [{}] is not a bean of type DeliveryCostCalculationStrategy", carrierSla.getScript()); } } } 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; } }
ExternalDeliveryCostCalculationStrategy implements DeliveryCostCalculationStrategy, ApplicationContextAware { @Override public Total calculate(final MutableShoppingCart cart) { 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 && CarrierSla.EXTERNAL.equals(carrierSla.getSlaType())) { final Object strategy = applicationContext.getBean(carrierSla.getScript()); if (strategy instanceof DeliveryCostCalculationStrategy) { strategyExecutionPlan.add((DeliveryCostCalculationStrategy) strategy); } else { LOG.error("CarrierSla.script [{}] is not a bean of type DeliveryCostCalculationStrategy", carrierSla.getScript()); } } } 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; } ExternalDeliveryCostCalculationStrategy(final CarrierSlaService carrierSlaService); }
ExternalDeliveryCostCalculationStrategy implements DeliveryCostCalculationStrategy, ApplicationContextAware { @Override public Total calculate(final MutableShoppingCart cart) { 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 && CarrierSla.EXTERNAL.equals(carrierSla.getSlaType())) { final Object strategy = applicationContext.getBean(carrierSla.getScript()); if (strategy instanceof DeliveryCostCalculationStrategy) { strategyExecutionPlan.add((DeliveryCostCalculationStrategy) strategy); } else { LOG.error("CarrierSla.script [{}] is not a bean of type DeliveryCostCalculationStrategy", carrierSla.getScript()); } } } 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; } ExternalDeliveryCostCalculationStrategy(final CarrierSlaService carrierSlaService); @Override Total calculate(final MutableShoppingCart cart); @Override void setApplicationContext(final ApplicationContext applicationContext); }
ExternalDeliveryCostCalculationStrategy implements DeliveryCostCalculationStrategy, ApplicationContextAware { @Override public Total calculate(final MutableShoppingCart cart) { 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 && CarrierSla.EXTERNAL.equals(carrierSla.getSlaType())) { final Object strategy = applicationContext.getBean(carrierSla.getScript()); if (strategy instanceof DeliveryCostCalculationStrategy) { strategyExecutionPlan.add((DeliveryCostCalculationStrategy) strategy); } else { LOG.error("CarrierSla.script [{}] is not a bean of type DeliveryCostCalculationStrategy", carrierSla.getScript()); } } } 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; } ExternalDeliveryCostCalculationStrategy(final CarrierSlaService carrierSlaService); @Override Total calculate(final MutableShoppingCart cart); @Override void setApplicationContext(final ApplicationContext applicationContext);  }