From 6a29c0ea3f33770106957499a1f89cb97ac6478c Mon Sep 17 00:00:00 2001 From: KIMGYUTAE Date: Tue, 10 Sep 2024 10:29:54 +0900 Subject: [PATCH 1/4] =?UTF-8?q?refactor:=20=ED=95=84=EC=88=98=20=EC=A7=88?= =?UTF-8?q?=EB=AC=B8=EC=9D=B4=20=EC=95=84=EB=8B=8C=20=EA=B2=BD=EC=9A=B0=20?= =?UTF-8?q?=EC=B5=9C=EC=86=8C=20=EA=B8=80=EC=9E=90=EC=88=98=20=EC=A0=9C?= =?UTF-8?q?=ED=95=9C=EC=9D=84=20=EB=B0=9B=EC=A7=80=20=EC=95=8A=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../InvalidTextAnswerLengthException.java | 6 ++--- .../service/module/TextAnswerValidator.java | 23 +++++++++++-------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/backend/src/main/java/reviewme/review/domain/exception/InvalidTextAnswerLengthException.java b/backend/src/main/java/reviewme/review/domain/exception/InvalidTextAnswerLengthException.java index 236531179..254a7c03e 100644 --- a/backend/src/main/java/reviewme/review/domain/exception/InvalidTextAnswerLengthException.java +++ b/backend/src/main/java/reviewme/review/domain/exception/InvalidTextAnswerLengthException.java @@ -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); } } diff --git a/backend/src/main/java/reviewme/review/service/module/TextAnswerValidator.java b/backend/src/main/java/reviewme/review/service/module/TextAnswerValidator.java index 5e22c00ff..ae0381ceb 100644 --- a/backend/src/main/java/reviewme/review/service/module/TextAnswerValidator.java +++ b/backend/src/main/java/reviewme/review/service/module/TextAnswerValidator.java @@ -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); } } } From a4b385eec0b1e3f648eafef41b725a25da5409ce Mon Sep 17 00:00:00 2001 From: KIMGYUTAE Date: Tue, 10 Sep 2024 10:30:16 +0900 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20=ED=95=84=EC=88=98=20=EC=A7=88?= =?UTF-8?q?=EB=AC=B8=EC=9D=B4=20=EC=95=84=EB=8B=8C=20=EA=B2=BD=EC=9A=B0?= =?UTF-8?q?=EC=97=90=20=EB=8C=80=ED=95=9C=20=EA=B2=80=EC=A6=9D=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/TextAnswerValidatorTest.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/backend/src/test/java/reviewme/review/service/module/TextAnswerValidatorTest.java b/backend/src/test/java/reviewme/review/service/module/TextAnswerValidatorTest.java index 6f365a37d..9ffcef873 100644 --- a/backend/src/test/java/reviewme/review/service/module/TextAnswerValidatorTest.java +++ b/backend/src/test/java/reviewme/review/service/module/TextAnswerValidatorTest.java @@ -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); + + // 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(); + } } From 63da839bb04dc4482fd3c7e3234a261e22ed2178 Mon Sep 17 00:00:00 2001 From: KIMGYUTAE Date: Sun, 15 Sep 2024 11:20:45 +0900 Subject: [PATCH 3/4] =?UTF-8?q?refactor:=20InvalidTextAnswerLengthExceptio?= =?UTF-8?q?n=20=EC=97=90=20min=EA=B0=92=20=EA=B3=A0=EC=A0=95=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=EC=9E=90=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/exception/InvalidTextAnswerLengthException.java | 6 ++++++ .../reviewme/review/service/module/TextAnswerValidator.java | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/backend/src/main/java/reviewme/review/domain/exception/InvalidTextAnswerLengthException.java b/backend/src/main/java/reviewme/review/domain/exception/InvalidTextAnswerLengthException.java index 254a7c03e..1ab677989 100644 --- a/backend/src/main/java/reviewme/review/domain/exception/InvalidTextAnswerLengthException.java +++ b/backend/src/main/java/reviewme/review/domain/exception/InvalidTextAnswerLengthException.java @@ -11,4 +11,10 @@ public InvalidTextAnswerLengthException(long questionId, int answerLength, int m log.warn("AnswerLength is out of bound - questionId: {}, answerLength: {}, minLength: {}, maxLength: {}", questionId, answerLength, minLength, maxLength, this); } + + public InvalidTextAnswerLengthException(long questionId, int answerLength, int maxLength) { + super("답변의 길이는 0자 이상 %d자 이하여야 해요.".formatted(maxLength)); + log.warn("AnswerLength is out of bound - questionId: {}, answerLength: {}, minLength: 0, maxLength: {}", + questionId, answerLength, maxLength, this); + } } diff --git a/backend/src/main/java/reviewme/review/service/module/TextAnswerValidator.java b/backend/src/main/java/reviewme/review/service/module/TextAnswerValidator.java index ae0381ceb..0d1c50de4 100644 --- a/backend/src/main/java/reviewme/review/service/module/TextAnswerValidator.java +++ b/backend/src/main/java/reviewme/review/service/module/TextAnswerValidator.java @@ -33,7 +33,7 @@ private void validateLength(TextAnswer textAnswer, Question question) { } if (!question.isRequired() && answerLength > MAX_LENGTH) { - throw new InvalidTextAnswerLengthException(question.getId(), answerLength, ZERO_LENGTH, MAX_LENGTH); + throw new InvalidTextAnswerLengthException(question.getId(), answerLength, MAX_LENGTH); } } } From a74b3b3543155e283fe34af3e8b599caeb846263 Mon Sep 17 00:00:00 2001 From: KIMGYUTAE Date: Sun, 15 Sep 2024 11:28:42 +0900 Subject: [PATCH 4/4] =?UTF-8?q?refactor:=20InvalidTextAnswerLengthExceptio?= =?UTF-8?q?n=20=EC=83=9D=EC=84=B1=EC=9E=90=20=EC=B2=B4=EC=9D=B4=EB=8B=9D?= =?UTF-8?q?=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/exception/InvalidTextAnswerLengthException.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/backend/src/main/java/reviewme/review/domain/exception/InvalidTextAnswerLengthException.java b/backend/src/main/java/reviewme/review/domain/exception/InvalidTextAnswerLengthException.java index 1ab677989..75d0c36e4 100644 --- a/backend/src/main/java/reviewme/review/domain/exception/InvalidTextAnswerLengthException.java +++ b/backend/src/main/java/reviewme/review/domain/exception/InvalidTextAnswerLengthException.java @@ -13,8 +13,6 @@ public InvalidTextAnswerLengthException(long questionId, int answerLength, int m } public InvalidTextAnswerLengthException(long questionId, int answerLength, int maxLength) { - super("답변의 길이는 0자 이상 %d자 이하여야 해요.".formatted(maxLength)); - log.warn("AnswerLength is out of bound - questionId: {}, answerLength: {}, minLength: 0, maxLength: {}", - questionId, answerLength, maxLength, this); + this(questionId, answerLength, 0, maxLength); } }