ShoppingCartImpl implements MutableShoppingCart { @Override public boolean addGiftToCart(final String supplier, final String sku, final String skuName, final BigDecimal quantity, final String promotionCode) { for (final CartItemImpl gift : getGifts()) { if (ShoppingCartUtils.isCartItem(gift, supplier, sku)) { gift.addQuantity(quantity); addPromoCode(gift, promotionCode); return false; } } final CartItemImpl newItem = new CartItemImpl(); newItem.setSupplierCode(supplier); newItem.setProductSkuCode(sku); newItem.setProductName(skuName); newItem.setQuantity(quantity); newItem.setGift(true); newItem.setPromoApplied(true); newItem.setAppliedPromo(promotionCode); getGifts().add(newItem); return true; }  @Override void initialise(final AmountCalculationStrategy calculationStrategy); @Override void markDirty(); @Override void clean(); @Override void recalculate(); @Override @JsonIgnore boolean isSeparateBillingAddress(); @Override @JsonIgnore boolean isBillingAddressNotRequired(); @Override @JsonIgnore boolean isDeliveryAddressNotRequired(); @Override @JsonIgnore String getOrderMessage(); @Override String getGuid(); @Override @JsonIgnore Map<String, Long> getCarrierSlaId(); @Override @JsonIgnore boolean isAllCarrierSlaSelected(); @Override @JsonIgnore boolean isAllCartItemsBucketed(); @Override @JsonIgnore List<CartItem> getCartItemList(); @Override @JsonIgnore Map<DeliveryBucket, List<CartItem>> getCartItemMap(); @Override @JsonIgnore List<CartItem> getShippingList(); @Override @JsonIgnore Map<DeliveryBucket, List<CartItem>> getShippingListMap(); @Override BigDecimal getProductSkuQuantity(final String supplier,
                                            final String sku); @Override boolean addProductSkuToCart(final String supplier,
                                       final String sku,
                                       final String skuName,
                                       final BigDecimal quantity,
                                       final String itemGroup,
                                       final boolean configurable,
                                       final boolean notSoldSeparately); @Override boolean addShippingToCart(final DeliveryBucket deliveryBucket,
                                     final String carrierSlaGUID,
                                     final String carrierSlaName,
                                     final BigDecimal quantity); @Override boolean addGiftToCart(final String supplier,
                                 final String sku,
                                 final String skuName,
                                 final BigDecimal quantity,
                                 final String promotionCode); @Override boolean setProductSkuToCart(final String supplier,
                                       final String sku,
                                       final String skuName,
                                       final BigDecimal quantity,
                                       final String itemGroup); @Override boolean setProductSkuDeliveryBucket(final String sku,
                                               final DeliveryBucket deliveryBucket); @Override boolean setGiftDeliveryBucket(final String sku,
                                         final DeliveryBucket deliveryBucket); @Override boolean removeCartItem(final String supplier,
                                  final String productSku,
                                  final String itemGroup); @Override boolean removeCartItemQuantity(final String supplier,
                                          final String productSku,
                                          final BigDecimal quantity,
                                          final String itemGroup); @Override boolean removeItemPromotions(); @Override boolean removeItemOffers(); @Override boolean removeShipping(); @Override boolean removeShipping(final String carrierSlaGUID,
                                  final DeliveryBucket deliveryBucket); @Override boolean setProductSkuPrice(final String supplier,
                                      final String skuCode,
                                      final BigDecimal salePrice,
                                      final BigDecimal listPrice); @Override boolean setShippingPrice(final String carrierSlaGUID,
                                    final DeliveryBucket deliveryBucket,
                                    final BigDecimal salePrice,
                                    final BigDecimal listPrice); @Override boolean setGiftPrice(final String supplier,
                                final String skuCode,
                                final BigDecimal salePrice,
                                final BigDecimal listPrice); @Override boolean setProductSkuPromotion(final String supplier,
                                          final String skuCode,
                                          final BigDecimal promoPrice,
                                          final String promoCode); @Override boolean setProductSkuOffer(final String supplier,
                                      final String skuCode,
                                      final BigDecimal fixedPrice,
                                      final String authCode); @Override boolean setShippingPromotion(final String carrierSlaGUID,
                                        final DeliveryBucket deliveryBucket,
                                        final BigDecimal promoPrice,
                                        final String promoCode); @Override boolean setProductSkuTax(final String supplier,
                                    final String skuCode,
                                    final BigDecimal netPrice,
                                    final BigDecimal grossPrice,
                                    final BigDecimal rate,
                                    final String taxCode,
                                    final boolean exclPrice); @Override boolean setShippingTax(final String carrierSlaGUID, final DeliveryBucket deliveryBucket, final BigDecimal netPrice, final BigDecimal grossPrice, final BigDecimal rate, final String taxCode, final boolean exclPrice); @Override List<String> getCoupons(); @Override @JsonIgnore List<String> getAppliedCoupons(); @Override boolean addCoupon(final String coupon); @Override boolean removeCoupon(final String coupon); @Override @JsonIgnore int getCartItemsCount(); @Override @JsonIgnore List<String> getCartItemsSuppliers(); @Override String getCurrencyCode(); @Override void setCurrencyCode(final String currencyCode); @Override @JsonIgnore String getCustomerName(); @Override long getModifiedTimestamp(); @Override @JsonIgnore boolean isModified(); @Override @JsonIgnore boolean hasGifts(); @Override @JsonIgnore boolean isPromotionsDisabled(); @Override void setPromotionsDisabled(final boolean promotionsDisabled); @Override @JsonIgnore String getOrdernum(); @Override void setOrdernum(final String ordernum); @Override @JsonIgnore String getCustomerEmail(); @Override @JsonIgnore int getLogonState(); @Override MutableShoppingContext getShoppingContext(); @Override String getCurrentLocale(); @Override void setCurrentLocale(final String currentLocale); @Override MutableOrderInfo getOrderInfo(); @Override long getProcessingStartTimestamp(); @Override Total getTotal();  }
@Test public void testAddGiftToCart() throws Exception { boolean newItem1 = cart.addProductSkuToCart("s01", "sku01", "SKU name", BigDecimal.TEN, null, false, false); boolean newItem2 = cart.addGiftToCart("s01", "sku01", "SKU name", BigDecimal.ONE, "TEST01"); boolean newItem3 = cart.addProductSkuToCart("s01", "sku02", "SKU name", BigDecimal.TEN, null, false, false); boolean newItem4 = cart.addGiftToCart("s01", "sku01", "SKU name", BigDecimal.ONE, "TEST02"); assertTrue("Must create new item", newItem1); assertTrue("Must not create new item", newItem2); assertTrue("Must create new item", newItem3); assertFalse("Must not create new item", newItem4); assertEquals("Size should be 30", 22, cart.getCartItemsCount()); assertTrue("sku exists", cart.getCartItemList().stream().anyMatch( item -> "sku01".equals(item.getProductSkuCode()) && "s01".equals(item.getSupplierCode()))); assertEquals("Index must be 0 for sku01", "sku01", cart.getGifts().get(0).getProductSkuCode()); assertTrue("sku exists", cart.getCartItemList().stream().anyMatch( item -> "sku02".equals(item.getProductSkuCode()) && "s01".equals(item.getSupplierCode()))); assertEquals("Items must have 2 elements", 3, cart.getCartItemList().size()); assertEquals("1st element must be sku01", "sku01", cart.getCartItemList().get(0).getProductSkuCode()); assertEquals("2nd element must be sku02", "sku02", cart.getCartItemList().get(1).getProductSkuCode()); assertEquals("3rd element must be sku01", "sku01", cart.getCartItemList().get(2).getProductSkuCode()); assertTrue("3rd element must be gift", cart.getCartItemList().get(2).isGift()); assertEquals("3rd element must have promos", "TEST01,TEST02", cart.getCartItemList().get(2).getAppliedPromo()); }