Skip to content

Commit

Permalink
feat: 도메인 검증 기능 추가 및 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
nuyh99 committed Aug 3, 2023
1 parent 2857876 commit 5657fc3
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import wooteco.prolog.roadmap.domain.RecommendedPost;
import wooteco.prolog.session.domain.Mission;
import wooteco.prolog.session.domain.Session;
import wooteco.prolog.studylog.domain.TagName;
Expand Down Expand Up @@ -71,6 +72,8 @@ public enum BadRequestCode {
ESSAY_ANSWER_NOT_VALID_USER(8014, "본인이 작성한 답변만 수정할 수 있습니다."),

ROADMAP_RECOMMENDED_POST_NOT_FOUND(8101, "해당 추천 포스트가 존재하지 않습니다."),
ROADMAP_RECOMMENDED_POST_INVALID_URL_LENGTH(8102, String.format(
"해당 추천 포스트의 URL 길이는 1 ~ %d여야 합니다.", RecommendedPost.URL_LENGTH_UPPER_BOUND)),

FILE_NAME_EMPTY_EXCEPTION(9001, "파일 이름이 존재하지 않습니다."),
UNSUPPORTED_FILE_EXTENSION_EXCEPTION(9002, "지원하지 않는 파일 확장자입니다."),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package wooteco.prolog.roadmap.domain;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import wooteco.prolog.common.exception.BadRequestException;

import javax.persistence.Column;
import javax.persistence.Entity;
Expand All @@ -14,12 +14,18 @@
import javax.persistence.ManyToOne;
import java.util.Objects;

import static java.util.Objects.hash;
import static java.util.Objects.isNull;
import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_KEYWORD_NOT_FOUND_EXCEPTION;
import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_RECOMMENDED_POST_INVALID_URL_LENGTH;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Getter
public class RecommendedPost {

public static final int URL_LENGTH_UPPER_BOUND = 512;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand All @@ -31,6 +37,24 @@ public class RecommendedPost {
@JoinColumn(nullable = false)
private Keyword keyword;

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

this.id = id;
this.url = trimmed;
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) {
throw new BadRequestException(ROADMAP_RECOMMENDED_POST_INVALID_URL_LENGTH);
}
}

public RecommendedPost(final String url, final Keyword keyword) {
this(null, url, keyword);
}
Expand Down Expand Up @@ -58,6 +82,6 @@ public boolean equals(final Object o) {

@Override
public int hashCode() {
return Objects.hash(id);
return hash(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,72 @@

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import wooteco.prolog.common.exception.BadRequestException;

import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_KEYWORD_NOT_FOUND_EXCEPTION;
import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_RECOMMENDED_POST_INVALID_URL_LENGTH;

class RecommendedPostTest {

@Test
@DisplayName("추천 포스트 생성 시 키워드가 null이면 예외가 발생한다")
void construct_fail1() {
assertThatThrownBy(() -> new RecommendedPost("https://example.com", null))
.isInstanceOf(BadRequestException.class)
.hasMessage(ROADMAP_KEYWORD_NOT_FOUND_EXCEPTION.getMessage());
}

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

//when, then
assertThatThrownBy(() -> new RecommendedPost(url, null))
.isInstanceOf(BadRequestException.class)
.hasMessage(ROADMAP_KEYWORD_NOT_FOUND_EXCEPTION.getMessage());
}

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

//when, then
assertThatThrownBy(() -> new RecommendedPost(url, keyword))
.isInstanceOf(BadRequestException.class)
.hasMessage(ROADMAP_RECOMMENDED_POST_INVALID_URL_LENGTH.getMessage());
}

@Test
@DisplayName("추천 포스트 생성 테스트")
void construct() {
//given
final Keyword keyword = Keyword.createKeyword("name", "description", 1, 1, 1L, null);
final String url = "http://www.salmon";

//when, then
assertDoesNotThrow(() -> new RecommendedPost(url, keyword));
}

@Test
@DisplayName("삭제 기능 테스트")
void remove() {
//given
final Keyword keyword = Keyword.createKeyword("이름", "설명", 1, 1, 1L, null);
final RecommendedPost recommendedPost = new RecommendedPost(1L, "https://example.com", null);
recommendedPost.addKeyword(keyword);
final RecommendedPost recommendedPost = new RecommendedPost("https://example.com", keyword);

//when
recommendedPost.remove();
Expand Down

0 comments on commit 5657fc3

Please sign in to comment.