Skip to content

Commit d1cb670

Browse files
authored
Merge pull request #221 from JECT-Study/develop
release: develop → main
2 parents e7ebc64 + e8abdcf commit d1cb670

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

src/main/java/com/ject/vs/chat/port/ChatService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.stereotype.Service;
2323
import org.springframework.transaction.annotation.Transactional;
2424

25+
import java.util.Comparator;
2526
import java.util.List;
2627

2728
@Service
@@ -98,6 +99,9 @@ public List<ChatListItemResult> getChatList(Long userId, VoteStatus status) {
9899
unreadCount
99100
);
100101
})
102+
.sorted(Comparator.comparing(
103+
ChatListItemResult::lastMessageAt,
104+
Comparator.nullsLast(Comparator.reverseOrder())))
101105
.toList();
102106
}
103107

src/unitTest/java/com/ject/vs/chat/port/ChatServiceTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import com.ject.vs.chat.domain.ChatRoomUnreadRepository;
77
import com.ject.vs.chat.exception.ChatForbiddenException;
88
import com.ject.vs.chat.exception.InvalidMessageException;
9+
import com.ject.vs.chat.port.in.dto.ChatListItemResult;
910
import com.ject.vs.chat.port.in.dto.MarkAsReadCommand;
1011
import com.ject.vs.chat.port.in.dto.MessagePageResult;
1112
import com.ject.vs.chat.port.in.dto.MessageResult;
1213
import com.ject.vs.chat.port.in.dto.SendMessageCommand;
14+
import com.ject.vs.vote.domain.VoteStatus;
1315
import com.ject.vs.user.domain.ImageColor;
1416
import com.ject.vs.user.domain.User;
1517
import com.ject.vs.user.port.in.UserQueryUseCase;
@@ -27,6 +29,7 @@
2729
import org.mockito.junit.jupiter.MockitoExtension;
2830
import org.springframework.data.domain.PageRequest;
2931

32+
import java.time.Instant;
3033
import java.util.List;
3134
import java.util.Optional;
3235

@@ -139,6 +142,57 @@ class markAsRead {
139142
}
140143
}
141144

145+
@Nested
146+
class getChatList {
147+
148+
@Test
149+
void 최근_메시지_시간_기준_내림차순으로_정렬한다() {
150+
// given
151+
Long userId = 1L;
152+
given(voteParticipationQueryUseCase.findAllVoteIdsByUserId(userId)).willReturn(List.of(10L, 20L, 30L));
153+
given(voteQueryUseCase.findAllVoteIdsByStatus(List.of(10L, 20L, 30L), VoteStatus.ONGOING))
154+
.willReturn(List.of(10L, 20L, 30L));
155+
156+
Instant endAt = Instant.parse("2026-12-31T00:00:00Z");
157+
given(voteQueryUseCase.getVoteChatSummary(10L))
158+
.willReturn(new VoteQueryUseCase.VoteChatSummary(10L, "t10", null, VoteStatus.ONGOING, endAt, "A", "B"));
159+
given(voteQueryUseCase.getVoteChatSummary(20L))
160+
.willReturn(new VoteQueryUseCase.VoteChatSummary(20L, "t20", null, VoteStatus.ONGOING, endAt, "A", "B"));
161+
given(voteQueryUseCase.getVoteChatSummary(30L))
162+
.willReturn(new VoteQueryUseCase.VoteChatSummary(30L, "t30", null, VoteStatus.ONGOING, endAt, "A", "B"));
163+
164+
given(voteParticipationQueryUseCase.countParticipantsByVoteId(any())).willReturn(1L);
165+
given(chatRoomUnreadRepository.findByIdUserIdAndIdVoteId(eq(userId), any())).willReturn(Optional.empty());
166+
given(chatMessageRepository.countByVoteId(any())).willReturn(0L);
167+
168+
ChatMessage oldMsg = mock(ChatMessage.class);
169+
ChatMessage midMsg = mock(ChatMessage.class);
170+
ChatMessage newMsg = mock(ChatMessage.class);
171+
172+
given(oldMsg.getContent()).willReturn("old");
173+
given(midMsg.getContent()).willReturn("mid");
174+
given(newMsg.getContent()).willReturn("new");
175+
given(oldMsg.getCreatedAt()).willReturn(Instant.parse("2026-01-01T00:00:00Z"));
176+
given(midMsg.getCreatedAt()).willReturn(Instant.parse("2026-01-02T00:00:00Z"));
177+
given(newMsg.getCreatedAt()).willReturn(Instant.parse("2026-01-03T00:00:00Z"));
178+
179+
given(chatMessageRepository.findFirstByVoteIdOrderByIdDesc(10L)).willReturn(Optional.of(oldMsg));
180+
given(chatMessageRepository.findFirstByVoteIdOrderByIdDesc(20L)).willReturn(Optional.of(midMsg));
181+
given(chatMessageRepository.findFirstByVoteIdOrderByIdDesc(30L)).willReturn(Optional.of(newMsg));
182+
183+
// when
184+
List<ChatListItemResult> result = chatService.getChatList(userId, VoteStatus.ONGOING);
185+
186+
// then
187+
assertThat(result).extracting(ChatListItemResult::voteId).containsExactly(30L, 20L, 10L);
188+
assertThat(result).extracting(ChatListItemResult::lastMessageAt)
189+
.containsExactly(
190+
Instant.parse("2026-01-03T00:00:00Z"),
191+
Instant.parse("2026-01-02T00:00:00Z"),
192+
Instant.parse("2026-01-01T00:00:00Z"));
193+
}
194+
}
195+
142196
@Nested
143197
class getMessages {
144198

0 commit comments

Comments
 (0)