|
|
@@ -0,0 +1,327 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.dto.CreateProductOrderDTO;
|
|
|
+import com.etotem.cfc.dto.ProductOrderDTO;
|
|
|
+import com.etotem.cfc.entity.Product;
|
|
|
+import com.etotem.cfc.entity.ProductOrder;
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
+import com.etotem.cfc.mapper.ProductMapper;
|
|
|
+import com.etotem.cfc.mapper.ProductOrderMapper;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
+import com.etotem.cfc.service.InventoryService;
|
|
|
+import com.etotem.cfc.service.MemberPriceResolver;
|
|
|
+import com.etotem.cfc.service.PaymentService;
|
|
|
+import com.etotem.cfc.service.ProductGiftRuleService;
|
|
|
+import com.etotem.cfc.service.ProductOrderService;
|
|
|
+import com.etotem.cfc.service.ProductService;
|
|
|
+import com.etotem.cfc.util.RedisLockUtil;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
+import static org.mockito.ArgumentMatchers.anyString;
|
|
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.never;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * SC-X-07 商城履约闭环(下单→支付→发货→确认收货)+ SC-P-55 订单支付 契约测试(S-cfc-12,product.order)
|
|
|
+ *
|
|
|
+ * <p>覆盖三类流(纯 Mockito,无 Spring 上下文 / 无 DB):</p>
|
|
|
+ * <ul>
|
|
|
+ * <li><b>正常流</b>:创建订单(待支付)→ 支付回调(标记已付+扣库存)→ 快递发货(待收货)→ 确认收货(完成);</li>
|
|
|
+ * <li><b>异常流</b>:商品不存在/已下架/库存不足;订单不存在/状态异常;发货未支付;确认收货非待收货;</li>
|
|
|
+ * <li><b>权限流</b>:非本人支付 → 「无权操作此订单」;非买家确认收货 → 「无权操作此订单」;未登录下单 → 「请先登录」。</li>
|
|
|
+ * </ul>
|
|
|
+ */
|
|
|
+class OrderCreatePayShipConfirmScX07ContractTest {
|
|
|
+
|
|
|
+ private ProductOrderService service;
|
|
|
+ private ProductOrderMapper orderMapper;
|
|
|
+ private ProductMapper productMapper;
|
|
|
+ private UserMapper userMapper;
|
|
|
+ private MemberPriceResolver memberPriceResolver;
|
|
|
+ private ProductGiftRuleService productGiftRuleService;
|
|
|
+ private PaymentService paymentService;
|
|
|
+ private ProductService productService;
|
|
|
+ private InventoryService inventoryService;
|
|
|
+ private RedisLockUtil redisLockUtil;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() throws Exception {
|
|
|
+ service = new ProductOrderService();
|
|
|
+ orderMapper = mock(ProductOrderMapper.class);
|
|
|
+ productMapper = mock(ProductMapper.class);
|
|
|
+ userMapper = mock(UserMapper.class);
|
|
|
+ memberPriceResolver = mock(MemberPriceResolver.class);
|
|
|
+ productGiftRuleService = mock(ProductGiftRuleService.class);
|
|
|
+ paymentService = mock(PaymentService.class);
|
|
|
+ productService = mock(ProductService.class);
|
|
|
+ inventoryService = mock(InventoryService.class);
|
|
|
+ redisLockUtil = mock(RedisLockUtil.class);
|
|
|
+
|
|
|
+ inject(service, "orderMapper", orderMapper);
|
|
|
+ inject(service, "productMapper", productMapper);
|
|
|
+ inject(service, "userMapper", userMapper);
|
|
|
+ inject(service, "memberPriceResolver", memberPriceResolver);
|
|
|
+ inject(service, "productGiftRuleService", productGiftRuleService);
|
|
|
+ inject(service, "paymentService", paymentService);
|
|
|
+ inject(service, "productService", productService);
|
|
|
+ inject(service, "inventoryService", inventoryService);
|
|
|
+ inject(service, "redisLockUtil", redisLockUtil);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void inject(Object target, String field, Object value) throws Exception {
|
|
|
+ Field f = target.getClass().getDeclaredField(field);
|
|
|
+ f.setAccessible(true);
|
|
|
+ f.set(target, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Product physicalProduct(Long id, String name, Integer price, Integer stock) {
|
|
|
+ Product p = new Product();
|
|
|
+ p.setId(id);
|
|
|
+ p.setName(name);
|
|
|
+ p.setPrice(price);
|
|
|
+ p.setStatus("on_shelf");
|
|
|
+ p.setProductType("physical");
|
|
|
+ p.setStock(stock);
|
|
|
+ p.setCoverImage("img.png");
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ProductOrder baseOrder(String orderNo, String status, Long buyerId) {
|
|
|
+ ProductOrder o = new ProductOrder();
|
|
|
+ o.setId(1L);
|
|
|
+ o.setOrderNo(orderNo);
|
|
|
+ o.setStatus(status);
|
|
|
+ o.setBuyerId(buyerId);
|
|
|
+ o.setProductId(10L);
|
|
|
+ o.setProductName("成长测评");
|
|
|
+ o.setProductType("physical");
|
|
|
+ o.setQuantity(1);
|
|
|
+ o.setUnitPrice(9900);
|
|
|
+ o.setTotalAmount(9900);
|
|
|
+ o.setMoneyAmount(9900);
|
|
|
+ o.setPointsUsed(0);
|
|
|
+ o.setDeliveryMethod(1);
|
|
|
+ return o;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 正常流(SC-X-07 闭环) ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 正常流_创建订单_待支付() {
|
|
|
+ Product product = physicalProduct(10L, "成长测评", 9900, 100);
|
|
|
+ when(productMapper.selectById(10L)).thenReturn(product);
|
|
|
+ User buyer = new User();
|
|
|
+ buyer.setId(1L);
|
|
|
+ buyer.setFamilyId(5L);
|
|
|
+ when(userMapper.selectById(1L)).thenReturn(buyer);
|
|
|
+ when(memberPriceResolver.resolveEligibleMemberPrice(product, 1L)).thenReturn(9900);
|
|
|
+ when(productGiftRuleService.getActiveByProductId(10L)).thenReturn(null);
|
|
|
+ when(orderMapper.insert(any(ProductOrder.class))).thenAnswer(inv -> {
|
|
|
+ ProductOrder o = inv.getArgument(0);
|
|
|
+ o.setId(100L);
|
|
|
+ return 1;
|
|
|
+ });
|
|
|
+
|
|
|
+ CreateProductOrderDTO dto = new CreateProductOrderDTO();
|
|
|
+ dto.setProductId(10L);
|
|
|
+ dto.setQuantity(1);
|
|
|
+ dto.setAddressSnapshot("北京市朝阳区xx路1号");
|
|
|
+ dto.setPaymentMethod("wechat");
|
|
|
+
|
|
|
+ Result<ProductOrderDTO> result = service.create(dto, 1L);
|
|
|
+
|
|
|
+ assertEquals(200, result.getCode(), "下单应成功");
|
|
|
+ ProductOrderDTO dto2 = result.getData();
|
|
|
+ assertNotNull(dto2);
|
|
|
+ assertEquals("pending", dto2.getStatus(), "新订单应为待支付");
|
|
|
+ assertEquals(9900, dto2.getMoneyAmount());
|
|
|
+ assertEquals(1, dto2.getQuantity());
|
|
|
+ // 订单已入库
|
|
|
+ verify(orderMapper).insert(any(ProductOrder.class));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ void 正常流_支付回调_标记已付并扣库存() {
|
|
|
+ ProductOrder order = baseOrder("PO20261107A", "pending", 1L);
|
|
|
+ order.setOrderNo("PO20261107A");
|
|
|
+ when(orderMapper.selectOne(any(com.baomidou.mybatisplus.core.conditions.Wrapper.class))).thenReturn(order);
|
|
|
+ when(redisLockUtil.tryLock(anyString(), anyLong())).thenReturn(true);
|
|
|
+ Product product = physicalProduct(10L, "成长测评", 9900, 100);
|
|
|
+ when(productMapper.selectById(10L)).thenReturn(product);
|
|
|
+ when(productService.decreaseStock(10L, 1)).thenReturn(true);
|
|
|
+
|
|
|
+ Result<String> result = service.handlePaymentSuccess("PO20261107A", "wx_tx_001");
|
|
|
+
|
|
|
+ assertEquals(200, result.getCode());
|
|
|
+ assertEquals("paid", order.getStatus(), "回调后订单应为已支付");
|
|
|
+ verify(productService).decreaseStock(10L, 1);
|
|
|
+ verify(orderMapper).updateById(order);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 正常流_快递发货_待收货() {
|
|
|
+ ProductOrder order = baseOrder("PO20261107B", "paid", 1L);
|
|
|
+ order.setOrderNo("PO20261107B");
|
|
|
+ when(orderMapper.selectOne(any(com.baomidou.mybatisplus.core.conditions.Wrapper.class))).thenReturn(order);
|
|
|
+ when(userMapper.selectById(1L)).thenReturn(null); // 无openid → 微信同步跳过
|
|
|
+
|
|
|
+ Result<String> result = service.ship("PO20261107B", "SF123456", "顺丰速运");
|
|
|
+
|
|
|
+ assertEquals(200, result.getCode(), "发货应成功");
|
|
|
+ assertEquals("pending_receipt", order.getStatus(), "发货后应待收货");
|
|
|
+ assertEquals("SF123456", order.getLogisticsNo());
|
|
|
+ verify(orderMapper).updateById(order);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 正常流_确认收货_完成() {
|
|
|
+ ProductOrder order = baseOrder("PO20261107C", "pending_receipt", 1L);
|
|
|
+ order.setOrderNo("PO20261107C");
|
|
|
+ when(orderMapper.selectOne(any(com.baomidou.mybatisplus.core.conditions.Wrapper.class))).thenReturn(order);
|
|
|
+
|
|
|
+ Result<String> result = service.confirmReceive("PO20261107C", 1L);
|
|
|
+
|
|
|
+ assertEquals(200, result.getCode(), "确认收货应成功");
|
|
|
+ assertEquals("completed", order.getStatus(), "确认后应完成");
|
|
|
+ verify(orderMapper).updateById(order);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 正常流(SC-P-55 支付) ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ void 正常流_P55_微信支付参数() {
|
|
|
+ ProductOrder order = baseOrder("PO20261107D", "pending", 1L);
|
|
|
+ order.setOrderNo("PO20261107D");
|
|
|
+ when(orderMapper.selectOne(any(com.baomidou.mybatisplus.core.conditions.Wrapper.class))).thenReturn(order);
|
|
|
+ User buyer = new User();
|
|
|
+ buyer.setId(1L);
|
|
|
+ buyer.setOpenid("oABCDEF");
|
|
|
+ when(userMapper.selectById(1L)).thenReturn(buyer);
|
|
|
+ when(paymentService.createWechatPrepay(anyString(), anyString(), any(Integer.class),
|
|
|
+ anyString(), any(List.class))).thenReturn(Collections.singletonMap("prepay_id", "wx_prepay_1"));
|
|
|
+
|
|
|
+ Result<Map<String, Object>> result = service.pay("PO20261107D", 1L);
|
|
|
+
|
|
|
+ assertEquals(200, result.getCode());
|
|
|
+ Map<String, Object> data = result.getData();
|
|
|
+ assertNotNull(data.get("wechatPayParams"), "应返回微信支付参数");
|
|
|
+ assertEquals(0, data.get("pointsUsed"));
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 异常流 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 异常流_创建订单_商品不存在() {
|
|
|
+ when(productMapper.selectById(999L)).thenReturn(null);
|
|
|
+ CreateProductOrderDTO dto = new CreateProductOrderDTO();
|
|
|
+ dto.setProductId(999L);
|
|
|
+ dto.setQuantity(1);
|
|
|
+ Result<ProductOrderDTO> result = service.create(dto, 1L);
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ assertTrue(result.getMessage().contains("不存在"), "实际: " + result.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 异常流_创建订单_已下架() {
|
|
|
+ Product product = physicalProduct(11L, "已下架商品", 5000, 10);
|
|
|
+ product.setStatus("off_shelf");
|
|
|
+ when(productMapper.selectById(11L)).thenReturn(product);
|
|
|
+ CreateProductOrderDTO dto = new CreateProductOrderDTO();
|
|
|
+ dto.setProductId(11L);
|
|
|
+ dto.setQuantity(1);
|
|
|
+ Result<ProductOrderDTO> result = service.create(dto, 1L);
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ assertTrue(result.getMessage().contains("下架"), "实际: " + result.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 异常流_创建订单_库存不足() {
|
|
|
+ Product product = physicalProduct(12L, "库存告急", 5000, 1);
|
|
|
+ when(productMapper.selectById(12L)).thenReturn(product);
|
|
|
+ CreateProductOrderDTO dto = new CreateProductOrderDTO();
|
|
|
+ dto.setProductId(12L);
|
|
|
+ dto.setQuantity(5);
|
|
|
+ Result<ProductOrderDTO> result = service.create(dto, 1L);
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ assertTrue(result.getMessage().contains("库存不足"), "实际: " + result.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 异常流_支付_订单不存在() {
|
|
|
+ when(orderMapper.selectOne(any(com.baomidou.mybatisplus.core.conditions.Wrapper.class))).thenReturn(null);
|
|
|
+ Result<Map<String, Object>> result = service.pay("PO_NOT_EXIST", 1L);
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ assertTrue(result.getMessage().contains("订单不存在"), "实际: " + result.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 异常流_发货_未支付() {
|
|
|
+ ProductOrder order = baseOrder("PO20261107E", "pending", 1L);
|
|
|
+ order.setOrderNo("PO20261107E");
|
|
|
+ when(orderMapper.selectOne(any(com.baomidou.mybatisplus.core.conditions.Wrapper.class))).thenReturn(order);
|
|
|
+ Result<String> result = service.ship("PO20261107E", "SF1", "顺丰");
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ assertTrue(result.getMessage().contains("仅已支付订单可发货"), "实际: " + result.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 异常流_确认收货_非待收货() {
|
|
|
+ ProductOrder order = baseOrder("PO20261107F", "paid", 1L);
|
|
|
+ order.setOrderNo("PO20261107F");
|
|
|
+ when(orderMapper.selectOne(any(com.baomidou.mybatisplus.core.conditions.Wrapper.class))).thenReturn(order);
|
|
|
+ Result<String> result = service.confirmReceive("PO20261107F", 1L);
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ assertTrue(result.getMessage().contains("仅待收货订单可确认收货"), "实际: " + result.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 权限流 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ void 权限流_非本人支付_无权操作() {
|
|
|
+ ProductOrder order = baseOrder("PO20261107G", "pending", 1L); // 买家=1
|
|
|
+ order.setOrderNo("PO20261107G");
|
|
|
+ when(orderMapper.selectOne(any(com.baomidou.mybatisplus.core.conditions.Wrapper.class))).thenReturn(order);
|
|
|
+ Result<Map<String, Object>> result = service.pay("PO20261107G", 2L); // 操作者=2
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ assertTrue(result.getMessage().contains("无权操作此订单"), "实际: " + result.getMessage());
|
|
|
+ verify(paymentService, never()).createWechatPrepay(anyString(), anyString(), any(), anyString(), any());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 权限流_非买家确认收货_无权操作() {
|
|
|
+ ProductOrder order = baseOrder("PO20261107H", "pending_receipt", 1L);
|
|
|
+ order.setOrderNo("PO20261107H");
|
|
|
+ when(orderMapper.selectOne(any(com.baomidou.mybatisplus.core.conditions.Wrapper.class))).thenReturn(order);
|
|
|
+ Result<String> result = service.confirmReceive("PO20261107H", 99L);
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ assertTrue(result.getMessage().contains("无权操作此订单"), "实际: " + result.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void 权限流_未登录下单_请先登录() {
|
|
|
+ CreateProductOrderDTO dto = new CreateProductOrderDTO();
|
|
|
+ dto.setProductId(10L);
|
|
|
+ dto.setQuantity(1);
|
|
|
+ Result<ProductOrderDTO> result = service.create(dto, null);
|
|
|
+ assertEquals(500, result.getCode());
|
|
|
+ assertTrue(result.getMessage().contains("请先登录"), "实际: " + result.getMessage());
|
|
|
+ }
|
|
|
+}
|