Skip to content

Commit

Permalink
refactor: 다른 유형의 답변이 동시에 입력되는지에 대한 검증 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
nayonsoso committed Nov 14, 2024
1 parent 3947423 commit 4b2bbd1
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ public record ReviewAnswerRequest(
@Nullable
String text
) {

public boolean isTextEmpty() {
return text == null || text.isBlank();
}

public boolean isSelectedOptionIdsEmpty() {
return selectedOptionIds == null || selectedOptionIds.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,7 @@

public abstract class AnswerMapper {

public final Answer mapToAnswer(ReviewAnswerRequest answerRequest) {
if (!isAnswerMatchesQuestionType(answerRequest)) {
throw new QuestionTypeAnswerMismatchException(answerRequest.questionId());
}
if (isAnswerEmpty(answerRequest)) {
return null;
}
return doMap(answerRequest);
}

protected abstract boolean supports(QuestionType questionType);

protected abstract Answer doMap(ReviewAnswerRequest answerRequest);

protected abstract boolean isAnswerMatchesQuestionType(ReviewAnswerRequest request);

protected abstract boolean isAnswerEmpty(ReviewAnswerRequest request);
protected abstract Answer mapToAnswer(ReviewAnswerRequest answerRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,10 @@ protected boolean supports(QuestionType questionType) {
}

@Override
protected CheckboxAnswer doMap(ReviewAnswerRequest answerRequest) {
protected CheckboxAnswer mapToAnswer(ReviewAnswerRequest answerRequest) {
if(answerRequest.isSelectedOptionIdsEmpty()) {
return null;
}
return new CheckboxAnswer(answerRequest.questionId(), answerRequest.selectedOptionIds());
}

@Override
protected boolean isAnswerMatchesQuestionType(ReviewAnswerRequest request) {
return request.selectedOptionIds() != null && request.text() == null;
}

@Override
protected boolean isAnswerEmpty(ReviewAnswerRequest request) {
return request.selectedOptionIds() != null && request.selectedOptionIds().isEmpty();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,10 @@ protected boolean supports(QuestionType questionType) {
}

@Override
protected TextAnswer doMap(ReviewAnswerRequest answerRequest) {
protected TextAnswer mapToAnswer(ReviewAnswerRequest answerRequest) {
if(answerRequest.isTextEmpty()) {
return null;
}
return new TextAnswer(answerRequest.questionId(), answerRequest.text());
}

@Override
protected boolean isAnswerMatchesQuestionType(ReviewAnswerRequest request) {
return request.text() != null && request.selectedOptionIds() == null;
}

@Override
protected boolean isAnswerEmpty(ReviewAnswerRequest request) {
return request.text() != null && request.text().isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,14 @@ class AnswerMapperFactoryTest {
private final AnswerMapper answerMapper = new AnswerMapper() {

@Override
protected Answer doMap(ReviewAnswerRequest answerRequest) {
protected Answer mapToAnswer(ReviewAnswerRequest answerRequest) {
return null;
}

@Override
protected boolean supports(QuestionType questionType) {
return questionType == QuestionType.CHECKBOX;
}

@Override
protected boolean isAnswerMatchesQuestionType(ReviewAnswerRequest request) {
return false;
}

@Override
protected boolean isAnswerEmpty(ReviewAnswerRequest request) {
return false;
}
};

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package reviewme.review.service.mapper;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import reviewme.review.domain.CheckboxAnswer;
import reviewme.review.domain.CheckboxAnswerSelectedOption;
import reviewme.review.service.dto.request.ReviewAnswerRequest;
Expand All @@ -18,7 +19,7 @@ class CheckboxAnswerMapperTest {
CheckboxAnswerMapper mapper = new CheckboxAnswerMapper();

// when
CheckboxAnswer actual = (CheckboxAnswer) mapper.mapToAnswer(request);
CheckboxAnswer actual = mapper.mapToAnswer(request);

// then
assertThat(actual.getQuestionId()).isEqualTo(1L);
Expand All @@ -27,16 +28,17 @@ class CheckboxAnswerMapperTest {
.containsExactly(1L, 2L, 3L);
}

@Test
void 체크박스_답변_요청에_텍스트가_포함되어_있으면_예외를_발생시킨다() {
@ParameterizedTest
@NullAndEmptySource
void 체크박스_답변이_비어있는_경우_null로_매핑한다(List<Long> selectedOptionIds) {
// given
ReviewAnswerRequest request = new ReviewAnswerRequest(1L, List.of(1L, 2L, 3L), "text");
ReviewAnswerRequest request = new ReviewAnswerRequest(1L, selectedOptionIds, null);
CheckboxAnswerMapper mapper = new CheckboxAnswerMapper();

// when
CheckboxAnswerMapper mapper = new CheckboxAnswerMapper();
CheckboxAnswer actual = mapper.mapToAnswer(request);

// then
assertThatThrownBy(() -> mapper.mapToAnswer(request))
.isInstanceOf(QuestionTypeAnswerMismatchException.class);
assertThat(actual).isNull();
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,41 @@
package reviewme.review.service.mapper;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;
import reviewme.review.domain.TextAnswer;
import reviewme.review.service.dto.request.ReviewAnswerRequest;

class TextAnswerMapperTest {

/*
TODO: Request를 추상화해야 할까요?
떠오르는 방법은 아래와 같습니다.
1: static factory method를 사용 -> 걷잡을 수 없어지지 않을까요?
2: 다른 방식으로 추상화 ?
*/

@Test
void 텍스트_답변을_요청으로부터_매핑한다() {
// given
ReviewAnswerRequest request = new ReviewAnswerRequest(1L, null, "text");

// when
TextAnswerMapper mapper = new TextAnswerMapper();
TextAnswer actual = (TextAnswer) mapper.mapToAnswer(request);
TextAnswer actual = mapper.mapToAnswer(request);

// then
assertThat(actual.getContent()).isEqualTo("text");
}

@Test
void 텍스트_답변_요청에_옵션이_포함되어_있으면_예외를_발생시킨다() {
@ParameterizedTest
@NullSource
@ValueSource(strings = {"", " "})
void 텍스트_답변이_비어있는_경우_null로_매핑한다(String text) {
// given
ReviewAnswerRequest request = new ReviewAnswerRequest(1L, List.of(1L), "text");
ReviewAnswerRequest request = new ReviewAnswerRequest(1L, null, text);

// when
TextAnswerMapper mapper = new TextAnswerMapper();
TextAnswer actual = mapper.mapToAnswer(request);

// then
assertThatThrownBy(() -> mapper.mapToAnswer(request))
.isInstanceOf(QuestionTypeAnswerMismatchException.class);
assertThat(actual).isNull();
}
}

0 comments on commit 4b2bbd1

Please sign in to comment.