1. 新增 api 绑定客户凭证进行权限校验
2. 去除 api 定义的缓存策略 3. 新增短信渠道 4. 新增用户信息模糊查询 5. 修复全局的单元测试
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
package com.zt.plat.module.system.dal.mysql.user;
|
||||
|
||||
import com.zt.plat.framework.common.pojo.PageResult;
|
||||
import com.zt.plat.framework.test.core.ut.BaseDbUnitTest;
|
||||
import com.zt.plat.module.system.controller.admin.user.vo.user.UserPageReqVO;
|
||||
import com.zt.plat.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import com.zt.plat.module.system.dal.dataobject.userdept.UserDeptDO;
|
||||
import com.zt.plat.module.system.dal.mysql.userdept.UserDeptMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collections;
|
||||
|
||||
import static com.zt.plat.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static com.zt.plat.framework.test.core.util.RandomUtils.randomPojo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* {@link AdminUserMapper} 单元测试
|
||||
*/
|
||||
public class AdminUserMapperTest extends BaseDbUnitTest {
|
||||
|
||||
@Resource
|
||||
private AdminUserMapper adminUserMapper;
|
||||
@Resource
|
||||
private UserDeptMapper userDeptMapper;
|
||||
|
||||
@Test
|
||||
public void testSelectPage_keywordMatch() {
|
||||
// 准备数据:两个用户,只有第一个命中 keyword
|
||||
AdminUserDO matchUser = randomUser("key-nick", "key-user", "13800000000", "WK001");
|
||||
AdminUserDO otherUser = randomUser("other", "otherUser", "13900000000", "WK002");
|
||||
adminUserMapper.insert(matchUser);
|
||||
adminUserMapper.insert(otherUser);
|
||||
|
||||
// 调用
|
||||
UserPageReqVO reqVO = new UserPageReqVO();
|
||||
reqVO.setPageNo(1);
|
||||
reqVO.setPageSize(10);
|
||||
reqVO.setKeyword("key");
|
||||
PageResult<AdminUserDO> page = adminUserMapper.selectPage(reqVO, null, (java.util.Collection<Long>) null);
|
||||
|
||||
// 断言:只返回命中的用户
|
||||
assertEquals(1, page.getList().size());
|
||||
assertPojoEquals(matchUser, page.getList().get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectPage_filterByDeptIds() {
|
||||
// 用户 A 属于部门 1,用户 B 属于部门 2
|
||||
AdminUserDO dept1User = randomUser("dept1", "dept1User", "13000000001", "WK101");
|
||||
AdminUserDO dept2User = randomUser("dept2", "dept2User", "13000000002", "WK102");
|
||||
adminUserMapper.insert(dept1User);
|
||||
adminUserMapper.insert(dept2User);
|
||||
UserDeptDO ud1 = new UserDeptDO();
|
||||
ud1.setUserId(dept1User.getId());
|
||||
ud1.setDeptId(1L);
|
||||
userDeptMapper.insert(ud1);
|
||||
|
||||
UserDeptDO ud2 = new UserDeptDO();
|
||||
ud2.setUserId(dept2User.getId());
|
||||
ud2.setDeptId(2L);
|
||||
userDeptMapper.insert(ud2);
|
||||
|
||||
UserPageReqVO reqVO = new UserPageReqVO();
|
||||
reqVO.setPageNo(1);
|
||||
reqVO.setPageSize(10);
|
||||
|
||||
// 仅过滤部门 1
|
||||
PageResult<AdminUserDO> page = adminUserMapper.selectPage(reqVO, Collections.singletonList(1L), null);
|
||||
|
||||
assertEquals(1, page.getList().size());
|
||||
assertEquals(dept1User.getId(), page.getList().get(0).getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectPage_filterByUserIds() {
|
||||
AdminUserDO user1 = randomUser("u1", "u1", "15000000001", "WK201");
|
||||
AdminUserDO user2 = randomUser("u2", "u2", "15000000002", "WK202");
|
||||
adminUserMapper.insert(user1);
|
||||
adminUserMapper.insert(user2);
|
||||
|
||||
UserPageReqVO reqVO = new UserPageReqVO();
|
||||
reqVO.setPageNo(1);
|
||||
reqVO.setPageSize(10);
|
||||
|
||||
PageResult<AdminUserDO> page = adminUserMapper.selectPage(reqVO, null, Collections.singletonList(user2.getId()));
|
||||
|
||||
assertEquals(1, page.getList().size());
|
||||
assertEquals(user2.getId(), page.getList().get(0).getId());
|
||||
}
|
||||
|
||||
private AdminUserDO randomUser(String nickname, String username, String mobile, String workcode) {
|
||||
AdminUserDO user = randomPojo(AdminUserDO.class, o -> {
|
||||
o.setId(null);
|
||||
o.setNickname(nickname);
|
||||
o.setUsername(username);
|
||||
o.setMobile(mobile);
|
||||
o.setWorkcode(workcode);
|
||||
o.setSex(1);
|
||||
o.setStatus(0);
|
||||
o.setUserSource(1);
|
||||
// 这些字段仅用于展示,不参与持久化,避免断言时与查询结果不一致
|
||||
o.setDeptIds(null);
|
||||
o.setDeptNames(null);
|
||||
o.setCompanyIds(null);
|
||||
o.setCompanyDeptInfos(null);
|
||||
o.setCreateTime(LocalDateTime.now().withNano(0));
|
||||
});
|
||||
// 保证关键字段非空
|
||||
if (!StringUtils.hasText(user.getPassword())) {
|
||||
user.setPassword("pwd");
|
||||
}
|
||||
if (user.getTenantId() == null) {
|
||||
user.setTenantId(1L);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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));
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -85,7 +85,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest {
|
||||
// 断言
|
||||
assertEquals(smsLogId, resultSmsLogId);
|
||||
// 断言调用
|
||||
verify(smsProducer).sendSmsSendMessage(eq(smsLogId), eq(user.getMobile()),
|
||||
verify(smsProducer).sendSmsSendMessage(eq(smsLogId), eq(user.getMobile()), eq(content),
|
||||
eq(template.getChannelId()), eq(template.getApiTemplateId()),
|
||||
eq(Lists.newArrayList(new KeyValue<>("code", "1234"), new KeyValue<>("op", "login"))));
|
||||
}
|
||||
@@ -124,7 +124,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest {
|
||||
// 断言
|
||||
assertEquals(smsLogId, resultSmsLogId);
|
||||
// 断言调用
|
||||
verify(smsProducer).sendSmsSendMessage(eq(smsLogId), eq(mobile),
|
||||
verify(smsProducer).sendSmsSendMessage(eq(smsLogId), eq(mobile), eq(content),
|
||||
eq(template.getChannelId()), eq(template.getApiTemplateId()),
|
||||
eq(Lists.newArrayList(new KeyValue<>("code", "1234"), new KeyValue<>("op", "login"))));
|
||||
}
|
||||
@@ -164,7 +164,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest {
|
||||
// 断言
|
||||
assertEquals(smsLogId, resultSmsLogId);
|
||||
// 断言调用
|
||||
verify(smsProducer).sendSmsSendMessage(eq(smsLogId), eq(mobile),
|
||||
verify(smsProducer).sendSmsSendMessage(eq(smsLogId), eq(mobile), eq(content),
|
||||
eq(template.getChannelId()), eq(template.getApiTemplateId()),
|
||||
eq(Lists.newArrayList(new KeyValue<>("code", "1234"), new KeyValue<>("op", "login"))));
|
||||
}
|
||||
@@ -204,7 +204,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest {
|
||||
// 断言
|
||||
assertEquals(smsLogId, resultSmsLogId);
|
||||
// 断言调用
|
||||
verify(smsProducer, times(0)).sendSmsSendMessage(anyLong(), anyString(),
|
||||
verify(smsProducer, times(0)).sendSmsSendMessage(anyLong(), anyString(), anyString(),
|
||||
anyLong(), any(), anyList());
|
||||
}
|
||||
|
||||
@@ -260,13 +260,13 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testDoSendSms() throws Throwable {
|
||||
// 准备参数
|
||||
SmsSendMessage message = randomPojo(SmsSendMessage.class);
|
||||
SmsSendMessage message = randomPojo(SmsSendMessage.class, o -> o.setContent(randomString()));
|
||||
// mock SmsClientFactory 的方法
|
||||
SmsClient smsClient = spy(SmsClient.class);
|
||||
when(smsChannelService.getSmsClient(eq(message.getChannelId()))).thenReturn(smsClient);
|
||||
// mock SmsClient 的方法
|
||||
SmsSendRespDTO sendResult = randomPojo(SmsSendRespDTO.class);
|
||||
when(smsClient.sendSms(eq(message.getLogId()), eq(message.getMobile()), eq(message.getApiTemplateId()),
|
||||
when(smsClient.sendSms(eq(message.getLogId()), eq(message.getMobile()), eq(message.getContent()), eq(message.getApiTemplateId()),
|
||||
eq(message.getTemplateParams()))).thenReturn(sendResult);
|
||||
|
||||
// 调用
|
||||
@@ -287,12 +287,15 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest {
|
||||
when(smsChannelService.getSmsClient(eq(channelCode))).thenReturn(smsClient);
|
||||
// mock SmsClient 的方法
|
||||
List<SmsReceiveRespDTO> receiveResults = randomPojoList(SmsReceiveRespDTO.class);
|
||||
when(smsClient.parseSmsReceiveStatus(eq(text))).thenReturn(receiveResults);
|
||||
|
||||
// 调用
|
||||
smsSendService.receiveSmsStatus(channelCode, text);
|
||||
// 断言
|
||||
receiveResults.forEach(result -> smsLogService.updateSmsReceiveResult(eq(result.getLogId()), eq(result.getSuccess()),
|
||||
eq(result.getReceiveTime()), eq(result.getErrorCode()), eq(result.getErrorCode())));
|
||||
for (SmsReceiveRespDTO result : receiveResults) {
|
||||
verify(smsLogService).updateSmsReceiveResult(eq(result.getLogId()), eq(result.getSuccess()),
|
||||
eq(result.getReceiveTime()), eq(result.getErrorCode()), eq(result.getErrorMsg()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -273,7 +273,7 @@ public class SocialUserServiceImplTest extends BaseDbUnitTest {
|
||||
// 准备参数
|
||||
SocialUserPageReqVO reqVO = new SocialUserPageReqVO();
|
||||
reqVO.setType(SocialTypeEnum.GITEE.getType());
|
||||
reqVO.setNickname("芋");
|
||||
reqVO.setNickname("ZT");
|
||||
reqVO.setOpenid("zt");
|
||||
reqVO.setCreateTime(buildBetweenTime(2020, 1, 10, 2020, 1, 20));
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.zt.plat.module.system.service.sso.client;
|
||||
|
||||
import com.zt.plat.module.system.framework.sso.config.ExternalSsoProperties;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
@@ -16,7 +18,6 @@ import static org.mockito.Mockito.mock;
|
||||
* 使用 Mock 的 RedisTemplate,避免外部依赖。
|
||||
*/
|
||||
@SpringBootTest(classes = { ExternalSsoClientConfiguration.class, ExternalSsoClientConfigurationLoadTest.TestBeans.class })
|
||||
@Import(ExternalSsoProperties.class)
|
||||
class ExternalSsoClientConfigurationLoadTest {
|
||||
|
||||
@TestConfiguration
|
||||
@@ -33,6 +34,16 @@ class ExternalSsoClientConfigurationLoadTest {
|
||||
props.getRemote().setBaseUrl("http://localhost");
|
||||
return props;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ObjectMapper objectMapper() {
|
||||
return new ObjectMapper();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RestTemplateBuilder restTemplateBuilder() {
|
||||
return new RestTemplateBuilder();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -306,7 +306,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
|
||||
// 准备参数
|
||||
TenantPageReqVO reqVO = new TenantPageReqVO();
|
||||
reqVO.setName("ZT");
|
||||
reqVO.setContactName("艿");
|
||||
reqVO.setContactName("ZT");
|
||||
reqVO.setContactMobile("1560");
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
reqVO.setCreateTime(buildBetweenTime(2020, 12, 1, 2020, 12, 24));
|
||||
|
||||
@@ -681,6 +681,7 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest {
|
||||
o.setDeptNames("-");
|
||||
o.setCompanyDeptInfos(null);// 保证 deptIds 的范围
|
||||
o.setUserSource(null);
|
||||
o.setWorkcode(null);
|
||||
};
|
||||
return randomPojo(AdminUserDO.class, ArrayUtils.append(consumer, consumers));
|
||||
}
|
||||
|
||||
@@ -242,6 +242,7 @@ CREATE TABLE IF NOT EXISTS `system_operate_log` (
|
||||
CREATE TABLE IF NOT EXISTS "system_users" (
|
||||
"id" bigint not null GENERATED BY DEFAULT AS IDENTITY,
|
||||
"username" varchar(30) not null,
|
||||
"workcode" varchar(100) default null,
|
||||
"password" varchar(100) not null default '',
|
||||
"nickname" varchar(30) not null,
|
||||
"remark" varchar(500) default null,
|
||||
@@ -267,6 +268,7 @@ CREATE TABLE IF NOT EXISTS "system_users" (
|
||||
CREATE TABLE IF NOT EXISTS "system_sms_channel" (
|
||||
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||
"signature" varchar(10) NOT NULL,
|
||||
"epid" varchar(63) DEFAULT NULL,
|
||||
"code" varchar(63) NOT NULL,
|
||||
"status" tinyint NOT NULL,
|
||||
"remark" varchar(255) DEFAULT NULL,
|
||||
|
||||
Reference in New Issue
Block a user