优化单元测试.

This commit is contained in:
lijiahang
2025-03-07 14:57:26 +08:00
parent 093501a400
commit a1dd9eec01
6 changed files with 34 additions and 43 deletions

View File

@@ -20,21 +20,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.visor.launch.configuration;
package org.dromara.visor.common.configuration;
import cn.orionsec.kit.spring.SpringHolder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 应用配置类
* spring 配置类
*
* @author Jiahang Li
* @version 1.0.0
* @since 2023/6/20 10:34
*/
@Configuration
public class LaunchApplicationConfiguration {
public class SpringConfiguration {
/**
* @return spring 容器工具类

View File

@@ -24,6 +24,7 @@ package org.dromara.visor.framework.test.core.base;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import org.dromara.visor.common.configuration.SpringConfiguration;
import org.dromara.visor.framework.datasource.configuration.OrionDataSourceAutoConfiguration;
import org.dromara.visor.framework.mybatis.configuration.OrionMybatisAutoConfiguration;
import org.dromara.visor.framework.redis.configuration.OrionRedisAutoConfiguration;
@@ -57,6 +58,8 @@ import org.springframework.transaction.annotation.Transactional;
public class BaseUnitTest {
@Import({
// spring
SpringConfiguration.class,
// mock
OrionMockBeanTestConfiguration.class,
OrionMockRedisTestConfiguration.class,
@@ -74,7 +77,6 @@ public class BaseUnitTest {
RedisAutoConfiguration.class,
RedissonAutoConfiguration.class,
})
// TODO
public static class Application {
}

View File

@@ -42,7 +42,11 @@ import java.util.Optional;
* @version 1.0.0
* @since 2023/6/19 16:55
*/
@SpringBootApplication(scanBasePackages = {"org.dromara.visor.launch", "org.dromara.visor.module"})
@SpringBootApplication(scanBasePackages = {
"org.dromara.visor.launch",
"org.dromara.visor.common",
"org.dromara.visor.module"
})
public class LaunchApplication {
public static void main(String[] args) {

View File

@@ -82,6 +82,10 @@
<groupId>org.dromara.visor</groupId>
<artifactId>orion-visor-spring-boot-starter-job</artifactId>
</dependency>
<dependency>
<groupId>org.dromara.visor</groupId>
<artifactId>orion-visor-spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -135,13 +135,6 @@ public interface HostService {
*/
void deleteHostRelByIdListAsync(List<Long> idList);
/**
* 获取当前更新配置的 hostId
*
* @return hostId
*/
Long getCurrentUpdateConfigHostId();
/**
* 清除缓存
*/

View File

@@ -83,8 +83,6 @@ import java.util.stream.Collectors;
@Service
public class HostServiceImpl implements HostService {
private static final ThreadLocal<Long> CURRENT_UPDATE_CONFIG_ID = new ThreadLocal<>();
@Resource
private HostDAO hostDAO;
@@ -185,30 +183,25 @@ public class HostServiceImpl implements HostService {
OperatorLogs.add(ExtraFieldConst.CONFIG, param);
log.info("HostService-updateHostConfig request: {}", param);
Long id = request.getId();
try {
CURRENT_UPDATE_CONFIG_ID.set(id);
// 查询主机信息
HostDO host = hostDAO.selectById(id);
Valid.notNull(host, ErrorMessage.HOST_ABSENT);
HostTypeEnum type = Valid.valid(HostTypeEnum::of, host.getType());
GenericsDataModel beforeConfig = type.parse(host.getConfig());
GenericsDataModel newConfig = type.parse(request.getConfig());
// 添加日志参数
OperatorLogs.add(OperatorLogs.ID, id);
OperatorLogs.add(OperatorLogs.NAME, host.getName());
// 更新前校验
type.doValid(beforeConfig, newConfig);
// 修改配置
HostDO updateHost = HostDO.builder()
.id(id)
.config(newConfig.serial())
.build();
int effect = hostDAO.updateById(updateHost);
log.info("HostService-updateHostConfig effect: {}", effect);
return effect;
} finally {
CURRENT_UPDATE_CONFIG_ID.remove();
}
// 查询主机信息
HostDO host = hostDAO.selectById(id);
Valid.notNull(host, ErrorMessage.HOST_ABSENT);
HostTypeEnum type = Valid.valid(HostTypeEnum::of, host.getType());
GenericsDataModel beforeConfig = type.parse(host.getConfig());
GenericsDataModel newConfig = type.parse(request.getConfig());
// 添加日志参数
OperatorLogs.add(OperatorLogs.ID, id);
OperatorLogs.add(OperatorLogs.NAME, host.getName());
// 更新前校验
type.doValid(beforeConfig, newConfig);
// 修改配置
HostDO updateHost = HostDO.builder()
.id(id)
.config(newConfig.serial())
.build();
int effect = hostDAO.updateById(updateHost);
log.info("HostService-updateHostConfig effect: {}", effect);
return effect;
}
@Override
@@ -348,11 +341,6 @@ public class HostServiceImpl implements HostService {
dataExtraApi.deleteByRelIdList(DataExtraTypeEnum.HOST, idList);
}
@Override
public Long getCurrentUpdateConfigHostId() {
return CURRENT_UPDATE_CONFIG_ID.get();
}
@Override
public void clearCache() {
RedisMaps.scanKeysDelete(HostCacheKeyDefine.HOST_INFO.format("*"));