1+ package org .c4marathon .assignment .settlement .presentation ;
2+
3+ import static org .c4marathon .assignment .settlement .domain .SettlementType .*;
4+ import static org .mockito .Mockito .*;
5+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
6+ import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .*;
7+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .*;
8+
9+ import java .util .List ;
10+
11+ import org .c4marathon .assignment .ControllerTestSupport ;
12+ import org .c4marathon .assignment .global .session .SessionConst ;
13+ import org .c4marathon .assignment .global .session .SessionMemberInfo ;
14+ import org .c4marathon .assignment .settlement .dto .ReceivedSettlementResponse ;
15+ import org .c4marathon .assignment .settlement .dto .SettlementDetailInfo ;
16+ import org .c4marathon .assignment .settlement .dto .SettlementRequest ;
17+ import org .c4marathon .assignment .settlement .dto .SettlementResponse ;
18+ import org .junit .jupiter .api .BeforeEach ;
19+ import org .junit .jupiter .api .DisplayName ;
20+ import org .junit .jupiter .api .Test ;
21+ import org .springframework .http .MediaType ;
22+ import org .springframework .mock .web .MockHttpSession ;
23+
24+
25+ class SettlementControllerTest extends ControllerTestSupport {
26+
27+ @ BeforeEach
28+ void initSession () {
29+ session = new MockHttpSession ();
30+ SessionMemberInfo memberInfo =
new SessionMemberInfo (
1L ,
"[email protected] " ,
1L );
31+ session .setAttribute (SessionConst .LOGIN_MEMBER , memberInfo );
32+ }
33+
34+ @ Test
35+ @ DisplayName ("정산 요청 API가 정상적으로 호출된다" )
36+ void settle () throws Exception {
37+ // given
38+ SettlementRequest request = new SettlementRequest (3 , 30000 , List .of (2L , 3L , 4L ), EQUAL );
39+
40+ // when & then
41+ mockMvc .perform (post ("/api/settle" )
42+ .contentType (MediaType .APPLICATION_JSON )
43+ .content (objectMapper .writeValueAsString (request ))
44+ .session (session )
45+ )
46+ .andExpect (status ().isOk ());
47+ }
48+
49+ @ DisplayName ("내가 요청한 정산 리스트를 정상적으로 조회한다" )
50+ @ Test
51+ void getRequestedSettlements () throws Exception {
52+ // given
53+ List <SettlementResponse > responses = List .of (
54+ new SettlementResponse (1L , 1L , 30000 , List .of (
55+ new SettlementDetailInfo (10L , 2L , 10000 ),
56+ new SettlementDetailInfo (11L , 3L , 10000 ),
57+ new SettlementDetailInfo (12L , 4L , 10000 )
58+ ))
59+ );
60+
61+ when (settlementService .getRequestedSettlements (1L )).thenReturn (responses );
62+
63+ // when & then
64+ mockMvc .perform (
65+ get ("/api/settlements/requested" )
66+ .session (session )
67+ )
68+ .andDo (print ())
69+ .andExpect (status ().isOk ())
70+ .andExpect (jsonPath ("$.size()" ).value (1 ))
71+ .andExpect (jsonPath ("$[0].settlementId" ).value (1L ))
72+ .andExpect (jsonPath ("$[0].requestAccountId" ).value (1L ))
73+ .andExpect (jsonPath ("$[0].totalAmount" ).value (30000 ));
74+ }
75+
76+ @ DisplayName ("내가 요청받은 정산 리스트를 정장석으로 조회한다." )
77+ @ Test
78+ void getReceivedSettlements () throws Exception {
79+ // given
80+ List <ReceivedSettlementResponse > responses = List .of (
81+ new ReceivedSettlementResponse (1L , 2L , 30000 , 1L , 10000 )
82+ );
83+
84+ when (settlementService .getReceivedSettlements (1L )).thenReturn (responses );
85+ // when // then
86+ mockMvc .perform (
87+ get ("/api/settlements/received" )
88+ .session (session )
89+ )
90+ .andDo (print ())
91+ .andExpect (status ().isOk ())
92+ .andExpect (jsonPath ("$.size()" ).value (1 ))
93+ .andExpect (jsonPath ("$[0].settlementId" ).value (1L ))
94+ .andExpect (jsonPath ("$[0].requestAccountId" ).value (2L ))
95+ .andExpect (jsonPath ("$[0].totalAmount" ).value (30000 ))
96+ .andExpect (jsonPath ("$[0].myAccountId" ).value (1L ))
97+ .andExpect (jsonPath ("$[0].mySettlementAmount" ).value (10000 ));
98+ }
99+ }
0 commit comments