Skip to content

Commit

Permalink
placeOrder의 유효성 검증 중 validateHasDealProductUuid 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
daadaadaah committed Jun 24, 2023
1 parent c972fa3 commit 91e8443
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public T getOneByKeyAndHashKey(String key, String hashKey, TypeReference<T> obje
return convertObjectFromString(item, objectTypeReference);
}

public boolean hasKey(String key, String hashKey) {
return hashOperations.hasKey(key, hashKey);
}

public List<T> getAllByKey(String key, TypeReference<T> objectTypeReference) {
List<T> list = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ public TimeDealProductDetail getTimeDealProductDetailByDealProductUuid(UUID deal
return timeDealProductDetail;
}

public boolean hasDealProductUuid(UUID dealProductUuid) {
String dealOpenDate = getDateForCurrentDealProducts();

String key = "timeDealProducts:"+dealOpenDate;

String hashKey = dealProductUuid.toString();

return redisHashRepository.hasKey(key, hashKey);
}

private List<TimeDealProductSummary> convertTimeDealProductToTimeDealProductSummary(List<TimeDealProduct> timeDealProducts) {

List<TimeDealProductSummary> timeDealProductSummaries = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.hcommerce.heecommerce.deal;

import java.util.UUID;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.NOT_FOUND)
public class TimeDealProductNotFoundException extends RuntimeException {
public TimeDealProductNotFoundException(UUID orderUuid) {
super(orderUuid + " : 해당 타임 딜 상품을 찾을 수 없습니다.");
}
}
43 changes: 40 additions & 3 deletions src/main/java/com/hcommerce/heecommerce/order/OrderService.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
package com.hcommerce.heecommerce.order;

import com.hcommerce.heecommerce.deal.DealQueryRepository;
import com.hcommerce.heecommerce.deal.TimeDealProductNotFoundException;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class OrderService {

private OrderCommandRepository orderRepository;

private DealQueryRepository dealQueryRepository;

@Autowired
public OrderService(OrderCommandRepository orderRepository) {
public OrderService(
OrderCommandRepository orderRepository,
DealQueryRepository dealQueryRepository
) {
this.orderRepository = orderRepository;
this.dealQueryRepository = dealQueryRepository;
}

public void completeOrderReceipt(UUID orderUuid) {
Expand All @@ -22,4 +29,34 @@ public void completeOrderReceipt(UUID orderUuid) {
throw new OrderNotFoundException(orderUuid);
}
}

public void placeOrder(OrderForm orderForm) {
validateOrderForm(orderForm);
}

private void validateOrderForm(OrderForm orderForm) {
validateHasDealProductUuid(orderForm.getDealProductUuid());

// TODO : Mybatis 연동이 필요하므로, 다른 PR에서 작업할 예정
validateHasUserId(orderForm.getUserId());

// TODO : RedisRepository에 추가적인 함수 필요하므로, 다른 PR에서 작업할 예정
validateOrderQuantityInMaxOrderQuantityPerOrder(orderForm.getOrderQuantity());
}

private void validateHasDealProductUuid(UUID dealProductUuid) {
boolean hasDealProductUuid = dealQueryRepository.hasDealProductUuid(dealProductUuid);

if(!hasDealProductUuid) {
throw new TimeDealProductNotFoundException(dealProductUuid);
}
}

private void validateHasUserId(int userId) {

}

private void validateOrderQuantityInMaxOrderQuantityPerOrder(int orderQuantity) {

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package com.hcommerce.heecommerce.order;

import static com.hcommerce.heecommerce.order.OutOfStockHandlingOption.ALL_CANCEL;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.verify;
import static org.mockito.Mockito.times;

import com.hcommerce.heecommerce.deal.DealQueryRepository;
import com.hcommerce.heecommerce.deal.TimeDealProductNotFoundException;
import java.util.UUID;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -8,25 +17,24 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.verify;
import static org.mockito.Mockito.times;

@DisplayName("OrderService")
@ExtendWith(MockitoExtension.class)
class OrderServiceTest {

@Mock
private OrderCommandRepository orderCommandRepository;

@Mock
private DealQueryRepository dealQueryRepository;

@InjectMocks
private OrderService orderService;

private static final UUID TEMP_NOT_EXIST_UUID = UUID.fromString("8b455042-e709-11ed-93e5-0242ac110002");


private static final UUID NOT_EXIST_DEAL_PRODUCTUUID = UUID.fromString("8b455042-e709-11ed-93e5-0242ac110001");

@Nested
@DisplayName("completeOrderReceipt")
class Describe_CompleteOrderReceipt {
Expand Down Expand Up @@ -65,4 +73,42 @@ void it_throws_OrderNotFoundException() {
}
}
}

@Nested
@DisplayName("placeOrder")
class Describe_PlaceOrder {

@Nested
@DisplayName("with invalid OrderForm's DealProductUuid")
class Context_With_Invalid_DealProductUuid {
@Test
@DisplayName("throws TimeDealProductNotFoundException")
void It_throws_TimeDealProductNotFoundException() {
// given
given(dealQueryRepository.hasDealProductUuid(NOT_EXIST_DEAL_PRODUCTUUID)).willReturn(false);

OrderForm orderForm = OrderForm.builder()
.userId(1)
.recipientInfoForm(
RecipientInfoForm.builder()
.recipientName("leecommerce")
.recipientPhoneNumber("01087654321")
.recipientAddress("서울시 ")
.recipientDetailAddress("101호")
.shippingRequest("빠른 배송 부탁드려요!")
.build()
)
.outOfStockHandlingOption(ALL_CANCEL)
.dealProductUuid(NOT_EXIST_DEAL_PRODUCTUUID)
.orderQuantity(1)
.paymentType(PaymentType.CREDIT_CARD)
.build();

// then
assertThrows(TimeDealProductNotFoundException.class, () -> {
orderService.placeOrder(orderForm);
});
}
}
}
}

0 comments on commit 91e8443

Please sign in to comment.