Commit c7f0b5c73dd2bbd7daf74fa0e0ea69a972dc9868
1 parent
4c634245
支付宝 H5 Client 单元测试
Showing
3 changed files
with
141 additions
and
34 deletions
yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AbstractAlipayClientTest.java
| 1 | 1 | package cn.iocoder.yudao.framework.pay.core.client.impl.alipay; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.date.LocalDateTimeUtil; |
| 4 | +import cn.hutool.core.util.RandomUtil; | |
| 4 | 5 | import cn.hutool.core.util.ReflectUtil; |
| 5 | 6 | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| 6 | 7 | import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; |
| 7 | 8 | import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; |
| 9 | +import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO; | |
| 8 | 10 | import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO; |
| 9 | 11 | import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO; |
| 10 | 12 | import cn.iocoder.yudao.framework.pay.core.client.exception.PayException; |
| ... | ... | @@ -16,6 +18,7 @@ import com.alipay.api.DefaultSigner; |
| 16 | 18 | import com.alipay.api.domain.AlipayTradeRefundModel; |
| 17 | 19 | import com.alipay.api.request.AlipayTradeRefundRequest; |
| 18 | 20 | import com.alipay.api.response.AlipayTradeRefundResponse; |
| 21 | +import org.junit.jupiter.api.BeforeEach; | |
| 19 | 22 | import org.junit.jupiter.api.DisplayName; |
| 20 | 23 | import org.junit.jupiter.api.Test; |
| 21 | 24 | import org.mockito.ArgumentMatcher; |
| ... | ... | @@ -52,6 +55,12 @@ public abstract class AbstractAlipayClientTest extends BaseMockitoUnitTest { |
| 52 | 55 | |
| 53 | 56 | private AbstractAlipayPayClient client; |
| 54 | 57 | |
| 58 | + /** | |
| 59 | + * 子类需要实现该方法. 设置 client 的具体实现 | |
| 60 | + */ | |
| 61 | + @BeforeEach | |
| 62 | + public abstract void setUp(); | |
| 63 | + | |
| 55 | 64 | public void setClient(AbstractAlipayPayClient client) { |
| 56 | 65 | this.client = client; |
| 57 | 66 | } |
| ... | ... | @@ -177,4 +186,28 @@ public abstract class AbstractAlipayClientTest extends BaseMockitoUnitTest { |
| 177 | 186 | // 断言 |
| 178 | 187 | assertThrows(PayException.class, () -> client.unifiedRefund(refundReqDTO)); |
| 179 | 188 | } |
| 189 | + | |
| 190 | + @Test | |
| 191 | + @DisplayName("支付宝 Client 统一下单, 参数校验不通过") | |
| 192 | + public void test_unified_order_param_validate() { | |
| 193 | + // 准备请求参数 | |
| 194 | + String outTradeNo = randomString(); | |
| 195 | + String notifyUrl = randomURL(); | |
| 196 | + PayOrderUnifiedReqDTO reqDTO = randomPojo(PayOrderUnifiedReqDTO.class, o -> { | |
| 197 | + o.setOutTradeNo(outTradeNo); | |
| 198 | + o.setNotifyUrl(notifyUrl); | |
| 199 | + }); | |
| 200 | + // 断言 | |
| 201 | + assertThrows(ConstraintViolationException.class, () -> client.unifiedOrder(reqDTO)); | |
| 202 | + } | |
| 203 | + | |
| 204 | + protected PayOrderUnifiedReqDTO buildOrderUnifiedReqDTO(String notifyUrl, String outTradeNo, Integer price) { | |
| 205 | + return randomPojo(PayOrderUnifiedReqDTO.class, o -> { | |
| 206 | + o.setOutTradeNo(outTradeNo); | |
| 207 | + o.setNotifyUrl(notifyUrl); | |
| 208 | + o.setPrice(price); | |
| 209 | + o.setSubject(RandomUtil.randomString(32)); | |
| 210 | + o.setBody(RandomUtil.randomString(32)); | |
| 211 | + }); | |
| 212 | + } | |
| 180 | 213 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayQrPayClientTest.java
| 1 | 1 | package cn.iocoder.yudao.framework.pay.core.client.impl.alipay; |
| 2 | 2 | |
| 3 | -import cn.hutool.core.util.RandomUtil; | |
| 4 | 3 | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| 5 | 4 | import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; |
| 6 | 5 | import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; |
| ... | ... | @@ -17,8 +16,6 @@ import org.junit.jupiter.api.Test; |
| 17 | 16 | import org.mockito.ArgumentMatcher; |
| 18 | 17 | import org.mockito.InjectMocks; |
| 19 | 18 | |
| 20 | -import javax.validation.ConstraintViolationException; | |
| 21 | - | |
| 22 | 19 | import static cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum.CLOSED; |
| 23 | 20 | import static cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum.WAITING; |
| 24 | 21 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| ... | ... | @@ -42,11 +39,12 @@ public class AlipayQrPayClientTest extends AbstractAlipayClientTest { |
| 42 | 39 | } |
| 43 | 40 | |
| 44 | 41 | @Test |
| 45 | - @DisplayName("支付包扫描支付下单成功") | |
| 42 | + @DisplayName("支付宝扫描支付下单成功") | |
| 46 | 43 | public void test_unified_order_success() throws AlipayApiException { |
| 47 | 44 | // 准备返回对象 |
| 48 | 45 | String notifyUrl = randomURL(); |
| 49 | 46 | String qrCode = randomString(); |
| 47 | + Integer price = randomInteger(); | |
| 50 | 48 | AlipayTradePrecreateResponse response = randomPojo(AlipayTradePrecreateResponse.class, o -> { |
| 51 | 49 | o.setQrCode(qrCode); |
| 52 | 50 | o.setSubCode(""); |
| ... | ... | @@ -58,7 +56,7 @@ public class AlipayQrPayClientTest extends AbstractAlipayClientTest { |
| 58 | 56 | }))).thenReturn(response); |
| 59 | 57 | // 准备请求参数 |
| 60 | 58 | String outTradeNo = randomString(); |
| 61 | - PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo); | |
| 59 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo, price); | |
| 62 | 60 | |
| 63 | 61 | PayOrderRespDTO resp = client.unifiedOrder(reqDTO); |
| 64 | 62 | // 断言 |
| ... | ... | @@ -70,11 +68,12 @@ public class AlipayQrPayClientTest extends AbstractAlipayClientTest { |
| 70 | 68 | } |
| 71 | 69 | |
| 72 | 70 | @Test |
| 73 | - @DisplayName("支付包扫描支付,渠道返回失败") | |
| 71 | + @DisplayName("支付宝扫描支付,渠道返回失败") | |
| 74 | 72 | public void test_unified_order_channel_failed() throws AlipayApiException { |
| 75 | 73 | String notifyUrl = randomURL(); |
| 76 | 74 | String subCode = randomString(); |
| 77 | 75 | String subMsg = randomString(); |
| 76 | + Integer price = randomInteger(); | |
| 78 | 77 | AlipayTradePrecreateResponse response = randomPojo(AlipayTradePrecreateResponse.class, o -> { |
| 79 | 78 | o.setSubCode(subCode); |
| 80 | 79 | o.setSubMsg(subMsg); |
| ... | ... | @@ -86,7 +85,7 @@ public class AlipayQrPayClientTest extends AbstractAlipayClientTest { |
| 86 | 85 | }))).thenReturn(response); |
| 87 | 86 | // 准备请求参数 |
| 88 | 87 | String outTradeNo = randomString(); |
| 89 | - PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo); | |
| 88 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo, price); | |
| 90 | 89 | |
| 91 | 90 | PayOrderRespDTO resp = client.unifiedOrder(reqDTO); |
| 92 | 91 | // 断言 |
| ... | ... | @@ -97,59 +96,38 @@ public class AlipayQrPayClientTest extends AbstractAlipayClientTest { |
| 97 | 96 | } |
| 98 | 97 | |
| 99 | 98 | @Test |
| 100 | - @DisplayName("支付包扫描支付,抛出系统异常") | |
| 99 | + @DisplayName("支付宝扫描支付, 抛出系统异常") | |
| 101 | 100 | public void test_unified_order_throw_pay_exception() throws AlipayApiException { |
| 102 | 101 | // 准备请求参数 |
| 103 | 102 | String outTradeNo = randomString(); |
| 104 | 103 | String notifyUrl = randomURL(); |
| 104 | + Integer price = randomInteger(); | |
| 105 | 105 | // mock |
| 106 | 106 | when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePrecreateRequest>) request -> { |
| 107 | 107 | assertEquals(notifyUrl, request.getNotifyUrl()); |
| 108 | 108 | return true; |
| 109 | 109 | }))).thenThrow(new RuntimeException("系统异常")); |
| 110 | 110 | // 准备请求参数 |
| 111 | - PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo); | |
| 111 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo,price); | |
| 112 | 112 | // 断言 |
| 113 | 113 | assertThrows(PayException.class, () -> client.unifiedOrder(reqDTO)); |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | @Test |
| 117 | - @DisplayName("支付包扫描支付,抛出业务异常") | |
| 117 | + @DisplayName("支付宝 Client 统一下单,抛出业务异常") | |
| 118 | 118 | public void test_unified_order_throw_service_exception() throws AlipayApiException { |
| 119 | 119 | // 准备请求参数 |
| 120 | 120 | String outTradeNo = randomString(); |
| 121 | 121 | String notifyUrl = randomURL(); |
| 122 | + Integer price = randomInteger(); | |
| 122 | 123 | // mock |
| 123 | 124 | when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePrecreateRequest>) request -> { |
| 124 | 125 | assertEquals(notifyUrl, request.getNotifyUrl()); |
| 125 | 126 | return true; |
| 126 | 127 | }))).thenThrow(ServiceExceptionUtil.exception(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR)); |
| 127 | 128 | // 准备请求参数 |
| 128 | - PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo); | |
| 129 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo, price); | |
| 129 | 130 | // 断言 |
| 130 | 131 | assertThrows(ServiceException.class, () -> client.unifiedOrder(reqDTO)); |
| 131 | 132 | } |
| 132 | - | |
| 133 | - @Test | |
| 134 | - @DisplayName("支付包扫描支付,参数校验不通过") | |
| 135 | - public void test_unified_order_param_validate() { | |
| 136 | - // 准备请求参数 | |
| 137 | - String outTradeNo = randomString(); | |
| 138 | - String notifyUrl = randomURL(); | |
| 139 | - PayOrderUnifiedReqDTO reqDTO = randomPojo(PayOrderUnifiedReqDTO.class, o -> { | |
| 140 | - o.setOutTradeNo(outTradeNo); | |
| 141 | - o.setNotifyUrl(notifyUrl); | |
| 142 | - }); | |
| 143 | - // 断言 | |
| 144 | - assertThrows(ConstraintViolationException.class, () -> client.unifiedOrder(reqDTO)); | |
| 145 | - } | |
| 146 | - | |
| 147 | - private PayOrderUnifiedReqDTO buildOrderUnifiedReqDTO(String notifyUrl, String outTradeNo) { | |
| 148 | - return randomPojo(PayOrderUnifiedReqDTO.class, o -> { | |
| 149 | - o.setOutTradeNo(outTradeNo); | |
| 150 | - o.setNotifyUrl(notifyUrl); | |
| 151 | - o.setSubject(RandomUtil.randomString(32)); | |
| 152 | - o.setBody(RandomUtil.randomString(32)); | |
| 153 | - }); | |
| 154 | - } | |
| 155 | 133 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayWapPayClientTest.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.domain.AlipayTradeWapPayModel; | |
| 9 | +import com.alipay.api.request.AlipayTradeWapPayRequest; | |
| 10 | +import com.alipay.api.response.AlipayTradeWapPayResponse; | |
| 11 | +import org.junit.jupiter.api.BeforeEach; | |
| 12 | +import org.junit.jupiter.api.DisplayName; | |
| 13 | +import org.junit.jupiter.api.Test; | |
| 14 | +import org.mockito.ArgumentMatcher; | |
| 15 | +import org.mockito.InjectMocks; | |
| 16 | + | |
| 17 | +import static cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum.CLOSED; | |
| 18 | +import static cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum.WAITING; | |
| 19 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | |
| 20 | +import static org.junit.jupiter.api.Assertions.*; | |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 22 | +import static org.mockito.ArgumentMatchers.argThat; | |
| 23 | +import static org.mockito.ArgumentMatchers.eq; | |
| 24 | +import static org.mockito.Mockito.when; | |
| 25 | + | |
| 26 | +/** | |
| 27 | + * {@link AlipayWapPayClient} 单元测试 | |
| 28 | + * | |
| 29 | + * @author jason | |
| 30 | + */ | |
| 31 | +public class AlipayWapPayClientTest extends AbstractAlipayClientTest { | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * 支付宝 H5 支付 Client | |
| 35 | + */ | |
| 36 | + @InjectMocks | |
| 37 | + private AlipayWapPayClient client = new AlipayWapPayClient(randomLongId(), config); | |
| 38 | + | |
| 39 | + @BeforeEach | |
| 40 | + public void setUp() { | |
| 41 | + setClient(client); | |
| 42 | + } | |
| 43 | + | |
| 44 | + @Test | |
| 45 | + @DisplayName("支付宝 H5 支付下单成功") | |
| 46 | + public void test_unified_order_success() throws AlipayApiException { | |
| 47 | + // 准备响应对象 | |
| 48 | + String h5Body = randomString(); | |
| 49 | + Integer price = randomInteger(); | |
| 50 | + AlipayTradeWapPayResponse response = randomPojo(AlipayTradeWapPayResponse.class, o -> { | |
| 51 | + o.setSubCode(""); | |
| 52 | + o.setBody(h5Body); | |
| 53 | + }); | |
| 54 | + String notifyUrl = randomURL(); | |
| 55 | + // mock | |
| 56 | + when(defaultAlipayClient.pageExecute(argThat((ArgumentMatcher<AlipayTradeWapPayRequest>) request -> { | |
| 57 | + assertInstanceOf(AlipayTradeWapPayModel.class, request.getBizModel()); | |
| 58 | + AlipayTradeWapPayModel bizModel = (AlipayTradeWapPayModel) request.getBizModel(); | |
| 59 | + assertEquals(String.valueOf(price / 100.0), bizModel.getTotalAmount()); | |
| 60 | + assertEquals(notifyUrl, request.getNotifyUrl()); | |
| 61 | + return true; | |
| 62 | + }), eq(Method.GET.name()))).thenReturn(response); | |
| 63 | + | |
| 64 | + String outTradeNo = randomString(); | |
| 65 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo, price); | |
| 66 | + PayOrderRespDTO resp = client.unifiedOrder(reqDTO); | |
| 67 | + assertEquals(WAITING.getStatus(), resp.getStatus()); | |
| 68 | + assertEquals(PayOrderDisplayModeEnum.URL.getMode(), resp.getDisplayMode()); | |
| 69 | + assertEquals(outTradeNo, resp.getOutTradeNo()); | |
| 70 | + assertEquals(h5Body, resp.getDisplayContent()); | |
| 71 | + assertSame(response, resp.getRawData()); | |
| 72 | + } | |
| 73 | + | |
| 74 | + @Test | |
| 75 | + @DisplayName("支付宝 H5 支付,渠道返回失败") | |
| 76 | + public void test_unified_order_channel_failed() throws AlipayApiException { | |
| 77 | + // 准备响应对象 | |
| 78 | + String subCode = randomString(); | |
| 79 | + String subMsg = randomString(); | |
| 80 | + AlipayTradeWapPayResponse response = randomPojo(AlipayTradeWapPayResponse.class, o -> { | |
| 81 | + o.setSubCode(subCode); | |
| 82 | + o.setSubMsg(subMsg); | |
| 83 | + }); | |
| 84 | + // mock | |
| 85 | + when(defaultAlipayClient.pageExecute(argThat((ArgumentMatcher<AlipayTradeWapPayRequest>) request -> true), | |
| 86 | + eq(Method.GET.name()))).thenReturn(response); | |
| 87 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(randomURL(), randomString(), randomInteger()); | |
| 88 | + | |
| 89 | + PayOrderRespDTO resp = client.unifiedOrder(reqDTO); | |
| 90 | + // 断言 | |
| 91 | + assertEquals(CLOSED.getStatus(), resp.getStatus()); | |
| 92 | + assertEquals(subCode, resp.getChannelErrorCode()); | |
| 93 | + assertEquals(subMsg, resp.getChannelErrorMsg()); | |
| 94 | + assertSame(response, resp.getRawData()); | |
| 95 | + } | |
| 96 | +} | ... | ... |