From ef3c19896c515c004b2a6040f2c962be4f542a4b Mon Sep 17 00:00:00 2001 From: SungKyum Kim Date: Thu, 24 Oct 2024 11:32:06 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20ChatRoomValidator,=20=ED=83=80?= =?UTF-8?q?=EC=9E=85=EA=B3=BC=20=ED=83=80=EA=B2=9F=20id=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=AF=B8=20=EC=A1=B4=EC=9E=AC=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EC=B1=84=ED=8C=85=EB=B0=A9=20=EA=B2=80=EC=A6=9D=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/exception/ChatErrorMessage.java | 3 +- .../chat/implement/ChatRoomValidator.java | 23 +++++++ .../infrastructure/ChatRoomRepository.java | 2 + .../chat/implement/ChatRoomValidatorTest.java | 67 +++++++++++++++++++ 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 backend/src/main/java/mouda/backend/chat/implement/ChatRoomValidator.java create mode 100644 backend/src/test/java/mouda/backend/chat/implement/ChatRoomValidatorTest.java diff --git a/backend/src/main/java/mouda/backend/chat/exception/ChatErrorMessage.java b/backend/src/main/java/mouda/backend/chat/exception/ChatErrorMessage.java index ee84fef6f..8f91eb32b 100644 --- a/backend/src/main/java/mouda/backend/chat/exception/ChatErrorMessage.java +++ b/backend/src/main/java/mouda/backend/chat/exception/ChatErrorMessage.java @@ -11,8 +11,9 @@ public enum ChatErrorMessage { BET_DARAKBANG_MEMBER_NOT_FOUND("참여하지 않은 안내면진다입니다."), INVALID_CHATROOM_TYPE("잘못된 채팅 방 타입입니다."), UNAUTHORIZED("권한이 없습니다."), - INVALID_DATE_TIME_FORMAT("날짜와 시간 형식이 올바르지 않습니다."), + CHATROOM_ALREADY_EXISTS("이미 존재하는 채팅방입니다."), // 새로 추가된 메시지 + ; private final String message; diff --git a/backend/src/main/java/mouda/backend/chat/implement/ChatRoomValidator.java b/backend/src/main/java/mouda/backend/chat/implement/ChatRoomValidator.java new file mode 100644 index 000000000..bcdae82ae --- /dev/null +++ b/backend/src/main/java/mouda/backend/chat/implement/ChatRoomValidator.java @@ -0,0 +1,23 @@ +package mouda.backend.chat.implement; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Component; + +import lombok.RequiredArgsConstructor; +import mouda.backend.chat.domain.ChatRoomType; +import mouda.backend.chat.exception.ChatErrorMessage; +import mouda.backend.chat.exception.ChatException; +import mouda.backend.chat.infrastructure.ChatRoomRepository; + +@Component +@RequiredArgsConstructor +public class ChatRoomValidator { + + private final ChatRoomRepository chatRoomRepository; + + public void validateAlreadyExists(long targetId, ChatRoomType chatRoomType) { + if (chatRoomRepository.existsByTargetIdAndType(targetId, chatRoomType)) { + throw new ChatException(HttpStatus.BAD_REQUEST, ChatErrorMessage.CHATROOM_ALREADY_EXISTS); + } + } +} diff --git a/backend/src/main/java/mouda/backend/chat/infrastructure/ChatRoomRepository.java b/backend/src/main/java/mouda/backend/chat/infrastructure/ChatRoomRepository.java index 516c86a9c..e2553b5ae 100644 --- a/backend/src/main/java/mouda/backend/chat/infrastructure/ChatRoomRepository.java +++ b/backend/src/main/java/mouda/backend/chat/infrastructure/ChatRoomRepository.java @@ -11,4 +11,6 @@ public interface ChatRoomRepository extends JpaRepository Optional findByIdAndDarakbangId(Long chatRoomId, long darakbangId); Optional findByTargetIdAndType(long targetId, ChatRoomType chatRoomType); + + boolean existsByTargetIdAndType(long targetId, ChatRoomType chatRoomType); } diff --git a/backend/src/test/java/mouda/backend/chat/implement/ChatRoomValidatorTest.java b/backend/src/test/java/mouda/backend/chat/implement/ChatRoomValidatorTest.java new file mode 100644 index 000000000..4e9f2700c --- /dev/null +++ b/backend/src/test/java/mouda/backend/chat/implement/ChatRoomValidatorTest.java @@ -0,0 +1,67 @@ +package mouda.backend.chat.implement; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import mouda.backend.bet.entity.BetEntity; +import mouda.backend.bet.infrastructure.BetRepository; +import mouda.backend.chat.domain.ChatRoomType; +import mouda.backend.chat.entity.ChatRoomEntity; +import mouda.backend.chat.exception.ChatException; +import mouda.backend.chat.infrastructure.ChatRoomRepository; +import mouda.backend.common.fixture.BetEntityFixture; +import mouda.backend.common.fixture.ChatRoomEntityFixture; +import mouda.backend.common.fixture.DarakbangSetUp; +import mouda.backend.common.fixture.MoimFixture; +import mouda.backend.moim.domain.Moim; +import mouda.backend.moim.infrastructure.MoimRepository; + +@SpringBootTest +class ChatRoomValidatorTest extends DarakbangSetUp { + + @Autowired + ChatRoomValidator chatRoomValidator; + + @Autowired + MoimRepository moimRepository; + + @Autowired + BetRepository betRepository; + + @Autowired + ChatRoomRepository chatRoomRepository; + + @DisplayName("이미 존재하는 채팅방인지 검증한다. - ChatRoomType.MOIM") + @Test + void existedChatRoom_typeMoim() { + // given + Moim moim = MoimFixture.getCoffeeMoim(darakbang.getId()); + Moim savedMoim = moimRepository.save(moim); + + ChatRoomEntity chatRoomEntityOfMoim = ChatRoomEntityFixture.getChatRoomEntityOfMoim(savedMoim.getId(), darakbang.getId()); + chatRoomRepository.save(chatRoomEntityOfMoim); + + // when & then + assertThatThrownBy(() -> chatRoomValidator.validateAlreadyExists(savedMoim.getId(), ChatRoomType.MOIM)) + .isInstanceOf(ChatException.class); + } + + @DisplayName("이미 존재하는 채팅방인지 검증한다. - ChatRoomType.BET") + @Test + void existedChatRoom_typeBet() { + // given + BetEntity betEntity = BetEntityFixture.getBetEntity(darakbang.getId(), darakbangAnna.getId()); + BetEntity savedBet = betRepository.save(betEntity); + + ChatRoomEntity chatRoomEntityOfBet = ChatRoomEntityFixture.getChatRoomEntityOfBet(savedBet.getId(), darakbang.getId()); + chatRoomRepository.save(chatRoomEntityOfBet); + + // when & then + assertThatThrownBy(() -> chatRoomValidator.validateAlreadyExists(savedBet.getId(), ChatRoomType.BET)) + .isInstanceOf(ChatException.class); + } +} From 77bf8bca6fa7d1f878f197349739250a4f4f982e Mon Sep 17 00:00:00 2001 From: SungKyum Kim Date: Thu, 24 Oct 2024 11:46:54 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=EC=B1=84=ED=8C=85=EB=B0=A9=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=EC=8B=9C=20=EC=9D=B4=EB=AF=B8=20=EC=A1=B4?= =?UTF-8?q?=EC=9E=AC=ED=95=98=EB=8A=94=20=EC=B1=84=ED=8C=85=EB=B0=A9=20?= =?UTF-8?q?=EA=B2=80=EC=A6=9D=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/implement/ChatRoomWriter.java | 3 + .../chat/implement/ChatRoomWriterTest.java | 79 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 backend/src/test/java/mouda/backend/chat/implement/ChatRoomWriterTest.java diff --git a/backend/src/main/java/mouda/backend/chat/implement/ChatRoomWriter.java b/backend/src/main/java/mouda/backend/chat/implement/ChatRoomWriter.java index 189e442c6..16c22ed7a 100644 --- a/backend/src/main/java/mouda/backend/chat/implement/ChatRoomWriter.java +++ b/backend/src/main/java/mouda/backend/chat/implement/ChatRoomWriter.java @@ -12,8 +12,11 @@ public class ChatRoomWriter { private final ChatRoomRepository chatRoomRepository; + private final ChatRoomValidator chatRoomValidator; public long append(long targetId, long darakbangId, ChatRoomType chatRoomType) { + chatRoomValidator.validateAlreadyExists(targetId, chatRoomType); + ChatRoomEntity chatRoomEntity = ChatRoomEntity.builder() .targetId(targetId) .darakbangId(darakbangId) diff --git a/backend/src/test/java/mouda/backend/chat/implement/ChatRoomWriterTest.java b/backend/src/test/java/mouda/backend/chat/implement/ChatRoomWriterTest.java new file mode 100644 index 000000000..893683574 --- /dev/null +++ b/backend/src/test/java/mouda/backend/chat/implement/ChatRoomWriterTest.java @@ -0,0 +1,79 @@ +package mouda.backend.chat.implement; + +import static org.assertj.core.api.Assertions.*; + +import java.util.Optional; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import mouda.backend.bet.entity.BetEntity; +import mouda.backend.bet.infrastructure.BetRepository; +import mouda.backend.chat.domain.ChatRoomType; +import mouda.backend.chat.entity.ChatRoomEntity; +import mouda.backend.chat.exception.ChatException; +import mouda.backend.chat.infrastructure.ChatRoomRepository; +import mouda.backend.common.fixture.BetEntityFixture; +import mouda.backend.common.fixture.DarakbangSetUp; +import mouda.backend.common.fixture.MoimFixture; +import mouda.backend.moim.domain.Moim; +import mouda.backend.moim.infrastructure.MoimRepository; + +class ChatRoomWriterTest extends DarakbangSetUp { + + @Autowired + ChatRoomWriter chatRoomWriter; + + @Autowired + MoimRepository moimRepository; + + @Autowired + BetRepository betRepository; + + @Autowired + ChatRoomRepository chatRoomRepository; + + @DisplayName("새로운 채팅방을 생성한다.") + @Test + void append() { + // given + Moim moim = MoimFixture.getCoffeeMoim(darakbang.getId()); + Moim savedMoim = moimRepository.save(moim); + + // when + chatRoomWriter.append(savedMoim.getId(), darakbang.getId(), ChatRoomType.MOIM); + + //then + Optional chatRoomEntity = chatRoomRepository.findByTargetIdAndType(savedMoim.getId(), ChatRoomType.MOIM); + assertThat(chatRoomEntity.isPresent()).isTrue(); + assertThat(chatRoomEntity.get().getTargetId()).isEqualTo(savedMoim.getId()); + assertThat(chatRoomEntity.get().getType()).isEqualTo(ChatRoomType.MOIM); + } + + @DisplayName("이미 존재하는 모임 채팅방일 땐 예외를 발생한다.") + @Test + void append_alreadyExistedMoimChatRoom() { + // given + Moim moim = MoimFixture.getCoffeeMoim(darakbang.getId()); + Moim savedMoim = moimRepository.save(moim); + chatRoomWriter.append(savedMoim.getId(), darakbang.getId(), ChatRoomType.MOIM); + + // when & then + assertThatThrownBy(() -> chatRoomWriter.append(savedMoim.getId(), darakbang.getId(), ChatRoomType.MOIM)) + .isInstanceOf(ChatException.class); + } + + @DisplayName("이미 존재하는 배팅 채팅방일 땐 예외를 발생한다.") + @Test + void append_alreadyExistedBetChatRoom() { + // given + BetEntity betEntity = BetEntityFixture.getBetEntity(darakbang.getId(), darakbangAnna.getId()); + BetEntity savedBet = betRepository.save(betEntity); + chatRoomWriter.append(savedBet.getId(), darakbang.getId(), ChatRoomType.BET); + + // when & then + assertThatThrownBy(() -> chatRoomWriter.append(savedBet.getId(), darakbang.getId(), ChatRoomType.BET)) + .isInstanceOf(ChatException.class); + } +}