|
|
@@ -0,0 +1,140 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.dto.CreateProductOrderDTO;
|
|
|
+import com.etotem.cfc.dto.OrderItemVO;
|
|
|
+import com.etotem.cfc.dto.ProductOrderDTO;
|
|
|
+import com.etotem.cfc.entity.Product;
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
+import com.etotem.cfc.mapper.OrderItemMapper;
|
|
|
+import com.etotem.cfc.mapper.ProductMapper;
|
|
|
+import com.etotem.cfc.mapper.ProductOrderMapper;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
+import com.etotem.cfc.pricing.BundleDiscountResult;
|
|
|
+import com.etotem.cfc.pricing.LineItem;
|
|
|
+import com.etotem.cfc.service.BundlePricingService;
|
|
|
+import com.etotem.cfc.service.CouponService;
|
|
|
+import com.etotem.cfc.service.MemberPriceResolver;
|
|
|
+import com.etotem.cfc.service.ProductGiftRuleService;
|
|
|
+import com.etotem.cfc.service.ProductOrderService;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
+import org.mockito.InjectMocks;
|
|
|
+import org.mockito.Mock;
|
|
|
+import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.ArgumentMatchers.anyList;
|
|
|
+import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * ProductOrderService.createMultiItem 单测:服务端权威重算套餐优惠计入 moneyAmount(前端 discountAmount 仅含规则优惠)。
|
|
|
+ */
|
|
|
+@ExtendWith(MockitoExtension.class)
|
|
|
+class ProductOrderServiceTest {
|
|
|
+
|
|
|
+ @Mock private ProductMapper productMapper;
|
|
|
+ @Mock private UserMapper userMapper;
|
|
|
+ @Mock private ProductOrderMapper orderMapper;
|
|
|
+ @Mock private OrderItemMapper orderItemMapper;
|
|
|
+ @Mock private MemberPriceResolver memberPriceResolver;
|
|
|
+ @Mock private BundlePricingService bundlePricingService;
|
|
|
+ @Mock private ProductGiftRuleService productGiftRuleService;
|
|
|
+ @Mock private CouponService couponService;
|
|
|
+ @InjectMocks private ProductOrderService orderService;
|
|
|
+
|
|
|
+ private Product product(Long id, String name, int price) {
|
|
|
+ Product p = new Product();
|
|
|
+ p.setId(id);
|
|
|
+ p.setName(name);
|
|
|
+ p.setPrice(price);
|
|
|
+ p.setProductType("assessment");
|
|
|
+ p.setStatus("on_shelf");
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+
|
|
|
+ private CreateProductOrderDTO dto(long productId, int qty, Integer discountAmount) {
|
|
|
+ CreateProductOrderDTO dto = new CreateProductOrderDTO();
|
|
|
+ OrderItemVO item = new OrderItemVO();
|
|
|
+ item.setProductId(productId);
|
|
|
+ item.setQuantity(qty);
|
|
|
+ dto.setItems(Collections.singletonList(item));
|
|
|
+ dto.setDiscountAmount(discountAmount);
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void createMultiItem_addsServerBundleDiscountToMoneyAmount() {
|
|
|
+ // 2×69900 配套餐省11800 → moneyAmount = 139800 - 0 - 11800 = 128000
|
|
|
+ when(productMapper.selectById(10L)).thenReturn(product(10L, "DAN测评", 69900));
|
|
|
+ when(memberPriceResolver.resolveEligibleMemberPrice(any(Product.class), anyLong())).thenReturn(69900);
|
|
|
+ BundleDiscountResult r = new BundleDiscountResult();
|
|
|
+ r.setActive(true);
|
|
|
+ r.setOriginalTotal(139800);
|
|
|
+ r.setPayPrice(128000);
|
|
|
+ r.setDiscount(11800);
|
|
|
+ r.setBundleList(new ArrayList<>());
|
|
|
+ when(bundlePricingService.compute(anyList())).thenReturn(r);
|
|
|
+ when(userMapper.selectById(anyLong())).thenReturn(new User());
|
|
|
+ when(orderMapper.insert(any())).thenReturn(1);
|
|
|
+ when(orderItemMapper.insert(any())).thenReturn(1);
|
|
|
+
|
|
|
+ Result<ProductOrderDTO> result = orderService.create(dto(10L, 2, null), 1L);
|
|
|
+
|
|
|
+ assertTrue(result.getCode() == 200, "下单应成功: " + result.getMessage());
|
|
|
+ assertEquals(128000, result.getData().getMoneyAmount());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void createMultiItem_bundleLinesCollectResolvedIdentityAndPrice() {
|
|
|
+ // 收集行项:解析后 productId、skuId=null、quantity、unitPrice=69900
|
|
|
+ when(productMapper.selectById(10L)).thenReturn(product(10L, "DAN测评", 69900));
|
|
|
+ when(memberPriceResolver.resolveEligibleMemberPrice(any(Product.class), anyLong())).thenReturn(69900);
|
|
|
+ when(bundlePricingService.compute(anyList())).thenReturn(BundleDiscountResult.inactive());
|
|
|
+ when(userMapper.selectById(anyLong())).thenReturn(new User());
|
|
|
+ when(orderMapper.insert(any())).thenReturn(1);
|
|
|
+ when(orderItemMapper.insert(any())).thenReturn(1);
|
|
|
+
|
|
|
+ orderService.create(dto(10L, 2, null), 1L);
|
|
|
+
|
|
|
+ ArgumentCaptor<List<LineItem>> captor = ArgumentCaptor.forClass(List.class);
|
|
|
+ verify(bundlePricingService).compute(captor.capture());
|
|
|
+ List<LineItem> lines = captor.getValue();
|
|
|
+ assertEquals(1, lines.size());
|
|
|
+ assertEquals(10L, lines.get(0).getProductId());
|
|
|
+ assertEquals(null, lines.get(0).getSkuId());
|
|
|
+ assertEquals(2, lines.get(0).getQuantity());
|
|
|
+ assertEquals(69900, lines.get(0).getUnitPrice());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void createMultiItem_frontendRuleDiscountNotMixedWithBundle() {
|
|
|
+ // 前端传入规则优惠5000(不含套餐)+ 套餐11800 → moneyAmount = 139800 - 5000 - 11800 = 123000
|
|
|
+ when(productMapper.selectById(10L)).thenReturn(product(10L, "DAN测评", 69900));
|
|
|
+ when(memberPriceResolver.resolveEligibleMemberPrice(any(Product.class), anyLong())).thenReturn(69900);
|
|
|
+ BundleDiscountResult r = new BundleDiscountResult();
|
|
|
+ r.setActive(true);
|
|
|
+ r.setOriginalTotal(139800);
|
|
|
+ r.setPayPrice(128000);
|
|
|
+ r.setDiscount(11800);
|
|
|
+ r.setBundleList(new ArrayList<>());
|
|
|
+ when(bundlePricingService.compute(anyList())).thenReturn(r);
|
|
|
+ when(userMapper.selectById(anyLong())).thenReturn(new User());
|
|
|
+ when(orderMapper.insert(any())).thenReturn(1);
|
|
|
+ when(orderItemMapper.insert(any())).thenReturn(1);
|
|
|
+
|
|
|
+ Result<ProductOrderDTO> result = orderService.create(dto(10L, 2, 5000), 1L);
|
|
|
+
|
|
|
+ assertTrue(result.getCode() == 200);
|
|
|
+ assertEquals(123000, result.getData().getMoneyAmount());
|
|
|
+ }
|
|
|
+}
|