mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-23 07:41:53 +08:00
支付宝 扫码支付 统一下单 单元测试
This commit is contained in:
parent
a159a983a2
commit
1b03fdb08b
@ -1,99 +1,165 @@
|
|||||||
package cn.iocoder.yudao.framework.pay.core.client.impl.alipay;
|
package cn.iocoder.yudao.framework.pay.core.client.impl.alipay;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.RandomUtil;
|
||||||
import cn.hutool.core.util.ReflectUtil;
|
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;
|
||||||
|
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO;
|
||||||
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO;
|
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO;
|
||||||
|
import cn.iocoder.yudao.framework.pay.core.client.exception.PayException;
|
||||||
|
import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderDisplayModeEnum;
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
||||||
import com.alipay.api.AlipayApiException;
|
import com.alipay.api.AlipayApiException;
|
||||||
import com.alipay.api.DefaultAlipayClient;
|
import com.alipay.api.DefaultAlipayClient;
|
||||||
import com.alipay.api.request.AlipayTradePrecreateRequest;
|
import com.alipay.api.request.AlipayTradePrecreateRequest;
|
||||||
import com.alipay.api.response.AlipayTradePrecreateResponse;
|
import com.alipay.api.response.AlipayTradePrecreateResponse;
|
||||||
import org.junit.jupiter.api.Disabled;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.ArgumentMatcher;
|
import org.mockito.ArgumentMatcher;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
import javax.validation.ConstraintViolationException;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
import static cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayPayClientConfig.MODE_PUBLIC_KEY;
|
||||||
|
import static cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum.CLOSED;
|
||||||
|
import static cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum.WAITING;
|
||||||
|
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.mockito.ArgumentMatchers.argThat;
|
import static org.mockito.ArgumentMatchers.argThat;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
/**
|
||||||
|
* {@link AlipayQrPayClient} 单元测试
|
||||||
|
*
|
||||||
|
* @author jason
|
||||||
|
*/
|
||||||
public class AlipayQrPayClientTest extends BaseMockitoUnitTest {
|
public class AlipayQrPayClientTest extends BaseMockitoUnitTest {
|
||||||
|
|
||||||
private static final String SERVER_URL_SANDBOX = "https://openapi.alipaydev.com/gateway.do";
|
private final AlipayPayClientConfig config = randomPojo(AlipayPayClientConfig.class, t -> {
|
||||||
|
t.setServerUrl(randomURL());
|
||||||
private final AlipayPayClientConfig config = new AlipayPayClientConfig()
|
t.setMode(MODE_PUBLIC_KEY);
|
||||||
.setAppId("2021000118634035")
|
t.setSignType(AlipayPayClientConfig.SIGN_TYPE_DEFAULT);
|
||||||
.setServerUrl(SERVER_URL_SANDBOX)
|
t.setAppCertContent("");
|
||||||
.setSignType(AlipayPayClientConfig.SIGN_TYPE_DEFAULT)
|
t.setAlipayPublicCertContent("");
|
||||||
// TODO @tina:key 可以随机就好,简洁一点哈。
|
t.setRootCertContent("");
|
||||||
.setPrivateKey("MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCHsEV1cDupwJ" +
|
});
|
||||||
"v890x84qbppUtRIfhaKSwSVN0thCcsDCaAsGR5MZslDkO8NCT9V4r2SVXjyY7eJUZlZd1M0C8T" +
|
|
||||||
"01Tg4UOx5LUbic0O3A1uJMy6V1n9IyYwbAW3AEZhBd5bSbPgrqvmv3NeWSTQT6Anxnllf+2iDH" +
|
|
||||||
"6zyA2fPl7cYyQtbZoDJQFGqr4F+cGh2R6akzRKNoBkAeMYwoY6es2lX8sJxCVPWUmxNUoL3tScw" +
|
|
||||||
"lSpd7Bxw0q9c/X01jMwuQ0+Va358zgFiGERTE6yD01eu40OBDXOYO3z++y+TAYHlQQ2toMO63tr" +
|
|
||||||
"epo88X3xV3R44/1DH+k2pAm2IF5ixiLrAgMBAAECggEAPx3SoXcseaD7rmcGcE0p4SMfbsUDdk" +
|
|
||||||
"USmBBbtfF0GzwnqNLkWa+mgE0rWt9SmXngTQH97vByAYmLPl1s3G82ht1V7Sk7yQMe74lhFllr" +
|
|
||||||
"8eEyTjeVx3dTK1EEM4TwN+936DTXdFsr4TELJEcJJdD0KaxcCcfBLRDs2wnitEFZ9N+GoZybVmY8w" +
|
|
||||||
"0e0MI7PLObUZ2l0X4RurQnfG9ZxjXjC7PkeMVv7cGGylpNFi3BbvkRhdhLPDC2E6wqnr9e7zk+hiENi" +
|
|
||||||
"vAezXrtxtwKovzCtnWJ1r0IO14Rh47H509Ic0wFnj+o5YyUL4LdmpL7yaaH6fM7zcSLFjNZPHvZCKPw" +
|
|
||||||
"YcQKBgQDQFho98QvnL8ex4v6cry4VitGpjSXm1qP3vmMQk4rTsn8iPWtcxPjqGEqOQJjdi4Mi0VZKQO" +
|
|
||||||
"LFwlH0kl95wNrD/isJ4O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bLk6i4g2Xlr8fyW9ODnkU88RI2w9UdPhQU4cPPwBN" +
|
|
||||||
"lrsYhKkVK2OxwM3kFqjoBBY0CZoZCsSQ3LDH5WeZqPArlsS6xa2zqJBuuoKjMrdpELl3eXSjP8K54eDJCbeetCZNKWLL3DPahTPB7LZ" +
|
|
||||||
"ikfYmslb0QUvCgGapD0xkS7eVq70NaL1G57MWABs4tbfWgxike4Daj3EfUrzIVspQxj7w8HEj9WozJPgL88kSJSits0pqD3n5r8HSuseQIDAQAB");
|
|
||||||
|
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
AlipayQrPayClient client = new AlipayQrPayClient(10L,config);
|
AlipayQrPayClient client = new AlipayQrPayClient(randomLongId(), config);
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private DefaultAlipayClient defaultAlipayClient;
|
private DefaultAlipayClient defaultAlipayClient;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDoInit(){
|
public void testDoInit() {
|
||||||
client.doInit();
|
client.doInit();
|
||||||
assertNotSame(defaultAlipayClient, ReflectUtil.getFieldValue(client, "defaultAlipayClient"));
|
assertNotSame(defaultAlipayClient, ReflectUtil.getFieldValue(client, "defaultAlipayClient"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Disabled // TODO 芋艿:临时禁用
|
public void testUnifiedOrderSuccess() throws AlipayApiException {
|
||||||
public void create() throws AlipayApiException {
|
// 准备返回对象
|
||||||
// TODO @tina:参数可以尽量随机一点,使用随机方法。这样的好处是,避免对固定参数的依赖,导致可能仅仅满足固定参数的结果
|
String notifyUrl = randomURL();
|
||||||
// 这里,设置可以直接随机整个对象。
|
String qrCode = randomString();
|
||||||
Long shopOrderId = System.currentTimeMillis();
|
AlipayTradePrecreateResponse response = randomPojo(AlipayTradePrecreateResponse.class, o -> {
|
||||||
PayOrderUnifiedReqDTO reqDTO=new PayOrderUnifiedReqDTO();
|
o.setQrCode(qrCode);
|
||||||
reqDTO.setOutTradeNo(String.valueOf(System.currentTimeMillis()));
|
o.setSubCode("");
|
||||||
reqDTO.setPrice(1);
|
});
|
||||||
reqDTO.setBody("内容:" + shopOrderId);
|
// mock
|
||||||
reqDTO.setSubject("标题:"+shopOrderId);
|
when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePrecreateRequest>) request -> {
|
||||||
String notify="http://niubi.natapp1.cc/api/pay/order/notify";
|
assertEquals(notifyUrl, request.getNotifyUrl());
|
||||||
reqDTO.setNotifyUrl(notify);
|
|
||||||
|
|
||||||
AlipayTradePrecreateResponse response=randomPojo(AlipayTradePrecreateResponse.class,o->o.setQrCode("success"));
|
|
||||||
|
|
||||||
when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePrecreateRequest>) request ->{
|
|
||||||
assertEquals(notify,request.getNotifyUrl());
|
|
||||||
return true;
|
return true;
|
||||||
}))).thenReturn(response);
|
}))).thenReturn(response);
|
||||||
|
// 准备请求参数
|
||||||
|
String outTradeNo = randomString();
|
||||||
|
PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo);
|
||||||
|
|
||||||
|
PayOrderRespDTO resp = client.unifiedOrder(reqDTO);
|
||||||
|
// 断言
|
||||||
|
assertEquals(WAITING.getStatus(), resp.getStatus());
|
||||||
|
assertEquals(PayOrderDisplayModeEnum.QR_CODE.getMode(), resp.getDisplayMode());
|
||||||
|
assertEquals(outTradeNo, resp.getOutTradeNo());
|
||||||
|
assertEquals(qrCode, resp.getDisplayContent());
|
||||||
|
assertSame(response, resp.getRawData());
|
||||||
|
}
|
||||||
|
|
||||||
// PayCommonResult<PayOrderUnifiedRespDTO> result = client.doUnifiedOrder(reqDTO);
|
@Test
|
||||||
// // 断言
|
public void testUnifiedOrderChannelFailed() throws AlipayApiException {
|
||||||
// assertEquals(response.getCode(), result.getApiCode());
|
String notifyUrl = randomURL();
|
||||||
// assertEquals(response.getMsg(), result.getApiMsg());
|
String subCode = randomString();
|
||||||
// // TODO @tina:这个断言木有过?
|
String subMsg = randomString();
|
||||||
// assertEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), result.getCode());
|
AlipayTradePrecreateResponse response = randomPojo(AlipayTradePrecreateResponse.class, o -> {
|
||||||
// assertEquals(GlobalErrorCodeConstants.SUCCESS.getMsg(), result.getMsg());
|
o.setSubCode(subCode);
|
||||||
|
o.setSubMsg(subMsg);
|
||||||
|
});
|
||||||
|
// mock
|
||||||
|
when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePrecreateRequest>) request -> {
|
||||||
|
assertEquals(notifyUrl, request.getNotifyUrl());
|
||||||
|
return true;
|
||||||
|
}))).thenReturn(response);
|
||||||
|
// 准备请求参数
|
||||||
|
String outTradeNo = randomString();
|
||||||
|
PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo);
|
||||||
|
|
||||||
|
PayOrderRespDTO resp = client.unifiedOrder(reqDTO);
|
||||||
|
// 断言
|
||||||
|
assertEquals(CLOSED.getStatus(), resp.getStatus());
|
||||||
|
assertEquals(subCode, resp.getChannelErrorCode());
|
||||||
|
assertEquals(subMsg, resp.getChannelErrorMsg());
|
||||||
|
assertSame(response, resp.getRawData());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test_unifiedOrder_throw_pay_exception() throws AlipayApiException {
|
||||||
|
// 准备请求参数
|
||||||
|
String outTradeNo = randomString();
|
||||||
|
String notifyUrl = randomURL();
|
||||||
|
// mock
|
||||||
|
when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePrecreateRequest>) request -> {
|
||||||
|
assertEquals(notifyUrl, request.getNotifyUrl());
|
||||||
|
return true;
|
||||||
|
}))).thenThrow(new RuntimeException("系统异常"));
|
||||||
|
// 准备请求参数
|
||||||
|
PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo);
|
||||||
|
// 断言
|
||||||
|
assertThrows(PayException.class, () -> client.unifiedOrder(reqDTO));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test_unifiedOrder_throw_service_exception() throws AlipayApiException {
|
||||||
|
// 准备请求参数
|
||||||
|
String outTradeNo = randomString();
|
||||||
|
String notifyUrl = randomURL();
|
||||||
|
// mock
|
||||||
|
when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePrecreateRequest>) request -> {
|
||||||
|
assertEquals(notifyUrl, request.getNotifyUrl());
|
||||||
|
return true;
|
||||||
|
}))).thenThrow(ServiceExceptionUtil.exception(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR));
|
||||||
|
// 准备请求参数
|
||||||
|
PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo);
|
||||||
|
// 断言
|
||||||
|
assertThrows(ServiceException.class, () -> client.unifiedOrder(reqDTO));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test_unifiedOrder_param_validate() {
|
||||||
|
// 准备请求参数
|
||||||
|
String outTradeNo = randomString();
|
||||||
|
String notifyUrl = randomURL();
|
||||||
|
PayOrderUnifiedReqDTO reqDTO = randomPojo(PayOrderUnifiedReqDTO.class, o -> {
|
||||||
|
o.setOutTradeNo(outTradeNo);
|
||||||
|
o.setNotifyUrl(notifyUrl);
|
||||||
|
});
|
||||||
|
// 断言
|
||||||
|
assertThrows(ConstraintViolationException.class, () -> client.unifiedOrder(reqDTO));
|
||||||
|
}
|
||||||
|
|
||||||
|
private PayOrderUnifiedReqDTO buildOrderUnifiedReqDTO(String notifyUrl, String outTradeNo) {
|
||||||
|
return randomPojo(PayOrderUnifiedReqDTO.class, o -> {
|
||||||
|
o.setOutTradeNo(outTradeNo);
|
||||||
|
o.setNotifyUrl(notifyUrl);
|
||||||
|
o.setSubject(RandomUtil.randomString(32));
|
||||||
|
o.setBody(RandomUtil.randomString(32));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user