Skip to content

Commit

Permalink
Merge pull request #167 from DayByQuest/test/questService
Browse files Browse the repository at this point in the history
[Test] 퀘스트 서비스 테스트를 작성한다
  • Loading branch information
vectorch9 authored Dec 21, 2023
2 parents 6f781e5 + 4677136 commit c3257d4
Show file tree
Hide file tree
Showing 11 changed files with 477 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/main/java/daybyquest/quest/domain/Quest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import jakarta.persistence.EntityListeners;
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 @@ -92,7 +93,7 @@ public class Quest {
@CreatedDate
private LocalDateTime createdAt;

@ElementCollection
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "quest_image", joinColumns = @JoinColumn(name = "quest_id"))
private List<Image> images;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package daybyquest.quest.application;

import static daybyquest.quest.domain.QuestCategory.NORMAL;
import static daybyquest.support.fixture.BadgeFixtures.BADGE_1;
import static daybyquest.support.fixture.QuestFixtures.QUEST_1;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import daybyquest.quest.domain.Quest;
import daybyquest.quest.dto.response.QuestResponse;
import daybyquest.support.test.ServiceTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class GetQuestByIdServiceTest extends ServiceTest {

@Autowired
private GetQuestByIdService getQuestByIdService;

@Test
void 퀘스트를_조회한다() {
// given
final Long aliceId = ALICE를_저장한다();
final Long badgeId = badges.save(BADGE_1.생성()).getId();

final Quest expected = QUEST_1.일반_퀘스트_생성(badgeId);
QUEST_1.세부사항을_설정한다(expected);
final Long questId = quests.save(expected).getId();

// when
final QuestResponse response = getQuestByIdService.invoke(aliceId, questId);

// then
assertAll(() -> {
assertThat(response.id()).isEqualTo(questId);
assertThat(response.category()).isEqualTo(NORMAL.toString());
assertThat(response.title()).isEqualTo(QUEST_1.title);
assertThat(response.content()).isEqualTo(QUEST_1.content);
assertThat(response.interest()).isEqualTo(QUEST_1.interest);
assertThat(response.rewardCount()).isEqualTo(QUEST_1.rewardCount);
assertThat(response.imageIdentifier()).isEqualTo(QUEST_1.imageIdentifier);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package daybyquest.quest.application;

import static daybyquest.support.fixture.BadgeFixtures.BADGE_1;
import static daybyquest.support.fixture.QuestFixtures.QUEST_1;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import daybyquest.quest.domain.Quest;
import daybyquest.quest.dto.response.QuestImagesResponse;
import daybyquest.support.test.ServiceTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class GetQuestImagesServiceTest extends ServiceTest {

@Autowired
private GetQuestImagesService getQuestImagesService;

@Test
void 퀘스트_예시_사진을_조회한다() {
// given
final Long badgeId = badges.save(BADGE_1.생성()).getId();

final Quest expected = QUEST_1.일반_퀘스트_생성(badgeId);
QUEST_1.세부사항을_설정한다(expected);
final Long questId = quests.save(expected).getId();

// when
final QuestImagesResponse response = getQuestImagesService.invoke(questId);

// then
assertAll(() -> {
assertThat(response.description()).isEqualTo(QUEST_1.imageDescription);
assertThat(response.imageIdentifiers())
.containsExactlyInAnyOrderElementsOf(QUEST_1.imageIdentifiers);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package daybyquest.quest.application;

import static daybyquest.support.fixture.BadgeFixtures.BADGE_1;
import static daybyquest.support.fixture.QuestFixtures.QUEST_1;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import daybyquest.quest.domain.Quest;
import daybyquest.quest.dto.request.SaveQuestDetailRequest;
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 SaveQuestDetailServiceTest extends ServiceTest {

@Autowired
private SaveQuestDetailService saveQuestDetailService;

@Test
void 퀘스트_세부사항을_설정한다() {
// given
final Long badgeId = badges.save(BADGE_1.생성()).getId();
final Long questId = quests.save(QUEST_1.일반_퀘스트_생성(badgeId)).getId();

final Quest quest = QUEST_1.일반_퀘스트_생성(questId, badgeId);
QUEST_1.세부사항을_설정한다(quest);
final SaveQuestDetailRequest request = 퀘스트_세부사항_설정_요청(quest);

// when
saveQuestDetailService.invoke(questId, request);

// then
final Quest actual = quests.getById(questId);
assertAll(() -> {
assertThat(actual.getId()).isEqualTo(questId);
assertThat(actual.getTitle()).isEqualTo(QUEST_1.title);
assertThat(actual.getContent()).isEqualTo(QUEST_1.content);
assertThat(actual.getInterestName()).isEqualTo(QUEST_1.interest);
assertThat(actual.getLabel()).isEqualTo(QUEST_1.label);
assertThat(actual.getRewardCount()).isEqualTo(QUEST_1.rewardCount);
});
}

private SaveQuestDetailRequest 퀘스트_세부사항_설정_요청(final Quest quest) {
final SaveQuestDetailRequest request = new SaveQuestDetailRequest();
ReflectionTestUtils.setField(request, "title", quest.getTitle());
ReflectionTestUtils.setField(request, "content", quest.getContent());
ReflectionTestUtils.setField(request, "interest", quest.getInterestName());
ReflectionTestUtils.setField(request, "label", quest.getLabel());
ReflectionTestUtils.setField(request, "rewardCount", quest.getRewardCount());
return request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package daybyquest.quest.application;

import static daybyquest.global.error.ExceptionCode.ALREADY_EXIST_REWARD;
import static daybyquest.support.fixture.BadgeFixtures.BADGE_1;
import static daybyquest.support.fixture.QuestFixtures.QUEST_1;
import static daybyquest.support.fixture.QuestFixtures.QUEST_2;
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.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.then;

import daybyquest.global.error.exception.InvalidDomainException;
import daybyquest.global.error.exception.NotExistBadgeException;
import daybyquest.image.domain.Image;
import daybyquest.quest.domain.Quest;
import daybyquest.quest.dto.request.SaveQuestRequest;
import daybyquest.support.test.ServiceTest;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.multipart.MultipartFile;

public class SaveQuestServiceTest extends ServiceTest {

@Autowired
private SaveQuestService saveQuestService;

@Test
void 퀘스트를_저장한다() {
// given
final Long badgeId = badges.save(BADGE_1.생성()).getId();
final SaveQuestRequest request = 퀘스트_생성_요청(QUEST_1.일반_퀘스트_생성(badgeId));
final List<MultipartFile> files = 사진_파일(QUEST_1.imageIdentifiers);

// when
final Long questId = saveQuestService.invoke(request, files);

// then
final Quest quest = quests.getById(questId);
final List<String> actualFiles = quest.getImages().stream().map(Image::getIdentifier).toList();
assertAll(() -> {
assertThat(quest.getId()).isEqualTo(questId);
assertThat(quest.getImageDescription()).isEqualTo(QUEST_1.imageDescription);
assertThat(quest.getBadgeId()).isEqualTo(badgeId);
assertThat(quest.getImage().getIdentifier()).isEqualTo(BADGE_1.imageIdentifier);
assertThat(actualFiles).containsExactlyInAnyOrderElementsOf(QUEST_1.imageIdentifiers);
then(questClient).should().requestLabels(eq(questId), any(), any());
});
}

@Test
void 존재하지_않는_뱃지를_보상으로_지정할_수_없다() {
// given
final SaveQuestRequest request = 퀘스트_생성_요청(QUEST_1.일반_퀘스트_생성(1L));
final List<MultipartFile> files = 사진_파일(QUEST_1.imageIdentifiers);

// when & then
assertThatThrownBy(() -> saveQuestService.invoke(request, files))
.isInstanceOf(NotExistBadgeException.class);
}

@Test
void 이미_보상으로_지정된_뱃지를_보상으로_지정할_수_없다() {
// given
final Long badgeId = badges.save(BADGE_1.생성()).getId();
quests.save(QUEST_2.일반_퀘스트_생성(badgeId));

final SaveQuestRequest request = 퀘스트_생성_요청(QUEST_1.일반_퀘스트_생성(badgeId));
final List<MultipartFile> files = 사진_파일(QUEST_1.imageIdentifiers);

// when & then
assertThatThrownBy(() -> saveQuestService.invoke(request, files))
.isInstanceOf(InvalidDomainException.class)
.hasMessageContaining(ALREADY_EXIST_REWARD.getMessage());
}

private SaveQuestRequest 퀘스트_생성_요청(final Quest quest) {
final SaveQuestRequest request = new SaveQuestRequest();
ReflectionTestUtils.setField(request, "badgeId", quest.getBadgeId());
ReflectionTestUtils.setField(request, "imageDescription", quest.getImageDescription());
return request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package daybyquest.quest.application;

import static daybyquest.support.fixture.BadgeFixtures.BADGE_1;
import static daybyquest.support.fixture.BadgeFixtures.BADGE_2;
import static daybyquest.support.fixture.BadgeFixtures.BADGE_3;
import static daybyquest.support.fixture.QuestFixtures.QUEST_1;
import static daybyquest.support.fixture.QuestFixtures.QUEST_2;
import static org.assertj.core.api.Assertions.assertThat;

import daybyquest.quest.dto.response.PageQuestsResponse;
import daybyquest.quest.dto.response.QuestResponse;
import daybyquest.support.test.ServiceTest;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class SearchQuestServiceTest extends ServiceTest {

@Autowired
private SearchQuestService searchQuestService;

@Test
void 퀘스트를_검색한다() {
// given
final Long aliceId = ALICE를_저장한다();
final Long badge1Id = badges.save(BADGE_1.생성()).getId();
final Long badge2Id = badges.save(BADGE_2.생성()).getId();
final Long badge3Id = badges.save(BADGE_3.생성()).getId();

final List<Long> expected = List.of(
quests.save(QUEST_1.세부사항을_설정한다(QUEST_1.일반_퀘스트_생성(badge1Id))).getId(),
quests.save(QUEST_1.세부사항을_설정한다(QUEST_1.일반_퀘스트_생성(badge2Id))).getId());
quests.save(QUEST_2.세부사항을_설정한다(QUEST_2.일반_퀘스트_생성(badge3Id)));

// when
final PageQuestsResponse response = searchQuestService.invoke(aliceId, QUEST_1.title, 페이지());
final List<Long> actual = response.quests().stream().map(QuestResponse::id).toList();

// then
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package daybyquest.quest.application.group;

import static daybyquest.support.fixture.GroupFixtures.GROUP_1;
import static daybyquest.support.fixture.GroupFixtures.GROUP_2;
import static daybyquest.support.fixture.InterestFixtures.INTEREST;
import static daybyquest.support.fixture.QuestFixtures.QUEST_1;
import static org.assertj.core.api.Assertions.assertThat;

import daybyquest.group.domain.Group;
import daybyquest.quest.dto.response.MultipleQuestsResponse;
import daybyquest.quest.dto.response.QuestResponse;
import daybyquest.support.test.ServiceTest;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class GetGroupQuestsServiceTest extends ServiceTest {

@Autowired
private GetGroupQuestsService getGroupQuestsService;

@Test
void 그룹_퀘스트_목록을_조회한다() {
// given
final Long aliceId = 중재자_권한으로_ALICE를_저장한다();
interests.save(INTEREST.생성());
final Group group1 = groups.save(aliceId, GROUP_1.생성());
final Group group2 = groups.save(aliceId, GROUP_2.생성());

final List<Long> expected = List.of(
quests.save(QUEST_1.보상_없이_세부사항을_설정한다(QUEST_1.그룹_퀘스트_생성(group1))).getId(),
quests.save(QUEST_1.보상_없이_세부사항을_설정한다(QUEST_1.그룹_퀘스트_생성(group1))).getId());
quests.save(QUEST_1.보상_없이_세부사항을_설정한다(QUEST_1.그룹_퀘스트_생성(group2)));

// when
final MultipleQuestsResponse response = getGroupQuestsService.invoke(aliceId, group1.getId());
final List<Long> actual = response.quests().stream().map(QuestResponse::id).toList();

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

0 comments on commit c3257d4

Please sign in to comment.