use of org.broadleafcommerce.core.offer.domain.OfferOfferRuleXrefImpl in project BroadleafCommerce by BroadleafCommerce.
the class OfferServiceTest method testApplyOffersToOrder_Order.
public void testApplyOffersToOrder_Order() throws Exception {
final ThreadLocal<Order> myOrder = new ThreadLocal<>();
EasyMock.expect(offerDaoMock.createOrderItemPriceDetailAdjustment()).andAnswer(OfferDataItemProvider.getCreateOrderItemPriceDetailAdjustmentAnswer()).anyTimes();
CandidateOrderOfferAnswer candidateOrderOfferAnswer = new CandidateOrderOfferAnswer();
OrderAdjustmentAnswer orderAdjustmentAnswer = new OrderAdjustmentAnswer();
EasyMock.expect(offerDaoMock.createOrderAdjustment()).andAnswer(orderAdjustmentAnswer).atLeastOnce();
OrderItemPriceDetailAnswer orderItemPriceDetailAnswer = new OrderItemPriceDetailAnswer();
EasyMock.expect(orderItemDaoMock.createOrderItemPriceDetail()).andAnswer(orderItemPriceDetailAnswer).atLeastOnce();
OrderItemQualifierAnswer orderItemQualifierAnswer = new OrderItemQualifierAnswer();
EasyMock.expect(orderItemDaoMock.createOrderItemQualifier()).andAnswer(orderItemQualifierAnswer).atLeastOnce();
CandidateItemOfferAnswer candidateItemOfferAnswer = new CandidateItemOfferAnswer();
OrderItemAdjustmentAnswer orderItemAdjustmentAnswer = new OrderItemAdjustmentAnswer();
EasyMock.expect(fgServiceMock.addItemToFulfillmentGroup(EasyMock.isA(FulfillmentGroupItemRequest.class), EasyMock.eq(false))).andAnswer(OfferDataItemProvider.getAddItemToFulfillmentGroupAnswer()).anyTimes();
EasyMock.expect(orderServiceMock.removeItem(EasyMock.isA(Long.class), EasyMock.isA(Long.class), EasyMock.eq(false))).andAnswer(OfferDataItemProvider.getRemoveItemFromOrderAnswer()).anyTimes();
EasyMock.expect(orderServiceMock.save(EasyMock.isA(Order.class), EasyMock.isA(Boolean.class))).andAnswer(OfferDataItemProvider.getSaveOrderAnswer()).anyTimes();
EasyMock.expect(orderServiceMock.findOrderById(EasyMock.isA(Long.class))).andAnswer(new IAnswer<Order>() {
@Override
public Order answer() throws Throwable {
return myOrder.get();
}
}).anyTimes();
EasyMock.expect(orderServiceMock.getAutomaticallyMergeLikeItems()).andReturn(true).anyTimes();
EasyMock.expect(orderItemServiceMock.saveOrderItem(EasyMock.isA(OrderItem.class))).andAnswer(OfferDataItemProvider.getSaveOrderItemAnswer()).anyTimes();
EasyMock.expect(fgItemDaoMock.save(EasyMock.isA(FulfillmentGroupItem.class))).andAnswer(OfferDataItemProvider.getSaveFulfillmentGroupItemAnswer()).anyTimes();
EasyMock.expect(multishipOptionServiceMock.findOrderMultishipOptions(EasyMock.isA(Long.class))).andAnswer(new IAnswer<List<OrderMultishipOption>>() {
@Override
public List<OrderMultishipOption> answer() throws Throwable {
return new ArrayList<>();
}
}).anyTimes();
multishipOptionServiceMock.deleteAllOrderMultishipOptions(EasyMock.isA(Order.class));
EasyMock.expectLastCall().anyTimes();
EasyMock.expect(fgServiceMock.collapseToOneShippableFulfillmentGroup(EasyMock.isA(Order.class), EasyMock.eq(false))).andAnswer(OfferDataItemProvider.getSameOrderAnswer()).anyTimes();
EasyMock.expect(fgItemDaoMock.create()).andAnswer(OfferDataItemProvider.getCreateFulfillmentGroupItemAnswer()).anyTimes();
fgItemDaoMock.delete(EasyMock.isA(FulfillmentGroupItem.class));
EasyMock.expectLastCall().anyTimes();
EasyMock.expect(offerTimeZoneProcessorMock.getTimeZone(EasyMock.isA(OfferImpl.class))).andReturn(TimeZone.getTimeZone("CST")).anyTimes();
replay();
Order order = dataProvider.createBasicOrder();
myOrder.set(order);
List<Offer> offers = dataProvider.createOrderBasedOffer("order.subTotal.getAmount()>126", OfferDiscountType.PERCENT_OFF);
offerService.applyAndSaveOffersToOrder(offers, order);
int adjustmentCount = order.getOrderAdjustments().size();
assertTrue(adjustmentCount == 1);
assertTrue(order.getSubTotal().subtract(order.getOrderAdjustmentsValue()).equals(new Money(116.95D)));
order = dataProvider.createBasicOrder();
myOrder.set(order);
offers = dataProvider.createOrderBasedOffer("order.subTotal.getAmount()>126", OfferDiscountType.PERCENT_OFF);
List<Offer> offers2 = dataProvider.createItemBasedOfferWithItemCriteria("order.subTotal.getAmount()>20", OfferDiscountType.PERCENT_OFF, "([MVEL.eval(\"toUpperCase()\",\"test1\"), MVEL.eval(\"toUpperCase()\",\"test2\")] contains MVEL.eval(\"toUpperCase()\", discreteOrderItem.category.name))", "([MVEL.eval(\"toUpperCase()\",\"test1\"), MVEL.eval(\"toUpperCase()\",\"test2\")] contains MVEL.eval(\"toUpperCase()\", discreteOrderItem.category.name))");
offers.addAll(offers2);
offerService.applyAndSaveOffersToOrder(offers, order);
// with the item offers in play, the subtotal restriction for the order offer is no longer valid
adjustmentCount = countItemAdjustments(order);
int qualifierCount = countItemQualifiers(order);
assertTrue(adjustmentCount == 2);
assertTrue(qualifierCount == 2);
adjustmentCount = order.getOrderAdjustments().size();
assertTrue(adjustmentCount == 0);
// assertTrue(order.getSubTotal().equals(new Money(124.95D)));
order = dataProvider.createBasicOrder();
myOrder.set(order);
OfferRule orderRule = new OfferRuleImpl();
// orderRule.setMatchRule("order.subTotal.getAmount()>124");
orderRule.setMatchRule("order.subTotal.getAmount()>100");
Offer offer = offers.get(0);
OfferOfferRuleXref ruleXref = new OfferOfferRuleXrefImpl(offer, orderRule, OfferRuleType.ORDER.getType());
offer.getOfferMatchRulesXref().put(OfferRuleType.ORDER.getType(), ruleXref);
offerService.applyAndSaveOffersToOrder(offers, order);
// now that the order restriction has been lessened, even with the item level discounts applied,
// the order offer still qualifies
adjustmentCount = countItemAdjustments(order);
qualifierCount = countItemQualifiers(order);
assertTrue(adjustmentCount == 2);
assertTrue(qualifierCount == 2);
adjustmentCount = order.getOrderAdjustments().size();
assertTrue(adjustmentCount == 1);
assertTrue(order.getSubTotal().subtract(order.getOrderAdjustmentsValue()).equals(new Money(112.45D)));
assertTrue(order.getSubTotal().equals(new Money(124.95D)));
order = dataProvider.createBasicPromotableOrder().getOrder();
myOrder.set(order);
// offers.get(0).setCombinableWithOtherOffers(false);
List<Offer> offers3 = dataProvider.createOrderBasedOffer("order.subTotal.getAmount()>20", OfferDiscountType.AMOUNT_OFF);
offers.addAll(offers3);
offerService.applyAndSaveOffersToOrder(offers, order);
adjustmentCount = order.getOrderAdjustments().size();
assertTrue(adjustmentCount == 2);
order = dataProvider.createBasicPromotableOrder().getOrder();
myOrder.set(order);
offers.get(0).setCombinableWithOtherOffers(false);
offerService.applyAndSaveOffersToOrder(offers, order);
// there is a non combinable order offer now
adjustmentCount = countItemAdjustments(order);
qualifierCount = countItemQualifiers(order);
assertTrue(adjustmentCount == 2);
assertTrue(qualifierCount == 2);
adjustmentCount = order.getOrderAdjustments().size();
assertTrue(adjustmentCount == 1);
assertTrue(order.getSubTotal().subtract(order.getOrderAdjustmentsValue()).equals(new Money(112.45D)));
assertTrue(order.getSubTotal().equals(new Money(124.95D)));
order = dataProvider.createBasicPromotableOrder().getOrder();
myOrder.set(order);
offers.get(0).setTotalitarianOffer(true);
offerService.applyAndSaveOffersToOrder(offers, order);
// there is a totalitarian order offer now - it is better than the item offers - the item offers are removed
adjustmentCount = countItemAdjustments(order);
qualifierCount = countItemQualifiers(order);
assertTrue(adjustmentCount == 0);
assertTrue(qualifierCount == 0);
adjustmentCount = order.getOrderAdjustments().size();
assertTrue(adjustmentCount == 1);
assertTrue(order.getSubTotal().subtract(order.getOrderAdjustmentsValue()).equals(new Money(116.95D)));
assertTrue(order.getSubTotal().equals(new Money(129.95D)));
order = dataProvider.createBasicPromotableOrder().getOrder();
myOrder.set(order);
offers.get(0).setValue(new BigDecimal(".05"));
offers.get(2).setValue(new BigDecimal(".01"));
offers.get(2).setDiscountType(OfferDiscountType.PERCENT_OFF);
offerService.applyAndSaveOffersToOrder(offers, order);
// even though the first order offer is totalitarian, it is worth less than the order item offer, so it is removed.
// the other order offer is still valid, however, and is included.
adjustmentCount = countItemAdjustments(order);
assertTrue(adjustmentCount == 2);
adjustmentCount = order.getOrderAdjustments().size();
assertTrue(adjustmentCount == 1);
assertTrue(order.getSubTotal().subtract(order.getOrderAdjustmentsValue()).equals(new Money(124.94D)));
assertTrue(order.getSubTotal().equals(new Money(124.95D)));
verify();
}
Aggregations