Skip to content

Commit

Permalink
refactor: 불필요한 검증 제거
Browse files Browse the repository at this point in the history
- 선택된 키워드와 질문이 DB에 있는지를 validator 에서 검증한 후에도, repository.getById 를 할 때 한번 더 검증이 들어간다. 따라서 'DB에 있는지'에 대한 검증을 validator 에서 할 필요는 없다는 판단 하에 해당 로직을 삭제한다.
  • Loading branch information
nayonsoso committed Aug 8, 2024
1 parent f738f9d commit 71a1950
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 88 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import org.springframework.stereotype.Component;
import reviewme.keyword.domain.exception.DuplicateKeywordException;
import reviewme.keyword.domain.exception.KeywordLimitExceedException;
import reviewme.keyword.domain.exception.KeywordsNotFoundException;
import reviewme.keyword.repository.KeywordRepository;

@Component
@RequiredArgsConstructor
Expand All @@ -15,12 +13,9 @@ public class ReviewCreationKeywordValidator {
private static final int MIN_KEYWORD_COUNT = 1;
private static final int MAX_KEYWORD_COUNT = 5;

private final KeywordRepository keywordRepository;

void validate(List<Long> selectedKeywordIds) {
validateKeywordCount(selectedKeywordIds.size());
validateUniqueKeyword(selectedKeywordIds);
validateExistsKeyword(selectedKeywordIds);
}

private void validateUniqueKeyword(List<Long> selectedKeywordIds) {
Expand All @@ -33,14 +28,6 @@ private void validateUniqueKeyword(List<Long> selectedKeywordIds) {
}
}

private void validateExistsKeyword(List<Long> selectedKeywordIds) {
boolean doesKeywordExist = selectedKeywordIds.stream()
.anyMatch(keywordRepository::existsById);
if (!doesKeywordExist) {
throw new KeywordsNotFoundException(selectedKeywordIds);
}
}

private void validateKeywordCount(int keywordsCount) {
if (keywordsCount < MIN_KEYWORD_COUNT || keywordsCount > MAX_KEYWORD_COUNT) {
throw new KeywordLimitExceedException(MIN_KEYWORD_COUNT, MAX_KEYWORD_COUNT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import reviewme.question.domain.exception.DuplicateQuestionException;
import reviewme.question.domain.exception.QuestionsNotFoundException;
import reviewme.review.repository.QuestionRepository;

@Component
@RequiredArgsConstructor
public class ReviewCreationQuestionValidator {

private final QuestionRepository questionRepository;

void validate(List<Long> questionIds) {
validateUniqueQuestion(questionIds);
validateExistsQuestion(questionIds);
}

private void validateUniqueQuestion(List<Long> questionIds) {
Expand All @@ -27,12 +22,4 @@ private void validateUniqueQuestion(List<Long> questionIds) {
throw new DuplicateQuestionException(questionIds);
}
}

private void validateExistsQuestion(List<Long> questionIds) {
boolean doesQuestionExist = questionIds.stream()
.anyMatch(questionRepository::existsById);
if (!doesQuestionExist) {
throw new QuestionsNotFoundException(questionIds);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import reviewme.keyword.domain.Keyword;
import reviewme.keyword.domain.exception.DuplicateKeywordException;
import reviewme.keyword.domain.exception.KeywordLimitExceedException;
import reviewme.keyword.domain.exception.KeywordsNotFoundException;
import reviewme.keyword.repository.KeywordRepository;
import reviewme.support.ServiceTest;

Expand All @@ -26,23 +25,6 @@ class ReviewCreationKeywordValidatorTest {
@Autowired
KeywordRepository keywordRepository;

@Test
void 존재하는_키워드의_아이디인지_검사한다() {
// given
Keyword keyword1 = keywordRepository.save(회의를_이끌어요.create());
Keyword keyword2 = keywordRepository.save(추진력이_좋아요.create());
long nonExistKeywordId = Long.MAX_VALUE;

// when, then
assertAll(
() -> assertThatCode(
() -> reviewCreationKeywordValidator.validate(List.of(keyword1.getId(), keyword2.getId())))
.doesNotThrowAnyException(),
() -> assertThatCode(() -> reviewCreationKeywordValidator.validate(List.of(nonExistKeywordId)))
.isInstanceOf(KeywordsNotFoundException.class)
);
}

@Test
void 중복되는_아이디의_키워드인지_검사한다() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import reviewme.question.domain.exception.DuplicateQuestionException;
import reviewme.question.domain.exception.QuestionsNotFoundException;
import reviewme.review.repository.QuestionRepository;
import reviewme.support.ServiceTest;

Expand All @@ -22,21 +21,6 @@ class ReviewCreationQuestionValidatorTest {
@Autowired
private QuestionRepository questionRepository;

@Test
void 존재하는_질문의_아이디인지_검사한다() {
// given
long existQuestionId = questionRepository.save(소프트스킬이_어떤가요.create()).getId();
long nonExistQuestionId = Long.MAX_VALUE;

// when, then
assertAll(
() -> assertThatCode(() -> reviewCreationQuestionValidator.validate(List.of(existQuestionId)))
.doesNotThrowAnyException(),
() -> assertThatCode(() -> reviewCreationQuestionValidator.validate(List.of(nonExistQuestionId)))
.isInstanceOf(QuestionsNotFoundException.class)
);
}

@Test
void 중복되는_아이디의_질문인지_검사한다() {
// given
Expand Down

0 comments on commit 71a1950

Please sign in to comment.