-
Notifications
You must be signed in to change notification settings - Fork 32
/
CashierTest.java
97 lines (83 loc) · 3.62 KB
/
CashierTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package fr.univcotedazur.simpletcfs.components;
import fr.univcotedazur.simpletcfs.entities.*;
import fr.univcotedazur.simpletcfs.exceptions.PaymentException;
import fr.univcotedazur.simpletcfs.interfaces.Bank;
import fr.univcotedazur.simpletcfs.interfaces.Payment;
import fr.univcotedazur.simpletcfs.repositories.CustomerRepository;
import fr.univcotedazur.simpletcfs.repositories.OrderRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.annotation.Commit;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.anyDouble;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
@SpringBootTest
@Transactional // default behavior : rollback DB operations after each test (even if it fails)
@Commit // test-specific annotation to change default behaviour to Commit on all tests (could be applied on a method
// This annotation obliges us to clean the DB (removing the 2 customers) but it is only here for illustration
// The "rollback" policy should be privileged unless some specific testing context appears
class CashierTest {
@Autowired
private CustomerRepository customerRepository;
@Autowired
private OrderRepository orderRepository;
@Autowired
private Payment cashier;
@MockBean
private Bank bankMock;
// Test context
private Set<Item> items;
private Customer john;
private Customer pat;
@BeforeEach
void setUpContext() {
items = new HashSet<>();
items.add(new Item(Cookies.CHOCOLALALA, 3));
items.add(new Item(Cookies.DARK_TEMPTATION, 2));
// Customers
john = new Customer("john", "1234896983"); // ends with the secret YES Card number
john.setCart(items);
customerRepository.save(john);
pat = new Customer("pat", "1234567890"); // should be rejected by the payment service
pat.setCart(items);
customerRepository.save(pat);
// Mocking the bank proxy
when(bankMock.pay(eq(john), anyDouble())).thenReturn(Optional.of("playReceiptOKId"));
when(bankMock.pay(eq(pat), anyDouble())).thenReturn(Optional.empty());
}
@AfterEach
void cleanUpContext() {
orderRepository.deleteAll();
customerRepository.deleteAll();
}
@Test
void processToPayment() throws Exception {
double price = (3 * Cookies.CHOCOLALALA.getPrice()) + (2 * Cookies.DARK_TEMPTATION.getPrice());
// paying order
Order order = cashier.payOrderFromCart(john, price);
assertNotNull(order);
assertEquals(john, order.getCustomer());
assertEquals(items, order.getItems());
assertEquals(price, order.getPrice(), 0.0);
assertEquals(2,order.getItems().size());
assertEquals(OrderStatus.IN_PROGRESS, order.getStatus());
Set<Order> johnOrders = john.getOrders();
assertEquals(1, johnOrders.size());
assertEquals(order, johnOrders.iterator().next());
}
@Test
void identifyPaymentError() {
Assertions.assertThrows( PaymentException.class, () -> cashier.payOrderFromCart(pat, 44.2));
}
}