Skip to content

Commit

Permalink
Merge pull request #165 from DayByQuest/test/postService
Browse files Browse the repository at this point in the history
[Test] 게시물 조회 서비스들을 테스트한다
  • Loading branch information
vectorch9 authored Dec 18, 2023
2 parents 33c0887 + 3b4b5ba commit 7fd825e
Show file tree
Hide file tree
Showing 26 changed files with 769 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/main/java/daybyquest/badge/domain/Badges.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class Badges {
this.badgeRepository = badgeRepository;
}

public void save(final Badge badge) {
badgeRepository.save(badge);
public Badge save(final Badge badge) {
return badgeRepository.save(badge);
}

public void validateExistentById(final Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public SaveGroupService(final Groups groups, final ImageService imageService) {
public Long invoke(final Long loginId, final SaveGroupRequest request, final MultipartFile file) {
final Image image = imageService.convertToImage(CATEGORY, file);
final Group group = toEntity(request, image);
return groups.save(loginId, group);
return groups.save(loginId, group).getId();
}

public Group toEntity(final SaveGroupRequest request, final Image image) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/daybyquest/group/domain/Groups.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public class Groups {
this.interests = interests;
}

public Long save(final Long userId, final Group group) {
public Group save(final Long userId, final Group group) {
interests.validateInterest(group.getInterest());
validateNotExistentByName(group.getName());
final Group savedGroup = groupRepository.save(group);
groupUsers.addUser(GroupUser.createGroupManager(userId, savedGroup));
return savedGroup.getId();
return savedGroup;
}

public void validateNotExistentByName(final String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
@Service
public class CheckPostService {


private final Quests quests;

private final GroupUsers groupUsers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import daybyquest.post.dto.response.PostWithQuestResponse;
import daybyquest.post.query.PostDao;
import daybyquest.post.query.PostData;
import daybyquest.quest.domain.Quests;
import daybyquest.user.query.Profile;
import daybyquest.user.query.ProfileDao;
import org.springframework.stereotype.Service;
Expand All @@ -16,12 +15,9 @@ public class GetPostService {

private final ProfileDao profileDao;

private final Quests quests;

public GetPostService(final PostDao postDao, final ProfileDao profileDao, final Quests quests) {
public GetPostService(final PostDao postDao, final ProfileDao profileDao) {
this.postDao = postDao;
this.profileDao = profileDao;
this.quests = quests;
}

@Transactional(readOnly = true)
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/daybyquest/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,9 @@ private void validateQuestLink() {
throw new InvalidDomainException(NOT_LINKED_POST);
}
}

public Long getQuestId() {
validateQuestLink();
return questId;
}
}
2 changes: 1 addition & 1 deletion src/main/java/daybyquest/post/domain/Posts.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Posts {

public Post save(final Post post) {
users.validateExistentById(post.getUserId());
if (post.getQuestId() != null) {
if (post.isQuestLinked()) {
participants.validateExistent(post.getUserId(), post.getQuestId());
}
return postRepository.save(post);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public SaveQuestService(final Quests quests, final Badges badges, final ImageSer
@Transactional
public Long invoke(final SaveQuestRequest request, final List<MultipartFile> files) {
final Quest quest = toEntity(request, toImageList(files));
final Long questId = quests.save(quest);
final Long questId = quests.save(quest).getId();
questClient.requestLabels(questId, quest.getImages().stream().map(Image::getIdentifier).toList(),
quest.getImageDescription());
return questId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Long invoke(final Long loginId, final SaveGroupQuestRequest request,
final List<MultipartFile> files) {
groupUsers.validateGroupManager(loginId, request.getGroupId());
final Quest quest = toEntity(request, toImageList(files));
final Long questId = quests.save(quest);
final Long questId = quests.save(quest).getId();
questClient.requestLabels(questId, quest.getImages().stream().map(Image::getIdentifier).toList(),
quest.getImageDescription());
return questId;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/daybyquest/quest/domain/Quests.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public class Quests {
this.badges = badges;
}

public Long save(final Quest quest) {
public Quest save(final Quest quest) {
if (quest.getBadgeId() != null) {
badges.validateExistentById(quest.getBadgeId());
validateNotExistentByBadgeId(quest.getBadgeId());
}
validateGroupQuest(quest);
return questRepository.save(quest).getId();
return questRepository.save(quest);
}

private void validateGroupQuest(final Quest quest) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/daybyquest/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
Expand Down Expand Up @@ -69,7 +70,7 @@ public class User {
@Embedded
private Image image;

@ElementCollection
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "user_interest", joinColumns = @JoinColumn(name = "user_id"))
@Column(length = 10)
private List<String> interests;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/daybyquest/group/domain/GroupsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class GroupsTest {
given(groupRepository.save(any(Group.class))).willReturn(GROUP_1.생성(groupId));

// when
final Long actualId = groups.save(userId, GROUP_1.생성());
final Long actualId = groups.save(userId, GROUP_1.생성()).getId();

// then
assertAll(() -> {
Expand Down
150 changes: 150 additions & 0 deletions src/test/java/daybyquest/post/application/CheckPostServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package daybyquest.post.application;

import static daybyquest.global.error.ExceptionCode.ALREADY_JUDGED_POST;
import static daybyquest.global.error.ExceptionCode.NOT_GROUP_MANAGER;
import static daybyquest.global.error.ExceptionCode.NOT_LINKED_POST;
import static daybyquest.post.domain.PostState.FAIL;
import static daybyquest.post.domain.PostState.SUCCESS;
import static daybyquest.support.fixture.GroupFixtures.GROUP_1;
import static daybyquest.support.fixture.InterestFixtures.INTEREST;
import static daybyquest.support.fixture.PostFixtures.POST_1;
import static daybyquest.support.fixture.QuestFixtures.QUEST_1;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import daybyquest.global.error.exception.InvalidDomainException;
import daybyquest.group.domain.Group;
import daybyquest.group.domain.GroupUser;
import daybyquest.post.domain.Post;
import daybyquest.post.dto.request.CheckPostRequest;
import daybyquest.quest.domain.Quest;
import daybyquest.support.test.ServiceTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.util.ReflectionTestUtils;

public class CheckPostServiceTest extends ServiceTest {

@Autowired
private CheckPostService checkPostService;

@Test
void 그룹장이_그룹_퀘스트를_성공_시킨다() {
// given
final Long aliceId = 중재자_권한으로_ALICE를_저장한다();
interests.save(INTEREST.생성());

final Group group = groups.save(aliceId, GROUP_1.생성());
final Quest quest = quests.save(QUEST_1.보상_없이_세부사항을_설정한다(QUEST_1.그룹_퀘스트_생성(group)));
final Long questId = quest.getId();
participants.saveWithUserIdAndQuestId(aliceId, questId);

final Post post = POST_1.생성(aliceId, questId);
post.needCheck();
final Long postId = posts.save(post).getId();

final CheckPostRequest request = 게시물_판정_요청(true);

// when
checkPostService.invoke(aliceId, postId, request);

// then
final Post actual = posts.getById(postId);
assertThat(actual.getState()).isEqualTo(SUCCESS);
}

@Test
void 그룹장이_그룹_퀘스트를_실패_시킨다() {
// given
final Long aliceId = 중재자_권한으로_ALICE를_저장한다();
interests.save(INTEREST.생성());

final Group group = groups.save(aliceId, GROUP_1.생성());
final Quest quest = quests.save(QUEST_1.보상_없이_세부사항을_설정한다(QUEST_1.그룹_퀘스트_생성(group)));
final Long questId = quest.getId();
participants.saveWithUserIdAndQuestId(aliceId, questId);

final Post post = POST_1.생성(aliceId, questId);
post.needCheck();
final Long postId = posts.save(post).getId();

final CheckPostRequest request = 게시물_판정_요청(false);

// when
checkPostService.invoke(aliceId, postId, request);

// then
final Post actual = posts.getById(postId);
assertThat(actual.getState()).isEqualTo(FAIL);
}

@Test
void 그룹장이_아니면_예외를_던진다() {
// given
final Long aliceId = 중재자_권한으로_ALICE를_저장한다();
final Long bobId = 중재자_권한으로_BOB을_저장한다();
interests.save(INTEREST.생성());

final Group group = groups.save(aliceId, GROUP_1.생성());
final Quest quest = quests.save(QUEST_1.보상_없이_세부사항을_설정한다(QUEST_1.그룹_퀘스트_생성(group)));
final Long questId = quest.getId();
groupUsers.addUser(GroupUser.createGroupMember(bobId, group));

participants.saveWithUserIdAndQuestId(aliceId, questId);

final Post post = POST_1.생성(aliceId, questId);
post.needCheck();
final Long postId = posts.save(post).getId();

final CheckPostRequest request = 게시물_판정_요청(false);

// when & then
assertThatThrownBy(() -> checkPostService.invoke(bobId, postId, request))
.isInstanceOf(InvalidDomainException.class)
.hasMessageContaining(NOT_GROUP_MANAGER.getMessage());
}

@Test
void 퀘스트가_링크되지_않은_게시물을_확인할_수_없다() {
// given
final Long aliceId = 중재자_권한으로_ALICE를_저장한다();

final Long postId = posts.save(POST_1.생성(aliceId)).getId();

final CheckPostRequest request = 게시물_판정_요청(true);

// when & then
assertThatThrownBy(() -> checkPostService.invoke(aliceId, postId, request))
.isInstanceOf(InvalidDomainException.class)
.hasMessageContaining(NOT_LINKED_POST.getMessage());
}

@Test
void 이미_확인된_게시물은_확인할_수_없다() {
// given
final Long aliceId = 중재자_권한으로_ALICE를_저장한다();
interests.save(INTEREST.생성());

final Group group = groups.save(aliceId, GROUP_1.생성());
final Quest quest = quests.save(QUEST_1.보상_없이_세부사항을_설정한다(QUEST_1.그룹_퀘스트_생성(group)));
final Long questId = quest.getId();
participants.saveWithUserIdAndQuestId(aliceId, questId);

final Post post = POST_1.생성(aliceId, questId);
post.success();
final Long postId = posts.save(post).getId();

final CheckPostRequest request = 게시물_판정_요청(true);

// when & then
assertThatThrownBy(() -> checkPostService.invoke(aliceId, postId, request))
.isInstanceOf(InvalidDomainException.class)
.hasMessageContaining(ALREADY_JUDGED_POST.getMessage());
}

private CheckPostRequest 게시물_판정_요청(final boolean approval) {
final CheckPostRequest request = new CheckPostRequest();
ReflectionTestUtils.setField(request, "approval", approval);
return request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package daybyquest.post.application;

import static daybyquest.global.error.ExceptionCode.NOT_GROUP_MANAGER;
import static daybyquest.support.fixture.GroupFixtures.GROUP_1;
import static daybyquest.support.fixture.InterestFixtures.INTEREST;
import static daybyquest.support.fixture.PostFixtures.POST_1;
import static daybyquest.support.fixture.QuestFixtures.QUEST_1;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import daybyquest.global.error.exception.InvalidDomainException;
import daybyquest.group.domain.Group;
import daybyquest.group.domain.GroupUser;
import daybyquest.post.domain.Post;
import daybyquest.post.dto.response.SimplePostResponse;
import daybyquest.quest.domain.Quest;
import daybyquest.support.test.ServiceTest;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class GetNeedCheckPostServiceTest extends ServiceTest {

@Autowired
private GetNeedCheckPostsService getNeedCheckPostsService;

@Test
void 확인이_필요한_게시물을_조회한다() {
// given
final Long aliceId = 중재자_권한으로_ALICE를_저장한다();
final Long bobId = BOB을_저장한다();
interests.save(INTEREST.생성());

final Group group = groups.save(aliceId, GROUP_1.생성());
final Quest quest = quests.save(QUEST_1.보상_없이_세부사항을_설정한다(QUEST_1.그룹_퀘스트_생성(group)));
final Long questId = quest.getId();

groupUsers.addUser(GroupUser.createGroupMember(bobId, group));

participants.saveWithUserIdAndQuestId(aliceId, questId);
participants.saveWithUserIdAndQuestId(bobId, questId);

final Post post1 = POST_1.생성(aliceId, questId);
post1.needCheck();
final Post post2 = POST_1.생성(aliceId, questId);
post2.needCheck();

final List<Long> actual = List.of(posts.save(post1).getId(),
posts.save(post2).getId());
posts.save(POST_1.생성(bobId));

// when
final List<Long> expected = getNeedCheckPostsService.invoke(aliceId, group.getId()).posts()
.stream().map(SimplePostResponse::id).toList();

// then
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
}

@Test
void 관리자가_아니라면_조회할_수_없다() {
// given
final Long aliceId = 중재자_권한으로_ALICE를_저장한다();
final Long bobId = BOB을_저장한다();
interests.save(INTEREST.생성());

final Group group = groups.save(aliceId, GROUP_1.생성());
final Quest quest = quests.save(QUEST_1.보상_없이_세부사항을_설정한다(QUEST_1.그룹_퀘스트_생성(group)));
final Long questId = quest.getId();

groupUsers.addUser(GroupUser.createGroupMember(bobId, group));

// when & then
assertThatThrownBy(() -> getNeedCheckPostsService.invoke(bobId, questId))
.isInstanceOf(InvalidDomainException.class)
.hasMessageContaining(NOT_GROUP_MANAGER.getMessage());
}
}
Loading

0 comments on commit 7fd825e

Please sign in to comment.