1717import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
1818
1919import java .security .Principal ;
20+ import java .util .HashMap ;
21+ import java .util .Map ;
2022
2123import static org .junit .jupiter .api .Assertions .assertThrows ;
2224import static org .mockito .ArgumentMatchers .*;
@@ -56,6 +58,60 @@ private Principal createMockPrincipal(Long userId) {
5658 return new UsernamePasswordAuthenticationToken (mockUserDetails , null , null );
5759 }
5860
61+ @ Nested
62+ @ DisplayName ("WebSocket 방 입장 처리" )
63+ class HandleWebSocketJoinRoomTest {
64+
65+ @ Test
66+ @ DisplayName ("성공 - 인증된 사용자의 방 입장 확인" )
67+ void t1 () {
68+ // given
69+ Long roomId = 1L ;
70+ Map <String , Object > payload = new HashMap <>();
71+ Principal mockPrincipal = createMockPrincipal (userId );
72+ doNothing ().when (sessionManager ).updateLastActivity (userId );
73+
74+ // when
75+ controller .handleWebSocketJoinRoom (roomId , payload , mockPrincipal );
76+
77+ // then
78+ verify (sessionManager ).updateLastActivity (userId );
79+ }
80+
81+ @ Test
82+ @ DisplayName ("실패 - 인증 정보가 없는 경우 아무 동작도 하지 않음" )
83+ void t2 () {
84+ // given
85+ Long roomId = 1L ;
86+ Map <String , Object > payload = new HashMap <>();
87+ Principal principal = null ;
88+
89+ // when
90+ controller .handleWebSocketJoinRoom (roomId , payload , principal );
91+
92+ // then
93+ verify (sessionManager , never ()).updateLastActivity (any (Long .class ));
94+ }
95+
96+ @ Test
97+ @ DisplayName ("실패 - CustomException 발생 시 예외를 그대로 던짐" )
98+ void t3 () {
99+ // given
100+ Long roomId = 1L ;
101+ Map <String , Object > payload = new HashMap <>();
102+ Principal mockPrincipal = createMockPrincipal (userId );
103+ CustomException expectedException = new CustomException (ErrorCode .BAD_REQUEST );
104+ doThrow (expectedException ).when (sessionManager ).updateLastActivity (userId );
105+
106+ // when & then
107+ assertThrows (CustomException .class , () -> {
108+ controller .handleWebSocketJoinRoom (roomId , payload , mockPrincipal );
109+ });
110+
111+ verify (sessionManager ).updateLastActivity (userId );
112+ }
113+ }
114+
59115 @ Nested
60116 @ DisplayName ("Heartbeat 처리" )
61117 class HandleHeartbeatTest {
@@ -85,7 +141,7 @@ void t2() {
85141 controller .handleHeartbeat (principal , headerAccessor );
86142
87143 // then
88- verify (sessionManager , never ()).updateLastActivity (any ());
144+ verify (sessionManager , never ()).updateLastActivity (any (Long . class ));
89145 verify (errorHelper ).sendUnauthorizedError (sessionId );
90146 }
91147
@@ -191,4 +247,39 @@ void t4() {
191247 verifyNoInteractions (errorHelper );
192248 }
193249 }
250+
251+ @ Nested
252+ @ DisplayName ("예외 처리" )
253+ class ExceptionHandlerTest {
254+
255+ @ Test
256+ @ DisplayName ("CustomException 처리 - ErrorHelper를 통해 에러 전송" )
257+ void t1 () {
258+ // given
259+ CustomException exception = new CustomException (ErrorCode .ROOM_NOT_FOUND );
260+
261+ // when
262+ controller .handleCustomException (exception , headerAccessor );
263+
264+ // then
265+ verify (errorHelper ).sendCustomExceptionToUser (sessionId , exception );
266+ }
267+
268+ @ Test
269+ @ DisplayName ("일반 Exception 처리 - ErrorHelper를 통해 일반 에러 전송" )
270+ void t2 () {
271+ // given
272+ Exception exception = new RuntimeException ("예상치 못한 오류" );
273+
274+ // when
275+ controller .handleGeneralException (exception , headerAccessor );
276+
277+ // then
278+ verify (errorHelper ).sendGenericErrorToUser (
279+ eq (sessionId ),
280+ eq (exception ),
281+ eq ("요청 처리 중 서버 오류가 발생했습니다." )
282+ );
283+ }
284+ }
194285}
0 commit comments