Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,9 @@ public void disconnectAlarm(Long userId) {
alertRepository.delete(userId, emitterId);
sseEmitter.complete();
}

// 사용자가 온라인인지 오프라인인지 확인 (Use UserId)
public boolean isOnline(Long userId) {
return alertRepository.getEmitterId(userId).isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public ResponseResult saveRequestToRedis(Long requestUserId, Long emotionRecordI
.getUser()
.getUserId();

// 사용자가 오프라인이면 202 보내고 종료
if (!alertService.isOnline(responseUserId)) {
return new ResponseResult(ErrorCode.CHAT_REQUEST_SSE_FAILED, "Request is not sent.");
}

// 요청자와 응답자가 같은 경우
if (requestUserId.equals(responseUserId)) {
return new ResponseResult(400, "You can't chat with yourself.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ void setUp() {
@Test
@DisplayName("채팅 요청: Redis에 저장 성공")
void testSaveRequestToRedis_SUCCESS() {

// given
when(emotionRecordRepository.findById(emotionRecordId)).thenReturn(Optional.of(emotionRecord)); // 감정 기록 조회
when(userRepository.findByUserIdWithCache(requestUserId)).thenReturn(Optional.of(requestUser)); // 요청자 정보 조회
Expand All @@ -130,6 +131,9 @@ void testSaveRequestToRedis_SUCCESS() {
// when(alertService.send(eq(responseUserId), eq("alarm"), any(Alert.class)))
// .thenReturn(new ResponseResult(ErrorCode.SUCCESS)); // 알림 전송 mock

// 상대방이 온라인이라고 가정..
when(alertService.isOnline(responseUserId)).thenReturn(true);

// Redis 관련 mock 설정
ValueOperations<String, Object> valueOps = mock(ValueOperations.class); // ValueOperations 객체 생성
when(redisTemplate.opsForValue()).thenReturn(valueOps); // redisTemplate에서 valueOps 반환하도록 설정
Expand Down Expand Up @@ -171,6 +175,20 @@ void testDeleteRequestFromRedis_SUCCESS() {
verify(alertService).send(responseUserId, "cancel", "Chat request has been canceled.");
}

@Test
@DisplayName("상대방이 오프라인일 경우")
void testSaveRequestToRedis_Offline() {
// given
when(emotionRecordRepository.findById(emotionRecordId)).thenReturn(Optional.of(emotionRecord));
when(alertService.isOnline(responseUserId)).thenReturn(false); // 핵심만 남김

// when
ResponseResult result = chatRoomService.saveRequestToRedis(requestUserId, emotionRecordId);

// then
assertEquals(202, result.getCode());
}

@Test
@DisplayName("채팅 요청 거절")
void testRequestRejected_SUCCESS() {
Expand Down