Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(#60) 질문 제공 방식 순차방식 구현 #63

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,19 @@ void getNextSurvey() {
SurveyBundle surveyBundle = new SurveyBundle();
surveyBundleRepository.save(surveyBundle);
BalanceSurvey balanceSurvey = new BalanceSurvey("질문내용", surveyBundle);
surveyRepository.save(balanceSurvey);
MultipleChoiceSurvey multipleSurvey = new MultipleChoiceSurvey("다음 질문내용", surveyBundle);
surveyRepository.saveAll(List.of(balanceSurvey, multipleSurvey));
List<KeywordScore> scores =
List.of(
KeywordScore.builder().keyword(Keyword.ACHIEVEMENT).score(BigDecimal.ONE).build(),
KeywordScore.builder().keyword(Keyword.BENEVOLENCE).score(BigDecimal.TWO).build());
SurveyOption option =
SurveyOption.builder().survey(balanceSurvey).scores(scores).content("질문 옵션 내용").build();
surveyOptionRepository.save(option);
SurveyOption multipleOption1 =
SurveyOption.builder().survey(multipleSurvey).scores(scores).content("1점").build();
SurveyOption multipleOption2 =
SurveyOption.builder().survey(multipleSurvey).scores(scores).content("2점").build();
surveyOptionRepository.saveAll(List.of(option, multipleOption1, multipleOption2));

// when
SurveyResponse survey = surveyService.getNextSurvey(surveyBundle.getId(), member.getId());
Expand Down