Commit eff9fafde8c23e9025d9fbace538b382715dd14e

Authored by jason
1 parent 800c44a4

创建支付宝 Client 抽象类测试类, 测试公共方法

yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AbstractAlipayClientTest.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.hutool.core.util.ReflectUtil;
  5 +import cn.iocoder.yudao.framework.common.exception.ServiceException;
  6 +import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
  7 +import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
  8 +import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO;
  9 +import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO;
  10 +import cn.iocoder.yudao.framework.pay.core.client.exception.PayException;
  11 +import cn.iocoder.yudao.framework.pay.core.enums.refund.PayRefundStatusRespEnum;
  12 +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
  13 +import com.alipay.api.AlipayApiException;
  14 +import com.alipay.api.DefaultAlipayClient;
  15 +import com.alipay.api.DefaultSigner;
  16 +import com.alipay.api.domain.AlipayTradeRefundModel;
  17 +import com.alipay.api.request.AlipayTradeRefundRequest;
  18 +import com.alipay.api.response.AlipayTradeRefundResponse;
  19 +import org.junit.jupiter.api.DisplayName;
  20 +import org.junit.jupiter.api.Test;
  21 +import org.mockito.ArgumentMatcher;
  22 +import org.mockito.Mock;
  23 +
  24 +import javax.validation.ConstraintViolationException;
  25 +import java.util.Date;
  26 +
  27 +import static cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayPayClientConfig.MODE_PUBLIC_KEY;
  28 +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
  29 +import static org.junit.jupiter.api.Assertions.*;
  30 +import static org.mockito.ArgumentMatchers.argThat;
  31 +import static org.mockito.Mockito.when;
  32 +
  33 +/**
  34 + * @author jason
  35 + */
  36 +public abstract class AbstractAlipayClientTest extends BaseMockitoUnitTest {
  37 +
  38 + private final String privateKey = randomString();
  39 +
  40 + protected AlipayPayClientConfig config = randomPojo(AlipayPayClientConfig.class, t -> {
  41 + t.setServerUrl(randomURL());
  42 + t.setPrivateKey(privateKey);
  43 + t.setMode(MODE_PUBLIC_KEY);
  44 + t.setSignType(AlipayPayClientConfig.SIGN_TYPE_DEFAULT);
  45 + t.setAppCertContent("");
  46 + t.setAlipayPublicCertContent("");
  47 + t.setRootCertContent("");
  48 + });
  49 +
  50 + @Mock
  51 + protected DefaultAlipayClient defaultAlipayClient;
  52 +
  53 + private AbstractAlipayPayClient client;
  54 +
  55 + public void setClient(AbstractAlipayPayClient client) {
  56 + this.client = client;
  57 + }
  58 +
  59 + @Test
  60 + @DisplayName("支付宝 Client 初始化")
  61 + public void test_do_init() {
  62 + client.doInit();
  63 + DefaultAlipayClient realClient = (DefaultAlipayClient) ReflectUtil.getFieldValue(client, "client");
  64 + assertNotSame(defaultAlipayClient, realClient);
  65 + assertInstanceOf(DefaultSigner.class, realClient.getSigner());
  66 + assertEquals(privateKey, ((DefaultSigner) realClient.getSigner()).getPrivateKey());
  67 + }
  68 +
  69 + @Test
  70 + @DisplayName("支付宝 Client 统一退款成功")
  71 + public void test_unified_refund_success() throws AlipayApiException {
  72 + // 准备返回对象
  73 + String notifyUrl = randomURL();
  74 + Date refundTime = randomDate();
  75 + String outRefundNo = randomString();
  76 + String outTradeNo = randomString();
  77 + Integer refundAmount = randomInteger();
  78 + AlipayTradeRefundResponse response = randomPojo(AlipayTradeRefundResponse.class, o -> {
  79 + o.setSubCode("");
  80 + o.setGmtRefundPay(refundTime);
  81 + });
  82 + // mock
  83 + when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradeRefundRequest>) request -> {
  84 + assertInstanceOf(AlipayTradeRefundModel.class, request.getBizModel());
  85 + AlipayTradeRefundModel bizModel = (AlipayTradeRefundModel) request.getBizModel();
  86 + assertEquals(outRefundNo, bizModel.getOutRequestNo());
  87 + assertEquals(outTradeNo, bizModel.getOutTradeNo());
  88 + assertEquals(String.valueOf(refundAmount / 100.0), bizModel.getRefundAmount());
  89 + return true;
  90 + }))).thenReturn(response);
  91 + // 准备请求参数
  92 + PayRefundUnifiedReqDTO refundReqDTO = randomPojo(PayRefundUnifiedReqDTO.class, o -> {
  93 + o.setOutRefundNo(outRefundNo);
  94 + o.setOutTradeNo(outTradeNo);
  95 + o.setNotifyUrl(notifyUrl);
  96 + o.setRefundPrice(refundAmount);
  97 + });
  98 + PayRefundRespDTO resp = client.unifiedRefund(refundReqDTO);
  99 + // 断言
  100 + assertEquals(PayRefundStatusRespEnum.SUCCESS.getStatus(), resp.getStatus());
  101 + assertNull(resp.getChannelRefundNo());
  102 + assertEquals(LocalDateTimeUtil.of(refundTime), resp.getSuccessTime());
  103 + assertEquals(outRefundNo, resp.getOutRefundNo());
  104 + assertSame(response, resp.getRawData());
  105 + }
  106 +
  107 + @Test
  108 + @DisplayName("支付宝 Client 统一退款,渠道返回失败")
  109 + public void test_unified_refund_channel_failed() throws AlipayApiException {
  110 + // 准备返回对象
  111 + String notifyUrl = randomURL();
  112 + String subCode = randomString();
  113 + String subMsg = randomString();
  114 + AlipayTradeRefundResponse response = randomPojo(AlipayTradeRefundResponse.class, o -> {
  115 + o.setSubCode(subCode);
  116 + o.setSubMsg(subMsg);
  117 + });
  118 + // mock
  119 + when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradeRefundRequest>) request -> {
  120 + assertInstanceOf(AlipayTradeRefundModel.class, request.getBizModel());
  121 + return true;
  122 + }))).thenReturn(response);
  123 + // 准备请求参数
  124 + String outRefundNo = randomString();
  125 + String outTradeNo = randomString();
  126 + PayRefundUnifiedReqDTO refundReqDTO = randomPojo(PayRefundUnifiedReqDTO.class, o -> {
  127 + o.setOutRefundNo(outRefundNo);
  128 + o.setOutTradeNo(outTradeNo);
  129 + o.setNotifyUrl(notifyUrl);
  130 + });
  131 + PayRefundRespDTO resp = client.unifiedRefund(refundReqDTO);
  132 + // 断言
  133 + assertEquals(PayRefundStatusRespEnum.FAILURE.getStatus(), resp.getStatus());
  134 + assertNull(resp.getChannelRefundNo());
  135 + assertEquals(subCode, resp.getChannelErrorCode());
  136 + assertEquals(subMsg, resp.getChannelErrorMsg());
  137 + assertNull(resp.getSuccessTime());
  138 + assertEquals(outRefundNo, resp.getOutRefundNo());
  139 + assertSame(response, resp.getRawData());
  140 + }
  141 +
  142 + @Test
  143 + @DisplayName("支付宝 Client 统一退款,参数校验不通过")
  144 + public void test_unified_refund_param_validate() {
  145 + // 准备请求参数
  146 + String notifyUrl = randomURL();
  147 + PayRefundUnifiedReqDTO refundReqDTO = randomPojo(PayRefundUnifiedReqDTO.class, o -> {
  148 + o.setOutTradeNo("");
  149 + o.setNotifyUrl(notifyUrl);
  150 + });
  151 + // 断言
  152 + assertThrows(ConstraintViolationException.class, () -> client.unifiedRefund(refundReqDTO));
  153 + }
  154 +
  155 + @Test
  156 + @DisplayName("支付宝 Client 统一退款,抛出业务异常")
  157 + public void test_unified_refund_throw_service_exception() throws AlipayApiException {
  158 + // mock
  159 + when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradeRefundRequest>) request -> true)))
  160 + .thenThrow(ServiceExceptionUtil.exception(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR));
  161 + // 准备请求参数
  162 + String notifyUrl = randomURL();
  163 + PayRefundUnifiedReqDTO refundReqDTO = randomPojo(PayRefundUnifiedReqDTO.class, o -> o.setNotifyUrl(notifyUrl));
  164 + // 断言
  165 + assertThrows(ServiceException.class, () -> client.unifiedRefund(refundReqDTO));
  166 + }
  167 +
  168 + @Test
  169 + @DisplayName("支付宝 Client 统一退款,抛出系统异常")
  170 + public void test_unified_refund_throw_pay_exception() throws AlipayApiException {
  171 + // mock
  172 + when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradeRefundRequest>) request -> true)))
  173 + .thenThrow(new RuntimeException("系统异常"));
  174 + // 准备请求参数
  175 + String notifyUrl = randomURL();
  176 + PayRefundUnifiedReqDTO refundReqDTO = randomPojo(PayRefundUnifiedReqDTO.class, o -> o.setNotifyUrl(notifyUrl));
  177 + // 断言
  178 + assertThrows(PayException.class, () -> client.unifiedRefund(refundReqDTO));
  179 + }
  180 +}
... ...
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.date.LocalDateTimeUtil;
4 3 import cn.hutool.core.util.RandomUtil;
5   -import cn.hutool.core.util.ReflectUtil;
6 4 import cn.iocoder.yudao.framework.common.exception.ServiceException;
7 5 import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
8 6 import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
9 7 import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO;
10 8 import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO;
11   -import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO;
12   -import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO;
13 9 import cn.iocoder.yudao.framework.pay.core.client.exception.PayException;
14 10 import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderDisplayModeEnum;
15   -import cn.iocoder.yudao.framework.pay.core.enums.refund.PayRefundStatusRespEnum;
16   -import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
17 11 import com.alipay.api.AlipayApiException;
18   -import com.alipay.api.DefaultAlipayClient;
19   -import com.alipay.api.domain.AlipayTradeRefundModel;
20 12 import com.alipay.api.request.AlipayTradePrecreateRequest;
21   -import com.alipay.api.request.AlipayTradeRefundRequest;
22 13 import com.alipay.api.response.AlipayTradePrecreateResponse;
23   -import com.alipay.api.response.AlipayTradeRefundResponse;
  14 +import org.junit.jupiter.api.BeforeEach;
24 15 import org.junit.jupiter.api.DisplayName;
25 16 import org.junit.jupiter.api.Test;
26 17 import org.mockito.ArgumentMatcher;
27 18 import org.mockito.InjectMocks;
28   -import org.mockito.Mock;
29 19  
30 20 import javax.validation.ConstraintViolationException;
31   -import java.util.Date;
32 21  
33   -import static cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayPayClientConfig.MODE_PUBLIC_KEY;
34 22 import static cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum.CLOSED;
35 23 import static cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum.WAITING;
36 24 import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
37 25 import static org.junit.jupiter.api.Assertions.*;
38 26 import static org.mockito.ArgumentMatchers.argThat;
39 27 import static org.mockito.Mockito.when;
  28 +
40 29 /**
41 30 * {@link AlipayQrPayClient} 单元测试
42 31 *
43 32 * @author jason
44 33 */
45   -public class AlipayQrPayClientTest extends BaseMockitoUnitTest {
46   -
47   - private final AlipayPayClientConfig config = randomPojo(AlipayPayClientConfig.class, t -> {
48   - t.setServerUrl(randomURL());
49   - t.setMode(MODE_PUBLIC_KEY);
50   - t.setSignType(AlipayPayClientConfig.SIGN_TYPE_DEFAULT);
51   - t.setAppCertContent("");
52   - t.setAlipayPublicCertContent("");
53   - t.setRootCertContent("");
54   - });
  34 +public class AlipayQrPayClientTest extends AbstractAlipayClientTest {
55 35  
56 36 @InjectMocks
57   - AlipayQrPayClient client = new AlipayQrPayClient(randomLongId(), config);
58   -
59   - @Mock
60   - private DefaultAlipayClient defaultAlipayClient;
  37 + private AlipayQrPayClient client = new AlipayQrPayClient(randomLongId(), config);
61 38  
62   - @Test
63   - public void test_do_init() {
64   - client.doInit();
65   - assertNotSame(defaultAlipayClient, ReflectUtil.getFieldValue(client, "defaultAlipayClient"));
  39 + @BeforeEach
  40 + public void setUp() {
  41 + setClient(client);
66 42 }
67 43  
68 44 @Test
... ... @@ -176,119 +152,4 @@ public class AlipayQrPayClientTest extends BaseMockitoUnitTest {
176 152 o.setBody(RandomUtil.randomString(32));
177 153 });
178 154 }
179   -
180   - @Test
181   - @DisplayName("支付包扫描退款成功")
182   - public void test_unified_refund_success() throws AlipayApiException {
183   - // 准备返回对象
184   - String notifyUrl = randomURL();
185   - Date refundTime = randomDate();
186   - String outRefundNo = randomString();
187   - String outTradeNo = randomString();
188   - Integer refundAmount = randomInteger();
189   - AlipayTradeRefundResponse response = randomPojo(AlipayTradeRefundResponse.class, o -> {
190   - o.setSubCode("");
191   - o.setGmtRefundPay(refundTime);
192   - });
193   - // mock
194   - when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradeRefundRequest>) request -> {
195   - assertInstanceOf(AlipayTradeRefundModel.class, request.getBizModel());
196   - AlipayTradeRefundModel bizModel = (AlipayTradeRefundModel) request.getBizModel();
197   - assertEquals(outRefundNo, bizModel.getOutRequestNo());
198   - assertEquals(outTradeNo, bizModel.getOutTradeNo());
199   - assertEquals(String.valueOf(refundAmount / 100.0), bizModel.getRefundAmount());
200   - return true;
201   - }))).thenReturn(response);
202   - // 准备请求参数
203   - PayRefundUnifiedReqDTO refundReqDTO = randomPojo(PayRefundUnifiedReqDTO.class, o -> {
204   - o.setOutRefundNo(outRefundNo);
205   - o.setOutTradeNo(outTradeNo);
206   - o.setNotifyUrl(notifyUrl);
207   - o.setRefundPrice(refundAmount);
208   - });
209   - PayRefundRespDTO resp = client.unifiedRefund(refundReqDTO);
210   - // 断言
211   - assertEquals(PayRefundStatusRespEnum.SUCCESS.getStatus(), resp.getStatus());
212   - assertNull(resp.getChannelRefundNo());
213   - assertEquals(LocalDateTimeUtil.of(refundTime), resp.getSuccessTime());
214   - assertEquals(outRefundNo, resp.getOutRefundNo());
215   - assertSame(response, resp.getRawData());
216   - }
217   -
218   - @Test
219   - @DisplayName("支付包扫描退款,渠道返回失败")
220   - public void test_unified_refund_channel_failed() throws AlipayApiException {
221   - // 准备返回对象
222   - String notifyUrl = randomURL();
223   - String subCode = randomString();
224   - String subMsg = randomString();
225   - AlipayTradeRefundResponse response = randomPojo(AlipayTradeRefundResponse.class, o -> {
226   - o.setSubCode(subCode);
227   - o.setSubMsg(subMsg);
228   - });
229   - // mock
230   - when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradeRefundRequest>) request -> {
231   - assertInstanceOf(AlipayTradeRefundModel.class, request.getBizModel());
232   - return true;
233   - }))).thenReturn(response);
234   - // 准备请求参数
235   - String outRefundNo = randomString();
236   - String outTradeNo = randomString();
237   - PayRefundUnifiedReqDTO refundReqDTO = randomPojo(PayRefundUnifiedReqDTO.class, o -> {
238   - o.setOutRefundNo(outRefundNo);
239   - o.setOutTradeNo(outTradeNo);
240   - o.setNotifyUrl(notifyUrl);
241   - });
242   - PayRefundRespDTO resp = client.unifiedRefund(refundReqDTO);
243   - // 断言
244   - assertEquals(PayRefundStatusRespEnum.FAILURE.getStatus(), resp.getStatus());
245   - assertNull(resp.getChannelRefundNo());
246   - assertEquals(subCode, resp.getChannelErrorCode());
247   - assertEquals(subMsg, resp.getChannelErrorMsg());
248   - assertNull(resp.getSuccessTime());
249   - assertEquals(outRefundNo, resp.getOutRefundNo());
250   - assertSame(response, resp.getRawData());
251   - }
252   -
253   - @Test
254   - @DisplayName("支付包扫描退款,参数校验不通过")
255   - public void test_unified_refund_param_validate() {
256   - // 准备请求参数
257   - String notifyUrl = randomURL();
258   - PayRefundUnifiedReqDTO refundReqDTO = randomPojo(PayRefundUnifiedReqDTO.class, o -> {
259   - o.setOutTradeNo("");
260   - o.setNotifyUrl(notifyUrl);
261   - });
262   - // 断言
263   - assertThrows(ConstraintViolationException.class, () -> client.unifiedRefund(refundReqDTO));
264   - }
265   -
266   - @Test
267   - @DisplayName("支付包扫描退款,抛出业务异常")
268   - public void test_unified_refund_throw_service_exception() throws AlipayApiException {
269   - // mock
270   - when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradeRefundRequest>) request -> true)))
271   - .thenThrow(ServiceExceptionUtil.exception(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR));
272   - // 准备请求参数
273   - String notifyUrl = randomURL();
274   - PayRefundUnifiedReqDTO refundReqDTO = randomPojo(PayRefundUnifiedReqDTO.class, o -> {
275   - o.setNotifyUrl(notifyUrl);
276   - });
277   - // 断言
278   - assertThrows(ServiceException.class, () -> client.unifiedRefund(refundReqDTO));
279   - }
280   - @Test
281   - @DisplayName("支付包扫描退款,抛出系统异常")
282   - public void test_unified_refund_throw_pay_exception() throws AlipayApiException {
283   - // mock
284   - when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradeRefundRequest>) request -> true)))
285   - .thenThrow(new RuntimeException("系统异常"));
286   - // 准备请求参数
287   - String notifyUrl = randomURL();
288   - PayRefundUnifiedReqDTO refundReqDTO = randomPojo(PayRefundUnifiedReqDTO.class, o -> {
289   - o.setNotifyUrl(notifyUrl);
290   - });
291   - // 断言
292   - assertThrows(PayException.class, () -> client.unifiedRefund(refundReqDTO));
293   - }
294 155 }
... ...