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

[BE] 모임 예외 처리 추가 #705

Merged
merged 2 commits into from
Oct 23, 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 @@ -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);
Expand All @@ -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("최대 인원을 초과했거나, 더이상 모임에 참여할 수 없는 상태입니다.");
}
}

Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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 상태로 변경할 수 없습니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Loading