diff --git a/src/main/java/cn/iocoder/dashboard/framework/redis/config/RedisConfig.java b/src/main/java/cn/iocoder/dashboard/framework/redis/config/RedisConfig.java index 432d5618c..52bef8059 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/redis/config/RedisConfig.java +++ b/src/main/java/cn/iocoder/dashboard/framework/redis/config/RedisConfig.java @@ -48,8 +48,8 @@ public class RedisConfig { * 创建 Redis Pub/Sub 广播消费的容器 */ @Bean - public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory factory, - List> listeners) { + public RedisMessageListenerContainer redisMessageListenerContainer( + RedisConnectionFactory factory, List> listeners) { // 创建 RedisMessageListenerContainer 对象 RedisMessageListenerContainer container = new RedisMessageListenerContainer(); // 设置 RedisConnection 工厂。 @@ -69,8 +69,8 @@ public class RedisConfig { * Redis Stream 的 xreadgroup 命令:https://www.geek-book.com/src/docs/redis/redis/redis.io/commands/xreadgroup.html */ @Bean(initMethod = "start", destroyMethod = "stop") - public StreamMessageListenerContainer> redisStreamMessageListenerContainer(RedisTemplate redisTemplate, - List> listeners) { + public StreamMessageListenerContainer> redisStreamMessageListenerContainer( + RedisTemplate redisTemplate, List> listeners) { // 第一步,创建 StreamMessageListenerContainer 容器 // 创建 options 配置 StreamMessageListenerContainer.StreamMessageListenerContainerOptions> containerOptions = diff --git a/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/impl/SmsClientFactoryImpl.java b/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/impl/SmsClientFactoryImpl.java index ddfe2302f..4f0db5c02 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/impl/SmsClientFactoryImpl.java +++ b/src/main/java/cn/iocoder/dashboard/framework/sms/core/client/impl/SmsClientFactoryImpl.java @@ -38,6 +38,7 @@ public class SmsClientFactoryImpl implements SmsClientFactory { AbstractSmsClient client = clients.get(properties.getId()); if (client == null) { client = this.createSmsClient(properties); + client.init(); clients.put(client.getId(), client); } else { client.refresh(properties); diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/mq/consumer/sms/SmsSendConsumer.java b/src/main/java/cn/iocoder/dashboard/modules/system/mq/consumer/sms/SysSmsSendConsumer.java similarity index 89% rename from src/main/java/cn/iocoder/dashboard/modules/system/mq/consumer/sms/SmsSendConsumer.java rename to src/main/java/cn/iocoder/dashboard/modules/system/mq/consumer/sms/SysSmsSendConsumer.java index 1e63ca662..70b167168 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/mq/consumer/sms/SmsSendConsumer.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/mq/consumer/sms/SysSmsSendConsumer.java @@ -16,7 +16,7 @@ import javax.annotation.Resource; */ @Component @Slf4j -public class SmsSendConsumer extends AbstractStreamMessageListener { +public class SysSmsSendConsumer extends AbstractStreamMessageListener { @Resource private SysSmsService smsService; diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsLogService.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsLogService.java index fd04a62b5..a7a6f8fee 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsLogService.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsLogService.java @@ -5,7 +5,7 @@ import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO; import java.util.Map; /** - * 短信日志服务接口 + * 短信日志 Service 实现类 * * @author zzf * @date 13:48 2021/3/2 diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsSendLogServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsLogServiceImpl.java similarity index 96% rename from src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsSendLogServiceImpl.java rename to src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsLogServiceImpl.java index 00c43e8f1..32f4883e0 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsSendLogServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsLogServiceImpl.java @@ -15,14 +15,14 @@ import java.util.Map; import java.util.Objects; /** - * 短信发送日志服务实现类 + * 短信日志 Service 实现类 * * @author zzf * @date 2021/1/25 9:25 */ @Slf4j @Service -public class SysSmsSendLogServiceImpl implements SysSmsLogService { +public class SysSmsLogServiceImpl implements SysSmsLogService { @Resource private SysSmsLogMapper smsLogMapper; diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsServiceImpl.java index 959a997b0..87f8298da 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsServiceImpl.java @@ -12,7 +12,6 @@ import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsTemplateDO; import cn.iocoder.dashboard.modules.system.dal.dataobject.user.SysUserDO; import cn.iocoder.dashboard.modules.system.mq.message.sms.SysSmsSendMessage; import cn.iocoder.dashboard.modules.system.mq.producer.sms.SysSmsProducer; -import cn.iocoder.dashboard.modules.system.service.sms.SysSmsChannelService; import cn.iocoder.dashboard.modules.system.service.sms.SysSmsLogService; import cn.iocoder.dashboard.modules.system.service.sms.SysSmsService; import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService; @@ -41,8 +40,6 @@ import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*; @Slf4j public class SysSmsServiceImpl implements SysSmsService { - @Resource - private SysSmsChannelService smsChannelService; @Resource private SysSmsTemplateService smsTemplateService; @Resource @@ -145,7 +142,8 @@ public class SysSmsServiceImpl implements SysSmsService { SmsCommonResult sendResult = smsClient.send(message.getLogId(), message.getMobile(), message.getApiTemplateId(), message.getTemplateParams()); smsLogService.updateSmsSendResult(message.getLogId(), sendResult.getCode(), sendResult.getMsg(), - sendResult.getApiCode(), sendResult.getApiMsg(), sendResult.getApiRequestId(), sendResult.getData().getSerialNo()); + sendResult.getApiCode(), sendResult.getApiMsg(), sendResult.getApiRequestId(), + sendResult.getData() != null ? sendResult.getData().getSerialNo() : null); } @Override diff --git a/src/test-integration/java/cn/iocoder/dashboard/BaseDbAndRedisIntegrationTest.java b/src/test-integration/java/cn/iocoder/dashboard/BaseDbAndRedisIntegrationTest.java new file mode 100644 index 000000000..98e1721c2 --- /dev/null +++ b/src/test-integration/java/cn/iocoder/dashboard/BaseDbAndRedisIntegrationTest.java @@ -0,0 +1,37 @@ +package cn.iocoder.dashboard; + +import cn.iocoder.dashboard.framework.datasource.config.DataSourceConfiguration; +import cn.iocoder.dashboard.framework.mybatis.config.MybatisConfiguration; +import cn.iocoder.dashboard.framework.redis.config.RedisConfig; +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; +import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; +import org.redisson.spring.starter.RedissonAutoConfiguration; +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = BaseDbAndRedisIntegrationTest.Application.class) +@ActiveProfiles("integration-test") // 设置使用 application-integration-test 配置文件 +public class BaseDbAndRedisIntegrationTest { + + @Import({ + // DB 配置类 + DataSourceConfiguration.class, // 自己的 DB 配置类 + DataSourceAutoConfiguration.class, // Spring DB 自动配置类 + DataSourceTransactionManagerAutoConfiguration.class, // Spring 事务自动配置类 + DruidDataSourceAutoConfigure.class, // Druid 自动配置类 + // MyBatis 配置类 + MybatisConfiguration.class, // 自己的 MyBatis 配置类 + MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类 + // Redis 配置类 + RedisAutoConfiguration.class, // Spring Redis 自动配置类 + RedisConfig.class, // 自己的 Redis 配置类 + RedissonAutoConfiguration.class, // Redisson 自动高配置类 + }) + public static class Application { + } + +} diff --git a/src/test-integration/java/cn/iocoder/dashboard/framework/redis/core/stream/RedisStreamTest.java b/src/test-integration/java/cn/iocoder/dashboard/framework/redis/core/stream/RedisStreamTest.java index c976528ae..888357570 100644 --- a/src/test-integration/java/cn/iocoder/dashboard/framework/redis/core/stream/RedisStreamTest.java +++ b/src/test-integration/java/cn/iocoder/dashboard/framework/redis/core/stream/RedisStreamTest.java @@ -4,7 +4,7 @@ import cn.hutool.core.thread.ThreadUtil; import cn.iocoder.dashboard.BaseRedisIntegrationTest; import cn.iocoder.dashboard.framework.redis.core.util.RedisMessageUtils; import cn.iocoder.dashboard.modules.system.mq.consumer.mail.SysMailSendConsumer; -import cn.iocoder.dashboard.modules.system.mq.consumer.sms.SmsSendConsumer; +import cn.iocoder.dashboard.modules.system.mq.consumer.sms.SysSmsSendConsumer; import cn.iocoder.dashboard.modules.system.mq.message.mail.SysMailSendMessage; import cn.iocoder.dashboard.modules.system.mq.message.sms.SysSmsSendMessage; import org.junit.jupiter.api.Disabled; @@ -18,7 +18,7 @@ import java.util.concurrent.TimeUnit; public class RedisStreamTest { - @Import({SmsSendConsumer.class, SysMailSendConsumer.class}) + @Import({SysSmsSendConsumer.class, SysMailSendConsumer.class}) @Disabled public static class ConsumerTest extends BaseRedisIntegrationTest { diff --git a/src/test-integration/java/cn/iocoder/dashboard/modules/system/service/package-info.java b/src/test-integration/java/cn/iocoder/dashboard/modules/system/service/package-info.java new file mode 100644 index 000000000..09c1d9d14 --- /dev/null +++ b/src/test-integration/java/cn/iocoder/dashboard/modules/system/service/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.dashboard.modules.system.service; diff --git a/src/test-integration/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsServiceIntegrationTest.java b/src/test-integration/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsServiceIntegrationTest.java new file mode 100644 index 000000000..b4d57a719 --- /dev/null +++ b/src/test-integration/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsServiceIntegrationTest.java @@ -0,0 +1,74 @@ +package cn.iocoder.dashboard.modules.system.service.sms; + +import cn.hutool.core.map.MapUtil; +import cn.hutool.core.thread.ThreadUtil; +import cn.iocoder.dashboard.BaseDbAndRedisIntegrationTest; +import cn.iocoder.dashboard.common.enums.UserTypeEnum; +import cn.iocoder.dashboard.framework.sms.config.SmsConfiguration; +import cn.iocoder.dashboard.modules.system.mq.consumer.sms.SysSmsSendConsumer; +import cn.iocoder.dashboard.modules.system.mq.producer.sms.SysSmsProducer; +import cn.iocoder.dashboard.modules.system.service.sms.impl.SysSmsChannelServiceImpl; +import cn.iocoder.dashboard.modules.system.service.sms.impl.SysSmsLogServiceImpl; +import cn.iocoder.dashboard.modules.system.service.sms.impl.SysSmsServiceImpl; +import cn.iocoder.dashboard.modules.system.service.sms.impl.SysSmsTemplateServiceImpl; +import cn.iocoder.dashboard.modules.system.service.user.SysUserService; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; + +import javax.annotation.Resource; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +@Import({SmsConfiguration.class, + SysSmsChannelServiceImpl.class, SysSmsServiceImpl.class, SysSmsTemplateServiceImpl.class, SysSmsLogServiceImpl.class, + SysSmsProducer.class, SysSmsSendConsumer.class}) +public class SysSmsServiceIntegrationTest extends BaseDbAndRedisIntegrationTest { + + @Resource + private SysSmsServiceImpl smsService; + @Resource + private SysSmsChannelServiceImpl smsChannelService; + + @MockBean + private SysUserService userService; + + @Test + public void testSendSingleSms_云片发送成功() { + // 参数准备 + String mobile = "15601691399"; + Long userId = 1L; + Integer userType = UserTypeEnum.ADMIN.getValue(); + String templateCode = "test_01"; + Map templateParams = MapUtil.builder() + .put("operation", "登陆").put("code", "1234").build(); + // 调用 + smsService.sendSingleSms(mobile, userId, userType, templateCode, templateParams); + + // 等待 MQ 消费 + ThreadUtil.sleep(1, TimeUnit.HOURS); + } + + @Test + public void testSendSingleSms_阿里云发送成功() { + // 参数准备 + String mobile = "15601691399"; + Long userId = 1L; + Integer userType = UserTypeEnum.ADMIN.getValue(); + String templateCode = "test_02"; + Map templateParams = MapUtil.builder() + .put("code", "1234").build(); + // 调用 + smsService.sendSingleSms(mobile, userId, userType, templateCode, templateParams); + + // 等待 MQ 消费 + ThreadUtil.sleep(1, TimeUnit.HOURS); + } + +// @Test +// public void testDoSendSms() { +// // 等待 MQ 消费 +// ThreadUtil.sleep(1, TimeUnit.HOURS); +// } + +} diff --git a/src/test-integration/resources/application-integration-test.yaml b/src/test-integration/resources/application-integration-test.yaml index 88b92273c..43a846ee2 100644 --- a/src/test-integration/resources/application-integration-test.yaml +++ b/src/test-integration/resources/application-integration-test.yaml @@ -9,19 +9,15 @@ spring: # 数据源配置项 datasource: name: ruoyi-vue-pro - url: jdbc:h2:mem:testdb;MODE=MYSQL;DATABASE_TO_UPPER=false; # MODE 使用 MySQL 模式;DATABASE_TO_UPPER 配置表和字段使用小写 - driver-class-name: org.h2.Driver - username: sa - password: - schema: classpath:sql/create_tables.sql # MySQL 转 H2 的语句,使用 https://www.jooq.org/translate/ 工具 - druid: - async-init: true # 单元测试,异步初始化 Druid 连接池,提升启动速度 - initial-size: 1 # 单元测试,配置为 1,提升启动速度 + url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.name}?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT + driver-class-name: com.mysql.jdbc.Driver + username: root + password: 123456 # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优 redis: host: 127.0.0.1 # 地址 - port: 6379 # 端口(单元测试,使用 16379 端口) + port: 6379 # 端口 database: 0 # 数据库索引 mybatis: