-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionControllerTestIT.java
More file actions
309 lines (271 loc) · 13.1 KB
/
Copy pathTransactionControllerTestIT.java
File metadata and controls
309 lines (271 loc) · 13.1 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package app.adapters.input;
import app.domain.dto.CreateNewAuthor;
import app.domain.dto.CreateNewBook;
import app.domain.dto.CreateNewCustomer;
import app.domain.dto.CreateNewTransaktion;
import app.adapters.output.entity.UserEntity;
import app.adapters.output.repositories.AuthorRepository;
import app.adapters.output.repositories.BookRepository;
import app.adapters.output.repositories.CustomerRepository;
import app.adapters.output.repositories.TransactionRepository;
import app.adapters.output.repositories.UserRepository;
import app.domain.model.Book;
import app.domain.model.Customer;
import app.domain.model.Transaction;
import app.domain.port.input.BookUseCase;
import app.domain.port.input.CustomerUseCase;
import app.domain.port.input.TransactionUseCase;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
@WithMockUser(username = "user")
@Tag("integration")
class TransactionControllerTestIT {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private TransactionRepository transactionRepository;
@Autowired
private BookRepository bookRepository;
@Autowired
private CustomerRepository customerRepository;
@Autowired
private AuthorRepository authorRepository;
@Autowired
private TransactionUseCase transactionUseCase;
@Autowired
private BookUseCase bookUseCase;
@Autowired
private CustomerUseCase customerUseCase;
@Autowired
private UserRepository userRepository;
@Autowired
private PasswordEncoder passwordEncoder;
private Customer customer;
private Book book;
@BeforeEach
public void setUp() {
customer = customerUseCase.createNewCustomer(
new CreateNewCustomer("Test Customer Transaction", "test_transaction@example.com", true));
book = bookUseCase.createNewBook(new CreateNewBook("Test Book Transaction", "1234567892",
2021, List.of(
new CreateNewAuthor("Test Author Transaction", "test"))));
// "member" is the account that holds the membership above, so returning and extending can
// be exercised as the borrower. The class-level "user" deliberately holds none.
userRepository.save(new UserEntity(
"member", passwordEncoder.encode("secret"), "USER", customer.getCustomerId()));
}
@Test
void testCreateNewTransaction() throws Exception {
LocalDate today = LocalDate.now();
LocalDate futureDueDate = today.plusDays(1);
UUID customerId = customer.getCustomerId();
UUID bookId = book.getBookId();
CreateNewTransaktion newTransaktion = new CreateNewTransaktion(
today,
futureDueDate,
customerId,
bookId
);
mockMvc.perform(post("/transactions")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(newTransaktion)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.transactionId").exists())
.andExpect(jsonPath("$.borrowDate").value(today.toString()))
.andExpect(jsonPath("$.dueDate").value(futureDueDate.toString()))
.andExpect(jsonPath("$.customer.customerId").value(newTransaktion.getCustomerId().toString()))
.andExpect(jsonPath("$.book.bookId").value(newTransaktion.getBookId().toString()));
}
@Test
void testCreateNewTransaction_BadRequest() throws Exception {
CreateNewTransaktion invalidTransaction = new CreateNewTransaktion(
LocalDate.now(),
LocalDate.now().plusDays(10),
UUID.randomUUID(),
null
);
mockMvc.perform(post("/transactions")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(invalidTransaction)))
.andExpect(status().isBadRequest());
}
@Test
@WithMockUser(username = "member")
void testBorrowBook() throws Exception {
UUID customerId = customer.getCustomerId();
UUID bookId = book.getBookId();
mockMvc.perform(post("/transactions/borrowBook/{customerId}/{bookId}", customerId, bookId))
.andExpect(status().isOk())
.andExpect(content().string("Book borrowed successfully."));
long transactionCount = transactionRepository.countByCustomerCustomerId(customerId);
assertEquals(1, transactionCount);
}
@Test
@WithMockUser(username = "member")
void testBorrowBook_bookNotAvailable() throws Exception {
UUID customerId = customer.getCustomerId();
UUID bookId = book.getBookId();
transactionUseCase.borrowBook(customerId, bookId);
mockMvc.perform(post("/transactions/borrowBook/{customerId}/{bookId}", customerId, bookId))
.andExpect(status().isBadRequest())
.andExpect(content().string("Failed to borrow book: Book is not available for borrowing."));
}
@Test
@WithMockUser(username = "member")
void testReturnBook() throws Exception {
UUID customerId = customer.getCustomerId();
UUID bookId = book.getBookId();
transactionUseCase.borrowBook(customerId, bookId);
mockMvc.perform(post("/transactions/returnBook/{bookId}", bookId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.message").value("Transaction successful."));
}
/**
* Knowing a book id is not enough to close somebody else's loan. "user" holds no membership,
* so it is not the borrower and must be refused.
*/
@Test
void testReturnBook_refusedForSomeoneElsesLoan() throws Exception {
transactionUseCase.borrowBook(customer.getCustomerId(), book.getBookId());
mockMvc.perform(post("/transactions/returnBook/{bookId}", book.getBookId()))
.andExpect(status().isForbidden())
.andExpect(jsonPath("$.message").value("You can only return books you have out."));
}
/** Staff take returns at the desk, so an administrator may close any loan. */
@Test
@WithMockUser(username = "desk", roles = "ADMIN")
void testReturnBook_allowedForAdministrators() throws Exception {
transactionUseCase.borrowBook(customer.getCustomerId(), book.getBookId());
mockMvc.perform(post("/transactions/returnBook/{bookId}", book.getBookId()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.message").value("Transaction successful."));
}
@Test
@WithMockUser(username = "member")
void testReturnBook_noTransactionFound() throws Exception {
UUID bookId = book.getBookId();
mockMvc.perform(post("/transactions/returnBook/{bookId}", bookId))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.message").value("Failed to return book: This book has no open loan to return."));
}
@Test
void testReturnBook_bookNotFound() throws Exception {
UUID bookId = UUID.randomUUID();
mockMvc.perform(post("/transactions/returnBook/{bookId}", bookId))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.message").value("Book not found."));
}
@Test
void testGetTransactionID() throws Exception {
UUID customerId = customer.getCustomerId();
UUID bookId = book.getBookId();
Transaction transaction = transactionUseCase.borrowBook(customerId, bookId);
assertNotNull(transaction);
assertNotNull(transaction.getTransactionId());
UUID transactionId = transaction.getTransactionId();
mockMvc.perform(get("/transactions/{id}", transactionId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.transactionId").value(transactionId.toString()))
.andExpect(jsonPath("$.data.borrowDate").exists())
.andExpect(jsonPath("$.data.dueDate").exists())
.andExpect(jsonPath("$.data.customer.customerId").value(customerId.toString()))
.andExpect(jsonPath("$.data.book.bookId").value(bookId.toString()));
}
@Test
void testGetTransactionById_NotFound() throws Exception {
UUID invalidTransactionId = UUID.randomUUID();
mockMvc.perform(get("/transactions/{id}", invalidTransactionId)
.accept("application/single-transaction-response+json;version=1"))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.message").value("Transaction not found"))
.andExpect(jsonPath("$.transactionId").value(invalidTransactionId.toString()));
}
@Test
void testGetTransactionById_InvalidUUID() throws Exception {
String invalidUuid = "123-invalid-uuid";
mockMvc.perform(get("/transactions/{id}", invalidUuid)
.accept("application/single-transaction-response+json;version=1"))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.message").value("Invalid UUID format"))
.andExpect(jsonPath("$.providedId").value(invalidUuid));
}
@Test
@WithMockUser(username = "admin", roles = "ADMIN")
void testViewBorrowingHistory() throws Exception {
UUID customerId = customer.getCustomerId();
UUID bookId = book.getBookId();
transactionUseCase.borrowBook(customerId, bookId);
mockMvc.perform(get("/transactions/history/{customerId}", customerId))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data[0].transactionId").exists())
.andExpect(jsonPath("$.data[0].borrowDate").exists())
.andExpect(jsonPath("$.data[0].dueDate").exists())
.andExpect(jsonPath("$.data[0].customerId").value(customerId.toString()))
.andExpect(jsonPath("$.data[0].bookId").value(bookId.toString()))
.andExpect(jsonPath("$.data[0].book.bookId").value(bookId.toString()));
}
@Test
@WithMockUser(username = "admin", roles = "ADMIN")
void testViewBorrowingHistory_noTransactionsFound() throws Exception {
UUID customerId = customer.getCustomerId();
mockMvc.perform(get("/transactions/history/{customerId}", customerId))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.message").value("No transactions found for this customer"));
}
/** A member must not be able to read another member's loans by guessing a customer id. */
@Test
void testViewBorrowingHistory_refusedForMembers() throws Exception {
mockMvc.perform(get("/transactions/history/{customerId}", customer.getCustomerId()))
.andExpect(status().isForbidden());
}
/** The seeded "user" account holds no membership, so its own history is simply empty. */
@Test
void testMyHistory_withoutMembership() throws Exception {
mockMvc.perform(get("/transactions/me"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data").isArray())
.andExpect(jsonPath("$.totalItems").value(0));
}
@AfterEach
public void tearDown() {
transactionRepository.deleteAll();
userRepository.deleteAll();
customerRepository.deleteAll();
bookRepository.deleteAll();
authorRepository.deleteAll();
}
/** A member borrowing against somebody else's membership would spend their loan limit. */
@Test
@WithMockUser(username = "member", roles = "USER")
void borrowingAgainstAnotherMembershipIsRefused() throws Exception {
Customer otherMember = customerUseCase.createNewCustomer(
new CreateNewCustomer("Someone Else", "someone.else@example.com", true));
mockMvc.perform(post("/transactions/borrowBook/" + otherMember.getCustomerId() + "/"
+ book.getBookId()))
.andExpect(status().isForbidden());
}
}