@Test public void testDetermineDeliveryBucketNewNoChangeSingleGroup() throws Exception { final ShopService shopService = context.mock(ShopService.class, "shopService"); final ProductService productService = context.mock(ProductService.class, "productService"); final WarehouseService warehouseService = context.mock(WarehouseService.class, "warehouseService"); final InventoryResolver inventoryResolver = context.mock(InventoryResolver.class, "inventoryResolver"); final OrderSplittingStrategyImpl strategy = new OrderSplittingStrategyImpl(shopService, productService, warehouseService, inventoryResolver) { @Override Pair<String, String> getDeliveryGroup(final CartItem item, final Map<String, Warehouse> warehouses) { return new Pair<>(CustomerOrderDelivery.STANDARD_DELIVERY_GROUP, "s1"); } }; final Warehouse warehouse = context.mock(Warehouse.class, "warehouse"); final CartItem item = context.mock(CartItem.class, "item"); final ShoppingCart cart = context.mock(ShoppingCart.class, "cart"); final OrderInfo info = context.mock(OrderInfo.class, "info"); final ShoppingContext ctx = context.mock(ShoppingContext.class, "ctx"); final CartItem other = context.mock(CartItem.class, "other"); final DeliveryBucket itemBucket = new DeliveryBucketImpl(CustomerOrderDelivery.STANDARD_DELIVERY_GROUP, "s1"); context.checking(new Expectations() {{ allowing(item).getDeliveryBucket(); will(returnValue(null)); allowing(item).getProductSkuCode(); will(returnValue("ABC")); allowing(item).getItemGroup(); will(returnValue(null)); allowing(item).isGift(); will(returnValue(false)); allowing(other).getDeliveryBucket(); will(returnValue(itemBucket)); allowing(other).getProductSkuCode(); will(returnValue("CED")); allowing(other).getItemGroup(); will(returnValue(null)); allowing(other).isGift(); will(returnValue(false)); allowing(cart).getOrderInfo(); will(returnValue(info)); allowing(info).getMultipleDeliveryAvailable(); will(returnValue(Collections.singletonMap("s1", Boolean.TRUE))); allowing(info).isMultipleDelivery(); will(returnValue(true)); allowing(cart).getShoppingContext(); will(returnValue(ctx)); allowing(ctx).getCustomerShopId(); will(returnValue(10L)); allowing(cart).getCartItemList(); will(returnValue(Arrays.asList(item, other))); allowing(warehouseService).getByShopIdMapped(10L, false); will(returnValue(Collections.singletonMap("s1", warehouse))); allowing(warehouse).isMultipleShippingSupported(); will(returnValue(true)); }}); assertEquals(new DeliveryBucketImpl(CustomerOrderDelivery.STANDARD_DELIVERY_GROUP, "s1"), strategy.determineDeliveryBucket(item, cart)); context.assertIsSatisfied(); }
@Override public DeliveryBucket determineDeliveryBucket(final CartItem item, final ShoppingCart cart) { final Map<String, Boolean> onePhysicalDelivery = new HashMap<>(); for (final Map.Entry<String, Boolean> isAllowed : cart.getOrderInfo().getMultipleDeliveryAvailable().entrySet()) { onePhysicalDelivery.put(isAllowed.getKey(), !cart.getOrderInfo().isMultipleDelivery() || !isAllowed.getValue()); } final Map<DeliveryBucket, List<CartItem>> deliveryGroups = new HashMap<>(); DeliveryBucket itemBucket = item.getDeliveryBucket(); if (item.getDeliveryBucket() == null) { final Map<String, Warehouse> warehouses = warehouseService.getByShopIdMapped(cart.getShoppingContext().getCustomerShopId(), false); final Pair<String, String> deliveryGroup = getDeliveryGroup(item, warehouses); final Warehouse warehouse = warehouses.get(deliveryGroup.getSecond()); itemBucket = new DeliveryBucketImpl(deliveryGroup.getFirst(), deliveryGroup.getSecond()); onePhysicalDelivery.put(deliveryGroup.getSecond(), !cart.getOrderInfo().isMultipleDelivery() || warehouse == null || !warehouse.isMultipleShippingSupported()); } for (CartItem cartItem : cart.getCartItemList()) { final boolean itemToCheck = item.isGift() == cartItem.isGift() && item.getProductSkuCode().equals(cartItem.getProductSkuCode()); final DeliveryBucket cartItemBucket = itemToCheck ? itemBucket : cartItem.getDeliveryBucket(); if (cartItemBucket != null) { List<CartItem> items = deliveryGroups.computeIfAbsent(cartItemBucket, k -> new ArrayList<>()); items.add(cartItem); } } groupDeliveriesIntoMixedIfNecessary(deliveryGroups, onePhysicalDelivery); for (final Map.Entry<DeliveryBucket, List<CartItem>> group : deliveryGroups.entrySet()) { for (final CartItem groupItem : group.getValue()) { final boolean itemToCheck = item.isGift() == groupItem.isGift() && item.getProductSkuCode().equals(groupItem.getProductSkuCode()); if (itemToCheck) { return group.getKey(); } } } return itemBucket; }
OrderSplittingStrategyImpl implements OrderSplittingStrategy { @Override public DeliveryBucket determineDeliveryBucket(final CartItem item, final ShoppingCart cart) { final Map<String, Boolean> onePhysicalDelivery = new HashMap<>(); for (final Map.Entry<String, Boolean> isAllowed : cart.getOrderInfo().getMultipleDeliveryAvailable().entrySet()) { onePhysicalDelivery.put(isAllowed.getKey(), !cart.getOrderInfo().isMultipleDelivery() || !isAllowed.getValue()); } final Map<DeliveryBucket, List<CartItem>> deliveryGroups = new HashMap<>(); DeliveryBucket itemBucket = item.getDeliveryBucket(); if (item.getDeliveryBucket() == null) { final Map<String, Warehouse> warehouses = warehouseService.getByShopIdMapped(cart.getShoppingContext().getCustomerShopId(), false); final Pair<String, String> deliveryGroup = getDeliveryGroup(item, warehouses); final Warehouse warehouse = warehouses.get(deliveryGroup.getSecond()); itemBucket = new DeliveryBucketImpl(deliveryGroup.getFirst(), deliveryGroup.getSecond()); onePhysicalDelivery.put(deliveryGroup.getSecond(), !cart.getOrderInfo().isMultipleDelivery() || warehouse == null || !warehouse.isMultipleShippingSupported()); } for (CartItem cartItem : cart.getCartItemList()) { final boolean itemToCheck = item.isGift() == cartItem.isGift() && item.getProductSkuCode().equals(cartItem.getProductSkuCode()); final DeliveryBucket cartItemBucket = itemToCheck ? itemBucket : cartItem.getDeliveryBucket(); if (cartItemBucket != null) { List<CartItem> items = deliveryGroups.computeIfAbsent(cartItemBucket, k -> new ArrayList<>()); items.add(cartItem); } } groupDeliveriesIntoMixedIfNecessary(deliveryGroups, onePhysicalDelivery); for (final Map.Entry<DeliveryBucket, List<CartItem>> group : deliveryGroups.entrySet()) { for (final CartItem groupItem : group.getValue()) { final boolean itemToCheck = item.isGift() == groupItem.isGift() && item.getProductSkuCode().equals(groupItem.getProductSkuCode()); if (itemToCheck) { return group.getKey(); } } } return itemBucket; } }
OrderSplittingStrategyImpl implements OrderSplittingStrategy { @Override public DeliveryBucket determineDeliveryBucket(final CartItem item, final ShoppingCart cart) { final Map<String, Boolean> onePhysicalDelivery = new HashMap<>(); for (final Map.Entry<String, Boolean> isAllowed : cart.getOrderInfo().getMultipleDeliveryAvailable().entrySet()) { onePhysicalDelivery.put(isAllowed.getKey(), !cart.getOrderInfo().isMultipleDelivery() || !isAllowed.getValue()); } final Map<DeliveryBucket, List<CartItem>> deliveryGroups = new HashMap<>(); DeliveryBucket itemBucket = item.getDeliveryBucket(); if (item.getDeliveryBucket() == null) { final Map<String, Warehouse> warehouses = warehouseService.getByShopIdMapped(cart.getShoppingContext().getCustomerShopId(), false); final Pair<String, String> deliveryGroup = getDeliveryGroup(item, warehouses); final Warehouse warehouse = warehouses.get(deliveryGroup.getSecond()); itemBucket = new DeliveryBucketImpl(deliveryGroup.getFirst(), deliveryGroup.getSecond()); onePhysicalDelivery.put(deliveryGroup.getSecond(), !cart.getOrderInfo().isMultipleDelivery() || warehouse == null || !warehouse.isMultipleShippingSupported()); } for (CartItem cartItem : cart.getCartItemList()) { final boolean itemToCheck = item.isGift() == cartItem.isGift() && item.getProductSkuCode().equals(cartItem.getProductSkuCode()); final DeliveryBucket cartItemBucket = itemToCheck ? itemBucket : cartItem.getDeliveryBucket(); if (cartItemBucket != null) { List<CartItem> items = deliveryGroups.computeIfAbsent(cartItemBucket, k -> new ArrayList<>()); items.add(cartItem); } } groupDeliveriesIntoMixedIfNecessary(deliveryGroups, onePhysicalDelivery); for (final Map.Entry<DeliveryBucket, List<CartItem>> group : deliveryGroups.entrySet()) { for (final CartItem groupItem : group.getValue()) { final boolean itemToCheck = item.isGift() == groupItem.isGift() && item.getProductSkuCode().equals(groupItem.getProductSkuCode()); if (itemToCheck) { return group.getKey(); } } } return itemBucket; } OrderSplittingStrategyImpl(final ShopService shopService,
                                      final ProductService productService,
                                      final WarehouseService warehouseService,
                                      final InventoryResolver inventoryResolver); }
OrderSplittingStrategyImpl implements OrderSplittingStrategy { @Override public DeliveryBucket determineDeliveryBucket(final CartItem item, final ShoppingCart cart) { final Map<String, Boolean> onePhysicalDelivery = new HashMap<>(); for (final Map.Entry<String, Boolean> isAllowed : cart.getOrderInfo().getMultipleDeliveryAvailable().entrySet()) { onePhysicalDelivery.put(isAllowed.getKey(), !cart.getOrderInfo().isMultipleDelivery() || !isAllowed.getValue()); } final Map<DeliveryBucket, List<CartItem>> deliveryGroups = new HashMap<>(); DeliveryBucket itemBucket = item.getDeliveryBucket(); if (item.getDeliveryBucket() == null) { final Map<String, Warehouse> warehouses = warehouseService.getByShopIdMapped(cart.getShoppingContext().getCustomerShopId(), false); final Pair<String, String> deliveryGroup = getDeliveryGroup(item, warehouses); final Warehouse warehouse = warehouses.get(deliveryGroup.getSecond()); itemBucket = new DeliveryBucketImpl(deliveryGroup.getFirst(), deliveryGroup.getSecond()); onePhysicalDelivery.put(deliveryGroup.getSecond(), !cart.getOrderInfo().isMultipleDelivery() || warehouse == null || !warehouse.isMultipleShippingSupported()); } for (CartItem cartItem : cart.getCartItemList()) { final boolean itemToCheck = item.isGift() == cartItem.isGift() && item.getProductSkuCode().equals(cartItem.getProductSkuCode()); final DeliveryBucket cartItemBucket = itemToCheck ? itemBucket : cartItem.getDeliveryBucket(); if (cartItemBucket != null) { List<CartItem> items = deliveryGroups.computeIfAbsent(cartItemBucket, k -> new ArrayList<>()); items.add(cartItem); } } groupDeliveriesIntoMixedIfNecessary(deliveryGroups, onePhysicalDelivery); for (final Map.Entry<DeliveryBucket, List<CartItem>> group : deliveryGroups.entrySet()) { for (final CartItem groupItem : group.getValue()) { final boolean itemToCheck = item.isGift() == groupItem.isGift() && item.getProductSkuCode().equals(groupItem.getProductSkuCode()); if (itemToCheck) { return group.getKey(); } } } return itemBucket; } OrderSplittingStrategyImpl(final ShopService shopService,
                                      final ProductService productService,
                                      final WarehouseService warehouseService,
                                      final InventoryResolver inventoryResolver); @Override DeliveryBucket determineDeliveryBucket(final CartItem item,
                                                  final ShoppingCart cart); @Override Map<DeliveryBucket, List<CartItem>> determineDeliveryBuckets(final long shopId,
                                                                        final Collection<CartItem> items,
                                                                        final Map<String, Boolean> onePhysicalDelivery); @Override Map<String, Boolean> isMultipleDeliveriesAllowed(final long shopId, final Collection<CartItem> items); }
OrderSplittingStrategyImpl implements OrderSplittingStrategy { @Override public DeliveryBucket determineDeliveryBucket(final CartItem item, final ShoppingCart cart) { final Map<String, Boolean> onePhysicalDelivery = new HashMap<>(); for (final Map.Entry<String, Boolean> isAllowed : cart.getOrderInfo().getMultipleDeliveryAvailable().entrySet()) { onePhysicalDelivery.put(isAllowed.getKey(), !cart.getOrderInfo().isMultipleDelivery() || !isAllowed.getValue()); } final Map<DeliveryBucket, List<CartItem>> deliveryGroups = new HashMap<>(); DeliveryBucket itemBucket = item.getDeliveryBucket(); if (item.getDeliveryBucket() == null) { final Map<String, Warehouse> warehouses = warehouseService.getByShopIdMapped(cart.getShoppingContext().getCustomerShopId(), false); final Pair<String, String> deliveryGroup = getDeliveryGroup(item, warehouses); final Warehouse warehouse = warehouses.get(deliveryGroup.getSecond()); itemBucket = new DeliveryBucketImpl(deliveryGroup.getFirst(), deliveryGroup.getSecond()); onePhysicalDelivery.put(deliveryGroup.getSecond(), !cart.getOrderInfo().isMultipleDelivery() || warehouse == null || !warehouse.isMultipleShippingSupported()); } for (CartItem cartItem : cart.getCartItemList()) { final boolean itemToCheck = item.isGift() == cartItem.isGift() && item.getProductSkuCode().equals(cartItem.getProductSkuCode()); final DeliveryBucket cartItemBucket = itemToCheck ? itemBucket : cartItem.getDeliveryBucket(); if (cartItemBucket != null) { List<CartItem> items = deliveryGroups.computeIfAbsent(cartItemBucket, k -> new ArrayList<>()); items.add(cartItem); } } groupDeliveriesIntoMixedIfNecessary(deliveryGroups, onePhysicalDelivery); for (final Map.Entry<DeliveryBucket, List<CartItem>> group : deliveryGroups.entrySet()) { for (final CartItem groupItem : group.getValue()) { final boolean itemToCheck = item.isGift() == groupItem.isGift() && item.getProductSkuCode().equals(groupItem.getProductSkuCode()); if (itemToCheck) { return group.getKey(); } } } return itemBucket; } OrderSplittingStrategyImpl(final ShopService shopService,
                                      final ProductService productService,
                                      final WarehouseService warehouseService,
                                      final InventoryResolver inventoryResolver); @Override DeliveryBucket determineDeliveryBucket(final CartItem item,
                                                  final ShoppingCart cart); @Override Map<DeliveryBucket, List<CartItem>> determineDeliveryBuckets(final long shopId,
                                                                        final Collection<CartItem> items,
                                                                        final Map<String, Boolean> onePhysicalDelivery); @Override Map<String, Boolean> isMultipleDeliveriesAllowed(final long shopId, final Collection<CartItem> items);  }