diff --git a/spring-chatting-backend-server/src/test/java/chatting/chat/domain/participant/api/ParticipantControllerTest.java b/spring-chatting-backend-server/src/test/java/chatting/chat/domain/participant/api/ParticipantControllerTest.java new file mode 100644 index 0000000..b3c12b7 --- /dev/null +++ b/spring-chatting-backend-server/src/test/java/chatting/chat/domain/participant/api/ParticipantControllerTest.java @@ -0,0 +1,102 @@ +package chatting.chat.domain.participant.api; + +import static com.example.commondto.error.ErrorCode.CANNOT_FIND_ROOM; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import chatting.chat.domain.participant.service.ParticipantServiceImpl; +import chatting.chat.domain.user.api.UserController; +import chatting.chat.domain.user.entity.User; +import chatting.chat.domain.user.service.UserService; +import chatting.chat.web.error.GlobalExceptionHandler; +import chatting.chat.web.filter.UserContextInterceptor; +import chatting.chat.web.sessionCluster.redis.UserRedisSession; +import chatting.chat.web.sessionCluster.redis.UserRedisSessionRepository; +import com.example.commondto.error.CustomException; +import jakarta.servlet.http.Cookie; +import java.util.Optional; +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.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; +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; + +@ExtendWith({SpringExtension.class}) +@WebMvcTest(ParticipantController.class) +@ActiveProfiles("testAPI") +@Import(GlobalExceptionHandler.class) +@Slf4j +class ParticipantControllerTest { + @Autowired + private MockMvc mockMvc; + + @MockBean + private ParticipantServiceImpl participantService; + + @MockBean + private UserRedisSessionRepository userRedisSessionRepository; + + @Autowired + private UserContextInterceptor userContextInterceptor; + + @BeforeEach + public void setUp() { + this.mockMvc = MockMvcBuilders + .standaloneSetup(new ParticipantController(participantService)) + .setControllerAdvice(new GlobalExceptionHandler()) + .addInterceptors(userContextInterceptor) + .build(); + } + + + @Test + @DisplayName("채팅방에 참여 시 참여 성공") + void whenValidParticipantAdd_thenSuccessShouldBeReturned() throws Exception { + // given + String refreshToken = "validToken"; + String userId = "userId"; + UserRedisSession userRedisSession = new UserRedisSession(userId, refreshToken); + when(userRedisSessionRepository.findById(refreshToken)).thenReturn(Optional.of(userRedisSession)); + when(participantService.addParticipant(1L,userId)).thenReturn("success"); + + // when + then + mockMvc.perform(post("/participant").param("roomId","1").cookie(new Cookie("refreshToken", refreshToken))) + .andExpect(status().isOk()).andExpect(jsonPath("$", is("success"))); + } + + @Test + @DisplayName("채팅방이 존재하지 않을 때 참여 시 에러 반환") + void addParticipant() throws Exception { + // given + String refreshToken = "validToken"; + String userId = "userId"; + UserRedisSession userRedisSession = new UserRedisSession(userId, refreshToken); + when(userRedisSessionRepository.findById(refreshToken)).thenReturn(Optional.of(userRedisSession)); + when(participantService.addParticipant(1L,userId)).thenThrow(new CustomException(CANNOT_FIND_ROOM)); + + // when + then + mockMvc.perform(post("/participant").param("roomId","1").cookie(new Cookie("refreshToken", refreshToken))) + .andExpect(status().isNotFound()); + } + + @Test + void getParticipants() { + } + + @Test + void getParticipant() { + } +} \ No newline at end of file diff --git a/spring-chatting-backend-server/src/test/java/chatting/chat/domain/participant/service/ParticipantServiceImplTest.java b/spring-chatting-backend-server/src/test/java/chatting/chat/domain/participant/service/ParticipantServiceImplTest.java index 50999c6..d181a82 100644 --- a/spring-chatting-backend-server/src/test/java/chatting/chat/domain/participant/service/ParticipantServiceImplTest.java +++ b/spring-chatting-backend-server/src/test/java/chatting/chat/domain/participant/service/ParticipantServiceImplTest.java @@ -42,6 +42,11 @@ class ParticipantServiceImplTest extends Initializer { .userName("friendName") .userStatus("").build(); + private final User testNewUser = User.builder() + .userId("newUserId") + .userName("newUserName") + .userStatus("").build(); + @BeforeEach void setUpRemove() { userRepository.deleteAll(); @@ -136,4 +141,171 @@ void whenParicipantUserIsNotValid_thenCustomExceptionShouldBeReturned() throws C assertThat(customException.getErrorCode()).isEqualTo(ErrorCode.CANNOT_FIND_FRIEND); } + @Test + @DisplayName("채팅방 참여 시 채팅방에 존재하지 않으면 에러 반환") + void whenRoomIsNotValid_thenCustomExceptionShouldBeReturned() throws CustomException{ + // given + userService.save(testUser.getUserId(), testUser.getUserName(), testUser.getUserStatus()); + userService.save(testFriendUser.getUserId(), testFriendUser.getUserName(), testFriendUser.getUserStatus()); + UserContext.setUserId(testUser.getUserId()); // ThreadLocal 에 userId 저장 + + // 친구 추가 + friendService.save(testUser.getUserId(), testFriendUser.getUserId()); + + // when then + CustomException customException = assertThrows(CustomException.class, () -> { + participantService.addParticipant(1L, testUser.getUserId()); + }); + + assertThat(customException.getErrorCode()).isEqualTo(ErrorCode.CANNOT_FIND_ROOM); + } + + @Test + @DisplayName("채팅방에 참여 성공") + void whenValidParticipantAdd_thenSuccessShouldBeReturned() { + // given + // 유저 추가 + userService.save(testUser.getUserId(), testUser.getUserName(), testUser.getUserStatus()); + userService.save(testFriendUser.getUserId(), testFriendUser.getUserName(), testFriendUser.getUserStatus()); + UserContext.setUserId(testUser.getUserId()); // ThreadLocal 에 userId 저장 + + // 친구 추가 + friendService.save(testUser.getUserId(), testFriendUser.getUserId()); + + // 채팅방 생성 + RoomDto roomDto = userService.makeRoomWithFriends(RequestAddChatRoomDTO.builder() + .userId(testUser.getUserId()) + .friendIds(Arrays.asList(testFriendUser.getUserId())) + .build()); + + userService.save(testNewUser.getUserId(), testNewUser.getUserName(), testNewUser.getUserStatus()); + UserContext.setUserId(testNewUser.getUserId()); // ThreadLocal 에 userId 저장 + + // when + String success = participantService.addParticipant(roomDto.getRoomId(), testNewUser.getUserId()); + + // then + List participants = participantService.findParticipantByRoomId(roomDto.getRoomId()); + + participants.forEach(participantDto -> log.info("participantDto: {}", participantDto)); + assertThat(participants).hasSize(3); + assertThat(participants.get(0).getRoomId()).isEqualTo(roomDto.getRoomId()); + assertThat(participants.get(0).getUserDto().getUserId()).isEqualTo(testUser.getUserId()); + assertThat(participants.get(1).getRoomId()).isEqualTo(roomDto.getRoomId()); + assertThat(participants.get(1).getUserDto().getUserId()).isEqualTo(testFriendUser.getUserId()); + assertThat(participants.get(2).getRoomId()).isEqualTo(roomDto.getRoomId()); + assertThat(participants.get(2).getUserDto().getUserId()).isEqualTo(testNewUser.getUserId()); + } + + @Test + @DisplayName("채팅방에서 나가기 성공") + void whenValidParticipantRemove_thenSuccessShouldBeReturned() { + // given + // 유저 추가 + userService.save(testUser.getUserId(), testUser.getUserName(), testUser.getUserStatus()); + userService.save(testFriendUser.getUserId(), testFriendUser.getUserName(), testFriendUser.getUserStatus()); + UserContext.setUserId(testUser.getUserId()); // ThreadLocal 에 userId 저장 + + // 친구 추가 + friendService.save(testUser.getUserId(), testFriendUser.getUserId()); + + // 채팅방 생성 + RoomDto roomDto = userService.makeRoomWithFriends(RequestAddChatRoomDTO.builder() + .userId(testUser.getUserId()) + .friendIds(Arrays.asList(testFriendUser.getUserId())) + .build()); + + userService.save(testNewUser.getUserId(), testNewUser.getUserName(), testNewUser.getUserStatus()); + UserContext.setUserId(testNewUser.getUserId()); // ThreadLocal 에 userId 저장 + + // 채팅방 참여 + participantService.addParticipant(roomDto.getRoomId(), testNewUser.getUserId()); + + // when + String success = participantService.remove(roomDto.getRoomId(), testNewUser.getUserId()); + + // then + List participants = participantService.findParticipantByRoomId(roomDto.getRoomId()); + + participants.forEach(participantDto -> log.info("participantDto: {}", participantDto)); + assertThat(participants).hasSize(2); + assertThat(participants.get(0).getRoomId()).isEqualTo(roomDto.getRoomId()); + assertThat(participants.get(0).getUserDto().getUserId()).isEqualTo(testUser.getUserId()); + assertThat(participants.get(1).getRoomId()).isEqualTo(roomDto.getRoomId()); + assertThat(participants.get(1).getUserDto().getUserId()).isEqualTo(testFriendUser.getUserId()); + } + + @Test + @DisplayName("채팅방에 참여중인 유저 목록 조회 성공") + void whenValidParticipantGet_thenSuccessShouldBeReturned() { + // given + // 유저 추가 + userService.save(testUser.getUserId(), testUser.getUserName(), testUser.getUserStatus()); + userService.save(testFriendUser.getUserId(), testFriendUser.getUserName(), testFriendUser.getUserStatus()); + UserContext.setUserId(testUser.getUserId()); // ThreadLocal 에 userId 저장 + + // 친구 추가 + friendService.save(testUser.getUserId(), testFriendUser.getUserId()); + + // 채팅방 생성 + RoomDto roomDto = userService.makeRoomWithFriends(RequestAddChatRoomDTO.builder() + .userId(testUser.getUserId()) + .friendIds(Arrays.asList(testFriendUser.getUserId())) + .build()); + + userService.save(testNewUser.getUserId(), testNewUser.getUserName(), testNewUser.getUserStatus()); + UserContext.setUserId(testNewUser.getUserId()); // ThreadLocal 에 userId 저장 + + // 채팅방 참여 + participantService.addParticipant(roomDto.getRoomId(), testNewUser.getUserId()); + + // when + List participants = participantService.findParticipantByRoomId(roomDto.getRoomId()); + + // then + participants.forEach(participantDto -> log.info("participantDto: {}", participantDto)); + assertThat(participants).hasSize(3); + assertThat(participants.get(0).getRoomId()).isEqualTo(roomDto.getRoomId()); + assertThat(participants.get(0).getUserDto().getUserId()).isEqualTo(testUser.getUserId()); + assertThat(participants.get(1).getRoomId()).isEqualTo(roomDto.getRoomId()); + assertThat(participants.get(1).getUserDto().getUserId()).isEqualTo(testFriendUser.getUserId()); + assertThat(participants.get(2).getRoomId()).isEqualTo(roomDto.getRoomId()); + assertThat(participants.get(2).getUserDto().getUserId()).isEqualTo(testNewUser.getUserId()); + } + + @Test + @DisplayName("기존 채팅방 참여 시 내 채팅방 목록 조회 성공") + void whenValidParticipantGetMyRoom_thenSuccessShouldBeReturned() { + // given + // 유저 추가 + userService.save(testUser.getUserId(), testUser.getUserName(), testUser.getUserStatus()); + userService.save(testFriendUser.getUserId(), testFriendUser.getUserName(), testFriendUser.getUserStatus()); + UserContext.setUserId(testUser.getUserId()); // ThreadLocal 에 userId 저장 + + // 친구 추가 + friendService.save(testUser.getUserId(), testFriendUser.getUserId()); + + // 채팅방 생성 + RoomDto roomDto = userService.makeRoomWithFriends(RequestAddChatRoomDTO.builder() + .userId(testUser.getUserId()) + .friendIds(Arrays.asList(testFriendUser.getUserId())) + .build()); + + userService.save(testNewUser.getUserId(), testNewUser.getUserName(), testNewUser.getUserStatus()); + UserContext.setUserId(testNewUser.getUserId()); // ThreadLocal 에 userId 저장 + + // 채팅방 참여 + participantService.addParticipant(roomDto.getRoomId(), testNewUser.getUserId()); + + // when + List participants = participantService.findAllByUserId(testNewUser.getUserId()); + + // then + participants.forEach(participantDto -> log.info("participantDto: {}", participantDto)); + assertThat(participants).hasSize(1); + assertThat(participants.get(0).getRoomId()).isEqualTo(roomDto.getRoomId()); + assertThat(participants.get(0).getUserDto().getUserId()).isEqualTo(testNewUser.getUserId()); + } + + } \ No newline at end of file