-
Notifications
You must be signed in to change notification settings - Fork 2
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
[BE] refactor: 필수 질문이 아닌 경우 최소 글자수 제한 제거 #589
Changes from 2 commits
6a29c0e
a4b385e
63da839
a74b3b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import reviewme.question.domain.Question; | ||
import reviewme.question.repository.QuestionRepository; | ||
import reviewme.review.domain.TextAnswer; | ||
import reviewme.review.domain.exception.InvalidTextAnswerLengthException; | ||
|
@@ -11,26 +12,28 @@ | |
@RequiredArgsConstructor | ||
public class TextAnswerValidator { | ||
|
||
private static final int ZERO_LENGTH = 0; | ||
private static final int MIN_LENGTH = 20; | ||
private static final int MAX_LENGTH = 1_000; | ||
|
||
private final QuestionRepository questionRepository; | ||
|
||
public void validate(TextAnswer textAnswer) { | ||
validateExistQuestion(textAnswer); | ||
validateLength(textAnswer); | ||
} | ||
Question question = questionRepository.findById(textAnswer.getQuestionId()) | ||
.orElseThrow(() -> new SubmittedQuestionNotFoundException(textAnswer.getQuestionId())); | ||
|
||
private void validateExistQuestion(TextAnswer textAnswer) { | ||
if (!questionRepository.existsById(textAnswer.getQuestionId())) { | ||
throw new SubmittedQuestionNotFoundException(textAnswer.getQuestionId()); | ||
} | ||
validateLength(textAnswer, question); | ||
} | ||
|
||
private void validateLength(TextAnswer textAnswer) { | ||
private void validateLength(TextAnswer textAnswer, Question question) { | ||
int answerLength = textAnswer.getContent().length(); | ||
if (answerLength < MIN_LENGTH || answerLength > MAX_LENGTH) { | ||
throw new InvalidTextAnswerLengthException(answerLength, MIN_LENGTH, MAX_LENGTH); | ||
|
||
if (question.isRequired() && (answerLength < MIN_LENGTH || answerLength > MAX_LENGTH)) { | ||
throw new InvalidTextAnswerLengthException(question.getId(), answerLength, MIN_LENGTH, MAX_LENGTH); | ||
} | ||
|
||
if (!question.isRequired() && answerLength > MAX_LENGTH) { | ||
throw new InvalidTextAnswerLengthException(question.getId(), answerLength, ZERO_LENGTH, MAX_LENGTH); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로그에서만 사용되는 것이라면 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 조하요 반영했습니다~ |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import static org.assertj.core.api.Assertions.assertThatCode; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static reviewme.fixture.QuestionFixture.서술형_옵션_질문; | ||
import static reviewme.fixture.QuestionFixture.서술형_필수_질문; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
@@ -37,7 +38,7 @@ class TextAnswerValidatorTest { | |
|
||
@ParameterizedTest | ||
@ValueSource(ints = {19, 10001}) | ||
void 답변_길이가_유효하지_않으면_예외가_발생한다(int length) { | ||
void 필수_질문의_답변_길이가_유효하지_않으면_예외가_발생한다(int length) { | ||
// given | ||
String content = "답".repeat(length); | ||
Question savedQuestion = questionRepository.save(서술형_필수_질문()); | ||
|
@@ -47,4 +48,27 @@ class TextAnswerValidatorTest { | |
assertThatThrownBy(() -> textAnswerValidator.validate(textAnswer)) | ||
.isInstanceOf(InvalidTextAnswerLengthException.class); | ||
} | ||
|
||
@Test | ||
void 선택_질문의_답변_길이가_유효하지_않으면_예외가_발생한다() { | ||
// given | ||
String content = "답".repeat(10001); | ||
Question savedQuestion = questionRepository.save(서술형_옵션_질문()); | ||
TextAnswer textAnswer = new TextAnswer(savedQuestion.getId(), content); | ||
|
||
Comment on lines
+52
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 테스트 코드도 잘 작성된 것 같아요😇 |
||
// when, then | ||
assertThatThrownBy(() -> textAnswerValidator.validate(textAnswer)) | ||
.isInstanceOf(InvalidTextAnswerLengthException.class); | ||
} | ||
|
||
@Test | ||
void 선택_질문은_최소_글자수_제한을_받지_않는다() { | ||
// given | ||
String content = "답".repeat(1); | ||
Question savedQuestion = questionRepository.save(서술형_옵션_질문()); | ||
TextAnswer textAnswer = new TextAnswer(savedQuestion.getId(), content); | ||
|
||
// when, then | ||
assertThatCode(() -> textAnswerValidator.validate(textAnswer)).doesNotThrowAnyException(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 좋네요 이 함수 가독성 짱짱~