Commit d3c02f2a575ad46bb94c68d3d644c133a0e38950
Merge remote-tracking branch 'origin/feature/mall_product' into feature/mall_product
Showing
1 changed file
with
127 additions
and
61 deletions
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 | + | |
| 3 | +import cn.hutool.core.util.RandomUtil; | |
| 2 | 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.order.PayOrderRespDTO; | |
| 3 | 9 | import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO; |
| 10 | +import cn.iocoder.yudao.framework.pay.core.client.exception.PayException; | |
| 11 | +import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderDisplayModeEnum; | |
| 4 | 12 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 5 | 13 | import com.alipay.api.AlipayApiException; |
| 6 | 14 | import com.alipay.api.DefaultAlipayClient; |
| 7 | 15 | import com.alipay.api.request.AlipayTradePrecreateRequest; |
| 8 | 16 | import com.alipay.api.response.AlipayTradePrecreateResponse; |
| 9 | -import org.junit.jupiter.api.Disabled; | |
| 10 | 17 | import org.junit.jupiter.api.Test; |
| 11 | 18 | import org.mockito.ArgumentMatcher; |
| 12 | 19 | import org.mockito.InjectMocks; |
| 13 | 20 | import org.mockito.Mock; |
| 14 | 21 | |
| 15 | -import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | |
| 16 | -import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 17 | -import static org.junit.jupiter.api.Assertions.assertNotSame; | |
| 22 | +import javax.validation.ConstraintViolationException; | |
| 23 | + | |
| 24 | +import static cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayPayClientConfig.MODE_PUBLIC_KEY; | |
| 25 | +import static cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum.CLOSED; | |
| 26 | +import static cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum.WAITING; | |
| 27 | +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | |
| 28 | +import static org.junit.jupiter.api.Assertions.*; | |
| 18 | 29 | import static org.mockito.ArgumentMatchers.argThat; |
| 19 | 30 | import static org.mockito.Mockito.when; |
| 20 | - | |
| 31 | +/** | |
| 32 | + * {@link AlipayQrPayClient} 单元测试 | |
| 33 | + * | |
| 34 | + * @author jason | |
| 35 | + */ | |
| 21 | 36 | public class AlipayQrPayClientTest extends BaseMockitoUnitTest { |
| 22 | 37 | |
| 23 | - private static final String SERVER_URL_SANDBOX = "https://openapi.alipaydev.com/gateway.do"; | |
| 24 | - | |
| 25 | - private final AlipayPayClientConfig config = new AlipayPayClientConfig() | |
| 26 | - .setAppId("2021000118634035") | |
| 27 | - .setServerUrl(SERVER_URL_SANDBOX) | |
| 28 | - .setSignType(AlipayPayClientConfig.SIGN_TYPE_DEFAULT) | |
| 29 | - // TODO @tina:key 可以随机就好,简洁一点哈。 | |
| 30 | - .setPrivateKey("MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCHsEV1cDupwJ" + | |
| 31 | - "v890x84qbppUtRIfhaKSwSVN0thCcsDCaAsGR5MZslDkO8NCT9V4r2SVXjyY7eJUZlZd1M0C8T" + | |
| 32 | - "01Tg4UOx5LUbic0O3A1uJMy6V1n9IyYwbAW3AEZhBd5bSbPgrqvmv3NeWSTQT6Anxnllf+2iDH" + | |
| 33 | - "6zyA2fPl7cYyQtbZoDJQFGqr4F+cGh2R6akzRKNoBkAeMYwoY6es2lX8sJxCVPWUmxNUoL3tScw" + | |
| 34 | - "lSpd7Bxw0q9c/X01jMwuQ0+Va358zgFiGERTE6yD01eu40OBDXOYO3z++y+TAYHlQQ2toMO63tr" + | |
| 35 | - "epo88X3xV3R44/1DH+k2pAm2IF5ixiLrAgMBAAECggEAPx3SoXcseaD7rmcGcE0p4SMfbsUDdk" + | |
| 36 | - "USmBBbtfF0GzwnqNLkWa+mgE0rWt9SmXngTQH97vByAYmLPl1s3G82ht1V7Sk7yQMe74lhFllr" + | |
| 37 | - "8eEyTjeVx3dTK1EEM4TwN+936DTXdFsr4TELJEcJJdD0KaxcCcfBLRDs2wnitEFZ9N+GoZybVmY8w" + | |
| 38 | - "0e0MI7PLObUZ2l0X4RurQnfG9ZxjXjC7PkeMVv7cGGylpNFi3BbvkRhdhLPDC2E6wqnr9e7zk+hiENi" + | |
| 39 | - "vAezXrtxtwKovzCtnWJ1r0IO14Rh47H509Ic0wFnj+o5YyUL4LdmpL7yaaH6fM7zcSLFjNZPHvZCKPw" + | |
| 40 | - "YcQKBgQDQFho98QvnL8ex4v6cry4VitGpjSXm1qP3vmMQk4rTsn8iPWtcxPjqGEqOQJjdi4Mi0VZKQO" + | |
| 41 | - "LFwlH0kl95wNrD/isJ4O1yeYfX7YAXApzHqYNINzM79HemO3Yx1qLMW3okRFJ9pPRzbQ9qkTpsaegsm" + | |
| 42 | - "yX316zOBhzGRYjKbutTYwKBgQCm7phr9XdFW5Vh+XR90mVs483nrLmMiDKg7YKxSLJ8amiDjzPejCn7i9" + | |
| 43 | - "5Hah08P+2MIZLIPbh2VLacczR6ltRRzN5bg5etFuqSgfkuHyxpoDmpjbe08+Q2h8JBYqcC5Nhv1AKU4iOU" + | |
| 44 | - "hVLHo/FBAQliMcGc/J3eiYTFC7EsNx382QKBgClb20doe7cttgFTXswBvaUmfFm45kmla924B7SpvrQpDD" + | |
| 45 | - "/f+VDtDZRp05fGmxuduSjYdtA3aVtpLiTwWu22OUUvZZqHDGruYOO4Hvdz23mL5b4ayqImCwoNU4bAZIc9v1" + | |
| 46 | - "8p/UNf3/55NNE3oGcf/bev9rH2OjCQ4nM+Ktwhg8CFAoGACSgvbkShzUkv0ZcIf9ppu+ZnJh1AdGgINvGwaJ" + | |
| 47 | - "8vQ0nm/8h8NOoFZ4oNoGc+wU5Ubops7dUM6FjPR5e+OjdJ4E7Xp7d5O4J1TaIZlCEbo5OpdhaTDDcQvrkFu+Z4e" + | |
| 48 | - "N0qzj+YAKjDAOOrXc4tbr5q0FsgXscwtcNfaBuzFVTUrUkCgYEAwzPnMNhWG3zOWLUs2QFA2GP4Y+J8cpUYfj6p" + | |
| 49 | - "bKKzeLwyG9qBwF1NJpN8m+q9q7V9P2LY+9Lp9e1mGsGeqt5HMEA3P6vIpcqLJLqE/4PBLLRzfccTcmqb1m71+erx" + | |
| 50 | - "TRhHBRkGS+I7dZEb3olQfnS1Y1tpMBxiwYwR3LW4oXuJwj8=") | |
| 51 | - .setAlipayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnq90KnF4dTnlzzmxpujbI05OYqi5WxAS6cL0" + | |
| 52 | - "gnZFv2gK51HExF8v/BaP7P979PhFMgWTqmOOI+Dtno5s+yD09XTY1WkshbLk6i4g2Xlr8fyW9ODnkU88RI2w9UdPhQU4cPPwBN" + | |
| 53 | - "lrsYhKkVK2OxwM3kFqjoBBY0CZoZCsSQ3LDH5WeZqPArlsS6xa2zqJBuuoKjMrdpELl3eXSjP8K54eDJCbeetCZNKWLL3DPahTPB7LZ" + | |
| 54 | - "ikfYmslb0QUvCgGapD0xkS7eVq70NaL1G57MWABs4tbfWgxike4Daj3EfUrzIVspQxj7w8HEj9WozJPgL88kSJSits0pqD3n5r8HSuseQIDAQAB"); | |
| 38 | + private final AlipayPayClientConfig config = randomPojo(AlipayPayClientConfig.class, t -> { | |
| 39 | + t.setServerUrl(randomURL()); | |
| 40 | + t.setMode(MODE_PUBLIC_KEY); | |
| 41 | + t.setSignType(AlipayPayClientConfig.SIGN_TYPE_DEFAULT); | |
| 42 | + t.setAppCertContent(""); | |
| 43 | + t.setAlipayPublicCertContent(""); | |
| 44 | + t.setRootCertContent(""); | |
| 45 | + }); | |
| 55 | 46 | |
| 56 | 47 | @InjectMocks |
| 57 | - AlipayQrPayClient client = new AlipayQrPayClient(10L,config); | |
| 48 | + AlipayQrPayClient client = new AlipayQrPayClient(randomLongId(), config); | |
| 58 | 49 | |
| 59 | 50 | @Mock |
| 60 | 51 | private DefaultAlipayClient defaultAlipayClient; |
| 61 | 52 | |
| 62 | 53 | @Test |
| 63 | - public void testDoInit(){ | |
| 54 | + public void testDoInit() { | |
| 64 | 55 | client.doInit(); |
| 65 | 56 | assertNotSame(defaultAlipayClient, ReflectUtil.getFieldValue(client, "defaultAlipayClient")); |
| 66 | 57 | } |
| 67 | 58 | |
| 68 | 59 | @Test |
| 69 | - @Disabled // TODO 芋艿:临时禁用 | |
| 70 | - public void create() throws AlipayApiException { | |
| 71 | - // TODO @tina:参数可以尽量随机一点,使用随机方法。这样的好处是,避免对固定参数的依赖,导致可能仅仅满足固定参数的结果 | |
| 72 | - // 这里,设置可以直接随机整个对象。 | |
| 73 | - Long shopOrderId = System.currentTimeMillis(); | |
| 74 | - PayOrderUnifiedReqDTO reqDTO=new PayOrderUnifiedReqDTO(); | |
| 75 | - reqDTO.setOutTradeNo(String.valueOf(System.currentTimeMillis())); | |
| 76 | - reqDTO.setPrice(1); | |
| 77 | - reqDTO.setBody("内容:" + shopOrderId); | |
| 78 | - reqDTO.setSubject("标题:"+shopOrderId); | |
| 79 | - String notify="http://niubi.natapp1.cc/api/pay/order/notify"; | |
| 80 | - reqDTO.setNotifyUrl(notify); | |
| 60 | + public void testUnifiedOrderSuccess() throws AlipayApiException { | |
| 61 | + // 准备返回对象 | |
| 62 | + String notifyUrl = randomURL(); | |
| 63 | + String qrCode = randomString(); | |
| 64 | + AlipayTradePrecreateResponse response = randomPojo(AlipayTradePrecreateResponse.class, o -> { | |
| 65 | + o.setQrCode(qrCode); | |
| 66 | + o.setSubCode(""); | |
| 67 | + }); | |
| 68 | + // mock | |
| 69 | + when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePrecreateRequest>) request -> { | |
| 70 | + assertEquals(notifyUrl, request.getNotifyUrl()); | |
| 71 | + return true; | |
| 72 | + }))).thenReturn(response); | |
| 73 | + // 准备请求参数 | |
| 74 | + String outTradeNo = randomString(); | |
| 75 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo); | |
| 81 | 76 | |
| 82 | - AlipayTradePrecreateResponse response=randomPojo(AlipayTradePrecreateResponse.class,o->o.setQrCode("success")); | |
| 77 | + PayOrderRespDTO resp = client.unifiedOrder(reqDTO); | |
| 78 | + // 断言 | |
| 79 | + assertEquals(WAITING.getStatus(), resp.getStatus()); | |
| 80 | + assertEquals(PayOrderDisplayModeEnum.QR_CODE.getMode(), resp.getDisplayMode()); | |
| 81 | + assertEquals(outTradeNo, resp.getOutTradeNo()); | |
| 82 | + assertEquals(qrCode, resp.getDisplayContent()); | |
| 83 | + assertSame(response, resp.getRawData()); | |
| 84 | + } | |
| 83 | 85 | |
| 84 | - when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePrecreateRequest>) request ->{ | |
| 85 | - assertEquals(notify,request.getNotifyUrl()); | |
| 86 | + @Test | |
| 87 | + public void testUnifiedOrderChannelFailed() throws AlipayApiException { | |
| 88 | + String notifyUrl = randomURL(); | |
| 89 | + String subCode = randomString(); | |
| 90 | + String subMsg = randomString(); | |
| 91 | + AlipayTradePrecreateResponse response = randomPojo(AlipayTradePrecreateResponse.class, o -> { | |
| 92 | + o.setSubCode(subCode); | |
| 93 | + o.setSubMsg(subMsg); | |
| 94 | + }); | |
| 95 | + // mock | |
| 96 | + when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePrecreateRequest>) request -> { | |
| 97 | + assertEquals(notifyUrl, request.getNotifyUrl()); | |
| 86 | 98 | return true; |
| 87 | 99 | }))).thenReturn(response); |
| 100 | + // 准备请求参数 | |
| 101 | + String outTradeNo = randomString(); | |
| 102 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo); | |
| 88 | 103 | |
| 104 | + PayOrderRespDTO resp = client.unifiedOrder(reqDTO); | |
| 105 | + // 断言 | |
| 106 | + assertEquals(CLOSED.getStatus(), resp.getStatus()); | |
| 107 | + assertEquals(subCode, resp.getChannelErrorCode()); | |
| 108 | + assertEquals(subMsg, resp.getChannelErrorMsg()); | |
| 109 | + assertSame(response, resp.getRawData()); | |
| 110 | + } | |
| 89 | 111 | |
| 90 | -// PayCommonResult<PayOrderUnifiedRespDTO> result = client.doUnifiedOrder(reqDTO); | |
| 91 | -// // 断言 | |
| 92 | -// assertEquals(response.getCode(), result.getApiCode()); | |
| 93 | -// assertEquals(response.getMsg(), result.getApiMsg()); | |
| 94 | -// // TODO @tina:这个断言木有过? | |
| 95 | -// assertEquals(GlobalErrorCodeConstants.SUCCESS.getCode(), result.getCode()); | |
| 96 | -// assertEquals(GlobalErrorCodeConstants.SUCCESS.getMsg(), result.getMsg()); | |
| 112 | + @Test | |
| 113 | + public void test_unifiedOrder_throw_pay_exception() throws AlipayApiException { | |
| 114 | + // 准备请求参数 | |
| 115 | + String outTradeNo = randomString(); | |
| 116 | + String notifyUrl = randomURL(); | |
| 117 | + // mock | |
| 118 | + when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePrecreateRequest>) request -> { | |
| 119 | + assertEquals(notifyUrl, request.getNotifyUrl()); | |
| 120 | + return true; | |
| 121 | + }))).thenThrow(new RuntimeException("系统异常")); | |
| 122 | + // 准备请求参数 | |
| 123 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo); | |
| 124 | + // 断言 | |
| 125 | + assertThrows(PayException.class, () -> client.unifiedOrder(reqDTO)); | |
| 126 | + } | |
| 127 | + | |
| 128 | + @Test | |
| 129 | + public void test_unifiedOrder_throw_service_exception() throws AlipayApiException { | |
| 130 | + // 准备请求参数 | |
| 131 | + String outTradeNo = randomString(); | |
| 132 | + String notifyUrl = randomURL(); | |
| 133 | + // mock | |
| 134 | + when(defaultAlipayClient.execute(argThat((ArgumentMatcher<AlipayTradePrecreateRequest>) request -> { | |
| 135 | + assertEquals(notifyUrl, request.getNotifyUrl()); | |
| 136 | + return true; | |
| 137 | + }))).thenThrow(ServiceExceptionUtil.exception(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR)); | |
| 138 | + // 准备请求参数 | |
| 139 | + PayOrderUnifiedReqDTO reqDTO = buildOrderUnifiedReqDTO(notifyUrl, outTradeNo); | |
| 140 | + // 断言 | |
| 141 | + assertThrows(ServiceException.class, () -> client.unifiedOrder(reqDTO)); | |
| 142 | + } | |
| 143 | + | |
| 144 | + @Test | |
| 145 | + public void test_unifiedOrder_param_validate() { | |
| 146 | + // 准备请求参数 | |
| 147 | + String outTradeNo = randomString(); | |
| 148 | + String notifyUrl = randomURL(); | |
| 149 | + PayOrderUnifiedReqDTO reqDTO = randomPojo(PayOrderUnifiedReqDTO.class, o -> { | |
| 150 | + o.setOutTradeNo(outTradeNo); | |
| 151 | + o.setNotifyUrl(notifyUrl); | |
| 152 | + }); | |
| 153 | + // 断言 | |
| 154 | + assertThrows(ConstraintViolationException.class, () -> client.unifiedOrder(reqDTO)); | |
| 155 | + } | |
| 97 | 156 | |
| 157 | + private PayOrderUnifiedReqDTO buildOrderUnifiedReqDTO(String notifyUrl, String outTradeNo) { | |
| 158 | + return randomPojo(PayOrderUnifiedReqDTO.class, o -> { | |
| 159 | + o.setOutTradeNo(outTradeNo); | |
| 160 | + o.setNotifyUrl(notifyUrl); | |
| 161 | + o.setSubject(RandomUtil.randomString(32)); | |
| 162 | + o.setBody(RandomUtil.randomString(32)); | |
| 163 | + }); | |
| 98 | 164 | } |
| 99 | 165 | } | ... | ... |