1+ package com .back .domain .user .controller ;
2+
3+ import com .back .domain .comment .entity .Comment ;
4+ import com .back .domain .comment .repository .CommentRepository ;
5+ import com .back .domain .node .entity .BaseLine ;
6+ import com .back .domain .node .entity .DecisionLine ;
7+ import com .back .domain .node .entity .DecisionLineStatus ;
8+ import com .back .domain .node .repository .BaseLineRepository ;
9+ import com .back .domain .node .repository .DecisionLineRepository ;
10+ import com .back .domain .post .entity .Post ;
11+ import com .back .domain .post .enums .PostCategory ;
12+ import com .back .domain .post .repository .PostRepository ;
13+ import com .back .domain .scenario .entity .Scenario ;
14+ import com .back .domain .scenario .entity .ScenarioStatus ;
15+ import com .back .domain .scenario .repository .ScenarioRepository ;
16+ import com .back .domain .user .dto .UserInfoRequest ;
17+ import com .back .domain .user .entity .*;
18+ import com .back .domain .user .repository .UserRepository ;
19+ import com .back .global .security .CustomUserDetails ;
20+ import com .fasterxml .jackson .databind .ObjectMapper ;
21+ import org .junit .jupiter .api .BeforeEach ;
22+ import org .junit .jupiter .api .DisplayName ;
23+ import org .junit .jupiter .api .Test ;
24+ import org .springframework .beans .factory .annotation .Autowired ;
25+ import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
26+ import org .springframework .boot .test .context .SpringBootTest ;
27+ import org .springframework .http .MediaType ;
28+ import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
29+ import org .springframework .test .context .ActiveProfiles ;
30+ import org .springframework .test .web .servlet .MockMvc ;
31+ import org .springframework .transaction .annotation .Transactional ;
32+
33+ import java .time .LocalDateTime ;
34+
35+ import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .authentication ;
36+ import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .csrf ;
37+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .*;
38+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .jsonPath ;
39+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
40+
41+ @ SpringBootTest
42+ @ AutoConfigureMockMvc
43+ @ ActiveProfiles ("test" )
44+ @ Transactional
45+ public class UserInfoControllerTest {
46+ @ Autowired
47+ private MockMvc mockMvc ;
48+
49+ @ Autowired
50+ private ObjectMapper objectMapper ;
51+
52+ @ Autowired
53+ private UserRepository userRepository ;
54+
55+ @ Autowired
56+ private ScenarioRepository scenarioRepository ;
57+
58+ @ Autowired
59+ private PostRepository postRepository ;
60+
61+ @ Autowired
62+ private CommentRepository commentRepository ;
63+
64+ @ Autowired
65+ private BaseLineRepository baseLineRepository ;
66+
67+ @ Autowired
68+ private DecisionLineRepository decisionLineRepository ;
69+
70+ private User testUser ;
71+ private UsernamePasswordAuthenticationToken authentication ;
72+
73+ @ BeforeEach
74+ void setUp () {
75+ testUser = User .builder ()
76+ 77+ .password ("password" )
78+ .username ("TestUser" )
79+ .nickname ("testnick" )
80+ .birthdayAt (LocalDateTime .of (1990 , 1 , 1 , 0 , 0 ))
81+ .gender (Gender .M )
82+ .mbti (Mbti .INFP )
83+ .beliefs ("Test beliefs" )
84+ .lifeSatis (7 )
85+ .relationship (8 )
86+ .workLifeBal (6 )
87+ .riskAvoid (5 )
88+ .role (Role .USER )
89+ .authProvider (AuthProvider .LOCAL )
90+ .build ();
91+ testUser = userRepository .save (testUser );
92+
93+ CustomUserDetails userDetails = new CustomUserDetails (testUser );
94+ authentication = new UsernamePasswordAuthenticationToken (
95+ userDetails , null , userDetails .getAuthorities ());
96+ }
97+
98+ @ Test
99+ @ DisplayName ("성공 - 사용자 통계 정보 조회 성공" )
100+ void t1 () throws Exception {
101+ BaseLine testBaseLine = BaseLine .builder ()
102+ .user (testUser )
103+ .title ("Test BaseLine" )
104+ .build ();
105+ testBaseLine = baseLineRepository .save (testBaseLine );
106+
107+ DecisionLine testDecisionLine = DecisionLine .builder ()
108+ .user (testUser )
109+ .baseLine (testBaseLine )
110+ .status (DecisionLineStatus .COMPLETED )
111+ .build ();
112+ testDecisionLine = decisionLineRepository .save (testDecisionLine );
113+
114+ Scenario scenario = Scenario .builder ()
115+ .user (testUser )
116+ .decisionLine (testDecisionLine )
117+ .status (ScenarioStatus .COMPLETED )
118+ .job ("Software Engineer" )
119+ .total (100 )
120+ .summary ("Test summary 1" )
121+ .description ("Test description 1" )
122+ .build ();
123+ scenarioRepository .save (scenario );
124+
125+ Post post = Post .builder ()
126+ .user (testUser )
127+ .title ("Test Post 1" )
128+ .content ("Content 1" )
129+ .category (PostCategory .CHAT )
130+ .hide (false )
131+ .build ();
132+ postRepository .save (post );
133+
134+ Comment comment = Comment .builder ()
135+ .user (testUser )
136+ .post (post )
137+ .content ("Comment 1" )
138+ .hide (false )
139+ .build ();
140+ commentRepository .save (comment );
141+
142+ mockMvc .perform (get ("/api/v1/users-info/stats" )
143+ .with (authentication (authentication )))
144+ .andExpect (status ().isOk ())
145+ .andExpect (jsonPath ("$.scenarioCount" ).value (1 ))
146+ .andExpect (jsonPath ("$.totalPoints" ).value (100 ))
147+ .andExpect (jsonPath ("$.postCount" ).value (1 ))
148+ .andExpect (jsonPath ("$.commentCount" ).value (1 ))
149+ .andExpect (jsonPath ("$.mbti" ).value ("INFP" ));
150+ }
151+
152+ @ Test
153+ @ DisplayName ("성공 - 사용자 정보 조회 성공" )
154+ void t2 () throws Exception {
155+ mockMvc .perform (get ("/api/v1/users-info" )
156+ .with (authentication (authentication )))
157+ .andExpect (status ().isOk ())
158+ .
andExpect (
jsonPath (
"$.email" ).
value (
"[email protected] " ))
159+ .andExpect (jsonPath ("$.username" ).value ("TestUser" ))
160+ .andExpect (jsonPath ("$.nickname" ).value ("testnick" ))
161+ .andExpect (jsonPath ("$.gender" ).value ("M" ))
162+ .andExpect (jsonPath ("$.mbti" ).value ("INFP" ))
163+ .andExpect (jsonPath ("$.beliefs" ).value ("Test beliefs" ))
164+ .andExpect (jsonPath ("$.lifeSatis" ).value (7 ))
165+ .andExpect (jsonPath ("$.relationship" ).value (8 ))
166+ .andExpect (jsonPath ("$.workLifeBal" ).value (6 ))
167+ .andExpect (jsonPath ("$.riskAvoid" ).value (5 ));
168+ }
169+
170+ @ Test
171+ @ DisplayName ("성공 - 사용자 정보 생성 성공" )
172+ void t3 () throws Exception {
173+ UserInfoRequest request = new UserInfoRequest (
174+ "UpdatedUser" ,
175+ LocalDateTime .of (1995 , 5 , 15 , 0 , 0 ),
176+ Gender .F ,
177+ Mbti .ENFJ ,
178+ "Updated beliefs" ,
179+ 9 ,
180+ 7 ,
181+ 8 ,
182+ 6
183+ );
184+
185+ mockMvc .perform (post ("/api/v1/users-info" )
186+ .with (authentication (authentication ))
187+ .with (csrf ())
188+ .contentType (MediaType .APPLICATION_JSON )
189+ .content (objectMapper .writeValueAsString (request )))
190+ .andExpect (status ().isOk ())
191+ .andExpect (jsonPath ("$.username" ).value ("UpdatedUser" ))
192+ .andExpect (jsonPath ("$.gender" ).value ("F" ))
193+ .andExpect (jsonPath ("$.mbti" ).value ("ENFJ" ))
194+ .andExpect (jsonPath ("$.beliefs" ).value ("Updated beliefs" ))
195+ .andExpect (jsonPath ("$.lifeSatis" ).value (9 ))
196+ .andExpect (jsonPath ("$.relationship" ).value (7 ))
197+ .andExpect (jsonPath ("$.workLifeBal" ).value (8 ))
198+ .andExpect (jsonPath ("$.riskAvoid" ).value (6 ));
199+ }
200+
201+ @ Test
202+ @ DisplayName ("성공 - 사용자 정보 수정 성공" )
203+ void t4 () throws Exception {
204+ UserInfoRequest request = new UserInfoRequest (
205+ "ModifiedUser" ,
206+ null ,
207+ null ,
208+ Mbti .ISTJ ,
209+ null ,
210+ 10 ,
211+ null ,
212+ null ,
213+ null
214+ );
215+
216+ mockMvc .perform (put ("/api/v1/users-info" )
217+ .with (authentication (authentication ))
218+ .with (csrf ())
219+ .contentType (MediaType .APPLICATION_JSON )
220+ .content (objectMapper .writeValueAsString (request )))
221+ .andExpect (status ().isOk ())
222+ .andExpect (jsonPath ("$.username" ).value ("ModifiedUser" ))
223+ .andExpect (jsonPath ("$.mbti" ).value ("ISTJ" ))
224+ .andExpect (jsonPath ("$.lifeSatis" ).value (10 ))
225+ .andExpect (jsonPath ("$.gender" ).value ("M" ))
226+ .andExpect (jsonPath ("$.relationship" ).value (8 ));
227+ }
228+
229+ @ Test
230+ @ DisplayName ("실패 - 인증되지 않은 사용자 정보 조회 실패" )
231+ void t5 () throws Exception {
232+ mockMvc .perform (get ("/api/v1/users-info" ))
233+ .andExpect (status ().is4xxClientError ());
234+ }
235+
236+ @ Test
237+ @ DisplayName ("성공 - null 필드는 업데이트하지 않는 부분 검증" )
238+ void t6 () throws Exception {
239+ UserInfoRequest request = new UserInfoRequest (
240+ null ,
241+ null ,
242+ null ,
243+ null ,
244+ "Only beliefs updated" ,
245+ null ,
246+ null ,
247+ null ,
248+ null
249+ );
250+
251+ mockMvc .perform (put ("/api/v1/users-info" )
252+ .with (authentication (authentication ))
253+ .with (csrf ())
254+ .contentType (MediaType .APPLICATION_JSON )
255+ .content (objectMapper .writeValueAsString (request )))
256+ .andExpect (status ().isOk ())
257+ .andExpect (jsonPath ("$.beliefs" ).value ("Only beliefs updated" ))
258+ .andExpect (jsonPath ("$.username" ).value ("TestUser" ))
259+ .andExpect (jsonPath ("$.mbti" ).value ("INFP" ));
260+ }
261+ }
0 commit comments