|
|
@@ -0,0 +1,159 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import com.etotem.cfc.entity.PaymentOrder;
|
|
|
+import com.etotem.cfc.entity.ProductOrder;
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 订单支付成功 webhook 通知(cfOrder)组装测试。
|
|
|
+ * 覆盖:ProductOrderService.notifyOrderPaidWebhook(商品订单,含收件人+金额分转元)
|
|
|
+ * + MembershipService.notifyMembershipPaidWebhook(会员付费,金额分转元)
|
|
|
+ * 模式:Mockito + ReflectionTestUtils,不起 Spring 上下文。
|
|
|
+ */
|
|
|
+public class OrderWebhookNotifyTest {
|
|
|
+
|
|
|
+ private ProductOrderService productOrderService;
|
|
|
+ private MembershipService membershipService;
|
|
|
+ private WechatWaitMsgService wechatWaitMsgService;
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ public void setUp() {
|
|
|
+ productOrderService = new ProductOrderService();
|
|
|
+ membershipService = new MembershipService();
|
|
|
+ wechatWaitMsgService = mock(WechatWaitMsgService.class);
|
|
|
+ userMapper = mock(UserMapper.class);
|
|
|
+
|
|
|
+ ReflectionTestUtils.setField(productOrderService, "wechatWaitMsgService", wechatWaitMsgService);
|
|
|
+ ReflectionTestUtils.setField(productOrderService, "userMapper", userMapper);
|
|
|
+ ReflectionTestUtils.setField(membershipService, "wechatWaitMsgService", wechatWaitMsgService);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void 商品订单_包含订单号商品金额下单人收件人() {
|
|
|
+ ProductOrder order = new ProductOrder();
|
|
|
+ order.setOrderNo("O20261110TEST001");
|
|
|
+ order.setProductName("鳕鱼");
|
|
|
+ order.setQuantity(2);
|
|
|
+ order.setMoneyAmount(5990); // 分 → ¥59.90
|
|
|
+ order.setBuyerId(27L);
|
|
|
+ order.setAddressSnapshot("{\"receiverName\":\"张三\",\"phone\":\"13800138000\","
|
|
|
+ + "\"province\":\"北京市\",\"city\":\"北京市\",\"district\":\"朝阳区\",\"street\":\"望京街1号\"}");
|
|
|
+
|
|
|
+ User buyer = new User();
|
|
|
+ buyer.setId(27L);
|
|
|
+ buyer.setNickname("CF");
|
|
|
+ when(userMapper.selectById(27L)).thenReturn(buyer);
|
|
|
+
|
|
|
+ ReflectionTestUtils.invokeMethod(productOrderService, "notifyOrderPaidWebhook", order);
|
|
|
+
|
|
|
+ ArgumentCaptor<String> contentCaptor = ArgumentCaptor.forClass(String.class);
|
|
|
+ verify(wechatWaitMsgService).sendWaitMsg(contentCaptor.capture(), eq("cfOrder"));
|
|
|
+ String content = contentCaptor.getValue();
|
|
|
+ assertTrue(content.contains("O20261110TEST001"), "含订单号: " + content);
|
|
|
+ assertTrue(content.contains("鳕鱼 x2"), "含商品名与数量: " + content);
|
|
|
+ assertTrue(content.contains("¥59.90"), "金额分转元(5990→59.90): " + content);
|
|
|
+ assertTrue(content.contains("CF"), "含下单人昵称: " + content);
|
|
|
+ assertTrue(content.contains("张三"), "含收件人姓名: " + content);
|
|
|
+ assertTrue(content.contains("13800138000"), "含收件人电话: " + content);
|
|
|
+ assertTrue(content.contains("北京市北京市朝阳区望京街1号"), "含完整地址: " + content);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void 商品订单_无地址快照_显示自提无需配送() {
|
|
|
+ ProductOrder order = new ProductOrder();
|
|
|
+ order.setOrderNo("O20261110TEST002");
|
|
|
+ order.setProductName("会员卡");
|
|
|
+ order.setQuantity(1);
|
|
|
+ order.setMoneyAmount(9900);
|
|
|
+ order.setBuyerId(34L);
|
|
|
+
|
|
|
+ User buyer = new User();
|
|
|
+ buyer.setId(34L);
|
|
|
+ buyer.setNickname("书宁宁");
|
|
|
+ when(userMapper.selectById(34L)).thenReturn(buyer);
|
|
|
+
|
|
|
+ ReflectionTestUtils.invokeMethod(productOrderService, "notifyOrderPaidWebhook", order);
|
|
|
+
|
|
|
+ ArgumentCaptor<String> contentCaptor = ArgumentCaptor.forClass(String.class);
|
|
|
+ verify(wechatWaitMsgService).sendWaitMsg(contentCaptor.capture(), eq("cfOrder"));
|
|
|
+ String content = contentCaptor.getValue();
|
|
|
+ assertTrue(content.contains("自提/无需配送"), "无地址时显示自提: " + content);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void 会员付费_包含订单号等级金额下单人() {
|
|
|
+ PaymentOrder order = new PaymentOrder();
|
|
|
+ order.setOrderNo("M20261110TEST003");
|
|
|
+ order.setLevelCode("BASIC");
|
|
|
+ order.setPeriod("yearly");
|
|
|
+ order.setAmount(19900); // 分 → ¥199.00
|
|
|
+
|
|
|
+ User admin = new User();
|
|
|
+ admin.setId(27L);
|
|
|
+ admin.setNickname("CF");
|
|
|
+
|
|
|
+ ReflectionTestUtils.invokeMethod(membershipService, "notifyMembershipPaidWebhook", order, admin);
|
|
|
+
|
|
|
+ ArgumentCaptor<String> contentCaptor = ArgumentCaptor.forClass(String.class);
|
|
|
+ verify(wechatWaitMsgService).sendWaitMsg(contentCaptor.capture(), eq("cfOrder"));
|
|
|
+ String content = contentCaptor.getValue();
|
|
|
+ assertTrue(content.contains("M20261110TEST003"), "含订单号: " + content);
|
|
|
+ assertTrue(content.contains("BASIC"), "含等级: " + content);
|
|
|
+ assertTrue(content.contains("yearly"), "含订阅周期: " + content);
|
|
|
+ assertTrue(content.contains("¥199.00"), "金额分转元(19900→199.00): " + content);
|
|
|
+ assertTrue(content.contains("CF"), "含下单人: " + content);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void 会员付费_金额为0_不下发空内容() {
|
|
|
+ PaymentOrder order = new PaymentOrder();
|
|
|
+ order.setOrderNo("M20261110TEST004");
|
|
|
+ order.setLevelCode("BASIC");
|
|
|
+ order.setAmount(null);
|
|
|
+
|
|
|
+ ReflectionTestUtils.invokeMethod(membershipService, "notifyMembershipPaidWebhook", order, null);
|
|
|
+
|
|
|
+ ArgumentCaptor<String> contentCaptor = ArgumentCaptor.forClass(String.class);
|
|
|
+ verify(wechatWaitMsgService).sendWaitMsg(contentCaptor.capture(), eq("cfOrder"));
|
|
|
+ String content = contentCaptor.getValue();
|
|
|
+ assertTrue(content.contains("¥0.00"), "金额为0正常显示: " + content);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void 商品订单_金额同totalAmount_fallback() {
|
|
|
+ ProductOrder order = new ProductOrder();
|
|
|
+ order.setOrderNo("O20261110TEST005");
|
|
|
+ order.setProductName("测试商品");
|
|
|
+ order.setQuantity(1);
|
|
|
+ order.setMoneyAmount(null); // fallback 到 totalAmount
|
|
|
+ order.setTotalAmount(12345);
|
|
|
+ order.setBuyerId(105L);
|
|
|
+ order.setAddressSnapshot(null);
|
|
|
+
|
|
|
+ User buyer = new User();
|
|
|
+ buyer.setId(105L);
|
|
|
+ buyer.setNickname(null);
|
|
|
+ when(userMapper.selectById(105L)).thenReturn(buyer);
|
|
|
+
|
|
|
+ ReflectionTestUtils.invokeMethod(productOrderService, "notifyOrderPaidWebhook", order);
|
|
|
+
|
|
|
+ ArgumentCaptor<String> contentCaptor = ArgumentCaptor.forClass(String.class);
|
|
|
+ verify(wechatWaitMsgService).sendWaitMsg(contentCaptor.capture(), eq("cfOrder"));
|
|
|
+ String content = contentCaptor.getValue();
|
|
|
+ assertTrue(content.contains("¥123.45"), "fallback totalAmount 12345→123.45: " + content);
|
|
|
+ assertTrue(content.contains("用户105"), "昵称缺失回退用户ID: " + content);
|
|
|
+ }
|
|
|
+}
|