generated from pricelees/issue-pr-template
refactor: PaymentEntity 코틀린 전환 및 불필요한 null-check validation 제거
This commit is contained in:
parent
0791e953c7
commit
2ee1920099
@ -32,7 +32,7 @@ public class PaymentService {
|
|||||||
|
|
||||||
public ReservationPaymentResponse savePayment(PaymentApprove.Response paymentResponse,
|
public ReservationPaymentResponse savePayment(PaymentApprove.Response paymentResponse,
|
||||||
Reservation reservation) {
|
Reservation reservation) {
|
||||||
PaymentEntity paymentEntity = new PaymentEntity(paymentResponse.orderId, paymentResponse.paymentKey,
|
PaymentEntity paymentEntity = new PaymentEntity(null, paymentResponse.orderId, paymentResponse.paymentKey,
|
||||||
paymentResponse.totalAmount, reservation, paymentResponse.approvedAt);
|
paymentResponse.totalAmount, reservation, paymentResponse.approvedAt);
|
||||||
PaymentEntity saved = paymentRepository.save(paymentEntity);
|
PaymentEntity saved = paymentRepository.save(paymentEntity);
|
||||||
return ReservationPaymentResponse.from(saved);
|
return ReservationPaymentResponse.from(saved);
|
||||||
|
|||||||
@ -1,108 +1,29 @@
|
|||||||
package roomescape.payment.infrastructure.persistence;
|
package roomescape.payment.infrastructure.persistence
|
||||||
|
|
||||||
import java.time.OffsetDateTime;
|
import jakarta.persistence.*
|
||||||
|
import roomescape.reservation.domain.Reservation
|
||||||
import org.springframework.http.HttpStatus;
|
import java.time.OffsetDateTime
|
||||||
|
|
||||||
import jakarta.persistence.Column;
|
|
||||||
import jakarta.persistence.Entity;
|
|
||||||
import jakarta.persistence.FetchType;
|
|
||||||
import jakarta.persistence.GeneratedValue;
|
|
||||||
import jakarta.persistence.GenerationType;
|
|
||||||
import jakarta.persistence.Id;
|
|
||||||
import jakarta.persistence.JoinColumn;
|
|
||||||
import jakarta.persistence.OneToOne;
|
|
||||||
import roomescape.common.exception.ErrorType;
|
|
||||||
import roomescape.common.exception.RoomescapeException;
|
|
||||||
import roomescape.reservation.domain.Reservation;
|
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class PaymentEntity {
|
@Table(name = "payment")
|
||||||
|
class PaymentEntity(
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
var id: Long? = null,
|
||||||
|
|
||||||
@Id
|
@Column(nullable = false)
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
var orderId: String,
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private String orderId;
|
var paymentKey: String,
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private String paymentKey;
|
var totalAmount: Long,
|
||||||
|
|
||||||
@Column(nullable = false)
|
@OneToOne(fetch = FetchType.LAZY)
|
||||||
private Long totalAmount;
|
@JoinColumn(name = "reservation_id", nullable = false)
|
||||||
|
var reservation: Reservation,
|
||||||
|
|
||||||
@OneToOne(fetch = FetchType.LAZY)
|
@Column(nullable = false)
|
||||||
@JoinColumn(name = "reservation_id", nullable = false)
|
var approvedAt: OffsetDateTime
|
||||||
private Reservation reservation;
|
)
|
||||||
|
|
||||||
@Column(nullable = false)
|
|
||||||
private OffsetDateTime approvedAt;
|
|
||||||
|
|
||||||
protected PaymentEntity() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public PaymentEntity(String orderId, String paymentKey, Long totalAmount, Reservation reservation,
|
|
||||||
OffsetDateTime approvedAt) {
|
|
||||||
validate(orderId, paymentKey, totalAmount, reservation, approvedAt);
|
|
||||||
this.orderId = orderId;
|
|
||||||
this.paymentKey = paymentKey;
|
|
||||||
this.totalAmount = totalAmount;
|
|
||||||
this.reservation = reservation;
|
|
||||||
this.approvedAt = approvedAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void validate(String orderId, String paymentKey, Long totalAmount, Reservation reservation,
|
|
||||||
OffsetDateTime approvedAt) {
|
|
||||||
validateIsNullOrBlank(orderId, "orderId");
|
|
||||||
validateIsNullOrBlank(paymentKey, "paymentKey");
|
|
||||||
validateIsInvalidAmount(totalAmount);
|
|
||||||
validateIsNull(reservation, "reservation");
|
|
||||||
validateIsNull(approvedAt, "approvedAt");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void validateIsNullOrBlank(String input, String fieldName) {
|
|
||||||
if (input == null || input.isBlank()) {
|
|
||||||
throw new RoomescapeException(ErrorType.REQUEST_DATA_BLANK, String.format("[value : %s]", fieldName),
|
|
||||||
HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void validateIsInvalidAmount(Long totalAmount) {
|
|
||||||
if (totalAmount == null || totalAmount < 0) {
|
|
||||||
throw new RoomescapeException(ErrorType.INVALID_REQUEST_DATA,
|
|
||||||
String.format("[totalAmount : %d]", totalAmount), HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private <T> void validateIsNull(T value, String fieldName) {
|
|
||||||
if (value == null) {
|
|
||||||
throw new RoomescapeException(ErrorType.REQUEST_DATA_BLANK, String.format("[value : %s]", fieldName),
|
|
||||||
HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getOrderId() {
|
|
||||||
return orderId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPaymentKey() {
|
|
||||||
return paymentKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getTotalAmount() {
|
|
||||||
return totalAmount;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Reservation getReservation() {
|
|
||||||
return reservation;
|
|
||||||
}
|
|
||||||
|
|
||||||
public OffsetDateTime getApprovedAt() {
|
|
||||||
return approvedAt;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -58,7 +58,7 @@ data class ReservationPaymentResponse(
|
|||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun from(saved: PaymentEntity): ReservationPaymentResponse {
|
fun from(saved: PaymentEntity): ReservationPaymentResponse {
|
||||||
return ReservationPaymentResponse(
|
return ReservationPaymentResponse(
|
||||||
saved.id,
|
saved.id!!,
|
||||||
saved.orderId,
|
saved.orderId,
|
||||||
saved.paymentKey,
|
saved.paymentKey,
|
||||||
saved.totalAmount,
|
saved.totalAmount,
|
||||||
|
|||||||
@ -54,7 +54,7 @@ public interface ReservationRepository extends JpaRepository<Reservation, Long>,
|
|||||||
)
|
)
|
||||||
FROM Reservation r
|
FROM Reservation r
|
||||||
JOIN r.theme t
|
JOIN r.theme t
|
||||||
LEFT JOIN Payment p
|
LEFT JOIN PaymentEntity p
|
||||||
ON p.reservation = r
|
ON p.reservation = r
|
||||||
WHERE r.member.id = :memberId
|
WHERE r.member.id = :memberId
|
||||||
""")
|
""")
|
||||||
|
|||||||
@ -1,78 +0,0 @@
|
|||||||
package roomescape.payment.infrastructure.persistence;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.*;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.time.LocalTime;
|
|
||||||
import java.time.OffsetDateTime;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.DisplayName;
|
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
|
||||||
import org.junit.jupiter.params.provider.CsvSource;
|
|
||||||
import org.junit.jupiter.params.provider.NullAndEmptySource;
|
|
||||||
import org.junit.jupiter.params.provider.NullSource;
|
|
||||||
|
|
||||||
import roomescape.common.exception.RoomescapeException;
|
|
||||||
import roomescape.member.infrastructure.persistence.Member;
|
|
||||||
import roomescape.member.infrastructure.persistence.Role;
|
|
||||||
import roomescape.reservation.domain.Reservation;
|
|
||||||
import roomescape.reservation.domain.ReservationStatus;
|
|
||||||
import roomescape.reservation.domain.ReservationTime;
|
|
||||||
import roomescape.theme.domain.Theme;
|
|
||||||
|
|
||||||
class PaymentEntityTest {
|
|
||||||
|
|
||||||
private Reservation reservation;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void setUp() {
|
|
||||||
LocalDate now = LocalDate.now();
|
|
||||||
ReservationTime reservationTime = new ReservationTime(LocalTime.now());
|
|
||||||
Theme theme = new Theme("name", "desc", "thumb");
|
|
||||||
Member member = new Member(null, "name", "email", "password", Role.MEMBER);
|
|
||||||
|
|
||||||
reservation = new Reservation(now, reservationTime, theme, member, ReservationStatus.CONFIRMED);
|
|
||||||
}
|
|
||||||
|
|
||||||
@ParameterizedTest
|
|
||||||
@DisplayName("paymentKey가 null 또는 빈값이면 예외가 발생한다.")
|
|
||||||
@NullAndEmptySource
|
|
||||||
void invalidPaymentKey(String paymentKey) {
|
|
||||||
assertThatThrownBy(() -> new PaymentEntity("order-id", paymentKey, 10000L, reservation, OffsetDateTime.now()))
|
|
||||||
.isInstanceOf(RoomescapeException.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@ParameterizedTest
|
|
||||||
@DisplayName("orderId가 null 또는 빈값이면 예외가 발생한다.")
|
|
||||||
@NullAndEmptySource
|
|
||||||
void invalidOrderId(String orderId) {
|
|
||||||
assertThatThrownBy(() -> new PaymentEntity(orderId, "payment-key", 10000L, reservation, OffsetDateTime.now()))
|
|
||||||
.isInstanceOf(RoomescapeException.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@ParameterizedTest
|
|
||||||
@DisplayName("amount가 null 또는 0 이하면 예외가 발생한다.")
|
|
||||||
@CsvSource(value = {"null", "-1"}, nullValues = {"null"})
|
|
||||||
void invalidOrderId(Long totalAmount) {
|
|
||||||
assertThatThrownBy(
|
|
||||||
() -> new PaymentEntity("orderId", "payment-key", totalAmount, reservation, OffsetDateTime.now()))
|
|
||||||
.isInstanceOf(RoomescapeException.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@ParameterizedTest
|
|
||||||
@DisplayName("Reservation이 null이면 예외가 발생한다.")
|
|
||||||
@NullSource
|
|
||||||
void invalidReservation(Reservation reservation) {
|
|
||||||
assertThatThrownBy(() -> new PaymentEntity("orderId", "payment-key", 10000L, reservation, OffsetDateTime.now()))
|
|
||||||
.isInstanceOf(RoomescapeException.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@ParameterizedTest
|
|
||||||
@DisplayName("승인 날짜가 null이면 예외가 발생한다.")
|
|
||||||
@NullSource
|
|
||||||
void invalidApprovedAt(OffsetDateTime approvedAt) {
|
|
||||||
assertThatThrownBy(() -> new PaymentEntity("orderId", "payment-key", 10000L, reservation, approvedAt))
|
|
||||||
.isInstanceOf(RoomescapeException.class);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -399,7 +399,7 @@ public class ReservationControllerTest {
|
|||||||
Reservation saved = reservationRepository.save(
|
Reservation saved = reservationRepository.save(
|
||||||
new Reservation(date, time, theme, member, ReservationStatus.CONFIRMED));
|
new Reservation(date, time, theme, member, ReservationStatus.CONFIRMED));
|
||||||
PaymentEntity savedPaymentEntity = paymentRepository.save(
|
PaymentEntity savedPaymentEntity = paymentRepository.save(
|
||||||
new PaymentEntity("pk", "oi", 1000L, saved, OffsetDateTime.now().minusHours(1L)));
|
new PaymentEntity(null, "pk", "oi", 1000L, saved, OffsetDateTime.now().minusHours(1L)));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
when(paymentClient.cancelPayment(any(PaymentCancel.Request.class)))
|
when(paymentClient.cancelPayment(any(PaymentCancel.Request.class)))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user