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

[Test] 그룹 도메인, 쿼리 로직에 대한 테스트를 작성한다 #80

Merged
merged 3 commits into from
Nov 3, 2023
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
5 changes: 3 additions & 2 deletions src/main/java/daybyquest/group/domain/Groups.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}
}
}
33 changes: 33 additions & 0 deletions src/test/java/daybyquest/group/domain/GroupTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
143 changes: 143 additions & 0 deletions src/test/java/daybyquest/group/domain/GroupsTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
97 changes: 97 additions & 0 deletions src/test/java/daybyquest/group/query/GroupDaoQuerydslImplTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
43 changes: 43 additions & 0 deletions src/test/java/daybyquest/support/fixture/GroupFixtures.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading