forked from solid-connection/solid-connect-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServiceTest.java
More file actions
447 lines (364 loc) · 18.5 KB
/
ChatServiceTest.java
File metadata and controls
447 lines (364 loc) · 18.5 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package com.example.solidconnection.chat.service;
import static com.example.solidconnection.common.exception.ErrorCode.CHAT_PARTICIPANT_NOT_FOUND;
import static com.example.solidconnection.common.exception.ErrorCode.CHAT_PARTNER_NOT_FOUND;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode;
import static org.junit.jupiter.api.Assertions.assertAll;
import com.example.solidconnection.chat.domain.ChatAttachment;
import com.example.solidconnection.chat.domain.ChatMessage;
import com.example.solidconnection.chat.domain.ChatParticipant;
import com.example.solidconnection.chat.domain.ChatReadStatus;
import com.example.solidconnection.chat.domain.ChatRoom;
import com.example.solidconnection.chat.dto.ChatMessageResponse;
import com.example.solidconnection.chat.dto.ChatMessageSendRequest;
import com.example.solidconnection.chat.dto.ChatMessageSendResponse;
import com.example.solidconnection.chat.dto.ChatParticipantResponse;
import com.example.solidconnection.chat.dto.ChatRoomListResponse;
import com.example.solidconnection.chat.fixture.ChatAttachmentFixture;
import com.example.solidconnection.chat.fixture.ChatMessageFixture;
import com.example.solidconnection.chat.fixture.ChatParticipantFixture;
import com.example.solidconnection.chat.fixture.ChatReadStatusFixture;
import com.example.solidconnection.chat.fixture.ChatRoomFixture;
import com.example.solidconnection.chat.repository.ChatReadStatusRepositoryForTest;
import com.example.solidconnection.common.dto.SliceResponse;
import com.example.solidconnection.common.exception.CustomException;
import com.example.solidconnection.siteuser.domain.SiteUser;
import com.example.solidconnection.siteuser.fixture.SiteUserFixture;
import com.example.solidconnection.support.TestContainerSpringBootTest;
import java.time.ZonedDateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.BDDMockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.messaging.simp.SimpMessagingTemplate;
@TestContainerSpringBootTest
@DisplayName("채팅 서비스 테스트")
class ChatServiceTest {
@Autowired
private ChatService chatService;
@Autowired
private ChatReadStatusRepositoryForTest chatReadStatusRepositoryForTest;
@Autowired
private SiteUserFixture siteUserFixture;
@Autowired
private ChatRoomFixture chatRoomFixture;
@Autowired
private ChatParticipantFixture chatParticipantFixture;
@Autowired
private ChatMessageFixture chatMessageFixture;
@Autowired
private ChatReadStatusFixture chatReadStatusFixture;
@Autowired
private ChatAttachmentFixture chatAttachmentFixture;
@MockBean
private SimpMessagingTemplate simpMessagingTemplate;
private SiteUser user;
private SiteUser mentor1;
private SiteUser mentor2;
@BeforeEach
void setUp() {
user = siteUserFixture.사용자();
mentor1 = siteUserFixture.사용자(1, "mentor1");
mentor2 = siteUserFixture.사용자(2, "mentor2");
}
@Nested
class 채팅방_목록을_조회한다 {
@Test
void 채팅방이_없으면_빈_목록을_반환한다() {
// when
ChatRoomListResponse response = chatService.getChatRooms(user.getId());
// then
assertThat(response.chatRooms()).isEmpty();
}
@Test
void 최신_메시지_순으로_정렬되어_조회한다() {
// given
ChatRoom chatRoom1 = chatRoomFixture.채팅방(false);
chatParticipantFixture.참여자(user.getId(), chatRoom1);
chatParticipantFixture.참여자(mentor1.getId(), chatRoom1);
ChatMessage oldMessage = chatMessageFixture.메시지("오래된 메시지", mentor1.getId(), chatRoom1);
ChatRoom chatRoom2 = chatRoomFixture.채팅방(false);
chatParticipantFixture.참여자(user.getId(), chatRoom2);
chatParticipantFixture.참여자(mentor2.getId(), chatRoom2);
ChatMessage newMessage = chatMessageFixture.메시지("최신 메시지", mentor2.getId(), chatRoom2);
// when
ChatRoomListResponse response = chatService.getChatRooms(user.getId());
// then
assertAll(
() -> assertThat(response.chatRooms()).hasSize(2),
() -> assertThat(response.chatRooms().get(0).partner().partnerId()).isEqualTo(mentor2.getId()),
() -> assertThat(response.chatRooms().get(0).lastChatMessage()).isEqualTo(newMessage.getContent()),
() -> assertThat(response.chatRooms().get(1).partner().partnerId()).isEqualTo(mentor1.getId()),
() -> assertThat(response.chatRooms().get(1).lastChatMessage()).isEqualTo(oldMessage.getContent())
);
}
@Test
void 그룹_채팅방은_제외하고_1대1_채팅방만_조회한다() {
// given
ChatRoom oneOnOneRoom = chatRoomFixture.채팅방(false);
chatParticipantFixture.참여자(user.getId(), oneOnOneRoom);
chatParticipantFixture.참여자(mentor1.getId(), oneOnOneRoom);
ChatRoom groupRoom = chatRoomFixture.채팅방(true);
chatParticipantFixture.참여자(user.getId(), groupRoom);
chatParticipantFixture.참여자(mentor1.getId(), groupRoom);
chatParticipantFixture.참여자(mentor2.getId(), groupRoom);
// when
ChatRoomListResponse response = chatService.getChatRooms(user.getId());
// then
assertAll(
() -> assertThat(response.chatRooms()).hasSize(1),
() -> assertThat(response.chatRooms().get(0).id()).isEqualTo(oneOnOneRoom.getId())
);
}
@Test
void 채팅_상대방이_없으면_예외가_발생한다() {
// given
ChatRoom chatRoom = chatRoomFixture.채팅방(false);
chatParticipantFixture.참여자(user.getId(), chatRoom);
// when & then
assertThatCode(() -> chatService.getChatRooms(user.getId()))
.isInstanceOf(CustomException.class)
.hasMessage(CHAT_PARTNER_NOT_FOUND.getMessage());
}
}
@Nested
class 읽지_않은_메시지_수를_조회한다 {
private ChatRoom chatRoom;
private ChatParticipant participant;
@BeforeEach
void setUp() {
chatRoom = chatRoomFixture.채팅방(false);
participant = chatParticipantFixture.참여자(user.getId(), chatRoom);
chatParticipantFixture.참여자(mentor1.getId(), chatRoom);
}
@Test
void 읽음_상태가_없으면_모든_상대방_메시지를_카운팅한다() {
// given
chatMessageFixture.메시지("메시지1", mentor1.getId(), chatRoom);
chatMessageFixture.메시지("메시지2", mentor1.getId(), chatRoom);
// when
ChatRoomListResponse response = chatService.getChatRooms(user.getId());
// then
assertThat(response.chatRooms().get(0).unReadCount()).isEqualTo(2);
}
@Test
void 읽음_상태_이후_메시지만_읽지_않은_메시지로_카운팅한다() {
// given
chatMessageFixture.메시지("읽은 메시지", mentor1.getId(), chatRoom);
chatReadStatusFixture.읽음상태(chatRoom.getId(), participant.getId());
chatMessageFixture.메시지("읽지 않은 메시지1", mentor1.getId(), chatRoom);
chatMessageFixture.메시지("읽지 않은 메시지2", mentor1.getId(), chatRoom);
// when
ChatRoomListResponse response = chatService.getChatRooms(user.getId());
// then
assertThat(response.chatRooms().get(0).unReadCount()).isEqualTo(2);
}
}
@Nested
class 채팅_메시지를_조회한다 {
private static final int NO_NEXT_PAGE_NUMBER = -1;
private ChatRoom chatRoom;
private Pageable pageable;
@BeforeEach
void setUp() {
chatRoom = chatRoomFixture.채팅방(false);
chatParticipantFixture.참여자(user.getId(), chatRoom);
chatParticipantFixture.참여자(mentor1.getId(), chatRoom);
pageable = PageRequest.of(0, 20, Sort.by(Sort.Direction.DESC, "createdAt"));
}
@Test
void 메시지가_없는_채팅방에서_빈_목록을_반환한다() {
// when
SliceResponse<ChatMessageResponse> response = chatService.getChatMessages(user.getId(), chatRoom.getId(), pageable);
// then
assertAll(
() -> assertThat(response.content()).isEmpty(),
() -> assertThat(response.nextPageNumber()).isEqualTo(NO_NEXT_PAGE_NUMBER)
);
}
@Test
void 첨부파일이_없는_메시지들을_정상_조회한다() {
// given
ChatMessage message1 = chatMessageFixture.메시지("메시지1", mentor1.getId(), chatRoom);
ChatMessage message2 = chatMessageFixture.메시지("메시지2", user.getId(), chatRoom);
// when
SliceResponse<ChatMessageResponse> response = chatService.getChatMessages(user.getId(), chatRoom.getId(), pageable);
// then
assertAll(
() -> assertThat(response.content()).hasSize(2),
() -> assertThat(response.content().get(0).content()).isEqualTo(message2.getContent()),
() -> assertThat(response.content().get(0).senderId()).isEqualTo(user.getId()),
() -> assertThat(response.content().get(1).content()).isEqualTo(message1.getContent()),
() -> assertThat(response.content().get(1).senderId()).isEqualTo(mentor1.getId())
);
}
@Test
void 첨부파일이_있는_메시지를_정상_조회한다() {
// given
ChatMessage messageWithImage = chatMessageFixture.메시지("이미지", mentor1.getId(), chatRoom);
ChatAttachment imageAttachment = chatAttachmentFixture.첨부파일(
true,
"https://example.com/image.png",
"https://example.com/thumb.png",
messageWithImage
);
// when
SliceResponse<ChatMessageResponse> response = chatService.getChatMessages(user.getId(), chatRoom.getId(), pageable);
// then
assertAll(
() -> assertThat(response.content()).hasSize(1),
() -> assertThat(response.content().get(0).content()).isEqualTo(messageWithImage.getContent()),
() -> assertThat(response.content().get(0).attachments()).hasSize(1),
() -> assertThat(response.content().get(0).attachments().get(0).id()).isEqualTo(imageAttachment.getId())
);
}
@Test
void 페이징이_정상_작동한다() {
for (int i = 1; i <= 25; i++) {
chatMessageFixture.메시지("메시지" + i, (i % 2 == 0) ? user.getId() : mentor1.getId(), chatRoom);
}
Pageable firstPage = PageRequest.of(0, 20, Sort.by(Sort.Direction.DESC, "createdAt"));
Pageable secondPage = PageRequest.of(1, 20, Sort.by(Sort.Direction.DESC, "createdAt"));
// when
SliceResponse<ChatMessageResponse> firstResponse = chatService.getChatMessages(user.getId(), chatRoom.getId(), firstPage);
SliceResponse<ChatMessageResponse> secondResponse = chatService.getChatMessages(user.getId(), chatRoom.getId(), secondPage);
// then
assertAll(
() -> assertThat(firstResponse.nextPageNumber()).isEqualTo(2),
() -> assertThat(secondResponse.nextPageNumber()).isEqualTo(NO_NEXT_PAGE_NUMBER)
);
}
@Test
void 채팅방_참여자가_아니면_예외가_발생한다() {
// when & then
assertThatCode(() -> chatService.getChatMessages(mentor2.getId(), chatRoom.getId(), pageable))
.isInstanceOf(CustomException.class)
.hasMessage(CHAT_PARTICIPANT_NOT_FOUND.getMessage());
}
@Test
void 존재하지_않는_채팅방에_접근하면_예외가_발생한다() {
// given
long nonExistentRoomId = 999L;
// when & then
assertThatCode(() -> chatService.getChatMessages(user.getId(), nonExistentRoomId, pageable))
.isInstanceOf(CustomException.class)
.hasMessage(CHAT_PARTICIPANT_NOT_FOUND.getMessage());
}
}
@Nested
class 채팅방_파트너_정보를_조회한다 {
@Test
void 채팅방_파트너를_정상_조회한다() {
// given
ChatRoom chatRoom = chatRoomFixture.채팅방(false);
chatParticipantFixture.참여자(user.getId(), chatRoom);
chatParticipantFixture.참여자(mentor1.getId(), chatRoom);
// when
ChatParticipantResponse response = chatService.getChatPartner(user.getId(), chatRoom.getId());
// then
assertAll(
() -> assertThat(response.partnerId()).isEqualTo(mentor1.getId()),
() -> assertThat(response.nickname()).isEqualTo(mentor1.getNickname()),
() -> assertThat(response.profileUrl()).isEqualTo(mentor1.getProfileImageUrl())
);
}
}
@Nested
class 채팅_메시지_읽음을_처리한다 {
private ChatRoom chatRoom;
private ChatParticipant participant;
@BeforeEach
void setUp() {
chatRoom = chatRoomFixture.채팅방(false);
participant = chatParticipantFixture.참여자(user.getId(), chatRoom);
chatParticipantFixture.참여자(mentor1.getId(), chatRoom);
}
@Test
void 처음_읽음_처리_시_새로운_읽음_상태를_생성한다() {
// given
chatMessageFixture.메시지("읽지 않은 메시지1", mentor1.getId(), chatRoom);
chatMessageFixture.메시지("읽지 않은 메시지2", mentor1.getId(), chatRoom);
// when
chatService.markChatMessagesAsRead(user.getId(), chatRoom.getId());
// then
ChatReadStatus afterStatus = chatReadStatusRepositoryForTest
.findByChatRoomIdAndChatParticipantId(chatRoom.getId(), participant.getId())
.orElseThrow();
assertThat(afterStatus.getChatRoomId()).isEqualTo(chatRoom.getId());
}
@Test
void 기존_읽음_상태가_있으면_updatedAt을_갱신한다() {
// given
ChatReadStatus chatReadStatus = chatReadStatusFixture.읽음상태(chatRoom.getId(), participant.getId());
ZonedDateTime updatedAt = chatReadStatus.getUpdatedAt();
chatMessageFixture.메시지("새로운 메시지", mentor1.getId(), chatRoom);
// when
chatService.markChatMessagesAsRead(user.getId(), chatRoom.getId());
// then
ChatReadStatus updatedStatus = chatReadStatusRepositoryForTest
.findByChatRoomIdAndChatParticipantId(chatRoom.getId(), participant.getId())
.orElseThrow();
assertAll(
() -> assertThat(updatedStatus.getId()).isEqualTo(chatReadStatus.getId()),
() -> assertThat(updatedStatus.getUpdatedAt()).isAfter(updatedAt)
);
}
@Test
void 채팅방_참여자가_아니면_예외가_발생한다() {
// given
ChatRoom chatRoom = chatRoomFixture.채팅방(false);
chatParticipantFixture.참여자(user.getId(), chatRoom);
chatParticipantFixture.참여자(mentor1.getId(), chatRoom);
// when & then
assertThatCode(() -> chatService.markChatMessagesAsRead(mentor2.getId(), chatRoom.getId()))
.isInstanceOf(CustomException.class)
.hasMessage(CHAT_PARTICIPANT_NOT_FOUND.getMessage());
}
}
@Nested
class 채팅_메시지를_전송한다 {
private SiteUser sender;
private ChatParticipant senderParticipant;
private ChatRoom chatRoom;
@BeforeEach
void setUp() {
sender = siteUserFixture.사용자(111, "sender");
chatRoom = chatRoomFixture.채팅방(false);
senderParticipant = chatParticipantFixture.참여자(sender.getId(), chatRoom);
}
@Test
void 채팅방_참여자는_메시지를_전송할_수_있다() {
// given
final String content = "안녕하세요";
ChatMessageSendRequest request = new ChatMessageSendRequest(content);
// when
chatService.sendChatMessage(request, sender.getId(), chatRoom.getId());
// then
ArgumentCaptor<String> destinationCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<ChatMessageSendResponse> payloadCaptor = ArgumentCaptor.forClass(ChatMessageSendResponse.class);
BDDMockito.verify(simpMessagingTemplate).convertAndSend(destinationCaptor.capture(), payloadCaptor.capture());
assertAll(
() -> assertThat(destinationCaptor.getValue()).isEqualTo("/topic/chat/" + chatRoom.getId()),
() -> assertThat(payloadCaptor.getValue().content()).isEqualTo(content),
() -> assertThat(payloadCaptor.getValue().senderId()).isEqualTo(senderParticipant.getId())
);
}
@Test
void 채팅_참여자가_아니면_메시지를_전송할_수_없다() {
// given
SiteUser nonParticipant = siteUserFixture.사용자(333, "nonParticipant");
ChatMessageSendRequest request = new ChatMessageSendRequest("안녕하세요");
// when & then
assertThatCode(() -> chatService.sendChatMessage(request, nonParticipant.getId(), chatRoom.getId()))
.isInstanceOf(CustomException.class)
.hasMessage(CHAT_PARTICIPANT_NOT_FOUND.getMessage());
}
}
}