1
+ package me .oldboy .integration .controllers .api_user_details_scenario ;
2
+
3
+ import com .fasterxml .jackson .core .type .TypeReference ;
4
+ import com .fasterxml .jackson .databind .ObjectMapper ;
5
+ import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
6
+ import lombok .SneakyThrows ;
7
+ import me .oldboy .config .test_data_source .TestContainerInit ;
8
+ import me .oldboy .controllers .api .BalanceController ;
9
+ import me .oldboy .dto .transaction_dto .TransactionReadDto ;
10
+ import me .oldboy .integration .annotation .IT ;
11
+ import org .junit .jupiter .api .BeforeEach ;
12
+ import org .junit .jupiter .api .Test ;
13
+ import org .springframework .beans .factory .annotation .Autowired ;
14
+ import org .springframework .security .test .context .support .WithUserDetails ;
15
+ import org .springframework .test .web .servlet .MockMvc ;
16
+ import org .springframework .test .web .servlet .MvcResult ;
17
+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
18
+ import org .springframework .web .context .WebApplicationContext ;
19
+
20
+ import java .util .List ;
21
+
22
+ import static me .oldboy .test_constant .TestConstantFields .*;
23
+ import static org .assertj .core .api .Assertions .assertThat ;
24
+ import static org .springframework .security .test .web .servlet .setup .SecurityMockMvcConfigurers .springSecurity ;
25
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
26
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .content ;
27
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
28
+
29
+ @ IT
30
+ class BalanceControllerIT extends TestContainerInit {
31
+
32
+ @ Autowired
33
+ private BalanceController balanceController ;
34
+ @ Autowired
35
+ private WebApplicationContext webApplicationContext ;
36
+ private MockMvc mockMvc ;
37
+
38
+ @ BeforeEach
39
+ void setUp (){
40
+ mockMvc = MockMvcBuilders .webAppContextSetup (webApplicationContext )
41
+ .apply (springSecurity ())
42
+ .build ();
43
+ }
44
+
45
+ @ Test
46
+ @ SneakyThrows
47
+ @ WithUserDetails (value = EXIST_EMAIL , userDetailsServiceBeanName = "clientDetailsService" )
48
+ void getBalanceDetails_ShouldReturnOk_AndDtoList_Test () {
49
+ MvcResult result = mockMvc .perform (get ("/api/myBalance" ))
50
+ .andExpect (status ().isOk ())
51
+ .andReturn ();
52
+
53
+ String strResult = result .getResponse ().getContentAsString ();
54
+ List <TransactionReadDto > listFromResponse = new ObjectMapper ()
55
+ .registerModule (new JavaTimeModule ())
56
+ .readValue (strResult , new TypeReference <List <TransactionReadDto >>() {});
57
+
58
+ assertThat (listFromResponse .size ()).isGreaterThan (1 );
59
+ }
60
+
61
+ @ Test
62
+ @ SneakyThrows
63
+ @ WithUserDetails (value = "user@test.com" , userDetailsServiceBeanName = "clientDetailsService" )
64
+ void getBalanceDetails_ShouldReturn_2xx_NoContent_ClientHasNoTransaction_Test () {
65
+ MvcResult result = mockMvc .perform (get ("/api/myBalance" ))
66
+ .andExpect (status ().is2xxSuccessful ())
67
+ .andReturn ();
68
+
69
+ String strResult = result .getResponse ().getContentAsString ();
70
+
71
+ assertThat (strResult .isEmpty ()).isTrue ();
72
+ }
73
+
74
+ @ Test
75
+ @ SneakyThrows
76
+ void getBalanceDetails_ShouldReturnNotAuth_4xx_Test () {
77
+ mockMvc .perform (get ("/api/myBalance" ))
78
+ .andExpect (status ().is4xxClientError ())
79
+ .andExpect (content ().string ("" ));
80
+ }
81
+ }
0 commit comments