Skip to content

Commit

Permalink
fix: RecommendedPost에서 url의 NPE 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
nuyh99 committed Aug 10, 2023
1 parent 10839e5 commit c97e785
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javax.persistence.ManyToOne;
import java.util.Objects;

import static io.micrometer.core.instrument.util.StringUtils.isBlank;
import static java.util.Objects.hash;
import static java.util.Objects.isNull;
import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_KEYWORD_NOT_FOUND_EXCEPTION;
Expand All @@ -38,19 +39,18 @@ public class RecommendedPost {
private Keyword keyword;

public RecommendedPost(final Long id, final String url, final Keyword keyword) {
final String trimmed = url.trim();
validate(trimmed, keyword);
validate(url, keyword);

this.id = id;
this.url = trimmed;
this.url = url.trim();
this.keyword = keyword;
}

private void validate(final String url, final Keyword keyword) {
if (isNull(keyword)) {
throw new BadRequestException(ROADMAP_KEYWORD_NOT_FOUND_EXCEPTION);
}
if (url.isEmpty() || url.length() > URL_LENGTH_UPPER_BOUND) {
if (isBlank(url) || url.trim().length() > URL_LENGTH_UPPER_BOUND) {
throw new BadRequestException(ROADMAP_RECOMMENDED_POST_INVALID_URL_LENGTH);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import wooteco.prolog.common.exception.BadRequestException;

import java.util.stream.Collectors;
Expand All @@ -24,11 +26,20 @@ void construct_fail1() {
.hasMessage(ROADMAP_KEYWORD_NOT_FOUND_EXCEPTION.getMessage());
}

@ParameterizedTest
@NullAndEmptySource
@DisplayName("추천 포스트 생성 시 url이 null이면 예외가 발생한다")
void construct_fail2(final String url) {
assertThatThrownBy(() -> new RecommendedPost(url, null))
.isInstanceOf(BadRequestException.class)
.hasMessage(ROADMAP_KEYWORD_NOT_FOUND_EXCEPTION.getMessage());
}

@Test
@DisplayName("추천 포스트 생성 시 url의 길이가 공백 제외 0이면 예외가 발생한다")
void construct_fail2() {
void construct_fail3() {
//given
final String url = " ";
final String url = " ";

//when, then
assertThatThrownBy(() -> new RecommendedPost(url, null))
Expand All @@ -38,7 +49,7 @@ void construct_fail2() {

@Test
@DisplayName("추천 포스트 생성 시 url의 길이가 공백 제외 512보다 크면 예외가 발생한다")
void construct_fail3() {
void construct_fail4() {
//given
final Keyword keyword = Keyword.createKeyword("name", "description", 1, 1, 1L, null);
final String url = Stream.generate(() -> "a")
Expand Down

0 comments on commit c97e785

Please sign in to comment.