Skip to content

Commit

Permalink
[#31] feat: 번들 존재하지 않는 경우 예외처리 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
NaMinhyeok committed Feb 3, 2025
1 parent b459fcd commit 6042344
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class CustomException extends RuntimeException {
new CustomException(ErrorType.INCORRECT_TOKEN_FORMAT);
public static final CustomException INVALID_APPLE_ID_TOKEN =
new CustomException(ErrorType.INVALID_APPLE_ID_TOKEN);
public static final CustomException NOT_READY_FOR_NEXT_BUNDLE =
new CustomException(ErrorType.NOT_READY_FOR_NEXT_BUNDLE);

private final ErrorType errorType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public enum ErrorType {
// business
INVALID_APPLE_ID_TOKEN(
HttpStatus.BAD_REQUEST, ErrorCode.E400, "유효하지 않은 애플로그인입니다.", LogLevel.WARN),

// survey
NOT_READY_FOR_NEXT_BUNDLE(
HttpStatus.BAD_REQUEST, ErrorCode.E400, "다음 번들을 진행할 준비가 되지 않았습니다.", LogLevel.WARN),
;

private final HttpStatus status;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.nexters.jaknaesocore.domain.survey.repository;

import java.util.Optional;
import org.nexters.jaknaesocore.domain.survey.model.SurveyBundle;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SurveyBundleRepository extends JpaRepository<SurveyBundle, Long> {}
public interface SurveyBundleRepository extends JpaRepository<SurveyBundle, Long> {

Optional<SurveyBundle> findFirstByIdGreaterThanOrderByIdAsc(final Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.nexters.jaknaesocore.common.support.error.CustomException;
import org.nexters.jaknaesocore.domain.survey.dto.SurveyHistoryDetailResponse;
import org.nexters.jaknaesocore.domain.survey.dto.SurveyHistoryResponse;
import org.nexters.jaknaesocore.domain.survey.dto.SurveyResponse;
Expand Down Expand Up @@ -50,7 +51,12 @@ public SurveyHistoryResponse getSurveyHistory(final Long memberId) {
List<SurveySubmission> submissions = findSubmissionsForBundle(memberId, currentBundleId);

if (bundle.isAllSubmitted(submissions)) {
return createNextBundleSurveyHistory(currentBundleId);
Long nextBundleId =
surveyBundleRepository
.findFirstByIdGreaterThanOrderByIdAsc(currentBundleId)
.map(SurveyBundle::getId)
.orElseThrow(() -> CustomException.NOT_READY_FOR_NEXT_BUNDLE);
return createNextBundleSurveyHistory(nextBundleId);
}
return createCurrentBundleSurveyHistory(currentBundleId, submissions);
}
Expand All @@ -70,8 +76,8 @@ private SurveyHistoryResponse createInitialSurveyHistory() {
return new SurveyHistoryResponse(1L, List.of(), 1);
}

private SurveyHistoryResponse createNextBundleSurveyHistory(final Long currentBundleId) {
return new SurveyHistoryResponse(currentBundleId + 1, List.of(), 1);
private SurveyHistoryResponse createNextBundleSurveyHistory(final Long bundleId) {
return new SurveyHistoryResponse(bundleId, List.of(), 1);
}

private SurveyHistoryResponse createCurrentBundleSurveyHistory(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.nexters.jaknaesocore.domain.survey.repository;

import static org.assertj.core.api.BDDAssertions.*;
import static org.junit.jupiter.api.Assertions.*;

import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.nexters.jaknaesocore.common.support.IntegrationTest;
import org.nexters.jaknaesocore.domain.survey.model.SurveyBundle;
import org.springframework.beans.factory.annotation.Autowired;

class SurveyBundleRepositoryTest extends IntegrationTest {

@Autowired private SurveyBundleRepository surveyBundleRepository;

@AfterEach
void tearDown() {
surveyBundleRepository.deleteAllInBatch();
}

@Test
void 주어진_ID_보다_큰_번들_중_가장_작은_ID를_가진_번들을_찾는다() {
// given
SurveyBundle surveyBundle1 = new SurveyBundle();
SurveyBundle surveyBundle2 = new SurveyBundle();
SurveyBundle surveyBundle3 = new SurveyBundle();
SurveyBundle surveyBundle4 = new SurveyBundle();

surveyBundleRepository.saveAll(
List.of(surveyBundle1, surveyBundle2, surveyBundle3, surveyBundle4));
// when
Optional<SurveyBundle> result =
surveyBundleRepository.findFirstByIdGreaterThanOrderByIdAsc(surveyBundle2.getId());
// then
then(result)
.hasValueSatisfying(
surveyBundle -> then(surveyBundle.getId()).isEqualTo(surveyBundle3.getId()));
}

@Test
void 주어진_ID보다_큰_번들이_없으면_빈_값을_반환한다() {
// given
SurveyBundle surveyBundle1 = new SurveyBundle();
SurveyBundle surveyBundle2 = new SurveyBundle();
SurveyBundle surveyBundle3 = new SurveyBundle();
SurveyBundle surveyBundle4 = new SurveyBundle();

surveyBundleRepository.saveAll(
List.of(surveyBundle1, surveyBundle2, surveyBundle3, surveyBundle4));
// when
Optional<SurveyBundle> result = surveyBundleRepository.findFirstByIdGreaterThanOrderByIdAsc(4L);
// then
then(result).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.nexters.jaknaesocore.common.support.IntegrationTest;
import org.nexters.jaknaesocore.common.support.error.CustomException;
import org.nexters.jaknaesocore.domain.member.model.Member;
import org.nexters.jaknaesocore.domain.member.repository.MemberRepository;
import org.nexters.jaknaesocore.domain.survey.dto.SurveyHistoryResponse;
Expand Down Expand Up @@ -156,10 +157,64 @@ void getNextSurvey() {

@Test
void 설문_기록이_번들_크기만큼_있으면_다음_번들ID를_제공한다() {
// given
Member member = Member.create();
memberRepository.save(member);
SurveyBundle surveyBundle1 = new SurveyBundle();
SurveyBundle surveyBundle2 = new SurveyBundle();

surveyBundleRepository.saveAll(List.of(surveyBundle1, surveyBundle2));

BalanceSurvey survey1 = new BalanceSurvey("대학 동기 모임에서 나의 승진 이야기가 나왔습니다", surveyBundle1);
BalanceSurvey survey2 = new BalanceSurvey("회사에서 팀 리더로 뽑혔습니다", surveyBundle1);
MultipleChoiceSurvey survey3 = new MultipleChoiceSurvey("나의 행복 지수는", surveyBundle1);
MultipleChoiceSurvey survey4 = new MultipleChoiceSurvey("나는 노는게 좋다.", surveyBundle1);

surveyRepository.saveAll(List.of(survey1, survey2, survey3, survey4));

List<KeywordScore> scores =
List.of(
KeywordScore.builder().keyword(Keyword.ACHIEVEMENT).score(BigDecimal.ONE).build(),
KeywordScore.builder().keyword(Keyword.BENEVOLENCE).score(BigDecimal.TWO).build());

SurveyOption option1 =
SurveyOption.builder().survey(survey1).scores(scores).content("한다.").build();
SurveyOption option2 =
SurveyOption.builder().survey(survey2).scores(scores).content("한다.").build();
SurveyOption option3 =
SurveyOption.builder().survey(survey3).scores(scores).content("3점").build();
SurveyOption option4 =
SurveyOption.builder().survey(survey4).scores(scores).content("4점").build();

surveyOptionRepository.saveAll(List.of(option1, option2, option3, option4));

SurveySubmission submission1 =
SurveySubmission.builder().member(member).selectedOption(option1).survey(survey1).build();
SurveySubmission submission2 =
SurveySubmission.builder().member(member).selectedOption(option2).survey(survey2).build();
SurveySubmission submission3 =
SurveySubmission.builder().member(member).selectedOption(option3).survey(survey3).build();
SurveySubmission submission4 =
SurveySubmission.builder().member(member).selectedOption(option4).survey(survey4).build();

surveySubmissionRepository.saveAll(List.of(submission1, submission2, submission3, submission4));

// when
SurveyHistoryResponse response = surveyService.getSurveyHistory(member.getId());

// then
then(response)
.extracting("bundleId", "nextSurveyIndex", "surveyHistoryDetails")
.containsExactly(surveyBundle1.getId() + 1L, 1, List.of());
}

@Test
void 진행할_다음_설문_번들이_존재하지_않으면_예외를_발생한다() {
// given
Member member = Member.create();
memberRepository.save(member);
SurveyBundle surveyBundle = new SurveyBundle();

surveyBundleRepository.save(surveyBundle);

BalanceSurvey survey1 = new BalanceSurvey("대학 동기 모임에서 나의 승진 이야기가 나왔습니다", surveyBundle);
Expand All @@ -185,7 +240,6 @@ void getNextSurvey() {

surveyOptionRepository.saveAll(List.of(option1, option2, option3, option4));

// 모든 설문에 대한 제출 생성
SurveySubmission submission1 =
SurveySubmission.builder().member(member).selectedOption(option1).survey(survey1).build();
SurveySubmission submission2 =
Expand All @@ -196,13 +250,10 @@ void getNextSurvey() {
SurveySubmission.builder().member(member).selectedOption(option4).survey(survey4).build();

surveySubmissionRepository.saveAll(List.of(submission1, submission2, submission3, submission4));

// when
SurveyHistoryResponse response = surveyService.getSurveyHistory(member.getId());

// then
then(response)
.extracting("bundleId", "nextSurveyIndex", "surveyHistoryDetails")
.containsExactly(surveyBundle.getId() + 1L, 1, List.of());
thenThrownBy(() -> surveyService.getSurveyHistory(member.getId()))
.isInstanceOf(CustomException.class)
.isEqualTo(CustomException.NOT_READY_FOR_NEXT_BUNDLE);
}
}

0 comments on commit 6042344

Please sign in to comment.