mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-30 03:01:53 +08:00
mall + pay:
1. 支付订单的状态同步 Job[单测]
This commit is contained in:
parent
15cca8f3de
commit
cc853a2b92
@ -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. 如果调用直接支付成功,则直接更新支付单状态为成功。例如说:付款码支付,免密支付时,就直接验证支付成功
|
||||||
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user