-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMockNotificationServiceTest.java
More file actions
200 lines (164 loc) · 9.3 KB
/
MockNotificationServiceTest.java
File metadata and controls
200 lines (164 loc) · 9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.dreamypatisiel.devdevdev.domain.service.notification;
import static com.dreamypatisiel.devdevdev.domain.service.notification.NotificationService.UNREAD_NOTIFICATION_FORMAT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import com.dreamypatisiel.devdevdev.domain.entity.Member;
import com.dreamypatisiel.devdevdev.domain.repository.SseEmitterRepository;
import com.dreamypatisiel.devdevdev.domain.repository.notification.NotificationRepository;
import com.dreamypatisiel.devdevdev.global.common.MemberProvider;
import com.dreamypatisiel.devdevdev.global.common.TimeProvider;
import com.dreamypatisiel.devdevdev.redis.sub.NotificationMessageDto;
import java.io.IOException;
import java.time.LocalDateTime;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.security.core.Authentication;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
@SpringBootTest
@Transactional
class MockNotificationServiceTest {
@MockBean
TimeProvider timeProvider;
@MockBean
private MemberProvider memberProvider;
@MockBean
private NotificationRepository notificationRepository;
@MockBean
private SseEmitterRepository sseEmitterRepository;
@SpyBean
private NotificationService notificationService;
@ParameterizedTest
@ValueSource(longs = {1, 2, 10, 50, 100})
@DisplayName("읽지 않은 알림이 존재할 때, SSE 구독 등록 후 알림을 전송한다.")
void addClientAndSendNotificationHaveUnreadNotification(long unreadNotificationCount) throws Exception {
// given
Authentication mockAuthentication = mock(Authentication.class);
Member mockMember = mock(Member.class);
SseEmitter realEmitter = new SseEmitter();
SseEmitter spyEmitter = spy(realEmitter);
given(timeProvider.getLocalDateTimeNow()).willReturn(LocalDateTime.of(2025, 1, 1, 0, 0, 0));
given(memberProvider.getMemberByAuthentication(mockAuthentication)).willReturn(mockMember);
given(notificationRepository.countByMemberAndIsReadIsFalse(mockMember)).willReturn(unreadNotificationCount);
given(notificationService.createSseEmitter(anyLong())).willReturn(spyEmitter);
String notificationMessage = String.format(UNREAD_NOTIFICATION_FORMAT, unreadNotificationCount);
NotificationMessageDto notificationMessageDto = new NotificationMessageDto(notificationMessage,
timeProvider.getLocalDateTimeNow());
// when
SseEmitter resultEmitter = notificationService.addClientAndSendNotification(mockAuthentication);
// then
verify(memberProvider).getMemberByAuthentication(mockAuthentication);
verify(sseEmitterRepository).save(mockMember, resultEmitter);
verify(notificationRepository).countByMemberAndIsReadIsFalse(mockMember);
verify(resultEmitter).send(notificationMessageDto);
verify(sseEmitterRepository, never()).remove(mockMember);
}
@Test
@DisplayName("읽지 않은 알림이 없을 때, 구독자만 등록하고 알림은 전송하지 않는다.")
void addClientAndSendNotificationHaveNotUnreadNotification() throws IOException {
// given
Authentication mockAuthentication = mock(Authentication.class);
Member mockMember = mock(Member.class);
SseEmitter realEmitter = new SseEmitter();
SseEmitter spyEmitter = spy(realEmitter);
given(memberProvider.getMemberByAuthentication(mockAuthentication)).willReturn(mockMember);
given(notificationRepository.countByMemberAndIsReadIsFalse(mockMember)).willReturn(0L);
given(notificationService.createSseEmitter(anyLong())).willReturn(spyEmitter);
// when
SseEmitter resultEmitter = notificationService.addClientAndSendNotification(mockAuthentication);
// then
verify(memberProvider).getMemberByAuthentication(mockAuthentication);
verify(sseEmitterRepository).save(mockMember, resultEmitter);
verify(notificationRepository).countByMemberAndIsReadIsFalse(mockMember);
verify(resultEmitter, never()).send(String.format(UNREAD_NOTIFICATION_FORMAT, any()));
verify(sseEmitterRepository, never()).remove(mockMember);
}
@ParameterizedTest
@ValueSource(longs = {1, 2, 10, 50, 100})
@DisplayName("알림 전송 중 예외가 발생하면 구독자를 제거한다.")
void addClientAndSendNotificationIoException(Long unreadNotificationCount) throws IOException {
// given
Authentication mockAuthentication = mock(Authentication.class);
Member mockMember = mock(Member.class);
SseEmitter realEmitter = new SseEmitter();
SseEmitter spyEmitter = spy(realEmitter);
given(timeProvider.getLocalDateTimeNow()).willReturn(LocalDateTime.of(2025, 1, 1, 0, 0, 0));
given(memberProvider.getMemberByAuthentication(mockAuthentication)).willReturn(mockMember);
given(notificationRepository.countByMemberAndIsReadIsFalse(mockMember)).willReturn(unreadNotificationCount);
given(notificationService.createSseEmitter(anyLong())).willReturn(spyEmitter);
String notificationMessage = String.format(UNREAD_NOTIFICATION_FORMAT, unreadNotificationCount);
NotificationMessageDto notificationMessageDto = new NotificationMessageDto(notificationMessage,
timeProvider.getLocalDateTimeNow());
doThrow(new IOException()).when(spyEmitter).send(notificationMessageDto);
// when // then
assertThatCode(() -> notificationService.addClientAndSendNotification(mockAuthentication))
.doesNotThrowAnyException();
verify(memberProvider).getMemberByAuthentication(mockAuthentication);
verify(sseEmitterRepository).save(mockMember, spyEmitter);
verify(notificationRepository).countByMemberAndIsReadIsFalse(mockMember);
verify(spyEmitter).send(notificationMessageDto);
verify(sseEmitterRepository).remove(mockMember);
}
@ParameterizedTest
@ValueSource(longs = {1, 2, 10, 50, 100})
@DisplayName("구독자가 이미 존재하는 경우 기존의 SSEmitter 를 반환합니다.")
void addClientAndSendNotificationIsExistMember(Long unreadNotificationCount) throws IOException {
// given
Authentication mockAuthentication = mock(Authentication.class);
Member mockMember = mock(Member.class);
SseEmitter realEmitter = new SseEmitter();
SseEmitter spyEmitter = spy(realEmitter);
given(timeProvider.getLocalDateTimeNow()).willReturn(LocalDateTime.of(2025, 1, 1, 0, 0, 0));
given(memberProvider.getMemberByAuthentication(mockAuthentication)).willReturn(mockMember);
given(sseEmitterRepository.save(mockMember, spyEmitter)).willReturn(spyEmitter);
given(sseEmitterRepository.existByMember(mockMember)).willReturn(true);
given(sseEmitterRepository.findByMemberId(mockMember)).willReturn(spyEmitter);
// when
SseEmitter sseEmitter = notificationService.addClientAndSendNotification(mockAuthentication);
// then
assertThat(sseEmitter).isEqualTo(spyEmitter);
}
@Test
@DisplayName("구독자에게 알림을 전송합니다.")
void sendNotification() throws IOException {
// given
Member mockMember = mock(Member.class);
NotificationMessageDto mockMessageDto = mock(NotificationMessageDto.class);
// SseEmitter를 모킹해줍니다.
SseEmitter mockEmitter = mock(SseEmitter.class);
given(sseEmitterRepository.findByMemberId(mockMember)).willReturn(mockEmitter);
// when
notificationService.sendNotification(mockMessageDto, mockMember);
verify(mockEmitter).send(mockMessageDto);
verify(sseEmitterRepository, never()).remove(mockMember);
}
@Test
@DisplayName("구독자에게 알림 전송 중 예외가 발생하면 구독자를 제거합니다.")
void sendNotificationIoException() throws IOException {
// given
Member mockMember = mock(Member.class);
NotificationMessageDto mockMessageDto = mock(NotificationMessageDto.class);
// SseEmitter를 모킹해줍니다.
SseEmitter mockEmitter = mock(SseEmitter.class);
given(sseEmitterRepository.findByMemberId(mockMember)).willReturn(mockEmitter);
doThrow(new IOException()).when(mockEmitter).send(mockMessageDto);
// when // then
assertThatCode(() -> notificationService.sendNotification(mockMessageDto, mockMember))
.doesNotThrowAnyException();
verify(mockEmitter).send(mockMessageDto);
verify(sseEmitterRepository).remove(mockMember);
}
}