Commit 1740128dc3ef779649d9215d3860c6b0d54a99ae
1 parent
2f75a716
删除短信渠道时,增加校验是否有模板
Showing
7 changed files
with
48 additions
and
3 deletions
src/main/java/cn/iocoder/dashboard/modules/system/controller/sms/vo/channel/SysSmsChannelBaseVO.java
src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/sms/SysSmsTemplateMapper.java
src/main/java/cn/iocoder/dashboard/modules/system/enums/SysErrorCodeConstants.java
| ... | ... | @@ -81,6 +81,7 @@ public interface SysErrorCodeConstants { |
| 81 | 81 | // ========== 短信渠道 1002011000 ========== |
| 82 | 82 | ErrorCode SMS_CHANNEL_NOT_EXISTS = new ErrorCode(1002011000, "短信渠道不存在"); |
| 83 | 83 | ErrorCode SMS_CHANNEL_DISABLE = new ErrorCode(1002011001, "短信渠道不处于开启状态,不允许选择"); |
| 84 | + ErrorCode SMS_CHANNEL_HAS_CHILDREN = new ErrorCode(1002011002, "无法删除,该短信渠道还有短信模板"); | |
| 84 | 85 | |
| 85 | 86 | // ========== 短信模板 1002011000 ========== |
| 86 | 87 | ErrorCode SMS_TEMPLATE_NOT_EXISTS = new ErrorCode(1002011000, "短信模板不存在"); | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsTemplateService.java
| ... | ... | @@ -91,4 +91,12 @@ public interface SysSmsTemplateService { |
| 91 | 91 | */ |
| 92 | 92 | List<SysSmsTemplateDO> getSmsTemplateList(SysSmsTemplateExportReqVO exportReqVO); |
| 93 | 93 | |
| 94 | + /** | |
| 95 | + * 获得指定短信渠道下的短信模板数量 | |
| 96 | + * | |
| 97 | + * @param channelId 短信渠道编号 | |
| 98 | + * @return 数量 | |
| 99 | + */ | |
| 100 | + Integer countByChannelId(Long channelId); | |
| 101 | + | |
| 94 | 102 | } | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsChannelServiceImpl.java
| ... | ... | @@ -11,6 +11,7 @@ import cn.iocoder.dashboard.modules.system.convert.sms.SysSmsChannelConvert; |
| 11 | 11 | import cn.iocoder.dashboard.modules.system.dal.dataobject.sms.SysSmsChannelDO; |
| 12 | 12 | import cn.iocoder.dashboard.modules.system.dal.mysql.sms.SysSmsChannelMapper; |
| 13 | 13 | import cn.iocoder.dashboard.modules.system.service.sms.SysSmsChannelService; |
| 14 | +import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService; | |
| 14 | 15 | import org.springframework.stereotype.Service; |
| 15 | 16 | |
| 16 | 17 | import javax.annotation.PostConstruct; |
| ... | ... | @@ -19,6 +20,7 @@ import java.util.Collection; |
| 19 | 20 | import java.util.List; |
| 20 | 21 | |
| 21 | 22 | import static cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil.exception; |
| 23 | +import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_HAS_CHILDREN; | |
| 22 | 24 | import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_NOT_EXISTS; |
| 23 | 25 | |
| 24 | 26 | /** |
| ... | ... | @@ -36,6 +38,9 @@ public class SysSmsChannelServiceImpl implements SysSmsChannelService { |
| 36 | 38 | @Resource |
| 37 | 39 | private SysSmsChannelMapper smsChannelMapper; |
| 38 | 40 | |
| 41 | + @Resource | |
| 42 | + private SysSmsTemplateService smsTemplateService; | |
| 43 | + | |
| 39 | 44 | @Override |
| 40 | 45 | @PostConstruct |
| 41 | 46 | public void initSmsClients() { |
| ... | ... | @@ -70,6 +75,10 @@ public class SysSmsChannelServiceImpl implements SysSmsChannelService { |
| 70 | 75 | public void deleteSmsChannel(Long id) { |
| 71 | 76 | // 校验存在 |
| 72 | 77 | this.validateSmsChannelExists(id); |
| 78 | + // 校验是否有字典数据 | |
| 79 | + if (smsTemplateService.countByChannelId(id) > 0) { | |
| 80 | + throw exception(SMS_CHANNEL_HAS_CHILDREN); | |
| 81 | + } | |
| 73 | 82 | // 更新 |
| 74 | 83 | smsChannelMapper.deleteById(id); |
| 75 | 84 | } | ... | ... |
src/main/java/cn/iocoder/dashboard/modules/system/service/sms/impl/SysSmsTemplateServiceImpl.java
| ... | ... | @@ -21,7 +21,6 @@ import cn.iocoder.dashboard.modules.system.service.sms.SysSmsTemplateService; |
| 21 | 21 | import com.google.common.annotations.VisibleForTesting; |
| 22 | 22 | import org.springframework.stereotype.Service; |
| 23 | 23 | import org.springframework.util.Assert; |
| 24 | -import org.springframework.validation.annotation.Validated; | |
| 25 | 24 | |
| 26 | 25 | import javax.annotation.Resource; |
| 27 | 26 | import java.util.Collection; |
| ... | ... | @@ -40,7 +39,6 @@ import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*; |
| 40 | 39 | * @date 2021/1/25 9:25 |
| 41 | 40 | */ |
| 42 | 41 | @Service |
| 43 | -@Validated | |
| 44 | 42 | public class SysSmsTemplateServiceImpl implements SysSmsTemplateService { |
| 45 | 43 | |
| 46 | 44 | /** |
| ... | ... | @@ -142,6 +140,11 @@ public class SysSmsTemplateServiceImpl implements SysSmsTemplateService { |
| 142 | 140 | return smsTemplateMapper.selectList(exportReqVO); |
| 143 | 141 | } |
| 144 | 142 | |
| 143 | + @Override | |
| 144 | + public Integer countByChannelId(Long channelId) { | |
| 145 | + return smsTemplateMapper.selectCountByChannelId(channelId); | |
| 146 | + } | |
| 147 | + | |
| 145 | 148 | @VisibleForTesting |
| 146 | 149 | public SysSmsChannelDO checkSmsChannel(Long channelId) { |
| 147 | 150 | SysSmsChannelDO channelDO = smsChannelService.getSmsChannel(channelId); | ... | ... |
src/test/java/cn/iocoder/dashboard/modules/system/service/sms/SysSmsChannelServiceTest.java
| ... | ... | @@ -20,12 +20,15 @@ import javax.annotation.Resource; |
| 20 | 20 | import java.util.function.Consumer; |
| 21 | 21 | |
| 22 | 22 | import static cn.hutool.core.util.RandomUtil.randomEle; |
| 23 | +import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_HAS_CHILDREN; | |
| 23 | 24 | import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.SMS_CHANNEL_NOT_EXISTS; |
| 24 | 25 | import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; |
| 25 | 26 | import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; |
| 26 | 27 | import static cn.iocoder.dashboard.util.RandomUtils.*; |
| 27 | 28 | import static cn.iocoder.dashboard.util.date.DateUtils.buildTime; |
| 28 | 29 | import static org.junit.jupiter.api.Assertions.*; |
| 30 | +import static org.mockito.ArgumentMatchers.eq; | |
| 31 | +import static org.mockito.Mockito.when; | |
| 29 | 32 | |
| 30 | 33 | /** |
| 31 | 34 | * {@link SysSmsChannelServiceImpl} 的单元测试类 |
| ... | ... | @@ -44,6 +47,9 @@ public class SysSmsChannelServiceTest extends BaseDbUnitTest { |
| 44 | 47 | @Resource |
| 45 | 48 | private SysSmsChannelMapper smsChannelMapper; |
| 46 | 49 | |
| 50 | + @MockBean | |
| 51 | + private SysSmsTemplateService smsTemplateService; | |
| 52 | + | |
| 47 | 53 | @Test |
| 48 | 54 | public void testCreateSmsChannel_success() { |
| 49 | 55 | // 准备参数 |
| ... | ... | @@ -67,6 +73,7 @@ public class SysSmsChannelServiceTest extends BaseDbUnitTest { |
| 67 | 73 | SysSmsChannelUpdateReqVO reqVO = randomPojo(SysSmsChannelUpdateReqVO.class, o -> { |
| 68 | 74 | o.setId(dbSmsChannel.getId()); // 设置更新的 ID |
| 69 | 75 | o.setStatus(randomCommonStatus()); |
| 76 | + o.setCallbackUrl(randomString()); | |
| 70 | 77 | }); |
| 71 | 78 | |
| 72 | 79 | // 调用 |
| ... | ... | @@ -109,6 +116,20 @@ public class SysSmsChannelServiceTest extends BaseDbUnitTest { |
| 109 | 116 | } |
| 110 | 117 | |
| 111 | 118 | @Test |
| 119 | + public void testDeleteSmsChannel_hasChildren() { | |
| 120 | + // mock 数据 | |
| 121 | + SysSmsChannelDO dbSmsChannel = randomSmsChannelDO(); | |
| 122 | + smsChannelMapper.insert(dbSmsChannel);// @Sql: 先插入出一条存在的数据 | |
| 123 | + // 准备参数 | |
| 124 | + Long id = dbSmsChannel.getId(); | |
| 125 | + // mock 方法 | |
| 126 | + when(smsTemplateService.countByChannelId(eq(id))).thenReturn(10); | |
| 127 | + | |
| 128 | + // 调用, 并断言异常 | |
| 129 | + assertServiceException(() -> smsChannelService.deleteSmsChannel(id), SMS_CHANNEL_HAS_CHILDREN); | |
| 130 | + } | |
| 131 | + | |
| 132 | + @Test | |
| 112 | 133 | public void testGetSmsChannelPage() { |
| 113 | 134 | // mock 数据 |
| 114 | 135 | SysSmsChannelDO dbSmsChannel = randomPojo(SysSmsChannelDO.class, o -> { // 等会查询到 | ... | ... |