houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.system.service.sms;
H 2
3 import com.iailab.framework.common.enums.CommonStatusEnum;
4 import com.iailab.framework.common.pojo.PageResult;
5 import com.iailab.framework.common.util.object.BeanUtils;
6 import com.iailab.module.system.framework.sms.core.client.SmsClient;
7 import com.iailab.module.system.framework.sms.core.client.SmsClientFactory;
8 import com.iailab.module.system.framework.sms.core.property.SmsChannelProperties;
9 import com.iailab.framework.test.core.ut.BaseDbUnitTest;
10 import com.iailab.module.system.controller.admin.sms.vo.channel.SmsChannelPageReqVO;
11 import com.iailab.module.system.controller.admin.sms.vo.channel.SmsChannelSaveReqVO;
12 import com.iailab.module.system.dal.dataobject.sms.SmsChannelDO;
13 import com.iailab.module.system.dal.mysql.sms.SmsChannelMapper;
14 import org.junit.jupiter.api.Test;
15 import org.springframework.boot.test.mock.mockito.MockBean;
16 import org.springframework.context.annotation.Import;
17
18 import javax.annotation.Resource;
19 import java.util.List;
20
21 import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
22 import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildTime;
23 import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId;
24 import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals;
25 import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException;
26 import static com.iailab.framework.test.core.util.RandomUtils.*;
27 import static com.iailab.module.system.enums.ErrorCodeConstants.SMS_CHANNEL_HAS_CHILDREN;
28 import static com.iailab.module.system.enums.ErrorCodeConstants.SMS_CHANNEL_NOT_EXISTS;
29 import static org.junit.jupiter.api.Assertions.*;
30 import static org.mockito.ArgumentMatchers.eq;
31 import static org.mockito.Mockito.*;
32
33 @Import(SmsChannelServiceImpl.class)
34 public class SmsChannelServiceTest extends BaseDbUnitTest {
35
36     @Resource
37     private SmsChannelServiceImpl smsChannelService;
38
39     @Resource
40     private SmsChannelMapper smsChannelMapper;
41
42     @MockBean
43     private SmsClientFactory smsClientFactory;
44     @MockBean
45     private SmsTemplateService smsTemplateService;
46
47     @Test
48     public void testCreateSmsChannel_success() {
49         // 准备参数
50         SmsChannelSaveReqVO reqVO = randomPojo(SmsChannelSaveReqVO.class, o -> o.setStatus(randomCommonStatus()))
51                 .setId(null); // 防止 id 被赋值
52
53         // 调用
54         Long smsChannelId = smsChannelService.createSmsChannel(reqVO);
55         // 断言
56         assertNotNull(smsChannelId);
57         // 校验记录的属性是否正确
58         SmsChannelDO smsChannel = smsChannelMapper.selectById(smsChannelId);
59         assertPojoEquals(reqVO, smsChannel, "id");
60         // 断言 cache
61         assertNull(smsChannelService.getIdClientCache().getIfPresent(smsChannel.getId()));
62         assertNull(smsChannelService.getCodeClientCache().getIfPresent(smsChannel.getCode()));
63     }
64
65     @Test
66     public void testUpdateSmsChannel_success() {
67         // mock 数据
68         SmsChannelDO dbSmsChannel = randomPojo(SmsChannelDO.class);
69         smsChannelMapper.insert(dbSmsChannel);// @Sql: 先插入出一条存在的数据
70         // 准备参数
71         SmsChannelSaveReqVO reqVO = randomPojo(SmsChannelSaveReqVO.class, o -> {
72             o.setId(dbSmsChannel.getId()); // 设置更新的 ID
73             o.setStatus(randomCommonStatus());
74             o.setCallbackUrl(randomString());
75         });
76
77         // 调用
78         smsChannelService.updateSmsChannel(reqVO);
79         // 校验是否更新正确
80         SmsChannelDO smsChannel = smsChannelMapper.selectById(reqVO.getId()); // 获取最新的
81         assertPojoEquals(reqVO, smsChannel);
82         // 断言 cache
83         assertNull(smsChannelService.getIdClientCache().getIfPresent(smsChannel.getId()));
84         assertNull(smsChannelService.getCodeClientCache().getIfPresent(smsChannel.getCode()));
85     }
86
87     @Test
88     public void testUpdateSmsChannel_notExists() {
89         // 准备参数
90         SmsChannelSaveReqVO reqVO = randomPojo(SmsChannelSaveReqVO.class);
91
92         // 调用, 并断言异常
93         assertServiceException(() -> smsChannelService.updateSmsChannel(reqVO), SMS_CHANNEL_NOT_EXISTS);
94     }
95
96     @Test
97     public void testDeleteSmsChannel_success() {
98         // mock 数据
99         SmsChannelDO dbSmsChannel = randomPojo(SmsChannelDO.class);
100         smsChannelMapper.insert(dbSmsChannel);// @Sql: 先插入出一条存在的数据
101         // 准备参数
102         Long id = dbSmsChannel.getId();
103
104         // 调用
105         smsChannelService.deleteSmsChannel(id);
106         // 校验数据不存在了
107         assertNull(smsChannelMapper.selectById(id));
108         // 断言 cache
109         assertNull(smsChannelService.getIdClientCache().getIfPresent(dbSmsChannel.getId()));
110         assertNull(smsChannelService.getCodeClientCache().getIfPresent(dbSmsChannel.getCode()));
111     }
112
113     @Test
114     public void testDeleteSmsChannel_notExists() {
115         // 准备参数
116         Long id = randomLongId();
117
118         // 调用, 并断言异常
119         assertServiceException(() -> smsChannelService.deleteSmsChannel(id), SMS_CHANNEL_NOT_EXISTS);
120     }
121
122     @Test
123     public void testDeleteSmsChannel_hasChildren() {
124         // mock 数据
125         SmsChannelDO dbSmsChannel = randomPojo(SmsChannelDO.class);
126         smsChannelMapper.insert(dbSmsChannel);// @Sql: 先插入出一条存在的数据
127         // 准备参数
128         Long id = dbSmsChannel.getId();
129         // mock 方法
130         when(smsTemplateService.getSmsTemplateCountByChannelId(eq(id))).thenReturn(10L);
131
132         // 调用, 并断言异常
133         assertServiceException(() -> smsChannelService.deleteSmsChannel(id), SMS_CHANNEL_HAS_CHILDREN);
134     }
135
136     @Test
137     public void testGetSmsChannel() {
138         // mock 数据
139         SmsChannelDO dbSmsChannel = randomPojo(SmsChannelDO.class);
140         smsChannelMapper.insert(dbSmsChannel); // @Sql: 先插入出一条存在的数据
141         // 准备参数
142         Long id = dbSmsChannel.getId();
143
144         // 调用,并断言
145         assertPojoEquals(dbSmsChannel, smsChannelService.getSmsChannel(id));
146     }
147
148     @Test
149     public void testGetSmsChannelList() {
150         // mock 数据
151         SmsChannelDO dbSmsChannel01 = randomPojo(SmsChannelDO.class);
152         smsChannelMapper.insert(dbSmsChannel01);
153         SmsChannelDO dbSmsChannel02 = randomPojo(SmsChannelDO.class);
154         smsChannelMapper.insert(dbSmsChannel02);
155         // 准备参数
156
157         // 调用
158         List<SmsChannelDO> list = smsChannelService.getSmsChannelList();
159         // 断言
160         assertEquals(2, list.size());
161         assertPojoEquals(dbSmsChannel01, list.get(0));
162         assertPojoEquals(dbSmsChannel02, list.get(1));
163     }
164
165     @Test
166     public void testGetSmsChannelPage() {
167        // mock 数据
168        SmsChannelDO dbSmsChannel = randomPojo(SmsChannelDO.class, o -> { // 等会查询到
169            o.setSignature("iailab");
170            o.setStatus(CommonStatusEnum.ENABLE.getStatus());
171            o.setCreateTime(buildTime(2020, 12, 12));
172        });
173        smsChannelMapper.insert(dbSmsChannel);
174        // 测试 signature 不匹配
175        smsChannelMapper.insert(cloneIgnoreId(dbSmsChannel, o -> o.setSignature("源码")));
176        // 测试 status 不匹配
177        smsChannelMapper.insert(cloneIgnoreId(dbSmsChannel, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
178        // 测试 createTime 不匹配
179        smsChannelMapper.insert(cloneIgnoreId(dbSmsChannel, o -> o.setCreateTime(buildTime(2020, 11, 11))));
180        // 准备参数
181        SmsChannelPageReqVO reqVO = new SmsChannelPageReqVO();
182        reqVO.setSignature("平台");
183        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
184        reqVO.setCreateTime(buildBetweenTime(2020, 12, 1, 2020, 12, 24));
185
186        // 调用
187        PageResult<SmsChannelDO> pageResult = smsChannelService.getSmsChannelPage(reqVO);
188        // 断言
189        assertEquals(1, pageResult.getTotal());
190        assertEquals(1, pageResult.getList().size());
191        assertPojoEquals(dbSmsChannel, pageResult.getList().get(0));
192     }
193
194     @Test
195     public void testGetSmsClient_id() {
196         // mock 数据
197         SmsChannelDO channel = randomPojo(SmsChannelDO.class);
198         smsChannelMapper.insert(channel);
199         // mock 参数
200         Long id = channel.getId();
201         // mock 方法
202         SmsClient mockClient = mock(SmsClient.class);
203         when(smsClientFactory.getSmsClient(eq(id))).thenReturn(mockClient);
204
205         // 调用
206         SmsClient client = smsChannelService.getSmsClient(id);
207         // 断言
208         assertSame(client, mockClient);
209         verify(smsClientFactory).createOrUpdateSmsClient(argThat(arg -> {
210             SmsChannelProperties properties = BeanUtils.toBean(channel, SmsChannelProperties.class);
211             return properties.equals(arg);
212         }));
213     }
214
215     @Test
216     public void testGetSmsClient_code() {
217         // mock 数据
218         SmsChannelDO channel = randomPojo(SmsChannelDO.class);
219         smsChannelMapper.insert(channel);
220         // mock 参数
221         String code = channel.getCode();
222         // mock 方法
223         SmsClient mockClient = mock(SmsClient.class);
224         when(smsClientFactory.getSmsClient(eq(code))).thenReturn(mockClient);
225
226         // 调用
227         SmsClient client = smsChannelService.getSmsClient(code);
228         // 断言
229         assertSame(client, mockClient);
230         verify(smsClientFactory).createOrUpdateSmsClient(argThat(arg -> {
231             SmsChannelProperties properties = BeanUtils.toBean(channel, SmsChannelProperties.class);
232             return properties.equals(arg);
233         }));
234     }
235
236 }