From 95cf5b683ab02f0a9070d1f9dd67519472ebee03 Mon Sep 17 00:00:00 2001 From: DH CHOI Date: Fri, 3 Nov 2023 14:24:14 +0900 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=EC=9E=98=EB=AA=BB=EC=9E=91=EC=84=B1?= =?UTF-8?q?=EB=90=9C=20Groups=EC=9D=98=20=EA=B2=80=EC=A6=9D=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/daybyquest/group/domain/Groups.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/daybyquest/group/domain/Groups.java b/src/main/java/daybyquest/group/domain/Groups.java index c9029b6..df4104d 100644 --- a/src/main/java/daybyquest/group/domain/Groups.java +++ b/src/main/java/daybyquest/group/domain/Groups.java @@ -1,5 +1,6 @@ package daybyquest.group.domain; +import daybyquest.global.error.exception.InvalidDomainException; import daybyquest.global.error.exception.NotExistGroupException; import daybyquest.interest.domain.Interests; import daybyquest.user.domain.Users; @@ -61,8 +62,8 @@ public void validateExistentById(final Long id) { } private void validateNotMember(final Long userId, final Long groupId) { - if (!groupUserRepository.existsByUserIdAndGroupId(userId, groupId)) { - throw new NotExistGroupException(); + if (groupUserRepository.existsByUserIdAndGroupId(userId, groupId)) { + throw new InvalidDomainException(); } } } From 19c356c2fe9f7341d416df1cb97e3c00973c6d33 Mon Sep 17 00:00:00 2001 From: DH CHOI Date: Fri, 3 Nov 2023 14:24:33 +0900 Subject: [PATCH 2/3] =?UTF-8?q?test:=20=EA=B7=B8=EB=A3=B9=20=ED=94=BD?= =?UTF-8?q?=EC=8A=A4=EC=B2=98=EB=A5=BC=20=EC=9E=91=EC=84=B1=ED=95=98?= =?UTF-8?q?=EA=B3=A0=20=EB=8F=84=EB=A9=94=EC=9D=B8=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../daybyquest/group/domain/GroupTest.java | 33 ++++ .../daybyquest/group/domain/GroupsTest.java | 143 ++++++++++++++++++ .../support/fixture/GroupFixtures.java | 43 ++++++ 3 files changed, 219 insertions(+) create mode 100644 src/test/java/daybyquest/group/domain/GroupTest.java create mode 100644 src/test/java/daybyquest/group/domain/GroupsTest.java create mode 100644 src/test/java/daybyquest/support/fixture/GroupFixtures.java diff --git a/src/test/java/daybyquest/group/domain/GroupTest.java b/src/test/java/daybyquest/group/domain/GroupTest.java new file mode 100644 index 0000000..b14c158 --- /dev/null +++ b/src/test/java/daybyquest/group/domain/GroupTest.java @@ -0,0 +1,33 @@ +package daybyquest.group.domain; + +import static daybyquest.support.fixture.GroupFixtures.GROUP_1; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import daybyquest.global.error.exception.InvalidDomainException; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +public class GroupTest { + + @ParameterizedTest + @ValueSource(strings = {"", "일이삼사오육칠팔구십일이삼사오육"}) + void 그룹_이름이_1_에서_15_글자_사이가_아니면_예외를_던진다(final String name) { + // given & when & then + assertThatThrownBy(() -> new Group(GROUP_1.interest, name, GROUP_1.description, GROUP_1.대표_사진())) + .isInstanceOf(InvalidDomainException.class); + } + + @Test + void 설명이_200_글자_초과면_예외를_던진다() { + // given + final String description = Stream.generate(() -> "x").limit(201).collect(Collectors.joining()); + + // when & then + assertThatThrownBy( + () -> new Group(GROUP_1.interest, GROUP_1.name, description, GROUP_1.대표_사진())) + .isInstanceOf(InvalidDomainException.class); + } +} diff --git a/src/test/java/daybyquest/group/domain/GroupsTest.java b/src/test/java/daybyquest/group/domain/GroupsTest.java new file mode 100644 index 0000000..ce10d97 --- /dev/null +++ b/src/test/java/daybyquest/group/domain/GroupsTest.java @@ -0,0 +1,143 @@ +package daybyquest.group.domain; + + +import static daybyquest.support.fixture.GroupFixtures.GROUP_1; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.BDDMockito.then; + +import daybyquest.global.error.exception.NotExistGroupException; +import daybyquest.interest.domain.Interests; +import daybyquest.user.domain.Users; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +public class GroupsTest { + + @Mock + private GroupRepository groupRepository; + + @Mock + private GroupUserRepository groupUserRepository; + + @Mock + private Users users; + + @Mock + private Interests interests; + + @InjectMocks + private Groups groups; + + @Test + void 사용자와_관심사를_검증하고_그룹을_저장한다() { + // given + final Long userId = 1L; + final Long groupId = 2L; + given(groupRepository.save(any(Group.class))).willReturn(GROUP_1.생성(groupId)); + + // when + final Long actualId = groups.save(userId, GROUP_1.생성()); + + // then + assertAll(() -> { + then(users).should().validateModeratorById(userId); + then(interests).should().validateInterest(GROUP_1.interest); + then(groupRepository).should().save(any(Group.class)); + assertThat(actualId).isEqualTo(groupId); + }); + } + + @Test + void 그룹을_저장할_땐_사용자를_함께_저장한다() { + // given + final Long userId = 1L; + final Long groupId = 2L; + given(groupRepository.save(any(Group.class))).willReturn(GROUP_1.생성(groupId)); + + // when + groups.save(userId, GROUP_1.생성()); + + // then + then(groupUserRepository).should().save(any(GroupUser.class)); + } + + @Test + void 사용자를_추가할_땐_그룹ID의_존재여부와_이미_회원이_아닌지_검증한다() { + // given + final Long userId = 1L; + final Long groupId = 2L; + final Group group = GROUP_1.생성(groupId); + final GroupUser groupUser = GroupUser.createGroupMember(userId, group); + given(groupRepository.existsById(groupId)).willReturn(true); + + // when + groups.addUser(groupUser); + + // then + assertAll(() -> { + then(groupRepository).should().existsById(groupId); + then(groupUserRepository).should().existsByUserIdAndGroupId(userId, groupId); + then(groupUserRepository).should().save(any(GroupUser.class)); + }); + } + + @Test + void 사용자를_추가할_때_회원_역할이라면_사용자ID_존재여부를_검증한다() { + // given + final Long userId = 1L; + final Long groupId = 2L; + final Group group = GROUP_1.생성(groupId); + final GroupUser groupUser = GroupUser.createGroupMember(userId, group); + given(groupRepository.existsById(groupId)).willReturn(true); + + // when + groups.addUser(groupUser); + + // then + then(users).should().validateExistentById(userId); + } + + @Test + void 사용자를_추가할_때_관리자_역할이라면_MODERATOR_여부를_검증한다() { + // given + final Long userId = 1L; + final Long groupId = 2L; + final Group group = GROUP_1.생성(groupId); + final GroupUser groupUser = GroupUser.createGroupManager(userId, group); + given(groupRepository.existsById(groupId)).willReturn(true); + + // when + groups.addUser(groupUser); + + // then + then(users).should().validateModeratorById(userId); + } + + @Test + void 그룹_ID_존재_여부를_검증한다() { + // given + final Long groupId = 1L; + given(groupRepository.existsById(groupId)).willReturn(true); + + // when + groups.validateExistentById(groupId); + + // then + then(groupRepository).should().existsById(groupId); + } + + @Test + void 게시물_ID_존재_여부를_검증_시_없다면_예외를_던진다() { + // given & when & then + assertThatThrownBy(() -> groups.validateExistentById(1L)) + .isInstanceOf(NotExistGroupException.class); + } +} diff --git a/src/test/java/daybyquest/support/fixture/GroupFixtures.java b/src/test/java/daybyquest/support/fixture/GroupFixtures.java new file mode 100644 index 0000000..d64af4a --- /dev/null +++ b/src/test/java/daybyquest/support/fixture/GroupFixtures.java @@ -0,0 +1,43 @@ +package daybyquest.support.fixture; + +import daybyquest.group.domain.Group; +import daybyquest.image.vo.Image; +import org.springframework.test.util.ReflectionTestUtils; + +public enum GroupFixtures { + + GROUP_1("그룹1", "관심사", "설명1", "group1.png"), + GROUP_2("그룹2", "관심사", "설명2", "group2.png"), + GROUP_3("그룹3", "관심사", "설명3", "group3.png"), + GROUP_4("그룹4", "관심사", "설명4", "group4.png"); + + public final String name; + + public final String interest; + + public final String description; + + public final String imageIdentifier; + + GroupFixtures(final String name, final String interest, final String description, + final String imageIdentifier) { + this.name = name; + this.interest = interest; + this.description = description; + this.imageIdentifier = imageIdentifier; + } + + public Group 생성(final Long id) { + final Group group = new Group(this.interest, this.name, this.description, 대표_사진()); + ReflectionTestUtils.setField(group, "id", id); + return group; + } + + public Group 생성() { + return 생성(null); + } + + public Image 대표_사진() { + return new Image(this.imageIdentifier); + } +} From 73fbe8138f74b6d0476574605321505a71157969 Mon Sep 17 00:00:00 2001 From: DH CHOI Date: Fri, 3 Nov 2023 14:24:42 +0900 Subject: [PATCH 3/3] =?UTF-8?q?test:=20=EA=B7=B8=EB=A3=B9=20=EC=BF=BC?= =?UTF-8?q?=EB=A6=AC=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../group/query/GroupDaoQuerydslImplTest.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/test/java/daybyquest/group/query/GroupDaoQuerydslImplTest.java diff --git a/src/test/java/daybyquest/group/query/GroupDaoQuerydslImplTest.java b/src/test/java/daybyquest/group/query/GroupDaoQuerydslImplTest.java new file mode 100644 index 0000000..54a710e --- /dev/null +++ b/src/test/java/daybyquest/group/query/GroupDaoQuerydslImplTest.java @@ -0,0 +1,97 @@ +package daybyquest.group.query; + +import static daybyquest.support.fixture.GroupFixtures.GROUP_1; +import static daybyquest.support.fixture.UserFixtures.ALICE; +import static daybyquest.support.fixture.UserFixtures.BOB; +import static daybyquest.support.fixture.UserFixtures.CHARLIE; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertAll; + +import daybyquest.global.error.exception.NotExistGroupException; +import daybyquest.group.domain.Group; +import daybyquest.group.domain.GroupUser; +import daybyquest.support.test.QuerydslTest; +import daybyquest.user.domain.User; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Import; + +@Import(GroupDaoQuerydslImpl.class) +public class GroupDaoQuerydslImplTest extends QuerydslTest { + + @Autowired + private GroupDaoQuerydslImpl groupDao; + + @PersistenceContext + private EntityManager entityManager; + + @Test + void 그룹_ID로_데이터를_조회한다() { + // given + final User user = entityManager.merge(BOB.생성()); + final Group group = entityManager.merge(GROUP_1.생성()); + + // when + final GroupData groupData = groupDao.getById(user.getId(), group.getId()); + + // then + assertAll(() -> { + assertThat(groupData.getInterest()).isEqualTo(GROUP_1.interest); + assertThat(groupData.getName()).isEqualTo(GROUP_1.name); + assertThat(groupData.getDescription()).isEqualTo(GROUP_1.description); + assertThat(groupData.getImageIdentifier()).isEqualTo(GROUP_1.imageIdentifier); + assertThat(groupData.getUserCount()).isZero(); + assertThat(groupData.isGroupManager()).isFalse(); + }); + } + + @Test + void 그룹_인원_수가_함께_조회된다() { + // given + final User bob = entityManager.merge(BOB.생성()); + final User alice = entityManager.merge(ALICE.생성()); + final User charlie = entityManager.merge(CHARLIE.생성()); + final Group group = entityManager.merge(GROUP_1.생성()); + + entityManager.merge(GroupUser.createGroupMember(bob.getId(), group)); + entityManager.merge(GroupUser.createGroupMember(alice.getId(), group)); + entityManager.merge(GroupUser.createGroupManager(charlie.getId(), group)); + + // when + final GroupData groupData = groupDao.getById(bob.getId(), group.getId()); + + // then + assertThat(groupData.getUserCount()).isEqualTo(3); + } + + @Test + void 그룹_관리자_여부가_함께_조회된다() { + // given + final User bob = entityManager.merge(BOB.생성()); + final User alice = entityManager.merge(ALICE.생성()); + final Group group = entityManager.merge(GROUP_1.생성()); + + entityManager.merge(GroupUser.createGroupMember(bob.getId(), group)); + entityManager.merge(GroupUser.createGroupManager(alice.getId(), group)); + + // when + final GroupData bobActual = groupDao.getById(bob.getId(), group.getId()); + final GroupData aliceAtual = groupDao.getById(alice.getId(), group.getId()); + + // then + assertAll(() -> { + assertThat(bobActual.isGroupManager()).isFalse(); + assertThat(aliceAtual.isGroupManager()).isTrue(); + }); + } + + @Test + void 그룹ID가_없다면_예외를_던진다() { + // given & when & then + assertThatThrownBy(() -> groupDao.getById(1L, 2L)) + .isInstanceOf(NotExistGroupException.class); + } +}