潘志宝
8 天以前 81ce77b2e152ffbedd61d3a768b5b292701a7c64
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.infra.service.db;
H 2
3 import cn.hutool.core.map.MapUtil;
4 import cn.hutool.core.util.ReflectUtil;
5 import cn.hutool.crypto.symmetric.AES;
6 import com.iailab.framework.mybatis.core.type.EncryptTypeHandler;
7 import com.iailab.framework.mybatis.core.util.JdbcUtils;
8 import com.iailab.framework.test.core.ut.BaseDbUnitTest;
9 import com.iailab.module.infra.controller.admin.db.vo.DataSourceConfigSaveReqVO;
10 import com.iailab.module.infra.dal.dataobject.db.DataSourceConfigDO;
11 import com.iailab.module.infra.dal.mysql.db.DataSourceConfigMapper;
12 import com.baomidou.dynamic.datasource.creator.DataSourceProperty;
13 import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
14 import org.junit.jupiter.api.BeforeEach;
15 import org.junit.jupiter.api.Test;
16 import org.mockito.MockedStatic;
17 import org.mockito.stubbing.Answer;
18 import org.springframework.boot.test.mock.mockito.MockBean;
19 import org.springframework.context.annotation.Import;
20
21 import javax.annotation.Resource;
22 import java.util.List;
23
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.randomLongId;
27 import static com.iailab.framework.test.core.util.RandomUtils.randomPojo;
28 import static com.iailab.module.infra.enums.ErrorCodeConstants.DATA_SOURCE_CONFIG_NOT_EXISTS;
29 import static org.junit.jupiter.api.Assertions.*;
30 import static org.mockito.ArgumentMatchers.anyString;
31 import static org.mockito.ArgumentMatchers.eq;
32 import static org.mockito.Mockito.mockStatic;
33 import static org.mockito.Mockito.when;
34
35 /**
36  * {@link DataSourceConfigServiceImpl} 的单元测试类
37  *
38  * @author iailab
39  */
40 @Import(DataSourceConfigServiceImpl.class)
41 public class DataSourceConfigServiceImplTest extends BaseDbUnitTest {
42
43     @Resource
44     private DataSourceConfigServiceImpl dataSourceConfigService;
45
46     @Resource
47     private DataSourceConfigMapper dataSourceConfigMapper;
48
49     @MockBean
50     private AES aes;
51
52     @MockBean
53     private DynamicDataSourceProperties dynamicDataSourceProperties;
54
55     @BeforeEach
56     public void setUp() {
57         // mock 一个空实现的 StringEncryptor,避免 EncryptTypeHandler 报错
58         ReflectUtil.setFieldValue(EncryptTypeHandler.class, "aes", aes);
59         when(aes.encryptBase64(anyString())).then((Answer<String>) invocation -> invocation.getArgument(0));
60         when(aes.decryptStr(anyString())).then((Answer<String>) invocation -> invocation.getArgument(0));
61
62         // mock DynamicDataSourceProperties
63         when(dynamicDataSourceProperties.getPrimary()).thenReturn("primary");
64         DataSourceProperty dataSourceProperty = new DataSourceProperty();
65         dataSourceProperty.setUrl("http://localhost:3306");
66         dataSourceProperty.setUsername("yunai");
67         dataSourceProperty.setPassword("tudou");
68         when(dynamicDataSourceProperties.getDatasource()).thenReturn(MapUtil.of("primary", dataSourceProperty));
69     }
70
71     @Test
72     public void testCreateDataSourceConfig_success() {
73         try (MockedStatic<JdbcUtils> databaseUtilsMock = mockStatic(JdbcUtils.class)) {
74             // 准备参数
75             DataSourceConfigSaveReqVO reqVO = randomPojo(DataSourceConfigSaveReqVO.class)
76                     .setId(null); // 避免 id 被设置
77             // mock 方法
78             databaseUtilsMock.when(() -> JdbcUtils.isConnectionOK(eq(reqVO.getUrl()),
79                     eq(reqVO.getUsername()), eq(reqVO.getPassword()))).thenReturn(true);
80
81             // 调用
82             Long dataSourceConfigId = dataSourceConfigService.createDataSourceConfig(reqVO);
83             // 断言
84             assertNotNull(dataSourceConfigId);
85             // 校验记录的属性是否正确
86             DataSourceConfigDO dataSourceConfig = dataSourceConfigMapper.selectById(dataSourceConfigId);
87             assertPojoEquals(reqVO, dataSourceConfig, "id");
88         }
89     }
90
91     @Test
92     public void testUpdateDataSourceConfig_success() {
93         try (MockedStatic<JdbcUtils> databaseUtilsMock = mockStatic(JdbcUtils.class)) {
94             // mock 数据
95             DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
96             dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
97             // 准备参数
98             DataSourceConfigSaveReqVO reqVO = randomPojo(DataSourceConfigSaveReqVO.class, o -> {
99                 o.setId(dbDataSourceConfig.getId()); // 设置更新的 ID
100             });
101             // mock 方法
102             databaseUtilsMock.when(() -> JdbcUtils.isConnectionOK(eq(reqVO.getUrl()),
103                     eq(reqVO.getUsername()), eq(reqVO.getPassword()))).thenReturn(true);
104
105             // 调用
106             dataSourceConfigService.updateDataSourceConfig(reqVO);
107             // 校验是否更新正确
108             DataSourceConfigDO dataSourceConfig = dataSourceConfigMapper.selectById(reqVO.getId()); // 获取最新的
109             assertPojoEquals(reqVO, dataSourceConfig);
110         }
111     }
112
113     @Test
114     public void testUpdateDataSourceConfig_notExists() {
115         // 准备参数
116         DataSourceConfigSaveReqVO reqVO = randomPojo(DataSourceConfigSaveReqVO.class);
117
118         // 调用, 并断言异常
119         assertServiceException(() -> dataSourceConfigService.updateDataSourceConfig(reqVO), DATA_SOURCE_CONFIG_NOT_EXISTS);
120     }
121
122     @Test
123     public void testDeleteDataSourceConfig_success() {
124         // mock 数据
125         DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
126         dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
127         // 准备参数
128         Long id = dbDataSourceConfig.getId();
129
130         // 调用
131         dataSourceConfigService.deleteDataSourceConfig(id);
132         // 校验数据不存在了
133         assertNull(dataSourceConfigMapper.selectById(id));
134     }
135
136     @Test
137     public void testDeleteDataSourceConfig_notExists() {
138         // 准备参数
139         Long id = randomLongId();
140
141         // 调用, 并断言异常
142         assertServiceException(() -> dataSourceConfigService.deleteDataSourceConfig(id), DATA_SOURCE_CONFIG_NOT_EXISTS);
143     }
144
145     @Test // 测试使用 password 查询,可以查询到数据
146     public void testSelectPassword() {
147         // mock 数据
148         DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
149         dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
150
151         // 调用
152         DataSourceConfigDO result = dataSourceConfigMapper.selectOne(DataSourceConfigDO::getPassword,
153                 EncryptTypeHandler.encrypt(dbDataSourceConfig.getPassword()));
154         assertPojoEquals(dbDataSourceConfig, result);
155     }
156
157     @Test
158     public void testGetDataSourceConfig_master() {
159         // 准备参数
160         Long id = 0L;
161         // mock 方法
162
163         // 调用
164         DataSourceConfigDO dataSourceConfig = dataSourceConfigService.getDataSourceConfig(id);
165         // 断言
166         assertEquals(id, dataSourceConfig.getId());
167         assertEquals("primary", dataSourceConfig.getName());
168         assertEquals("http://localhost:3306", dataSourceConfig.getUrl());
169         assertEquals("yunai", dataSourceConfig.getUsername());
170         assertEquals("tudou", dataSourceConfig.getPassword());
171     }
172
173     @Test
174     public void testGetDataSourceConfig_normal() {
175         // mock 数据
176         DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
177         dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
178         // 准备参数
179         Long id = dbDataSourceConfig.getId();
180
181         // 调用
182         DataSourceConfigDO dataSourceConfig = dataSourceConfigService.getDataSourceConfig(id);
183         // 断言
184         assertPojoEquals(dbDataSourceConfig, dataSourceConfig);
185     }
186
187     @Test
188     public void testGetDataSourceConfigList() {
189         // mock 数据
190         DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
191         dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
192         // 准备参数
193
194         // 调用
195         List<DataSourceConfigDO> dataSourceConfigList = dataSourceConfigService.getDataSourceConfigList();
196         // 断言
197         assertEquals(2, dataSourceConfigList.size());
198         // master
199         assertEquals(0L, dataSourceConfigList.get(0).getId());
200         assertEquals("primary", dataSourceConfigList.get(0).getName());
201         assertEquals("http://localhost:3306", dataSourceConfigList.get(0).getUrl());
202         assertEquals("yunai", dataSourceConfigList.get(0).getUsername());
203         assertEquals("tudou", dataSourceConfigList.get(0).getPassword());
204         // normal
205         assertPojoEquals(dbDataSourceConfig, dataSourceConfigList.get(1));
206     }
207
208 }