From de3c373c1712b14c8b964c5740b10a311ae6b9ed Mon Sep 17 00:00:00 2001 From: hyunn522 Date: Tue, 10 Sep 2024 15:53:12 +0900 Subject: [PATCH 01/13] =?UTF-8?q?refactor:=20entity=EC=97=90=EC=84=9C=20re?= =?UTF-8?q?quest=20dto=20=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/space/space_spring/entity/ChatRoom.java | 8 ++------ .../space/space_spring/entity/document/ChatMessage.java | 9 ++++----- .../java/space/space_spring/service/ChatRoomService.java | 2 +- .../java/space/space_spring/service/ChattingService.java | 6 ++++-- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/main/java/space/space_spring/entity/ChatRoom.java b/src/main/java/space/space_spring/entity/ChatRoom.java index 0f3104da..57dd9c24 100644 --- a/src/main/java/space/space_spring/entity/ChatRoom.java +++ b/src/main/java/space/space_spring/entity/ChatRoom.java @@ -7,10 +7,6 @@ import lombok.Getter; import lombok.NoArgsConstructor; import org.hibernate.annotations.Comment; -import space.space_spring.dto.chat.request.CreateChatRoomRequest; - -import java.time.LocalDateTime; -import java.time.ZoneId; @Entity @Getter @@ -40,10 +36,10 @@ public class ChatRoom extends BaseEntity{ @Column(name = "chat_room_img") private String img; - public static ChatRoom of(Space space, CreateChatRoomRequest createChatRoomRequest, String chatRoomImgUrl) { + public static ChatRoom of(Space space, String chatRoomName, String chatRoomImgUrl) { return ChatRoom.builder() .space(space) - .name(createChatRoomRequest.getName()) + .name(chatRoomName) .img(chatRoomImgUrl) .build(); } diff --git a/src/main/java/space/space_spring/entity/document/ChatMessage.java b/src/main/java/space/space_spring/entity/document/ChatMessage.java index d55a8128..00643a0c 100644 --- a/src/main/java/space/space_spring/entity/document/ChatMessage.java +++ b/src/main/java/space/space_spring/entity/document/ChatMessage.java @@ -5,7 +5,6 @@ import org.springframework.data.annotation.Id; import org.springframework.data.annotation.TypeAlias; import org.springframework.data.mongodb.core.mapping.Document; -import space.space_spring.dto.chat.request.ChatMessageRequest; import space.space_spring.entity.enumStatus.ChatMessageType; import java.time.LocalDateTime; @@ -36,15 +35,15 @@ public class ChatMessage { private LocalDateTime createdAt; - public static ChatMessage of(ChatMessageRequest chatMessageRequest, Long chatRoomId, Long senderId, String senderName, String senderImg) { + public static ChatMessage of(HashMap content, Long chatRoomId, Long spaceId, Long senderId, String senderName, String senderImg, ChatMessageType messageType) { return ChatMessage.builder() - .content(chatMessageRequest.getContent()) + .content(content) .chatRoomId(chatRoomId) - .spaceId(chatMessageRequest.getSpaceId()) + .spaceId(spaceId) .senderId(senderId) .senderName(senderName) .senderImg(senderImg) - .messageType(chatMessageRequest.getMessageType()) + .messageType(messageType) .createdAt(LocalDateTime.now(ZoneId.of("Asia/Seoul"))) .build(); } diff --git a/src/main/java/space/space_spring/service/ChatRoomService.java b/src/main/java/space/space_spring/service/ChatRoomService.java index 3b3ccebc..cdf9a7e2 100644 --- a/src/main/java/space/space_spring/service/ChatRoomService.java +++ b/src/main/java/space/space_spring/service/ChatRoomService.java @@ -110,7 +110,7 @@ public CreateChatRoomResponse createChatRoom(Long userId, Long spaceId, CreateCh Space spaceBySpaceId = spaceUtils.findSpaceBySpaceId(spaceId); // TODO 3: chatRoom 생성 및 저장 - ChatRoom chatRoom = chatRoomDao.save(ChatRoom.of(spaceBySpaceId, createChatRoomRequest, chatRoomImgUrl)); + ChatRoom chatRoom = chatRoomDao.save(ChatRoom.of(spaceBySpaceId, createChatRoomRequest.getName(), chatRoomImgUrl)); // TODO 4: user_chatRoom 매핑 정보 저장 UserChatRoom userChatRoom = userChatRoomDao.save(UserChatRoom.of(chatRoom, userByUserId, LocalDateTime.now())); diff --git a/src/main/java/space/space_spring/service/ChattingService.java b/src/main/java/space/space_spring/service/ChattingService.java index 72b0e836..01f37b2f 100644 --- a/src/main/java/space/space_spring/service/ChattingService.java +++ b/src/main/java/space/space_spring/service/ChattingService.java @@ -58,11 +58,13 @@ public ChatMessageResponse sendChatMessage(Long senderId, ChatMessageRequest cha // TODO 4: DB에 메시지 저장 ChatMessage message = chattingDao.insert(ChatMessage.of( - chatMessageRequest, + chatMessageRequest.getContent(), chatRoomId, + chatMessageRequest.getSpaceId(), senderId, senderName, - senderProfileImg + senderProfileImg, + chatMessageRequest.getMessageType() )); return ChatMessageResponse.of(message); From 2269fee0e7385c18107c8a0374ba7dc13513a00a Mon Sep 17 00:00:00 2001 From: hyunn522 Date: Tue, 10 Sep 2024 15:53:52 +0900 Subject: [PATCH 02/13] =?UTF-8?q?refactor:=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EB=A9=94=EC=86=8C=EB=93=9C=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/space/space_spring/dao/chat/ChatRoomDao.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/space/space_spring/dao/chat/ChatRoomDao.java b/src/main/java/space/space_spring/dao/chat/ChatRoomDao.java index 9c6ab523..e66f7798 100644 --- a/src/main/java/space/space_spring/dao/chat/ChatRoomDao.java +++ b/src/main/java/space/space_spring/dao/chat/ChatRoomDao.java @@ -4,11 +4,7 @@ import org.springframework.stereotype.Repository; import space.space_spring.dao.chat.custom.ChatRoomDaoCustom; import space.space_spring.entity.ChatRoom; -import space.space_spring.entity.Space; - -import java.util.List; @Repository public interface ChatRoomDao extends JpaRepository, ChatRoomDaoCustom { - List findBySpace(Space space); } From 4c4015f76fb7ba0804f64fa92a72036f10df2293 Mon Sep 17 00:00:00 2001 From: hyunn522 Date: Tue, 10 Sep 2024 16:03:55 +0900 Subject: [PATCH 03/13] =?UTF-8?q?feat:=20repository=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80=20=EB=B0=8F?= =?UTF-8?q?=20=EC=98=81=EC=86=8D=EC=84=B1=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../space/space_spring/entity/ChatRoom.java | 2 +- .../space_spring/entity/UserChatRoom.java | 2 +- .../repository/ChatRoomDaoTest.java | 102 ++++++++++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 src/test/java/space/space_spring/repository/ChatRoomDaoTest.java diff --git a/src/main/java/space/space_spring/entity/ChatRoom.java b/src/main/java/space/space_spring/entity/ChatRoom.java index 57dd9c24..754d42df 100644 --- a/src/main/java/space/space_spring/entity/ChatRoom.java +++ b/src/main/java/space/space_spring/entity/ChatRoom.java @@ -23,7 +23,7 @@ public class ChatRoom extends BaseEntity{ private Long id; @Comment("채팅방이 속한 스페이스 ID") - @ManyToOne(fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name = "space_id") private Space space; diff --git a/src/main/java/space/space_spring/entity/UserChatRoom.java b/src/main/java/space/space_spring/entity/UserChatRoom.java index 4b36bbfc..20ddb513 100644 --- a/src/main/java/space/space_spring/entity/UserChatRoom.java +++ b/src/main/java/space/space_spring/entity/UserChatRoom.java @@ -30,7 +30,7 @@ public class UserChatRoom extends BaseEntity{ private ChatRoom chatRoom; @Comment("사용자 ID") - @ManyToOne(fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name = "user_id") private User user; diff --git a/src/test/java/space/space_spring/repository/ChatRoomDaoTest.java b/src/test/java/space/space_spring/repository/ChatRoomDaoTest.java new file mode 100644 index 00000000..d718f333 --- /dev/null +++ b/src/test/java/space/space_spring/repository/ChatRoomDaoTest.java @@ -0,0 +1,102 @@ +package space.space_spring.repository; + +import org.junit.jupiter.api.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.context.annotation.Import; +import space.space_spring.config.QueryDslConfig; +import space.space_spring.dao.chat.ChatRoomDao; +import space.space_spring.dao.chat.UserChatRoomDao; +import space.space_spring.entity.ChatRoom; +import space.space_spring.entity.Space; +import space.space_spring.entity.User; +import space.space_spring.entity.UserChatRoom; +import space.space_spring.entity.enumStatus.UserSignupType; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.*; + +@DataJpaTest +@Import({QueryDslConfig.class, QueryDslConfig.class}) +public class ChatRoomDaoTest { + + @Autowired + private ChatRoomDao chatRoomDao; + + @Autowired + private UserChatRoomDao userChatRoomDao; + + private Space testSpace; + + private ChatRoom testChatRoom1; + + private ChatRoom testChatRoom2; + + private User testUser; + + @BeforeEach + void 테스트_셋업() { + testUser = new User(); + + testUser.saveUser("testUser@test.com", "Asdf1234!", "testUser", UserSignupType.LOCAL); + + testSpace = new Space(); + + // testSpace에 속한 채팅방 생성 + testChatRoom1 = ChatRoom.of(testSpace, "testChatRoom1", ""); + testChatRoom2 = ChatRoom.of(testSpace, "testChatRoom2", ""); + } + + @Test + @DisplayName("채팅방_저장_테스트") + void 채팅방_저장_테스트() { + // given + + // when + ChatRoom savedTestChatRoom1 = chatRoomDao.save(testChatRoom1); + ChatRoom savedTestChatRoom2 = chatRoomDao.save(testChatRoom2); + + // then + assertThat(savedTestChatRoom1.getName()).isEqualTo(testChatRoom1.getName()); + assertThat(savedTestChatRoom2.getName()).isEqualTo(testChatRoom2.getName()); + } + + @Test + @DisplayName("id로_채팅방_조회_테스트") + void id로_채팅방_조회_테스트() { + // given + ChatRoom savedTestChatRoom1 = chatRoomDao.save(testChatRoom1); + ChatRoom savedTestChatRoom2 = chatRoomDao.save(testChatRoom2); + Long testChatRoom1Id = savedTestChatRoom1.getId(); + Long testChatRoom2Id = savedTestChatRoom2.getId(); + + // when + Optional chatRoom1byId = chatRoomDao.findById(testChatRoom1Id); + Optional chatRoom2byId = chatRoomDao.findById(testChatRoom2Id); + + // then + chatRoom1byId.ifPresent(chatRoom -> assertThat(chatRoom.getId()).isEqualTo(testChatRoom1Id)); + chatRoom2byId.ifPresent(chatRoom -> assertThat(chatRoom.getId()).isEqualTo(testChatRoom2Id)); + } + + @Test + @DisplayName("유저와_스페이스로_채팅방_조회_테스트") + void 유저와_스페이스로_채팅방_조회_테스트() { + // given + ChatRoom savedTestChatRoom1 = chatRoomDao.save(testChatRoom1); + ChatRoom savedTestChatRoom2 = chatRoomDao.save(testChatRoom2); + UserChatRoom userChatRoom1 = UserChatRoom.of(savedTestChatRoom1, testUser, LocalDateTime.now()); + UserChatRoom userChatRoom2 = UserChatRoom.of(savedTestChatRoom2, testUser, LocalDateTime.now()); + userChatRoomDao.save(userChatRoom1); + userChatRoomDao.save(userChatRoom2); + + // when + List chatRoomListByUser = chatRoomDao.findByUserAndSpace(testUser, testSpace); + + // then + assertThat(chatRoomListByUser.size()).isEqualTo(2); + } +} From 6f15bab82558c778be16f13fc922e54f41bf828c Mon Sep 17 00:00:00 2001 From: hyunn522 Date: Tue, 10 Sep 2024 17:11:32 +0900 Subject: [PATCH 04/13] =?UTF-8?q?refactor:=20=EA=B2=80=EC=A6=9D=20?= =?UTF-8?q?=EB=8C=80=EC=83=81=20name=20->=20id=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/space/space_spring/repository/ChatRoomDaoTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/space/space_spring/repository/ChatRoomDaoTest.java b/src/test/java/space/space_spring/repository/ChatRoomDaoTest.java index d718f333..1a02b7c6 100644 --- a/src/test/java/space/space_spring/repository/ChatRoomDaoTest.java +++ b/src/test/java/space/space_spring/repository/ChatRoomDaoTest.java @@ -45,7 +45,7 @@ public class ChatRoomDaoTest { testSpace = new Space(); - // testSpace에 속한 채팅방 생성 + // testSpace에 속한 chatRoom 엔티티 생성 testChatRoom1 = ChatRoom.of(testSpace, "testChatRoom1", ""); testChatRoom2 = ChatRoom.of(testSpace, "testChatRoom2", ""); } @@ -60,8 +60,8 @@ public class ChatRoomDaoTest { ChatRoom savedTestChatRoom2 = chatRoomDao.save(testChatRoom2); // then - assertThat(savedTestChatRoom1.getName()).isEqualTo(testChatRoom1.getName()); - assertThat(savedTestChatRoom2.getName()).isEqualTo(testChatRoom2.getName()); + assertThat(savedTestChatRoom1.getId()).isEqualTo(testChatRoom1.getId()); + assertThat(savedTestChatRoom2.getId()).isEqualTo(testChatRoom2.getId()); } @Test From 38859573703bb323ad18f0014f90fb2a086b14e4 Mon Sep 17 00:00:00 2001 From: hyunn522 Date: Tue, 10 Sep 2024 17:24:42 +0900 Subject: [PATCH 05/13] =?UTF-8?q?feat:=20UserChatRoomDao=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/UserChatRoomDaoTest.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/test/java/space/space_spring/repository/UserChatRoomDaoTest.java diff --git a/src/test/java/space/space_spring/repository/UserChatRoomDaoTest.java b/src/test/java/space/space_spring/repository/UserChatRoomDaoTest.java new file mode 100644 index 00000000..3d3ddad1 --- /dev/null +++ b/src/test/java/space/space_spring/repository/UserChatRoomDaoTest.java @@ -0,0 +1,107 @@ +package space.space_spring.repository; + +import org.junit.jupiter.api.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.context.annotation.Import; +import space.space_spring.config.QueryDslConfig; +import space.space_spring.dao.chat.ChatRoomDao; +import space.space_spring.dao.chat.UserChatRoomDao; +import space.space_spring.entity.ChatRoom; +import space.space_spring.entity.Space; +import space.space_spring.entity.User; +import space.space_spring.entity.UserChatRoom; +import space.space_spring.entity.enumStatus.UserSignupType; + +import java.time.LocalDateTime; +import java.util.List; + +import static org.assertj.core.api.Assertions.*; + +@DataJpaTest +@Import({QueryDslConfig.class, QueryDslConfig.class}) +@TestMethodOrder(value = MethodOrderer.OrderAnnotation.class) +public class UserChatRoomDaoTest { + + @Autowired + private UserChatRoomDao userChatRoomDao; + + @Autowired + private ChatRoomDao chatRoomDao; + + private User testUser1; + + private User testUser2; + + private UserChatRoom testUserChatRoom1; + + private UserChatRoom testUserChatRoom2; + + private ChatRoom testChatRoom; + + @BeforeEach + void 테스트_셋업() { + testUser1 = new User(); + testUser2 = new User(); + + testUser1.saveUser("testUser1@test.com", "Asdf1234!", "testUser1", UserSignupType.LOCAL); + testUser2.saveUser("testUser2@test.com", "Asdf1234!", "testUser2", UserSignupType.LOCAL); + + Space testSpace = new Space(); + + // testSpace에 속한 채팅방 생성 및 저장 + testChatRoom = ChatRoom.of(testSpace, "testChatRoom", ""); + chatRoomDao.save(testChatRoom); + + // testChatRoom에 대한 testUserChatRoom 엔티티 생성 + testUserChatRoom1 = UserChatRoom.of(testChatRoom, testUser1, LocalDateTime.now()); + testUserChatRoom2 = UserChatRoom.of(testChatRoom, testUser2, LocalDateTime.now()); + } + + @Test + @DisplayName("유저채팅방_저장_테스트") + void 유저채팅방_저장_테스트() { + // given + + // when + UserChatRoom savedTestUserChatRoom1 = userChatRoomDao.save(testUserChatRoom1); + UserChatRoom savedTestUserChatRoom2 = userChatRoomDao.save(testUserChatRoom2); + + // then + assertThat(savedTestUserChatRoom1.getId()).isEqualTo(testUserChatRoom1.getId()); + assertThat(savedTestUserChatRoom2.getId()).isEqualTo(testUserChatRoom2.getId()); + + assertThat(savedTestUserChatRoom1.getUser().getUserId()).isEqualTo(testUser1.getUserId()); + assertThat(savedTestUserChatRoom2.getUser().getUserId()).isEqualTo(testUser2.getUserId()); + } + + @Test + @DisplayName("채팅방으로_유저채팅방_조회_테스트") + void 채팅방으로_유저채팅방_저장_테스트() { + // given + userChatRoomDao.save(testUserChatRoom1); + userChatRoomDao.save(testUserChatRoom2); + + // when + List userChatRoomList = userChatRoomDao.findByChatRoom(testChatRoom); + + // then + assertThat(userChatRoomList.size()).isEqualTo(2); + } + + @Test + @DisplayName("유저와_채팅방으로_유저채팅방_저장_테스트") + void 유저와_채팅방으로_유저채팅방_저장_테스트() { + // given + userChatRoomDao.save(testUserChatRoom1); + userChatRoomDao.save(testUserChatRoom2); + + // when + UserChatRoom userChatRoom1 = userChatRoomDao.findByUserAndChatRoom(testUser1, testChatRoom); + UserChatRoom userChatRoom2 = userChatRoomDao.findByUserAndChatRoom(testUser2, testChatRoom); + + // then + assertThat(userChatRoom1.getId()).isEqualTo(testUserChatRoom1.getId()); + assertThat(userChatRoom2.getId()).isEqualTo(testUserChatRoom2.getId()); + } +} From 8344f554d6284eb408dfbb27e8df0f19ef510f4f Mon Sep 17 00:00:00 2001 From: hyunn522 Date: Wed, 11 Sep 2024 13:43:14 +0900 Subject: [PATCH 06/13] =?UTF-8?q?refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EB=B3=80=EC=88=98=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../space/space_spring/repository/ChatRoomDaoTest.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/test/java/space/space_spring/repository/ChatRoomDaoTest.java b/src/test/java/space/space_spring/repository/ChatRoomDaoTest.java index 1a02b7c6..4748b831 100644 --- a/src/test/java/space/space_spring/repository/ChatRoomDaoTest.java +++ b/src/test/java/space/space_spring/repository/ChatRoomDaoTest.java @@ -70,16 +70,14 @@ public class ChatRoomDaoTest { // given ChatRoom savedTestChatRoom1 = chatRoomDao.save(testChatRoom1); ChatRoom savedTestChatRoom2 = chatRoomDao.save(testChatRoom2); - Long testChatRoom1Id = savedTestChatRoom1.getId(); - Long testChatRoom2Id = savedTestChatRoom2.getId(); // when - Optional chatRoom1byId = chatRoomDao.findById(testChatRoom1Id); - Optional chatRoom2byId = chatRoomDao.findById(testChatRoom2Id); + Optional chatRoom1byId = chatRoomDao.findById(savedTestChatRoom1.getId()); + Optional chatRoom2byId = chatRoomDao.findById(savedTestChatRoom2.getId()); // then - chatRoom1byId.ifPresent(chatRoom -> assertThat(chatRoom.getId()).isEqualTo(testChatRoom1Id)); - chatRoom2byId.ifPresent(chatRoom -> assertThat(chatRoom.getId()).isEqualTo(testChatRoom2Id)); + chatRoom1byId.ifPresent(chatRoom -> assertThat(chatRoom.getId()).isEqualTo(savedTestChatRoom1.getId())); + chatRoom2byId.ifPresent(chatRoom -> assertThat(chatRoom.getId()).isEqualTo(savedTestChatRoom2.getId())); } @Test From f6cc7f5809c289a3de7ad2d0a69d77e03fe61e4f Mon Sep 17 00:00:00 2001 From: hyunn522 Date: Wed, 11 Sep 2024 13:44:14 +0900 Subject: [PATCH 07/13] =?UTF-8?q?fix:=20=EC=B1=84=ED=8C=85=EB=B0=A9=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EC=8B=9C=20=EB=A9=A4=EB=B2=84=20=EA=B0=81?= =?UTF-8?q?=EA=B0=81=EC=97=90=20=EB=8C=80=ED=95=9C=20userChatRoom=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/space/space_spring/service/ChatRoomService.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/space/space_spring/service/ChatRoomService.java b/src/main/java/space/space_spring/service/ChatRoomService.java index cdf9a7e2..831a421c 100644 --- a/src/main/java/space/space_spring/service/ChatRoomService.java +++ b/src/main/java/space/space_spring/service/ChatRoomService.java @@ -113,7 +113,11 @@ public CreateChatRoomResponse createChatRoom(Long userId, Long spaceId, CreateCh ChatRoom chatRoom = chatRoomDao.save(ChatRoom.of(spaceBySpaceId, createChatRoomRequest.getName(), chatRoomImgUrl)); // TODO 4: user_chatRoom 매핑 정보 저장 - UserChatRoom userChatRoom = userChatRoomDao.save(UserChatRoom.of(chatRoom, userByUserId, LocalDateTime.now())); + userChatRoomDao.save(UserChatRoom.of(chatRoom, userByUserId, LocalDateTime.now())); + for (Long id : createChatRoomRequest.getMemberList()) { + User user = userUtils.findUserByUserId(id); + userChatRoomDao.save(UserChatRoom.of(chatRoom, user, LocalDateTime.now())); + } // TODO 5: chatroom id 반환 return CreateChatRoomResponse.of(chatRoom.getId()); From cf32ad636d071387fe062fffde12bb0d488ad1e6 Mon Sep 17 00:00:00 2001 From: hyunn522 Date: Thu, 12 Sep 2024 14:38:22 +0900 Subject: [PATCH 08/13] =?UTF-8?q?refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/space/space_spring/controller/ChatRoomController.java | 2 +- src/main/java/space/space_spring/service/ChatRoomService.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/space/space_spring/controller/ChatRoomController.java b/src/main/java/space/space_spring/controller/ChatRoomController.java index aeccf610..9434092e 100644 --- a/src/main/java/space/space_spring/controller/ChatRoomController.java +++ b/src/main/java/space/space_spring/controller/ChatRoomController.java @@ -71,7 +71,7 @@ public BaseResponse updateLastReadTime( @JwtLoginAuth Long userId, @PathVariable Long spaceId, @PathVariable Long chatRoomId) { - return new BaseResponse<>(chatRoomService.updateLastReadTime(userId, spaceId, chatRoomId)); + return new BaseResponse<>(chatRoomService.updateLastReadTime(userId, chatRoomId)); } /** diff --git a/src/main/java/space/space_spring/service/ChatRoomService.java b/src/main/java/space/space_spring/service/ChatRoomService.java index 831a421c..24b17393 100644 --- a/src/main/java/space/space_spring/service/ChatRoomService.java +++ b/src/main/java/space/space_spring/service/ChatRoomService.java @@ -156,7 +156,7 @@ public ReadChatRoomMemberResponse readChatRoomMembers(Long spaceId, Long chatRoo } @Transactional - public ChatSuccessResponse updateLastReadTime(Long userId, Long spaceId, Long chatRoomId) { + public ChatSuccessResponse updateLastReadTime(Long userId, Long chatRoomId) { User userByUserId = userUtils.findUserByUserId(userId); ChatRoom chatRoomByChatRoomId = chatRoomDao.findById(chatRoomId) .orElseThrow(() -> new CustomException(CHATROOM_NOT_EXIST)); From 1631f85fea81defe82c15fb3f8f9aaa0b15f9a52 Mon Sep 17 00:00:00 2001 From: hyunn522 Date: Thu, 12 Sep 2024 14:51:34 +0900 Subject: [PATCH 09/13] =?UTF-8?q?feat:=20ChatRoomService=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ChatRoomServiceTest.java | 326 ++++++++++++++++++ 1 file changed, 326 insertions(+) create mode 100644 src/test/java/space/space_spring/service/ChatRoomServiceTest.java diff --git a/src/test/java/space/space_spring/service/ChatRoomServiceTest.java b/src/test/java/space/space_spring/service/ChatRoomServiceTest.java new file mode 100644 index 00000000..ff3b8ac5 --- /dev/null +++ b/src/test/java/space/space_spring/service/ChatRoomServiceTest.java @@ -0,0 +1,326 @@ +package space.space_spring.service; + +import org.junit.jupiter.api.*; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.util.ReflectionTestUtils; +import space.space_spring.dao.UserSpaceDao; +import space.space_spring.dao.chat.ChatRoomDao; +import space.space_spring.dao.chat.ChattingDao; +import space.space_spring.dao.chat.UserChatRoomDao; +import space.space_spring.dto.chat.request.CreateChatRoomRequest; +import space.space_spring.dto.chat.request.JoinChatRoomRequest; +import space.space_spring.dto.chat.response.ChatSuccessResponse; +import space.space_spring.dto.chat.response.CreateChatRoomResponse; +import space.space_spring.dto.chat.response.ReadChatRoomMemberResponse; +import space.space_spring.dto.chat.response.ReadChatRoomResponse; +import space.space_spring.entity.*; +import space.space_spring.entity.enumStatus.UserSignupType; +import space.space_spring.entity.enumStatus.UserSpaceAuth; +import space.space_spring.util.space.SpaceUtils; +import space.space_spring.util.user.UserUtils; + +import java.nio.charset.StandardCharsets; +import java.time.LocalDateTime; +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +class ChatRoomServiceTest { + + @InjectMocks + private ChatRoomService chatRoomService; + + @Mock + private ChatRoomDao chatRoomDao; + + @Mock + private UserUtils userUtils; + + @Mock + private SpaceUtils spaceUtils; + + @Mock + private UserSpaceDao userSpaceDao; + + @Mock + private ChattingDao chattingDao; + + @Mock + private UserChatRoomDao userChatRoomDao; + + private User user1; + + private User user2; + + private User user3; + + private Space testSpace; + + private UserSpace testUserSpace1; + + private UserSpace testUserSpace2; + + private UserSpace testUserSpace3; + + private ChatRoom chatRoom1; + + private ChatRoom chatRoom2; + + private UserChatRoom userChatRoom1; + + private UserChatRoom userChatRoom2; + + private UserChatRoom userChatRoom1ByUser2; + + private UserChatRoom userChatRoom1ByUser3; + + private CreateChatRoomRequest createChatRoomRequest1; + + private CreateChatRoomRequest createChatRoomRequest2; + + private JoinChatRoomRequest joinChatRoomRequest; + + @BeforeEach + void 채팅방_테스트_설정() { + /** + * <관리자인 user1은 chatRoom1, chatRoom2 생성> + * + */ + user1 = new User(); + user1.saveUser("user1@test.com", "Asdf1234!", "user1", UserSignupType.LOCAL); + ReflectionTestUtils.setField(user1, "userId", 0L); + lenient().when(userUtils.findUserByUserId(0L)).thenReturn(user1); + + user2 = new User(); + user2.saveUser("user2@test.com", "Asdf1234!", "user2", UserSignupType.LOCAL); + ReflectionTestUtils.setField(user2, "userId", 1L); + lenient().when(userUtils.findUserByUserId(1L)).thenReturn(user2); + + user3 = new User(); + user3.saveUser("user3@test.com", "Asdf1234!", "user3", UserSignupType.LOCAL); + ReflectionTestUtils.setField(user3, "userId", 2L); + lenient().when(userUtils.findUserByUserId(2L)).thenReturn(user3); + + testSpace = new Space(); + testSpace.saveSpace("testSpace", ""); + ReflectionTestUtils.setField(testSpace, "spaceId", 0L); + lenient().when(spaceUtils.findSpaceBySpaceId(0L)).thenReturn(testSpace); + + testUserSpace1 = new UserSpace(); + testUserSpace2 = new UserSpace(); + testUserSpace3 = new UserSpace(); + testUserSpace1.createUserSpace(user1, testSpace, UserSpaceAuth.MANAGER); + testUserSpace2.createUserSpace(user2, testSpace, UserSpaceAuth.NORMAL); + testUserSpace3.createUserSpace(user3, testSpace, UserSpaceAuth.NORMAL); + + lenient().when(chatRoomDao.save(any(ChatRoom.class))).thenAnswer(invocationOnMock -> { + ChatRoom savedChatRoom = invocationOnMock.getArgument(0); + if ("chatRoom1".equals(savedChatRoom.getName())) { + chatRoom1 = savedChatRoom; + ReflectionTestUtils.setField(chatRoom1, "id", 0L); + ReflectionTestUtils.setField(chatRoom1, "status", "ACTIVE"); + ReflectionTestUtils.setField(chatRoom1, "createdAt", LocalDateTime.now()); + return chatRoom1; + } else if ("chatRoom2".equals(savedChatRoom.getName())) { + chatRoom2 = savedChatRoom; + ReflectionTestUtils.setField(chatRoom2, "id", 1L); + ReflectionTestUtils.setField(chatRoom2, "status", "ACTIVE"); + ReflectionTestUtils.setField(chatRoom2, "createdAt", LocalDateTime.now()); + return chatRoom2; + } + return null; + }); + + // chatRoom1, chatRoom2 생성 시 사용할 request + MockMultipartFile mockImgFile = new MockMultipartFile("mockImgFile", "test.png", "png", "test file".getBytes(StandardCharsets.UTF_8) ); + createChatRoomRequest1 = new CreateChatRoomRequest(); + createChatRoomRequest1.setName("chatRoom1"); + createChatRoomRequest1.setImg(mockImgFile); + createChatRoomRequest1.setMemberList(List.of(1L)); + + createChatRoomRequest2 = new CreateChatRoomRequest(); + createChatRoomRequest2.setName("chatRoom2"); + createChatRoomRequest2.setImg(mockImgFile); + createChatRoomRequest2.setMemberList(List.of()); + + // user3 초대 시 사용할 request + joinChatRoomRequest = new JoinChatRoomRequest(); + ReflectionTestUtils.setField(joinChatRoomRequest, "memberList", List.of(2L)); + + lenient().when(userChatRoomDao.save(any(UserChatRoom.class))).thenAnswer(invocationOnMock -> { + UserChatRoom savedChatRoom = invocationOnMock.getArgument(0); + if ("chatRoom1".equals(savedChatRoom.getChatRoom().getName())) { + if ("user1".equals(savedChatRoom.getUser().getUserName())) { + userChatRoom1 = savedChatRoom; + ReflectionTestUtils.setField(userChatRoom1, "id", 0L); + ReflectionTestUtils.setField(userChatRoom1, "status", "ACTIVE"); + ReflectionTestUtils.setField(userChatRoom1, "createdAt", LocalDateTime.now()); + return userChatRoom1; + } else if ("user2".equals(savedChatRoom.getUser().getUserName())) { + userChatRoom1ByUser2 = savedChatRoom; + ReflectionTestUtils.setField(userChatRoom1ByUser2, "id", 1L); + ReflectionTestUtils.setField(userChatRoom1ByUser2, "status", "ACTIVE"); + ReflectionTestUtils.setField(userChatRoom1ByUser2, "createdAt", LocalDateTime.now()); + return userChatRoom1ByUser2; + } else { + userChatRoom1ByUser3 = savedChatRoom; + ReflectionTestUtils.setField(userChatRoom1ByUser3, "id", 2L); + ReflectionTestUtils.setField(userChatRoom1ByUser3, "status", "ACTIVE"); + ReflectionTestUtils.setField(userChatRoom1ByUser3, "createdAt", LocalDateTime.now()); + return userChatRoom1ByUser3; + } + } else if ("chatRoom2".equals(savedChatRoom.getChatRoom().getName())) { + userChatRoom2 = savedChatRoom; + ReflectionTestUtils.setField(userChatRoom2, "id", 3L); + ReflectionTestUtils.setField(userChatRoom2, "status", "ACTIVE"); + ReflectionTestUtils.setField(userChatRoom2, "createdAt", LocalDateTime.now()); + return userChatRoom2; + } + return null; + }); + } + + @Test + @DisplayName("특정_스페이스_내의_채팅방_생성_테스트") + void 특정_스페이스_내의_채팅방_생성_테스트() { + // given + + // when + CreateChatRoomResponse createdChatRoom1 = chatRoomService.createChatRoom(user1.getUserId(), testSpace.getSpaceId(), createChatRoomRequest1, ""); + CreateChatRoomResponse createdChatRoom2 = chatRoomService.createChatRoom(user1.getUserId(), testSpace.getSpaceId(), createChatRoomRequest2, ""); + + // then + assertThat(createdChatRoom1.getChatRoomId()).isEqualTo(0L); + assertThat(createdChatRoom2.getChatRoomId()).isEqualTo(1L); + } + + @Test + @DisplayName("특정_스페이스_내의_전체_채팅방_조회_테스트") + void 특정_스페이스_내의_전체_채팅방_조회_테스트() { + // given + chatRoomService.createChatRoom(user1.getUserId(), testSpace.getSpaceId(), createChatRoomRequest1, ""); + chatRoomService.createChatRoom(user1.getUserId(), testSpace.getSpaceId(), createChatRoomRequest2, ""); + + lenient().when(chattingDao.findTopByChatRoomIdOrderByCreatedAtDesc(chatRoom1.getId())).thenReturn(null); // 마지막 메시지가 없다고 가정 + lenient().when(chattingDao.countByChatRoomIdAndCreatedAtBetween(chatRoom1.getId(), LocalDateTime.now(), LocalDateTime.now())).thenReturn(0); + when(chatRoomDao.findByUserAndSpace(user1, testSpace)).thenReturn(List.of(chatRoom1, chatRoom2)); + when(userChatRoomDao.findByUserAndChatRoom(user1, chatRoom1)).thenReturn(userChatRoom1); + + lenient().when(chattingDao.findTopByChatRoomIdOrderByCreatedAtDesc(chatRoom2.getId())).thenReturn(null); // 마지막 메시지가 없다고 가정 + lenient().when(chattingDao.countByChatRoomIdAndCreatedAtBetween(chatRoom2.getId(), LocalDateTime.now(), LocalDateTime.now())).thenReturn(0); + when(userChatRoomDao.findByUserAndChatRoom(user1, chatRoom2)).thenReturn(userChatRoom2); + + // when + ReadChatRoomResponse readChatRoomResponse = chatRoomService.readChatRooms(user1.getUserId(), testSpace.getSpaceId()); + + // then + assertThat(readChatRoomResponse.getChatRoomList().size()).isEqualTo(2); + } + + @Test + @DisplayName("특정_채팅방의_전체_멤버_조회_테스트") + void 특정_채팅방의_전체_멤버_조회_테스트() { + // given + chatRoomService.createChatRoom(user1.getUserId(), testSpace.getSpaceId(), createChatRoomRequest1, ""); + chatRoomService.createChatRoom(user1.getUserId(), testSpace.getSpaceId(), createChatRoomRequest2, ""); + + when(chatRoomDao.findById(chatRoom1.getId())).thenReturn(Optional.ofNullable(chatRoom1)); + when(userChatRoomDao.findByChatRoom(chatRoom1)).thenReturn(List.of(userChatRoom1, userChatRoom1ByUser2)); + when(userSpaceDao.findUserSpaceByUserAndSpace(user1, testSpace)).thenReturn(Optional.ofNullable(testUserSpace1)); + + when(chatRoomDao.findById(chatRoom2.getId())).thenReturn(Optional.ofNullable(chatRoom2)); + when(userChatRoomDao.findByChatRoom(chatRoom2)).thenReturn(List.of(userChatRoom2)); + when(userSpaceDao.findUserSpaceByUserAndSpace(user2, testSpace)).thenReturn(Optional.ofNullable(testUserSpace2)); + + // when + ReadChatRoomMemberResponse readChatRoomMemberResponse1 = chatRoomService.readChatRoomMembers(testSpace.getSpaceId(), chatRoom1.getId()); + ReadChatRoomMemberResponse readChatRoomMemberResponse2 = chatRoomService.readChatRoomMembers(testSpace.getSpaceId(), chatRoom2.getId()); + + // then + assertThat(readChatRoomMemberResponse1.getUserList().size()).isEqualTo(2); + assertThat(readChatRoomMemberResponse1.getUserList().get(0).getUserId()).isEqualTo(0L); + assertThat(readChatRoomMemberResponse1.getUserList().get(1).getUserId()).isEqualTo(1L); + assertThat(readChatRoomMemberResponse2.getUserList().size()).isEqualTo(1); + assertThat(readChatRoomMemberResponse2.getUserList().get(0).getUserId()).isEqualTo(0L); + } + + @Test + @DisplayName("특정_채팅방으로의_멤버_초대_테스트") + void 특정_채팅방으로의_멤버_초대_테스트() { + // given + chatRoomService.createChatRoom(user1.getUserId(), testSpace.getSpaceId(), createChatRoomRequest1, ""); + when(chatRoomDao.findById(chatRoom1.getId())).thenReturn(Optional.ofNullable(chatRoom1)); + when(userChatRoomDao.findByChatRoom(chatRoom1)).thenReturn(List.of(userChatRoom1, userChatRoom1ByUser2)); + + // when + ChatSuccessResponse chatSuccessResponse = chatRoomService.joinChatRoom(chatRoom1.getId(), joinChatRoomRequest); + + // then + assertThat(chatSuccessResponse.isSuccess()).isEqualTo(true); + } + + @Test + @DisplayName("특정_채팅방에서_특정_유저가_마지막으로_읽은_시간_수정_테스트") + void 특정_채팅방에서_특정_유저가_마지막으로_읽은_시간_수정_테스트() { + // given + chatRoomService.createChatRoom(user1.getUserId(), testSpace.getSpaceId(), createChatRoomRequest1, ""); + when(chatRoomDao.findById(chatRoom1.getId())).thenReturn(Optional.ofNullable(chatRoom1)); + when(userChatRoomDao.findByUserAndChatRoom(user1, chatRoom1)).thenReturn(userChatRoom1); + + // when + ChatSuccessResponse chatSuccessResponse = chatRoomService.updateLastReadTime(user1.getUserId(), chatRoom1.getId()); + + // then + assertThat(chatSuccessResponse.isSuccess()).isEqualTo(true); + } + + @Test + @DisplayName("특정_채팅방_이름_수정_테스트") + void 특정_채팅방_이름_수정_테스트() { + // given + chatRoomService.createChatRoom(user1.getUserId(), testSpace.getSpaceId(), createChatRoomRequest1, ""); + when(chatRoomDao.findById(chatRoom1.getId())).thenReturn(Optional.ofNullable(chatRoom1)); + + // when + ChatSuccessResponse chatSuccessResponse = chatRoomService.modifyChatRoomName(chatRoom1.getId(), "newChatRoom1"); + + // then + assertThat(chatSuccessResponse.isSuccess()).isEqualTo(true); + } + + @Test + @DisplayName("특정_채팅방_나가기_테스트") + void 특정_채팅방_나가기_테스트() { + // given + chatRoomService.createChatRoom(user1.getUserId(), testSpace.getSpaceId(), createChatRoomRequest1, ""); + when(chatRoomDao.findById(chatRoom1.getId())).thenReturn(Optional.ofNullable(chatRoom1)); + when(userChatRoomDao.findByUserAndChatRoom(user2, chatRoom1)).thenReturn(userChatRoom1ByUser2); + + // when + ChatSuccessResponse chatSuccessResponse = chatRoomService.exitChatRoom(user2.getUserId(), chatRoom1.getId()); + + // then + assertThat(chatSuccessResponse.isSuccess()).isEqualTo(true); + } + + @Test + @DisplayName("특정_채팅방_삭제_테스트") + void 특정_채팅방_삭제_테스트() { + // given + chatRoomService.createChatRoom(user1.getUserId(), testSpace.getSpaceId(), createChatRoomRequest1, ""); + when(chatRoomDao.findById(chatRoom1.getId())).thenReturn(Optional.ofNullable(chatRoom1)); + + // when + ChatSuccessResponse chatSuccessResponse = chatRoomService.deleteChatRoom(chatRoom1.getId()); + + // then + assertThat(chatSuccessResponse.isSuccess()).isEqualTo(true); + } +} From 16782982adfe045cbdf1063c98ec34540ba1908b Mon Sep 17 00:00:00 2001 From: hyunn522 Date: Thu, 12 Sep 2024 15:47:33 +0900 Subject: [PATCH 10/13] =?UTF-8?q?refactor:=20=ED=97=AC=ED=8D=BC=20?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=EB=A5=BC=20=ED=86=B5=ED=95=9C=20?= =?UTF-8?q?=EC=B1=85=EC=9E=84=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/ChatRoomServiceTest.java | 100 ++++++++++-------- 1 file changed, 56 insertions(+), 44 deletions(-) diff --git a/src/test/java/space/space_spring/service/ChatRoomServiceTest.java b/src/test/java/space/space_spring/service/ChatRoomServiceTest.java index ff3b8ac5..6b39b9f8 100644 --- a/src/test/java/space/space_spring/service/ChatRoomServiceTest.java +++ b/src/test/java/space/space_spring/service/ChatRoomServiceTest.java @@ -25,6 +25,7 @@ import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; +import java.time.ZoneId; import java.util.List; import java.util.Optional; @@ -87,26 +88,17 @@ class ChatRoomServiceTest { private JoinChatRoomRequest joinChatRoomRequest; + private LocalDateTime mockTime; + @BeforeEach void 채팅방_테스트_설정() { /** * <관리자인 user1은 chatRoom1, chatRoom2 생성> * */ - user1 = new User(); - user1.saveUser("user1@test.com", "Asdf1234!", "user1", UserSignupType.LOCAL); - ReflectionTestUtils.setField(user1, "userId", 0L); - lenient().when(userUtils.findUserByUserId(0L)).thenReturn(user1); - - user2 = new User(); - user2.saveUser("user2@test.com", "Asdf1234!", "user2", UserSignupType.LOCAL); - ReflectionTestUtils.setField(user2, "userId", 1L); - lenient().when(userUtils.findUserByUserId(1L)).thenReturn(user2); - - user3 = new User(); - user3.saveUser("user3@test.com", "Asdf1234!", "user3", UserSignupType.LOCAL); - ReflectionTestUtils.setField(user3, "userId", 2L); - lenient().when(userUtils.findUserByUserId(2L)).thenReturn(user3); + user1 = createUser("user1@test.com", 0L); + user2 = createUser("user2@test.com", 1L); + user3 = createUser("user3@test.com", 2L); testSpace = new Space(); testSpace.saveSpace("testSpace", ""); @@ -116,23 +108,19 @@ class ChatRoomServiceTest { testUserSpace1 = new UserSpace(); testUserSpace2 = new UserSpace(); testUserSpace3 = new UserSpace(); - testUserSpace1.createUserSpace(user1, testSpace, UserSpaceAuth.MANAGER); - testUserSpace2.createUserSpace(user2, testSpace, UserSpaceAuth.NORMAL); + testUserSpace1.createUserSpace(this.user1, testSpace, UserSpaceAuth.MANAGER); + testUserSpace2.createUserSpace(this.user2, testSpace, UserSpaceAuth.NORMAL); testUserSpace3.createUserSpace(user3, testSpace, UserSpaceAuth.NORMAL); + mockTime = LocalDateTime.now(); + lenient().when(chatRoomDao.save(any(ChatRoom.class))).thenAnswer(invocationOnMock -> { ChatRoom savedChatRoom = invocationOnMock.getArgument(0); if ("chatRoom1".equals(savedChatRoom.getName())) { - chatRoom1 = savedChatRoom; - ReflectionTestUtils.setField(chatRoom1, "id", 0L); - ReflectionTestUtils.setField(chatRoom1, "status", "ACTIVE"); - ReflectionTestUtils.setField(chatRoom1, "createdAt", LocalDateTime.now()); + chatRoom1 = createChatRoom("chatRoom1", 0L); return chatRoom1; } else if ("chatRoom2".equals(savedChatRoom.getName())) { - chatRoom2 = savedChatRoom; - ReflectionTestUtils.setField(chatRoom2, "id", 1L); - ReflectionTestUtils.setField(chatRoom2, "status", "ACTIVE"); - ReflectionTestUtils.setField(chatRoom2, "createdAt", LocalDateTime.now()); + chatRoom2 = createChatRoom("chatRoom2", 1L); return chatRoom2; } return null; @@ -158,29 +146,17 @@ class ChatRoomServiceTest { UserChatRoom savedChatRoom = invocationOnMock.getArgument(0); if ("chatRoom1".equals(savedChatRoom.getChatRoom().getName())) { if ("user1".equals(savedChatRoom.getUser().getUserName())) { - userChatRoom1 = savedChatRoom; - ReflectionTestUtils.setField(userChatRoom1, "id", 0L); - ReflectionTestUtils.setField(userChatRoom1, "status", "ACTIVE"); - ReflectionTestUtils.setField(userChatRoom1, "createdAt", LocalDateTime.now()); + userChatRoom1 = createUserChatRoom(0L, chatRoom1, user1); return userChatRoom1; } else if ("user2".equals(savedChatRoom.getUser().getUserName())) { - userChatRoom1ByUser2 = savedChatRoom; - ReflectionTestUtils.setField(userChatRoom1ByUser2, "id", 1L); - ReflectionTestUtils.setField(userChatRoom1ByUser2, "status", "ACTIVE"); - ReflectionTestUtils.setField(userChatRoom1ByUser2, "createdAt", LocalDateTime.now()); + userChatRoom1ByUser2 = createUserChatRoom(1L, chatRoom1, user2); return userChatRoom1ByUser2; } else { - userChatRoom1ByUser3 = savedChatRoom; - ReflectionTestUtils.setField(userChatRoom1ByUser3, "id", 2L); - ReflectionTestUtils.setField(userChatRoom1ByUser3, "status", "ACTIVE"); - ReflectionTestUtils.setField(userChatRoom1ByUser3, "createdAt", LocalDateTime.now()); + userChatRoom1ByUser3 = createUserChatRoom(2L, chatRoom1, user3); return userChatRoom1ByUser3; } } else if ("chatRoom2".equals(savedChatRoom.getChatRoom().getName())) { - userChatRoom2 = savedChatRoom; - ReflectionTestUtils.setField(userChatRoom2, "id", 3L); - ReflectionTestUtils.setField(userChatRoom2, "status", "ACTIVE"); - ReflectionTestUtils.setField(userChatRoom2, "createdAt", LocalDateTime.now()); + userChatRoom2 = createUserChatRoom(3L, chatRoom2, user1); return userChatRoom2; } return null; @@ -208,13 +184,13 @@ class ChatRoomServiceTest { chatRoomService.createChatRoom(user1.getUserId(), testSpace.getSpaceId(), createChatRoomRequest1, ""); chatRoomService.createChatRoom(user1.getUserId(), testSpace.getSpaceId(), createChatRoomRequest2, ""); - lenient().when(chattingDao.findTopByChatRoomIdOrderByCreatedAtDesc(chatRoom1.getId())).thenReturn(null); // 마지막 메시지가 없다고 가정 - lenient().when(chattingDao.countByChatRoomIdAndCreatedAtBetween(chatRoom1.getId(), LocalDateTime.now(), LocalDateTime.now())).thenReturn(0); + when(chattingDao.findTopByChatRoomIdOrderByCreatedAtDesc(chatRoom1.getId())).thenReturn(null); // 마지막 메시지가 없다고 가정 + when(chattingDao.countByChatRoomIdAndCreatedAtBetween(chatRoom1.getId(), convertToKoreaTime(mockTime), convertToKoreaTime(mockTime))).thenReturn(0); when(chatRoomDao.findByUserAndSpace(user1, testSpace)).thenReturn(List.of(chatRoom1, chatRoom2)); when(userChatRoomDao.findByUserAndChatRoom(user1, chatRoom1)).thenReturn(userChatRoom1); - lenient().when(chattingDao.findTopByChatRoomIdOrderByCreatedAtDesc(chatRoom2.getId())).thenReturn(null); // 마지막 메시지가 없다고 가정 - lenient().when(chattingDao.countByChatRoomIdAndCreatedAtBetween(chatRoom2.getId(), LocalDateTime.now(), LocalDateTime.now())).thenReturn(0); + when(chattingDao.findTopByChatRoomIdOrderByCreatedAtDesc(chatRoom2.getId())).thenReturn(null); // 마지막 메시지가 없다고 가정 + when(chattingDao.countByChatRoomIdAndCreatedAtBetween(chatRoom2.getId(), convertToKoreaTime(mockTime), convertToKoreaTime(mockTime))).thenReturn(0); when(userChatRoomDao.findByUserAndChatRoom(user1, chatRoom2)).thenReturn(userChatRoom2); // when @@ -222,6 +198,8 @@ class ChatRoomServiceTest { // then assertThat(readChatRoomResponse.getChatRoomList().size()).isEqualTo(2); + assertThat(readChatRoomResponse.getChatRoomList().get(0).getName()).isEqualTo("chatRoom1"); + assertThat(readChatRoomResponse.getChatRoomList().get(1).getName()).isEqualTo("chatRoom2"); } @Test @@ -323,4 +301,38 @@ class ChatRoomServiceTest { // then assertThat(chatSuccessResponse.isSuccess()).isEqualTo(true); } + + private User createUser(String email, Long userId) { + User user = new User(); + user.saveUser(email, "Asdf1234!", email.split("@")[0], UserSignupType.LOCAL); + ReflectionTestUtils.setField(user, "userId", userId); + lenient().when(userUtils.findUserByUserId(userId)).thenReturn(user); + return user; + } + + private ChatRoom createChatRoom(String name, Long id) { + ChatRoom chatRoom = new ChatRoom(); + ReflectionTestUtils.setField(chatRoom, "id", id); + ReflectionTestUtils.setField(chatRoom, "status", "ACTIVE"); + ReflectionTestUtils.setField(chatRoom, "createdAt", mockTime); + ReflectionTestUtils.setField(chatRoom, "name", name); + return chatRoom; + } + + private UserChatRoom createUserChatRoom(Long id, ChatRoom chatRoom, User user) { + UserChatRoom userChatRoom = new UserChatRoom(); + ReflectionTestUtils.setField(userChatRoom, "id", id); + ReflectionTestUtils.setField(userChatRoom, "status", "ACTIVE"); + ReflectionTestUtils.setField(userChatRoom, "createdAt", mockTime); + ReflectionTestUtils.setField(userChatRoom, "chatRoom", chatRoom); + ReflectionTestUtils.setField(userChatRoom, "user", user); + ReflectionTestUtils.setField(userChatRoom, "lastReadTime", mockTime); + return userChatRoom; + } + + private LocalDateTime convertToKoreaTime(LocalDateTime time) { + return time.atZone(ZoneId.of("UTC")) + .withZoneSameInstant(ZoneId.of("Asia/Seoul")) + .toLocalDateTime(); + } } From e89bf4a5682f823019660a8a7f0f730130d9957c Mon Sep 17 00:00:00 2001 From: hyunn522 Date: Fri, 13 Sep 2024 09:02:41 +0900 Subject: [PATCH 11/13] =?UTF-8?q?refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=A3=BC=EC=9E=85=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/space/space_spring/controller/ChatRoomController.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/space/space_spring/controller/ChatRoomController.java b/src/main/java/space/space_spring/controller/ChatRoomController.java index 9434092e..50392f3d 100644 --- a/src/main/java/space/space_spring/controller/ChatRoomController.java +++ b/src/main/java/space/space_spring/controller/ChatRoomController.java @@ -16,7 +16,6 @@ import space.space_spring.response.BaseResponse; import space.space_spring.service.ChatRoomService; import space.space_spring.service.S3Uploader; -import space.space_spring.util.userSpace.UserSpaceUtils; import java.io.IOException; @@ -30,7 +29,6 @@ public class ChatRoomController { private final ChatRoomService chatRoomService; private final S3Uploader s3Uploader; - private final UserSpaceUtils userSpaceUtils; /** * 모든 채팅방 정보 조회 From 083c5d6621b143d6d52515b92af802696df1ca96 Mon Sep 17 00:00:00 2001 From: hyunn522 Date: Fri, 13 Sep 2024 18:30:06 +0900 Subject: [PATCH 12/13] =?UTF-8?q?refactor:=20=EC=83=9D=EC=84=B1=EC=9E=90?= =?UTF-8?q?=20builder=20=ED=8C=A8=ED=84=B4=EC=9C=BC=EB=A1=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/request/CreateChatRoomRequest.java | 4 ++-- .../dto/chat/request/JoinChatRoomRequest.java | 6 +++++ .../service/ChatRoomServiceTest.java | 22 ++++++++++--------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/main/java/space/space_spring/dto/chat/request/CreateChatRoomRequest.java b/src/main/java/space/space_spring/dto/chat/request/CreateChatRoomRequest.java index c492d0b9..e8143551 100644 --- a/src/main/java/space/space_spring/dto/chat/request/CreateChatRoomRequest.java +++ b/src/main/java/space/space_spring/dto/chat/request/CreateChatRoomRequest.java @@ -4,8 +4,8 @@ import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; +import lombok.Builder; import lombok.Getter; -import lombok.NoArgsConstructor; import lombok.Setter; import org.springframework.web.multipart.MultipartFile; @@ -13,7 +13,7 @@ @Getter @Setter -@NoArgsConstructor +@Builder public class CreateChatRoomRequest { @Size(min = 2, max = 15, message = "채팅방 이름은 2자 이상, 15자 이내의 문자열이어야 합니다.") diff --git a/src/main/java/space/space_spring/dto/chat/request/JoinChatRoomRequest.java b/src/main/java/space/space_spring/dto/chat/request/JoinChatRoomRequest.java index 63d41259..5b6e3d53 100644 --- a/src/main/java/space/space_spring/dto/chat/request/JoinChatRoomRequest.java +++ b/src/main/java/space/space_spring/dto/chat/request/JoinChatRoomRequest.java @@ -1,11 +1,17 @@ package space.space_spring.dto.chat.request; import jakarta.validation.constraints.NotEmpty; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; +import lombok.NoArgsConstructor; import java.util.List; @Getter +@Builder +@NoArgsConstructor /* controller단 테스트 위해 적용*/ +@AllArgsConstructor public class JoinChatRoomRequest { @NotEmpty(message = "1명 이상의 멤버를 초대해야 합니다.") diff --git a/src/test/java/space/space_spring/service/ChatRoomServiceTest.java b/src/test/java/space/space_spring/service/ChatRoomServiceTest.java index 6b39b9f8..97facaf5 100644 --- a/src/test/java/space/space_spring/service/ChatRoomServiceTest.java +++ b/src/test/java/space/space_spring/service/ChatRoomServiceTest.java @@ -128,19 +128,21 @@ class ChatRoomServiceTest { // chatRoom1, chatRoom2 생성 시 사용할 request MockMultipartFile mockImgFile = new MockMultipartFile("mockImgFile", "test.png", "png", "test file".getBytes(StandardCharsets.UTF_8) ); - createChatRoomRequest1 = new CreateChatRoomRequest(); - createChatRoomRequest1.setName("chatRoom1"); - createChatRoomRequest1.setImg(mockImgFile); - createChatRoomRequest1.setMemberList(List.of(1L)); - createChatRoomRequest2 = new CreateChatRoomRequest(); - createChatRoomRequest2.setName("chatRoom2"); - createChatRoomRequest2.setImg(mockImgFile); - createChatRoomRequest2.setMemberList(List.of()); + createChatRoomRequest1 = CreateChatRoomRequest.builder() + .name("chatRoom1") + .img(mockImgFile) + .memberList(List.of(1L)) + .build(); + + createChatRoomRequest2 = CreateChatRoomRequest.builder() + .name("chatRoom2") + .img(mockImgFile) + .memberList(List.of()) + .build(); // user3 초대 시 사용할 request - joinChatRoomRequest = new JoinChatRoomRequest(); - ReflectionTestUtils.setField(joinChatRoomRequest, "memberList", List.of(2L)); + joinChatRoomRequest = JoinChatRoomRequest.builder().memberList(List.of(2L)).build(); lenient().when(userChatRoomDao.save(any(UserChatRoom.class))).thenAnswer(invocationOnMock -> { UserChatRoom savedChatRoom = invocationOnMock.getArgument(0); From ccbd06cbfe91c020dc272eebda9bca043fbef24d Mon Sep 17 00:00:00 2001 From: hyunn522 Date: Fri, 13 Sep 2024 18:30:42 +0900 Subject: [PATCH 13/13] =?UTF-8?q?feat:=20ChatRoomController=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ChatRoomControllerTest.java | 250 ++++++++++++++++++ 1 file changed, 250 insertions(+) create mode 100644 src/test/java/space/space_spring/controller/ChatRoomControllerTest.java diff --git a/src/test/java/space/space_spring/controller/ChatRoomControllerTest.java b/src/test/java/space/space_spring/controller/ChatRoomControllerTest.java new file mode 100644 index 00000000..6d1c244b --- /dev/null +++ b/src/test/java/space/space_spring/controller/ChatRoomControllerTest.java @@ -0,0 +1,250 @@ +package space.space_spring.controller; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +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.data.jpa.mapping.JpaMetamodelMappingContext; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.test.web.servlet.MockMvc; +import space.space_spring.argumentResolver.jwtLogin.JwtLoginAuthHandlerArgumentResolver; +import space.space_spring.argumentResolver.userSpace.UserSpaceAuthHandlerArgumentResolver; +import space.space_spring.argumentResolver.userSpace.UserSpaceIdHandlerArgumentResolver; +import space.space_spring.config.SecurityConfig; +import space.space_spring.dto.chat.request.CreateChatRoomRequest; +import space.space_spring.dto.chat.request.JoinChatRoomRequest; +import space.space_spring.dto.chat.response.*; +import space.space_spring.dto.userSpace.UserInfoInSpace; +import space.space_spring.interceptor.UserSpaceValidationInterceptor; +import space.space_spring.interceptor.jwtLogin.JwtLoginAuthInterceptor; +import space.space_spring.service.ChatRoomService; +import space.space_spring.service.S3Uploader; + +import java.nio.charset.StandardCharsets; +import java.util.List; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +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.*; + +@WebMvcTest(ChatRoomController.class) +@MockBean(JpaMetamodelMappingContext.class) +@Import(SecurityConfig.class) +@AutoConfigureMockMvc(addFilters = false) // security config ignore +public class ChatRoomControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private ObjectMapper objectMapper; + + @MockBean + private ChatRoomService chatRoomService; + + @MockBean + private S3Uploader s3Uploader; + + @MockBean + private JwtLoginAuthHandlerArgumentResolver jwtLoginAuthHandlerArgumentResolver; + + @MockBean + private UserSpaceIdHandlerArgumentResolver userSpaceIdHandlerArgumentResolver; + + @MockBean + private UserSpaceAuthHandlerArgumentResolver userSpaceAuthHandlerArgumentResolver; + + @MockBean + private JwtLoginAuthInterceptor jwtLoginAuthInterceptor; + + @MockBean + private UserSpaceValidationInterceptor userSpaceValidationInterceptor; + + private static Long userId; + + private static Long spaceId; + + private static Long chatRoomId; + + private static Long userSpaceId; + + private static MockMultipartFile mockImgFile; + + private ChatSuccessResponse commonResponse; + + @BeforeEach + void 채팅방_테스트_설정() throws Exception { + userId = 0L; + spaceId = 1L; + userSpaceId = 2L; + chatRoomId = 3L; + + given(userSpaceValidationInterceptor.preHandle(any(), any(), any())) + .willReturn(true); + + given(jwtLoginAuthInterceptor.preHandle(any(), any(), any())) + .willReturn(true); + + given(jwtLoginAuthHandlerArgumentResolver.resolveArgument(any(), any(), any(), any())) + .willReturn(userId); + + given(userSpaceIdHandlerArgumentResolver.resolveArgument(any(), any(), any(), any())) + .willReturn(userSpaceId); + + given(userSpaceAuthHandlerArgumentResolver.resolveArgument(any(), any(), any(), any())) + .willReturn("manager"); + + mockImgFile = new MockMultipartFile("img", "test.png", "png", "test file".getBytes(StandardCharsets.UTF_8)); + + commonResponse = ChatSuccessResponse.builder().build(); + } + + @Test + @DisplayName("모든_채팅방_조회_테스트") + void 모든_채팅방_조회_테스트() throws Exception { + // given + ReadChatRoomResponse response = ReadChatRoomResponse.of(List.of(new ChatRoomResponse[]{})); + given(chatRoomService.readChatRooms(userId, spaceId)) + .willReturn(response); + + // when & then + mockMvc.perform(get("/space/{spaceId}/chat/chatroom", spaceId)) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.message").exists()); + } + + @Test + @DisplayName("채팅방_생성_테스트") + void 채팅방_생성_테스트() throws Exception { + // given +// CreateChatRoomRequest request = new CreateChatRoomRequest(); + CreateChatRoomRequest request = CreateChatRoomRequest.builder() + .name("testChatRoom") + .img(mockImgFile) + .memberList(List.of(4L)) + .build(); +// request.setName("testChatRoom"); +// request.setImg(mockImgFile); +// request.setMemberList(List.of(4L)); + + CreateChatRoomResponse response = CreateChatRoomResponse.of(chatRoomId); + given(chatRoomService.createChatRoom(userId, spaceId, request, "asdf")) + .willReturn(response); + + // when & then + mockMvc.perform(multipart("/space/{spaceId}/chat/chatroom", spaceId) + .file(mockImgFile) + .param("name", request.getName()) + .param("memberList", request.getMemberList().stream() + .map(String::valueOf) + .toArray(String[]::new)) + .param("userSpaceAuth", "manager") + .contentType(MediaType.MULTIPART_FORM_DATA)) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.message").exists()); + } + + @Test + @DisplayName("특정_채팅방의_모든_유저_정보_조회_테스트") + void 특정_채팅방의_모든_유저_정보_조회_테스트() throws Exception { + // given + ReadChatRoomMemberResponse response = ReadChatRoomMemberResponse.of(List.of(new UserInfoInSpace[]{})); + given(chatRoomService.readChatRoomMembers(userId, spaceId)) + .willReturn(response); + + // when & then + mockMvc.perform(get("/space/{spaceId}/chat/{chatRoomId}/member", spaceId, chatRoomId)) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.message").exists()); + } + + @Test + @DisplayName("특정_채팅방에_유저_초대_테스트") + void 특정_채팅방에_유저_초대_테스트() throws Exception { + // given + JoinChatRoomRequest request = JoinChatRoomRequest.builder().memberList(List.of(1L)).build(); + String content = objectMapper.writeValueAsString(request); + + given(chatRoomService.joinChatRoom(chatRoomId, request)) + .willReturn(commonResponse); + + // when & then + mockMvc.perform(post("/space/{spaceId}/chat/{chatRoomId}/member", spaceId, chatRoomId) + .param("userSpaceAuth", "manager") + .contentType(MediaType.APPLICATION_JSON) + .content(content)) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.message").exists()); + } + + @Test + @DisplayName("특정_유저가_채팅방에서_떠난_시간_저장_테스트") + void 특정_유저가_채팅방에서_떠난_시간_저장_테스트() throws Exception { + // given + given(chatRoomService.updateLastReadTime(userId, chatRoomId)) + .willReturn(commonResponse); + + // when & then + mockMvc.perform(post("/space/{spaceId}/chat/{chatRoomId}/leave", spaceId, chatRoomId)) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.message").exists()); + } + + @Test + @DisplayName("특정_채팅방의_이름_수정_테스트") + void 특정_채팅방의_이름_수정_테스트() throws Exception { + // given + given(chatRoomService.modifyChatRoomName(chatRoomId, "newChatRoom")) + .willReturn(commonResponse); + + // when & then + mockMvc.perform(post("/space/{spaceId}/chat/{chatRoomId}/setting", spaceId, chatRoomId) + .param("name", "newChatRoom") + .param("userSpaceAuth", "manager")) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.message").exists()); + } + + @Test + @DisplayName("특정_채팅방에서_나가기_테스트") + void 특정_채팅방에서_나가기_테스트() throws Exception { + // given + given(chatRoomService.exitChatRoom(userId, chatRoomId)) + .willReturn(commonResponse); + + // when & then + mockMvc.perform(post("/space/{spaceId}/chat/{chatRoomId}/exit", spaceId, chatRoomId)) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.message").exists()); + } + + @Test + @DisplayName("특정_채팅방_삭제_테스트") + void 특정_채팅방_삭제_테스트() throws Exception { + // given + given(chatRoomService.deleteChatRoom(chatRoomId)) + .willReturn(commonResponse); + + // when & then + mockMvc.perform(post("/space/{spaceId}/chat/{chatRoomId}/delete", spaceId, chatRoomId) + .param("userSpaceAuth", "manager")) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.message").exists()); + } +}