Skip to content

Commit

Permalink
Add order service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robertdijk committed Oct 27, 2024
1 parent d3cdd03 commit d9ace89
Showing 1 changed file with 58 additions and 16 deletions.
74 changes: 58 additions & 16 deletions src/test/java/ch/wisv/events/core/service/OrderServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
import org.junit.Before;
import org.junit.Test;
import static org.mockito.ArgumentMatchers.any;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.mock.mockito.MockBean;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
Expand All @@ -62,30 +64,35 @@
public class OrderServiceImplTest extends ServiceTest {

/** OrderRepository. */
@Mock
@MockBean
private OrderRepository orderRepository;

/** OrderProductRepository. */
@Mock
@MockBean
private OrderProductRepository orderProductRepository;

/** OrderValidationService. */
@Mock
@MockBean
private OrderValidationService orderValidationService;

/** ProductService. */
@Mock
@MockBean
private ProductService productService;

/** MailService. */
@Mock
@MockBean
private MailService mailService;

/** TicketService. */
@Mock
@MockBean
private TicketService ticketService;

/** Administration Costs*/
@Value("${administrationCosts}")
private double administrationCosts;

/** OrderService. */
@Autowired
private OrderService orderService;

/** Order. */
Expand All @@ -99,21 +106,14 @@ public class OrderServiceImplTest extends ServiceTest {
*/
@Before
public void setUp() {
orderService = new OrderServiceImpl(
orderRepository,
orderProductRepository,
orderValidationService,
productService,
mailService,
ticketService
);
product = mock(Product.class);
when(product.getVatRate()).thenReturn(VatRate.VAT_HIGH);

order = new Order();
order.setOwner(mock(Customer.class));
order.setCreatedBy("events-online");
order.setAmount(1.d);
order.setAdministrationCosts(administrationCosts);
order.setAmount(1.d + administrationCosts);
order.setPaymentMethod(PaymentMethod.CASH);
order.setStatus(OrderStatus.PAID);
order.updateOrderAmount();
Expand Down Expand Up @@ -272,6 +272,48 @@ public void testCreateByOrderProductDto() throws Exception {
assertEquals(order.getOrderProducts(), ImmutableList.of(new OrderProduct(mockProduct, 1.d, 1L)));
}

/**
* Administration costs
*/
@Test
public void testCreateByOrderProductDtoAdministrationCosts() throws Exception {
HashMap<String, Long> products = new HashMap<>();
products.put("123-345-456", 1L);
OrderProductDto orderProductDto = new OrderProductDto();
orderProductDto.setProducts(products);

Product mockProduct = mock(Product.class);
when(mockProduct.getCost()).thenReturn(1.d);
when(mockProduct.getVatRate()).thenReturn(VatRate.VAT_HIGH);
when(productService.getByKey("123-345-456")).thenReturn(mockProduct);

Order order = orderService.createOrderByOrderProductDto(orderProductDto);

assertEquals((Double) administrationCosts, order.getAdministrationCosts());
assertEquals((Double) (1d + administrationCosts), order.getAmount());
}

/**
* No administration costs
*/
@Test
public void testCreateByOrderProductDtoNoAdministrationCosts() throws Exception {
HashMap<String, Long> products = new HashMap<>();
products.put("123-345-456", 1L);
OrderProductDto orderProductDto = new OrderProductDto();
orderProductDto.setProducts(products);

Product mockProduct = mock(Product.class);
when(mockProduct.getCost()).thenReturn(0d);
when(mockProduct.getVatRate()).thenReturn(VatRate.VAT_HIGH);
when(productService.getByKey("123-345-456")).thenReturn(mockProduct);

Order order = orderService.createOrderByOrderProductDto(orderProductDto);

assertEquals((Double) 0d, order.getAdministrationCosts());
assertEquals((Double) 0d, order.getAmount());
}

/**
* Update test.
*/
Expand Down

0 comments on commit d9ace89

Please sign in to comment.