Skip to content

Commit

Permalink
Merge pull request #123 from seniors-project/Feature/#117
Browse files Browse the repository at this point in the history
채팅 도메인 테스트 코드 작성 #117
  • Loading branch information
regchoi authored Dec 12, 2023
2 parents 1b386b9 + ff06226 commit 319ad3b
Show file tree
Hide file tree
Showing 6 changed files with 662 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.seniors.domain.chat.controller;

import com.seniors.config.security.CustomUserDetails;
import com.seniors.domain.chat.dto.ChatMessageDto;
import com.seniors.domain.chat.entity.ChatRoom;
import com.seniors.domain.chat.repository.ChatMessageRepository;
import com.seniors.domain.chat.repository.ChatRoomRepository;
import com.seniors.domain.config.WithMockCustomUser;
import com.seniors.domain.users.entity.Users;
import com.seniors.domain.users.repository.UsersRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.simp.stomp.StompFrameHandler;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession;
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.messaging.WebSocketStompClient;

import java.lang.reflect.Type;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;

@ActiveProfiles("dev")
@AutoConfigureMockMvc
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EnableWebSocketMessageBroker
@Slf4j
@WithMockCustomUser
class ChatMessageControllerTest {

@Autowired
private ChatRoomRepository chatRoomRepository;
@Autowired
private UsersRepository usersRepository;
@Autowired
private ChatMessageRepository chatMessageRepository;
private Authentication authentication;
private Users users;

@Autowired
private MockMvc mockMvc;

@Autowired
private WebApplicationContext webApplicationContext;
static final String WEBSOCKET_SUB_URI = "/sub/chat/room/";
static final String WEBSOCKET_PUB_URI = "/pub/chat/sendMessage";

@LocalServerPort
private int port;

@BeforeEach
void setUp() {
mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.apply(springSecurity())
.build();

authentication = SecurityContextHolder.getContext().getAuthentication();
CustomUserDetails customUserDetails = (CustomUserDetails) authentication.getPrincipal();

users = usersRepository.getOneUsers(customUserDetails.getUserId());
}
@Test
@DisplayName("채팅하기")
public void chatMessageSend() throws Exception {

// given
WebSocketStompClient stompClient = new WebSocketStompClient(new StandardWebSocketClient());
stompClient.setMessageConverter(new MappingJackson2MessageConverter());

StompSession stompSession = stompClient.connect("ws://localhost:" + port + "/api/chat", new StompSessionHandlerAdapter() {
}).get(1, TimeUnit.SECONDS);

ChatRoom chatRoom = new ChatRoom();
chatRoomRepository.save(chatRoom);
ChatMessageDto.ChatMessageTransDto chatMessageTransDto = ChatMessageDto.ChatMessageTransDto.of(chatRoom.getId(), users.getId(), "채팅 내용입니다.");

CompletableFuture<ChatMessageDto.ChatMessageTransDto> subscribeFuture = new CompletableFuture<>();

// when
stompSession.send(WEBSOCKET_PUB_URI, chatMessageTransDto);
stompSession.subscribe(WEBSOCKET_SUB_URI + chatMessageTransDto.getChatRoomId(), new StompFrameHandler() {
@Override
public Type getPayloadType(StompHeaders headers) {
return ChatMessageDto.ChatMessageTransDto.class;
}

@Override
public void handleFrame(StompHeaders headers, Object payload) {
subscribeFuture.complete((ChatMessageDto.ChatMessageTransDto) payload);
}
});

// then
assertEquals(chatMessageTransDto.getContent(), subscribeFuture.get(10, TimeUnit.SECONDS).getContent());
assertEquals(chatMessageTransDto.getContent(), chatMessageRepository.findAll().get(0).getContent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package com.seniors.domain.chat.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.seniors.common.constant.OAuthProvider;
import com.seniors.common.constant.ResultCode;
import com.seniors.config.security.CustomUserDetails;
import com.seniors.domain.chat.dto.ChatRoomDto;
import com.seniors.domain.chat.entity.ChatRoom;
import com.seniors.domain.chat.entity.ChatRoomMembers;
import com.seniors.domain.chat.repository.ChatRoomMembersRepository;
import com.seniors.domain.chat.repository.ChatRoomRepository;
import com.seniors.domain.chat.service.ChatRoomService;
import com.seniors.domain.config.WithMockCustomUser;
import com.seniors.domain.users.entity.Users;
import com.seniors.domain.users.repository.UsersRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import java.util.Random;

import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@Transactional
@ActiveProfiles("dev")
@AutoConfigureMockMvc
@ExtendWith(SpringExtension.class)
@SpringBootTest
@Slf4j
@WithMockCustomUser
class ChatRoomControllerTest {

@Autowired
private ObjectMapper objectMapper;
@Autowired
private MockMvc mockMvc;
@Autowired
private ChatRoomService chatRoomService;
@Autowired
private ChatRoomRepository chatRoomRepository;
@Autowired
private UsersRepository usersRepository;
@Autowired
private ChatRoomMembersRepository chatRoomMembersRepository;
private Authentication authentication;
private Users users;

@Autowired
private WebApplicationContext webApplicationContext;

@BeforeEach
void setUp() {

mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.apply(springSecurity())
.build();

authentication = SecurityContextHolder.getContext().getAuthentication();
CustomUserDetails customUserDetails = (CustomUserDetails) authentication.getPrincipal();

users = usersRepository.getOneUsers(customUserDetails.getUserId());
}

@Test
@DisplayName("채팅방 전체 조회")
void chatRoomList() throws Exception {

// given
Users users2 = Users.of("222", "[email protected]", "test2", OAuthProvider.KAKAO,
"male", "12-31", "20~29", "profileImageUrl");
Users users3 = Users.of("333", "[email protected]", "test3", OAuthProvider.KAKAO,
"male", "10-31", "30~39", "profileImageUrl");
try {
chatRoomService.addChatRoom(users.getId(), users2.getId());
chatRoomService.addChatRoom(users.getId(), users3.getId());
} catch (Exception e) {
}

// expected
mockMvc.perform(get("/api/chat/rooms")
.contentType(APPLICATION_JSON)
.principal(authentication)
)
.andExpect(status().isOk())
.andDo(print());
}

@Test
@DisplayName("채팅방 생성")
void chatRoomAdd() throws Exception {

// given
Users users2 = usersRepository.save(Users.of("222", "[email protected]", "test2", OAuthProvider.KAKAO,
"male", "12-31", "20~29", "profileImageUrl"));

ChatRoomDto.ChatRoomCreateDto chatRoomCreateDto = new ChatRoomDto.ChatRoomCreateDto();
chatRoomCreateDto.setChatUserId(users2.getId());
String json = objectMapper.writeValueAsString(chatRoomCreateDto);

// expected
mockMvc.perform(post("/api/chat/rooms")
.contentType(APPLICATION_JSON)
.content(json)
.principal(authentication)
)
.andExpect(status().isOk())
.andDo(print());
}
@Test
@DisplayName("채팅방 생성 실패")
void failedChatRoomAdd() throws Exception {

// given
Random random = new Random();
long randomNumber = random.nextLong() % 100L;
if (randomNumber < 0) {
randomNumber *= -1;
}

ChatRoomDto.ChatRoomCreateDto chatRoomCreateDto = new ChatRoomDto.ChatRoomCreateDto();
chatRoomCreateDto.setChatUserId(randomNumber);

String json = objectMapper.writeValueAsString(chatRoomCreateDto);

// expected
mockMvc.perform(post("/api/chat/rooms")
.contentType(APPLICATION_JSON)
.content(json)
.principal(authentication)
)
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.success").value(false))
.andExpect(jsonPath("$.code").value(ResultCode.NOT_FOUND.getCode()))
.andDo(print());
}

@Test
@DisplayName("채팅방 입장")
void chatRoomEnter() throws Exception {

// given
ChatRoom chatRoom = new ChatRoom();
chatRoomRepository.save(chatRoom);
chatRoomMembersRepository.save(ChatRoomMembers.of("user", chatRoom, users));

// expected
mockMvc.perform(get("/api/chat/rooms/{roomId}", chatRoom.getId())
.contentType(APPLICATION_JSON)
.principal(authentication)
).andExpect(status().isOk())
.andDo(print());
}

@Test
@DisplayName("채팅방 나가기")
void chatRoomExit() throws Exception {

// given
ChatRoom chatRoom = new ChatRoom();
chatRoomRepository.save(chatRoom);
chatRoomMembersRepository.save(ChatRoomMembers.of("user", chatRoom, users));

// expected
mockMvc.perform(delete("/api/chat/rooms/{roomId}", chatRoom.getId())
.contentType(APPLICATION_JSON)
.principal(authentication)
).andExpect(status().isOk())
.andDo(print());
}
}
Loading

0 comments on commit 319ad3b

Please sign in to comment.