mall + pay:

1. 支付订单的状态同步 Job[单测]
This commit is contained in:
YunaiV 2023-07-22 17:47:58 +08:00
parent 15cca8f3de
commit cc853a2b92
2 changed files with 107 additions and 2 deletions

View File

@ -40,7 +40,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -155,7 +154,6 @@ public class PayOrderServiceImpl implements PayOrderService {
.setReturnUrl(reqVO.getReturnUrl()) .setReturnUrl(reqVO.getReturnUrl())
// 订单相关字段 // 订单相关字段
.setPrice(order.getPrice()).setExpireTime(order.getExpireTime()); .setPrice(order.getPrice()).setExpireTime(order.getExpireTime());
unifiedOrderReqDTO.setExpireTime(LocalDateTimeUtils.addTime(Duration.ofSeconds(70))); // TODO 芋艿稍后删除掉
PayOrderRespDTO unifiedOrderResp = client.unifiedOrder(unifiedOrderReqDTO); PayOrderRespDTO unifiedOrderResp = client.unifiedOrder(unifiedOrderReqDTO);
// 4. 如果调用直接支付成功则直接更新支付单状态为成功例如说付款码支付免密支付时就直接验证支付成功 // 4. 如果调用直接支付成功则直接更新支付单状态为成功例如说付款码支付免密支付时就直接验证支付成功

View File

@ -36,6 +36,7 @@ import org.springframework.context.annotation.Import;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.Duration; import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.*; import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.*;
@ -849,4 +850,110 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest {
assertPojoEquals(dbOrderExtension, orderExtension); assertPojoEquals(dbOrderExtension, orderExtension);
} }
@Test
public void testSyncOrder_payClientNotFound() {
// 准备参数
LocalDateTime minCreateTime = LocalDateTime.now().minus(Duration.ofMinutes(10));
// mock 数据PayOrderExtensionDO
PayOrderExtensionDO orderExtension = randomPojo(PayOrderExtensionDO.class,
o -> o.setStatus(PayOrderStatusEnum.WAITING.getStatus())
.setCreateTime(LocalDateTime.now()));
orderExtensionMapper.insert(orderExtension);
// 调用
int count = orderService.syncOrder(minCreateTime);
// 断言
assertEquals(count, 0);
}
@Test
public void testSyncOrder_exception() {
// 准备参数
LocalDateTime minCreateTime = LocalDateTime.now().minus(Duration.ofMinutes(10));
// mock 数据PayOrderExtensionDO
PayOrderExtensionDO orderExtension = randomPojo(PayOrderExtensionDO.class,
o -> o.setStatus(PayOrderStatusEnum.WAITING.getStatus())
.setChannelId(10L)
.setCreateTime(LocalDateTime.now()));
orderExtensionMapper.insert(orderExtension);
// mock 方法PayClient
PayClient client = mock(PayClient.class);
when(payClientFactory.getPayClient(eq(10L))).thenReturn(client);
// mock 方法PayClient 异常
when(client.getOrder(any())).thenThrow(new RuntimeException());
// 调用
int count = orderService.syncOrder(minCreateTime);
// 断言
assertEquals(count, 0);
}
@Test
public void testSyncOrder_orderSuccess() {
PayOrderServiceImpl payOrderServiceImpl = mock(PayOrderServiceImpl.class);
try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) {
springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PayOrderServiceImpl.class)))
.thenReturn(payOrderServiceImpl);
// 准备参数
LocalDateTime minCreateTime = LocalDateTime.now().minus(Duration.ofMinutes(10));
// mock 数据PayOrderExtensionDO
PayOrderExtensionDO orderExtension = randomPojo(PayOrderExtensionDO.class,
o -> o.setStatus(PayOrderStatusEnum.WAITING.getStatus())
.setChannelId(10L).setNo("P110")
.setCreateTime(LocalDateTime.now()));
orderExtensionMapper.insert(orderExtension);
// mock 方法PayClient
PayClient client = mock(PayClient.class);
when(payClientFactory.getPayClient(eq(10L))).thenReturn(client);
// mock 方法PayClient 成功返回
PayOrderRespDTO respDTO = randomPojo(PayOrderRespDTO.class,
o -> o.setStatus(PayOrderStatusEnum.SUCCESS.getStatus()));
when(client.getOrder(eq("P110"))).thenReturn(respDTO);
// mock 方法PayChannelDO
PayChannelDO channel = randomPojo(PayChannelDO.class, o -> o.setId(10L));
when(channelService.validPayChannel(eq(10L))).thenReturn(channel);
// 调用
int count = orderService.syncOrder(minCreateTime);
// 断言
assertEquals(count, 1);
verify(payOrderServiceImpl).notifyOrder(same(channel), same(respDTO));
}
}
@Test
public void testSyncOrder_orderClosed() {
PayOrderServiceImpl payOrderServiceImpl = mock(PayOrderServiceImpl.class);
try (MockedStatic<SpringUtil> springUtilMockedStatic = mockStatic(SpringUtil.class)) {
springUtilMockedStatic.when(() -> SpringUtil.getBean(eq(PayOrderServiceImpl.class)))
.thenReturn(payOrderServiceImpl);
// 准备参数
LocalDateTime minCreateTime = LocalDateTime.now().minus(Duration.ofMinutes(10));
// mock 数据PayOrderExtensionDO
PayOrderExtensionDO orderExtension = randomPojo(PayOrderExtensionDO.class,
o -> o.setStatus(PayOrderStatusEnum.WAITING.getStatus())
.setChannelId(10L).setNo("P110")
.setCreateTime(LocalDateTime.now()));
orderExtensionMapper.insert(orderExtension);
// mock 方法PayClient
PayClient client = mock(PayClient.class);
when(payClientFactory.getPayClient(eq(10L))).thenReturn(client);
// mock 方法PayClient 成功返回
PayOrderRespDTO respDTO = randomPojo(PayOrderRespDTO.class,
o -> o.setStatus(PayOrderStatusEnum.CLOSED.getStatus()));
when(client.getOrder(eq("P110"))).thenReturn(respDTO);
// mock 方法PayChannelDO
PayChannelDO channel = randomPojo(PayChannelDO.class, o -> o.setId(10L));
when(channelService.validPayChannel(eq(10L))).thenReturn(channel);
// 调用
int count = orderService.syncOrder(minCreateTime);
// 断言
assertEquals(count, 0);
verify(payOrderServiceImpl).notifyOrder(same(channel), same(respDTO));
}
}
} }