|
| 1 | +package com.github.binarywang.wxpay.v3.util; |
| 2 | + |
| 3 | +import com.github.binarywang.wxpay.bean.profitsharing.request.ProfitSharingReceiverV3Request; |
| 4 | +import com.github.binarywang.wxpay.bean.profitsharing.request.ProfitSharingV3Request; |
| 5 | +import com.github.binarywang.wxpay.exception.WxPayException; |
| 6 | +import org.testng.annotations.Test; |
| 7 | + |
| 8 | +import java.security.cert.X509Certificate; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import static org.testng.Assert.*; |
| 13 | + |
| 14 | +/** |
| 15 | + * RsaCryptoUtil 测试类 |
| 16 | + */ |
| 17 | +public class RsaCryptoUtilTest { |
| 18 | + |
| 19 | + /** |
| 20 | + * 测试嵌套对象中的字段加密 |
| 21 | + * 验证 List<Receiver> 中每个 Receiver 对象的 name 字段是否能被正确加密 |
| 22 | + */ |
| 23 | + @Test |
| 24 | + public void testEncryptFieldsWithNestedObjects() throws WxPayException { |
| 25 | + // 由于需要真实的证书才能加密,这里只是验证递归逻辑是否正确 |
| 26 | + // 创建测试对象 |
| 27 | + ProfitSharingV3Request request = new ProfitSharingV3Request(); |
| 28 | + |
| 29 | + List<ProfitSharingV3Request.Receiver> receivers = new ArrayList<>(); |
| 30 | + ProfitSharingV3Request.Receiver receiver = new ProfitSharingV3Request.Receiver(); |
| 31 | + receiver.setName("张三"); // 设置需要加密的字段 |
| 32 | + receiver.setAccount("test-account"); |
| 33 | + receiver.setType("PERSONAL_OPENID"); |
| 34 | + receiver.setAmount(100); |
| 35 | + |
| 36 | + receivers.add(receiver); |
| 37 | + request.setReceivers(receivers); |
| 38 | + |
| 39 | + // 注意:这个测试需要有效的证书才能真正执行加密 |
| 40 | + // 这里只是演示如何设置测试数据 |
| 41 | + // 如果没有证书,会在实际加密时抛出异常 |
| 42 | + |
| 43 | + System.out.println("测试对象创建成功,name字段: " + receiver.getName()); |
| 44 | + // 验证name字段不为null |
| 45 | + assertNotNull(receiver.getName()); |
| 46 | + assertEquals(receiver.getName(), "张三"); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * 测试单个对象中的字段加密 |
| 51 | + * 验证直接在对象上的 @SpecEncrypt 字段是否能被正确加密 |
| 52 | + */ |
| 53 | + @Test |
| 54 | + public void testEncryptFieldsWithDirectField() throws WxPayException { |
| 55 | + // 创建测试对象 |
| 56 | + ProfitSharingReceiverV3Request request = new ProfitSharingReceiverV3Request(); |
| 57 | + request.setName("李四"); // 设置需要加密的字段 |
| 58 | + request.setAccount("test-account"); |
| 59 | + request.setType("PERSONAL_OPENID"); |
| 60 | + |
| 61 | + System.out.println("测试对象创建成功,name字段: " + request.getName()); |
| 62 | + // 验证name字段不为null |
| 63 | + assertNotNull(request.getName()); |
| 64 | + assertEquals(request.getName(), "李四"); |
| 65 | + } |
| 66 | +} |
0 commit comments