Skip to content

Commit 5b1449d

Browse files
committed
대출 객체에서 대출 기간 객체를 분리(Period) 및 단위 테스트 작성
1 parent 79795ea commit 5b1449d

File tree

4 files changed

+92
-24
lines changed

4 files changed

+92
-24
lines changed

build.gradle

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ dependencies {
2323
developmentOnly 'org.springframework.boot:spring-boot-devtools'
2424
testImplementation 'org.springframework.boot:spring-boot-starter-test'
2525
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
26+
implementation 'org.springframework.boot:spring-boot-starter-validation'
2627

2728
// https://mvnrepository.com/artifact/com.google.code.gson/gson
2829
implementation group: 'com.google.code.gson', name: 'gson', version: '2.10.1'
@@ -34,22 +35,13 @@ dependencies {
3435

3536
// mapstruct
3637
// https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor
37-
// annotationProcessor("org.mapstruct:mapstruct-processor:1.5.3.Final")
38-
// // https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor
39-
// testAnnotationProcessor("org.mapstruct:mapstruct-processor:1.5.3.Final")
40-
// // https://mvnrepository.com/artifact/org.mapstruct/mapstruct
41-
// implementation("org.mapstruct:mapstruct:1.5.3.Final")
42-
// // https://mvnrepository.com/artifact/org.projectlombok/lombok-mapstruct-binding
43-
// implementation("org.projectlombok:lombok-mapstruct-binding:0.2.0")
44-
45-
// implementation 'org.mapstruct:mapstruct:1.5.5.Final'
46-
// annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'
47-
// implementation 'org.mapstruct:mapstruct-jdk8:1.5.5.Final'
48-
// implementation 'org.mapstruct:mapstruct-spring-extensions:1.5.5.Final'
49-
50-
51-
implementation 'org.mapstruct:mapstruct:1.5.5.Final'
52-
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final'
38+
annotationProcessor("org.mapstruct:mapstruct-processor:1.5.3.Final")
39+
// https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor
40+
testAnnotationProcessor("org.mapstruct:mapstruct-processor:1.5.3.Final")
41+
// https://mvnrepository.com/artifact/org.mapstruct/mapstruct
42+
implementation("org.mapstruct:mapstruct:1.5.3.Final")
43+
// https://mvnrepository.com/artifact/org.projectlombok/lombok-mapstruct-binding
44+
implementation("org.projectlombok:lombok-mapstruct-binding:0.2.0")
5345

5446

5547

src/main/java/com/study/bookcafe/domain/Borrow.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.study.bookcafe.domain;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.Builder;
5-
import lombok.Getter;
6-
import lombok.NoArgsConstructor;
7-
import java.sql.Timestamp;
3+
import com.study.bookcafe.vo.Period;
4+
import lombok.*;
5+
6+
import java.time.LocalDateTime;
87

98
@Builder
109
@Getter
@@ -14,12 +13,15 @@ public class Borrow {
1413
private long id; // 대출 ID
1514
private Member member; // 회원
1615
private Book book; // 도서
17-
private Timestamp borrowDate; // 대출 날짜
18-
private Timestamp returnDate; // 반납 날짜
16+
// private Timestamp borrowDate; // 대출 날짜
17+
// private Timestamp returnDate; // 반납 날짜
18+
19+
private Period period; // 대출 기간
1920

20-
public Borrow(Member member, Book book) {
21+
public Borrow(Member member, Book book, LocalDateTime from) {
2122
this.member = member;
2223
this.book = book;
24+
this.period = new Period(from);
2325
}
2426

2527
/**
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.study.bookcafe.vo;
2+
3+
import lombok.NonNull;
4+
import java.time.LocalDateTime;
5+
import java.util.Objects;
6+
7+
public class Period {
8+
/*
9+
- 대출 일자가 반납 일자보다 더 이전인 것을 보장할 책임
10+
- 대출 기간(대출 일자 ~ 반납 일자)을 생성할 책임
11+
- 연장 여부에 따라 반납 일자가 변경
12+
- 기본: 1주, 연장 시: 1주 추가, 총 2주
13+
*/
14+
15+
private final LocalDateTime from; // 대출 일자
16+
private final LocalDateTime to; // 반납 일자
17+
18+
public Period(@NonNull LocalDateTime from) {
19+
this.from = from;
20+
this.to = this.from.plusWeeks(1);
21+
}
22+
23+
public Period(@NonNull LocalDateTime from, @NonNull LocalDateTime to) {
24+
if(from.isAfter(to)) throw new IllegalArgumentException("반납 일자는 대출 일자보다 이후여야 합니다.");
25+
26+
this.from = from;
27+
this.to = to;
28+
}
29+
30+
@Override
31+
public boolean equals(Object o) {
32+
if (this == o) return true;
33+
if (o == null || getClass() != o.getClass()) return false;
34+
Period period = (Period) o;
35+
return Objects.equals(from, period.from) && Objects.equals(to, period.to);
36+
}
37+
38+
@Override
39+
public int hashCode() {
40+
return Objects.hash(from, to);
41+
}
42+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.study.bookcafe.borrow;
2+
3+
import com.study.bookcafe.vo.Period;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.DisplayName;
6+
import org.junit.jupiter.api.Test;
7+
import java.time.LocalDateTime;
8+
9+
import static org.assertj.core.api.Assertions.*;
10+
11+
public class PeriodTest {
12+
13+
LocalDateTime date1 = LocalDateTime.of(2024, 12, 7, 12, 0, 0);
14+
LocalDateTime date2 = LocalDateTime.of(2024, 12, 8, 12, 0, 0);
15+
16+
@Test
17+
@DisplayName("반납일자는 대출일자보다 이후여야 한다.")
18+
public void createPeriodTest() {
19+
Period period1 = new Period(date1, date2);
20+
Period period2 = new Period(date2, date2);
21+
22+
assertThat(period1).isNotNull();
23+
assertThat(period2).isNotNull();
24+
}
25+
26+
@Test
27+
@DisplayName("반납일자가 대출일자보다 이전이거나 반납일자 또는 대출일자가 null이면 예외가 발생한다.")
28+
public void createPeriodExceptionTest() {
29+
Assertions.assertThrows(IllegalArgumentException.class, () -> new Period(date2, date1));
30+
Assertions.assertThrows(NullPointerException.class, () -> new Period(null, null));
31+
}
32+
}

0 commit comments

Comments
 (0)