Skip to content

Commit

Permalink
fix(article): validate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
gracefulBrown committed May 29, 2024
1 parent d7c10bf commit 6da2b3f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 54 deletions.
45 changes: 10 additions & 35 deletions backend/src/main/java/wooteco/prolog/article/domain/Title.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package wooteco.prolog.article.domain;

import static wooteco.prolog.common.exception.BadRequestCode.ARTICLE_TITLE_NULL_OR_EMPTY_EXCEPTION;

import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand All @@ -13,8 +8,10 @@
import org.apache.commons.text.StringEscapeUtils;
import org.jsoup.Jsoup;
import org.jsoup.safety.Safelist;
import wooteco.prolog.common.exception.BadRequestCode;
import wooteco.prolog.common.exception.BadRequestException;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.util.Objects;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -29,10 +26,12 @@ public class Title {
private String title;

public Title(String title) {
validateNull(title);
validateEmpty(title);
validateOnlyBlank(title);
validateMaxLength(title);
if (Objects.isNull(title) || title.isEmpty() || title.trim().isEmpty()) {
title = "제목없음";
}
if (title.length() > MAX_LENGTH) {
title = title.substring(0, MAX_LENGTH - 1);
}
this.title = trim(title);
}

Expand All @@ -41,28 +40,4 @@ private String trim(String name) {
String result = StringEscapeUtils.unescapeHtml4(Jsoup.clean(name, Safelist.none()));
return result.trim();
}

private void validateNull(String title) {
if (Objects.isNull(title)) {
throw new BadRequestException(ARTICLE_TITLE_NULL_OR_EMPTY_EXCEPTION);
}
}

private void validateEmpty(String title) {
if (title.isEmpty()) {
throw new BadRequestException(ARTICLE_TITLE_NULL_OR_EMPTY_EXCEPTION);
}
}

private void validateOnlyBlank(String title) {
if (title.trim().isEmpty()) {
throw new BadRequestException(ARTICLE_TITLE_NULL_OR_EMPTY_EXCEPTION);
}
}

private void validateMaxLength(String title) {
if (title.length() > MAX_LENGTH) {
throw new BadRequestException(BadRequestCode.ARTICLE_TITLE_OVER_LENGTH_EXCEPTION);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package wooteco.prolog.article.domain;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import joptsimple.internal.Strings;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -13,6 +9,10 @@
import wooteco.prolog.member.domain.Member;
import wooteco.prolog.member.domain.Role;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

class ArticleTest {

private Member member;
Expand Down Expand Up @@ -82,9 +82,10 @@ void update_invalidTitle() {
final Article article = new Article(member, title, url, imageUrl);

//when
article.update(Strings.repeat('a', 51), "newUrl");

//then
assertThatThrownBy(() -> article.update(Strings.repeat('a', 51), "newUrl"))
.isInstanceOf(BadRequestException.class);
assertThat(article.getTitle().getTitle()).isEqualTo(Strings.repeat('a', 49));
}

@DisplayName("유효하지 않은 URL으로 업데이트시 예외를 발생한다.")
Expand Down
19 changes: 6 additions & 13 deletions backend/src/test/java/wooteco/prolog/article/domain/TitleTest.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
package wooteco.prolog.article.domain;

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

import joptsimple.internal.Strings;
import org.junit.jupiter.api.DisplayName;
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 wooteco.prolog.common.exception.BadRequestException;

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

class TitleTest {

@ParameterizedTest(name = "타이틀이 \"{0}\" 일 때 예외가 발생한다")
@NullSource
@ValueSource(strings = {"", " "})
void createTitle_fail(final String title) {
//given
//when
//then
assertThatThrownBy(() -> new Title(title))
.isInstanceOf(BadRequestException.class);
Title result = new Title(title);
assertThat(result.getTitle()).isEqualTo("제목없음");
}

@DisplayName("타이틀의 길이가 최대길이를 초과할 경우 예외가 발생한다")
Expand All @@ -30,10 +25,8 @@ void createTitle_fail_overLength() {
//given
final String title = Strings.repeat('.', 51);

//when
//then
assertThatThrownBy(() -> new Title(title))
.isInstanceOf(BadRequestException.class);
Title result = new Title(title);
assertThat(result.getTitle()).isEqualTo(title.substring(0, 49));
}

@DisplayName("타이틀 앞뒤의 공백은 제거되어 저장된다.")
Expand Down

0 comments on commit 6da2b3f

Please sign in to comment.