-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathPaymentControllerTest.java
More file actions
76 lines (59 loc) · 2.54 KB
/
PaymentControllerTest.java
File metadata and controls
76 lines (59 loc) · 2.54 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
package com.bravo.user.controller;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.junit.jupiter.api.BeforeEach;
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.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import com.bravo.user.model.dto.PaymentDto;
import com.bravo.user.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@SpringBootTest()
@AutoConfigureMockMvc
public class PaymentControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private PaymentService paymentService;
private List<PaymentDto> payments;
@BeforeEach
public void beforeEach() {
log.info("Initializing payment list");
final List<Integer> userIds = IntStream.range(1, 10).boxed().toList();
this.payments = userIds.stream().map(id -> createPaymentDto(Integer.toString(id)))
.collect(Collectors.toList());
}
@Test
void getRetrieveByUserId() throws Exception {
log.info("ENTRY::: PaymentControllerTest.getRetrieveByUserId");
final String userId = "00963d9b-f884-485e-9455-fcf30c6ac379";
when(paymentService.retrieveByUserId(anyString())).thenReturn(this.payments);
final ResultActions result = this.mockMvc.perform(get("/payment/retrieve/".concat(userId)))
.andExpect(status().isOk());
for (int i = 0; i < payments.size(); i++) {
result.andExpect(jsonPath(String.format("$[%d].id", i)).value(payments.get(i).getId()));
}
log.info("Assignment: By Verify the retrieveByUserId ");
verify(paymentService).retrieveByUserId(userId);
}
private PaymentDto createPaymentDto(String userId) {
final PaymentDto payment = new PaymentDto();
payment.setId(userId);
payment.setCardNumberLast4("1111");
payment.setExpiryMonth(00);
payment.setExpiryYear(11);
return payment;
}
}