Commit cc1fa54ea52f051e3f1b2b05f3b9633ea9a6bfe0
1 parent
b5b23b3d
infra:完善 dataSource 的单元测试
Showing
2 changed files
with
64 additions
and
6 deletions
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/db/DataSourceConfigServiceImpl.java
| ... | ... | @@ -37,7 +37,7 @@ public class DataSourceConfigServiceImpl implements DataSourceConfigService { |
| 37 | 37 | @Override |
| 38 | 38 | public Long createDataSourceConfig(DataSourceConfigCreateReqVO createReqVO) { |
| 39 | 39 | DataSourceConfigDO dataSourceConfig = DataSourceConfigConvert.INSTANCE.convert(createReqVO); |
| 40 | - checkConnectionOK(dataSourceConfig); | |
| 40 | + validateConnectionOK(dataSourceConfig); | |
| 41 | 41 | |
| 42 | 42 | // 插入 |
| 43 | 43 | dataSourceConfigMapper.insert(dataSourceConfig); |
| ... | ... | @@ -50,7 +50,7 @@ public class DataSourceConfigServiceImpl implements DataSourceConfigService { |
| 50 | 50 | // 校验存在 |
| 51 | 51 | validateDataSourceConfigExists(updateReqVO.getId()); |
| 52 | 52 | DataSourceConfigDO updateObj = DataSourceConfigConvert.INSTANCE.convert(updateReqVO); |
| 53 | - checkConnectionOK(updateObj); | |
| 53 | + validateConnectionOK(updateObj); | |
| 54 | 54 | |
| 55 | 55 | // 更新 |
| 56 | 56 | dataSourceConfigMapper.updateById(updateObj); |
| ... | ... | @@ -88,7 +88,7 @@ public class DataSourceConfigServiceImpl implements DataSourceConfigService { |
| 88 | 88 | return result; |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | - private void checkConnectionOK(DataSourceConfigDO config) { | |
| 91 | + private void validateConnectionOK(DataSourceConfigDO config) { | |
| 92 | 92 | boolean success = JdbcUtils.isConnectionOK(config.getUrl(), config.getUsername(), config.getPassword()); |
| 93 | 93 | if (!success) { |
| 94 | 94 | throw exception(DATA_SOURCE_CONFIG_NOT_OK); | ... | ... |
yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/db/DataSourceConfigServiceImplTest.java
| 1 | 1 | package cn.iocoder.yudao.module.infra.service.db; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.map.MapUtil; | |
| 3 | 4 | import cn.hutool.core.util.ReflectUtil; |
| 4 | 5 | import cn.hutool.crypto.symmetric.AES; |
| 5 | 6 | import cn.iocoder.yudao.framework.mybatis.core.type.EncryptTypeHandler; |
| ... | ... | @@ -9,6 +10,7 @@ import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigCrea |
| 9 | 10 | import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigUpdateReqVO; |
| 10 | 11 | import cn.iocoder.yudao.module.infra.dal.dataobject.db.DataSourceConfigDO; |
| 11 | 12 | import cn.iocoder.yudao.module.infra.dal.mysql.db.DataSourceConfigMapper; |
| 13 | +import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty; | |
| 12 | 14 | import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties; |
| 13 | 15 | import org.junit.jupiter.api.BeforeEach; |
| 14 | 16 | import org.junit.jupiter.api.Test; |
| ... | ... | @@ -18,14 +20,14 @@ import org.springframework.boot.test.mock.mockito.MockBean; |
| 18 | 20 | import org.springframework.context.annotation.Import; |
| 19 | 21 | |
| 20 | 22 | import javax.annotation.Resource; |
| 23 | +import java.util.List; | |
| 21 | 24 | |
| 22 | 25 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 23 | 26 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 24 | 27 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; |
| 25 | 28 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; |
| 26 | 29 | import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.DATA_SOURCE_CONFIG_NOT_EXISTS; |
| 27 | -import static org.junit.jupiter.api.Assertions.assertNotNull; | |
| 28 | -import static org.junit.jupiter.api.Assertions.assertNull; | |
| 30 | +import static org.junit.jupiter.api.Assertions.*; | |
| 29 | 31 | import static org.mockito.ArgumentMatchers.anyString; |
| 30 | 32 | import static org.mockito.ArgumentMatchers.eq; |
| 31 | 33 | import static org.mockito.Mockito.mockStatic; |
| ... | ... | @@ -57,6 +59,11 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest { |
| 57 | 59 | ReflectUtil.setFieldValue(EncryptTypeHandler.class, "aes", aes); |
| 58 | 60 | when(aes.encryptBase64(anyString())).then((Answer<String>) invocation -> invocation.getArgument(0)); |
| 59 | 61 | when(aes.decryptStr(anyString())).then((Answer<String>) invocation -> invocation.getArgument(0)); |
| 62 | + | |
| 63 | + // mock DynamicDataSourceProperties | |
| 64 | + when(dynamicDataSourceProperties.getPrimary()).thenReturn("primary"); | |
| 65 | + when(dynamicDataSourceProperties.getDatasource()).thenReturn(MapUtil.of("primary", | |
| 66 | + new DataSourceProperty().setUrl("http://localhost:3306").setUsername("yunai").setPassword("tudou"))); | |
| 60 | 67 | } |
| 61 | 68 | |
| 62 | 69 | @Test |
| ... | ... | @@ -141,7 +148,58 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest { |
| 141 | 148 | // 调用 |
| 142 | 149 | DataSourceConfigDO result = dataSourceConfigMapper.selectOne(DataSourceConfigDO::getPassword, |
| 143 | 150 | EncryptTypeHandler.encrypt(dbDataSourceConfig.getPassword())); |
| 144 | - System.out.println(result); | |
| 151 | + assertPojoEquals(dbDataSourceConfig, result); | |
| 152 | + } | |
| 153 | + | |
| 154 | + @Test | |
| 155 | + public void testGetDataSourceConfig_master() { | |
| 156 | + // 准备参数 | |
| 157 | + Long id = 0L; | |
| 158 | + // mock 方法 | |
| 159 | + | |
| 160 | + // 调用 | |
| 161 | + DataSourceConfigDO dataSourceConfig = dataSourceConfigService.getDataSourceConfig(id); | |
| 162 | + // 断言 | |
| 163 | + assertEquals(id, dataSourceConfig.getId()); | |
| 164 | + assertEquals("primary", dataSourceConfig.getName()); | |
| 165 | + assertEquals("http://localhost:3306", dataSourceConfig.getUrl()); | |
| 166 | + assertEquals("yunai", dataSourceConfig.getUsername()); | |
| 167 | + assertEquals("tudou", dataSourceConfig.getPassword()); | |
| 168 | + } | |
| 169 | + | |
| 170 | + @Test | |
| 171 | + public void testGetDataSourceConfig_normal() { | |
| 172 | + // mock 数据 | |
| 173 | + DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class); | |
| 174 | + dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据 | |
| 175 | + // 准备参数 | |
| 176 | + Long id = dbDataSourceConfig.getId(); | |
| 177 | + | |
| 178 | + // 调用 | |
| 179 | + DataSourceConfigDO dataSourceConfig = dataSourceConfigService.getDataSourceConfig(id); | |
| 180 | + // 断言 | |
| 181 | + assertPojoEquals(dbDataSourceConfig, dataSourceConfig); | |
| 182 | + } | |
| 183 | + | |
| 184 | + @Test | |
| 185 | + public void testGetDataSourceConfigList() { | |
| 186 | + // mock 数据 | |
| 187 | + DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class); | |
| 188 | + dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据 | |
| 189 | + // 准备参数 | |
| 190 | + | |
| 191 | + // 调用 | |
| 192 | + List<DataSourceConfigDO> dataSourceConfigList = dataSourceConfigService.getDataSourceConfigList(); | |
| 193 | + // 断言 | |
| 194 | + assertEquals(2, dataSourceConfigList.size()); | |
| 195 | + // master | |
| 196 | + assertEquals(0L, dataSourceConfigList.get(0).getId()); | |
| 197 | + assertEquals("primary", dataSourceConfigList.get(0).getName()); | |
| 198 | + assertEquals("http://localhost:3306", dataSourceConfigList.get(0).getUrl()); | |
| 199 | + assertEquals("yunai", dataSourceConfigList.get(0).getUsername()); | |
| 200 | + assertEquals("tudou", dataSourceConfigList.get(0).getPassword()); | |
| 201 | + // normal | |
| 202 | + assertPojoEquals(dbDataSourceConfig, dataSourceConfigList.get(1)); | |
| 145 | 203 | } |
| 146 | 204 | |
| 147 | 205 | } | ... | ... |