Commit b119aadba5de1452edc8c7110d24f0ca7c898f41
1 parent
60875a04
支付宝 条码支付 Client 单元测试
Showing
2 changed files
with
163 additions
and
8 deletions
yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayBarPayClientTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.pay.core.client.impl.alipay; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.date.LocalDateTimeUtil; | ||
| 4 | +import cn.iocoder.yudao.framework.common.exception.ServiceException; | ||
| 5 | +import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO; | ||
| 6 | +import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO; | ||
| 7 | +import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderDisplayModeEnum; | ||
| 8 | +import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum; | ||
| 9 | +import com.alipay.api.AlipayApiException; | ||
| 10 | +import com.alipay.api.domain.AlipayTradePayModel; | ||
| 11 | +import com.alipay.api.request.AlipayTradePayRequest; | ||
| 12 | +import com.alipay.api.response.AlipayTradePayResponse; | ||
| 13 | +import org.junit.jupiter.api.BeforeEach; | ||
| 14 | +import org.junit.jupiter.api.DisplayName; | ||
| 15 | +import org.junit.jupiter.api.Test; | ||
| 16 | +import org.mockito.ArgumentMatcher; | ||
| 17 | +import org.mockito.InjectMocks; | ||
| 18 | + | ||
| 19 | +import java.util.Date; | ||
| 20 | +import java.util.HashMap; | ||
| 21 | +import java.util.Map; | ||
| 22 | + | ||
| 23 | +import static cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum.CLOSED; | ||
| 24 | +import static cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum.WAITING; | ||
| 25 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | ||
| 26 | +import static org.junit.jupiter.api.Assertions.*; | ||
| 27 | +import static org.mockito.ArgumentMatchers.argThat; | ||
| 28 | +import static org.mockito.Mockito.when; | ||
| 29 | + | ||
| 30 | +/** | ||
| 31 | + * {@link AlipayBarPayClient} 单元测试 | ||
| 32 | + * | ||
| 33 | + * @author jason | ||
| 34 | + */ | ||
| 35 | +public class AlipayBarPayClientTest extends AbstractAlipayClientTest { | ||
| 36 | + | ||
| 37 | + @InjectMocks | ||
| 38 | + private AlipayBarPayClient client = new AlipayBarPayClient(randomLongId(), config); | ||
| 39 | + | ||
| 40 | + @Override | ||
| 41 | + @BeforeEach | ||
| 42 | + public void setUp() { | ||
| 43 | + setClient(client); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + @Test | ||
| 47 | + @DisplayName("支付宝条码支付,非免密码支付下单成功") | ||
| 48 | + public void test_unified_order_success() throws AlipayApiException { | ||
| 49 | + String outTradeNo = randomString(); | ||
| 50 | + String notifyUrl = randomURL(); | ||
| 51 | + Integer price = randomInteger(); | ||
| 52 | + String authCode = randomString(); | ||
| 53 | + // 准备返回对象 | ||
| 54 | + AlipayTradePayResponse response = randomPojo(AlipayTradePayResponse.class, o -> o.setSubCode("")); | ||
| 55 | + | ||
| 56 | + // mock | ||
| 57 | + when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePayRequest>) request -> { | ||
| 58 | + assertInstanceOf(AlipayTradePayModel.class, request.getBizModel()); | ||
| 59 | + assertEquals(notifyUrl, request.getNotifyUrl()); | ||
| 60 | + AlipayTradePayModel model = (AlipayTradePayModel) request.getBizModel(); | ||
| 61 | + assertEquals(outTradeNo, model.getOutTradeNo()); | ||
| 62 | + assertEquals(String.valueOf(price / 100.0), model.getTotalAmount()); | ||
| 63 | + assertEquals(authCode, model.getAuthCode()); | ||
| 64 | + return true; | ||
| 65 | + }))).thenReturn(response); | ||
| 66 | + // 准备请求参数 | ||
| 67 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo, price); | ||
| 68 | + // 设置条码 | ||
| 69 | + Map<String, String> extraParam = new HashMap<>(); | ||
| 70 | + extraParam.put("auth_code", authCode); | ||
| 71 | + reqDTO.setChannelExtras(extraParam); | ||
| 72 | + // 下单请求 | ||
| 73 | + PayOrderRespDTO resp = client.unifiedOrder(reqDTO); | ||
| 74 | + // 断言 | ||
| 75 | + assertEquals(WAITING.getStatus(), resp.getStatus()); | ||
| 76 | + assertEquals(PayOrderDisplayModeEnum.BAR_CODE.getMode(), resp.getDisplayMode()); | ||
| 77 | + assertEquals(outTradeNo, resp.getOutTradeNo()); | ||
| 78 | + assertEquals("", resp.getDisplayContent()); | ||
| 79 | + assertSame(response, resp.getRawData()); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + @Test | ||
| 83 | + @DisplayName("支付宝条码支付,免密码支付下单成功") | ||
| 84 | + public void test_unified_order_code_10000_success() throws AlipayApiException { | ||
| 85 | + String outTradeNo = randomString(); | ||
| 86 | + String channelNo = randomString(); | ||
| 87 | + String channelUserId = randomString(); | ||
| 88 | + Date payTime = randomDate(); | ||
| 89 | + // 准备返回对象 | ||
| 90 | + AlipayTradePayResponse response = randomPojo(AlipayTradePayResponse.class, o -> { | ||
| 91 | + o.setSubCode(""); | ||
| 92 | + o.setCode("10000"); | ||
| 93 | + o.setOutTradeNo(outTradeNo); | ||
| 94 | + o.setTradeNo(channelNo); | ||
| 95 | + o.setBuyerUserId(channelUserId); | ||
| 96 | + o.setGmtPayment(payTime); | ||
| 97 | + }); | ||
| 98 | + // mock | ||
| 99 | + when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePayRequest>) request -> true))) | ||
| 100 | + .thenReturn(response); | ||
| 101 | + // 准备请求参数 | ||
| 102 | + String authCode = randomString(); | ||
| 103 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(randomURL(), outTradeNo, randomInteger()); | ||
| 104 | + // 设置条码 | ||
| 105 | + Map<String, String> extraParam = new HashMap<>(); | ||
| 106 | + extraParam.put("auth_code", authCode); | ||
| 107 | + reqDTO.setChannelExtras(extraParam); | ||
| 108 | + // 下单请求 | ||
| 109 | + PayOrderRespDTO resp = client.unifiedOrder(reqDTO); | ||
| 110 | + // 断言 | ||
| 111 | + assertEquals(PayOrderStatusRespEnum.SUCCESS.getStatus(), resp.getStatus()); | ||
| 112 | + assertEquals(outTradeNo, resp.getOutTradeNo()); | ||
| 113 | + assertEquals(channelNo, resp.getChannelOrderNo()); | ||
| 114 | + assertEquals(channelUserId, resp.getChannelUserId()); | ||
| 115 | + assertEquals(LocalDateTimeUtil.of(payTime), resp.getSuccessTime()); | ||
| 116 | + assertSame(response, resp.getRawData()); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + @Test | ||
| 120 | + @DisplayName("支付宝条码支付,没有传条码") | ||
| 121 | + public void test_unified_order_empty_auth_code() { | ||
| 122 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(randomURL(), randomString(), randomInteger()); | ||
| 123 | + // 断言 | ||
| 124 | + assertThrows(ServiceException.class, () -> client.unifiedOrder(reqDTO)); | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + @Test | ||
| 128 | + @DisplayName("支付宝条码支付,渠道返回失败") | ||
| 129 | + public void test_unified_order_channel_failed() throws AlipayApiException { | ||
| 130 | + // 准备响应对象 | ||
| 131 | + String subCode = randomString(); | ||
| 132 | + String subMsg = randomString(); | ||
| 133 | + AlipayTradePayResponse response = randomPojo(AlipayTradePayResponse.class, o -> { | ||
| 134 | + o.setSubCode(subCode); | ||
| 135 | + o.setSubMsg(subMsg); | ||
| 136 | + }); | ||
| 137 | + // mock | ||
| 138 | + when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePayRequest>) request -> true))) | ||
| 139 | + .thenReturn(response); | ||
| 140 | + // 准备请求参数 | ||
| 141 | + String authCode = randomString(); | ||
| 142 | + String outTradeNo = randomString(); | ||
| 143 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(randomURL(), outTradeNo, randomInteger()); | ||
| 144 | + // 设置条码 | ||
| 145 | + Map<String, String> extraParam = new HashMap<>(); | ||
| 146 | + extraParam.put("auth_code", authCode); | ||
| 147 | + reqDTO.setChannelExtras(extraParam); | ||
| 148 | + // 下单请求 | ||
| 149 | + PayOrderRespDTO resp = client.unifiedOrder(reqDTO); | ||
| 150 | + // 断言 | ||
| 151 | + assertEquals(CLOSED.getStatus(), resp.getStatus()); | ||
| 152 | + assertEquals(subCode, resp.getChannelErrorCode()); | ||
| 153 | + assertEquals(subMsg, resp.getChannelErrorMsg()); | ||
| 154 | + assertSame(response, resp.getRawData()); | ||
| 155 | + | ||
| 156 | + } | ||
| 157 | +} |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayPcPayClientTest.java
| @@ -23,6 +23,8 @@ import static org.mockito.ArgumentMatchers.eq; | @@ -23,6 +23,8 @@ import static org.mockito.ArgumentMatchers.eq; | ||
| 23 | import static org.mockito.Mockito.when; | 23 | import static org.mockito.Mockito.when; |
| 24 | 24 | ||
| 25 | /** | 25 | /** |
| 26 | + * {@link AlipayPcPayClient} 单元测试 | ||
| 27 | + * | ||
| 26 | * @author jason | 28 | * @author jason |
| 27 | */ | 29 | */ |
| 28 | public class AlipayPcPayClientTest extends AbstractAlipayClientTest { | 30 | public class AlipayPcPayClientTest extends AbstractAlipayClientTest { |
| @@ -41,15 +43,13 @@ public class AlipayPcPayClientTest extends AbstractAlipayClientTest { | @@ -41,15 +43,13 @@ public class AlipayPcPayClientTest extends AbstractAlipayClientTest { | ||
| 41 | public void test_unified_order_url_display_mode_success() throws AlipayApiException { | 43 | public void test_unified_order_url_display_mode_success() throws AlipayApiException { |
| 42 | // 准备返回对象 | 44 | // 准备返回对象 |
| 43 | String notifyUrl = randomURL(); | 45 | String notifyUrl = randomURL(); |
| 44 | - Integer price = randomInteger(); | ||
| 45 | - AlipayTradePagePayResponse response = randomPojo(AlipayTradePagePayResponse.class, o -> { | ||
| 46 | - o.setSubCode(""); | ||
| 47 | - }); | 46 | + AlipayTradePagePayResponse response = randomPojo(AlipayTradePagePayResponse.class, o -> o.setSubCode("")); |
| 48 | // mock | 47 | // mock |
| 49 | when(defaultAlipayClient.pageExecute(argThat((ArgumentMatcher<AlipayTradePagePayRequest>) request -> true), | 48 | when(defaultAlipayClient.pageExecute(argThat((ArgumentMatcher<AlipayTradePagePayRequest>) request -> true), |
| 50 | eq(Method.GET.name()))).thenReturn(response); | 49 | eq(Method.GET.name()))).thenReturn(response); |
| 51 | // 准备请求参数 | 50 | // 准备请求参数 |
| 52 | String outTradeNo = randomString(); | 51 | String outTradeNo = randomString(); |
| 52 | + Integer price = randomInteger(); | ||
| 53 | PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo, price); | 53 | PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo, price); |
| 54 | // 设置 displayMode 为 null. | 54 | // 设置 displayMode 为 null. |
| 55 | reqDTO.setDisplayMode(null); | 55 | reqDTO.setDisplayMode(null); |
| @@ -67,15 +67,13 @@ public class AlipayPcPayClientTest extends AbstractAlipayClientTest { | @@ -67,15 +67,13 @@ public class AlipayPcPayClientTest extends AbstractAlipayClientTest { | ||
| 67 | public void test_unified_order_form_display_mode_success() throws AlipayApiException { | 67 | public void test_unified_order_form_display_mode_success() throws AlipayApiException { |
| 68 | // 准备返回对象 | 68 | // 准备返回对象 |
| 69 | String notifyUrl = randomURL(); | 69 | String notifyUrl = randomURL(); |
| 70 | - Integer price = randomInteger(); | ||
| 71 | - AlipayTradePagePayResponse response = randomPojo(AlipayTradePagePayResponse.class, o -> { | ||
| 72 | - o.setSubCode(""); | ||
| 73 | - }); | 70 | + AlipayTradePagePayResponse response = randomPojo(AlipayTradePagePayResponse.class, o -> o.setSubCode("")); |
| 74 | // mock | 71 | // mock |
| 75 | when(defaultAlipayClient.pageExecute(argThat((ArgumentMatcher<AlipayTradePagePayRequest>) request -> true), | 72 | when(defaultAlipayClient.pageExecute(argThat((ArgumentMatcher<AlipayTradePagePayRequest>) request -> true), |
| 76 | eq(Method.POST.name()))).thenReturn(response); | 73 | eq(Method.POST.name()))).thenReturn(response); |
| 77 | // 准备请求参数 | 74 | // 准备请求参数 |
| 78 | String outTradeNo = randomString(); | 75 | String outTradeNo = randomString(); |
| 76 | + Integer price = randomInteger(); | ||
| 79 | PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo, price); | 77 | PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo, price); |
| 80 | reqDTO.setDisplayMode(PayOrderDisplayModeEnum.FORM.getMode()); | 78 | reqDTO.setDisplayMode(PayOrderDisplayModeEnum.FORM.getMode()); |
| 81 | 79 |