【同步】BOOT 和 CLOUD 的功能
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
package cn.iocoder.yudao.module.system.framework.sms.core.client.impl;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.core.KeyValue;
|
||||
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
||||
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO;
|
||||
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO;
|
||||
import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.anyMap;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
|
||||
/**
|
||||
* {@link HuaweiSmsClient} 的单元测试
|
||||
*
|
||||
* @author scholar
|
||||
*/
|
||||
public class HuaweiSmsClientTest extends BaseMockitoUnitTest {
|
||||
|
||||
private final SmsChannelProperties properties = new SmsChannelProperties()
|
||||
.setApiKey(randomString() + " " + randomString()) // 随机一个 apiKey,避免构建报错
|
||||
.setApiSecret(randomString()) // 随机一个 apiSecret,避免构建报错
|
||||
.setSignature("芋道源码");
|
||||
|
||||
@InjectMocks
|
||||
private HuaweiSmsClient smsClient = new HuaweiSmsClient(properties);
|
||||
|
||||
@Test
|
||||
public void testDoSendSms_success() throws Throwable {
|
||||
try (MockedStatic<HttpUtils> httpUtilsMockedStatic = mockStatic(HttpUtils.class)) {
|
||||
// 准备参数
|
||||
Long sendLogId = randomLongId();
|
||||
String mobile = randomString();
|
||||
String apiTemplateId = randomString() + " " + randomString();
|
||||
List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
|
||||
new KeyValue<>("1", 1234), new KeyValue<>("2", "login"));
|
||||
|
||||
// mock 方法
|
||||
httpUtilsMockedStatic.when(() -> HttpUtils.post(anyString(), anyMap(), anyString()))
|
||||
.thenReturn("{\"result\":[{\"originTo\":\"+86155****5678\",\"createTime\":\"2018-05-25T16:34:34Z\",\"from\":\"1069********0012\",\"smsMsgId\":\"d6e3cdd0-522b-4692-8304-a07553cdf591_8539659\",\"status\":\"000000\",\"countryId\":\"CN\",\"total\":2}],\"code\":\"000000\",\"description\":\"Success\"}\n");
|
||||
|
||||
// 调用
|
||||
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile,
|
||||
apiTemplateId, templateParams);
|
||||
// 断言
|
||||
assertTrue(result.getSuccess());
|
||||
assertEquals("d6e3cdd0-522b-4692-8304-a07553cdf591_8539659", result.getSerialNo());
|
||||
assertEquals("000000", result.getApiCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoSendSms_fail_01() throws Throwable {
|
||||
try (MockedStatic<HttpUtils> httpUtilsMockedStatic = mockStatic(HttpUtils.class)) {
|
||||
// 准备参数
|
||||
Long sendLogId = randomLongId();
|
||||
String mobile = randomString();
|
||||
String apiTemplateId = randomString() + " " + randomString();
|
||||
List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
|
||||
new KeyValue<>("1", 1234), new KeyValue<>("2", "login"));
|
||||
|
||||
// mock 方法
|
||||
httpUtilsMockedStatic.when(() -> HttpUtils.post(anyString(), anyMap(), anyString()))
|
||||
.thenReturn("{\"result\":[{\"total\":1,\"originTo\":\"17321315478\",\"createTime\":\"2024-08-18T11:32:20Z\",\"from\":\"x8824060312575\",\"smsMsgId\":\"06e4b966-ad87-479f-8b74-f57fb7aafb60_304613461\",\"countryId\":\"CN\",\"status\":\"E200033\"}],\"code\":\"E000510\",\"description\":\"The SMS fails to be sent. For details, see status.\"}");
|
||||
|
||||
// 调用
|
||||
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile,
|
||||
apiTemplateId, templateParams);
|
||||
// 断言
|
||||
assertFalse(result.getSuccess());
|
||||
assertEquals("06e4b966-ad87-479f-8b74-f57fb7aafb60_304613461", result.getSerialNo());
|
||||
assertEquals("E200033", result.getApiCode());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoSendSms_fail_02() throws Throwable {
|
||||
try (MockedStatic<HttpUtils> httpUtilsMockedStatic = mockStatic(HttpUtils.class)) {
|
||||
// 准备参数
|
||||
Long sendLogId = randomLongId();
|
||||
String mobile = randomString();
|
||||
String apiTemplateId = randomString() + " " + randomString();
|
||||
List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
|
||||
new KeyValue<>("1", 1234), new KeyValue<>("2", "login"));
|
||||
|
||||
// mock 方法
|
||||
httpUtilsMockedStatic.when(() -> HttpUtils.post(anyString(), anyMap(), anyString()))
|
||||
.thenReturn("{\"code\":\"E000102\",\"description\":\"Invalid app_key.\"}");
|
||||
|
||||
// 调用
|
||||
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile,
|
||||
apiTemplateId, templateParams);
|
||||
// 断言
|
||||
assertFalse(result.getSuccess());
|
||||
assertEquals("E000102", result.getApiCode());
|
||||
assertEquals("Invalid app_key.", result.getApiMsg());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseSmsReceiveStatus() {
|
||||
// 准备参数
|
||||
String text = "sequence=1&total=1&statusDesc=%E7%94%A8%E6%88%B7%E5%B7%B2%E6%88%90%E5%8A%9F%E6%94%B6%E5%88%B0%E7%9F%AD%E4%BF%A1&updateTime=2024-08-15T03%3A00%3A34Z&source=2&smsMsgId=70207ed7-1d02-41b0-8537-bb25fd1c2364_143684459&status=DELIVRD&extend=176";
|
||||
|
||||
// 调用
|
||||
List<SmsReceiveRespDTO> statuses = smsClient.parseSmsReceiveStatus(text);
|
||||
// 断言
|
||||
assertEquals(1, statuses.size());
|
||||
SmsReceiveRespDTO status = statuses.get(0);
|
||||
assertTrue(status.getSuccess());
|
||||
assertEquals("DELIVRD", status.getErrorCode());
|
||||
assertEquals(LocalDateTime.of(2024, 8, 15, 3, 0, 34), status.getReceiveTime());
|
||||
assertEquals("70207ed7-1d02-41b0-8537-bb25fd1c2364_143684459", status.getSerialNo());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package cn.iocoder.yudao.module.system.framework.sms.core.client.impl;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.core.KeyValue;
|
||||
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
|
||||
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
||||
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO;
|
||||
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO;
|
||||
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO;
|
||||
import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum;
|
||||
import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.MockedStatic;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomString;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.anyMap;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
|
||||
/**
|
||||
* {@link QiniuSmsClient} 的单元测试
|
||||
*
|
||||
* @author scholar
|
||||
*/
|
||||
public class QiniuSmsClientTest extends BaseMockitoUnitTest {
|
||||
|
||||
private final SmsChannelProperties properties = new SmsChannelProperties()
|
||||
.setApiKey(randomString())// 随机一个 apiKey,避免构建报错
|
||||
.setApiSecret(randomString()) // 随机一个 apiSecret,避免构建报错
|
||||
.setSignature("芋道源码");
|
||||
|
||||
@InjectMocks
|
||||
private QiniuSmsClient smsClient = new QiniuSmsClient(properties);
|
||||
|
||||
@Test
|
||||
public void testDoSendSms_success() throws Throwable {
|
||||
try (MockedStatic<HttpUtils> httpUtilsMockedStatic = mockStatic(HttpUtils.class)) {
|
||||
// 准备参数
|
||||
Long sendLogId = randomLongId();
|
||||
String mobile = randomString();
|
||||
String apiTemplateId = randomString() + " " + randomString();
|
||||
List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
|
||||
new KeyValue<>("1", 1234), new KeyValue<>("2", "login"));
|
||||
// mock 方法
|
||||
httpUtilsMockedStatic.when(() -> HttpUtils.post(anyString(), anyMap(), anyString()))
|
||||
.thenReturn("{\"message_id\":\"17245678901\"}");
|
||||
// 调用
|
||||
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile,
|
||||
apiTemplateId, templateParams);
|
||||
// 断言
|
||||
assertTrue(result.getSuccess());
|
||||
assertEquals("17245678901", result.getSerialNo());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoSendSms_fail() throws Throwable {
|
||||
try (MockedStatic<HttpUtils> httpUtilsMockedStatic = mockStatic(HttpUtils.class)) {
|
||||
// 准备参数
|
||||
Long sendLogId = randomLongId();
|
||||
String mobile = randomString();
|
||||
String apiTemplateId = randomString() + " " + randomString();
|
||||
List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
|
||||
new KeyValue<>("1", 1234), new KeyValue<>("2", "login"));
|
||||
// mock 方法
|
||||
httpUtilsMockedStatic.when(() -> HttpUtils.post(anyString(), anyMap(), anyString()))
|
||||
.thenReturn("{\"error\":\"BadToken\",\"message\":\"Your authorization token is invalid\",\"request_id\":\"etziWcJFo1C8Ne8X\"}");
|
||||
// 调用
|
||||
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile,
|
||||
apiTemplateId, templateParams);
|
||||
// 断言
|
||||
assertFalse(result.getSuccess());
|
||||
assertEquals("BadToken", result.getApiCode());
|
||||
assertEquals("Your authorization token is invalid", result.getApiMsg());
|
||||
assertEquals("etziWcJFo1C8Ne8X", result.getApiRequestId());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSmsTemplate() throws Throwable {
|
||||
try (MockedStatic<HttpUtils> httpUtilsMockedStatic = mockStatic(HttpUtils.class)) {
|
||||
// 准备参数
|
||||
String apiTemplateId = randomString();
|
||||
// mock 方法
|
||||
httpUtilsMockedStatic.when(() -> HttpUtils.get(anyString(), anyMap()))
|
||||
.thenReturn("{\"audit_status\":\"passed\",\"created_at\":1724231187,\"description\":\"\",\"disable_broadcast\":false,\"disable_broadcast_reason\":\"\",\"disable_reason\":\"\",\"disabled\":false,\"id\":\"1826184073773596672\",\"is_oversea\":false,\"name\":\"dd\",\"parameters\":[\"code\"],\"reject_reason\":\"\",\"signature_id\":\"1826099896017498112\",\"signature_text\":\"yudao\",\"template\":\"您的验证码为:${code}\",\"type\":\"verification\",\"uid\":1383022432,\"updated_at\":1724288561,\"variable_count\":0}");
|
||||
// 调用
|
||||
SmsTemplateRespDTO result = smsClient.getSmsTemplate(apiTemplateId);
|
||||
// 断言
|
||||
assertEquals("1826184073773596672", result.getId());
|
||||
assertEquals("您的验证码为:${code}", result.getContent());
|
||||
assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(), result.getAuditStatus());
|
||||
assertEquals("", result.getAuditReason());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseSmsReceiveStatus() {
|
||||
// 准备参数
|
||||
String text = "{\"items\":[{\"mobile\":\"18881234567\",\"message_id\":\"10135515063508004167\",\"status\":\"DELIVRD\",\"delivrd_at\":1724591666,\"error\":\"DELIVRD\",\"seq\":\"123\"}]}";
|
||||
// 调用
|
||||
List<SmsReceiveRespDTO> statuses = smsClient.parseSmsReceiveStatus(text);
|
||||
// 断言
|
||||
assertEquals(1, statuses.size());
|
||||
SmsReceiveRespDTO status = statuses.get(0);
|
||||
assertTrue(status.getSuccess());
|
||||
assertEquals("DELIVRD", status.getErrorMsg());
|
||||
assertEquals(LocalDateTime.of(2024, 8, 25, 21, 14, 26), status.getReceiveTime());
|
||||
assertEquals("18881234567", status.getMobile());
|
||||
assertEquals("10135515063508004167", status.getSerialNo());
|
||||
assertEquals(123, status.getLogId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvertSmsTemplateAuditStatus() {
|
||||
assertEquals(SmsTemplateAuditStatusEnum.SUCCESS.getStatus(),
|
||||
smsClient.convertSmsTemplateAuditStatus("passed"));
|
||||
assertEquals(SmsTemplateAuditStatusEnum.CHECKING.getStatus(),
|
||||
smsClient.convertSmsTemplateAuditStatus("reviewing"));
|
||||
assertEquals(SmsTemplateAuditStatusEnum.FAIL.getStatus(),
|
||||
smsClient.convertSmsTemplateAuditStatus("rejected"));
|
||||
assertThrows(IllegalArgumentException.class, () -> smsClient.convertSmsTemplateAuditStatus("unknown"),
|
||||
"未知审核状态(3)");
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.system.framework.sms.core.client.impl;
|
||||
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.iocoder.yudao.framework.common.core.KeyValue;
|
||||
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO;
|
||||
import cn.iocoder.yudao.module.system.framework.sms.core.client.SmsClient;
|
||||
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO;
|
||||
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO;
|
||||
import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties;
|
||||
@@ -12,7 +12,7 @@ import org.junit.jupiter.api.Test;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 各种 {@link SmsClientTests 集成测试
|
||||
* 各种 {@link SmsClient} 的集成测试
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@@ -24,8 +24,8 @@ public class SmsClientTests {
|
||||
@Disabled
|
||||
public void testAliyunSmsClient_getSmsTemplate() throws Throwable {
|
||||
SmsChannelProperties properties = new SmsChannelProperties()
|
||||
.setApiKey("LTAI5tAicJAxaSFiZuGGeXHR")
|
||||
.setApiSecret("Fdr9vadxnDvS6GJU0W1tijQ0VmLhYz");
|
||||
.setApiKey(System.getenv("SMS_ALIYUN_ACCESS_KEY"))
|
||||
.setApiSecret(System.getenv("SMS_ALIYUN_SECRET_KEY"));
|
||||
AliyunSmsClient client = new AliyunSmsClient(properties);
|
||||
// 准备参数
|
||||
String apiTemplateId = "SMS_207945135";
|
||||
@@ -39,9 +39,9 @@ public class SmsClientTests {
|
||||
@Disabled
|
||||
public void testAliyunSmsClient_sendSms() throws Throwable {
|
||||
SmsChannelProperties properties = new SmsChannelProperties()
|
||||
.setApiKey("LTAI5tAicJAxaSFiZuGGeXHR")
|
||||
.setApiSecret("Fdr9vadxnDvS6GJU0W1tijQ0VmLhYz")
|
||||
.setSignature("runpu");
|
||||
.setApiKey(System.getenv("SMS_ALIYUN_ACCESS_KEY"))
|
||||
.setApiSecret(System.getenv("SMS_ALIYUN_SECRET_KEY"))
|
||||
.setSignature("Ballcat");
|
||||
AliyunSmsClient client = new AliyunSmsClient(properties);
|
||||
// 准备参数
|
||||
Long sendLogId = System.currentTimeMillis();
|
||||
@@ -53,49 +53,21 @@ public class SmsClientTests {
|
||||
System.out.println(sendRespDTO);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void testAliyunSmsClient_parseSmsReceiveStatus() {
|
||||
SmsChannelProperties properties = new SmsChannelProperties()
|
||||
.setApiKey("LTAI5tAicJAxaSFiZuGGeXHR")
|
||||
.setApiSecret("Fdr9vadxnDvS6GJU0W1tijQ0VmLhYz");
|
||||
AliyunSmsClient client = new AliyunSmsClient(properties);
|
||||
// 准备参数
|
||||
String text = "[\n" +
|
||||
" {\n" +
|
||||
" \"phone_number\" : \"13900000001\",\n" +
|
||||
" \"send_time\" : \"2017-01-01 11:12:13\",\n" +
|
||||
" \"report_time\" : \"2017-02-02 22:23:24\",\n" +
|
||||
" \"success\" : true,\n" +
|
||||
" \"err_code\" : \"DELIVERED\",\n" +
|
||||
" \"err_msg\" : \"用户接收成功\",\n" +
|
||||
" \"sms_size\" : \"1\",\n" +
|
||||
" \"biz_id\" : \"12345\",\n" +
|
||||
" \"out_id\" : \"67890\"\n" +
|
||||
" }\n" +
|
||||
"]";
|
||||
// mock 方法
|
||||
|
||||
// 调用
|
||||
List<SmsReceiveRespDTO> statuses = client.parseSmsReceiveStatus(text);
|
||||
// 打印结果
|
||||
System.out.println(statuses);
|
||||
}
|
||||
|
||||
// ========== 腾讯云 ==========
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void testTencentSmsClient_sendSms() throws Throwable {
|
||||
String sdkAppId = "1400500458";
|
||||
SmsChannelProperties properties = new SmsChannelProperties()
|
||||
.setApiKey("LTAI5tAicJAxaSFiZuGGeXHR 1428926523")
|
||||
.setApiSecret("Fdr9vadxnDvS6GJU0W1tijQ0VmLhYz")
|
||||
.setApiKey(System.getenv("SMS_TENCENT_ACCESS_KEY") + " " + sdkAppId)
|
||||
.setApiSecret(System.getenv("SMS_TENCENT_SECRET_KEY"))
|
||||
.setSignature("芋道源码");
|
||||
TencentSmsClient client = new TencentSmsClient(properties);
|
||||
// 准备参数
|
||||
Long sendLogId = System.currentTimeMillis();
|
||||
String mobile = "15601691323";
|
||||
String apiTemplateId = "2136358";
|
||||
String apiTemplateId = "358212";
|
||||
// 调用
|
||||
SmsSendRespDTO sendRespDTO = client.sendSms(sendLogId, mobile, apiTemplateId, ListUtil.of(new KeyValue<>("code", "1024")));
|
||||
// 打印结果
|
||||
@@ -105,13 +77,14 @@ public class SmsClientTests {
|
||||
@Test
|
||||
@Disabled
|
||||
public void testTencentSmsClient_getSmsTemplate() throws Throwable {
|
||||
String sdkAppId = "1400500458";
|
||||
SmsChannelProperties properties = new SmsChannelProperties()
|
||||
.setApiKey("LTAI5tAicJAxaSFiZuGGeXHR 1428926523")
|
||||
.setApiSecret("Fdr9vadxnDvS6GJU0W1tijQ0VmLhYz")
|
||||
.setApiKey(System.getenv("SMS_TENCENT_ACCESS_KEY") + " " + sdkAppId)
|
||||
.setApiSecret(System.getenv("SMS_TENCENT_SECRET_KEY"))
|
||||
.setSignature("芋道源码");
|
||||
TencentSmsClient client = new TencentSmsClient(properties);
|
||||
// 准备参数
|
||||
String apiTemplateId = "2136358";
|
||||
String apiTemplateId = "358212";
|
||||
// 调用
|
||||
SmsTemplateRespDTO template = client.getSmsTemplate(apiTemplateId);
|
||||
// 打印结果
|
||||
@@ -123,15 +96,16 @@ public class SmsClientTests {
|
||||
@Test
|
||||
@Disabled
|
||||
public void testHuaweiSmsClient_sendSms() throws Throwable {
|
||||
String sender = "x8824060312575";
|
||||
SmsChannelProperties properties = new SmsChannelProperties()
|
||||
.setApiKey("123")
|
||||
.setApiSecret("456")
|
||||
.setApiKey(System.getenv("SMS_HUAWEI_ACCESS_KEY") + " " + sender)
|
||||
.setApiSecret(System.getenv("SMS_HUAWEI_SECRET_KEY"))
|
||||
.setSignature("runpu");
|
||||
HuaweiSmsClient client = new HuaweiSmsClient(properties);
|
||||
// 准备参数
|
||||
Long sendLogId = System.currentTimeMillis();
|
||||
String mobile = "15601691323";
|
||||
String apiTemplateId = "xx test01";
|
||||
String mobile = "17321315478";
|
||||
String apiTemplateId = "3644cdab863546a3b718d488659a99ef";
|
||||
List<KeyValue<String, Object>> templateParams = ListUtil.of(new KeyValue<>("code", "1024"));
|
||||
// 调用
|
||||
SmsSendRespDTO smsSendRespDTO = client.sendSms(sendLogId, mobile, apiTemplateId, templateParams);
|
||||
@@ -139,5 +113,39 @@ public class SmsClientTests {
|
||||
System.out.println(smsSendRespDTO);
|
||||
}
|
||||
|
||||
// ========== 七牛云 ==========
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void testQiniuSmsClient_sendSms() throws Throwable {
|
||||
SmsChannelProperties properties = new SmsChannelProperties()
|
||||
.setApiKey("SMS_QINIU_ACCESS_KEY")
|
||||
.setApiSecret("SMS_QINIU_SECRET_KEY");
|
||||
QiniuSmsClient client = new QiniuSmsClient(properties);
|
||||
// 准备参数
|
||||
Long sendLogId = System.currentTimeMillis();
|
||||
String mobile = "17321315478";
|
||||
String apiTemplateId = "3644cdab863546a3b718d488659a99ef";
|
||||
List<KeyValue<String, Object>> templateParams = ListUtil.of(new KeyValue<>("code", "1122"));
|
||||
// 调用
|
||||
SmsSendRespDTO smsSendRespDTO = client.sendSms(sendLogId, mobile, apiTemplateId, templateParams);
|
||||
// 打印结果
|
||||
System.out.println(smsSendRespDTO);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void testQiniuSmsClient_getSmsTemplate() throws Throwable {
|
||||
SmsChannelProperties properties = new SmsChannelProperties()
|
||||
.setApiKey("SMS_QINIU_ACCESS_KEY")
|
||||
.setApiSecret("SMS_QINIU_SECRET_KEY");
|
||||
QiniuSmsClient client = new QiniuSmsClient(properties);
|
||||
// 准备参数
|
||||
String apiTemplateId = "3644cdab863546a3b718d488659a99ef";
|
||||
// 调用
|
||||
SmsTemplateRespDTO template = client.getSmsTemplate(apiTemplateId);
|
||||
// 打印结果
|
||||
System.out.println(template);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoSendSms_fail() throws Throwable {
|
||||
public void testDoSendSms_fail_01() throws Throwable {
|
||||
try (MockedStatic<HttpUtils> httpUtilsMockedStatic = mockStatic(HttpUtils.class)) {
|
||||
// 准备参数
|
||||
Long sendLogId = randomLongId();
|
||||
@@ -117,6 +117,31 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoSendSms_fail_02() throws Throwable {
|
||||
try (MockedStatic<HttpUtils> httpUtilsMockedStatic = mockStatic(HttpUtils.class)) {
|
||||
// 准备参数
|
||||
Long sendLogId = randomLongId();
|
||||
String mobile = randomString();
|
||||
String apiTemplateId = randomString();
|
||||
List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
|
||||
new KeyValue<>("1", 1234), new KeyValue<>("2", "login"));
|
||||
|
||||
// mock 方法
|
||||
httpUtilsMockedStatic.when(() -> HttpUtils.post(anyString(), anyMap(), anyString()))
|
||||
.thenReturn("{\"Response\":{\"Error\":{\"Code\":\"AuthFailure.SecretIdNotFound\",\"Message\":\"The SecretId is not found, please ensure that your SecretId is correct.\"},\"RequestId\":\"2a88f82a-261c-4ac6-9fa9-c7d01aaa486a\"}}");
|
||||
|
||||
// 调用
|
||||
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile,
|
||||
apiTemplateId, templateParams);
|
||||
// 断言
|
||||
assertFalse(result.getSuccess());
|
||||
assertEquals("2a88f82a-261c-4ac6-9fa9-c7d01aaa486a", result.getApiRequestId());
|
||||
assertEquals("AuthFailure.SecretIdNotFound", result.getApiCode());
|
||||
assertEquals("The SecretId is not found, please ensure that your SecretId is correct.", result.getApiMsg());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseSmsReceiveStatus() {
|
||||
// 准备参数
|
||||
|
||||
@@ -144,7 +144,7 @@ public class OAuth2TokenServiceImplTest extends BaseDbAndRedisUnitTest {
|
||||
// 调用,并断言
|
||||
assertServiceException(() -> oauth2TokenService.refreshAccessToken(refreshToken, clientId),
|
||||
new ErrorCode(401, "刷新令牌已过期"));
|
||||
assertEquals(0, oauth2RefreshTokenMapper.selectCount());
|
||||
assertEquals(0, oauth2AccessTokenMapper.selectCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -57,9 +57,6 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
|
||||
// 校验记录的属性是否正确
|
||||
SmsChannelDO smsChannel = smsChannelMapper.selectById(smsChannelId);
|
||||
assertPojoEquals(reqVO, smsChannel, "id");
|
||||
// 断言 cache
|
||||
assertNull(smsChannelService.getIdClientCache().getIfPresent(smsChannel.getId()));
|
||||
assertNull(smsChannelService.getCodeClientCache().getIfPresent(smsChannel.getCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -79,9 +76,6 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
|
||||
// 校验是否更新正确
|
||||
SmsChannelDO smsChannel = smsChannelMapper.selectById(reqVO.getId()); // 获取最新的
|
||||
assertPojoEquals(reqVO, smsChannel);
|
||||
// 断言 cache
|
||||
assertNull(smsChannelService.getIdClientCache().getIfPresent(smsChannel.getId()));
|
||||
assertNull(smsChannelService.getCodeClientCache().getIfPresent(smsChannel.getCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -105,9 +99,6 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
|
||||
smsChannelService.deleteSmsChannel(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(smsChannelMapper.selectById(id));
|
||||
// 断言 cache
|
||||
assertNull(smsChannelService.getIdClientCache().getIfPresent(dbSmsChannel.getId()));
|
||||
assertNull(smsChannelService.getCodeClientCache().getIfPresent(dbSmsChannel.getCode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -164,31 +155,31 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
|
||||
|
||||
@Test
|
||||
public void testGetSmsChannelPage() {
|
||||
// mock 数据
|
||||
SmsChannelDO dbSmsChannel = randomPojo(SmsChannelDO.class, o -> { // 等会查询到
|
||||
o.setSignature("芋道源码");
|
||||
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
o.setCreateTime(buildTime(2020, 12, 12));
|
||||
});
|
||||
smsChannelMapper.insert(dbSmsChannel);
|
||||
// 测试 signature 不匹配
|
||||
smsChannelMapper.insert(cloneIgnoreId(dbSmsChannel, o -> o.setSignature("源码")));
|
||||
// 测试 status 不匹配
|
||||
smsChannelMapper.insert(cloneIgnoreId(dbSmsChannel, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
// 测试 createTime 不匹配
|
||||
smsChannelMapper.insert(cloneIgnoreId(dbSmsChannel, o -> o.setCreateTime(buildTime(2020, 11, 11))));
|
||||
// 准备参数
|
||||
SmsChannelPageReqVO reqVO = new SmsChannelPageReqVO();
|
||||
reqVO.setSignature("芋道");
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
reqVO.setCreateTime(buildBetweenTime(2020, 12, 1, 2020, 12, 24));
|
||||
// mock 数据
|
||||
SmsChannelDO dbSmsChannel = randomPojo(SmsChannelDO.class, o -> { // 等会查询到
|
||||
o.setSignature("芋道源码");
|
||||
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
o.setCreateTime(buildTime(2020, 12, 12));
|
||||
});
|
||||
smsChannelMapper.insert(dbSmsChannel);
|
||||
// 测试 signature 不匹配
|
||||
smsChannelMapper.insert(cloneIgnoreId(dbSmsChannel, o -> o.setSignature("源码")));
|
||||
// 测试 status 不匹配
|
||||
smsChannelMapper.insert(cloneIgnoreId(dbSmsChannel, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
|
||||
// 测试 createTime 不匹配
|
||||
smsChannelMapper.insert(cloneIgnoreId(dbSmsChannel, o -> o.setCreateTime(buildTime(2020, 11, 11))));
|
||||
// 准备参数
|
||||
SmsChannelPageReqVO reqVO = new SmsChannelPageReqVO();
|
||||
reqVO.setSignature("芋道");
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
reqVO.setCreateTime(buildBetweenTime(2020, 12, 1, 2020, 12, 24));
|
||||
|
||||
// 调用
|
||||
PageResult<SmsChannelDO> pageResult = smsChannelService.getSmsChannelPage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbSmsChannel, pageResult.getList().get(0));
|
||||
// 调用
|
||||
PageResult<SmsChannelDO> pageResult = smsChannelService.getSmsChannelPage(reqVO);
|
||||
// 断言
|
||||
assertEquals(1, pageResult.getTotal());
|
||||
assertEquals(1, pageResult.getList().size());
|
||||
assertPojoEquals(dbSmsChannel, pageResult.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -196,29 +187,23 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
|
||||
// mock 数据
|
||||
SmsChannelDO channel = randomPojo(SmsChannelDO.class);
|
||||
smsChannelMapper.insert(channel);
|
||||
// mock 参数
|
||||
// 准备参数
|
||||
Long id = channel.getId();
|
||||
// mock 方法
|
||||
SmsClient mockClient = mock(SmsClient.class);
|
||||
when(smsClientFactory.getSmsClient(eq(id))).thenReturn(mockClient);
|
||||
SmsChannelProperties properties = BeanUtils.toBean(channel, SmsChannelProperties.class);
|
||||
when(smsClientFactory.createOrUpdateSmsClient(eq(properties))).thenReturn(mockClient);
|
||||
|
||||
// 调用
|
||||
SmsClient client = smsChannelService.getSmsClient(id);
|
||||
// 断言
|
||||
assertSame(client, mockClient);
|
||||
verify(smsClientFactory).createOrUpdateSmsClient(argThat(arg -> {
|
||||
SmsChannelProperties properties = BeanUtils.toBean(channel, SmsChannelProperties.class);
|
||||
return properties.equals(arg);
|
||||
}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSmsClient_code() {
|
||||
// mock 数据
|
||||
SmsChannelDO channel = randomPojo(SmsChannelDO.class);
|
||||
smsChannelMapper.insert(channel);
|
||||
// mock 参数
|
||||
String code = channel.getCode();
|
||||
// 准备参数
|
||||
String code = randomString();
|
||||
// mock 方法
|
||||
SmsClient mockClient = mock(SmsClient.class);
|
||||
when(smsClientFactory.getSmsClient(eq(code))).thenReturn(mockClient);
|
||||
@@ -227,10 +212,6 @@ public class SmsChannelServiceTest extends BaseDbUnitTest {
|
||||
SmsClient client = smsChannelService.getSmsClient(code);
|
||||
// 断言
|
||||
assertSame(client, mockClient);
|
||||
verify(smsClientFactory).createOrUpdateSmsClient(argThat(arg -> {
|
||||
SmsChannelProperties properties = BeanUtils.toBean(channel, SmsChannelProperties.class);
|
||||
return properties.equals(arg);
|
||||
}));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user