|
@@ -2,7 +2,6 @@ package cn.iocoder.yudao.framework.pay.core.client.impl.alipay;
|
|
|
|
|
|
import cn.hutool.core.date.LocalDateTimeUtil;
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
-import cn.hutool.core.util.ReflectUtil;
|
|
|
import cn.iocoder.yudao.framework.common.exception.ServiceException;
|
|
|
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
|
|
|
import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
|
|
@@ -18,6 +17,7 @@ import com.alipay.api.DefaultSigner;
|
|
|
import com.alipay.api.domain.AlipayTradeRefundModel;
|
|
|
import com.alipay.api.request.AlipayTradeRefundRequest;
|
|
|
import com.alipay.api.response.AlipayTradeRefundResponse;
|
|
|
+import lombok.Setter;
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
import org.junit.jupiter.api.DisplayName;
|
|
|
import org.junit.jupiter.api.Test;
|
|
@@ -34,25 +34,26 @@ import static org.mockito.ArgumentMatchers.argThat;
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
/**
|
|
|
+ * 支付宝 Client 的测试基类
|
|
|
+ *
|
|
|
* @author jason
|
|
|
*/
|
|
|
public abstract class AbstractAlipayClientTest extends BaseMockitoUnitTest {
|
|
|
|
|
|
- private final String privateKey = randomString();
|
|
|
-
|
|
|
- protected AlipayPayClientConfig config = randomPojo(AlipayPayClientConfig.class, t -> {
|
|
|
- t.setServerUrl(randomURL());
|
|
|
- t.setPrivateKey(privateKey);
|
|
|
- t.setMode(MODE_PUBLIC_KEY);
|
|
|
- t.setSignType(AlipayPayClientConfig.SIGN_TYPE_DEFAULT);
|
|
|
- t.setAppCertContent("");
|
|
|
- t.setAlipayPublicCertContent("");
|
|
|
- t.setRootCertContent("");
|
|
|
+ protected AlipayPayClientConfig config = randomPojo(AlipayPayClientConfig.class, o -> {
|
|
|
+ o.setServerUrl(randomURL());
|
|
|
+ o.setPrivateKey(randomString());
|
|
|
+ o.setMode(MODE_PUBLIC_KEY);
|
|
|
+ o.setSignType(AlipayPayClientConfig.SIGN_TYPE_DEFAULT);
|
|
|
+ o.setAppCertContent("");
|
|
|
+ o.setAlipayPublicCertContent("");
|
|
|
+ o.setRootCertContent("");
|
|
|
});
|
|
|
|
|
|
@Mock
|
|
|
protected DefaultAlipayClient defaultAlipayClient;
|
|
|
|
|
|
+ @Setter
|
|
|
private AbstractAlipayPayClient client;
|
|
|
|
|
|
/**
|
|
@@ -61,24 +62,22 @@ public abstract class AbstractAlipayClientTest extends BaseMockitoUnitTest {
|
|
|
@BeforeEach
|
|
|
public abstract void setUp();
|
|
|
|
|
|
- public void setClient(AbstractAlipayPayClient client) {
|
|
|
- this.client = client;
|
|
|
- }
|
|
|
-
|
|
|
@Test
|
|
|
@DisplayName("支付宝 Client 初始化")
|
|
|
- public void test_do_init() {
|
|
|
+ public void testDoInit() {
|
|
|
+ // 调用
|
|
|
client.doInit();
|
|
|
- DefaultAlipayClient realClient = (DefaultAlipayClient) ReflectUtil.getFieldValue(client, "client");
|
|
|
+ // 断言
|
|
|
+ DefaultAlipayClient realClient = client.getClient();
|
|
|
assertNotSame(defaultAlipayClient, realClient);
|
|
|
assertInstanceOf(DefaultSigner.class, realClient.getSigner());
|
|
|
- assertEquals(privateKey, ((DefaultSigner) realClient.getSigner()).getPrivateKey());
|
|
|
+ assertEquals(config.getPrivateKey(), ((DefaultSigner) realClient.getSigner()).getPrivateKey());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- @DisplayName("支付宝 Client 统一退款成功")
|
|
|
- public void test_unified_refund_success() throws AlipayApiException {
|
|
|
- // 准备返回对象
|
|
|
+ @DisplayName("支付宝 Client 统一退款:成功")
|
|
|
+ public void testUnifiedRefund_success() throws AlipayApiException {
|
|
|
+ // mock 方法
|
|
|
String notifyUrl = randomURL();
|
|
|
Date refundTime = randomDate();
|
|
|
String outRefundNo = randomString();
|
|
@@ -88,7 +87,6 @@ public abstract class AbstractAlipayClientTest extends BaseMockitoUnitTest {
|
|
|
o.setSubCode("");
|
|
|
o.setGmtRefundPay(refundTime);
|
|
|
});
|
|
|
- // mock
|
|
|
when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradeRefundRequest>) request -> {
|
|
|
assertInstanceOf(AlipayTradeRefundModel.class, request.getBizModel());
|
|
|
AlipayTradeRefundModel bizModel = (AlipayTradeRefundModel) request.getBizModel();
|
|
@@ -104,19 +102,23 @@ public abstract class AbstractAlipayClientTest extends BaseMockitoUnitTest {
|
|
|
o.setNotifyUrl(notifyUrl);
|
|
|
o.setRefundPrice(refundAmount);
|
|
|
});
|
|
|
+
|
|
|
+ // 调用
|
|
|
PayRefundRespDTO resp = client.unifiedRefund(refundReqDTO);
|
|
|
// 断言
|
|
|
assertEquals(PayRefundStatusRespEnum.SUCCESS.getStatus(), resp.getStatus());
|
|
|
+ assertEquals(outRefundNo, resp.getOutRefundNo());
|
|
|
assertNull(resp.getChannelRefundNo());
|
|
|
assertEquals(LocalDateTimeUtil.of(refundTime), resp.getSuccessTime());
|
|
|
- assertEquals(outRefundNo, resp.getOutRefundNo());
|
|
|
assertSame(response, resp.getRawData());
|
|
|
+ assertNull(resp.getChannelErrorCode());
|
|
|
+ assertNull(resp.getChannelErrorMsg());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- @DisplayName("支付宝 Client 统一退款,渠道返回失败")
|
|
|
+ @DisplayName("支付宝 Client 统一退款:渠道返回失败")
|
|
|
public void test_unified_refund_channel_failed() throws AlipayApiException {
|
|
|
- // 准备返回对象
|
|
|
+ // mock 方法
|
|
|
String notifyUrl = randomURL();
|
|
|
String subCode = randomString();
|
|
|
String subMsg = randomString();
|
|
@@ -124,7 +126,6 @@ public abstract class AbstractAlipayClientTest extends BaseMockitoUnitTest {
|
|
|
o.setSubCode(subCode);
|
|
|
o.setSubMsg(subMsg);
|
|
|
});
|
|
|
- // mock
|
|
|
when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradeRefundRequest>) request -> {
|
|
|
assertInstanceOf(AlipayTradeRefundModel.class, request.getBizModel());
|
|
|
return true;
|
|
@@ -137,59 +138,64 @@ public abstract class AbstractAlipayClientTest extends BaseMockitoUnitTest {
|
|
|
o.setOutTradeNo(outTradeNo);
|
|
|
o.setNotifyUrl(notifyUrl);
|
|
|
});
|
|
|
+
|
|
|
+ // 调用
|
|
|
PayRefundRespDTO resp = client.unifiedRefund(refundReqDTO);
|
|
|
// 断言
|
|
|
assertEquals(PayRefundStatusRespEnum.FAILURE.getStatus(), resp.getStatus());
|
|
|
+ assertEquals(outRefundNo, resp.getOutRefundNo());
|
|
|
assertNull(resp.getChannelRefundNo());
|
|
|
- assertEquals(subCode, resp.getChannelErrorCode());
|
|
|
- assertEquals(subMsg, resp.getChannelErrorMsg());
|
|
|
assertNull(resp.getSuccessTime());
|
|
|
- assertEquals(outRefundNo, resp.getOutRefundNo());
|
|
|
assertSame(response, resp.getRawData());
|
|
|
+ assertEquals(subCode, resp.getChannelErrorCode());
|
|
|
+ assertEquals(subMsg, resp.getChannelErrorMsg());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- @DisplayName("支付宝 Client 统一退款,参数校验不通过")
|
|
|
- public void test_unified_refund_param_validate() {
|
|
|
+ @DisplayName("支付宝 Client 统一退款:参数校验不通过")
|
|
|
+ public void testUnifiedRefund_paramInvalidate() {
|
|
|
// 准备请求参数
|
|
|
String notifyUrl = randomURL();
|
|
|
PayRefundUnifiedReqDTO refundReqDTO = randomPojo(PayRefundUnifiedReqDTO.class, o -> {
|
|
|
o.setOutTradeNo("");
|
|
|
o.setNotifyUrl(notifyUrl);
|
|
|
});
|
|
|
- // 断言
|
|
|
+
|
|
|
+ // 调用,并断言
|
|
|
assertThrows(ConstraintViolationException.class, () -> client.unifiedRefund(refundReqDTO));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- @DisplayName("支付宝 Client 统一退款,抛出业务异常")
|
|
|
- public void test_unified_refund_throw_service_exception() throws AlipayApiException {
|
|
|
+ @DisplayName("支付宝 Client 统一退款:抛出业务异常")
|
|
|
+ public void testUnifiedRefund_throwServiceException() throws AlipayApiException {
|
|
|
// mock
|
|
|
when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradeRefundRequest>) request -> true)))
|
|
|
.thenThrow(ServiceExceptionUtil.exception(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR));
|
|
|
// 准备请求参数
|
|
|
String notifyUrl = randomURL();
|
|
|
PayRefundUnifiedReqDTO refundReqDTO = randomPojo(PayRefundUnifiedReqDTO.class, o -> o.setNotifyUrl(notifyUrl));
|
|
|
- // 断言
|
|
|
+
|
|
|
+ // 调用,并断言
|
|
|
assertThrows(ServiceException.class, () -> client.unifiedRefund(refundReqDTO));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- @DisplayName("支付宝 Client 统一退款,抛出系统异常")
|
|
|
- public void test_unified_refund_throw_pay_exception() throws AlipayApiException {
|
|
|
+ @DisplayName("支付宝 Client 统一退款:抛出系统异常")
|
|
|
+ public void testUnifiedRefund_throwPayException() throws AlipayApiException {
|
|
|
// mock
|
|
|
when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradeRefundRequest>) request -> true)))
|
|
|
.thenThrow(new RuntimeException("系统异常"));
|
|
|
// 准备请求参数
|
|
|
String notifyUrl = randomURL();
|
|
|
PayRefundUnifiedReqDTO refundReqDTO = randomPojo(PayRefundUnifiedReqDTO.class, o -> o.setNotifyUrl(notifyUrl));
|
|
|
- // 断言
|
|
|
+
|
|
|
+ // 调用,并断言
|
|
|
assertThrows(PayException.class, () -> client.unifiedRefund(refundReqDTO));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- @DisplayName("支付宝 Client 统一下单, 参数校验不通过")
|
|
|
- public void test_unified_order_param_validate() {
|
|
|
+ @DisplayName("支付宝 Client 统一下单:参数校验不通过")
|
|
|
+ public void testUnifiedOrder_paramInvalidate() {
|
|
|
// 准备请求参数
|
|
|
String outTradeNo = randomString();
|
|
|
String notifyUrl = randomURL();
|
|
@@ -197,7 +203,8 @@ public abstract class AbstractAlipayClientTest extends BaseMockitoUnitTest {
|
|
|
o.setOutTradeNo(outTradeNo);
|
|
|
o.setNotifyUrl(notifyUrl);
|
|
|
});
|
|
|
- // 断言
|
|
|
+
|
|
|
+ // 调用,并断言
|
|
|
assertThrows(ConstraintViolationException.class, () -> client.unifiedOrder(reqDTO));
|
|
|
}
|
|
|
|
|
@@ -210,4 +217,5 @@ public abstract class AbstractAlipayClientTest extends BaseMockitoUnitTest {
|
|
|
o.setBody(RandomUtil.randomString(32));
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
}
|