use of org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder in project BroadleafCommerce by BroadleafCommerce.
the class ItemOfferProcessorTest method testApplyItemQualifiersAndTargets.
public void testApplyItemQualifiersAndTargets() throws Exception {
replay();
List<PromotableCandidateItemOffer> qualifiedOffers = new ArrayList<PromotableCandidateItemOffer>();
Offer offer1 = 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))").get(0);
offer1.setId(1L);
Offer offer2 = 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))").get(0);
offer2.setId(2L);
OfferTargetCriteriaXref targetXref = offer2.getTargetItemCriteriaXref().iterator().next();
targetXref.getOfferItemCriteria().setQuantity(4);
offer2.getQualifyingItemCriteriaXref().clear();
offer2.setOfferItemTargetRuleType(OfferItemRestrictionRuleType.TARGET);
Offer offer3 = 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))").get(0);
PromotableOrder promotableOrder = dataProvider.createBasicPromotableOrder();
itemProcessor.filterItemLevelOffer(promotableOrder, qualifiedOffers, offer1);
assertTrue(qualifiedOffers.size() == 1 && qualifiedOffers.get(0).getOffer().equals(offer1) && qualifiedOffers.get(0).getCandidateQualifiersMap().size() == 1);
itemProcessor.filterItemLevelOffer(promotableOrder, qualifiedOffers, offer2);
assertTrue(qualifiedOffers.size() == 2 && qualifiedOffers.get(1).getOffer().equals(offer2) && qualifiedOffers.get(1).getCandidateQualifiersMap().size() == 0);
itemProcessor.filterItemLevelOffer(promotableOrder, qualifiedOffers, offer3);
assertTrue(qualifiedOffers.size() == 3 && qualifiedOffers.get(2).getOffer().equals(offer3) && qualifiedOffers.get(2).getCandidateQualifiersMap().size() == 1);
// Try with just the second offer. Expect to get 4 targets based on the offer having no qualifiers required
// and targeting category test1 or test2 and that the offer requires 4 target criteria.
Order order = dataProvider.createBasicOrder();
List<Offer> offerList = new ArrayList<Offer>();
offerList.add(offer2);
offerService.applyAndSaveOffersToOrder(offerList, order);
assertTrue(checkOrderItemOfferAppliedQuantity(order, offer2) == 4);
assertTrue(countPriceDetails(order) == 3);
// Now try with both offers. Since the targets can be reused, we expect to have 4 targets on offer2
// and 1 target on offer1
order = dataProvider.createBasicOrder();
// add in second offer (which happens to be offer1)
offerList.add(offer1);
offerService.applyAndSaveOffersToOrder(offerList, order);
assertTrue(checkOrderItemOfferAppliedQuantity(order, offer2) == 4);
assertTrue(countPriceDetails(order) == 3);
// All three offers - offer 3 is now higher priority so the best offer (offer 2) won't be applied
order = dataProvider.createBasicOrder();
// add in second offer (which happens to be offer1)
offerList.add(offer3);
offer3.setPriority(-1);
offerService.applyAndSaveOffersToOrder(offerList, order);
assertTrue(checkOrderItemOfferAppliedQuantity(order, offer3) == 2);
assertTrue(countPriceDetails(order) == 4);
verify();
}
use of org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder in project BroadleafCommerce by BroadleafCommerce.
the class ItemOfferProcessorTest method testFilterItemLevelOffer.
public void testFilterItemLevelOffer() throws Exception {
replay();
List<PromotableCandidateItemOffer> qualifiedOffers = new ArrayList<PromotableCandidateItemOffer>();
List<Offer> offers = dataProvider.createItemBasedOfferWithItemCriteria("order.subTotal.getAmount()>20", OfferDiscountType.PERCENT_OFF, null, null);
PromotableOrder order = dataProvider.createBasicPromotableOrder();
itemProcessor.filterItemLevelOffer(order, qualifiedOffers, offers.get(0));
// test that the valid order item offer is included - legacy format - no qualifier
// since there's no qualifier, both items can apply
// This line is commented out because we are no longer creating legacy offers.
// assertTrue(qualifiedOffers.size() == 2 && qualifiedOffers.get(0).getOffer().equals(offers.get(0)) && qualifiedOffers.get(1).getOffer().equals(offers.get(0)));
qualifiedOffers = new ArrayList<PromotableCandidateItemOffer>();
offers = 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))");
itemProcessor.filterItemLevelOffer(order, qualifiedOffers, offers.get(0));
// test that the valid order item offer is included
// there is a qualifier and the item qualifying criteria requires only 1, therefore there will be only one qualifier in the qualifiers map
// we don't know the targets yet, so there's only one CandidateItemOffer for now
assertTrue(qualifiedOffers.size() == 1 && qualifiedOffers.get(0).getOffer().equals(offers.get(0)) && qualifiedOffers.get(0).getCandidateQualifiersMap().size() == 1);
// Add a subtotal requirement that will be met by the item offer.
qualifiedOffers = new ArrayList<PromotableCandidateItemOffer>();
offers = 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.get(0).setQualifyingItemSubTotal(new Money(1));
itemProcessor.filterItemLevelOffer(order, qualifiedOffers, offers.get(0));
// test that the valid order item offer is included
// there is a qualifier and the item qualifying criteria requires only 1, therefore there will be only one qualifier in the qualifiers map
// we don't know the targets yet, so there's only one CandidateItemOffer for now
assertTrue(qualifiedOffers.size() == 1 && qualifiedOffers.get(0).getOffer().equals(offers.get(0)) && qualifiedOffers.get(0).getCandidateQualifiersMap().size() == 1);
// Add a subtotal requirement that will not be met by the item offer.
qualifiedOffers = new ArrayList<PromotableCandidateItemOffer>();
offers = 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.get(0).setQualifyingItemSubTotal(new Money(99999));
itemProcessor.filterItemLevelOffer(order, qualifiedOffers, offers.get(0));
// Since the item qualification subTotal is not met, the qualified offer size should
// be zero.
assertTrue(qualifiedOffers.size() == 0);
qualifiedOffers = new ArrayList<PromotableCandidateItemOffer>();
offers = dataProvider.createItemBasedOfferWithItemCriteria("order.subTotal.getAmount()>20", OfferDiscountType.PERCENT_OFF, "([MVEL.eval(\"toUpperCase()\",\"test5\"), MVEL.eval(\"toUpperCase()\",\"test6\")] contains MVEL.eval(\"toUpperCase()\", discreteOrderItem.category.name))", "([MVEL.eval(\"toUpperCase()\",\"test5\"), MVEL.eval(\"toUpperCase()\",\"test6\")] contains MVEL.eval(\"toUpperCase()\", discreteOrderItem.category.name))");
itemProcessor.filterItemLevelOffer(order, qualifiedOffers, offers.get(0));
// test that the invalid order item offer is excluded
assertTrue(qualifiedOffers.size() == 0);
verify();
}
use of org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder in project BroadleafCommerce by BroadleafCommerce.
the class OrderOfferProcessorTest method testCouldOfferApplyToOrder.
public void testCouldOfferApplyToOrder() throws Exception {
replay();
PromotableOrder order = dataProvider.createBasicPromotableOrder();
List<Offer> offers = dataProvider.createOrderBasedOffer("order.subTotal.getAmount()>20", OfferDiscountType.PERCENT_OFF);
boolean couldApply = orderProcessor.couldOfferApplyToOrder(offers.get(0), order, order.getDiscountableOrderItems().get(0), order.getFulfillmentGroups().get(0));
// test that the valid order offer is included
assertTrue(couldApply);
offers = dataProvider.createOrderBasedOffer("order.subTotal.getAmount()==0", OfferDiscountType.PERCENT_OFF);
couldApply = orderProcessor.couldOfferApplyToOrder(offers.get(0), order, order.getDiscountableOrderItems().get(0), order.getFulfillmentGroups().get(0));
// test that the invalid order offer is excluded
assertFalse(couldApply);
verify();
}
use of org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder in project BroadleafCommerce by BroadleafCommerce.
the class OrderOfferProcessorTest method testQualifyingQuantity.
public void testQualifyingQuantity() throws Exception {
replay();
PromotableOrder order = dataProvider.createBasicPromotableOrder();
List<Offer> offers = dataProvider.createOrderBasedOffer("order.subTotal.getAmount()>20", OfferDiscountType.PERCENT_OFF);
Offer firstOffer = offers.get(0);
OfferItemCriteria qualCriteria = new OfferItemCriteriaImpl();
int originalQuantityOnOrder = 5;
qualCriteria.setQuantity(originalQuantityOnOrder + 1);
qualCriteria.setMatchRule("([MVEL.eval(\"toUpperCase()\",\"test1\"), MVEL.eval(\"toUpperCase()\",\"test2\")] contains MVEL.eval(\"toUpperCase()\", discreteOrderItem.category.name))");
Set<OfferQualifyingCriteriaXref> criterias = new HashSet<OfferQualifyingCriteriaXref>();
OfferQualifyingCriteriaXref xref = new OfferQualifyingCriteriaXrefImpl();
xref.setOffer(firstOffer);
xref.setOfferItemCriteria(qualCriteria);
criterias.add(xref);
firstOffer.setQualifyingItemCriteriaXref(criterias);
List<PromotableOrderItem> orderItems = new ArrayList<PromotableOrderItem>();
for (PromotableOrderItem orderItem : order.getDiscountableOrderItems()) {
orderItems.add(orderItem);
}
CandidatePromotionItems candidates = orderProcessor.couldOfferApplyToOrderItems(offers.get(0), orderItems);
// test that the valid order offer is not included
assertTrue(!candidates.isMatchedQualifier() && candidates.getCandidateQualifiersMap().size() == 1);
int quantity = orderItems.get(0).getOrderItem().getQuantity();
orderItems.get(0).getOrderItem().setQuantity(quantity + 1);
candidates = orderProcessor.couldOfferApplyToOrderItems(offers.get(0), orderItems);
// test that the valid order offer is included
assertTrue(candidates.isMatchedQualifier() && candidates.getCandidateQualifiersMap().size() == 1);
verify();
}
use of org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrder in project BroadleafCommerce by BroadleafCommerce.
the class OrderItemTest method setUp.
@Override
protected void setUp() throws Exception {
PromotableOrder promotableOrder = new PromotableOrderImpl(new OrderImpl(), new PromotableItemFactoryImpl(), false);
DiscreteOrderItemImpl discreteOrderItem1 = new DiscreteOrderItemImpl();
discreteOrderItem1.setName("test1");
discreteOrderItem1.setOrderItemType(OrderItemType.DISCRETE);
discreteOrderItem1.setQuantity(2);
discreteOrderItem1.setRetailPrice(new Money(19.99D));
OrderItemPriceDetail pdetail = new OrderItemPriceDetailImpl();
pdetail.setOrderItem(discreteOrderItem1);
pdetail.setQuantity(2);
PromotableOrderItem orderItem1 = new PromotableOrderItemImpl(discreteOrderItem1, null, new PromotableItemFactoryImpl(), false);
priceDetail1 = new PromotableOrderItemPriceDetailImpl(orderItem1, 2);
OfferDataItemProvider dataProvider = new OfferDataItemProvider();
offer = 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))").get(0);
candidateOffer = new PromotableCandidateItemOfferImpl(promotableOrder, offer);
}
Aggregations