Skip to content

Commit

Permalink
refactor(Content): Feedback의 본문 내용인 Content 를 값객체로 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
zbqmgldjfh committed Jan 9, 2024
1 parent 17f0b25 commit 844fc69
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 12 deletions.
31 changes: 31 additions & 0 deletions src/main/java/com/kustacks/kuring/user/domain/Content.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.kustacks.kuring.user.domain;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Content {

private static final int MAX_LENGTH = 256;

@Column(name = "content", length = 256, nullable = false)
private String value;

public Content(String content) {
validate(content);
this.value = content;
}

public String getValue() {
return value;
}

private void validate(String content) {
if (content.length() > MAX_LENGTH) throw new IllegalArgumentException("본문 내용은 " + MAX_LENGTH + "자 이하여야 합니다");
if (content.isBlank()) throw new IllegalArgumentException("본문은 공백일 수 없습니다");
}
}
17 changes: 5 additions & 12 deletions src/main/java/com/kustacks/kuring/user/domain/Feedback.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.*;
import java.util.Objects;

@Entity
Expand All @@ -25,20 +18,20 @@ public class Feedback extends BaseTimeEntity {
@Column(name = "id", unique = true, nullable = false)
private Long id;

@Column(name = "content", length = 256, nullable = false)
private String content;
@Embedded
private Content content;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

public Feedback(String content, User user) {
this.content = content;
this.content = new Content(content);
this.user = user;
}

public String getContent() {
return content;
return content.getValue();
}

public Long getUserId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.kustacks.kuring.user.domain;

import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

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

public class UserFeedbackTest {

Expand Down Expand Up @@ -35,4 +42,30 @@ public void clear_all_feedback() {
// then
assertThat(user.getAllFeedback().size()).isEqualTo(0);
}

@DisplayName("256자 초과의 피드백은 예외를 발생시킨다")
@ParameterizedTest
@MethodSource("invalidLengthContentsInputProvider")
public void invalid_length_feedback(String content, String errorMessage) {
// given
User user = new User("token");

// when
ThrowingCallable actual = () -> user.addFeedback(content);

// then
assertThatThrownBy(actual)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(errorMessage);
}

private static Stream<Arguments> invalidLengthContentsInputProvider() {
return Stream.of(
Arguments.of("https://www.google.com/search?q=%EC%95%88%EB%8dsfajaslkfjasdkfjkldsafjlsadkjf" +
"lksajfldkadsfajaslkfjasdkfjkldsafjlsadkjflksajfldkajlafkj;lkdjalkfjads;jfalksdfjlasjf;" +
"ljlfsaddsfajaslkfjasdkfjkldsafjlsadkjflksajfldkajlafkj;lkdjalkfjads;jfalksdfjlasjf;ljlf" +
"sadjlafkj;lkdjalkfjads", "본문 내용은 256자 이하여야 합니다"),
Arguments.of("", "본문은 공백일 수 없습니다")
);
}
}

0 comments on commit 844fc69

Please sign in to comment.