From 398a724fdedf3fdfca60873ee9114b75b44b90bd Mon Sep 17 00:00:00 2001 From: hseong3243 Date: Fri, 15 Sep 2023 22:17:09 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[NAYB-151]=20fix:=20`Delivery`=20enum=20?= =?UTF-8?q?=ED=95=84=EB=93=9C=EC=97=90=20@Enumerated=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java b/src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java index 365a12c7..f1088664 100644 --- a/src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java +++ b/src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java @@ -7,6 +7,8 @@ import com.prgrms.nabmart.global.BaseTimeEntity; import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; @@ -42,6 +44,7 @@ public class Delivery extends BaseTimeEntity { @JoinColumn(name = "riderId") private Rider rider; + @Enumerated(EnumType.STRING) @Column(nullable = false) private DeliveryStatus deliveryStatus; From e1941d13fb41162507b55e233b500f1b950c098f Mon Sep 17 00:00:00 2001 From: hseong3243 Date: Fri, 15 Sep 2023 22:23:10 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[NAYB-151]=20fix:=20=EB=B0=B0=EC=B0=A8=20?= =?UTF-8?q?=EC=99=84=EB=A3=8C=EB=90=9C=20=EB=B0=B0=EB=8B=AC=EC=97=90=20?= =?UTF-8?q?=EB=8C=80=ED=95=B4=20=EC=A4=91=EB=B3=B5=20=EB=B0=B0=EC=B0=A8=20?= =?UTF-8?q?=EC=9A=94=EC=B2=AD=20=EC=8B=9C=20=EC=98=88=EC=99=B8=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../prgrms/nabmart/domain/delivery/Delivery.java | 9 +++++++++ .../delivery/service/DeliveryServiceTest.java | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java b/src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java index f1088664..5e86c14e 100644 --- a/src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java +++ b/src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java @@ -1,5 +1,6 @@ package com.prgrms.nabmart.domain.delivery; +import com.prgrms.nabmart.domain.delivery.exception.AlreadyAssignedDeliveryException; import com.prgrms.nabmart.domain.delivery.exception.InvalidDeliveryException; import com.prgrms.nabmart.domain.delivery.exception.UnauthorizedDeliveryException; import com.prgrms.nabmart.domain.order.Order; @@ -18,6 +19,7 @@ import jakarta.persistence.OneToOne; import jakarta.persistence.Version; import java.time.LocalDateTime; +import java.util.Objects; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; @@ -99,9 +101,16 @@ public void checkAuthority(final Rider rider) { } public void assignRider(Rider rider) { + checkAlreadyAssignedToRider(); this.rider = rider; } + private void checkAlreadyAssignedToRider() { + if (Objects.nonNull(this.rider)) { + throw new AlreadyAssignedDeliveryException("이미 배차 완료된 배달입니다."); + } + } + public void completeDelivery() { this.arrivedAt = LocalDateTime.now(); this.deliveryStatus = DeliveryStatus.DELIVERED; diff --git a/src/test/java/com/prgrms/nabmart/domain/delivery/service/DeliveryServiceTest.java b/src/test/java/com/prgrms/nabmart/domain/delivery/service/DeliveryServiceTest.java index e46005d9..241c41ff 100644 --- a/src/test/java/com/prgrms/nabmart/domain/delivery/service/DeliveryServiceTest.java +++ b/src/test/java/com/prgrms/nabmart/domain/delivery/service/DeliveryServiceTest.java @@ -10,6 +10,7 @@ import com.prgrms.nabmart.domain.delivery.Delivery; import com.prgrms.nabmart.domain.delivery.DeliveryStatus; import com.prgrms.nabmart.domain.delivery.Rider; +import com.prgrms.nabmart.domain.delivery.exception.AlreadyAssignedDeliveryException; import com.prgrms.nabmart.domain.delivery.exception.InvalidDeliveryException; import com.prgrms.nabmart.domain.delivery.exception.NotFoundDeliveryException; import com.prgrms.nabmart.domain.delivery.exception.NotFoundRiderException; @@ -401,6 +402,21 @@ void throwExceptionWhenNotFoundDelivery() { assertThatThrownBy(() -> deliveryService.acceptDelivery(acceptDeliveryCommand)) .isInstanceOf(NotFoundDeliveryException.class); } + + @Test + @DisplayName("예외: 이미 배차 완료된 배달") + void throwExceptionWhenAlreadyAssignedDelivery() { + //given + given(riderRepository.findById(any())).willReturn(Optional.ofNullable(rider)); + given(deliveryRepository.findByIdOptimistic(any())).willReturn( + Optional.ofNullable(delivery)); + delivery.assignRider(rider); + + //when + //then + assertThatThrownBy(() -> deliveryService.acceptDelivery(acceptDeliveryCommand)) + .isInstanceOf(AlreadyAssignedDeliveryException.class); + } } @Nested From 504b2d776398b024c7b7cd40435dce174466a35b Mon Sep 17 00:00:00 2001 From: hseong3243 Date: Fri, 15 Sep 2023 22:35:43 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[NAYB-151]=20fix:=20=EB=B0=B0=EC=B0=A8=20?= =?UTF-8?q?=EB=8C=80=EA=B8=B0=20=EC=A4=91=EC=9D=B8=20=EB=B0=B0=EB=8B=AC=20?= =?UTF-8?q?=EB=AA=A9=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=EC=97=90=20=EB=B0=B0?= =?UTF-8?q?=EC=B0=A8=20=EC=99=84=EB=A3=8C=EB=90=9C=20=EB=B0=B0=EB=8B=AC?= =?UTF-8?q?=EC=9D=B4=20=ED=8F=AC=ED=95=A8=EB=90=98=EB=8A=94=20=EC=BF=BC?= =?UTF-8?q?=EB=A6=AC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/delivery/repository/DeliveryRepository.java | 3 ++- .../domain/delivery/repository/DeliveryRepositoryTest.java | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/prgrms/nabmart/domain/delivery/repository/DeliveryRepository.java b/src/main/java/com/prgrms/nabmart/domain/delivery/repository/DeliveryRepository.java index 306063b9..3b4cc19a 100644 --- a/src/main/java/com/prgrms/nabmart/domain/delivery/repository/DeliveryRepository.java +++ b/src/main/java/com/prgrms/nabmart/domain/delivery/repository/DeliveryRepository.java @@ -21,7 +21,8 @@ public interface DeliveryRepository extends JpaRepository { @Query(value = "select d from Delivery d" + " where d.deliveryStatus" - + " = com.prgrms.nabmart.domain.delivery.DeliveryStatus.ACCEPTING_ORDER") + + " = com.prgrms.nabmart.domain.delivery.DeliveryStatus.ACCEPTING_ORDER" + + " and d.rider is null") Page findWaitingDeliveries(Pageable pageable); @Query(value = "select d from Delivery d" diff --git a/src/test/java/com/prgrms/nabmart/domain/delivery/repository/DeliveryRepositoryTest.java b/src/test/java/com/prgrms/nabmart/domain/delivery/repository/DeliveryRepositoryTest.java index 7f39f8e9..f7539150 100644 --- a/src/test/java/com/prgrms/nabmart/domain/delivery/repository/DeliveryRepositoryTest.java +++ b/src/test/java/com/prgrms/nabmart/domain/delivery/repository/DeliveryRepositoryTest.java @@ -88,6 +88,10 @@ void success() { int totalElements = 3; List orders = createAndSaveOrders(totalElements); List deliveries = createAndSaveDeliveries(orders); + List alreadyAssignedOrders = createAndSaveOrders(3); + List alreadyAssignedDeliveries + = createAndSaveDeliveries(alreadyAssignedOrders); + alreadyAssignedDeliveries.forEach(delivery -> delivery.assignRider(rider)); PageRequest pageRequest = PageRequest.of(0, 10); //when