@Test public void testGroupDeliveriesIntoMixedIfNecessaryMultipleSuppliersSingleMixed() throws Exception { final CartItem d1_1 = context.mock(CartItem.class, "d1_1"); final CartItem d2_1 = context.mock(CartItem.class, "d2_1"); final CartItem d3_1 = context.mock(CartItem.class, "d3_1"); final CartItem d1_2 = context.mock(CartItem.class, "d1_2"); final CartItem d2_2 = context.mock(CartItem.class, "d2_2"); final CartItem d3_2 = context.mock(CartItem.class, "d3_2"); final CartItem d1_3 = context.mock(CartItem.class, "d1_3"); final CartItem d2_3 = context.mock(CartItem.class, "d2_3"); final CartItem d3_3 = context.mock(CartItem.class, "d3_3"); context.checking(new Expectations() {{ allowing(d1_1).getItemGroup(); will(returnValue(null)); allowing(d2_1).getItemGroup(); will(returnValue(null)); allowing(d3_1).getItemGroup(); will(returnValue(null)); allowing(d1_2).getItemGroup(); will(returnValue(null)); allowing(d2_2).getItemGroup(); will(returnValue(null)); allowing(d3_2).getItemGroup(); will(returnValue(null)); allowing(d1_3).getItemGroup(); will(returnValue(null)); allowing(d2_3).getItemGroup(); will(returnValue(null)); allowing(d3_3).getItemGroup(); will(returnValue(null)); }}); final Map<DeliveryBucket, List<CartItem>> groups = new TreeMap<>(); groups.put(new DeliveryBucketImpl(CustomerOrderDelivery.STANDARD_DELIVERY_GROUP, "S001"), Collections.<CartItem>singletonList(d1_1)); groups.put(new DeliveryBucketImpl(CustomerOrderDelivery.DATE_WAIT_DELIVERY_GROUP, "S001"), Collections.<CartItem>singletonList(d2_1)); groups.put(new DeliveryBucketImpl(CustomerOrderDelivery.INVENTORY_WAIT_DELIVERY_GROUP, "S001"), Collections.<CartItem>singletonList(d3_1)); groups.put(new DeliveryBucketImpl(CustomerOrderDelivery.STANDARD_DELIVERY_GROUP, "S002"), Collections.<CartItem>singletonList(d1_2)); groups.put(new DeliveryBucketImpl(CustomerOrderDelivery.DATE_WAIT_DELIVERY_GROUP, "S002"), Collections.<CartItem>singletonList(d2_2)); groups.put(new DeliveryBucketImpl(CustomerOrderDelivery.INVENTORY_WAIT_DELIVERY_GROUP, "S002"), Collections.<CartItem>singletonList(d3_2)); groups.put(new DeliveryBucketImpl(CustomerOrderDelivery.STANDARD_DELIVERY_GROUP, "S003"), Collections.<CartItem>singletonList(d1_3)); groups.put(new DeliveryBucketImpl(CustomerOrderDelivery.DATE_WAIT_DELIVERY_GROUP, "S003"), Collections.<CartItem>singletonList(d2_3)); groups.put(new DeliveryBucketImpl(CustomerOrderDelivery.INVENTORY_WAIT_DELIVERY_GROUP, "S003"), Collections.<CartItem>singletonList(d3_3)); new OrderSplittingStrategyImpl(null, null, null, null).groupDeliveriesIntoMixedIfNecessary(groups, new HashMap<String, Boolean>() {{ put("S001", Boolean.TRUE); put("S002", Boolean.FALSE); put("S003", Boolean.TRUE); }}); assertEquals(5, groups.size()); final List<CartItem> mixed_1 = groups.get(new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, "S001")); assertNotNull(mixed_1); assertEquals(3, mixed_1.size()); assertSame(d1_1, mixed_1.get(0)); assertSame(d2_1, mixed_1.get(1)); assertSame(d3_1, mixed_1.get(2)); final List<CartItem> std_2 = groups.get(new DeliveryBucketImpl(CustomerOrderDelivery.STANDARD_DELIVERY_GROUP, "S002")); final List<CartItem> date_2 = groups.get(new DeliveryBucketImpl(CustomerOrderDelivery.DATE_WAIT_DELIVERY_GROUP, "S002")); final List<CartItem> inv_2 = groups.get(new DeliveryBucketImpl(CustomerOrderDelivery.INVENTORY_WAIT_DELIVERY_GROUP, "S002")); assertNotNull(std_2); assertEquals(1, std_2.size()); assertSame(d1_2, std_2.get(0)); assertNotNull(date_2); assertEquals(1, date_2.size()); assertSame(d2_2, date_2.get(0)); assertNotNull(inv_2); assertEquals(1, inv_2.size()); assertSame(d3_2, inv_2.get(0)); final List<CartItem> mixed_3 = groups.get(new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, "S003")); assertNotNull(mixed_3); assertEquals(3, mixed_3.size()); assertSame(d1_3, mixed_3.get(0)); assertSame(d2_3, mixed_3.get(1)); assertSame(d3_3, mixed_3.get(2)); }
void groupDeliveriesIntoMixedIfNecessary(final Map<DeliveryBucket, List<CartItem>> deliveryGroups, final Map<String, Boolean> onePhysicalDelivery) { final Map<String, Integer> deliveryQty = getPhysicalDeliveriesQty(deliveryGroups); final List<DeliveryBucket> removeBuckets = new ArrayList<>(); final Map<DeliveryBucket, List<CartItem>> collectors = new HashMap<>(); final Set<String> splitGroups = getSplitItemGroups(deliveryGroups); for (Map.Entry<DeliveryBucket, List<CartItem>> entry : deliveryGroups.entrySet()) { final String group = entry.getKey().getGroup(); final String supplier = entry.getKey().getSupplier(); final Boolean forceSingleDelivery = onePhysicalDelivery.get(supplier); if (forceSingleDelivery != null && forceSingleDelivery && deliveryQty.get(supplier) > 1) { if (!CustomerOrderDelivery.ELECTRONIC_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.NOSTOCK_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.OFFLINE_DELIVERY_GROUP.equals(group)) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); removeBuckets.add(entry.getKey()); collector.addAll(entry.getValue()); } } else if (CollectionUtils.isNotEmpty(splitGroups)) { final Iterator<CartItem> lineIt = entry.getValue().iterator(); while (lineIt.hasNext()) { final CartItem line = lineIt.next(); if (splitGroups.contains(line.getItemGroup())) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); collector.add(line); lineIt.remove(); if (entry.getValue().isEmpty()) { removeBuckets.add(entry.getKey()); } } } } } deliveryGroups.putAll(collectors); deliveryGroups.keySet().removeAll(removeBuckets); }
OrderSplittingStrategyImpl implements OrderSplittingStrategy { void groupDeliveriesIntoMixedIfNecessary(final Map<DeliveryBucket, List<CartItem>> deliveryGroups, final Map<String, Boolean> onePhysicalDelivery) { final Map<String, Integer> deliveryQty = getPhysicalDeliveriesQty(deliveryGroups); final List<DeliveryBucket> removeBuckets = new ArrayList<>(); final Map<DeliveryBucket, List<CartItem>> collectors = new HashMap<>(); final Set<String> splitGroups = getSplitItemGroups(deliveryGroups); for (Map.Entry<DeliveryBucket, List<CartItem>> entry : deliveryGroups.entrySet()) { final String group = entry.getKey().getGroup(); final String supplier = entry.getKey().getSupplier(); final Boolean forceSingleDelivery = onePhysicalDelivery.get(supplier); if (forceSingleDelivery != null && forceSingleDelivery && deliveryQty.get(supplier) > 1) { if (!CustomerOrderDelivery.ELECTRONIC_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.NOSTOCK_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.OFFLINE_DELIVERY_GROUP.equals(group)) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); removeBuckets.add(entry.getKey()); collector.addAll(entry.getValue()); } } else if (CollectionUtils.isNotEmpty(splitGroups)) { final Iterator<CartItem> lineIt = entry.getValue().iterator(); while (lineIt.hasNext()) { final CartItem line = lineIt.next(); if (splitGroups.contains(line.getItemGroup())) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); collector.add(line); lineIt.remove(); if (entry.getValue().isEmpty()) { removeBuckets.add(entry.getKey()); } } } } } deliveryGroups.putAll(collectors); deliveryGroups.keySet().removeAll(removeBuckets); } }
OrderSplittingStrategyImpl implements OrderSplittingStrategy { void groupDeliveriesIntoMixedIfNecessary(final Map<DeliveryBucket, List<CartItem>> deliveryGroups, final Map<String, Boolean> onePhysicalDelivery) { final Map<String, Integer> deliveryQty = getPhysicalDeliveriesQty(deliveryGroups); final List<DeliveryBucket> removeBuckets = new ArrayList<>(); final Map<DeliveryBucket, List<CartItem>> collectors = new HashMap<>(); final Set<String> splitGroups = getSplitItemGroups(deliveryGroups); for (Map.Entry<DeliveryBucket, List<CartItem>> entry : deliveryGroups.entrySet()) { final String group = entry.getKey().getGroup(); final String supplier = entry.getKey().getSupplier(); final Boolean forceSingleDelivery = onePhysicalDelivery.get(supplier); if (forceSingleDelivery != null && forceSingleDelivery && deliveryQty.get(supplier) > 1) { if (!CustomerOrderDelivery.ELECTRONIC_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.NOSTOCK_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.OFFLINE_DELIVERY_GROUP.equals(group)) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); removeBuckets.add(entry.getKey()); collector.addAll(entry.getValue()); } } else if (CollectionUtils.isNotEmpty(splitGroups)) { final Iterator<CartItem> lineIt = entry.getValue().iterator(); while (lineIt.hasNext()) { final CartItem line = lineIt.next(); if (splitGroups.contains(line.getItemGroup())) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); collector.add(line); lineIt.remove(); if (entry.getValue().isEmpty()) { removeBuckets.add(entry.getKey()); } } } } } deliveryGroups.putAll(collectors); deliveryGroups.keySet().removeAll(removeBuckets); } OrderSplittingStrategyImpl(final ShopService shopService,
                                      final ProductService productService,
                                      final WarehouseService warehouseService,
                                      final InventoryResolver inventoryResolver); }
OrderSplittingStrategyImpl implements OrderSplittingStrategy { void groupDeliveriesIntoMixedIfNecessary(final Map<DeliveryBucket, List<CartItem>> deliveryGroups, final Map<String, Boolean> onePhysicalDelivery) { final Map<String, Integer> deliveryQty = getPhysicalDeliveriesQty(deliveryGroups); final List<DeliveryBucket> removeBuckets = new ArrayList<>(); final Map<DeliveryBucket, List<CartItem>> collectors = new HashMap<>(); final Set<String> splitGroups = getSplitItemGroups(deliveryGroups); for (Map.Entry<DeliveryBucket, List<CartItem>> entry : deliveryGroups.entrySet()) { final String group = entry.getKey().getGroup(); final String supplier = entry.getKey().getSupplier(); final Boolean forceSingleDelivery = onePhysicalDelivery.get(supplier); if (forceSingleDelivery != null && forceSingleDelivery && deliveryQty.get(supplier) > 1) { if (!CustomerOrderDelivery.ELECTRONIC_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.NOSTOCK_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.OFFLINE_DELIVERY_GROUP.equals(group)) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); removeBuckets.add(entry.getKey()); collector.addAll(entry.getValue()); } } else if (CollectionUtils.isNotEmpty(splitGroups)) { final Iterator<CartItem> lineIt = entry.getValue().iterator(); while (lineIt.hasNext()) { final CartItem line = lineIt.next(); if (splitGroups.contains(line.getItemGroup())) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); collector.add(line); lineIt.remove(); if (entry.getValue().isEmpty()) { removeBuckets.add(entry.getKey()); } } } } } deliveryGroups.putAll(collectors); deliveryGroups.keySet().removeAll(removeBuckets); } 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 { void groupDeliveriesIntoMixedIfNecessary(final Map<DeliveryBucket, List<CartItem>> deliveryGroups, final Map<String, Boolean> onePhysicalDelivery) { final Map<String, Integer> deliveryQty = getPhysicalDeliveriesQty(deliveryGroups); final List<DeliveryBucket> removeBuckets = new ArrayList<>(); final Map<DeliveryBucket, List<CartItem>> collectors = new HashMap<>(); final Set<String> splitGroups = getSplitItemGroups(deliveryGroups); for (Map.Entry<DeliveryBucket, List<CartItem>> entry : deliveryGroups.entrySet()) { final String group = entry.getKey().getGroup(); final String supplier = entry.getKey().getSupplier(); final Boolean forceSingleDelivery = onePhysicalDelivery.get(supplier); if (forceSingleDelivery != null && forceSingleDelivery && deliveryQty.get(supplier) > 1) { if (!CustomerOrderDelivery.ELECTRONIC_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.NOSTOCK_DELIVERY_GROUP.equals(group) && !CustomerOrderDelivery.OFFLINE_DELIVERY_GROUP.equals(group)) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); removeBuckets.add(entry.getKey()); collector.addAll(entry.getValue()); } } else if (CollectionUtils.isNotEmpty(splitGroups)) { final Iterator<CartItem> lineIt = entry.getValue().iterator(); while (lineIt.hasNext()) { final CartItem line = lineIt.next(); if (splitGroups.contains(line.getItemGroup())) { final DeliveryBucket mix = new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, supplier); List<CartItem> collector = collectors.computeIfAbsent(mix, k -> new ArrayList<>()); collector.add(line); lineIt.remove(); if (entry.getValue().isEmpty()) { removeBuckets.add(entry.getKey()); } } } } } deliveryGroups.putAll(collectors); deliveryGroups.keySet().removeAll(removeBuckets); } 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);  }