diff --git a/server/src/main/java/server/haengdong/domain/BillAction.java b/server/src/main/java/server/haengdong/domain/BillAction.java index 24ab5a410..3253a3edb 100644 --- a/server/src/main/java/server/haengdong/domain/BillAction.java +++ b/server/src/main/java/server/haengdong/domain/BillAction.java @@ -1,6 +1,7 @@ package server.haengdong.domain; import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; @@ -16,6 +17,11 @@ @Entity public class BillAction { + private static final int MIN_TITLE_LENGTH = 2; + private static final int MAX_TITLE_LENGTH = 30; + private static final long MIN_PRICE = 1L; + private static final long MAX_PRICE = 10_000_000L; + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @@ -23,16 +29,32 @@ public class BillAction { @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) private Action action; + @Column(length = MAX_TITLE_LENGTH) private String title; private Long price; public BillAction(Action action, String title, Long price) { + validateTitle(title); + validatePrice(price); this.action = action; - this.title = title; + this.title = title.trim(); this.price = price; } + private void validateTitle(String title) { + int titleLength = title.trim().length(); + if (titleLength < MIN_TITLE_LENGTH || titleLength > MAX_TITLE_LENGTH) { + throw new IllegalArgumentException("앞뒤 공백을 제거한 지출 내역 제목은 2 ~ 30자여야 합니다."); + } + } + + private void validatePrice(Long price) { + if (price < MIN_PRICE || price > MAX_PRICE) { + throw new IllegalArgumentException("지출 금액은 10,000,000 이하의 자연수여야 합니다."); + } + } + public Long getSequence() { return action.getSequence(); } diff --git a/server/src/test/java/server/haengdong/domain/BillActionTest.java b/server/src/test/java/server/haengdong/domain/BillActionTest.java new file mode 100644 index 000000000..93160a114 --- /dev/null +++ b/server/src/test/java/server/haengdong/domain/BillActionTest.java @@ -0,0 +1,56 @@ +package server.haengdong.domain; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertAll; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +class BillActionTest { + + @DisplayName("앞뒤 공백을 제거한 title이 2 ~ 30자가 아니면 지출을 생성할 수 없다.") + @ParameterizedTest + @ValueSource(strings = {" 감 ", "", " ", "감자감자감자감자감자감자백호백호백호백호백호감자감자감자감자감자감자백호백호백호백호백호"}) + void validateTitle(String title) { + Event event = new Event("name", "token"); + Action action = new Action(event, 1L); + Long price = 100L; + + assertThatThrownBy(() -> new BillAction(action, title, price)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("앞뒤 공백을 제거한 지출 내역 제목은 2 ~ 30자여야 합니다."); + } + + @DisplayName("금액이 10,000,000 이하의 자연수가 아니면 지출을 생성할 수 없다.") + @ParameterizedTest + @ValueSource(longs = {0, 10_000_001, 20_000_000}) + void validatePrice(long price) { + Event event = new Event("name", "token"); + Action action = new Action(event, 1L); + String title = "title"; + + assertThatThrownBy(() -> new BillAction(action, title, price)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("지출 금액은 10,000,000 이하의 자연수여야 합니다."); + } + + @DisplayName("지출 내역을 올바르게 생성한다.") + @Test + void createBillAction() { + Event event = new Event("name", "token"); + Action action = new Action(event, 1L); + String title = "title"; + Long price = 1_000L; + + BillAction billAction = new BillAction(action, title, price); + + assertAll( + () -> assertThat(billAction.getAction()).isEqualTo(action), + () -> assertThat(billAction.getTitle()).isEqualTo(title), + () -> assertThat(billAction.getPrice()).isEqualTo(price) + ); + } +}