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

[BE] refactor: 필수 질문이 아닌 경우 최소 글자수 제한 제거 #589

Merged
merged 4 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -6,9 +6,9 @@
@Slf4j
public class InvalidTextAnswerLengthException extends BadRequestException {

public InvalidTextAnswerLengthException(int answerLength, int minLength, int maxLength) {
public InvalidTextAnswerLengthException(long questionId, int answerLength, int minLength, int maxLength) {
super("답변의 길이는 %d자 이상 %d자 이하여야 해요.".formatted(minLength, maxLength));
log.warn("AnswerLength is out of bound - answerLength: {}, minLength: {}, maxLength: {}",
answerLength, minLength, maxLength, this);
log.warn("AnswerLength is out of bound - questionId: {}, answerLength: {}, minLength: {}, maxLength: {}",
questionId, answerLength, minLength, maxLength, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Comment on lines +31 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 좋네요 이 함수 가독성 짱짱~

throw new InvalidTextAnswerLengthException(question.getId(), answerLength, ZERO_LENGTH, MAX_LENGTH);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

로그에서만 사용되는 것이라면 Exception(QuestionId, length, maxLength) 생성자를 만들어 min이 0일 때로 사용하면 어떨까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

조하요 반영했습니다~

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(서술형_필수_질문());
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
}
}