1+ package org .dfbf .soundlink .domain .blocklist ;
2+
3+ import org .dfbf .soundlink .CustomLogger ;
4+ import org .dfbf .soundlink .domain .blocklist .dto .BlockReq ;
5+ import org .dfbf .soundlink .domain .blocklist .entity .Blocklist ;
6+ import org .dfbf .soundlink .domain .blocklist .repository .BlockListQueryRepository ;
7+ import org .dfbf .soundlink .domain .blocklist .repository .BlockListRepository ;
8+ import org .dfbf .soundlink .domain .blocklist .service .BlockListService ;
9+ import org .dfbf .soundlink .domain .user .entity .User ;
10+ import org .dfbf .soundlink .domain .user .repository .UserRepository ;
11+ import org .dfbf .soundlink .global .exception .ErrorCode ;
12+ import org .dfbf .soundlink .global .exception .ResponseResult ;
13+ import org .junit .jupiter .api .BeforeEach ;
14+ import org .junit .jupiter .api .DisplayName ;
15+ import org .junit .jupiter .api .Test ;
16+ import org .junit .jupiter .api .extension .ExtendWith ;
17+ import org .mockito .InjectMocks ;
18+ import org .mockito .Mock ;
19+ import org .mockito .MockitoAnnotations ;
20+
21+ import java .util .Optional ;
22+ import java .util .List ;
23+
24+ import static org .assertj .core .api .Assertions .assertThat ;
25+ import static org .mockito .Mockito .*;
26+
27+ class BlocklistServiceTest {
28+
29+ @ InjectMocks
30+ private BlockListService blockListService ;
31+
32+ @ Mock
33+ private BlockListQueryRepository blockListQueryRepository ;
34+
35+ @ Mock
36+ private UserRepository userRepository ;
37+
38+ @ Mock
39+ private BlockListRepository blockListRepository ;
40+
41+ @ BeforeEach
42+ void setUp () {
43+ MockitoAnnotations .openMocks (this );
44+ }
45+
46+ @ Test
47+ @ DisplayName ("1. blockUserSuccess - 유저 차단 성공" )
48+ @ ExtendWith (CustomLogger .class )
49+ void blockUserSuccess () {
50+ // given
51+ Long userId = 1L ;
52+ String targetLoginId = "targetUser" ;
53+ BlockReq blockReq = new BlockReq (targetLoginId );
54+
55+ User user = mock (User .class );
56+ User blockedUser = mock (User .class );
57+
58+ when (userRepository .findById (userId )).thenReturn (Optional .of (user ));
59+ when (userRepository .findByLoginId (targetLoginId )).thenReturn (Optional .of (blockedUser ));
60+ when (blockListQueryRepository .findByUser_UserIdAndBlockedUser_LoginId (userId , targetLoginId ))
61+ .thenReturn (Optional .empty ());
62+
63+ // when
64+ ResponseResult result = blockListService .blockUser (userId , blockReq );
65+
66+ // then
67+ assertThat (result .getMessage ()).isEqualTo (ErrorCode .SUCCESS .getMessage ());
68+ verify (blockListRepository , times (1 )).save (any (Blocklist .class ));
69+ }
70+
71+ @ Test
72+ @ DisplayName ("2. blockUserFailWhenAlreadyBlocked - 이미 차단된 유저" )
73+ @ ExtendWith (CustomLogger .class )
74+ void blockUserFailWhenAlreadyBlocked () {
75+ // given
76+ Long userId = 1L ;
77+ String targetLoginId = "targetUser" ;
78+ BlockReq blockReq = new BlockReq (targetLoginId );
79+
80+ User user = mock (User .class );
81+ User blockedUser = mock (User .class );
82+
83+ when (userRepository .findById (userId )).thenReturn (Optional .of (user ));
84+ when (userRepository .findByLoginId (targetLoginId )).thenReturn (Optional .of (blockedUser ));
85+ when (blockListQueryRepository .findByUser_UserIdAndBlockedUser_LoginId (userId , targetLoginId ))
86+ .thenReturn (Optional .of (mock (Blocklist .class )));
87+
88+ // when
89+ ResponseResult result = blockListService .blockUser (userId , blockReq );
90+
91+ // then
92+ assertThat (result .getMessage ()).isEqualTo (ErrorCode .ALREADY_BLOCKED_USER .getMessage ());
93+ verify (blockListRepository , never ()).save (any ());
94+ }
95+
96+ @ Test
97+ @ DisplayName ("3. unblockUserSuccess - 유저 차단 해제" )
98+ @ ExtendWith (CustomLogger .class )
99+ void unblockUserSuccess () {
100+ // given
101+ Long userId = 1L ;
102+ Long blocklistId = 2L ;
103+ Blocklist blocklist = mock (Blocklist .class );
104+
105+ when (blockListQueryRepository .findByUser_UserIdAndBlockedUser_UserId (userId , blocklistId ))
106+ .thenReturn (Optional .of (blocklist ));
107+
108+ // when
109+ ResponseResult result = blockListService .unblockUser (userId , blocklistId );
110+
111+ // then
112+ assertThat (result .getMessage ()).isEqualTo (ErrorCode .SUCCESS .getMessage ());
113+ verify (blockListRepository , times (1 )).delete (blocklist );
114+ }
115+
116+ @ Test
117+ @ DisplayName ("4. searchBlocklistSuccess - 차단된 유저 전체 조회" )
118+ @ ExtendWith (CustomLogger .class )
119+ void searchBlocklistSuccess () {
120+ // given
121+ Long userId = 1L ;
122+
123+ User user = mock (User .class );
124+ User blockedUser = mock (User .class );
125+
126+ when (user .getUserId ()).thenReturn (userId );
127+ when (blockedUser .getUserId ()).thenReturn (2L );
128+ when (blockedUser .getNickname ()).thenReturn ("닉네임" );
129+ when (blockedUser .getLoginId ()).thenReturn ("loginId" );
130+
131+ Blocklist blocklist = mock (Blocklist .class );
132+ when (blocklist .getUser ()).thenReturn (user );
133+ when (blocklist .getBlockedUser ()).thenReturn (blockedUser );
134+
135+ List <Blocklist > mockBlocklist = List .of (blocklist );
136+
137+ when (blockListQueryRepository .findAllByUser_UserId (userId ))
138+ .thenReturn (mockBlocklist );
139+
140+ // when
141+ ResponseResult result = blockListService .getBlockListByUserId (userId );
142+
143+ // then
144+ assertThat (result .getMessage ()).isEqualTo (ErrorCode .SUCCESS .getMessage ());
145+ assertThat (result .getData ()).isInstanceOf (List .class );
146+ }
147+ }
0 commit comments