增加 sms/tenant/senstiveword 模块的单测覆盖率

pull/82/head
YunaiV 2023-12-04 13:14:25 +08:00
parent 49aee82b6a
commit efc9649fa5
4 changed files with 128 additions and 10 deletions

View File

@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.system.service.sensitiveword;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.SetUtils; import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordPageReqVO; import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordSaveVO; import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordSaveVO;
@ -13,6 +14,8 @@ import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -78,6 +81,35 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest {
assertNotNull(sensitiveWordService.getTagSensitiveWordTries().get("测试")); assertNotNull(sensitiveWordService.getTagSensitiveWordTries().get("测试"));
} }
@Test
public void testRefreshLocalCache() {
// mock 数据
SensitiveWordDO wordDO1 = randomPojo(SensitiveWordDO.class, o -> o.setName("傻瓜")
.setTags(singletonList("论坛")).setStatus(CommonStatusEnum.ENABLE.getStatus()));
wordDO1.setUpdateTime(LocalDateTime.now());
sensitiveWordMapper.insert(wordDO1);
sensitiveWordService.initLocalCache();
// mock 数据 ②
SensitiveWordDO wordDO2 = randomPojo(SensitiveWordDO.class, o -> o.setName("笨蛋")
.setTags(singletonList("蔬菜")).setStatus(CommonStatusEnum.ENABLE.getStatus()));
wordDO2.setUpdateTime(LocalDateTimeUtils.addTime(Duration.ofMinutes(1))); // 避免时间相同
sensitiveWordMapper.insert(wordDO2);
// 调用
sensitiveWordService.refreshLocalCache();
// 断言 sensitiveWordTagsCache 缓存
assertEquals(SetUtils.asSet("论坛", "蔬菜"), sensitiveWordService.getSensitiveWordTagSet());
// 断言 sensitiveWordCache
assertEquals(2, sensitiveWordService.getSensitiveWordCache().size());
assertPojoEquals(wordDO1, sensitiveWordService.getSensitiveWordCache().get(0));
assertPojoEquals(wordDO2, sensitiveWordService.getSensitiveWordCache().get(1));
// 断言 tagSensitiveWordTries 缓存
assertNotNull(sensitiveWordService.getDefaultSensitiveWordTrie());
assertEquals(2, sensitiveWordService.getTagSensitiveWordTries().size());
assertNotNull(sensitiveWordService.getTagSensitiveWordTries().get("论坛"));
assertNotNull(sensitiveWordService.getTagSensitiveWordTries().get("蔬菜"));
}
@Test @Test
public void testCreateSensitiveWord_success() { public void testCreateSensitiveWord_success() {
// 准备参数 // 准备参数

View File

@ -16,6 +16,7 @@ import cn.iocoder.yudao.module.system.mq.producer.sms.SmsProducer;
import cn.iocoder.yudao.module.system.service.member.MemberService; import cn.iocoder.yudao.module.system.service.member.MemberService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService; import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import org.assertj.core.util.Lists; import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
@ -35,7 +36,7 @@ import static org.mockito.Mockito.*;
public class SmsSendServiceImplTest extends BaseMockitoUnitTest { public class SmsSendServiceImplTest extends BaseMockitoUnitTest {
@InjectMocks @InjectMocks
private SmsSendServiceImpl smsService; private SmsSendServiceImpl smsSendService;
@Mock @Mock
private AdminUserService adminUserService; private AdminUserService adminUserService;
@ -80,7 +81,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest {
eq(content), eq(templateParams))).thenReturn(smsLogId); eq(content), eq(templateParams))).thenReturn(smsLogId);
// 调用 // 调用
Long resultSmsLogId = smsService.sendSingleSmsToAdmin(null, userId, templateCode, templateParams); Long resultSmsLogId = smsSendService.sendSingleSmsToAdmin(null, userId, templateCode, templateParams);
// 断言 // 断言
assertEquals(smsLogId, resultSmsLogId); assertEquals(smsLogId, resultSmsLogId);
// 断言调用 // 断言调用
@ -119,7 +120,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest {
eq(content), eq(templateParams))).thenReturn(smsLogId); eq(content), eq(templateParams))).thenReturn(smsLogId);
// 调用 // 调用
Long resultSmsLogId = smsService.sendSingleSmsToMember(null, userId, templateCode, templateParams); Long resultSmsLogId = smsSendService.sendSingleSmsToMember(null, userId, templateCode, templateParams);
// 断言 // 断言
assertEquals(smsLogId, resultSmsLogId); assertEquals(smsLogId, resultSmsLogId);
// 断言调用 // 断言调用
@ -159,7 +160,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest {
eq(content), eq(templateParams))).thenReturn(smsLogId); eq(content), eq(templateParams))).thenReturn(smsLogId);
// 调用 // 调用
Long resultSmsLogId = smsService.sendSingleSms(mobile, userId, userType, templateCode, templateParams); Long resultSmsLogId = smsSendService.sendSingleSms(mobile, userId, userType, templateCode, templateParams);
// 断言 // 断言
assertEquals(smsLogId, resultSmsLogId); assertEquals(smsLogId, resultSmsLogId);
// 断言调用 // 断言调用
@ -199,7 +200,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest {
eq(content), eq(templateParams))).thenReturn(smsLogId); eq(content), eq(templateParams))).thenReturn(smsLogId);
// 调用 // 调用
Long resultSmsLogId = smsService.sendSingleSms(mobile, userId, userType, templateCode, templateParams); Long resultSmsLogId = smsSendService.sendSingleSms(mobile, userId, userType, templateCode, templateParams);
// 断言 // 断言
assertEquals(smsLogId, resultSmsLogId); assertEquals(smsLogId, resultSmsLogId);
// 断言调用 // 断言调用
@ -214,7 +215,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest {
// mock 方法 // mock 方法
// 调用,并断言异常 // 调用,并断言异常
assertServiceException(() -> smsService.validateSmsTemplate(templateCode), assertServiceException(() -> smsSendService.validateSmsTemplate(templateCode),
SMS_SEND_TEMPLATE_NOT_EXISTS); SMS_SEND_TEMPLATE_NOT_EXISTS);
} }
@ -227,7 +228,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest {
// mock 方法 // mock 方法
// 调用,并断言异常 // 调用,并断言异常
assertServiceException(() -> smsService.buildTemplateParams(template, templateParams), assertServiceException(() -> smsSendService.buildTemplateParams(template, templateParams),
SMS_SEND_MOBILE_TEMPLATE_PARAM_MISS, "code"); SMS_SEND_MOBILE_TEMPLATE_PARAM_MISS, "code");
} }
@ -237,10 +238,24 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest {
// mock 方法 // mock 方法
// 调用,并断言异常 // 调用,并断言异常
assertServiceException(() -> smsService.validateMobile(null), assertServiceException(() -> smsSendService.validateMobile(null),
SMS_SEND_MOBILE_NOT_EXISTS); SMS_SEND_MOBILE_NOT_EXISTS);
} }
@Test
public void testSendBatchNotify() {
// 准备参数
// mock 方法
// 调用
UnsupportedOperationException exception = Assertions.assertThrows(
UnsupportedOperationException.class,
() -> smsSendService.sendBatchSms(null, null, null, null, null)
);
// 断言
assertEquals("暂时不支持该操作,感兴趣可以实现该功能哟!", exception.getMessage());
}
@Test @Test
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testDoSendSms() throws Throwable { public void testDoSendSms() throws Throwable {
@ -255,7 +270,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest {
eq(message.getTemplateParams()))).thenReturn(sendResult); eq(message.getTemplateParams()))).thenReturn(sendResult);
// 调用 // 调用
smsService.doSendSms(message); smsSendService.doSendSms(message);
// 断言 // 断言
verify(smsLogService).updateSmsSendResult(eq(message.getLogId()), verify(smsLogService).updateSmsSendResult(eq(message.getLogId()),
eq(sendResult.getSuccess()), eq(sendResult.getApiCode()), eq(sendResult.getSuccess()), eq(sendResult.getApiCode()),
@ -274,7 +289,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest {
List<SmsReceiveRespDTO> receiveResults = randomPojoList(SmsReceiveRespDTO.class); List<SmsReceiveRespDTO> receiveResults = randomPojoList(SmsReceiveRespDTO.class);
// 调用 // 调用
smsService.receiveSmsStatus(channelCode, text); smsSendService.receiveSmsStatus(channelCode, text);
// 断言 // 断言
receiveResults.forEach(result -> smsLogService.updateSmsReceiveResult(eq(result.getLogId()), eq(result.getSuccess()), receiveResults.forEach(result -> smsLogService.updateSmsReceiveResult(eq(result.getLogId()), eq(result.getSuccess()),
eq(result.getReceiveTime()), eq(result.getErrorCode()), eq(result.getErrorCode()))); eq(result.getReceiveTime()), eq(result.getErrorCode()), eq(result.getErrorCode())));

View File

@ -1,5 +1,6 @@
package cn.iocoder.yudao.module.system.service.sms; package cn.iocoder.yudao.module.system.service.sms;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
@ -21,6 +22,7 @@ import org.springframework.context.annotation.Import;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import static cn.hutool.core.util.RandomUtil.randomEle; import static cn.hutool.core.util.RandomUtil.randomEle;
@ -48,6 +50,19 @@ public class SmsTemplateServiceImplTest extends BaseDbUnitTest {
@MockBean @MockBean
private SmsClient smsClient; private SmsClient smsClient;
@Test
public void testFormatSmsTemplateContent() {
// 准备参数
String content = "正在进行登录操作{operation},您的验证码是{code}";
Map<String, Object> params = MapUtil.<String, Object>builder("operation", "登录")
.put("code", "1234").build();
// 调用
String result = smsTemplateService.formatSmsTemplateContent(content, params);
// 断言
assertEquals("正在进行登录操作登录您的验证码是1234", result);
}
@Test @Test
public void testParseTemplateContentParams() { public void testParseTemplateContentParams() {
// 准备参数 // 准备参数
@ -156,6 +171,34 @@ public class SmsTemplateServiceImplTest extends BaseDbUnitTest {
assertServiceException(() -> smsTemplateService.deleteSmsTemplate(id), SMS_TEMPLATE_NOT_EXISTS); assertServiceException(() -> smsTemplateService.deleteSmsTemplate(id), SMS_TEMPLATE_NOT_EXISTS);
} }
@Test
public void testGetSmsTemplate() {
// mock 数据
SmsTemplateDO dbSmsTemplate = randomSmsTemplateDO();
smsTemplateMapper.insert(dbSmsTemplate);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbSmsTemplate.getId();
// 调用
SmsTemplateDO smsTemplate = smsTemplateService.getSmsTemplate(id);
// 校验
assertPojoEquals(dbSmsTemplate, smsTemplate);
}
@Test
public void testGetSmsTemplateByCodeFromCache() {
// mock 数据
SmsTemplateDO dbSmsTemplate = randomSmsTemplateDO();
smsTemplateMapper.insert(dbSmsTemplate);// @Sql: 先插入出一条存在的数据
// 准备参数
String code = dbSmsTemplate.getCode();
// 调用
SmsTemplateDO smsTemplate = smsTemplateService.getSmsTemplateByCodeFromCache(code);
// 校验
assertPojoEquals(dbSmsTemplate, smsTemplate);
}
@Test @Test
public void testGetSmsTemplatePage() { public void testGetSmsTemplatePage() {
// mock 数据 // mock 数据
@ -201,6 +244,22 @@ public class SmsTemplateServiceImplTest extends BaseDbUnitTest {
assertPojoEquals(dbSmsTemplate, pageResult.getList().get(0)); assertPojoEquals(dbSmsTemplate, pageResult.getList().get(0));
} }
@Test
public void testGetSmsTemplateCountByChannelId() {
// mock 数据
SmsTemplateDO dbSmsTemplate = randomPojo(SmsTemplateDO.class, o -> o.setChannelId(1L));
smsTemplateMapper.insert(dbSmsTemplate);
// 测试 channelId 不匹配
smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setChannelId(2L)));
// 准备参数
Long channelId = 1L;
// 调用
Long count = smsTemplateService.getSmsTemplateCountByChannelId(channelId);
// 断言
assertEquals(1, count);
}
@Test @Test
public void testValidateSmsChannel_success() { public void testValidateSmsChannel_success() {
// 准备参数 // 准备参数

View File

@ -331,6 +331,18 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
assertPojoEquals(result, dbTenant); assertPojoEquals(result, dbTenant);
} }
@Test
public void testGetTenantByWebsite() {
// mock 数据
TenantDO dbTenant = randomPojo(TenantDO.class, o -> o.setWebsite("https://www.iocoder.cn"));
tenantMapper.insert(dbTenant);// @Sql: 先插入出一条存在的数据
// 调用
TenantDO result = tenantService.getTenantByWebsite("https://www.iocoder.cn");
// 校验存在
assertPojoEquals(result, dbTenant);
}
@Test @Test
public void testGetTenantListByPackageId() { public void testGetTenantListByPackageId() {
// mock 数据 // mock 数据