Search in sources :

Example 1 with OrderItem

use of org.rembx.jeeshop.order.model.OrderItem in project jeeshop by remibantos.

the class TestOrder method getInstance.

public static TestOrder getInstance() {
    if (instance != null)
        return instance;
    testUser = TestUser.getInstance();
    EntityManager entityManager = Persistence.createEntityManagerFactory(UserPersistenceUnit.NAME).createEntityManager();
    entityManager.getTransaction().begin();
    Address deliveryAddress = new Address("21 Blue street", "Chicago", "78801", "John", "Doe", "M.", null, "FRA");
    Address billingAddress = new Address("53 Green street", "Chicago", "78801", "John", "Doe", "M.", null, "FRA");
    entityManager.persist(deliveryAddress);
    entityManager.persist(billingAddress);
    order1 = new Order(testUser.firstUser(), null, deliveryAddress, billingAddress, OrderStatus.PAYMENT_VALIDATED);
    orderItem1 = new OrderItem(1L, 1L, 2);
    orderItem1.setOrder(order1);
    order1.setItems(Sets.newHashSet(orderItem1));
    entityManager.persist(order1);
    order2 = new Order(testUser.firstUser(), null, null, null, OrderStatus.CREATED);
    entityManager.persist(order2);
    entityManager.getTransaction().commit();
    instance = new TestOrder();
    entityManager.close();
    return instance;
}
Also used : Order(org.rembx.jeeshop.order.model.Order) EntityManager(javax.persistence.EntityManager) Address(org.rembx.jeeshop.user.model.Address) OrderItem(org.rembx.jeeshop.order.model.OrderItem)

Example 2 with OrderItem

use of org.rembx.jeeshop.order.model.OrderItem in project jeeshop by remibantos.

the class PriceEngineImplTest method computePrice_ShouldAddPriceOfEachOrderItemsMultipliedByQuantityWithFixedDeliveryFee.

@Test
public void computePrice_ShouldAddPriceOfEachOrderItemsMultipliedByQuantityWithFixedDeliveryFee() {
    SKU sku = new SKU();
    sku.setPrice(10.0);
    SKU sku2 = new SKU();
    sku2.setPrice(20.0);
    when(entityManager.find(SKU.class, 1L)).thenReturn(sku);
    when(entityManager.find(SKU.class, 2L)).thenReturn(sku2);
    when(orderConfiguration.getFixedDeliveryFee()).thenReturn(11.0);
    Order order = new Order();
    Set<OrderItem> items = new HashSet<>();
    items.add(new OrderItem(1L, 1L, 1));
    items.add(new OrderItem(2L, 2L, 2));
    order.setItems(items);
    orderPriceEngine.computePrice(order);
    verify(entityManager).find(SKU.class, 1L);
    verify(entityManager).find(SKU.class, 2L);
    verify(orderConfiguration).getFixedDeliveryFee();
    assertThat(order.getPrice()).isEqualTo(61.0);
}
Also used : Order(org.rembx.jeeshop.order.model.Order) OrderItem(org.rembx.jeeshop.order.model.OrderItem) SKU(org.rembx.jeeshop.catalog.model.SKU) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with OrderItem

use of org.rembx.jeeshop.order.model.OrderItem in project jeeshop by remibantos.

the class PriceEngineImplTest method computePrice_ShouldAddPriceOfEachOrderItemsMultipliedByQuantity.

@Test
public void computePrice_ShouldAddPriceOfEachOrderItemsMultipliedByQuantity() {
    SKU sku = new SKU();
    sku.setPrice(10.0);
    SKU sku2 = new SKU();
    sku2.setPrice(20.0);
    when(entityManager.find(SKU.class, 1L)).thenReturn(sku);
    when(entityManager.find(SKU.class, 2L)).thenReturn(sku2);
    when(orderConfiguration.getFixedDeliveryFee()).thenReturn(null);
    Order order = new Order();
    Set<OrderItem> items = new HashSet<>();
    items.add(new OrderItem(1L, 1L, 1));
    items.add(new OrderItem(2L, 2L, 2));
    order.setItems(items);
    orderPriceEngine.computePrice(order);
    verify(entityManager).find(SKU.class, 1L);
    verify(entityManager).find(SKU.class, 2L);
    verify(orderConfiguration).getFixedDeliveryFee();
    assertThat(order.getPrice()).isEqualTo(50.0);
    for (OrderItem item : order.getItems()) {
        if (item.getSkuId().equals(1L))
            assertThat(item.getPrice()).isEqualTo(10.0);
        else
            assertThat(item.getPrice()).isEqualTo(20.0);
    }
}
Also used : Order(org.rembx.jeeshop.order.model.Order) OrderItem(org.rembx.jeeshop.order.model.OrderItem) SKU(org.rembx.jeeshop.catalog.model.SKU) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with OrderItem

use of org.rembx.jeeshop.order.model.OrderItem in project jeeshop by remibantos.

the class PriceEngineImpl method computePrice.

@Override
public void computePrice(Order order) {
    if (CollectionUtils.isEmpty(order.getItems())) {
        throw new IllegalStateException("Order items list is empty " + order);
    }
    Double price = 0.0;
    for (OrderItem orderItem : order.getItems()) {
        SKU sku = entityManager.find(SKU.class, (orderItem).getSkuId());
        price += (sku.getPrice() * (orderItem).getQuantity());
        orderItem.setPrice(sku.getPrice());
    }
    final Double fixedDeliveryFee = orderConfiguration.getFixedDeliveryFee();
    price = applyEligibleDiscounts(order, price);
    if (fixedDeliveryFee != null) {
        price += fixedDeliveryFee;
        order.setDeliveryFee(fixedDeliveryFee);
    }
    order.setPrice(price);
}
Also used : OrderItem(org.rembx.jeeshop.order.model.OrderItem) SKU(org.rembx.jeeshop.catalog.model.SKU)

Example 5 with OrderItem

use of org.rembx.jeeshop.order.model.OrderItem in project jeeshop by remibantos.

the class OrdersCT method create_shouldThrowBadRequestWhenParametersHaveId.

@Test
public void create_shouldThrowBadRequestWhenParametersHaveId() throws Exception {
    Address address = new Address("7 Rue des arbres", "Paris", "92800", "John", "Doe", "M.", null, "USA");
    address.setId(777L);
    OrderItem orderItemWithId = new OrderItem();
    orderItemWithId.setId(777L);
    Set<OrderItem> orderItems = Collections.singleton(orderItemWithId);
    try {
        Order order = new Order(null, address, new Address("7 Rue des arbres", "Paris", "92800", "John", "Doe", "M.", null, "USA"));
        service.create(sessionContextMock, order, null);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.BAD_REQUEST);
    }
    try {
        Order order = new Order(null, new Address("7 Rue des arbres", "Paris", "92800", "John", "Doe", "M.", null, "USA"), address);
        service.create(sessionContextMock, order, null);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.BAD_REQUEST);
    }
    try {
        Order order = new Order(orderItems, new Address("7 Rue des arbres", "Paris", "92800", "John", "Doe", "M.", null, "USA"), address);
        service.create(sessionContextMock, order, null);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.BAD_REQUEST);
    }
}
Also used : Order(org.rembx.jeeshop.order.model.Order) TestOrder(org.rembx.jeeshop.order.test.TestOrder) Address(org.rembx.jeeshop.user.model.Address) WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) OrderItem(org.rembx.jeeshop.order.model.OrderItem) Test(org.junit.jupiter.api.Test)

Aggregations

OrderItem (org.rembx.jeeshop.order.model.OrderItem)6 Order (org.rembx.jeeshop.order.model.Order)5 Test (org.junit.jupiter.api.Test)4 HashSet (java.util.HashSet)3 SKU (org.rembx.jeeshop.catalog.model.SKU)3 Address (org.rembx.jeeshop.user.model.Address)3 TestOrder (org.rembx.jeeshop.order.test.TestOrder)2 EntityManager (javax.persistence.EntityManager)1 BasicUserPrincipal (org.apache.http.auth.BasicUserPrincipal)1 WebApplicationException (org.rembx.jeeshop.rest.WebApplicationException)1