1+ package com .somemore .notification .service ;
2+
3+ import static com .somemore .global .exception .ExceptionMessage .NOT_EXISTS_NOTIFICATION ;
4+ import static org .assertj .core .api .Assertions .assertThat ;
5+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
6+
7+ import com .somemore .IntegrationTestSupport ;
8+ import com .somemore .global .exception .BadRequestException ;
9+ import com .somemore .notification .domain .Notification ;
10+ import com .somemore .notification .domain .NotificationSubType ;
11+ import com .somemore .notification .dto .NotificationIdsRequestDto ;
12+ import com .somemore .notification .repository .NotificationRepository ;
13+
14+ import java .util .List ;
15+ import java .util .UUID ;
16+
17+ import org .junit .jupiter .api .DisplayName ;
18+ import org .junit .jupiter .api .Test ;
19+ import org .springframework .beans .factory .annotation .Autowired ;
20+ import org .springframework .transaction .annotation .Transactional ;
21+
22+ @ Transactional
23+ class NotificationCommandServiceTest extends IntegrationTestSupport {
24+
25+ @ Autowired
26+ private NotificationCommandService notificationCommandService ;
27+
28+ @ Autowired
29+ private NotificationRepository notificationRepository ;
30+
31+ @ DisplayName ("알림 1개를 읽음 처리한다." )
32+ @ Test
33+ void markSingleNotificationAsRead () {
34+ // given
35+ UUID receiverId = UUID .randomUUID ();
36+ Notification notification = createNotification (receiverId );
37+ notificationRepository .save (notification );
38+
39+ // when
40+ notificationCommandService .markSingleNotificationAsRead (receiverId , notification .getId ());
41+
42+ // then
43+ Notification updatedNotification = notificationRepository .findById (notification .getId ()).orElseThrow ();
44+ assertThat (updatedNotification .isRead ()).isTrue ();
45+ }
46+
47+ @ DisplayName ("존재하지 않는 알림을 읽음 처리하려고 하면 에러가 발생한다." )
48+ @ Test
49+ void markSingleNotificationAsReadWhenNotificationNotExists () {
50+ // given
51+ UUID receiverId = UUID .randomUUID ();
52+ Long nonExistentNotificationId = 999L ;
53+
54+ // when / then
55+ assertThatThrownBy (() -> notificationCommandService .markSingleNotificationAsRead (receiverId , nonExistentNotificationId ))
56+ .isInstanceOf (BadRequestException .class )
57+ .hasMessageContaining (NOT_EXISTS_NOTIFICATION .getMessage ());
58+ }
59+
60+ @ DisplayName ("다른 사용자의 알림을 읽음 처리하려고 하면 에러가 발생한다." )
61+ @ Test
62+ void markSingleNotificationAsReadWhenUnauthorized () {
63+ // given
64+ UUID receiverId = UUID .randomUUID ();
65+ UUID anotherUserId = UUID .randomUUID ();
66+ Notification notification = createNotification (anotherUserId );
67+ notificationRepository .save (notification );
68+
69+ // when / then
70+ assertThatThrownBy (() -> notificationCommandService .markSingleNotificationAsRead (receiverId , notification .getId ()))
71+ .isInstanceOf (BadRequestException .class )
72+ .hasMessageContaining ("권한이 없습니다" );
73+ }
74+
75+ @ DisplayName ("알림 N개를 읽음 처리한다." )
76+ @ Test
77+ void markMultipleNotificationsAsRead () {
78+ // given
79+ UUID receiverId = UUID .randomUUID ();
80+ List <Notification > notifications = List .of (
81+ createNotification (receiverId ),
82+ createNotification (receiverId )
83+ );
84+ notifications .forEach (notificationRepository ::save );
85+
86+ List <Long > notificationIds = notifications .stream ().map (Notification ::getId ).toList ();
87+ NotificationIdsRequestDto requestDto = NotificationIdsRequestDto .builder ()
88+ .ids (notificationIds )
89+ .build ();
90+
91+ // when
92+ notificationCommandService .markMultipleNotificationsAsRead (receiverId , requestDto );
93+
94+ // then
95+ List <Notification > updatedNotifications = notificationRepository .findAllByIds (notificationIds );
96+ updatedNotifications .forEach (notification -> assertThat (notification .isRead ()).isTrue ());
97+ }
98+
99+ @ DisplayName ("다른 사용자의 알림 N개를 읽음 처리하려고 하면 에러가 발생한다." )
100+ @ Test
101+ void markMultipleNotificationsAsReadWhenUnauthorized () {
102+ // given
103+ UUID receiverId = UUID .randomUUID ();
104+ UUID anotherUserId = UUID .randomUUID ();
105+ List <Notification > notifications = List .of (
106+ createNotification (anotherUserId ),
107+ createNotification (anotherUserId )
108+ );
109+ notifications .forEach (notificationRepository ::save );
110+
111+ List <Long > notificationIds = notifications .stream ()
112+ .map (Notification ::getId )
113+ .toList ();
114+ NotificationIdsRequestDto requestDto = NotificationIdsRequestDto .builder ()
115+ .ids (notificationIds )
116+ .build ();
117+
118+ // when / then
119+ assertThatThrownBy (() -> notificationCommandService .markMultipleNotificationsAsRead (receiverId , requestDto ))
120+ .isInstanceOf (BadRequestException .class )
121+ .hasMessageContaining ("권한이 없습니다" );
122+ }
123+
124+ private Notification createNotification (UUID receiverId ) {
125+ return Notification .builder ()
126+ .receiverId (receiverId )
127+ .title ("Unread" )
128+ .type (NotificationSubType .REVIEW_BLAH_BLAH )
129+ .relatedId (1L )
130+ .build ();
131+ }
132+ }
0 commit comments