Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

중복 모임 채팅방 생성 버그 픽스 #759

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface ChatRoomRepository extends JpaRepository<ChatRoomEntity, Long>
Optional<ChatRoomEntity> findByIdAndDarakbangId(Long chatRoomId, long darakbangId);

Optional<ChatRoomEntity> findByTargetIdAndType(long targetId, ChatRoomType chatRoomType);

boolean existsByTargetIdAndType(long targetId, ChatRoomType chatRoomType);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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> 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);
}
}