Merge remote-tracking branch 'base-version/main' into dev

# Conflicts:
#	zt-framework/zt-spring-boot-starter-test/src/main/java/com/zt/plat/framework/test/core/ut/BaseDbUnitTest.java
#	zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/framework/sms/core/client/impl/SmsClientFactoryImpl.java
#	zt-module-system/zt-module-system-server/src/main/java/com/zt/plat/module/system/framework/sms/core/enums/SmsChannelEnum.java
This commit is contained in:
chenbowen
2025-12-12 10:05:36 +08:00
66 changed files with 1326 additions and 211 deletions

View File

@@ -46,6 +46,7 @@ public class AliyunSmsClientTest extends BaseMockitoUnitTest {
Long sendLogId = randomLongId();
String mobile = randomString();
String apiTemplateId = randomString();
String content = randomString();
List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
new KeyValue<>("code", 1234), new KeyValue<>("op", "login"));
// mock 方法
@@ -55,7 +56,7 @@ public class AliyunSmsClientTest extends BaseMockitoUnitTest {
.then((Answer<String>) invocationOnMock -> (String) invocationOnMock.getArguments()[0]);
// 调用
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile,
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, content,
apiTemplateId, templateParams);
// 断言
assertTrue(result.getSuccess());
@@ -73,6 +74,7 @@ public class AliyunSmsClientTest extends BaseMockitoUnitTest {
Long sendLogId = randomLongId();
String mobile = randomString();
String apiTemplateId = randomString();
String content = randomString();
List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
new KeyValue<>("code", 1234), new KeyValue<>("op", "login"));
// mock 方法
@@ -82,7 +84,7 @@ public class AliyunSmsClientTest extends BaseMockitoUnitTest {
.then((Answer<String>) invocationOnMock -> (String) invocationOnMock.getArguments()[0]);
// 调用
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, apiTemplateId, templateParams);
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, content, apiTemplateId, templateParams);
// 断言
assertFalse(result.getSuccess());
assertEquals("B7700B8E-227E-5886-9564-26036172F01F", result.getApiRequestId());

View File

@@ -43,6 +43,7 @@ public class HuaweiSmsClientTest extends BaseMockitoUnitTest {
Long sendLogId = randomLongId();
String mobile = randomString();
String apiTemplateId = randomString() + " " + randomString();
String content = randomString();
List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
new KeyValue<>("1", 1234), new KeyValue<>("2", "login"));
@@ -51,7 +52,7 @@ public class HuaweiSmsClientTest extends BaseMockitoUnitTest {
.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,
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, content,
apiTemplateId, templateParams);
// 断言
assertTrue(result.getSuccess());
@@ -67,6 +68,7 @@ public class HuaweiSmsClientTest extends BaseMockitoUnitTest {
Long sendLogId = randomLongId();
String mobile = randomString();
String apiTemplateId = randomString() + " " + randomString();
String content = randomString();
List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
new KeyValue<>("1", 1234), new KeyValue<>("2", "login"));
@@ -75,7 +77,7 @@ public class HuaweiSmsClientTest extends BaseMockitoUnitTest {
.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,
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, content,
apiTemplateId, templateParams);
// 断言
assertFalse(result.getSuccess());
@@ -91,6 +93,7 @@ public class HuaweiSmsClientTest extends BaseMockitoUnitTest {
Long sendLogId = randomLongId();
String mobile = randomString();
String apiTemplateId = randomString() + " " + randomString();
String content = randomString();
List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
new KeyValue<>("1", 1234), new KeyValue<>("2", "login"));
@@ -99,7 +102,7 @@ public class HuaweiSmsClientTest extends BaseMockitoUnitTest {
.thenReturn("{\"code\":\"E000102\",\"description\":\"Invalid app_key.\"}");
// 调用
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile,
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, content,
apiTemplateId, templateParams);
// 断言
assertFalse(result.getSuccess());

View File

@@ -45,13 +45,14 @@ public class QiniuSmsClientTest extends BaseMockitoUnitTest {
Long sendLogId = randomLongId();
String mobile = randomString();
String apiTemplateId = randomString() + " " + randomString();
String content = 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,
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, content,
apiTemplateId, templateParams);
// 断言
assertTrue(result.getSuccess());
@@ -66,13 +67,14 @@ public class QiniuSmsClientTest extends BaseMockitoUnitTest {
Long sendLogId = randomLongId();
String mobile = randomString();
String apiTemplateId = randomString() + " " + randomString();
String content = 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,
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, content,
apiTemplateId, templateParams);
// 断言
assertFalse(result.getSuccess());

View File

@@ -29,6 +29,7 @@ public class SmsClientTests {
AliyunSmsClient client = new AliyunSmsClient(properties);
// 准备参数
String apiTemplateId = "SMS_207945135";
String content = "test";
// 调用
SmsTemplateRespDTO template = client.getSmsTemplate(apiTemplateId);
// 打印结果
@@ -47,8 +48,9 @@ public class SmsClientTests {
Long sendLogId = System.currentTimeMillis();
String mobile = "15601691323";
String apiTemplateId = "SMS_207945135";
String content = "test";
// 调用
SmsSendRespDTO sendRespDTO = client.sendSms(sendLogId, mobile, apiTemplateId, ListUtil.of(new KeyValue<>("code", "1024")));
SmsSendRespDTO sendRespDTO = client.sendSms(sendLogId, mobile, content, apiTemplateId, ListUtil.of(new KeyValue<String, Object>("code", "1024")));
// 打印结果
System.out.println(sendRespDTO);
}
@@ -68,8 +70,9 @@ public class SmsClientTests {
Long sendLogId = System.currentTimeMillis();
String mobile = "15601691323";
String apiTemplateId = "358212";
String content = "test";
// 调用
SmsSendRespDTO sendRespDTO = client.sendSms(sendLogId, mobile, apiTemplateId, ListUtil.of(new KeyValue<>("code", "1024")));
SmsSendRespDTO sendRespDTO = client.sendSms(sendLogId, mobile, content, apiTemplateId, ListUtil.of(new KeyValue<String, Object>("code", "1024")));
// 打印结果
System.out.println(sendRespDTO);
}
@@ -106,9 +109,10 @@ public class SmsClientTests {
Long sendLogId = System.currentTimeMillis();
String mobile = "17321315478";
String apiTemplateId = "3644cdab863546a3b718d488659a99ef";
List<KeyValue<String, Object>> templateParams = ListUtil.of(new KeyValue<>("code", "1024"));
String content = "test";
List<KeyValue<String, Object>> templateParams = ListUtil.of(new KeyValue<String, Object>("code", "1024"));
// 调用
SmsSendRespDTO smsSendRespDTO = client.sendSms(sendLogId, mobile, apiTemplateId, templateParams);
SmsSendRespDTO smsSendRespDTO = client.sendSms(sendLogId, mobile, content, apiTemplateId, templateParams);
// 打印结果
System.out.println(smsSendRespDTO);
}
@@ -126,9 +130,10 @@ public class SmsClientTests {
Long sendLogId = System.currentTimeMillis();
String mobile = "17321315478";
String apiTemplateId = "3644cdab863546a3b718d488659a99ef";
List<KeyValue<String, Object>> templateParams = ListUtil.of(new KeyValue<>("code", "1122"));
String content = "test";
List<KeyValue<String, Object>> templateParams = ListUtil.of(new KeyValue<String, Object>("code", "1122"));
// 调用
SmsSendRespDTO smsSendRespDTO = client.sendSms(sendLogId, mobile, apiTemplateId, templateParams);
SmsSendRespDTO smsSendRespDTO = client.sendSms(sendLogId, mobile, content, apiTemplateId, templateParams);
// 打印结果
System.out.println(smsSendRespDTO);
}

View File

@@ -45,6 +45,7 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest {
Long sendLogId = randomLongId();
String mobile = randomString();
String apiTemplateId = randomString();
String content = randomString();
List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
new KeyValue<>("1", 1234), new KeyValue<>("2", "login"));
// mock 方法
@@ -67,7 +68,7 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest {
"}");
// 调用
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile,
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, content,
apiTemplateId, templateParams);
// 断言
assertTrue(result.getSuccess());
@@ -84,6 +85,7 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest {
Long sendLogId = randomLongId();
String mobile = randomString();
String apiTemplateId = randomString();
String content = randomString();
List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
new KeyValue<>("1", 1234), new KeyValue<>("2", "login"));
@@ -107,7 +109,7 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest {
"}");
// 调用
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile,
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, content,
apiTemplateId, templateParams);
// 断言
assertFalse(result.getSuccess());
@@ -124,6 +126,7 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest {
Long sendLogId = randomLongId();
String mobile = randomString();
String apiTemplateId = randomString();
String content = randomString();
List<KeyValue<String, Object>> templateParams = Lists.newArrayList(
new KeyValue<>("1", 1234), new KeyValue<>("2", "login"));
@@ -132,7 +135,7 @@ public class TencentSmsClientTest extends BaseMockitoUnitTest {
.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,
SmsSendRespDTO result = smsClient.sendSms(sendLogId, mobile, content,
apiTemplateId, templateParams);
// 断言
assertFalse(result.getSuccess());

View File

@@ -85,7 +85,7 @@ public class DictDataServiceImplTest extends BaseDbUnitTest {
dictDataMapper.insert(cloneIgnoreId(dbDictData, o -> o.setValue("otherValue")));
// 准备参数
DictDataPageReqVO reqVO = new DictDataPageReqVO();
reqVO.setLabel("");
reqVO.setLabel("ZT");
reqVO.setDictType("yunai");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setValue("testValue");

View File

@@ -59,8 +59,8 @@ public class DictTypeServiceImplTest extends BaseDbUnitTest {
dictTypeMapper.insert(cloneIgnoreId(dbDictType, o -> o.setCreateTime(buildTime(2021, 1, 1))));
// 准备参数
DictTypePageReqVO reqVO = new DictTypePageReqVO();
reqVO.setName("nai");
reqVO.setType("");
reqVO.setName("yunai");
reqVO.setType("ZT");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setCreateTime(buildBetweenTime(2021, 1, 10, 2021, 1, 20));

View File

@@ -153,7 +153,7 @@ public class MenuServiceImplTest extends BaseDbUnitTest {
// 测试 name 不匹配
menuMapper.insert(cloneIgnoreId(menuDO, o -> o.setName("")));
// 准备参数
MenuListReqVO reqVO = new MenuListReqVO().setName("").setStatus(CommonStatusEnum.ENABLE.getStatus());
MenuListReqVO reqVO = new MenuListReqVO().setName("ZT").setStatus(CommonStatusEnum.ENABLE.getStatus());
// 调用
List<MenuDO> result = menuService.getMenuList(reqVO);

View File

@@ -0,0 +1,81 @@
package com.zt.plat.module.system.service.sms;
import com.zt.plat.framework.common.exception.ServiceException;
import com.zt.plat.module.system.dal.dataobject.sms.SmsChannelDO;
import com.zt.plat.module.system.dal.mysql.sms.SmsChannelMapper;
import com.zt.plat.module.system.framework.sms.core.client.SmsClient;
import com.zt.plat.module.system.framework.sms.core.client.SmsClientFactory;
import com.zt.plat.module.system.framework.sms.core.client.impl.extra.SmsBalanceClient;
import com.zt.plat.framework.test.core.ut.BaseMockitoUnitTest;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static com.zt.plat.framework.test.core.util.RandomUtils.randomLongId;
import static com.zt.plat.framework.test.core.util.RandomUtils.randomPojo;
import static com.zt.plat.module.system.enums.ErrorCodeConstants.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
public class SmsChannelServiceImplTest extends BaseMockitoUnitTest {
@InjectMocks
private SmsChannelServiceImpl smsChannelService;
@Mock
private SmsClientFactory smsClientFactory;
@Mock
private SmsChannelMapper smsChannelMapper;
@Mock
private SmsTemplateService smsTemplateService;
@Test
public void testQueryBalance_success() throws Throwable {
Long channelId = randomLongId();
SmsChannelDO channel = randomPojo(SmsChannelDO.class, o -> o.setId(channelId));
when(smsChannelMapper.selectById(eq(channelId))).thenReturn(channel);
SmsClient client = mock(SmsClient.class, withSettings().extraInterfaces(SmsBalanceClient.class));
when(smsClientFactory.createOrUpdateSmsClient(any())).thenReturn(client);
when(((SmsBalanceClient) client).queryBalance()).thenReturn(88);
Integer balance = smsChannelService.queryBalance(channelId);
assertThat(balance).isEqualTo(88);
verify((SmsBalanceClient) client, times(1)).queryBalance();
}
@Test
public void testQueryBalance_unsupported() {
Long channelId = randomLongId();
when(smsChannelMapper.selectById(eq(channelId))).thenReturn(randomPojo(SmsChannelDO.class, o -> o.setId(channelId)));
SmsClient client = mock(SmsClient.class); // 未实现 SmsBalanceClient
when(smsClientFactory.createOrUpdateSmsClient(any())).thenReturn(client);
ServiceException ex = assertThrows(ServiceException.class, () -> smsChannelService.queryBalance(channelId));
assertThat(ex.getCode()).isEqualTo(SMS_CHANNEL_BALANCE_UNSUPPORTED.getCode());
}
@Test
public void testQueryBalance_apiError() throws Throwable {
Long channelId = randomLongId();
when(smsChannelMapper.selectById(eq(channelId))).thenReturn(randomPojo(SmsChannelDO.class, o -> o.setId(channelId)));
SmsClient client = mock(SmsClient.class, withSettings().extraInterfaces(SmsBalanceClient.class));
when(smsClientFactory.createOrUpdateSmsClient(any())).thenReturn(client);
when(((SmsBalanceClient) client).queryBalance()).thenThrow(new RuntimeException("boom"));
ServiceException ex = assertThrows(ServiceException.class, () -> smsChannelService.queryBalance(channelId));
assertThat(ex.getCode()).isEqualTo(SMS_TEMPLATE_API_ERROR.getCode());
}
@Test
public void testQueryBalance_channelNotFound() {
Long channelId = randomLongId();
when(smsChannelMapper.selectById(eq(channelId))).thenReturn(null);
ServiceException ex = assertThrows(ServiceException.class, () -> smsChannelService.queryBalance(channelId));
assertThat(ex.getCode()).isEqualTo(SMS_CHANNEL_NOT_EXISTS.getCode());
}
}

Some files were not shown because too many files have changed in this diff Show More