add ChatService tests for creating a chat about an ad
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,85 @@
|
|||||||
|
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.when;
|
||||||
|
|
||||||
|
import com.krylov.refound.dto.ChatDto;
|
||||||
|
import com.krylov.refound.entity.Chat;
|
||||||
|
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.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||||
|
|
||||||
|
class ChatServiceTest {
|
||||||
|
|
||||||
|
private ChatRepository chatRepository;
|
||||||
|
private UserRepository userRepository;
|
||||||
|
private PostRepository postRepository;
|
||||||
|
private ChatService chatService;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
chatRepository = mock(ChatRepository.class);
|
||||||
|
userRepository = mock(UserRepository.class);
|
||||||
|
postRepository = mock(PostRepository.class);
|
||||||
|
MessageRepository 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;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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 createsChatAboutPostOfOtherUser() {
|
||||||
|
postOwnedBy(41L, 19L);
|
||||||
|
|
||||||
|
ChatDto chat = chatService.getOrCreateChat(14L, 19L, 41L);
|
||||||
|
|
||||||
|
assertThat(chat.getPostId()).isEqualTo(41L);
|
||||||
|
assertThat(chat.getOtherUserId()).isEqualTo(19L);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectsPostOfThirdUser() {
|
||||||
|
postOwnedBy(41L, 7L);
|
||||||
|
|
||||||
|
assertThatThrownBy(() -> chatService.getOrCreateChat(14L, 19L, 41L))
|
||||||
|
.isInstanceOfSatisfying(ApiException.class, e -> assertThat(e.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void rejectsChatWithYourself() {
|
||||||
|
assertThatThrownBy(() -> chatService.getOrCreateChat(19L, 19L, 41L))
|
||||||
|
.isInstanceOf(ApiException.class)
|
||||||
|
.hasMessageContaining("самим собой");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user