From 2e668455843b68cb3452b88ae1a8eed9f1fa9a6c Mon Sep 17 00:00:00 2001 From: YunaiV Date: Thu, 7 Apr 2022 23:53:19 +0800 Subject: [PATCH] =?UTF-8?q?code=20review=20=E8=85=BE=E8=AE=AF=E7=9F=AD?= =?UTF-8?q?=E4=BF=A1=E7=9A=84=E5=AE=9E=E7=8E=B0=EF=BC=8C=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E6=B2=A1=E5=95=A5=E9=97=AE=E9=A2=98=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/client/impl/tencent/TencentSmsClient.java | 13 ++++++++----- .../client/impl/tencent/TencentSmsClientTest.java | 10 ++++++---- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-biz-sms/src/main/java/cn/iocoder/yudao/framework/sms/core/client/impl/tencent/TencentSmsClient.java b/yudao-framework/yudao-spring-boot-starter-biz-sms/src/main/java/cn/iocoder/yudao/framework/sms/core/client/impl/tencent/TencentSmsClient.java index 0cb8e6aa1..43c94f776 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-sms/src/main/java/cn/iocoder/yudao/framework/sms/core/client/impl/tencent/TencentSmsClient.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-sms/src/main/java/cn/iocoder/yudao/framework/sms/core/client/impl/tencent/TencentSmsClient.java @@ -43,18 +43,19 @@ public class TencentSmsClient extends AbstractSmsClient { private SmsClient client; public TencentSmsClient(SmsChannelProperties properties) { - // 腾讯云发放短信的时候需要额外的参数 sdkAppId, 所以和 secretId 组合在一起放到 apiKey 字段中,格式为[secretId sdkAppId], - // 这边需要做拆分重新封装到 properties 内 + // 腾讯云发放短信的时候,需要额外的参数 sdkAppId。考虑到不破坏原有的 apiKey + apiSecret 的结构,所以将 secretId 拼接到 apiKey 字段中,格式为 "secretId sdkAppId"。 + // 因此,这边需要使用 TencentSmsChannelProperties 做拆分,重新封装到 properties 内。 super(TencentSmsChannelProperties.build(properties), new TencentSmsCodeMapping()); Assert.notEmpty(properties.getApiSecret(), "apiSecret 不能为空"); } @Override protected void doInit() { - // init 或者 refresh 时需要重新封装 properties + // init 或者 refresh 时,需要重新封装 properties properties = TencentSmsChannelProperties.build(properties); - // 实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId,secretKey。 + // 实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId,secretKey Credential credential = new Credential(properties.getApiKey(), properties.getApiSecret()); + // TODO @FinallySays:那把 ap-nanjing 枚举下到这个类的静态变量里哈。 client = new SmsClient(credential, "ap-nanjing"); } @@ -170,6 +171,7 @@ public class TencentSmsClient extends AbstractSmsClient { DescribeSmsTemplateListRequest request = new DescribeSmsTemplateListRequest(); request.setTemplateIdSet(new Long[]{Long.parseLong(apiTemplateId)}); // 地区 0:表示国内短信。1:表示国际/港澳台短信。 + // TODO @FinallySays:那把 0L 枚举下到这个类的静态变量里哈。 request.setInternational(0L); return request; } @@ -268,6 +270,7 @@ public class TencentSmsClient extends AbstractSmsClient { R apply(T t) throws TencentCloudSDKException; } + // TODO @FinallySays:要不单独一个类,不用作为内部类哈。这样,可能一看就知道,哟,腾讯短信是特殊的 @Data private static class TencentSmsChannelProperties extends SmsChannelProperties { @@ -281,7 +284,7 @@ public class TencentSmsClient extends AbstractSmsClient { String combineKey = properties.getApiKey(); Assert.notEmpty(combineKey, "apiKey 不能为空"); String[] keys = combineKey.trim().split(" "); - Assert.isTrue(keys.length == 2, "腾讯云短信 apiKey 配置格式错误,请配置为[secretId sdkAppId]"); + Assert.isTrue(keys.length == 2, "腾讯云短信 apiKey 配置格式错误,请配置 为[secretId sdkAppId]"); Assert.notBlank(keys[0], "腾讯云短信 secretId 不能为空"); Assert.notBlank(keys[1], "腾讯云短信 sdkAppId 不能为空"); result.setSdkAppId(keys[1]).setApiKey(keys[0]); diff --git a/yudao-framework/yudao-spring-boot-starter-biz-sms/src/test/java/cn/iocoder/yudao/framework/sms/core/client/impl/tencent/TencentSmsClientTest.java b/yudao-framework/yudao-spring-boot-starter-biz-sms/src/test/java/cn/iocoder/yudao/framework/sms/core/client/impl/tencent/TencentSmsClientTest.java index 69834087a..ae23bf494 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-sms/src/test/java/cn/iocoder/yudao/framework/sms/core/client/impl/tencent/TencentSmsClientTest.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-sms/src/test/java/cn/iocoder/yudao/framework/sms/core/client/impl/tencent/TencentSmsClientTest.java @@ -38,7 +38,7 @@ import static org.mockito.Mockito.when; /** * {@link TencentSmsClient} 的单元测试 * - * @author : shiwp + * @author shiwp */ public class TencentSmsClientTest extends BaseMockitoUnitTest { @@ -89,7 +89,8 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest { assertEquals(mobile, request.getPhoneNumberSet()[0]); assertEquals(properties.getSignature(), request.getSignName()); assertEquals(apiTemplateId, request.getTemplateId()); - assertEquals(toJsonString(ArrayUtils.toArray(new ArrayList<>(MapUtils.convertMap(templateParams).values()), String::valueOf)), toJsonString(request.getTemplateParamSet())); + assertEquals(toJsonString(ArrayUtils.toArray(new ArrayList<>(MapUtils.convertMap(templateParams).values()), String::valueOf)), + toJsonString(request.getTemplateParamSet())); assertEquals(sendLogId, ReflectUtil.getFieldValue(JsonUtils.parseObject(request.getSessionContext(), TencentSmsClient.SessionContext.class), "logId")); return true; }))).thenReturn(response); @@ -173,6 +174,7 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest { assertEquals(response.getDescribeTemplateStatusSet()[0].getReviewReply(), result.getData().getAuditReason()); } + // TODO @FinallySays:这个单测,按道理说应该是写成 4 个方法,每个对应一种情况。 @Test public void testConvertTemplateStatusDTO() { testTemplateStatus(SmsTemplateAuditStatusEnum.SUCCESS, 0L); @@ -181,6 +183,7 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest { DescribeTemplateListStatus templateStatus = new DescribeTemplateListStatus(); templateStatus.setStatusCode(3L); Long templateId = randomLongId(); + // 调用,并断言结果 assertThrows(IllegalStateException.class, () -> smsClient.convertTemplateStatusDTO(templateStatus), StrUtil.format("不能解析短信模版审核状态[3],模版id[{}]", templateId)); } @@ -192,5 +195,4 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest { assertEquals(expected.getStatus(), result.getAuditStatus()); } - -} \ No newline at end of file +}