From ae0cbefa4611f573c57cde912c6df0e277dc84f9 Mon Sep 17 00:00:00 2001 From: Jeong Jiho Date: Wed, 23 Oct 2024 15:15:12 +0900 Subject: [PATCH] =?UTF-8?q?[BE]=20=EB=AA=A8=EC=9E=84=20=EC=98=88=EC=99=B8?= =?UTF-8?q?=20=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80=20(#705)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../happy/friendogly/club/domain/Club.java | 12 ++--- .../happy/friendogly/club/domain/Status.java | 8 +++- .../friendogly/club/domain/ClubTest.java | 45 +++++++++++++++++++ .../club/service/ClubCommandServiceTest.java | 22 +++++++++ 4 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 backend/src/test/java/com/happy/friendogly/club/domain/ClubTest.java diff --git a/backend/src/main/java/com/happy/friendogly/club/domain/Club.java b/backend/src/main/java/com/happy/friendogly/club/domain/Club.java index dd77f487d..1563c8d95 100644 --- a/backend/src/main/java/com/happy/friendogly/club/domain/Club.java +++ b/backend/src/main/java/com/happy/friendogly/club/domain/Club.java @@ -147,7 +147,7 @@ public void removeChatRoomMember(Member member) { public void addClubMember(Member member) { validateAlreadyExists(member); - validateMemberCapacity(); + validateStatus(); ClubMember clubMember = ClubMember.create(this, member); clubMembers.add(clubMember); @@ -167,9 +167,9 @@ public boolean isAlreadyJoined(Member member) { .anyMatch(clubMember -> clubMember.isSameMember(member)); } - private void validateMemberCapacity() { - if (memberCapacity.isCapacityReached(countClubMember())) { - throw new FriendoglyException("최대 인원을 초과하여 모임에 참여할 수 없습니다."); + private void validateStatus() { + if (status.isFull() || status.isClosed()) { + throw new FriendoglyException("최대 인원을 초과했거나, 더이상 모임에 참여할 수 없는 상태입니다."); } } @@ -267,8 +267,8 @@ public void update(String title, String content, String status) { } private void updateStatus(String status) { - if (this.status.isFull()) { - return; + if (this.status.isFull() && Status.toStatus(status).isOpen()) { + throw new FriendoglyException("인원이 가득찬 모임은 다시 OPEN 상태로 변경할 수 없습니다."); } this.status = Status.toStatus(status); } diff --git a/backend/src/main/java/com/happy/friendogly/club/domain/Status.java b/backend/src/main/java/com/happy/friendogly/club/domain/Status.java index 2cfacf1a0..48e9f0c9d 100644 --- a/backend/src/main/java/com/happy/friendogly/club/domain/Status.java +++ b/backend/src/main/java/com/happy/friendogly/club/domain/Status.java @@ -16,10 +16,14 @@ public static Status toStatus(String status) { } public boolean isOpen() { - return this == Status.OPEN; + return this == OPEN; + } + + public boolean isClosed() { + return this == CLOSED; } public boolean isFull() { - return this == Status.FULL; + return this == FULL; } } diff --git a/backend/src/test/java/com/happy/friendogly/club/domain/ClubTest.java b/backend/src/test/java/com/happy/friendogly/club/domain/ClubTest.java new file mode 100644 index 000000000..e60bbea47 --- /dev/null +++ b/backend/src/test/java/com/happy/friendogly/club/domain/ClubTest.java @@ -0,0 +1,45 @@ +package com.happy.friendogly.club.domain; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.happy.friendogly.exception.FriendoglyException; +import com.happy.friendogly.member.domain.Member; +import com.happy.friendogly.pet.domain.Gender; +import com.happy.friendogly.pet.domain.Pet; +import com.happy.friendogly.pet.domain.SizeType; +import java.time.LocalDate; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class ClubTest { + + private Member member = new Member("member", "1dfhf993", "imageUrl"); + private Pet pet = new Pet(member, "뚱땡이", "설명", LocalDate.now().minusDays(1L), SizeType.SMALL, Gender.FEMALE, "imageUrl"); + + @DisplayName("인원이 가득찬 모임 상태를 OPEN으로 변경하면 예외가 발생한다.") + @Test + void update_FullToOpen_Fail() { + Club club = Club.create( + "title", + "content", + "서울특별시", + "송파구", + "신천동", + 1, // 정원 1명, 방장이 인원에 포함되므로, 모임 생성과 동시에 정원이 가득 참 + member, + Arrays.stream(Gender.values()).collect(Collectors.toSet()), + Arrays.stream(SizeType.values()).collect(Collectors.toSet()), + "imageUrl", + List.of(pet) + ); + + assertThat(club.getStatus()).isEqualTo(Status.FULL); + assertThatThrownBy(() -> club.update("title", "content", Status.OPEN.name())) + .isExactlyInstanceOf(FriendoglyException.class) + .hasMessage("인원이 가득찬 모임은 다시 OPEN 상태로 변경할 수 없습니다."); + } +} diff --git a/backend/src/test/java/com/happy/friendogly/club/service/ClubCommandServiceTest.java b/backend/src/test/java/com/happy/friendogly/club/service/ClubCommandServiceTest.java index 87788f013..9e6a68682 100644 --- a/backend/src/test/java/com/happy/friendogly/club/service/ClubCommandServiceTest.java +++ b/backend/src/test/java/com/happy/friendogly/club/service/ClubCommandServiceTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertAll; import com.happy.friendogly.club.domain.Club; +import com.happy.friendogly.club.domain.Status; import com.happy.friendogly.club.dto.request.DeleteKickedMemberRequest; import com.happy.friendogly.club.dto.request.SaveClubMemberRequest; import com.happy.friendogly.club.dto.request.SaveClubRequest; @@ -146,6 +147,27 @@ void joinClub_FailAlreadyParticipating() { .hasMessage("이미 참여 중인 모임입니다."); } + @DisplayName("CLOSED 상태인 모임에 참여 시도하면 예외가 발생한다.") + @Transactional + @Test + void joinClub_ClosedFail() { + Club savedClub = createSavedClub( + savedMember, + List.of(savedPet), + Set.of(Gender.FEMALE, Gender.FEMALE_NEUTERED), + Set.of(SizeType.SMALL) + ); + Member savedNewMember = memberRepository.save(new Member("name", "sdhfidf1", "imageUrl")); + Pet savedNewMemberPet = createSavedPet(savedNewMember); + + savedClub.update("title", "content", Status.CLOSED.name()); + + SaveClubMemberRequest request = new SaveClubMemberRequest(List.of(savedNewMemberPet.getId())); + assertThatThrownBy(() -> clubCommandService.joinClub(savedClub.getId(), savedNewMember.getId(), request)) + .isInstanceOf(FriendoglyException.class) + .hasMessage("최대 인원을 초과했거나, 더이상 모임에 참여할 수 없는 상태입니다."); + } + @DisplayName("참여 가능한 강아지가 없다면 참여할 수 없다.") @Test void joinClub_FailCanNotParticipate() {