Commit c0ffaf8ea9c34c7cd9397cb945c6f5d080ef644a
1 parent
c7f0b5c7
支付宝 PC 网站 Client 单元测试
Showing
1 changed file
with
114 additions
and
0 deletions
yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayPcPayClientTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.pay.core.client.impl.alipay; | ||
| 2 | + | ||
| 3 | +import cn.hutool.http.Method; | ||
| 4 | +import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO; | ||
| 5 | +import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO; | ||
| 6 | +import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderDisplayModeEnum; | ||
| 7 | +import com.alipay.api.AlipayApiException; | ||
| 8 | +import com.alipay.api.request.AlipayTradePagePayRequest; | ||
| 9 | +import com.alipay.api.response.AlipayTradePagePayResponse; | ||
| 10 | +import org.junit.jupiter.api.BeforeEach; | ||
| 11 | +import org.junit.jupiter.api.DisplayName; | ||
| 12 | +import org.junit.jupiter.api.Test; | ||
| 13 | +import org.mockito.ArgumentMatcher; | ||
| 14 | +import org.mockito.InjectMocks; | ||
| 15 | + | ||
| 16 | +import static cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum.CLOSED; | ||
| 17 | +import static cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum.WAITING; | ||
| 18 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | ||
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 20 | +import static org.junit.jupiter.api.Assertions.assertSame; | ||
| 21 | +import static org.mockito.ArgumentMatchers.argThat; | ||
| 22 | +import static org.mockito.ArgumentMatchers.eq; | ||
| 23 | +import static org.mockito.Mockito.when; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * @author jason | ||
| 27 | + */ | ||
| 28 | +public class AlipayPcPayClientTest extends AbstractAlipayClientTest { | ||
| 29 | + | ||
| 30 | + @InjectMocks | ||
| 31 | + private AlipayPcPayClient client = new AlipayPcPayClient(randomLongId(), config); | ||
| 32 | + | ||
| 33 | + @Override | ||
| 34 | + @BeforeEach | ||
| 35 | + public void setUp() { | ||
| 36 | + setClient(client); | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + @Test | ||
| 40 | + @DisplayName("支付宝 PC 网站支付 URL Display Mode 下单成功") | ||
| 41 | + public void test_unified_order_url_display_mode_success() throws AlipayApiException { | ||
| 42 | + // 准备返回对象 | ||
| 43 | + String notifyUrl = randomURL(); | ||
| 44 | + Integer price = randomInteger(); | ||
| 45 | + AlipayTradePagePayResponse response = randomPojo(AlipayTradePagePayResponse.class, o -> { | ||
| 46 | + o.setSubCode(""); | ||
| 47 | + }); | ||
| 48 | + // mock | ||
| 49 | + when(defaultAlipayClient.pageExecute(argThat((ArgumentMatcher<AlipayTradePagePayRequest>) request -> true), | ||
| 50 | + eq(Method.GET.name()))).thenReturn(response); | ||
| 51 | + // 准备请求参数 | ||
| 52 | + String outTradeNo = randomString(); | ||
| 53 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo, price); | ||
| 54 | + // 设置 displayMode 为 null. | ||
| 55 | + reqDTO.setDisplayMode(null); | ||
| 56 | + | ||
| 57 | + PayOrderRespDTO resp = client.unifiedOrder(reqDTO); | ||
| 58 | + // 断言 | ||
| 59 | + assertEquals(WAITING.getStatus(), resp.getStatus()); | ||
| 60 | + assertEquals(PayOrderDisplayModeEnum.URL.getMode(), resp.getDisplayMode()); | ||
| 61 | + assertEquals(outTradeNo, resp.getOutTradeNo()); | ||
| 62 | + assertSame(response, resp.getRawData()); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + @Test | ||
| 66 | + @DisplayName("支付宝 PC 网站支付 FORM Display Mode 下单成功") | ||
| 67 | + public void test_unified_order_form_display_mode_success() throws AlipayApiException { | ||
| 68 | + // 准备返回对象 | ||
| 69 | + String notifyUrl = randomURL(); | ||
| 70 | + Integer price = randomInteger(); | ||
| 71 | + AlipayTradePagePayResponse response = randomPojo(AlipayTradePagePayResponse.class, o -> { | ||
| 72 | + o.setSubCode(""); | ||
| 73 | + }); | ||
| 74 | + // mock | ||
| 75 | + when(defaultAlipayClient.pageExecute(argThat((ArgumentMatcher<AlipayTradePagePayRequest>) request -> true), | ||
| 76 | + eq(Method.POST.name()))).thenReturn(response); | ||
| 77 | + // 准备请求参数 | ||
| 78 | + String outTradeNo = randomString(); | ||
| 79 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo, price); | ||
| 80 | + reqDTO.setDisplayMode(PayOrderDisplayModeEnum.FORM.getMode()); | ||
| 81 | + | ||
| 82 | + PayOrderRespDTO resp = client.unifiedOrder(reqDTO); | ||
| 83 | + // 断言 | ||
| 84 | + assertEquals(WAITING.getStatus(), resp.getStatus()); | ||
| 85 | + assertEquals(PayOrderDisplayModeEnum.FORM.getMode(), resp.getDisplayMode()); | ||
| 86 | + assertEquals(outTradeNo, resp.getOutTradeNo()); | ||
| 87 | + assertSame(response, resp.getRawData()); | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + @Test | ||
| 91 | + @DisplayName("支付宝 PC 网站支付,渠道返回失败") | ||
| 92 | + public void test_unified_order_channel_failed() throws AlipayApiException { | ||
| 93 | + // 准备响应对象 | ||
| 94 | + String subCode = randomString(); | ||
| 95 | + String subMsg = randomString(); | ||
| 96 | + AlipayTradePagePayResponse response = randomPojo(AlipayTradePagePayResponse.class, o -> { | ||
| 97 | + o.setSubCode(subCode); | ||
| 98 | + o.setSubMsg(subMsg); | ||
| 99 | + }); | ||
| 100 | + // mock | ||
| 101 | + when(defaultAlipayClient.pageExecute(argThat((ArgumentMatcher<AlipayTradePagePayRequest>) request -> true), | ||
| 102 | + eq(Method.GET.name()))).thenReturn(response); | ||
| 103 | + // 准备请求参数 | ||
| 104 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(randomURL(), randomString(), randomInteger()); | ||
| 105 | + reqDTO.setDisplayMode(PayOrderDisplayModeEnum.URL.getMode()); | ||
| 106 | + | ||
| 107 | + PayOrderRespDTO resp = client.unifiedOrder(reqDTO); | ||
| 108 | + // 断言 | ||
| 109 | + assertEquals(CLOSED.getStatus(), resp.getStatus()); | ||
| 110 | + assertEquals(subCode, resp.getChannelErrorCode()); | ||
| 111 | + assertEquals(subMsg, resp.getChannelErrorMsg()); | ||
| 112 | + assertSame(response, resp.getRawData()); | ||
| 113 | + } | ||
| 114 | +} |