Search in sources :

Example 1 with Address

use of org.rembx.jeeshop.user.model.Address 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 Address

use of org.rembx.jeeshop.user.model.Address in project jeeshop by remibantos.

the class OrdersCT method create_shouldPersistOrderAndItsAddressesInCascade_SetCurrentUserToOrderForUserRole_AndProcessPayment.

@Test
public void create_shouldPersistOrderAndItsAddressesInCascade_SetCurrentUserToOrderForUserRole_AndProcessPayment() throws Exception {
    Address deliveryAddress = new Address("7 Rue des arbres", "Paris", "92800", "John", "Doe", "M.", null, "USA");
    Address billingAddress = new Address("8 Rue Toto", "Paris", "75001", "John", "Doe", "M.", null, "FRA");
    when(sessionContextMock.isUserInRole(JeeshopRoles.USER)).thenReturn(true);
    when(sessionContextMock.getUserPrincipal()).thenReturn(new BasicUserPrincipal(testOrder.firstOrdersUser().getLogin()));
    entityManager.getTransaction().begin();
    Order order = new Order(new HashSet<>(), new Address("7 Rue des arbres", "Paris", "92800", "John", "Doe", "M.", null, "USA"), new Address("8 Rue Toto", "Paris", "75001", "John", "Doe", "M.", null, "FRA"));
    order.setOrderDiscounts(new HashSet<>());
    service.create(sessionContextMock, order, null);
    entityManager.getTransaction().commit();
    verify(sessionContextMock).isUserInRole(JeeshopRoles.USER);
    // verify(mailerMock).sendMail(testMailTemplate.orderConfirmationMailTemplate().getSubject(), testOrder.firstOrdersUser().getLogin(), "<html><body>Hello M. John Doe. Your order has been registered...</body></html>");
    final Order persistedOrder = entityManager.find(Order.class, order.getId());
    assertThat(persistedOrder).isNotNull();
    assertThat(persistedOrder.getStatus()).isEqualTo(PAYMENT_VALIDATED);
    assertThat(persistedOrder.getUser()).isEqualTo(testOrder.firstOrdersUser());
    deliveryAddress.setId(persistedOrder.getDeliveryAddress().getId());
    billingAddress.setId(persistedOrder.getBillingAddress().getId());
    assertThat(persistedOrder.getBillingAddress()).isEqualTo(billingAddress);
    assertThat(persistedOrder.getDeliveryAddress()).isEqualTo(deliveryAddress);
    entityManager.getTransaction().begin();
    entityManager.remove(order);
    entityManager.getTransaction().commit();
}
Also used : Order(org.rembx.jeeshop.order.model.Order) TestOrder(org.rembx.jeeshop.order.test.TestOrder) Address(org.rembx.jeeshop.user.model.Address) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) Test(org.junit.jupiter.api.Test)

Example 3 with Address

use of org.rembx.jeeshop.user.model.Address in project jeeshop by remibantos.

the class OrdersCT method create_shouldSetGivenUserByLoginInOrder_ForADMINRole.

@Test
public void create_shouldSetGivenUserByLoginInOrder_ForADMINRole() throws Exception {
    when(sessionContextMock.isUserInRole(JeeshopRoles.USER)).thenReturn(false);
    when(sessionContextMock.isUserInRole(JeeshopRoles.ADMIN)).thenReturn(true);
    entityManager.getTransaction().begin();
    Order order = new Order(new HashSet<>(), new Address("7 Rue des arbres", "Paris", "92800", "John", "Doe", "M.", null, "USA"), new Address("7 Rue des arbres", "Paris", "92800", "John", "Doe", "M.", null, "USA"));
    order.setOrderDiscounts(new HashSet<>());
    service.create(sessionContextMock, order, "test@test.com");
    entityManager.getTransaction().commit();
    verify(sessionContextMock).isUserInRole(JeeshopRoles.USER);
    verify(sessionContextMock).isUserInRole(JeeshopRoles.ADMIN);
    final Order persistedOrder = entityManager.find(Order.class, order.getId());
    assertThat(persistedOrder).isNotNull();
    assertThat(persistedOrder.getStatus()).isEqualTo(PAYMENT_VALIDATED);
    assertThat(persistedOrder.getUser()).isEqualTo(testOrder.firstOrdersUser());
    entityManager.getTransaction().begin();
    entityManager.remove(order);
    entityManager.getTransaction().commit();
}
Also used : Order(org.rembx.jeeshop.order.model.Order) TestOrder(org.rembx.jeeshop.order.test.TestOrder) Address(org.rembx.jeeshop.user.model.Address) Test(org.junit.jupiter.api.Test)

Example 4 with Address

use of org.rembx.jeeshop.user.model.Address in project jeeshop by remibantos.

the class UsersCT method create_shouldJustPersistEndUserEvenWhenExceptionDuringMailSending.

@Test
public void create_shouldJustPersistEndUserEvenWhenExceptionDuringMailSending() throws Exception {
    User user = new User("register1@test.com", "test", "John", "Doe", "+33616161616", null, new Date(), "fr_FR", null);
    user.setGender("M.");
    Address address = new Address("7 blue street", "Nowhere", "00001", "John", "Doe", "M.", null, "FRA");
    user.setAddress(address);
    when(sessionContextMock.isUserInRole(JeeshopRoles.ADMIN)).thenReturn(false);
    entityManager.getTransaction().begin();
    service.create(sessionContextMock, user);
    entityManager.getTransaction().commit();
    doThrow(new IllegalStateException("Test Exception")).when(mailerMock).sendMail(testMailTemplate.userRegistrationMailTemplate().getSubject(), user.getLogin(), testMailTemplate.userRegistrationMailTemplate().getContent());
    verify(sessionContextMock).isUserInRole(JeeshopRoles.ADMIN);
    verify(mailerMock).sendMail(testMailTemplate.userRegistrationMailTemplate().getSubject(), user.getLogin(), "<html><body>Welcome M. John Doe</body></html>");
    final User persistedUser = entityManager.find(User.class, user.getId());
    assertThat(persistedUser).isNotNull();
    assertThat(persistedUser.getAddress()).isEqualTo(address);
    entityManager.remove(user);
}
Also used : TestUser(org.rembx.jeeshop.user.test.TestUser) User(org.rembx.jeeshop.user.model.User) Address(org.rembx.jeeshop.user.model.Address) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 5 with Address

use of org.rembx.jeeshop.user.model.Address 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

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