Skip to content

Commit

Permalink
refactor: OrderSellerServiceTest 리팩토링 & 상품재고가 0일 때, 재고가 부족할 때 예외처리 테스…
Browse files Browse the repository at this point in the history
…트 추가
  • Loading branch information
axhtl committed Sep 7, 2024
1 parent ef3f930 commit 9aacf5e
Show file tree
Hide file tree
Showing 2 changed files with 349 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.store.clothstar.order

import com.fasterxml.jackson.databind.ObjectMapper
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
Expand All @@ -13,8 +15,7 @@ import org.springframework.test.context.ActiveProfiles
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.ResultActions
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
import org.springframework.transaction.annotation.Transactional
import org.store.clothstar.category.repository.CategoryJpaRepository
import org.store.clothstar.member.domain.Member
Expand All @@ -33,6 +34,9 @@ import org.store.clothstar.order.dto.request.OrderRequestWrapper
import org.store.clothstar.order.repository.OrderDetailRepository
import org.store.clothstar.order.repository.OrderRepository
import org.store.clothstar.order.util.CreateOrderObject
import org.store.clothstar.product.domain.Item
import org.store.clothstar.product.domain.type.DisplayStatus
import org.store.clothstar.product.domain.type.SaleStatus
import org.store.clothstar.product.repository.ItemRepository
import org.store.clothstar.product.repository.ProductRepository
import java.time.LocalDateTime
Expand Down Expand Up @@ -198,6 +202,82 @@ class OrderUserIntegrationTest(
assertEquals(1, savedOrder.orderDetails.size)
}

@Test
@DisplayName("주문 생성시 상품재고가 0일경우 에러처리 테스트")
fun testCreateOrder_itemStockZeroException() {
//given
val member: Member = memberRepository.save(CreateOrderObject.getMember())
val address = addressRepository.save(CreateOrderObject.getAddress(member))
val category = categoryRepository.save(CreateOrderObject.getCategory())
sellerRepository.save(CreateOrderObject.getSeller(member))
val product = productRepository.save(CreateOrderObject.getProduct(member, category))
val item = itemRepository.save(CreateOrderObject.getItem(product)).apply {
stock = 0
}

val createOrderRequest = CreateOrderRequest(
paymentMethod = PaymentMethod.CARD,
memberId = member.memberId!!,
addressId = address.addressId!!
)
val createOrderDetailRequest = CreateOrderDetailRequest(
productId = product.productId!!,
itemId = item.itemId!!,
quantity = 1
)
val orderRequestWrapper = OrderRequestWrapper(createOrderRequest, createOrderDetailRequest)
val requestBody = objectMapper.writeValueAsString(orderRequestWrapper)

//when & then
val actions: ResultActions = mockMvc.perform(
MockMvcRequestBuilders.post(ORDER_URL)
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody)
)
.andExpect(status().isBadRequest)
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.errorCode").value(400))
.andExpect(jsonPath("$.message").value("품절된 상품입니다."))
}

@Test
@DisplayName("주문 생성시, 주문개수가 상품재고보다 많을 때 재고 부족 예외처리 테스트")
fun testCreateOrder_InsufficientStock_Exception() {
//given
val member: Member = memberRepository.save(CreateOrderObject.getMember())
val address = addressRepository.save(CreateOrderObject.getAddress(member))
val category = categoryRepository.save(CreateOrderObject.getCategory())
sellerRepository.save(CreateOrderObject.getSeller(member))
val product = productRepository.save(CreateOrderObject.getProduct(member, category))
val item = itemRepository.save(CreateOrderObject.getItem(product)).apply {
stock = 1
}

val createOrderRequest = CreateOrderRequest(
paymentMethod = PaymentMethod.CARD,
memberId = member.memberId!!,
addressId = address.addressId!!
)
val createOrderDetailRequest = CreateOrderDetailRequest(
productId = product.productId!!,
itemId = item.itemId!!,
quantity = 5
)
val orderRequestWrapper = OrderRequestWrapper(createOrderRequest, createOrderDetailRequest)
val requestBody = objectMapper.writeValueAsString(orderRequestWrapper)

//when & then
val actions: ResultActions = mockMvc.perform(
MockMvcRequestBuilders.post(ORDER_URL)
.contentType(MediaType.APPLICATION_JSON)
.content(requestBody)
)
.andExpect(status().isBadRequest)
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.errorCode").value(400))
.andExpect(jsonPath("$.message").value("주문 개수가 상품 재고보다 더 많아 요청을 처리할 수 없습니다."))
}

@Test
@DisplayName("주문상세 추가")
fun testAddOrderDetail() {
Expand Down
Loading

0 comments on commit 9aacf5e

Please sign in to comment.