Skip to content

Commit

Permalink
[#60] feat: 질문을 제출하지 않은 다음 질문을 가져오도록 변경
Browse files Browse the repository at this point in the history
- 정책이 변경됨에 따라 변경
- 뒤로가기를 고려하여 일관된 질문을 보내줄 수 있도록 랜덤에서 next로 변경
  • Loading branch information
NaMinhyeok committed Feb 6, 2025
1 parent 5374e48 commit 0fec92a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class CustomException extends RuntimeException {
new CustomException(ErrorType.SURVEY_NOT_FOUND);
public static final CustomException NOT_READY_FOR_NEXT_BUNDLE =
new CustomException(ErrorType.NOT_READY_FOR_NEXT_BUNDLE);
public static final CustomException ALREADY_COMPLETED_SURVEY_BUNDLE =
new CustomException(ErrorType.ALREADY_COMPLETED_SURVEY_BUNDLE);

private final ErrorType errorType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public enum ErrorType {
// survey
NOT_READY_FOR_NEXT_BUNDLE(
HttpStatus.BAD_REQUEST, ErrorCode.E400, "다음 번들을 진행할 준비가 되지 않았습니다.", LogLevel.WARN),

ALREADY_COMPLETED_SURVEY_BUNDLE(
HttpStatus.BAD_REQUEST, ErrorCode.E400, "이미 완료된 설문 번들입니다.", LogLevel.WARN),
;

private final HttpStatus status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.nexters.jaknaesocore.common.model.BaseTimeEntity;
import org.nexters.jaknaesocore.common.support.error.CustomException;

@Getter
@NoArgsConstructor
Expand All @@ -27,12 +28,22 @@ public Survey getUnSubmittedSurvey(final List<Survey> submittedSurvey) {
surveys.stream().filter(survey -> !submittedSurvey.contains(survey)).toList();

if (list.isEmpty()) {
throw new IllegalStateException("모든 설문을 완료하셨습니다.");
throw CustomException.ALREADY_COMPLETED_SURVEY_BUNDLE;
}
int randomNum = ThreadLocalRandom.current().nextInt(list.size());
return list.get(randomNum);
}

public Survey getNextSurvey(final List<Survey> submittedSurvey) {
List<Survey> list =
surveys.stream().filter(survey -> !submittedSurvey.contains(survey)).toList();

if (list.isEmpty()) {
throw CustomException.ALREADY_COMPLETED_SURVEY_BUNDLE;
}
return list.getFirst();
}

public boolean isAllSubmitted(final List<SurveySubmission> submissions) {
return surveys.stream()
.allMatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public SurveyResponse getNextSurvey(final Long bundleId, final Long memberId) {
.findById(bundleId)
.orElseThrow(() -> new IllegalArgumentException("설문 번들을 찾을 수 없습니다."));
List<Survey> submittedSurvey = getSubmittedSurvey(memberId);
Survey unSubmittedSurvey = bundle.getUnSubmittedSurvey(submittedSurvey);
Survey nextSurvey = bundle.getNextSurvey(submittedSurvey);

return SurveyResponse.of(unSubmittedSurvey);
return SurveyResponse.of(nextSurvey);
}

private List<Survey> getSubmittedSurvey(final Long memberId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.nexters.jaknaesocore.common.support.error.CustomException;
import org.springframework.test.util.ReflectionTestUtils;

class SurveyBundleTest {
Expand Down Expand Up @@ -47,8 +48,7 @@ void getUnSubmittedSurveyByList_AllSubmitted() {
// when & then
thenThrownBy(
() -> surveyBundle.getUnSubmittedSurvey(List.of(survey1, survey2, survey3, survey4)))
.isInstanceOf(IllegalStateException.class)
.hasMessage("모든 설문을 완료하셨습니다.");
.isEqualTo(CustomException.ALREADY_COMPLETED_SURVEY_BUNDLE);
}

@Nested
Expand Down Expand Up @@ -117,6 +117,57 @@ class whenNotAllSubmitted {
}
}

@DisplayName("getNextSurvey 메서드는")
@Nested
class getNextSurvey {
@DisplayName("번들의 모든 설문을 제출한 경우")
@Nested
class whenAllSubmitted {
@DisplayName("예외를 던진다.")
@Test
void 모든_설문을_제출했을_때_다음_설문을_가져오려고_할_때() {
// given
SurveyBundle surveyBundle = new SurveyBundle();

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

surveyBundle.getSurveys().addAll(List.of(survey1, survey2, survey3, survey4));

// when
// then
thenThrownBy(() -> surveyBundle.getNextSurvey(List.of(survey1, survey2, survey3, survey4)))
.isEqualTo(CustomException.ALREADY_COMPLETED_SURVEY_BUNDLE);
}
}

@DisplayName("번들의 모든 설문을 제출하지 않은 경우")
@Nested
class whenNotAllSubmitted {
@Test
void 번들_내부의_설문_중_제출하지않은_다음_설문을_가져온다() {
// given
SurveyBundle surveyBundle = new SurveyBundle();

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

surveyBundle.getSurveys().addAll(List.of(survey1, survey2, survey3, survey4));
// when
Survey nextSurvey = surveyBundle.getNextSurvey(List.of(survey1, survey2));

// then
then(nextSurvey).extracting("id", "content").containsExactly(3L, "나의 행복 지수는");
}
}
}

private BalanceSurvey createBalanceSurvey(String content, SurveyBundle surveyBundle, Long id) {
BalanceSurvey survey = new BalanceSurvey(content, surveyBundle);
ReflectionTestUtils.setField(survey, "id", id);
Expand Down

0 comments on commit 0fec92a

Please sign in to comment.