fix(测试跟踪): 修复测试计划切换环境,接口用例切换数据源失败的缺陷 (#17923)

--bug=1016635 --user=王孝刚
【测试跟踪】github#17753,接口场景切换环境后,SQL请求的数据源切换正常,但加入到测试计划后,数据源切换异常
https://www.tapd.cn/55049933/s/1243915

Co-authored-by: wxg0103 <727495428@qq.com>
This commit is contained in:
MeterSphere Bot 2022-09-17 12:38:04 +08:00 committed by GitHub
parent 0e19f0a408
commit d4c5099cca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 67 additions and 7 deletions

View File

@ -11,6 +11,7 @@ import io.metersphere.api.dto.EnvironmentType;
import io.metersphere.api.dto.definition.request.controller.MsLoopController; import io.metersphere.api.dto.definition.request.controller.MsLoopController;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.definition.request.variable.ScenarioVariable; import io.metersphere.api.dto.definition.request.variable.ScenarioVariable;
import io.metersphere.api.dto.scenario.DatabaseConfig;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.dto.scenario.request.BodyFile; import io.metersphere.api.dto.scenario.request.BodyFile;
import io.metersphere.api.service.ApiTestEnvironmentService; import io.metersphere.api.service.ApiTestEnvironmentService;
@ -851,4 +852,28 @@ public class ElementUtil {
} }
} }
public static DatabaseConfig dataSource(String projectId, String dataSourceId, EnvironmentConfig envConfig) {
try {
ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class);
List<ApiTestEnvironmentWithBLOBs> environment = environmentService.list(projectId);
EnvironmentConfig dataConfig = null;
List<String> dataName = new ArrayList<>();
List<ApiTestEnvironmentWithBLOBs> orgDataSource = environment.stream().filter(ApiTestEnvironmentWithBLOBs -> ApiTestEnvironmentWithBLOBs.getConfig().contains(dataSourceId)).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(orgDataSource)) {
dataConfig = JSONObject.parseObject(orgDataSource.get(0).getConfig(), EnvironmentConfig.class);
if (CollectionUtils.isNotEmpty(dataConfig.getDatabaseConfigs())) {
dataName = dataConfig.getDatabaseConfigs().stream().filter(DatabaseConfig -> DatabaseConfig.getId().equals(dataSourceId)).map(DatabaseConfig::getName).collect(Collectors.toList());
}
}
List<String> finalDataName = dataName;
List<DatabaseConfig> collect = envConfig.getDatabaseConfigs().stream().filter(DatabaseConfig -> DatabaseConfig.getName().equals(finalDataName.get(0))).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(collect)) {
return collect.get(0);
}
} catch (Exception e) {
LogUtil.error(e);
}
return null;
}
} }

View File

@ -126,7 +126,12 @@ public class MsJDBCPostProcessor extends MsTestElement {
} else { } else {
// 取当前环境下默认的一个数据源 // 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) { if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) {
this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0); DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, config.getConfig().get(this.getProjectId()));
if (dataSourceOrg != null) {
this.dataSource = dataSourceOrg;
} else {
this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0);
}
} }
} }
} }

View File

@ -126,7 +126,12 @@ public class MsJDBCPreProcessor extends MsTestElement {
} else { } else {
// 取当前环境下默认的一个数据源 // 取当前环境下默认的一个数据源
if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) { if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) {
this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0); DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, config.getConfig().get(this.getProjectId()));
if (dataSourceOrg != null) {
this.dataSource = dataSourceOrg;
} else {
this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0);
}
} }
} }
} }

View File

@ -147,7 +147,12 @@ public class MsJDBCSampler extends MsTestElement {
if (config.getConfig().get(this.getProjectId()) != null) { if (config.getConfig().get(this.getProjectId()) != null) {
envConfig = config.getConfig().get(this.getProjectId()); envConfig = config.getConfig().get(this.getProjectId());
if (CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) { if (CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) {
this.dataSource = envConfig.getDatabaseConfigs().get(0); DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, envConfig);
if (dataSourceOrg != null) {
this.dataSource = dataSourceOrg;
} else {
this.dataSource = envConfig.getDatabaseConfigs().get(0);
}
} }
} }
} }

View File

@ -35,7 +35,7 @@ import MsAsideContainer from "../../../../common/components/MsAsideContainer";
import MsMainContainer from "../../../../common/components/MsMainContainer"; import MsMainContainer from "../../../../common/components/MsMainContainer";
import MsAsideItem from "../../../../common/components/MsAsideItem"; import MsAsideItem from "../../../../common/components/MsAsideItem";
import EnvironmentEdit from "./EnvironmentEdit"; import EnvironmentEdit from "./EnvironmentEdit";
import {hasPermission, listenGoBack, removeGoBackListener} from "@/common/js/utils"; import {getUUID, hasPermission, listenGoBack, removeGoBackListener} from "@/common/js/utils";
import {Environment, parseEnvironment} from "../../model/EnvironmentModel"; import {Environment, parseEnvironment} from "../../model/EnvironmentModel";
export default { export default {
@ -113,6 +113,11 @@ export default {
let newEnvironment = {}; let newEnvironment = {};
newEnvironment = new Environment(environment); newEnvironment = new Environment(environment);
newEnvironment.id = null; newEnvironment.id = null;
newEnvironment.config.databaseConfigs.forEach(dataSource => {
if (dataSource.id) {
dataSource.id = getUUID();
}
})
newEnvironment.name = this.getNoRepeatName(newEnvironment.name); newEnvironment.name = this.getNoRepeatName(newEnvironment.name);
if (!this.validateEnvironment(newEnvironment)) { if (!this.validateEnvironment(newEnvironment)) {
return; return;

View File

@ -37,7 +37,7 @@ import MsAsideContainer from "../../../common/components/MsAsideContainer";
import MsMainContainer from "../../../common/components/MsMainContainer"; import MsMainContainer from "../../../common/components/MsMainContainer";
import MsAsideItem from "../../../common/components/MsAsideItem"; import MsAsideItem from "../../../common/components/MsAsideItem";
import EnvironmentEdit from "./environment/EnvironmentEdit"; import EnvironmentEdit from "./environment/EnvironmentEdit";
import {hasPermission, listenGoBack, removeGoBackListener} from "../../../../../common/js/utils"; import {getUUID, hasPermission, listenGoBack, removeGoBackListener} from "../../../../../common/js/utils";
import {Environment, parseEnvironment} from "../model/EnvironmentModel"; import {Environment, parseEnvironment} from "../model/EnvironmentModel";
import MsDialogHeader from "@/business/components/common/components/MsDialogHeader"; import MsDialogHeader from "@/business/components/common/components/MsDialogHeader";
@ -145,6 +145,11 @@ export default {
if (!this.validateEnvironment(newEnvironment)) { if (!this.validateEnvironment(newEnvironment)) {
return; return;
} }
newEnvironment.config.databaseConfigs.forEach(dataSource => {
if (dataSource.id) {
dataSource.id = getUUID();
}
})
this.$refs.environmentEdit._save(newEnvironment); this.$refs.environmentEdit._save(newEnvironment);
this.environments.unshift(newEnvironment); this.environments.unshift(newEnvironment);
this.$refs.environmentItems.itemSelected(this.environments.length - 1, newEnvironment); this.$refs.environmentItems.itemSelected(this.environments.length - 1, newEnvironment);

View File

@ -134,7 +134,7 @@ import EnvironmentEdit from "@/business/components/api/test/components/environme
import MsAsideItem from "@/business/components/common/components/MsAsideItem"; import MsAsideItem from "@/business/components/common/components/MsAsideItem";
import MsAsideContainer from "@/business/components/common/components/MsAsideContainer"; import MsAsideContainer from "@/business/components/common/components/MsAsideContainer";
import ProjectSwitch from "@/business/components/common/head/ProjectSwitch"; import ProjectSwitch from "@/business/components/common/head/ProjectSwitch";
import {downloadFile, getCurrentProjectID, operationConfirm} from "@/common/js/utils"; import {downloadFile, getCurrentProjectID, getUUID, operationConfirm} from "@/common/js/utils";
import EnvironmentImport from "@/business/components/project/menu/EnvironmentImport"; import EnvironmentImport from "@/business/components/project/menu/EnvironmentImport";
import MsMainContainer from "@/business/components/common/components/MsMainContainer"; import MsMainContainer from "@/business/components/common/components/MsMainContainer";
import MsContainer from "@/business/components/common/components/MsContainer"; import MsContainer from "@/business/components/common/components/MsContainer";
@ -298,6 +298,11 @@ export default {
parseEnvironment(temEnv); //parseEnvironment parseEnvironment(temEnv); //parseEnvironment
let newEnvironment = new Environment(temEnv); let newEnvironment = new Environment(temEnv);
newEnvironment.id = null; newEnvironment.id = null;
newEnvironment.config.databaseConfigs.forEach(dataSource => {
if (dataSource.id) {
dataSource.id = getUUID();
}
})
newEnvironment.name = this.getNoRepeatName(newEnvironment.name); newEnvironment.name = this.getNoRepeatName(newEnvironment.name);
this.dialogVisible = true; this.dialogVisible = true;
this.currentEnvironment = newEnvironment; this.currentEnvironment = newEnvironment;

View File

@ -135,7 +135,7 @@ import EnvironmentEdit from "@/business/components/api/test/components/environme
import MsAsideItem from "@/business/components/common/components/MsAsideItem"; import MsAsideItem from "@/business/components/common/components/MsAsideItem";
import MsAsideContainer from "@/business/components/common/components/MsAsideContainer"; import MsAsideContainer from "@/business/components/common/components/MsAsideContainer";
import ProjectSwitch from "@/business/components/common/head/ProjectSwitch"; import ProjectSwitch from "@/business/components/common/head/ProjectSwitch";
import {downloadFile, operationConfirm, strMapToObj} from "@/common/js/utils"; import {downloadFile, getUUID, operationConfirm, strMapToObj} from "@/common/js/utils";
import EnvironmentImport from "@/business/components/project/menu/EnvironmentImport"; import EnvironmentImport from "@/business/components/project/menu/EnvironmentImport";
import ShowMoreBtn from "@/business/components/track/case/components/ShowMoreBtn"; import ShowMoreBtn from "@/business/components/track/case/components/ShowMoreBtn";
import {_handleSelect, _handleSelectAll, getSelectDataCounts, setUnSelectIds} from "@/common/js/tableUtils"; import {_handleSelect, _handleSelectAll, getSelectDataCounts, setUnSelectIds} from "@/common/js/tableUtils";
@ -332,6 +332,11 @@ export default {
parseEnvironment(temEnv); //parseEnvironment parseEnvironment(temEnv); //parseEnvironment
let newEnvironment = new Environment(temEnv); let newEnvironment = new Environment(temEnv);
newEnvironment.id = null; newEnvironment.id = null;
newEnvironment.config.databaseConfigs.forEach(dataSource => {
if (dataSource.id) {
dataSource.id = getUUID();
}
})
newEnvironment.name = this.getNoRepeatName(newEnvironment.name); newEnvironment.name = this.getNoRepeatName(newEnvironment.name);
this.dialogVisible = true; this.dialogVisible = true;
this.currentEnvironment = newEnvironment; this.currentEnvironment = newEnvironment;