by gateway:

1. 补全 channel 单元测试
2. 完善部分 order 单元测试
This commit is contained in:
zhijiantianya@gmail.com
2023-07-18 20:21:25 +08:00
parent 1c282bd3cb
commit 3f410c2735
16 changed files with 493 additions and 230 deletions

View File

@@ -90,7 +90,7 @@ public class PayAppServiceTest extends BaseDbUnitTest {
PayAppUpdateReqVO reqVO = randomPojo(PayAppUpdateReqVO.class, o ->
o.setStatus((RandomUtil.randomEle(CommonStatusEnum.values()).getStatus())));
// 调用, 并断言异常
assertServiceException(() -> appService.updateApp(reqVO), PAY_APP_NOT_FOUND);
assertServiceException(() -> appService.updateApp(reqVO), APP_NOT_FOUND);
}
@Test
@@ -130,7 +130,7 @@ public class PayAppServiceTest extends BaseDbUnitTest {
Long id = randomLongId();
// 调用, 并断言异常
assertServiceException(() -> appService.deleteApp(id), PAY_APP_NOT_FOUND);
assertServiceException(() -> appService.deleteApp(id), APP_NOT_FOUND);
}
@Test
@@ -144,7 +144,7 @@ public class PayAppServiceTest extends BaseDbUnitTest {
when(orderService.getOrderCountByAppId(eq(id))).thenReturn(10L);
// 调用, 并断言异常
assertServiceException(() -> appService.deleteApp(id), PAY_APP_EXIST_ORDER_CANT_DELETE);
assertServiceException(() -> appService.deleteApp(id), APP_EXIST_ORDER_CANT_DELETE);
}
@Test
@@ -158,7 +158,7 @@ public class PayAppServiceTest extends BaseDbUnitTest {
when(refundService.getRefundCountByAppId(eq(id))).thenReturn(10L);
// 调用, 并断言异常
assertServiceException(() -> appService.deleteApp(id), PAY_APP_EXIST_REFUND_CANT_DELETE);
assertServiceException(() -> appService.deleteApp(id), APP_EXIST_REFUND_CANT_DELETE);
}
@Test
@@ -239,7 +239,7 @@ public class PayAppServiceTest extends BaseDbUnitTest {
@Test
public void testValidPayApp_notFound() {
assertServiceException(() -> appService.validPayApp(randomLongId()), PAY_APP_NOT_FOUND);
assertServiceException(() -> appService.validPayApp(randomLongId()), APP_NOT_FOUND);
}
@Test
@@ -252,7 +252,7 @@ public class PayAppServiceTest extends BaseDbUnitTest {
Long id = dbApp.getId();
// 调用,并断言异常
assertServiceException(() -> appService.validPayApp(id), PAY_APP_IS_DISABLE);
assertServiceException(() -> appService.validPayApp(id), APP_IS_DISABLE);
}
}

View File

@@ -26,8 +26,7 @@ import java.util.List;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.CHANNEL_EXIST_SAME_CHANNEL_ERROR;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.CHANNEL_NOT_EXISTS;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*;
import static org.junit.jupiter.api.Assertions.*;
@Import({PayChannelServiceImpl.class})
@@ -51,6 +50,40 @@ public class PayChannelServiceTest extends BaseDbUnitTest {
channelService.setChannelCache(null);
}
@Test
public void testInitLocalCache() {
// mock 数据
PayChannelDO dbChannel = randomPojo(PayChannelDO.class,
o -> o.setConfig(randomWxPayClientConfig()));
channelMapper.insert(dbChannel);// @Sql: 先插入出一条存在的数据
// 调用
channelService.initLocalCache();
// 校验缓存
assertEquals(1, channelService.getChannelCache().size());
assertEquals(dbChannel, channelService.getChannelCache().get(0));
}
@Test
public void testRefreshLocalCache() {
// mock 数据 01
PayChannelDO dbChannel = randomPojo(PayChannelDO.class,
o -> o.setConfig(randomWxPayClientConfig()));
channelMapper.insert(dbChannel);// @Sql: 先插入出一条存在的数据
channelService.initLocalCache();
// mock 数据 02
PayChannelDO dbChannel02 = randomPojo(PayChannelDO.class,
o -> o.setConfig(randomWxPayClientConfig()));
channelMapper.insert(dbChannel02);// @Sql: 先插入出一条存在的数据
// 调用
channelService.refreshLocalCache();
// 校验缓存
assertEquals(2, channelService.getChannelCache().size());
assertEquals(dbChannel, channelService.getChannelCache().get(0));
assertEquals(dbChannel02, channelService.getChannelCache().get(1));
}
@Test
public void testCreateChannel_success() {
// 准备参数
@@ -125,7 +158,7 @@ public class PayChannelServiceTest extends BaseDbUnitTest {
});
// 调用, 并断言异常
assertServiceException(() -> channelService.updateChannel(reqVO), CHANNEL_NOT_EXISTS);
assertServiceException(() -> channelService.updateChannel(reqVO), CHANNEL_NOT_FOUND);
}
@Test
@@ -153,7 +186,7 @@ public class PayChannelServiceTest extends BaseDbUnitTest {
Long id = randomLongId();
// 调用, 并断言异常
assertServiceException(() -> channelService.deleteChannel(id), CHANNEL_NOT_EXISTS);
assertServiceException(() -> channelService.deleteChannel(id), CHANNEL_NOT_FOUND);
}
@Test
@@ -214,6 +247,101 @@ public class PayChannelServiceTest extends BaseDbUnitTest {
assertPojoEquals(channel, dbChannel);
}
@Test
public void testValidPayChannel_notExists() {
// 准备参数
Long id = randomLongId();
// 调用, 并断言异常
assertServiceException(() -> channelService.validPayChannel(id), CHANNEL_NOT_FOUND);
}
@Test
public void testValidPayChannel_isDisable() {
// mock 数据
PayChannelDO dbChannel = randomPojo(PayChannelDO.class, o -> {
o.setCode(PayChannelEnum.ALIPAY_APP.getCode());
o.setConfig(randomAlipayPayClientConfig());
o.setStatus(CommonStatusEnum.DISABLE.getStatus());
});
channelMapper.insert(dbChannel);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbChannel.getId();
// 调用, 并断言异常
assertServiceException(() -> channelService.validPayChannel(id), CHANNEL_IS_DISABLE);
}
@Test
public void testValidPayChannel_success() {
// mock 数据
PayChannelDO dbChannel = randomPojo(PayChannelDO.class, o -> {
o.setCode(PayChannelEnum.ALIPAY_APP.getCode());
o.setConfig(randomAlipayPayClientConfig());
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
});
channelMapper.insert(dbChannel);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbChannel.getId();
// 调用
PayChannelDO channel = channelService.validPayChannel(id);
// 断言异常
assertPojoEquals(channel, dbChannel);
}
@Test
public void testValidPayChannel_appIdAndCode() {
// mock 数据
PayChannelDO dbChannel = randomPojo(PayChannelDO.class, o -> {
o.setCode(PayChannelEnum.ALIPAY_APP.getCode());
o.setConfig(randomAlipayPayClientConfig());
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
});
channelMapper.insert(dbChannel);// @Sql: 先插入出一条存在的数据
// 准备参数
Long appId = dbChannel.getAppId();
String code = dbChannel.getCode();
// 调用
PayChannelDO channel = channelService.validPayChannel(appId, code);
// 断言异常
assertPojoEquals(channel, dbChannel);
}
@Test
public void testGetEnableChannelList() {
// 准备参数
Long appId = randomLongId();
// mock 数据 01enable 不匹配)
PayChannelDO dbChannel01 = randomPojo(PayChannelDO.class, o -> {
o.setCode(PayChannelEnum.ALIPAY_APP.getCode());
o.setConfig(randomAlipayPayClientConfig());
o.setStatus(CommonStatusEnum.DISABLE.getStatus());
});
channelMapper.insert(dbChannel01);// @Sql: 先插入出一条存在的数据
// mock 数据 02appId 不匹配)
PayChannelDO dbChannel02 = randomPojo(PayChannelDO.class, o -> {
o.setCode(PayChannelEnum.ALIPAY_APP.getCode());
o.setConfig(randomAlipayPayClientConfig());
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
});
channelMapper.insert(dbChannel02);// @Sql: 先插入出一条存在的数据
// mock 数据 03
PayChannelDO dbChannel03 = randomPojo(PayChannelDO.class, o -> {
o.setCode(PayChannelEnum.ALIPAY_APP.getCode());
o.setConfig(randomAlipayPayClientConfig());
o.setAppId(appId);
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
});
channelMapper.insert(dbChannel03);// @Sql: 先插入出一条存在的数据
// 调用
List<PayChannelDO> channel = channelService.getEnableChannelList(appId);
// 断言异常
assertPojoEquals(channel, dbChannel03);
}
public WxPayClientConfig randomWxPayClientConfig() {
return new WxPayClientConfig()
.setAppId(randomString())

View File

@@ -3,32 +3,53 @@ package cn.iocoder.yudao.module.pay.service.order;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.yudao.framework.pay.config.PayProperties;
import cn.iocoder.yudao.framework.pay.core.client.PayClient;
import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory;
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO;
import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.framework.test.core.util.AssertUtils;
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderCreateReqDTO;
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.PayOrderExportReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.PayOrderPageReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.PayOrderSubmitReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.app.PayAppDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.channel.PayChannelDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderExtensionDO;import cn.iocoder.yudao.module.pay.dal.mysql.order.PayOrderExtensionMapper;
import cn.iocoder.yudao.module.pay.dal.mysql.order.PayOrderMapper;
import cn.iocoder.yudao.module.pay.enums.order.PayOrderNotifyStatusEnum;
import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum;
import cn.iocoder.yudao.module.pay.enums.order.PayOrderRefundStatusEnum;
import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum;
import cn.iocoder.yudao.module.pay.service.app.PayAppService;
import cn.iocoder.yudao.module.pay.service.channel.PayChannelService;
import cn.iocoder.yudao.module.pay.service.notify.PayNotifyService;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.*;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.equalsAny;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* {@link PayOrderServiceImpl} 的单元测试类
@@ -43,6 +64,8 @@ public class PayOrderServiceTest extends BaseDbUnitTest {
@Resource
private PayOrderMapper orderMapper;
@Resource
private PayOrderExtensionMapper orderExtensionMapper;
@MockBean
private PayClientFactory payClientFactory;
@@ -55,42 +78,18 @@ public class PayOrderServiceTest extends BaseDbUnitTest {
@MockBean
private PayNotifyService notifyService;
public String generateNo() {
return DateUtil.format(LocalDateTime.now(), "yyyyMMddHHmmss") + RandomUtil.randomInt(100000, 999999);
}
@Test
public void testGetOrderPage() {
String merchantOrderId = generateNo();
String channelOrderId = generateNo();
// mock 数据
PayOrderDO dbOrder = randomPojo(PayOrderDO.class, o -> { // 等会查询到
o.setAppId(1L);
o.setChannelId(1L);
o.setChannelId(10L);
o.setChannelCode(PayChannelEnum.WX_PUB.getCode());
o.setMerchantOrderId(merchantOrderId);
o.setSubject("灿灿子的炸弹猫");
o.setBody("斌斌子送给灿灿子的炸弹猫");
o.setNotifyUrl("https://hc.com/lbh");
o.setMerchantOrderId("110");
o.setNotifyStatus(PayOrderNotifyStatusEnum.SUCCESS.getStatus());
o.setPrice(10000);
o.setChannelFeeRate(0.01);
o.setChannelFeePrice(1L);
o.setStatus(PayOrderStatusEnum.SUCCESS.getStatus());
o.setUserIp("127.0.0.1");
o.setCreateTime(LocalDateTime.of(2018, 1, 1, 10, 1, 0));
o.setExpireTime(LocalDateTime.of(2018, 1, 1, 10, 30, 0));
o.setSuccessTime(LocalDateTime.of(2018, 1, 1, 10, 10, 2));
o.setNotifyTime(LocalDateTime.of(2018, 1, 1, 10, 10, 15));
o.setSuccessExtensionId(1L);
o.setCreateTime(buildTime(2018, 1, 15));
o.setRefundStatus(PayOrderRefundStatusEnum.NO.getStatus());
o.setRefundTimes(0);
o.setRefundPrice(0);
o.setChannelUserId("1008611");
o.setChannelOrderNo(channelOrderId);
o.setUpdateTime(LocalDateTime.of(2018, 1, 1, 10, 10, 15));
});
orderMapper.insert(dbOrder);
// 测试 appId 不匹配
@@ -100,7 +99,7 @@ public class PayOrderServiceTest extends BaseDbUnitTest {
// 测试 channelCode 不匹配
orderMapper.insert(cloneIgnoreId(dbOrder, o -> o.setChannelCode(PayChannelEnum.ALIPAY_APP.getCode())));
// 测试 merchantOrderId 不匹配
orderMapper.insert(cloneIgnoreId(dbOrder, o -> o.setMerchantOrderId(generateNo())));
orderMapper.insert(cloneIgnoreId(dbOrder, o -> o.setMerchantOrderId(randomString())));
// 测试 notifyStatus 不匹配
orderMapper.insert(cloneIgnoreId(dbOrder, o -> o.setNotifyStatus(PayOrderNotifyStatusEnum.FAILURE.getStatus())));
// 测试 status 不匹配
@@ -108,58 +107,38 @@ public class PayOrderServiceTest extends BaseDbUnitTest {
// 测试 refundStatus 不匹配
orderMapper.insert(cloneIgnoreId(dbOrder, o -> o.setRefundStatus(PayOrderRefundStatusEnum.ALL.getStatus())));
// 测试 createTime 不匹配
orderMapper.insert(cloneIgnoreId(dbOrder, o -> o.setCreateTime(LocalDateTime.of(2019, 1, 1, 10, 10,
1))));
orderMapper.insert(cloneIgnoreId(dbOrder, o -> o.setCreateTime(buildTime(2019, 1, 1))));
// 准备参数
PayOrderPageReqVO reqVO = new PayOrderPageReqVO();
reqVO.setAppId(1L);
reqVO.setChannelId(1L);
reqVO.setChannelId(10L);
reqVO.setChannelCode(PayChannelEnum.WX_PUB.getCode());
reqVO.setMerchantOrderId(merchantOrderId);
reqVO.setMerchantOrderId("110");
reqVO.setNotifyStatus(PayOrderNotifyStatusEnum.SUCCESS.getStatus());
reqVO.setStatus(PayOrderStatusEnum.SUCCESS.getStatus());
reqVO.setRefundStatus(PayOrderRefundStatusEnum.NO.getStatus());
reqVO.setCreateTime((new LocalDateTime[]{LocalDateTime.of(2018, 1, 1, 10, 1, 0), LocalDateTime.of(2018, 1, 1, 10, 1, 0)}));
reqVO.setCreateTime(buildBetweenTime(2018, 1, 10, 2018, 1, 30));
// 调用
PageResult<PayOrderDO> pageResult = orderService.getOrderPage(reqVO);
// 断言
assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size());
assertPojoEquals(dbOrder, pageResult.getList().get(0));
// assertEquals(0, dbOrder.getUpdateTime().compareTo(pageResult.getList().get(0).getUpdateTime()));
}
@Test
public void testGetOrderList() {
// mock 数据
String merchantOrderId = generateNo();
String channelOrderId = generateNo();
PayOrderDO dbOrder = randomPojo(PayOrderDO.class, o -> { // 等会查询到
o.setAppId(1L);
o.setChannelId(1L);
o.setChannelId(10L);
o.setChannelCode(PayChannelEnum.WX_PUB.getCode());
o.setMerchantOrderId(merchantOrderId);
o.setSubject("灿灿子的炸弹猫");
o.setBody("斌斌子送给灿灿子的炸弹猫");
o.setNotifyUrl("https://hc.com/lbh");
o.setMerchantOrderId("110");
o.setNotifyStatus(PayOrderNotifyStatusEnum.SUCCESS.getStatus());
o.setPrice(10000);
o.setChannelFeeRate(0.01);
o.setChannelFeePrice(1L);
o.setStatus(PayOrderStatusEnum.SUCCESS.getStatus());
o.setUserIp("127.0.0.1");
o.setCreateTime(LocalDateTime.of(2018, 1, 1, 10, 1, 0));
o.setExpireTime(LocalDateTime.of(2018, 1, 1, 10, 30, 0));
o.setSuccessTime(LocalDateTime.of(2018, 1, 1, 10, 10, 2));
o.setNotifyTime(LocalDateTime.of(2018, 1, 1, 10, 10, 15));
o.setSuccessExtensionId(1L);
o.setCreateTime(buildTime(2018, 1, 15));
o.setRefundStatus(PayOrderRefundStatusEnum.NO.getStatus());
o.setRefundTimes(0);
o.setRefundPrice(0);
o.setChannelUserId("1008611");
o.setChannelOrderNo(channelOrderId);
o.setUpdateTime(LocalDateTime.of(2018, 1, 1, 10, 10, 15));
});
orderMapper.insert(dbOrder);
// 测试 appId 不匹配
@@ -169,7 +148,7 @@ public class PayOrderServiceTest extends BaseDbUnitTest {
// 测试 channelCode 不匹配
orderMapper.insert(cloneIgnoreId(dbOrder, o -> o.setChannelCode(PayChannelEnum.ALIPAY_APP.getCode())));
// 测试 merchantOrderId 不匹配
orderMapper.insert(cloneIgnoreId(dbOrder, o -> o.setMerchantOrderId(generateNo())));
orderMapper.insert(cloneIgnoreId(dbOrder, o -> o.setMerchantOrderId(randomString())));
// 测试 notifyStatus 不匹配
orderMapper.insert(cloneIgnoreId(dbOrder, o -> o.setNotifyStatus(PayOrderNotifyStatusEnum.FAILURE.getStatus())));
// 测试 status 不匹配
@@ -177,18 +156,17 @@ public class PayOrderServiceTest extends BaseDbUnitTest {
// 测试 refundStatus 不匹配
orderMapper.insert(cloneIgnoreId(dbOrder, o -> o.setRefundStatus(PayOrderRefundStatusEnum.ALL.getStatus())));
// 测试 createTime 不匹配
orderMapper.insert(cloneIgnoreId(dbOrder, o -> o.setCreateTime(LocalDateTime.of(2019, 1, 1, 10, 10,
1))));
orderMapper.insert(cloneIgnoreId(dbOrder, o -> o.setCreateTime(buildTime(2019, 1, 1))));
// 准备参数
PayOrderExportReqVO reqVO = new PayOrderExportReqVO();
reqVO.setAppId(1L);
reqVO.setChannelId(1L);
reqVO.setChannelId(10L);
reqVO.setChannelCode(PayChannelEnum.WX_PUB.getCode());
reqVO.setMerchantOrderId(merchantOrderId);
reqVO.setMerchantOrderId("110");
reqVO.setNotifyStatus(PayOrderNotifyStatusEnum.SUCCESS.getStatus());
reqVO.setStatus(PayOrderStatusEnum.SUCCESS.getStatus());
reqVO.setRefundStatus(PayOrderRefundStatusEnum.NO.getStatus());
reqVO.setCreateTime((new LocalDateTime[]{LocalDateTime.of(2018, 1, 1, 10, 1, 0), LocalDateTime.of(2018, 1, 1, 10, 1, 0)}));
reqVO.setCreateTime(buildBetweenTime(2018, 1, 10, 2018, 1, 30));
// 调用
List<PayOrderDO> list = orderService.getOrderList(reqVO);
@@ -197,4 +175,154 @@ public class PayOrderServiceTest extends BaseDbUnitTest {
assertPojoEquals(dbOrder, list.get(0));
}
@Test
public void testCreateOrder_success() {
// mock 参数
PayOrderCreateReqDTO reqDTO = randomPojo(PayOrderCreateReqDTO.class,
o -> o.setAppId(1L).setMerchantOrderId("10")
.setSubject(randomString()).setBody(randomString()));
// mock 方法
PayAppDO app = randomPojo(PayAppDO.class, o -> o.setId(1L).setOrderNotifyUrl("http://127.0.0.1"));
when(appService.validPayApp(eq(reqDTO.getAppId()))).thenReturn(app);
// 调用
Long orderId = orderService.createOrder(reqDTO);
// 断言
PayOrderDO order = orderMapper.selectById(orderId);
assertPojoEquals(order, reqDTO);
assertEquals(order.getAppId(), 1L);
assertEquals(order.getNotifyUrl(), "http://127.0.0.1");
assertEquals(order.getStatus(), PayOrderStatusEnum.WAITING.getStatus());
assertEquals(order.getRefundStatus(), PayOrderRefundStatusEnum.NO.getStatus());
assertEquals(order.getRefundTimes(), 0);
assertEquals(order.getRefundPrice(), 0);
}
@Test
public void testCreateOrder_exists() {
// mock 参数
PayOrderCreateReqDTO reqDTO = randomPojo(PayOrderCreateReqDTO.class,
o -> o.setAppId(1L).setMerchantOrderId("10"));
// mock 数据
PayOrderDO dbOrder = randomPojo(PayOrderDO.class, o -> o.setAppId(1L).setMerchantOrderId("10"));
orderMapper.insert(dbOrder);
// 调用
Long orderId = orderService.createOrder(reqDTO);
// 断言
PayOrderDO order = orderMapper.selectById(orderId);
assertPojoEquals(dbOrder, order);
}
@Test
public void testSubmitOrder_notFound() {
// 准备参数
PayOrderSubmitReqVO reqVO = randomPojo(PayOrderSubmitReqVO.class);
String userIp = randomString();
// 调用, 并断言异常
assertServiceException(() -> orderService.submitOrder(reqVO, userIp), ORDER_NOT_FOUND);
}
@Test
public void testSubmitOrder_notWaiting() {
// mock 数据order
PayOrderDO order = randomPojo(PayOrderDO.class, o -> o.setStatus(PayOrderStatusEnum.SUCCESS.getStatus()));
orderMapper.insert(order);
// 准备参数
PayOrderSubmitReqVO reqVO = randomPojo(PayOrderSubmitReqVO.class, o -> o.setId(order.getId()));
String userIp = randomString();
// 调用, 并断言异常
assertServiceException(() -> orderService.submitOrder(reqVO, userIp), ORDER_STATUS_IS_NOT_WAITING);
}
@Test
public void testSubmitOrder_expired() {
// mock 数据order
PayOrderDO order = randomPojo(PayOrderDO.class, o -> o.setStatus(PayOrderStatusEnum.WAITING.getStatus())
.setExpireTime(addTime(Duration.ofDays(-1))));
orderMapper.insert(order);
// 准备参数
PayOrderSubmitReqVO reqVO = randomPojo(PayOrderSubmitReqVO.class, o -> o.setId(order.getId()));
String userIp = randomString();
// 调用, 并断言异常
assertServiceException(() -> orderService.submitOrder(reqVO, userIp), ORDER_IS_EXPIRED);
}
@Test
public void testSubmitOrder_channelNotFound() {
// mock 数据order
PayOrderDO order = randomPojo(PayOrderDO.class, o -> o.setStatus(PayOrderStatusEnum.WAITING.getStatus())
.setAppId(1L));
orderMapper.insert(order);
// 准备参数
PayOrderSubmitReqVO reqVO = randomPojo(PayOrderSubmitReqVO.class, o -> o.setId(order.getId())
.setChannelCode(PayChannelEnum.ALIPAY_APP.getCode()));
String userIp = randomString();
// mock 方法app
PayAppDO app = randomPojo(PayAppDO.class, o -> o.setId(1L));
when(appService.validPayApp(eq(1L))).thenReturn(app);
// mock 方法channel
PayChannelDO channel = randomPojo(PayChannelDO.class, o -> o.setCode(PayChannelEnum.ALIPAY_APP.getCode()));
when(channelService.validPayChannel(eq(1L), eq(PayChannelEnum.ALIPAY_APP.getCode())))
.thenReturn(channel);
// 调用, 并断言异常
assertServiceException(() -> orderService.submitOrder(reqVO, userIp), CHANNEL_NOT_FOUND);
}
@Test // 调用 unifiedOrder 接口,返回存在渠道错误
public void testSubmitOrder_channelError() {
// mock 数据order
PayOrderDO order = randomPojo(PayOrderDO.class, o -> o.setStatus(PayOrderStatusEnum.WAITING.getStatus())
.setAppId(1L));
orderMapper.insert(order);
// 准备参数
PayOrderSubmitReqVO reqVO = randomPojo(PayOrderSubmitReqVO.class, o -> o.setId(order.getId())
.setChannelCode(PayChannelEnum.ALIPAY_APP.getCode()));
String userIp = randomString();
// mock 方法app
PayAppDO app = randomPojo(PayAppDO.class, o -> o.setId(1L));
when(appService.validPayApp(eq(1L))).thenReturn(app);
// mock 方法channel
PayChannelDO channel = randomPojo(PayChannelDO.class, o -> o.setId(10L)
.setCode(PayChannelEnum.ALIPAY_APP.getCode()));
when(channelService.validPayChannel(eq(1L), eq(PayChannelEnum.ALIPAY_APP.getCode())))
.thenReturn(channel);
// mock 方法client
PayClient client = mock(PayClient.class);
when(payClientFactory.getPayClient(eq(10L))).thenReturn(client);
when(client.unifiedOrder(any(PayOrderUnifiedReqDTO.class))).thenThrow(new NullPointerException());
// 调用,并断言异常
assertThrows(NullPointerException.class, () -> orderService.submitOrder(reqVO, userIp));
// 断言数据记录PayOrderExtensionDO
PayOrderExtensionDO orderExtension = orderExtensionMapper.selectOne(null);
assertNotNull(orderExtension);
assertThat(orderExtension).extracting("no", "orderId").isNotNull();
assertThat(orderExtension)
.extracting("channelId", "channelCode","userIp" ,"status", "channelExtras",
"channelErrorCode", "channelErrorMsg", "channelNotifyData")
.containsExactly(10L, PayChannelEnum.ALIPAY_APP.getCode(), userIp,
PayOrderStatusEnum.WAITING.getStatus(), reqVO.getChannelExtras(),
null, null, null);
}
@Test // 【成功】支付结果为等待中
public void testSubmitOrder_waitingResult() {
}
@Test // 【成功】支付结果为已完成
public void testSubmitOrder_successResult() {
}
@Test // 【成功】支付结果为已关闭
public void testSubmitOrder_closedResult() {
}
}

View File

@@ -40,9 +40,9 @@ CREATE TABLE IF NOT EXISTS `pay_order` (
`body` varchar(128) NOT NULL,
`notify_url` varchar(1024) NOT NULL,
`notify_status` tinyint(4) NOT NULL,
`amount` bigint(20) NOT NULL,
`price` bigint(20) NOT NULL,
`channel_fee_rate` double DEFAULT 0,
`channel_fee_amount` bigint(20) DEFAULT 0,
`channel_fee_price` bigint(20) DEFAULT 0,
`status` tinyint(4) NOT NULL,
`user_ip` varchar(50) NOT NULL,
`expire_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
@@ -50,8 +50,8 @@ CREATE TABLE IF NOT EXISTS `pay_order` (
`notify_time` datetime(0) DEFAULT CURRENT_TIMESTAMP,
`success_extension_id` bigint(20) DEFAULT NULL COMMENT '支付成功的订单拓展单编号',
`refund_status` tinyint(4) NOT NULL,
`refund_times` tinyint(4) NOT NULL,
`refund_amount` bigint(20) NOT NULL,
`refund_times` int NOT NULL,
`refund_price` bigint(20) NOT NULL,
`channel_user_id` varchar(255) DEFAULT NULL,
`channel_order_no` varchar(64) DEFAULT NULL,
`creator` varchar(64) DEFAULT '',
@@ -62,6 +62,26 @@ CREATE TABLE IF NOT EXISTS `pay_order` (
PRIMARY KEY ("id")
) COMMENT = '支付订单';
CREATE TABLE IF NOT EXISTS `pay_order_extension` (
"id" number NOT NULL GENERATED BY DEFAULT AS IDENTITY,
`no` varchar(64) NOT NULL,
`order_id` bigint(20) NOT NULL,
`channel_id` bigint(20) NOT NULL,
`channel_code` varchar(32) NOT NULL,
`user_ip` varchar(50) NULL DEFAULT NULL,
`status` tinyint(4) NOT NULL,
`channel_extras` varchar(1024) NULL DEFAULT NULL,
`channel_error_code` varchar(64) NULL,
`channel_error_msg` varchar(64) NULL,
`channel_notify_data` varchar(64) NULL,
`creator` varchar(64) NULL DEFAULT '',
`create_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updater` varchar(64) NULL DEFAULT '',
`update_time` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`deleted` bit(1) NOT NULL DEFAULT FALSE,
PRIMARY KEY ("id")
) COMMENT = '支付订单拓展';
CREATE TABLE IF NOT EXISTS `pay_refund` (
"id" number NOT NULL GENERATED BY DEFAULT AS IDENTITY,
`app_id` bigint(20) NOT NULL,