code review 腾讯短信的实现,基本没啥问题了

This commit is contained in:
YunaiV 2022-04-07 23:53:19 +08:00
parent 91f758c295
commit 2e66845584
2 changed files with 14 additions and 9 deletions

View File

@ -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);
// 实例化一个认证对象入参需要传入腾讯云账户密钥对 secretIdsecretKey
// 实例化一个认证对象入参需要传入腾讯云账户密钥对 secretIdsecretKey
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]);

View File

@ -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());
}
}