@Test public void testGroupDeliveriesIntoMixedIfNecessaryStandardMultipleSuppliersSingleMix() throws Exception { final Map<DeliveryBucket, List<CartItem>> buckets = new HashMap<>(); final DeliveryBucket d1 = context.mock(DeliveryBucket.class, "d1"); final DeliveryBucket d2 = context.mock(DeliveryBucket.class, "d2"); final DeliveryBucket d3 = context.mock(DeliveryBucket.class, "d3"); final DeliveryBucket d4 = context.mock(DeliveryBucket.class, "d4"); final DeliveryBucket oos = context.mock(DeliveryBucket.class, "oos"); final DeliveryBucket off = context.mock(DeliveryBucket.class, "off"); buckets.put(d1, Collections.<CartItem>emptyList()); buckets.put(d2, Collections.<CartItem>emptyList()); buckets.put(d3, Collections.<CartItem>emptyList()); buckets.put(d4, Collections.<CartItem>emptyList()); buckets.put(oos, Collections.<CartItem>emptyList()); buckets.put(off, Collections.<CartItem>emptyList()); context.checking(new Expectations() {{ allowing(d1).getSupplier(); will(returnValue("s2")); allowing(d1).getGroup(); will(returnValue(CustomerOrderDelivery.STANDARD_DELIVERY_GROUP)); allowing(d2).getSupplier(); will(returnValue("s1")); allowing(d2).getGroup(); will(returnValue(CustomerOrderDelivery.DATE_WAIT_DELIVERY_GROUP)); allowing(d3).getSupplier(); will(returnValue("s2")); allowing(d3).getGroup(); will(returnValue(CustomerOrderDelivery.INVENTORY_WAIT_DELIVERY_GROUP)); allowing(d4).getSupplier(); will(returnValue("s1")); allowing(d4).getGroup(); will(returnValue(CustomerOrderDelivery.ELECTRONIC_DELIVERY_GROUP)); allowing(oos).getSupplier(); will(returnValue("s3")); allowing(oos).getGroup(); will(returnValue(CustomerOrderDelivery.NOSTOCK_DELIVERY_GROUP)); allowing(off).getSupplier(); will(returnValue("s1")); allowing(off).getGroup(); will(returnValue(CustomerOrderDelivery.OFFLINE_DELIVERY_GROUP)); }}); new OrderSplittingStrategyImpl(null, null, null, null).groupDeliveriesIntoMixedIfNecessary(buckets, new HashMap<String, Boolean>() {{ put("s1", Boolean.FALSE); put("s2", Boolean.TRUE); put("s3", Boolean.FALSE); }}); assertNotNull(buckets); assertEquals(5, buckets.size()); final Map<String, Set<String>> expected = new HashMap<String, Set<String>>() {{ put("s1", new HashSet<>(Arrays.asList( CustomerOrderDelivery.ELECTRONIC_DELIVERY_GROUP, CustomerOrderDelivery.OFFLINE_DELIVERY_GROUP, CustomerOrderDelivery.DATE_WAIT_DELIVERY_GROUP ))); put("s2", new HashSet<>(Collections.singletonList( CustomerOrderDelivery.MIX_DELIVERY_GROUP ))); put("s3", new HashSet<>(Collections.singletonList( CustomerOrderDelivery.NOSTOCK_DELIVERY_GROUP ))); }}; for (final DeliveryBucket key : buckets.keySet()) { assertTrue("Bucket not expected " + key, expected.get(key.getSupplier()).contains(key.getGroup())); } }
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);  }