mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-22 23:31:52 +08:00
infra:完善 dataSource 的单元测试
This commit is contained in:
parent
b5b23b3d7c
commit
cc1fa54ea5
@ -37,7 +37,7 @@ public class DataSourceConfigServiceImpl implements DataSourceConfigService {
|
||||
@Override
|
||||
public Long createDataSourceConfig(DataSourceConfigCreateReqVO createReqVO) {
|
||||
DataSourceConfigDO dataSourceConfig = DataSourceConfigConvert.INSTANCE.convert(createReqVO);
|
||||
checkConnectionOK(dataSourceConfig);
|
||||
validateConnectionOK(dataSourceConfig);
|
||||
|
||||
// 插入
|
||||
dataSourceConfigMapper.insert(dataSourceConfig);
|
||||
@ -50,7 +50,7 @@ public class DataSourceConfigServiceImpl implements DataSourceConfigService {
|
||||
// 校验存在
|
||||
validateDataSourceConfigExists(updateReqVO.getId());
|
||||
DataSourceConfigDO updateObj = DataSourceConfigConvert.INSTANCE.convert(updateReqVO);
|
||||
checkConnectionOK(updateObj);
|
||||
validateConnectionOK(updateObj);
|
||||
|
||||
// 更新
|
||||
dataSourceConfigMapper.updateById(updateObj);
|
||||
@ -88,7 +88,7 @@ public class DataSourceConfigServiceImpl implements DataSourceConfigService {
|
||||
return result;
|
||||
}
|
||||
|
||||
private void checkConnectionOK(DataSourceConfigDO config) {
|
||||
private void validateConnectionOK(DataSourceConfigDO config) {
|
||||
boolean success = JdbcUtils.isConnectionOK(config.getUrl(), config.getUsername(), config.getPassword());
|
||||
if (!success) {
|
||||
throw exception(DATA_SOURCE_CONFIG_NOT_OK);
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cn.iocoder.yudao.module.infra.service.db;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.crypto.symmetric.AES;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.type.EncryptTypeHandler;
|
||||
@ -9,6 +10,7 @@ import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigCrea
|
||||
import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.infra.dal.dataobject.db.DataSourceConfigDO;
|
||||
import cn.iocoder.yudao.module.infra.dal.mysql.db.DataSourceConfigMapper;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -18,14 +20,14 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.DATA_SOURCE_CONFIG_NOT_EXISTS;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
@ -57,6 +59,11 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest {
|
||||
ReflectUtil.setFieldValue(EncryptTypeHandler.class, "aes", aes);
|
||||
when(aes.encryptBase64(anyString())).then((Answer<String>) invocation -> invocation.getArgument(0));
|
||||
when(aes.decryptStr(anyString())).then((Answer<String>) invocation -> invocation.getArgument(0));
|
||||
|
||||
// mock DynamicDataSourceProperties
|
||||
when(dynamicDataSourceProperties.getPrimary()).thenReturn("primary");
|
||||
when(dynamicDataSourceProperties.getDatasource()).thenReturn(MapUtil.of("primary",
|
||||
new DataSourceProperty().setUrl("http://localhost:3306").setUsername("yunai").setPassword("tudou")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -141,7 +148,58 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest {
|
||||
// 调用
|
||||
DataSourceConfigDO result = dataSourceConfigMapper.selectOne(DataSourceConfigDO::getPassword,
|
||||
EncryptTypeHandler.encrypt(dbDataSourceConfig.getPassword()));
|
||||
System.out.println(result);
|
||||
assertPojoEquals(dbDataSourceConfig, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDataSourceConfig_master() {
|
||||
// 准备参数
|
||||
Long id = 0L;
|
||||
// mock 方法
|
||||
|
||||
// 调用
|
||||
DataSourceConfigDO dataSourceConfig = dataSourceConfigService.getDataSourceConfig(id);
|
||||
// 断言
|
||||
assertEquals(id, dataSourceConfig.getId());
|
||||
assertEquals("primary", dataSourceConfig.getName());
|
||||
assertEquals("http://localhost:3306", dataSourceConfig.getUrl());
|
||||
assertEquals("yunai", dataSourceConfig.getUsername());
|
||||
assertEquals("tudou", dataSourceConfig.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDataSourceConfig_normal() {
|
||||
// mock 数据
|
||||
DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
|
||||
dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
Long id = dbDataSourceConfig.getId();
|
||||
|
||||
// 调用
|
||||
DataSourceConfigDO dataSourceConfig = dataSourceConfigService.getDataSourceConfig(id);
|
||||
// 断言
|
||||
assertPojoEquals(dbDataSourceConfig, dataSourceConfig);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDataSourceConfigList() {
|
||||
// mock 数据
|
||||
DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
|
||||
dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
|
||||
// 准备参数
|
||||
|
||||
// 调用
|
||||
List<DataSourceConfigDO> dataSourceConfigList = dataSourceConfigService.getDataSourceConfigList();
|
||||
// 断言
|
||||
assertEquals(2, dataSourceConfigList.size());
|
||||
// master
|
||||
assertEquals(0L, dataSourceConfigList.get(0).getId());
|
||||
assertEquals("primary", dataSourceConfigList.get(0).getName());
|
||||
assertEquals("http://localhost:3306", dataSourceConfigList.get(0).getUrl());
|
||||
assertEquals("yunai", dataSourceConfigList.get(0).getUsername());
|
||||
assertEquals("tudou", dataSourceConfigList.get(0).getPassword());
|
||||
// normal
|
||||
assertPojoEquals(dbDataSourceConfig, dataSourceConfigList.get(1));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user