Skip to content

Commit

Permalink
refactor: BillAction 검증 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
khabh committed Jul 19, 2024
1 parent ced5660 commit 9c22e47
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
24 changes: 23 additions & 1 deletion server/src/main/java/server/haengdong/domain/BillAction.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,23 +17,44 @@
@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;

@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();
}
Expand Down
56 changes: 56 additions & 0 deletions server/src/test/java/server/haengdong/domain/BillActionTest.java
Original file line number Diff line number Diff line change
@@ -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)
);
}
}

0 comments on commit 9c22e47

Please sign in to comment.