diff --git a/src/main/java/com/prgrms/nabmart/domain/cart/repository/CartItemRepository.java b/src/main/java/com/prgrms/nabmart/domain/cart/repository/CartItemRepository.java index 2600efaa9..517c99bf4 100644 --- a/src/main/java/com/prgrms/nabmart/domain/cart/repository/CartItemRepository.java +++ b/src/main/java/com/prgrms/nabmart/domain/cart/repository/CartItemRepository.java @@ -2,12 +2,22 @@ import com.prgrms.nabmart.domain.cart.Cart; import com.prgrms.nabmart.domain.cart.CartItem; +import com.prgrms.nabmart.domain.user.User; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface CartItemRepository extends JpaRepository { List findAllByCartItemIdOrderByCreatedAt(Long cartItemId); List findAllByCartOrderByCreatedAt(Cart cart); + + @Modifying + @Query("delete from CartItem ci" + + " where ci.cart =" + + " (select c from Cart c where c.user = :user)") + void deleteByUser(@Param("user") User user); } diff --git a/src/main/java/com/prgrms/nabmart/domain/cart/repository/CartRepository.java b/src/main/java/com/prgrms/nabmart/domain/cart/repository/CartRepository.java index 7e30e3544..251ce41aa 100644 --- a/src/main/java/com/prgrms/nabmart/domain/cart/repository/CartRepository.java +++ b/src/main/java/com/prgrms/nabmart/domain/cart/repository/CartRepository.java @@ -8,4 +8,6 @@ public interface CartRepository extends JpaRepository { Optional findByUser(final User user); + + void deleteByUser(User user); } diff --git a/src/main/java/com/prgrms/nabmart/domain/coupon/repository/UserCouponRepository.java b/src/main/java/com/prgrms/nabmart/domain/coupon/repository/UserCouponRepository.java index 2e41494e5..4e661a90b 100644 --- a/src/main/java/com/prgrms/nabmart/domain/coupon/repository/UserCouponRepository.java +++ b/src/main/java/com/prgrms/nabmart/domain/coupon/repository/UserCouponRepository.java @@ -23,5 +23,7 @@ List findByUserAndIsUsedAndCouponEndAtAfter( @Param("user") User user, @Param("isUsed") boolean isUsed, @Param("currentDate") LocalDate currentDate); + + void deleteByUser(User findUser); } 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 f5af09fb2..365a12c73 100644 --- a/src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java +++ b/src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java @@ -28,13 +28,14 @@ public class Delivery extends BaseTimeEntity { private static final int ADDRESS_LENGTH = 500; private static final int ZERO = 0; + public static final String DELETED = "삭제됨"; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long deliveryId; @OneToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "orderId", nullable = false) + @JoinColumn(name = "orderId") private Order order; @ManyToOne(fetch = FetchType.LAZY) @@ -50,10 +51,26 @@ public class Delivery extends BaseTimeEntity { @Version private Long version; + @Column + private String address; + + @Column + private Integer orderPrice; + + @Column + private String riderRequest; + + @Column + private Integer deliveryFee; + @Builder public Delivery(final Order order) { this.order = order; this.deliveryStatus = DeliveryStatus.ACCEPTING_ORDER; + this.address = order.getAddress(); + this.orderPrice = order.getPrice(); + this.riderRequest = order.getRiderRequest(); + this.deliveryFee = order.getDeliveryFee(); } public boolean isOwnByUser(final User user) { @@ -86,4 +103,9 @@ public void completeDelivery() { this.arrivedAt = LocalDateTime.now(); this.deliveryStatus = DeliveryStatus.DELIVERED; } + + public void deleteAboutUser() { + this.order = null; + this.address = DELETED; + } } 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 b35b5323d..306063b9f 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 @@ -3,6 +3,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.user.User; import jakarta.persistence.LockModeType; import java.util.List; import java.util.Optional; @@ -19,18 +20,11 @@ public interface DeliveryRepository extends JpaRepository { Optional findByOrderIdWithOrder(@Param("orderId") Long orderId); @Query(value = "select d from Delivery d" - + " join fetch d.order" - + " where d.deliveryStatus" - + " = com.prgrms.nabmart.domain.delivery.DeliveryStatus.ACCEPTING_ORDER", - countQuery = "select d.deliveryId from Delivery d" + " where d.deliveryStatus" + " = com.prgrms.nabmart.domain.delivery.DeliveryStatus.ACCEPTING_ORDER") Page findWaitingDeliveries(Pageable pageable); @Query(value = "select d from Delivery d" - + " join fetch d.order" - + " where d.rider = :rider and d.deliveryStatus in :statuses", - countQuery = "select d.deliveryId from Delivery d" + " where d.rider = :rider and d.deliveryStatus in :statuses") Page findRiderDeliveries( @Param("rider") Rider rider, @@ -40,4 +34,7 @@ Page findRiderDeliveries( @Lock(LockModeType.OPTIMISTIC) @Query("select d from Delivery d where d.deliveryId = :deliveryId") Optional findByIdOptimistic(@Param("deliveryId") Long deliveryId); + + @Query("select d from Delivery d join d.order o where o.user = :user") + List findAllByUser(@Param("user") User user); } diff --git a/src/main/java/com/prgrms/nabmart/domain/delivery/service/response/FindDeliveryDetailResponse.java b/src/main/java/com/prgrms/nabmart/domain/delivery/service/response/FindDeliveryDetailResponse.java index 9aa8b214e..cd678602d 100644 --- a/src/main/java/com/prgrms/nabmart/domain/delivery/service/response/FindDeliveryDetailResponse.java +++ b/src/main/java/com/prgrms/nabmart/domain/delivery/service/response/FindDeliveryDetailResponse.java @@ -22,7 +22,7 @@ public static FindDeliveryDetailResponse from(final Delivery delivery) { delivery.getArrivedAt(), delivery.getOrder().getOrderId(), delivery.getOrder().getName(), - delivery.getOrder().getPrice(), - delivery.getOrder().getRiderRequest()); + delivery.getOrderPrice(), + delivery.getRiderRequest()); } } diff --git a/src/main/java/com/prgrms/nabmart/domain/delivery/service/response/FindRiderDeliveriesResponse.java b/src/main/java/com/prgrms/nabmart/domain/delivery/service/response/FindRiderDeliveriesResponse.java index 6004b434e..a05c6894e 100644 --- a/src/main/java/com/prgrms/nabmart/domain/delivery/service/response/FindRiderDeliveriesResponse.java +++ b/src/main/java/com/prgrms/nabmart/domain/delivery/service/response/FindRiderDeliveriesResponse.java @@ -39,10 +39,10 @@ public static FindRiderDeliveryResponse from(final Delivery delivery) { delivery.getDeliveryStatus(), delivery.getArrivedAt(), delivery.getCreatedAt(), - delivery.getOrder().getAddress(), - delivery.getOrder().getPrice(), - delivery.getOrder().getRiderRequest(), - delivery.getOrder().getDeliveryFee()); + delivery.getAddress(), + delivery.getOrderPrice(), + delivery.getRiderRequest(), + delivery.getDeliveryFee()); } } } diff --git a/src/main/java/com/prgrms/nabmart/domain/delivery/service/response/FindWaitingDeliveriesResponse.java b/src/main/java/com/prgrms/nabmart/domain/delivery/service/response/FindWaitingDeliveriesResponse.java index 002dad310..acda541c2 100644 --- a/src/main/java/com/prgrms/nabmart/domain/delivery/service/response/FindWaitingDeliveriesResponse.java +++ b/src/main/java/com/prgrms/nabmart/domain/delivery/service/response/FindWaitingDeliveriesResponse.java @@ -32,8 +32,8 @@ public static FindWaitingDeliveryResponse from(final Delivery delivery) { delivery.getDeliveryId(), delivery.getArrivedAt(), delivery.getCreatedAt(), - delivery.getOrder().getAddress(), - delivery.getOrder().getDeliveryFee()); + delivery.getAddress(), + delivery.getDeliveryFee()); } } } diff --git a/src/main/java/com/prgrms/nabmart/domain/item/repository/LikeItemRepository.java b/src/main/java/com/prgrms/nabmart/domain/item/repository/LikeItemRepository.java index be928d5ca..54384af51 100644 --- a/src/main/java/com/prgrms/nabmart/domain/item/repository/LikeItemRepository.java +++ b/src/main/java/com/prgrms/nabmart/domain/item/repository/LikeItemRepository.java @@ -15,4 +15,6 @@ public interface LikeItemRepository extends JpaRepository { @Query("select li from LikeItem li join fetch li.item where li.user = :user") Page findByUserWithItem(@Param("user") User user, Pageable pageable); + + void deleteByUser(User user); } diff --git a/src/main/java/com/prgrms/nabmart/domain/order/repository/OrderRepository.java b/src/main/java/com/prgrms/nabmart/domain/order/repository/OrderRepository.java index fb1b7db4e..cbc49bcf0 100644 --- a/src/main/java/com/prgrms/nabmart/domain/order/repository/OrderRepository.java +++ b/src/main/java/com/prgrms/nabmart/domain/order/repository/OrderRepository.java @@ -2,6 +2,7 @@ import com.prgrms.nabmart.domain.order.Order; import com.prgrms.nabmart.domain.order.OrderStatus; +import com.prgrms.nabmart.domain.user.User; import java.time.LocalDateTime; import java.util.List; import java.util.Optional; @@ -21,4 +22,6 @@ public interface OrderRepository extends JpaRepository { @Query("SELECT o FROM Order o WHERE o.createdAt <= :expiredTime AND o.status IN :statusList") List findByStatusInBeforeExpiredTime(LocalDateTime expiredTime, List statusList); + + void deleteByUser(User findUser); } diff --git a/src/main/java/com/prgrms/nabmart/domain/payment/repository/PaymentRepository.java b/src/main/java/com/prgrms/nabmart/domain/payment/repository/PaymentRepository.java index fd68441f6..4cef22141 100644 --- a/src/main/java/com/prgrms/nabmart/domain/payment/repository/PaymentRepository.java +++ b/src/main/java/com/prgrms/nabmart/domain/payment/repository/PaymentRepository.java @@ -1,10 +1,13 @@ package com.prgrms.nabmart.domain.payment.repository; import com.prgrms.nabmart.domain.payment.Payment; +import com.prgrms.nabmart.domain.user.User; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; public interface PaymentRepository extends JpaRepository { Optional findByOrder_UuidAndUser_UserId(String uuid, Long userId); + + void deleteByUser(User user); } diff --git a/src/main/java/com/prgrms/nabmart/domain/review/repository/ReviewRepository.java b/src/main/java/com/prgrms/nabmart/domain/review/repository/ReviewRepository.java index 7448a1c95..9eaac1aa5 100644 --- a/src/main/java/com/prgrms/nabmart/domain/review/repository/ReviewRepository.java +++ b/src/main/java/com/prgrms/nabmart/domain/review/repository/ReviewRepository.java @@ -16,6 +16,9 @@ public interface ReviewRepository extends JpaRepository { Long countByItem_ItemId(Long itemId); + void deleteByUser(User findUser); + @Query("select avg(r.rate) from Review r where r.item.itemId = :itemId") Double findAverageRatingByItemId(@Param("itemId") Long itemId); + } diff --git a/src/main/java/com/prgrms/nabmart/domain/user/service/UserService.java b/src/main/java/com/prgrms/nabmart/domain/user/service/UserService.java index 89751ed65..f87700e98 100644 --- a/src/main/java/com/prgrms/nabmart/domain/user/service/UserService.java +++ b/src/main/java/com/prgrms/nabmart/domain/user/service/UserService.java @@ -1,5 +1,14 @@ package com.prgrms.nabmart.domain.user.service; +import com.prgrms.nabmart.domain.cart.repository.CartItemRepository; +import com.prgrms.nabmart.domain.cart.repository.CartRepository; +import com.prgrms.nabmart.domain.coupon.repository.UserCouponRepository; +import com.prgrms.nabmart.domain.delivery.Delivery; +import com.prgrms.nabmart.domain.delivery.repository.DeliveryRepository; +import com.prgrms.nabmart.domain.item.repository.LikeItemRepository; +import com.prgrms.nabmart.domain.order.repository.OrderRepository; +import com.prgrms.nabmart.domain.payment.repository.PaymentRepository; +import com.prgrms.nabmart.domain.review.repository.ReviewRepository; import com.prgrms.nabmart.domain.user.User; import com.prgrms.nabmart.domain.user.exception.NotFoundUserException; import com.prgrms.nabmart.domain.user.repository.UserRepository; @@ -7,6 +16,7 @@ import com.prgrms.nabmart.domain.user.service.request.RegisterUserCommand; import com.prgrms.nabmart.domain.user.service.response.FindUserDetailResponse; import com.prgrms.nabmart.domain.user.service.response.RegisterUserResponse; +import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -16,6 +26,14 @@ public class UserService { private final UserRepository userRepository; + private final ReviewRepository reviewRepository; + private final LikeItemRepository likeItemRepository; + private final CartRepository cartRepository; + private final CartItemRepository cartItemRepository; + private final UserCouponRepository userCouponRepository; + private final OrderRepository orderRepository; + private final DeliveryRepository deliveryRepository; + private final PaymentRepository paymentRepository; @Transactional public RegisterUserResponse getOrRegisterUser(final RegisterUserCommand registerUserCommand) { @@ -47,7 +65,21 @@ public FindUserDetailResponse findUser(FindUserCommand findUserCommand) { @Transactional public void deleteUser(Long userId) { - userRepository.deleteById(userId); + User findUser = findUserByUserId(userId); + releaseDeliveryAboutUserInfo(findUser); + reviewRepository.deleteByUser(findUser); + likeItemRepository.deleteByUser(findUser); + cartItemRepository.deleteByUser(findUser); + cartRepository.deleteByUser(findUser); + paymentRepository.deleteByUser(findUser); + orderRepository.deleteByUser(findUser); + userCouponRepository.deleteByUser(findUser); + userRepository.delete(findUser); + } + + private void releaseDeliveryAboutUserInfo(User findUser) { + List userDeliveries = deliveryRepository.findAllByUser(findUser); + userDeliveries.forEach(Delivery::deleteAboutUser); } private User findUserByUserId(Long userId) { diff --git a/src/test/java/com/prgrms/nabmart/base/IntegrationTest.java b/src/test/java/com/prgrms/nabmart/base/IntegrationTest.java index 2d2c13cca..3c0e52c40 100644 --- a/src/test/java/com/prgrms/nabmart/base/IntegrationTest.java +++ b/src/test/java/com/prgrms/nabmart/base/IntegrationTest.java @@ -93,5 +93,7 @@ static void beforeAll() { properties.setProperty("TOSS_SECRET_KEY", "tossSecretKey"); properties.setProperty("REDIS_HOST", "localhost"); properties.setProperty("REDIS_PORT", "6379"); + properties.setProperty("spring.data.redis.host", "localhost"); + properties.setProperty("spring.data.redis.port", "6379"); } } diff --git a/src/test/java/com/prgrms/nabmart/domain/delivery/DeliveryIntegrationTest.java b/src/test/java/com/prgrms/nabmart/domain/delivery/DeliveryIntegrationTest.java index 742ae4a63..f90fd5e5e 100644 --- a/src/test/java/com/prgrms/nabmart/domain/delivery/DeliveryIntegrationTest.java +++ b/src/test/java/com/prgrms/nabmart/domain/delivery/DeliveryIntegrationTest.java @@ -2,34 +2,92 @@ import static org.assertj.core.api.Assertions.assertThat; -import com.prgrms.nabmart.base.IntegrationTest; import com.prgrms.nabmart.domain.category.MainCategory; import com.prgrms.nabmart.domain.category.SubCategory; import com.prgrms.nabmart.domain.category.fixture.CategoryFixture; +import com.prgrms.nabmart.domain.category.repository.MainCategoryRepository; +import com.prgrms.nabmart.domain.category.repository.SubCategoryRepository; +import com.prgrms.nabmart.domain.delivery.repository.DeliveryRepository; +import com.prgrms.nabmart.domain.delivery.repository.RiderRepository; import com.prgrms.nabmart.domain.delivery.service.DeliveryService; import com.prgrms.nabmart.domain.delivery.service.request.AcceptDeliveryCommand; import com.prgrms.nabmart.domain.item.Item; +import com.prgrms.nabmart.domain.item.repository.ItemRepository; import com.prgrms.nabmart.domain.item.support.ItemFixture; import com.prgrms.nabmart.domain.order.Order; import com.prgrms.nabmart.domain.order.OrderItem; +import com.prgrms.nabmart.domain.order.repository.OrderItemRepository; +import com.prgrms.nabmart.domain.order.repository.OrderRepository; import com.prgrms.nabmart.domain.user.User; +import com.prgrms.nabmart.domain.user.repository.UserRepository; import com.prgrms.nabmart.domain.user.support.UserFixture; import java.util.List; +import java.util.Properties; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.IntStream; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; -public class DeliveryIntegrationTest extends IntegrationTest { +@SpringBootTest +public class DeliveryIntegrationTest { + + @Autowired + UserRepository userRepository; + + @Autowired + MainCategoryRepository mainCategoryRepository; + + @Autowired + SubCategoryRepository subCategoryRepository; + + @Autowired + ItemRepository itemRepository; + + @Autowired + OrderItemRepository orderItemRepository; + + @Autowired + OrderRepository orderRepository; + + @Autowired + DeliveryRepository deliveryRepository; @Autowired DeliveryService deliveryService; + @Autowired + RiderRepository riderRepository; + + @BeforeAll + static void beforeAll() { + Properties properties = System.getProperties(); + properties.setProperty("ISSUER", "issuer"); + properties.setProperty("CLIENT_SECRET", "clientSecret"); + properties.setProperty("NAVER_CLIENT_ID", "naverClientId"); + properties.setProperty("NAVER_CLIENT_SECRET", "naverClientSecret"); + properties.setProperty("KAKAO_CLIENT_ID", "kakaoClientId"); + properties.setProperty("KAKAO_CLIENT_SECRET", "kakaoClientSecret"); + properties.setProperty("REDIRECT_URI", + "http://localhost:8080/login/oauth2/code/{registrationId}"); + properties.setProperty("EXPIRY_SECONDS", "60"); + properties.setProperty("TOSS_SUCCESS_URL", "tossSuccessUrl"); + properties.setProperty("TOSS_FAIL_URL", "tossFailUrl"); + properties.setProperty("TOSS_SECRET_KEY", "tossSecretKey"); + properties.setProperty("REDIS_HOST", "localhost"); + properties.setProperty("REDIS_PORT", "6379"); + properties.setProperty("spring.data.redis.host", "localhost"); + properties.setProperty("spring.data.redis.port", "6379"); + } + + @Nested @DisplayName("acceptDelivery 메서드 실행 시") class AcceptDeliveryTest { @@ -59,7 +117,6 @@ void setUpData() { orderRepository.save(order); } - List createAndSaveRiders(int end) { List riders = IntStream.range(0, end) .mapToObj(i -> Rider.builder() @@ -78,6 +135,16 @@ private Delivery createAndSaveDelivery() { return delivery; } + @AfterEach + void tearDown() { + deliveryRepository.deleteAll(); + riderRepository.deleteAll(); + orderRepository.deleteAll(); + itemRepository.deleteAll(); + subCategoryRepository.deleteAll(); + mainCategoryRepository.deleteAll(); + } + @Test @DisplayName("성공: 여러 명의 라이더 중 한 명만 성공") void success() throws InterruptedException { diff --git a/src/test/java/com/prgrms/nabmart/domain/delivery/support/DeliveryFixture.java b/src/test/java/com/prgrms/nabmart/domain/delivery/support/DeliveryFixture.java index d125afa51..1d072a0b7 100644 --- a/src/test/java/com/prgrms/nabmart/domain/delivery/support/DeliveryFixture.java +++ b/src/test/java/com/prgrms/nabmart/domain/delivery/support/DeliveryFixture.java @@ -44,6 +44,12 @@ public static Delivery acceptedDelivery(Order order, Rider rider) { return delivery; } + public static Delivery completedDelivery(Order order, Rider rider) { + Delivery delivery = acceptedDelivery(order, rider); + delivery.completeDelivery(); + return delivery; + } + public static FindDeliveryCommand findDeliveryCommand() { return new FindDeliveryCommand(USER_ID, ORDER_ID); } diff --git a/src/test/java/com/prgrms/nabmart/domain/order/OrderServiceIntegrationTest.java b/src/test/java/com/prgrms/nabmart/domain/order/OrderServiceIntegrationTest.java index 25c52eb52..b533e65c4 100644 --- a/src/test/java/com/prgrms/nabmart/domain/order/OrderServiceIntegrationTest.java +++ b/src/test/java/com/prgrms/nabmart/domain/order/OrderServiceIntegrationTest.java @@ -42,6 +42,8 @@ static void beforeAll() { properties.setProperty("TOSS_SECRET_KEY", "tossSecretKey"); properties.setProperty("REDIS_HOST", "redisHost"); properties.setProperty("REDIS_PORT", "6379"); + properties.setProperty("spring.data.redis.host", "localhost"); + properties.setProperty("spring.data.redis.port", "6379"); } @Autowired diff --git a/src/test/java/com/prgrms/nabmart/domain/payment/support/PaymentFixture.java b/src/test/java/com/prgrms/nabmart/domain/payment/support/PaymentFixture.java index f7b08316c..0c2daab6b 100644 --- a/src/test/java/com/prgrms/nabmart/domain/payment/support/PaymentFixture.java +++ b/src/test/java/com/prgrms/nabmart/domain/payment/support/PaymentFixture.java @@ -24,4 +24,10 @@ public static Payment canceledPayment(User user, Order order) { return payment; } + + public static Payment successPayment(User user, Order order) { + Payment payment = pendingPayment(user, order); + ReflectionTestUtils.setField(payment, "paymentStatus", PaymentStatus.SUCCESS); + return payment; + } } diff --git a/src/test/java/com/prgrms/nabmart/domain/user/UserIntegrationTest.java b/src/test/java/com/prgrms/nabmart/domain/user/UserIntegrationTest.java new file mode 100644 index 000000000..8592d7384 --- /dev/null +++ b/src/test/java/com/prgrms/nabmart/domain/user/UserIntegrationTest.java @@ -0,0 +1,171 @@ +package com.prgrms.nabmart.domain.user; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.prgrms.nabmart.base.IntegrationTest; +import com.prgrms.nabmart.domain.cart.Cart; +import com.prgrms.nabmart.domain.cart.CartItem; +import com.prgrms.nabmart.domain.cart.support.CartFixture; +import com.prgrms.nabmart.domain.cart.support.CartItemFixture; +import com.prgrms.nabmart.domain.category.MainCategory; +import com.prgrms.nabmart.domain.category.SubCategory; +import com.prgrms.nabmart.domain.category.fixture.CategoryFixture; +import com.prgrms.nabmart.domain.coupon.Coupon; +import com.prgrms.nabmart.domain.coupon.UserCoupon; +import com.prgrms.nabmart.domain.coupon.support.CouponFixture; +import com.prgrms.nabmart.domain.delivery.Delivery; +import com.prgrms.nabmart.domain.delivery.Rider; +import com.prgrms.nabmart.domain.delivery.support.DeliveryFixture; +import com.prgrms.nabmart.domain.item.Item; +import com.prgrms.nabmart.domain.item.LikeItem; +import com.prgrms.nabmart.domain.item.support.ItemFixture; +import com.prgrms.nabmart.domain.order.Order; +import com.prgrms.nabmart.domain.order.OrderItem; +import com.prgrms.nabmart.domain.payment.Payment; +import com.prgrms.nabmart.domain.payment.support.PaymentFixture; +import com.prgrms.nabmart.domain.review.Review; +import com.prgrms.nabmart.domain.review.support.ReviewFixture; +import com.prgrms.nabmart.domain.user.service.UserService; +import com.prgrms.nabmart.domain.user.support.UserFixture; +import jakarta.persistence.EntityManager; +import java.util.List; +import java.util.Properties; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.transaction.annotation.Transactional; + +@Transactional +@SpringBootTest +public class UserIntegrationTest extends IntegrationTest { + + @BeforeAll + static void beforeAll() { + Properties properties = System.getProperties(); + properties.setProperty("ISSUER", "issuer"); + properties.setProperty("CLIENT_SECRET", "clientSecret"); + properties.setProperty("NAVER_CLIENT_ID", "naverClientId"); + properties.setProperty("NAVER_CLIENT_SECRET", "naverClientSecret"); + properties.setProperty("KAKAO_CLIENT_ID", "kakaoClientId"); + properties.setProperty("KAKAO_CLIENT_SECRET", "kakaoClientSecret"); + properties.setProperty("REDIRECT_URI", + "http://localhost:8080/login/oauth2/code/{registrationId}"); + properties.setProperty("EXPIRY_SECONDS", "60"); + properties.setProperty("TOSS_SUCCESS_URL", "tossSuccessUrl"); + properties.setProperty("TOSS_FAIL_URL", "tossFailUrl"); + properties.setProperty("TOSS_SECRET_KEY", "tossSecretKey"); + properties.setProperty("REDIS_HOST", "localhost"); + properties.setProperty("REDIS_PORT", "6379"); + properties.setProperty("spring.data.redis.host", "localhost"); + properties.setProperty("spring.data.redis.port", "6379"); + } + + @Autowired + UserService userService; + + @Autowired + EntityManager em; + + protected User user = UserFixture.user(); + protected MainCategory mainCategory = CategoryFixture.mainCategory(); + protected SubCategory subCategory = CategoryFixture.subCategory(mainCategory); + protected Item item = ItemFixture.item(mainCategory, subCategory); + protected Cart cart = CartFixture.cart(user); + protected CartItem cartItem = CartItemFixture.cartItem(cart, item, 5); + protected Review review = ReviewFixture.review(user, item); + protected LikeItem likeItem = ItemFixture.likeItem(user, item); + protected Coupon coupon = CouponFixture.coupon(); + protected UserCoupon userCoupon = new UserCoupon(user, coupon); + protected OrderItem orderItem = new OrderItem(item, 5); + protected Order order = new Order(user, List.of(orderItem)); + protected Payment payment = PaymentFixture.pendingPayment(user, order); + protected Rider rider = DeliveryFixture.rider(); + protected Delivery delivery = DeliveryFixture.completedDelivery(order, rider); + + @BeforeEach + void setUp() { + userRepository.save(user); + mainCategoryRepository.save(mainCategory); + subCategoryRepository.save(subCategory); + itemRepository.save(item); + cartRepository.save(cart); + cartItemRepository.save(cartItem); + reviewRepository.save(review); + likeItemRepository.save(likeItem); + couponRepository.save(coupon); + userCouponRepository.save(userCoupon); + orderRepository.save(order); + paymentRepository.save(payment); + riderRepository.save(rider); + deliveryRepository.save(delivery); + } + + @Nested + @DisplayName("유저 삭제 시 연과된 엔티티도 삭제된다.") + class DeleteUserTest { + + @Test + @DisplayName("성공: 유저와 연관된 데이터는 삭제") + void successWhenUserDataIsDeleted() { + //given + Long userId = user.getUserId(); + + //when + userService.deleteUser(userId); + em.flush(); + em.clear(); + + //then + assertThat(orderRepository.findById(order.getOrderId())).isEmpty(); + assertThat(orderItemRepository.findById(orderItem.getOrderItemId())).isEmpty(); + assertThat(reviewRepository.findById(review.getReviewId())).isEmpty(); + assertThat(likeItemRepository.findById(likeItem.getLikeItemId())).isEmpty(); + assertThat(cartItemRepository.findById(cartItem.getCartItemId())).isEmpty(); + assertThat(cartRepository.findById(cart.getCartId())).isEmpty(); + assertThat(paymentRepository.findById(payment.getPayId())).isEmpty(); + assertThat(userCouponRepository.findById(userCoupon.getUserCouponId())).isEmpty(); + assertThat(userRepository.findById(user.getUserId())).isEmpty(); + } + + @Test + @DisplayName("성공: 서비스와 연관된 데이터는 삭제되지 않음") + void successWhenServiceDataStillAlive() { + //given + Long userId = user.getUserId(); + + //when + userService.deleteUser(userId); + em.flush(); + em.clear(); + + //when + assertThat( + mainCategoryRepository.findById(mainCategory.getMainCategoryId())).isNotEmpty(); + assertThat(subCategoryRepository.findById(subCategory.getSubCategoryId())).isNotEmpty(); + assertThat(itemRepository.findById(item.getItemId())).isNotEmpty(); + assertThat(couponRepository.findById(coupon.getCouponId())).isNotEmpty(); + assertThat(riderRepository.findById(rider.getRiderId())).isNotEmpty(); + assertThat(deliveryRepository.findById(delivery.getDeliveryId())).isNotEmpty(); + } + + @Test + @DisplayName("성공: Delivery와 User 관련 필드 업데이트") + void successDeliveryUpdate() { + //given + Long userId = user.getUserId(); + + //when + userService.deleteUser(userId); + em.flush(); + em.clear(); + + //then + assertThat(delivery.getAddress()).isEqualTo("삭제됨"); + assertThat(delivery.getOrder()).isNull(); + } + } +}