- GET /api/v1/chats/with/{otherUserId}?postId= finds an existing chat
(404 if there is none) and never creates one
- POST /api/v1/chats/with/{otherUserId}/messages?postId= with {content}
finds or creates the chat and saves the first message in one
transaction; same participant/post checks as before, WebSocket events
still sent after commit
- the old POST /with/{otherUserId} that created empty chats is removed
- chat list hides chats without messages (existing empty rows stay in DB)
- empty messages are rejected with 400
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
116 lines
4.1 KiB
Java
116 lines
4.1 KiB
Java
package com.krylov.refound.service;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
import static org.mockito.ArgumentMatchers.any;
|
|
import static org.mockito.ArgumentMatchers.anyLong;
|
|
import static org.mockito.Mockito.mock;
|
|
import static org.mockito.Mockito.never;
|
|
import static org.mockito.Mockito.verify;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
import com.krylov.refound.dto.ChatDto;
|
|
import com.krylov.refound.entity.Chat;
|
|
import com.krylov.refound.entity.Message;
|
|
import com.krylov.refound.entity.Post;
|
|
import com.krylov.refound.entity.User;
|
|
import com.krylov.refound.exception.ApiException;
|
|
import com.krylov.refound.repository.ChatRepository;
|
|
import com.krylov.refound.repository.MessageRepository;
|
|
import com.krylov.refound.repository.PostRepository;
|
|
import com.krylov.refound.repository.UserRepository;
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.mockito.ArgumentCaptor;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
|
|
|
class ChatServiceTest {
|
|
|
|
private ChatRepository chatRepository;
|
|
private UserRepository userRepository;
|
|
private PostRepository postRepository;
|
|
private MessageRepository messageRepository;
|
|
private ChatService chatService;
|
|
|
|
@BeforeEach
|
|
void setUp() {
|
|
chatRepository = mock(ChatRepository.class);
|
|
userRepository = mock(UserRepository.class);
|
|
postRepository = mock(PostRepository.class);
|
|
messageRepository = mock(MessageRepository.class);
|
|
chatService = new ChatService(chatRepository, messageRepository, mock(SimpMessagingTemplate.class),
|
|
userRepository, postRepository);
|
|
|
|
when(userRepository.existsById(anyLong())).thenReturn(true);
|
|
when(messageRepository.findFirstByChatIdOrderByCreatedAtDesc(any())).thenReturn(Optional.empty());
|
|
when(chatRepository.findByUsersAndPost(any(), any(), any())).thenReturn(List.of());
|
|
when(chatRepository.save(any(Chat.class))).thenAnswer(inv -> {
|
|
Chat chat = inv.getArgument(0);
|
|
chat.setId(100L);
|
|
return chat;
|
|
});
|
|
when(messageRepository.save(any(Message.class))).thenAnswer(inv -> {
|
|
Message message = inv.getArgument(0);
|
|
message.setId(500L);
|
|
return message;
|
|
});
|
|
}
|
|
|
|
private void postOwnedBy(long postId, long ownerId) {
|
|
User owner = new User();
|
|
owner.setId(ownerId);
|
|
Post post = new Post();
|
|
post.setId(postId);
|
|
post.setUser(owner);
|
|
when(postRepository.findById(postId)).thenReturn(Optional.of(post));
|
|
}
|
|
|
|
@Test
|
|
void startChatCreatesChatTogetherWithFirstMessage() {
|
|
postOwnedBy(41L, 19L);
|
|
|
|
ChatDto chat = chatService.startChat(14L, 19L, 41L, "Здравствуйте");
|
|
|
|
assertThat(chat.getPostId()).isEqualTo(41L);
|
|
assertThat(chat.getOtherUserId()).isEqualTo(19L);
|
|
ArgumentCaptor<Message> saved = ArgumentCaptor.forClass(Message.class);
|
|
verify(messageRepository).save(saved.capture());
|
|
assertThat(saved.getValue().getChatId()).isEqualTo(100L);
|
|
assertThat(saved.getValue().getContent()).isEqualTo("Здравствуйте");
|
|
}
|
|
|
|
@Test
|
|
void findChatDoesNotCreateChat() {
|
|
assertThat(chatService.findChat(14L, 19L, 41L)).isEmpty();
|
|
verify(chatRepository, never()).save(any());
|
|
}
|
|
|
|
@Test
|
|
void rejectsEmptyFirstMessageWithoutCreatingChat() {
|
|
postOwnedBy(41L, 19L);
|
|
|
|
assertThatThrownBy(() -> chatService.startChat(14L, 19L, 41L, " "))
|
|
.isInstanceOf(ApiException.class);
|
|
verify(chatRepository, never()).save(any());
|
|
}
|
|
|
|
@Test
|
|
void rejectsPostOfThirdUser() {
|
|
postOwnedBy(41L, 7L);
|
|
|
|
assertThatThrownBy(() -> chatService.startChat(14L, 19L, 41L, "Здравствуйте"))
|
|
.isInstanceOfSatisfying(ApiException.class, e -> assertThat(e.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST));
|
|
verify(chatRepository, never()).save(any());
|
|
}
|
|
|
|
@Test
|
|
void rejectsChatWithYourself() {
|
|
assertThatThrownBy(() -> chatService.startChat(19L, 19L, 41L, "Здравствуйте"))
|
|
.isInstanceOf(ApiException.class)
|
|
.hasMessageContaining("самим собой");
|
|
}
|
|
}
|