@Test public void testGroupDeliveriesIntoMixedIfNecessaryWithElectronicTwoSuppliersSingleTrue() throws Exception { final CartItem d1 = context.mock(CartItem.class, "d1"); final CartItem d2 = context.mock(CartItem.class, "d2"); final CartItem d3 = context.mock(CartItem.class, "d3"); final CartItem d4 = context.mock(CartItem.class, "d4"); context.checking(new Expectations() {{ allowing(d1).getItemGroup(); will(returnValue(null)); allowing(d2).getItemGroup(); will(returnValue(null)); allowing(d3).getItemGroup(); will(returnValue(null)); allowing(d4).getItemGroup(); will(returnValue(null)); }}); final Map<DeliveryBucket, List<CartItem>> groups = new TreeMap<>(); groups.put(new DeliveryBucketImpl(CustomerOrderDelivery.STANDARD_DELIVERY_GROUP, "S002"), Collections.<CartItem>singletonList(d1)); groups.put(new DeliveryBucketImpl(CustomerOrderDelivery.DATE_WAIT_DELIVERY_GROUP, "S001"), Collections.<CartItem>singletonList(d2)); groups.put(new DeliveryBucketImpl(CustomerOrderDelivery.INVENTORY_WAIT_DELIVERY_GROUP, "S001"), Collections.<CartItem>singletonList(d3)); groups.put(new DeliveryBucketImpl(CustomerOrderDelivery.ELECTRONIC_DELIVERY_GROUP, "S001"), Collections.<CartItem>singletonList(d4)); new OrderSplittingStrategyImpl(null, null, null, null).groupDeliveriesIntoMixedIfNecessary(groups, new HashMap<String, Boolean>() {{ put("S002", Boolean.TRUE); put("S001", Boolean.TRUE); }}); assertEquals(3, groups.size()); final List<CartItem> electronic = groups.get(new DeliveryBucketImpl(CustomerOrderDelivery.ELECTRONIC_DELIVERY_GROUP, "S001")); final List<CartItem> mixed = groups.get(new DeliveryBucketImpl(CustomerOrderDelivery.MIX_DELIVERY_GROUP, "S001")); final List<CartItem> standard2 = groups.get(new DeliveryBucketImpl(CustomerOrderDelivery.STANDARD_DELIVERY_GROUP, "S002")); assertNotNull(electronic); assertEquals(1, electronic.size()); assertSame(d4, electronic.get(0)); assertNotNull(mixed); assertEquals(2, mixed.size()); assertSame(d2, mixed.get(0)); assertSame(d3, mixed.get(1)); assertNotNull(standard2); assertEquals(1, standard2.size()); assertSame(d1, standard2.get(0)); }
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);  }