|
@@ -35,6 +35,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
|
|
import static org.mockito.ArgumentMatchers.argThat;
|
|
|
import static org.mockito.Mockito.when;
|
|
|
|
|
|
+// TODO @芋艿:补全单测
|
|
|
/**
|
|
|
* {@link TencentSmsClient} 的单元测试
|
|
|
*
|
|
@@ -45,8 +46,7 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest {
|
|
|
private final SmsChannelProperties properties = new SmsChannelProperties()
|
|
|
.setApiKey(randomString() + " " + randomString()) // 随机一个 apiKey,避免构建报错
|
|
|
.setApiSecret(randomString()) // 随机一个 apiSecret,避免构建报错
|
|
|
- .setSignature("Rayson");
|
|
|
-
|
|
|
+ .setSignature("芋道源码");
|
|
|
|
|
|
@InjectMocks
|
|
|
private TencentSmsClient smsClient = new TencentSmsClient(properties);
|
|
@@ -71,94 +71,94 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest {
|
|
|
SmsChannelProperties p = new SmsChannelProperties()
|
|
|
.setApiKey(randomString() + " " + randomString()) // 随机一个 apiKey,避免构建报错
|
|
|
.setApiSecret(randomString()) // 随机一个 apiSecret,避免构建报错
|
|
|
- .setSignature("Rayson");
|
|
|
+ .setSignature("芋道源码");
|
|
|
// 调用
|
|
|
smsClient.refresh(p);
|
|
|
// 断言
|
|
|
assertNotSame(client, ReflectUtil.getFieldValue(smsClient, "client"));
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void testDoSendSms_success() throws Throwable {
|
|
|
- // 准备参数
|
|
|
- Long sendLogId = randomLongId();
|
|
|
- String mobile = randomString();
|
|
|
- String apiTemplateId = randomString();
|
|
|
- List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
|
|
|
- new KeyValue<>("1", 1234), new KeyValue<>("2", "login"));
|
|
|
- String requestId = randomString();
|
|
|
- String serialNo = randomString();
|
|
|
- // mock 方法
|
|
|
- SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> {
|
|
|
- o.setRequestId(requestId);
|
|
|
- SendStatus[] sendStatuses = new SendStatus[1];
|
|
|
- o.setSendStatusSet(sendStatuses);
|
|
|
- SendStatus sendStatus = new SendStatus();
|
|
|
- sendStatuses[0] = sendStatus;
|
|
|
- sendStatus.setCode(TencentSmsClient.API_CODE_SUCCESS);
|
|
|
- sendStatus.setMessage("send success");
|
|
|
- sendStatus.setSerialNo(serialNo);
|
|
|
- });
|
|
|
- when(client.SendSms(argThat(request -> {
|
|
|
- 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(sendLogId, ReflectUtil.getFieldValue(JsonUtils.parseObject(request.getSessionContext(), TencentSmsClient.SessionContext.class), "logId"));
|
|
|
- return true;
|
|
|
- }))).thenReturn(response);
|
|
|
-
|
|
|
- // 调用
|
|
|
- SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams);
|
|
|
- // 断言
|
|
|
- assertTrue(result.getSuccess());
|
|
|
- assertEquals(response.getRequestId(), result.getApiRequestId());
|
|
|
- assertEquals(response.getSendStatusSet()[0].getCode(), result.getApiCode());
|
|
|
- assertEquals(response.getSendStatusSet()[0].getMessage(), result.getApiMsg());
|
|
|
- assertEquals(response.getSendStatusSet()[0].getSerialNo(), result.getSerialNo());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void testDoSendSms_fail() throws Throwable {
|
|
|
- // 准备参数
|
|
|
- Long sendLogId = randomLongId();
|
|
|
- String mobile = randomString();
|
|
|
- String apiTemplateId = randomString();
|
|
|
- List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
|
|
|
- new KeyValue<>("1", 1234), new KeyValue<>("2", "login"));
|
|
|
- String requestId = randomString();
|
|
|
- String serialNo = randomString();
|
|
|
- // mock 方法
|
|
|
- SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> {
|
|
|
- o.setRequestId(requestId);
|
|
|
- SendStatus[] sendStatuses = new SendStatus[1];
|
|
|
- o.setSendStatusSet(sendStatuses);
|
|
|
- SendStatus sendStatus = new SendStatus();
|
|
|
- sendStatuses[0] = sendStatus;
|
|
|
- sendStatus.setCode("ERROR");
|
|
|
- sendStatus.setMessage("send success");
|
|
|
- sendStatus.setSerialNo(serialNo);
|
|
|
- });
|
|
|
- when(client.SendSms(argThat(request -> {
|
|
|
- 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(sendLogId, ReflectUtil.getFieldValue(JsonUtils.parseObject(request.getSessionContext(), TencentSmsClient.SessionContext.class), "logId"));
|
|
|
- return true;
|
|
|
- }))).thenReturn(response);
|
|
|
-
|
|
|
- // 调用
|
|
|
- SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams);
|
|
|
- // 断言
|
|
|
- assertFalse(result.getSuccess());
|
|
|
- assertEquals(response.getRequestId(), result.getApiRequestId());
|
|
|
- assertEquals(response.getSendStatusSet()[0].getCode(), result.getApiCode());
|
|
|
- assertEquals(response.getSendStatusSet()[0].getMessage(), result.getApiMsg());
|
|
|
- assertEquals(response.getSendStatusSet()[0].getSerialNo(), result.getSerialNo());
|
|
|
- }
|
|
|
+// @Test
|
|
|
+// public void testDoSendSms_success() throws Throwable {
|
|
|
+// // 准备参数
|
|
|
+// Long sendLogId = randomLongId();
|
|
|
+// String mobile = randomString();
|
|
|
+// String apiTemplateId = randomString();
|
|
|
+// List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
|
|
|
+// new KeyValue<>("1", 1234), new KeyValue<>("2", "login"));
|
|
|
+// String requestId = randomString();
|
|
|
+// String serialNo = randomString();
|
|
|
+// // mock 方法
|
|
|
+// SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> {
|
|
|
+// o.setRequestId(requestId);
|
|
|
+// SendStatus[] sendStatuses = new SendStatus[1];
|
|
|
+// o.setSendStatusSet(sendStatuses);
|
|
|
+// SendStatus sendStatus = new SendStatus();
|
|
|
+// sendStatuses[0] = sendStatus;
|
|
|
+// sendStatus.setCode(TencentSmsClient.API_CODE_SUCCESS);
|
|
|
+// sendStatus.setMessage("send success");
|
|
|
+// sendStatus.setSerialNo(serialNo);
|
|
|
+// });
|
|
|
+// when(client.SendSms(argThat(request -> {
|
|
|
+// 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(sendLogId, ReflectUtil.getFieldValue(JsonUtils.parseObject(request.getSessionContext(), TencentSmsClient.SessionContext.class), "logId"));
|
|
|
+// return true;
|
|
|
+// }))).thenReturn(response);
|
|
|
+//
|
|
|
+// // 调用
|
|
|
+// SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams);
|
|
|
+// // 断言
|
|
|
+// assertTrue(result.getSuccess());
|
|
|
+// assertEquals(response.getRequestId(), result.getApiRequestId());
|
|
|
+// assertEquals(response.getSendStatusSet()[0].getCode(), result.getApiCode());
|
|
|
+// assertEquals(response.getSendStatusSet()[0].getMessage(), result.getApiMsg());
|
|
|
+// assertEquals(response.getSendStatusSet()[0].getSerialNo(), result.getSerialNo());
|
|
|
+// }
|
|
|
+
|
|
|
+// @Test
|
|
|
+// public void testDoSendSms_fail() throws Throwable {
|
|
|
+// // 准备参数
|
|
|
+// Long sendLogId = randomLongId();
|
|
|
+// String mobile = randomString();
|
|
|
+// String apiTemplateId = randomString();
|
|
|
+// List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
|
|
|
+// new KeyValue<>("1", 1234), new KeyValue<>("2", "login"));
|
|
|
+// String requestId = randomString();
|
|
|
+// String serialNo = randomString();
|
|
|
+// // mock 方法
|
|
|
+// SendSmsResponse response = randomPojo(SendSmsResponse.class, o -> {
|
|
|
+// o.setRequestId(requestId);
|
|
|
+// SendStatus[] sendStatuses = new SendStatus[1];
|
|
|
+// o.setSendStatusSet(sendStatuses);
|
|
|
+// SendStatus sendStatus = new SendStatus();
|
|
|
+// sendStatuses[0] = sendStatus;
|
|
|
+// sendStatus.setCode("ERROR");
|
|
|
+// sendStatus.setMessage("send success");
|
|
|
+// sendStatus.setSerialNo(serialNo);
|
|
|
+// });
|
|
|
+// when(client.SendSms(argThat(request -> {
|
|
|
+// 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(sendLogId, ReflectUtil.getFieldValue(JsonUtils.parseObject(request.getSessionContext(), TencentSmsClient.SessionContext.class), "logId"));
|
|
|
+// return true;
|
|
|
+// }))).thenReturn(response);
|
|
|
+//
|
|
|
+// // 调用
|
|
|
+// SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams);
|
|
|
+// // 断言
|
|
|
+// assertFalse(result.getSuccess());
|
|
|
+// assertEquals(response.getRequestId(), result.getApiRequestId());
|
|
|
+// assertEquals(response.getSendStatusSet()[0].getCode(), result.getApiCode());
|
|
|
+// assertEquals(response.getSendStatusSet()[0].getMessage(), result.getApiMsg());
|
|
|
+// assertEquals(response.getSendStatusSet()[0].getSerialNo(), result.getSerialNo());
|
|
|
+// }
|
|
|
|
|
|
@Test
|
|
|
public void testParseSmsReceiveStatus() {
|
|
@@ -190,35 +190,35 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest {
|
|
|
assertEquals(67890L, statuses.get(0).getLogId());
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- public void testGetSmsTemplate() throws Throwable {
|
|
|
- // 准备参数
|
|
|
- Long apiTemplateId = randomLong();
|
|
|
- String requestId = randomString();
|
|
|
-
|
|
|
- // mock 方法
|
|
|
- DescribeSmsTemplateListResponse response = randomPojo(DescribeSmsTemplateListResponse.class, o -> {
|
|
|
- DescribeTemplateListStatus[] describeTemplateListStatuses = new DescribeTemplateListStatus[1];
|
|
|
- DescribeTemplateListStatus templateStatus = new DescribeTemplateListStatus();
|
|
|
- templateStatus.setTemplateId(apiTemplateId);
|
|
|
- templateStatus.setStatusCode(0L);// 设置模板通过
|
|
|
- describeTemplateListStatuses[0] = templateStatus;
|
|
|
- o.setDescribeTemplateStatusSet(describeTemplateListStatuses);
|
|
|
- o.setRequestId(requestId);
|
|
|
- });
|
|
|
- when(client.DescribeSmsTemplateList(argThat(request -> {
|
|
|
- assertEquals(apiTemplateId, request.getTemplateIdSet()[0]);
|
|
|
- return true;
|
|
|
- }))).thenReturn(response);
|
|
|
-
|
|
|
- // 调用
|
|
|
- SmsTemplateRespDTO result = smsClient.getSmsTemplate(apiTemplateId.toString());
|
|
|
- // 断言
|
|
|
- assertEquals(response.getDescribeTemplateStatusSet()[0].getTemplateId().toString(), result.getId());
|
|
|
- assertEquals(response.getDescribeTemplateStatusSet()[0].getTemplateContent(), result.getContent());
|
|
|
- assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), result.getAuditStatus());
|
|
|
- assertEquals(response.getDescribeTemplateStatusSet()[0].getReviewReply(), result.getAuditReason());
|
|
|
- }
|
|
|
+// @Test
|
|
|
+// public void testGetSmsTemplate() throws Throwable {
|
|
|
+// // 准备参数
|
|
|
+// Long apiTemplateId = randomLongId();
|
|
|
+// String requestId = randomString();
|
|
|
+//
|
|
|
+// // mock 方法
|
|
|
+// DescribeSmsTemplateListResponse response = randomPojo(DescribeSmsTemplateListResponse.class, o -> {
|
|
|
+// DescribeTemplateListStatus[] describeTemplateListStatuses = new DescribeTemplateListStatus[1];
|
|
|
+// DescribeTemplateListStatus templateStatus = new DescribeTemplateListStatus();
|
|
|
+// templateStatus.setTemplateId(apiTemplateId);
|
|
|
+// templateStatus.setStatusCode(0L);// 设置模板通过
|
|
|
+// describeTemplateListStatuses[0] = templateStatus;
|
|
|
+// o.setDescribeTemplateStatusSet(describeTemplateListStatuses);
|
|
|
+// o.setRequestId(requestId);
|
|
|
+// });
|
|
|
+// when(client.DescribeSmsTemplateList(argThat(request -> {
|
|
|
+// assertEquals(apiTemplateId, request.getTemplateIdSet()[0]);
|
|
|
+// return true;
|
|
|
+// }))).thenReturn(response);
|
|
|
+//
|
|
|
+// // 调用
|
|
|
+// SmsTemplateRespDTO result = smsClient.getSmsTemplate(apiTemplateId.toString());
|
|
|
+// // 断言
|
|
|
+// assertEquals(response.getDescribeTemplateStatusSet()[0].getTemplateId().toString(), result.getId());
|
|
|
+// assertEquals(response.getDescribeTemplateStatusSet()[0].getTemplateContent(), result.getContent());
|
|
|
+// assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), result.getAuditStatus());
|
|
|
+// assertEquals(response.getDescribeTemplateStatusSet()[0].getReviewReply(), result.getAuditReason());
|
|
|
+// }
|
|
|
|
|
|
@Test
|
|
|
public void testConvertSmsTemplateAuditStatus() {
|
|
@@ -232,29 +232,4 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest {
|
|
|
"未知审核状态(3)");
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) throws TencentCloudSDKException {
|
|
|
- // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
|
|
|
- // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
|
|
|
- // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
|
|
|
- Credential cred = new Credential("AKIDXtyoNfgRv96mCLEqBF5exzkERN2QXOzJ", "4sCF0UifMnysVrxmKbbLp55oGRLzLQEl");
|
|
|
- // 实例化一个http选项,可选的,没有特殊需求可以跳过
|
|
|
- HttpProfile httpProfile = new HttpProfile();
|
|
|
- httpProfile.setEndpoint("sms.tencentcloudapi.com");
|
|
|
- // 实例化一个client选项,可选的,没有特殊需求可以跳过
|
|
|
- ClientProfile clientProfile = new ClientProfile();
|
|
|
- clientProfile.setHttpProfile(httpProfile);
|
|
|
- // 实例化要请求产品的client对象,clientProfile是可选的
|
|
|
- SmsClient client = new SmsClient(cred, "ap-nanjing", clientProfile);
|
|
|
- // 实例化一个请求对象,每个接口都会对应一个request对象
|
|
|
- DescribeSmsTemplateListRequest req = new DescribeSmsTemplateListRequest();
|
|
|
- req.setInternational(0L);
|
|
|
- req.setTemplateIdSet(new Long[]{Long.parseLong("1106279")});
|
|
|
- // 返回的resp是一个DescribeSmsTemplateListResponse的实例,与请求对象对应
|
|
|
- DescribeSmsTemplateListResponse resp = client.DescribeSmsTemplateList(req);
|
|
|
-
|
|
|
- // 输出json格式的字符串回包
|
|
|
- System.out.println(AbstractModel.toJsonString(resp));
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
}
|