create chats only together with the first message
- 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>
This commit is contained in:
@@ -5,10 +5,13 @@ 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;
|
||||
@@ -20,6 +23,7 @@ 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;
|
||||
|
||||
@@ -28,6 +32,7 @@ class ChatServiceTest {
|
||||
private ChatRepository chatRepository;
|
||||
private UserRepository userRepository;
|
||||
private PostRepository postRepository;
|
||||
private MessageRepository messageRepository;
|
||||
private ChatService chatService;
|
||||
|
||||
@BeforeEach
|
||||
@@ -35,7 +40,7 @@ class ChatServiceTest {
|
||||
chatRepository = mock(ChatRepository.class);
|
||||
userRepository = mock(UserRepository.class);
|
||||
postRepository = mock(PostRepository.class);
|
||||
MessageRepository messageRepository = mock(MessageRepository.class);
|
||||
messageRepository = mock(MessageRepository.class);
|
||||
chatService = new ChatService(chatRepository, messageRepository, mock(SimpMessagingTemplate.class),
|
||||
userRepository, postRepository);
|
||||
|
||||
@@ -47,6 +52,11 @@ class ChatServiceTest {
|
||||
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) {
|
||||
@@ -59,26 +69,46 @@ class ChatServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void createsChatAboutPostOfOtherUser() {
|
||||
void startChatCreatesChatTogetherWithFirstMessage() {
|
||||
postOwnedBy(41L, 19L);
|
||||
|
||||
ChatDto chat = chatService.getOrCreateChat(14L, 19L, 41L);
|
||||
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.getOrCreateChat(14L, 19L, 41L))
|
||||
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.getOrCreateChat(19L, 19L, 41L))
|
||||
assertThatThrownBy(() -> chatService.startChat(19L, 19L, 41L, "Здравствуйте"))
|
||||
.isInstanceOf(ApiException.class)
|
||||
.hasMessageContaining("самим собой");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user