Skip to content

Commit

Permalink
[Test] 퀘스트, 참여 도메인과 쿼리 테스트를 작성한다 (#85)
Browse files Browse the repository at this point in the history
* refactor: 퀘스트 검증 로직을 수정하고 추가한다

* test: 퀘스트 도메인 로직 테스트를 작성한다

* test: Quests 테스트를 작성한다

* refactor: Participant 도메인에 검증 로직을 추가한다

* test: Participant 도메인 테스트를 작성한다

* refactor: IncreaseLinkedCountListener가 하나의 트랜잭션에서 실행되도록 수정한다

* test: Participants 테스트를 작성한다

* test: QuestDaoQuerydslImpl 테스트를 작성한다
  • Loading branch information
vectorch9 authored Nov 4, 2023
1 parent ff47099 commit 0ec7be4
Show file tree
Hide file tree
Showing 11 changed files with 1,117 additions and 24 deletions.
11 changes: 7 additions & 4 deletions src/main/java/daybyquest/participant/domain/Participant.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public class Participant {
@Enumerated(EnumType.STRING)
private ParticipantState state;

private int linkedCount;
private Long linkedCount;

public Participant(Long userId, Quest quest) {
this.userId = userId;
this.quest = quest;
this.state = DOING;
this.linkedCount = 0;
this.linkedCount = 0L;
}

public Long getQuestId() {
Expand All @@ -64,8 +64,8 @@ private void validateDidNotTakeReward() {
}

private void validateRewardCount() {
if (quest.getRewardCount() != null && quest.getRewardCount() != 0
&& linkedCount < quest.getRewardCount()) {
if (quest.getRewardCount() == null || quest.getRewardCount() == 0
|| linkedCount < quest.getRewardCount()) {
throw new InvalidDomainException(NOT_FINISHABLE_QUEST);
}
}
Expand All @@ -85,6 +85,9 @@ public void doContinue() {
}

public void increaseLinkedCount() {
if (state != DOING && state != CONTINUE) {
throw new InvalidDomainException();
}
linkedCount += 1;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package daybyquest.participant.listener;

import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW;

import daybyquest.participant.domain.Participant;
import daybyquest.participant.domain.Participants;
import daybyquest.post.domain.SuccessfullyPostLinkedEvent;
import org.springframework.scheduling.annotation.Async;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.event.TransactionalEventListener;

@Component
public class IncreaseLinkedCountListener {
Expand All @@ -19,9 +16,8 @@ public IncreaseLinkedCountListener(final Participants participants) {
this.participants = participants;
}

@Async
@Transactional(propagation = REQUIRES_NEW)
@TransactionalEventListener(fallbackExecution = true)
@Transactional
@EventListener
public void listenSuccessfullyPostLinkedEvent(final SuccessfullyPostLinkedEvent event) {
final Participant participant = participants.getByUserIdAndQuestId(event.getUserId(),
event.getQuestId());
Expand Down
52 changes: 39 additions & 13 deletions src/main/java/daybyquest/quest/domain/Quest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
@EntityListeners(AuditingEntityListener.class)
public class Quest {

private static final int MAX_TITLE_LENGTH = 15;
private static final int MAX_TITLE_LENGTH = 30;

private static final int MAX_CONTENT_LENGTH = 300;

private static final int MAX_IMAGE_DESCRIPTION_LENGTH = 300;
private static final int MAX_IMAGE_DESCRIPTION_LENGTH = 100;

private static final int MAX_REWARD_COUNT = 100;
private static final Long MIN_REWARD_COUNT = 1L;

private static final Long MAX_REWARD_COUNT = 365L;

private static final int IMAGE_COUNT = 3;

Expand Down Expand Up @@ -81,9 +83,9 @@ public class Quest {
@CollectionTable(name = "quest_image", joinColumns = @JoinColumn(name = "quest_id"))
private List<Image> images;

private Quest(Long groupId, Long badgeId, String interestName, String title, String content,
QuestCategory category, Long rewardCount, LocalDateTime expiredAt,
String imageDescription, List<Image> images) {
private Quest(final Long groupId, final Long badgeId, final String interestName, final String title,
final String content, final QuestCategory category, final Long rewardCount,
final LocalDateTime expiredAt, final String imageDescription, final List<Image> images) {
this.groupId = groupId;
this.badgeId = badgeId;
this.interestName = interestName;
Expand All @@ -98,26 +100,35 @@ private Quest(Long groupId, Long badgeId, String interestName, String title, Str
validate();
}

public static Quest createNormalQuest(Long badgeId, String interestName, String title,
String content,
Long rewardCount, String imageDescription, List<Image> images) {
public static Quest createNormalQuest(final Long badgeId, final String interestName, final String title,
final String content, final Long rewardCount, final String imageDescription,
final List<Image> images) {
return new Quest(null, badgeId, interestName, title, content, QuestCategory.NORMAL, rewardCount
, null, imageDescription, images);
}

public static Quest createGroupQuest(Long groupId, String interestName, String title,
String content,
LocalDateTime expiredAt, String imageDescription, List<Image> images) {
public static Quest createGroupQuest(final Long groupId, final String interestName, final String title,
final String content, final LocalDateTime expiredAt, final String imageDescription,
final List<Image> images) {
validateGroupId(groupId);
return new Quest(groupId, null, interestName, title, content, QuestCategory.GROUP, null
, expiredAt, imageDescription, images);
}

private static void validateGroupId(final Long groupId) {
if (groupId == null) {
throw new InvalidDomainException();
}
}

private void validate() {
validateTitle();
validateContent();
validateImageDescription();
validateRewardCount();
validateImageCount();
validateReward();
validateExpiredAt();
}

private void validateTitle() {
Expand All @@ -139,7 +150,10 @@ private void validateImageDescription() {
}

private void validateRewardCount() {
if (rewardCount > MAX_REWARD_COUNT) {
if (rewardCount == null) {
return;
}
if (rewardCount < MIN_REWARD_COUNT || rewardCount > MAX_REWARD_COUNT) {
throw new InvalidDomainException();
}
}
Expand All @@ -150,6 +164,18 @@ private void validateImageCount() {
}
}

private void validateReward() {
if (rewardCount == null ^ badgeId == null) {
throw new InvalidDomainException();
}
}

private void validateExpiredAt() {
if (expiredAt != null && expiredAt.isBefore(LocalDateTime.now())) {
throw new InvalidDomainException();
}
}

public void setLabel(final String label) {
if (this.state != NEED_LABEL) {
throw new InvalidDomainException();
Expand Down
Loading

0 comments on commit 0ec7be4

Please sign in to comment.