Search in sources :

Example 36 with OrderItem

use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.

the class OrderTest method updateItemsInOrder.

@Test(groups = { "updateItemsInOrder" }, dependsOnGroups = { "getItemsForOrder" })
@Transactional
public void updateItemsInOrder() throws UpdateCartException, RemoveFromCartException {
    // Grab the order and the first OrderItem
    Order order = orderService.findOrderById(orderId);
    List<OrderItem> orderItems = order.getOrderItems();
    assert orderItems.size() > 0;
    OrderItem item = orderItems.get(0);
    // Set the quantity of the first OrderItem to 10
    OrderItemRequestDTO orderItemRequestDTO = new OrderItemRequestDTO();
    orderItemRequestDTO.setOrderItemId(item.getId());
    orderItemRequestDTO.setQuantity(10);
    order = orderService.updateItemQuantity(order.getId(), orderItemRequestDTO, true);
    // Assert that the quantity has changed
    OrderItem updatedItem = orderItemService.readOrderItemById(item.getId());
    assert updatedItem != null;
    assert updatedItem.getQuantity() == 10;
    // Assert that the appropriate fulfillment group item has changed
    assert order.getFulfillmentGroups().size() == 1;
    FulfillmentGroup fg = order.getFulfillmentGroups().get(0);
    assert fg.getFulfillmentGroupItems().size() == 1;
    boolean fgItemUpdated = false;
    for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
        if (fgi.getOrderItem().equals(updatedItem)) {
            assert fgi.getQuantity() == 10;
            fgItemUpdated = true;
        }
    }
    assert fgItemUpdated;
    // Set the quantity of the first OrderItem to 5
    orderItemRequestDTO = new OrderItemRequestDTO();
    orderItemRequestDTO.setOrderItemId(item.getId());
    orderItemRequestDTO.setQuantity(5);
    order = orderService.updateItemQuantity(order.getId(), orderItemRequestDTO, true);
    // Assert that the quantity has changed - going to a smaller quantity is also ok
    updatedItem = orderItemService.readOrderItemById(item.getId());
    assert updatedItem != null;
    assert updatedItem.getQuantity() == 5;
    // Assert that the appropriate fulfillment group item has changed
    assert order.getFulfillmentGroups().size() == 1;
    fg = order.getFulfillmentGroups().get(0);
    assert fg.getFulfillmentGroupItems().size() == 1;
    fgItemUpdated = false;
    for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
        if (fgi.getOrderItem().equals(updatedItem)) {
            assert fgi.getQuantity() == 5;
            fgItemUpdated = true;
        }
    }
    assert fgItemUpdated;
    // Setting the quantity to 0 should in fact remove the item completely
    int startingSize = order.getOrderItems().size();
    orderItemRequestDTO = new OrderItemRequestDTO();
    orderItemRequestDTO.setOrderItemId(item.getId());
    orderItemRequestDTO.setQuantity(0);
    order = orderService.updateItemQuantity(order.getId(), orderItemRequestDTO, true);
    // Assert that the item has been removed
    updatedItem = orderItemService.readOrderItemById(item.getId());
    assert updatedItem == null;
    assert order.getOrderItems().size() == startingSize - 1;
    // Assert that the appropriate fulfillment group item has been removed
    assert order.getFulfillmentGroups().size() == 0;
/*
        TODO Since we commented out some tests above, there is no longer an additional item
        in the cart, hence the fulfillment group is removed

        fg = order.getFulfillmentGroups().get(0);
        assert fg.getFulfillmentGroupItems().size() == startingSize - 1;
        boolean fgItemRemoved = true;
        for (FulfillmentGroupItem fgi : fg.getFulfillmentGroupItems()) {
            if (fgi.getOrderItem().equals(updatedItem)) {
                fgItemRemoved = false;
            }
        }
        assert fgItemRemoved;*/
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) FulfillmentGroupItem(org.broadleafcommerce.core.order.domain.FulfillmentGroupItem) FulfillmentGroup(org.broadleafcommerce.core.order.domain.FulfillmentGroup) Test(org.testng.annotations.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 37 with OrderItem

use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.

the class OrderTest method testIllegalUpdateScenarios.

@Test(groups = { "testIllegalUpdateScenarios" }, dependsOnGroups = { "addItemToOrder" })
@Transactional
public void testIllegalUpdateScenarios() throws UpdateCartException, AddToCartException, RemoveFromCartException {
    Order order = orderService.findOrderById(orderId);
    assert order != null;
    Product activeProduct = addTestProduct("mug", "cups", true);
    Product inactiveProduct = addTestProduct("cup", "cups", false);
    // Inactive skus should not be added
    OrderItemRequestDTO itemRequest = new OrderItemRequestDTO().setQuantity(1).setSkuId(activeProduct.getDefaultSku().getId());
    boolean addSuccessful = true;
    try {
        order = orderService.addItem(orderId, itemRequest, true);
    } catch (AddToCartException e) {
        addSuccessful = false;
    }
    assert addSuccessful;
    // should not be able to update to negative quantity
    OrderItem item = orderService.findLastMatchingItem(order, activeProduct.getDefaultSku().getId(), activeProduct.getId());
    itemRequest = new OrderItemRequestDTO().setQuantity(-3).setOrderItemId(item.getId());
    boolean updateSuccessful = true;
    try {
        orderService.updateItemQuantity(orderId, itemRequest, true);
    } catch (UpdateCartException e) {
        updateSuccessful = false;
    }
    assert !updateSuccessful;
    // shouldn't be able to update the quantity of a DOI inside of a bundle
    ProductBundle bundle = addProductBundle();
    itemRequest = new OrderItemRequestDTO().setQuantity(1).setProductId(bundle.getId()).setSkuId(bundle.getDefaultSku().getId());
    addSuccessful = true;
    try {
        order = orderService.addItem(orderId, itemRequest, true);
    } catch (AddToCartException e) {
        addSuccessful = false;
    }
    assert addSuccessful;
    BundleOrderItem bundleItem = (BundleOrderItem) orderService.findLastMatchingItem(order, bundle.getDefaultSku().getId(), bundle.getId());
    // should just be a single DOI inside the bundle
    DiscreteOrderItem doi = bundleItem.getDiscreteOrderItems().get(0);
    itemRequest = new OrderItemRequestDTO().setQuantity(4).setOrderItemId(doi.getId());
    try {
        orderService.updateItemQuantity(orderId, itemRequest, true);
    } catch (UpdateCartException e) {
        updateSuccessful = false;
    }
    assert !updateSuccessful;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItemRequestDTO(org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) AddToCartException(org.broadleafcommerce.core.order.service.exception.AddToCartException) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) ProductBundle(org.broadleafcommerce.core.catalog.domain.ProductBundle) Product(org.broadleafcommerce.core.catalog.domain.Product) UpdateCartException(org.broadleafcommerce.core.order.service.exception.UpdateCartException) Test(org.testng.annotations.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 38 with OrderItem

use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.

the class OrderTest method removeItemFromOrder.

@Test(groups = { "removeItemFromOrder" }, dependsOnGroups = { "getItemsForOrder" })
@Transactional
public void removeItemFromOrder() throws RemoveFromCartException {
    // Grab the order and the first OrderItem
    Order order = orderService.findOrderById(orderId);
    List<OrderItem> orderItems = order.getOrderItems();
    assert orderItems.size() > 0;
    int startingSize = orderItems.size();
    OrderItem item = orderItems.get(0);
    Long itemId = item.getId();
    assert item != null;
    // Remove the item
    order = orderService.removeItem(order.getId(), item.getId(), true);
    List<OrderItem> items = order.getOrderItems();
    OrderItem updatedItem = orderItemService.readOrderItemById(item.getId());
    // Assert that the item has been removed
    assert items != null;
    assert items.size() == startingSize - 1;
    assert updatedItem == null;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) Test(org.testng.annotations.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 39 with OrderItem

use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.

the class OrderTest method removeBundleFromOrder.

@Test(groups = { "removeBundleFromOrder" }, dependsOnGroups = { "addBundleToOrder" })
@Rollback(false)
@Transactional
public void removeBundleFromOrder() throws RemoveFromCartException {
    Order order = orderService.findOrderById(orderId);
    List<OrderItem> orderItems = order.getOrderItems();
    assert orderItems.size() == numOrderItems;
    int startingSize = orderItems.size();
    BundleOrderItem bundleOrderItem = (BundleOrderItem) orderItemService.readOrderItemById(bundleOrderItemId);
    assert bundleOrderItem != null;
    assert bundleOrderItem.getDiscreteOrderItems() != null;
    assert bundleOrderItem.getDiscreteOrderItems().size() == 1;
    order = orderService.removeItem(order.getId(), bundleOrderItem.getId(), true);
    List<OrderItem> items = order.getOrderItems();
    assert items != null;
    assert items.size() == startingSize - 1;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) Test(org.testng.annotations.Test) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Example 40 with OrderItem

use of org.broadleafcommerce.core.order.domain.OrderItem in project BroadleafCommerce by BroadleafCommerce.

the class OrderTest method getItemsForOrder.

@Test(groups = { "getItemsForOrder" }, dependsOnGroups = { "removeBundleFromOrder" })
@Transactional
public void getItemsForOrder() {
    Order order = orderService.findOrderById(orderId);
    List<OrderItem> orderItems = order.getOrderItems();
    assert orderItems != null;
    assert orderItems.size() == numOrderItems - 1;
}
Also used : Order(org.broadleafcommerce.core.order.domain.Order) DiscreteOrderItem(org.broadleafcommerce.core.order.domain.DiscreteOrderItem) OrderItem(org.broadleafcommerce.core.order.domain.OrderItem) BundleOrderItem(org.broadleafcommerce.core.order.domain.BundleOrderItem) Test(org.testng.annotations.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

OrderItem (org.broadleafcommerce.core.order.domain.OrderItem)100 DiscreteOrderItem (org.broadleafcommerce.core.order.domain.DiscreteOrderItem)69 BundleOrderItem (org.broadleafcommerce.core.order.domain.BundleOrderItem)54 Order (org.broadleafcommerce.core.order.domain.Order)46 ArrayList (java.util.ArrayList)32 GiftWrapOrderItem (org.broadleafcommerce.core.order.domain.GiftWrapOrderItem)23 FulfillmentGroupItem (org.broadleafcommerce.core.order.domain.FulfillmentGroupItem)20 FulfillmentGroup (org.broadleafcommerce.core.order.domain.FulfillmentGroup)19 Transactional (org.springframework.transaction.annotation.Transactional)19 Test (org.testng.annotations.Test)16 Money (org.broadleafcommerce.common.money.Money)13 OrderItemRequestDTO (org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO)13 HashMap (java.util.HashMap)11 Sku (org.broadleafcommerce.core.catalog.domain.Sku)10 CartOperationRequest (org.broadleafcommerce.core.order.service.workflow.CartOperationRequest)9 PromotableOrderItem (org.broadleafcommerce.core.offer.service.discount.domain.PromotableOrderItem)7 BigDecimal (java.math.BigDecimal)5 ItemNotFoundException (org.broadleafcommerce.core.order.service.exception.ItemNotFoundException)5 List (java.util.List)4 Product (org.broadleafcommerce.core.catalog.domain.Product)4