1+ package devut .buzzerbidder .domain .notification .listener ;
2+
3+ import static org .mockito .ArgumentMatchers .any ;
4+ import static org .mockito .ArgumentMatchers .contains ;
5+ import static org .mockito .ArgumentMatchers .eq ;
6+ import static org .mockito .Mockito .times ;
7+ import static org .mockito .Mockito .verify ;
8+
9+ import devut .buzzerbidder .domain .liveitem .event .LiveAuctionEndedEvent ;
10+ import devut .buzzerbidder .domain .notification .enums .NotificationType ;
11+ import devut .buzzerbidder .domain .notification .service .NotificationService ;
12+ import java .util .Map ;
13+ import org .junit .jupiter .api .DisplayName ;
14+ import org .junit .jupiter .api .Test ;
15+ import org .junit .jupiter .api .extension .ExtendWith ;
16+ import org .mockito .InjectMocks ;
17+ import org .mockito .Mock ;
18+ import org .mockito .junit .jupiter .MockitoExtension ;
19+
20+ @ ExtendWith (MockitoExtension .class )
21+ public class LiveAuctionEndedNotificationListenerTest {
22+
23+ @ Mock
24+ private NotificationService notificationService ;
25+
26+ @ InjectMocks
27+ private LiveAuctionEndedNotificationListener listener ;
28+
29+ // ========== 낙찰 성공 알림 테스트 ==========
30+
31+ @ Test
32+ @ DisplayName ("t1: 낙찰 성공시 판매자에게 낙찰 알림 전송" )
33+ void t1 () {
34+ // given
35+ Long liveItemId = 1L ;
36+ Long sellerUserId = 10L ;
37+ Long winnerUserId = 20L ;
38+ String liveItemName = "테스트 상품" ;
39+ Integer finalPrice = 50000 ;
40+
41+ LiveAuctionEndedEvent event = new LiveAuctionEndedEvent (
42+ liveItemId ,
43+ liveItemName ,
44+ sellerUserId ,
45+ true ,
46+ winnerUserId ,
47+ finalPrice
48+ );
49+
50+ // when
51+ listener .handle (event );
52+
53+ // then
54+ verify (notificationService ).createAndSend (
55+ eq (sellerUserId ),
56+ eq (NotificationType .LIVE_SUCCESS_SELLER ),
57+ contains ("낙찰되었습니다" ),
58+ eq ("LIVE_ITEM" ),
59+ eq (liveItemId ),
60+ any (Map .class )
61+ );
62+ }
63+
64+ @ Test
65+ @ DisplayName ("t2: 낙찰 성공시 낙찰자에게 낙찰 축하 알림 전송" )
66+ void t2 () {
67+ // given
68+ Long liveItemId = 2L ;
69+ Long sellerUserId = 15L ;
70+ Long winnerUserId = 25L ;
71+ String liveItemName = "경매 상품" ;
72+ Integer finalPrice = 75000 ;
73+
74+ LiveAuctionEndedEvent event = new LiveAuctionEndedEvent (
75+ liveItemId ,
76+ liveItemName ,
77+ sellerUserId ,
78+ true ,
79+ winnerUserId ,
80+ finalPrice
81+ );
82+
83+ // when
84+ listener .handle (event );
85+
86+ // then
87+ verify (notificationService ).createAndSend (
88+ eq (winnerUserId ),
89+ eq (NotificationType .LIVE_SUCCESS_BIDDER ),
90+ contains ("축하합니다" ),
91+ eq ("LIVE_ITEM" ),
92+ eq (liveItemId ),
93+ any (Map .class )
94+ );
95+ }
96+
97+ @ Test
98+ @ DisplayName ("t3: 낙찰 성공시 판매자와 낙찰자 모두에게 알림 전송" )
99+ void t3 () {
100+ // given
101+ Long liveItemId = 3L ;
102+ Long sellerUserId = 30L ;
103+ Long winnerUserId = 40L ;
104+ String liveItemName = "인기 상품" ;
105+ Integer finalPrice = 100000 ;
106+
107+ LiveAuctionEndedEvent event = new LiveAuctionEndedEvent (
108+ liveItemId ,
109+ liveItemName ,
110+ sellerUserId ,
111+ true ,
112+ winnerUserId ,
113+ finalPrice
114+ );
115+
116+ // when
117+ listener .handle (event );
118+
119+ // then
120+ verify (notificationService , times (2 )).createAndSend (
121+ any (Long .class ),
122+ any (NotificationType .class ),
123+ any (String .class ),
124+ eq ("LIVE_ITEM" ),
125+ eq (liveItemId ),
126+ any (Map .class )
127+ );
128+ }
129+
130+ @ Test
131+ @ DisplayName ("t4: 판매자 알림 메시지에 상품명과 낙찰가가 포함됨" )
132+ void t4 () {
133+ // given
134+ String liveItemName = "특별한 상품" ;
135+ Integer finalPrice = 120000 ;
136+
137+ LiveAuctionEndedEvent event = new LiveAuctionEndedEvent (
138+ 1L ,
139+ liveItemName ,
140+ 10L ,
141+ true ,
142+ 20L ,
143+ finalPrice
144+ );
145+
146+ // when
147+ listener .handle (event );
148+
149+ // then
150+ verify (notificationService ).createAndSend (
151+ eq (10L ),
152+ eq (NotificationType .LIVE_SUCCESS_SELLER ),
153+ contains (liveItemName ),
154+ eq ("LIVE_ITEM" ),
155+ eq (1L ),
156+ any (Map .class )
157+ );
158+
159+ verify (notificationService ).createAndSend (
160+ eq (10L ),
161+ eq (NotificationType .LIVE_SUCCESS_SELLER ),
162+ contains (String .valueOf (finalPrice )),
163+ eq ("LIVE_ITEM" ),
164+ eq (1L ),
165+ any (Map .class )
166+ );
167+ }
168+
169+ // ========== 유찰 알림 테스트 ==========
170+
171+ @ Test
172+ @ DisplayName ("t5: 유찰시 판매자에게 유찰 알림 전송" )
173+ void t5 () {
174+ // given
175+ Long liveItemId = 5L ;
176+ Long sellerUserId = 50L ;
177+ String liveItemName = "유찰 상품" ;
178+
179+ LiveAuctionEndedEvent event = new LiveAuctionEndedEvent (
180+ liveItemId ,
181+ liveItemName ,
182+ sellerUserId ,
183+ false ,
184+ null ,
185+ null
186+ );
187+
188+ // when
189+ listener .handle (event );
190+
191+ // then
192+ verify (notificationService ).createAndSend (
193+ eq (sellerUserId ),
194+ eq (NotificationType .LIVE_FAILED_SELLER ),
195+ contains ("유찰되었습니다" ),
196+ eq ("LIVE_ITEM" ),
197+ eq (liveItemId ),
198+ any (Map .class )
199+ );
200+ }
201+
202+ @ Test
203+ @ DisplayName ("t6: 유찰시 낙찰자에게는 알림을 전송하지 않음" )
204+ void t6 () {
205+ // given
206+ Long liveItemId = 6L ;
207+ Long sellerUserId = 60L ;
208+ String liveItemName = "미낙찰 상품" ;
209+
210+ LiveAuctionEndedEvent event = new LiveAuctionEndedEvent (
211+ liveItemId ,
212+ liveItemName ,
213+ sellerUserId ,
214+ false ,
215+ null ,
216+ null
217+ );
218+
219+ // when
220+ listener .handle (event );
221+
222+ // then
223+ verify (notificationService , times (1 )).createAndSend (
224+ any (Long .class ),
225+ any (NotificationType .class ),
226+ any (String .class ),
227+ any (String .class ),
228+ any (Long .class ),
229+ any (Map .class )
230+ );
231+ }
232+
233+ @ Test
234+ @ DisplayName ("t7: 유찰 알림 메시지에 상품명이 포함됨" )
235+ void t7 () {
236+ String liveItemName = "유찰된 상품" ;
237+
238+ LiveAuctionEndedEvent event = new LiveAuctionEndedEvent (
239+ 1L ,
240+ liveItemName ,
241+ 10L ,
242+ false ,
243+ null ,
244+ null
245+ );
246+
247+ // when
248+ listener .handle (event );
249+
250+ // then
251+ verify (notificationService ).createAndSend (
252+ eq (10L ),
253+ eq (NotificationType .LIVE_FAILED_SELLER ),
254+ contains (liveItemName ),
255+ eq ("LIVE_ITEM" ),
256+ eq (1L ),
257+ any (Map .class )
258+ );
259+ }
260+ }
0 commit comments