diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sensitiveword/SensitiveWordServiceImplTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sensitiveword/SensitiveWordServiceImplTest.java index cc7b11d50..b5be5e5d1 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sensitiveword/SensitiveWordServiceImplTest.java +++ b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sensitiveword/SensitiveWordServiceImplTest.java @@ -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.pojo.PageResult; 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.module.system.controller.admin.sensitiveword.vo.SensitiveWordPageReqVO; 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 javax.annotation.Resource; +import java.time.Duration; +import java.time.LocalDateTime; import java.util.Arrays; import java.util.List; @@ -78,6 +81,35 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { 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 public void testCreateSensitiveWord_success() { // 准备参数 diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsSendServiceImplTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsSendServiceImplTest.java index 96742c9a8..6e35fe31a 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsSendServiceImplTest.java +++ b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsSendServiceImplTest.java @@ -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.user.AdminUserService; import org.assertj.core.util.Lists; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; @@ -35,7 +36,7 @@ import static org.mockito.Mockito.*; public class SmsSendServiceImplTest extends BaseMockitoUnitTest { @InjectMocks - private SmsSendServiceImpl smsService; + private SmsSendServiceImpl smsSendService; @Mock private AdminUserService adminUserService; @@ -80,7 +81,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest { 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); // 断言调用 @@ -119,7 +120,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest { 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); // 断言调用 @@ -159,7 +160,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest { 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); // 断言调用 @@ -199,7 +200,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest { 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); // 断言调用 @@ -214,7 +215,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest { // mock 方法 // 调用,并断言异常 - assertServiceException(() -> smsService.validateSmsTemplate(templateCode), + assertServiceException(() -> smsSendService.validateSmsTemplate(templateCode), SMS_SEND_TEMPLATE_NOT_EXISTS); } @@ -227,7 +228,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest { // mock 方法 // 调用,并断言异常 - assertServiceException(() -> smsService.buildTemplateParams(template, templateParams), + assertServiceException(() -> smsSendService.buildTemplateParams(template, templateParams), SMS_SEND_MOBILE_TEMPLATE_PARAM_MISS, "code"); } @@ -237,10 +238,24 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest { // mock 方法 // 调用,并断言异常 - assertServiceException(() -> smsService.validateMobile(null), + assertServiceException(() -> smsSendService.validateMobile(null), 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 @SuppressWarnings("unchecked") public void testDoSendSms() throws Throwable { @@ -255,7 +270,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest { eq(message.getTemplateParams()))).thenReturn(sendResult); // 调用 - smsService.doSendSms(message); + smsSendService.doSendSms(message); // 断言 verify(smsLogService).updateSmsSendResult(eq(message.getLogId()), eq(sendResult.getSuccess()), eq(sendResult.getApiCode()), @@ -274,7 +289,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest { List receiveResults = randomPojoList(SmsReceiveRespDTO.class); // 调用 - smsService.receiveSmsStatus(channelCode, text); + smsSendService.receiveSmsStatus(channelCode, text); // 断言 receiveResults.forEach(result -> smsLogService.updateSmsReceiveResult(eq(result.getLogId()), eq(result.getSuccess()), eq(result.getReceiveTime()), eq(result.getErrorCode()), eq(result.getErrorCode()))); diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsTemplateServiceImplTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsTemplateServiceImplTest.java index 4ba4d43fd..2424cbcbb 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsTemplateServiceImplTest.java +++ b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsTemplateServiceImplTest.java @@ -1,5 +1,6 @@ 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.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; @@ -21,6 +22,7 @@ import org.springframework.context.annotation.Import; import javax.annotation.Resource; import java.util.List; +import java.util.Map; import java.util.function.Consumer; import static cn.hutool.core.util.RandomUtil.randomEle; @@ -48,6 +50,19 @@ public class SmsTemplateServiceImplTest extends BaseDbUnitTest { @MockBean private SmsClient smsClient; + @Test + public void testFormatSmsTemplateContent() { + // 准备参数 + String content = "正在进行登录操作{operation},您的验证码是{code}"; + Map params = MapUtil.builder("operation", "登录") + .put("code", "1234").build(); + + // 调用 + String result = smsTemplateService.formatSmsTemplateContent(content, params); + // 断言 + assertEquals("正在进行登录操作登录,您的验证码是1234", result); + } + @Test public void testParseTemplateContentParams() { // 准备参数 @@ -156,6 +171,34 @@ public class SmsTemplateServiceImplTest extends BaseDbUnitTest { 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 public void testGetSmsTemplatePage() { // mock 数据 @@ -201,6 +244,22 @@ public class SmsTemplateServiceImplTest extends BaseDbUnitTest { 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 public void testValidateSmsChannel_success() { // 准备参数 diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImplTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImplTest.java index 62e7808c8..87d66ea6c 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImplTest.java +++ b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImplTest.java @@ -331,6 +331,18 @@ public class TenantServiceImplTest extends BaseDbUnitTest { 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 public void testGetTenantListByPackageId() { // mock 数据