feat(接口测试): 支持多数据源自动切换

--story=1007191 --user=赵勇 多数据源切换环境逻辑优化 https://www.tapd.cn/55049933/s/1141228
This commit is contained in:
fit2-zhao 2022-04-20 17:41:39 +08:00 committed by fit2-zhao
parent 87940f3527
commit fc7e396b7b
15 changed files with 212 additions and 65 deletions

View File

@ -80,7 +80,7 @@ public class MsScenarioParser extends MsAbstractParser<ScenarioImport> {
data.forEach(item -> {
String scenarioDefinitionStr = item.getScenarioDefinition();
if (StringUtils.isNotBlank(scenarioDefinitionStr)) {
JSONObject scenarioDefinition = JSONObject.parseObject(scenarioDefinitionStr);
JSONObject scenarioDefinition = JSONObject.parseObject(scenarioDefinitionStr, Feature.DisableSpecialKeyDetect);
if (scenarioDefinition != null) {
JSONObject environmentMap = scenarioDefinition.getJSONObject("environmentMap");
if (environmentMap != null) {

View File

@ -121,7 +121,7 @@ public class MsDefinitionParser extends MsAbstractParser<ApiDefinitionImport> {
apiDefinition.setProjectId(this.projectId);
String request = apiDefinition.getRequest();
JSONObject requestObj = JSONObject.parseObject(request);
JSONObject requestObj = JSONObject.parseObject(request, Feature.DisableSpecialKeyDetect);
if(requestObj.get("projectId")!=null){
requestObj.put("projectId", apiDefinition.getProjectId());
}
@ -148,7 +148,7 @@ public class MsDefinitionParser extends MsAbstractParser<ApiDefinitionImport> {
}
cases.forEach(item -> {
String request = item.getRequest();
JSONObject requestObj = JSONObject.parseObject(request);
JSONObject requestObj = JSONObject.parseObject(request, Feature.DisableSpecialKeyDetect);
requestObj.put("useEnvironment", "");
item.setRequest(JSONObject.toJSONString(requestObj));
item.setApiDefinitionId(apiDefinition.getId());

View File

@ -233,7 +233,7 @@ public class ApiScenarioEnvService {
environmentType = EnvironmentType.JSON.toString();
}
String definition = apiScenarioWithBLOBs.getScenarioDefinition();
MsScenario scenario = JSONObject.parseObject(definition, MsScenario.class);
MsScenario scenario = JSONObject.parseObject(definition, MsScenario.class, Feature.DisableSpecialKeyDetect);
GenerateHashTreeUtil.parse(definition, scenario);
if (StringUtils.equals(environmentType, EnvironmentType.JSON.toString())) {
scenario.setEnvironmentMap(JSON.parseObject(environmentJson, Map.class));
@ -251,7 +251,7 @@ public class ApiScenarioEnvService {
boolean isEnv = true;
if (apiScenarioWithBLOBs != null) {
String definition = apiScenarioWithBLOBs.getScenarioDefinition();
MsScenario scenario = JSONObject.parseObject(definition, MsScenario.class);
MsScenario scenario = JSONObject.parseObject(definition, MsScenario.class, Feature.DisableSpecialKeyDetect);
Map<String, String> envMap = scenario.getEnvironmentMap();
if (testPlanApiScenarios != null) {
String envType = testPlanApiScenarios.getEnvironmentType();

View File

@ -126,7 +126,7 @@ public class GenerateHashTreeUtil {
MsThreadGroup group = new MsThreadGroup();
group.setLabel(item.getName());
group.setName(runRequest.getReportId());
MsScenario scenario = JSONObject.parseObject(item.getScenarioDefinition(), MsScenario.class);
MsScenario scenario = JSONObject.parseObject(item.getScenarioDefinition(), MsScenario.class, Feature.DisableSpecialKeyDetect);
group.setOnSampleError(scenario.getOnSampleError());
if (planEnvMap != null && planEnvMap.size() > 0) {
scenario.setEnvironmentMap(planEnvMap);

View File

@ -699,6 +699,35 @@ public class ApiAutomationService {
return scenarioWithBLOBs;
}
private static JSONObject jsonMerge(JSONObject source, JSONObject target) {
// 覆盖目标JSON为空直接返回覆盖源
if (target == null) {
return source;
}
for (String key : source.keySet()) {
Object value = source.get(key);
if (!target.containsKey(key)) {
target.put(key, value);
} else {
if (value instanceof JSONObject) {
JSONObject valueJson = (JSONObject) value;
JSONObject targetValue = jsonMerge(valueJson, target.getJSONObject(key));
target.put(key, targetValue);
} else if (value instanceof JSONArray) {
JSONArray valueArray = (JSONArray) value;
for (int i = 0; i < valueArray.size(); i++) {
JSONObject obj = (JSONObject) valueArray.get(i);
JSONObject targetValue = jsonMerge(obj, (JSONObject) target.getJSONArray(key).get(i));
target.getJSONArray(key).set(i, targetValue);
}
} else {
target.put(key, value);
}
}
}
return target;
}
public String setDomain(ApiScenarioEnvRequest request) {
Boolean enable = request.getEnvironmentEnable();
String scenarioDefinition = request.getDefinition();
@ -714,11 +743,17 @@ public class ApiAutomationService {
}
} else {
String scenarioId = request.getId();
ApiScenarioWithBLOBs apiScenarioWithBLOBs = apiScenarioMapper.selectByPrimaryKey(scenarioId);
if (apiScenarioWithBLOBs != null) {
String environmentType = apiScenarioWithBLOBs.getEnvironmentType();
String environmentGroupId = apiScenarioWithBLOBs.getEnvironmentGroupId();
String environmentJson = apiScenarioWithBLOBs.getEnvironmentJson();
ApiScenarioDTO scenario = getNewApiScenario(scenarioId);
if (scenario != null) {
String referenced = element.getString("referenced");
if (StringUtils.equalsIgnoreCase("REF", referenced)) {
JSONObject source = JSON.parseObject(scenario.getScenarioDefinition(), Feature.DisableSpecialKeyDetect);
element = jsonMerge(source, element);
}
element.put("referenced", referenced);
String environmentType = scenario.getEnvironmentType();
String environmentGroupId = scenario.getEnvironmentGroupId();
String environmentJson = scenario.getEnvironmentJson();
if (StringUtils.equals(environmentType, EnvironmentType.GROUP.name())) {
environmentMap = environmentGroupProjectService.getEnvMap(environmentGroupId);
} else if (StringUtils.equals(environmentType, EnvironmentType.JSON.name())) {
@ -727,7 +762,6 @@ public class ApiAutomationService {
}
}
ParameterConfig config = new ParameterConfig();
apiScenarioEnvService.setEnvConfig(environmentMap, config);
if (config.getConfig() != null && !config.getConfig().isEmpty()) {

View File

@ -62,6 +62,7 @@ public class MsHashTreeService {
public static final String AUTH_MANAGER = "authManager";
public static final String PROJECT_ID = "projectId";
public static final String ACTIVE = "active";
public static final String ENV_MAP = "environmentMap";
public void setHashTree(JSONArray hashTree) {
// 将引用转成复制
@ -255,6 +256,9 @@ public class MsHashTreeService {
boolean variableEnable = element.containsKey(VARIABLE_ENABLE)
? element.getBoolean(VARIABLE_ENABLE) : true;
if (environmentEnable && StringUtils.isNotEmpty(scenarioWithBLOBs.getEnvironmentJson())) {
element.put(ENV_MAP, JSON.parseObject(scenarioWithBLOBs.getEnvironmentJson(), Map.class));
}
if (StringUtils.equalsIgnoreCase(element.getString(REFERENCED), REF)) {
element = JSON.parseObject(scenarioWithBLOBs.getScenarioDefinition(), Feature.DisableSpecialKeyDetect);
element.put(REFERENCED, REF);

View File

@ -1550,7 +1550,7 @@ export default {
if (!this.currentScenario.headers) {
this.currentScenario.headers = [];
}
if (this.currentScenario.id) {
if (this.currentScenario && this.currentScenario.id) {
this.result = this.$get("/api/automation/getApiScenario/" + this.currentScenario.id, response => {
if (response.data) {
this.path = "/api/automation/update";
@ -1818,19 +1818,15 @@ export default {
return [];
},
checkALevelChecked() {
if (this.$refs.stepTree) {
let resourceIds = [];
if (this.$refs.stepTree) {
this.$refs.stepTree.root.childNodes.forEach(item => {
if (item.checked) {
resourceIds.push(item.data.resourceId);
}
})
if (resourceIds.length > 20) {
this.$warning(this.$t('api_test.automation.open_check_message'));
return false;
}
}
return true;
return resourceIds;
},
recursionExpansion(resourceIds, array) {
if (array) {
@ -1850,11 +1846,15 @@ export default {
},
openExpansion() {
this.expandedStatus = true;
if (this.checkALevelChecked()) {
let resourceIds = this.getAllResourceIds();
let resourceIds = [];
let openResourceIds = this.checkALevelChecked();
if (openResourceIds.length > 20) {
resourceIds = openResourceIds.slice(0, 20);
} else {
resourceIds = this.getAllResourceIds();
}
this.changeNodeStatus(resourceIds, this.scenarioDefinition);
this.recursionExpansion(resourceIds, this.$refs.stepTree.root.childNodes);
}
},
closeExpansion() {
this.expandedStatus = false;

View File

@ -52,6 +52,7 @@
:environmentType="environmentType"
:environmentGroupId="environmentGroupId"
:envMap="envMap"
:is-scenario="true"
@enable="enable"
@copy="copyRow"
@remove="remove"

View File

@ -82,7 +82,7 @@ import {createComponent} from "@/business/components/api/definition/components/j
import {Assertions, Extract} from "@/business/components/api/definition/model/ApiTestModel";
import {parseEnvironment} from "@/business/components/api/definition/model/EnvironmentModel";
import ApiEnvironmentConfig from "@/business/components/api/test/components/ApiEnvironmentConfig";
import {getCurrentProjectID} from "@/common/js/utils";
import {getCurrentProjectID, objToStrMap} from "@/common/js/utils";
import {getUUID} from "@/common/js/utils";
import MsJsr233Processor from "@/business/components/api/automation/scenario/component/Jsr233Processor";
@ -102,6 +102,10 @@ export default {
type: Boolean,
default: true,
},
isScenario: {
type: Boolean,
default: false,
},
isReadOnly: {
type: Boolean,
default: false
@ -133,8 +137,14 @@ export default {
this.getEnvironments();
},
deep: true
},
'$store.state.useEnvironment': function () {
if (!this.isScenario) {
this.request.environmentId = this.$store.state.useEnvironment;
this.getEnvironments();
}
},
},
methods: {
remove(row) {
let index = this.request.hashTree.indexOf(row);
@ -167,13 +177,42 @@ export default {
runTest() {
},
getEnvironments() {
this.environments = [];
itselfEnvironment() {
let id = this.request.projectId ? this.request.projectId : this.projectId;
this.result = this.$get('/api/environment/list/' + id, response => {
this.environments = response.data;
this.environments.forEach(environment => {
parseEnvironment(environment);
})
this.initDataSource();
});
},
getEnvironments() {
let envId = "";
let id = this.request.projectId ? this.request.projectId : this.projectId;
if (this.$store.state.scenarioEnvMap && this.$store.state.scenarioEnvMap instanceof Map
&& this.$store.state.scenarioEnvMap.has(this.projectId)) {
envId = this.$store.state.scenarioEnvMap.get(this.projectId);
&& this.$store.state.scenarioEnvMap.has(id)) {
envId = this.$store.state.scenarioEnvMap.get(id);
}
if (this.request.referenced === 'Created' && this.isScenario && !this.request.isRefEnvironment) {
this.itselfEnvironment();
return;
} else if (!this.isScenario && !this.request.customizeReq) {
this.itselfEnvironment();
return;
}
this.environments = [];
//
if (this.request.environmentEnable && this.request.refEevMap) {
let obj = Object.prototype.toString.call(this.request.refEevMap).match(/\[object (\w+)\]/)[1].toLowerCase();
if (obj !== 'object' && obj !== "map") {
this.request.refEevMap = objToStrMap(JSON.parse(this.request.refEevMap));
} else if (obj === 'object' && obj !== "map") {
this.request.refEevMap = objToStrMap(this.request.refEevMap);
}
if (this.request.refEevMap instanceof Map && this.request.refEevMap.has(id)) {
envId = this.request.refEevMap.get(id);
}
}
let targetDataSourceName = "";
let currentEnvironment = {};
@ -215,7 +254,7 @@ export default {
}
}
let flag = false;
if (currentEnvironment.config && currentEnvironment.config.databaseConfigs) {
if (currentEnvironment && currentEnvironment.config && currentEnvironment.config.databaseConfigs) {
currentEnvironment.config.databaseConfigs.forEach(item => {
if (item.id === this.request.dataSourceId) {
flag = true;

View File

@ -125,6 +125,7 @@ export default {
if (this.scenario.id && this.scenario.referenced === 'REF' && !this.scenario.loaded && this.scenario.hashTree) {
this.setDisabled(this.scenario.hashTree, this.scenario.projectId);
}
this.setOwnEnvironment(this.scenario.hashTree);
},
components: {ApiBaseComponent, MsSqlBasisParameters, MsTcpBasisParameters, MsDubboBasisParameters, MsApiRequestForm},
data() {
@ -138,7 +139,6 @@ export default {
computed: {
isDeletedOrRef() {
return this.scenario.referenced !== undefined && this.scenario.referenced === 'Deleted' || this.scenario.referenced === 'REF';
},
},
methods: {
@ -237,6 +237,21 @@ export default {
}
}
},
setOwnEnvironment(scenarioDefinition) {
for (let i in scenarioDefinition) {
let typeArray = ["JDBCPostProcessor", "JDBCSampler", "JDBCPreProcessor"]
if (typeArray.indexOf(scenarioDefinition[i].type) !== -1) {
scenarioDefinition[i].refEevMap = new Map();
scenarioDefinition[i].environmentEnable = this.scenario.environmentEnable;
if (this.scenario.environmentEnable && this.scenario.environmentMap) {
scenarioDefinition[i].refEevMap = this.scenario.environmentMap;
}
}
if (scenarioDefinition[i].hashTree !== undefined && scenarioDefinition[i].hashTree.length > 0) {
this.setOwnEnvironment(scenarioDefinition[i].hashTree);
}
}
},
calcProjectId(projectId, parentId) {
if (!projectId) {
return parentId ? parentId : getCurrentProjectID();

View File

@ -9,7 +9,7 @@
<el-dropdown-item command="enable" v-if="data.command && data.enable">{{ this.$t('ui.disable') }}</el-dropdown-item>
<el-dropdown-item command="enable" v-if="data.command && !data.enable">{{ this.$t('ui.enable') }}</el-dropdown-item>
<el-dropdown-item command="remove">{{ this.$t('api_test.automation.delete_step') }}</el-dropdown-item>
<el-dropdown-item command="rename">{{ this.$t('test_track.module.rename') }}</el-dropdown-item>
<el-dropdown-item command="rename" v-if="!isScenario">{{ this.$t('test_track.module.rename') }}</el-dropdown-item>
<el-dropdown-item command="scenarioVar" v-if="data.type==='scenario'">
{{ this.$t("api_test.automation.view_scene_variables") }}
</el-dropdown-item>
@ -56,6 +56,12 @@ export default {
name: "StepExtendBtns",
components: {STEP, MsVariableList, MsAddBasisApi},
props: {
isScenario: {
type: Boolean,
default() {
return false;
}
},
data: Object,
environmentType: String,
environmentGroupId: String,
@ -147,9 +153,25 @@ export default {
if (res.data) {
let data = JSON.parse(res.data);
this.data.hashTree = data.hashTree;
this.setOwnEnvironment(this.data.hashTree);
}
})
},
setOwnEnvironment(scenarioDefinition) {
for (let i in scenarioDefinition) {
let typeArray = ["JDBCPostProcessor", "JDBCSampler", "JDBCPreProcessor"]
if (typeArray.indexOf(scenarioDefinition[i].type) !== -1) {
scenarioDefinition[i].environmentEnable = this.data.environmentEnable;
scenarioDefinition[i].refEevMap = new Map();
if (this.data.environmentEnable && this.data.environmentMap) {
scenarioDefinition[i].refEevMap = this.data.environmentMap;
}
}
if (scenarioDefinition[i].hashTree !== undefined && scenarioDefinition[i].hashTree.length > 0) {
this.setOwnEnvironment(scenarioDefinition[i].hashTree);
}
}
},
saveAsApi() {
this.currentProtocol = this.data.protocol;
this.data.customizeReq = false;

View File

@ -20,7 +20,7 @@
:project-id="projectId"
:is-read-only="isReadOnly"
:useEnvironment='useEnvironment'
@setEnvironment="setEnvironment" ref="environmentSelect"/>
@setEnvironment="setEnvironment" ref="environmentSelect" v-if="api.protocol==='HTTP'"/>
</el-col>
<el-col :span="2">
<!-- 保存操作 -->

View File

@ -144,7 +144,6 @@
v-if="isXpack&&api.method==='ESB'" ref="esbDefinition"/>
<ms-sql-basis-parameters
:showScript="true"
:is-scenario="true"
:request="apiCase.request"
:response="apiCase.responseData"
v-if="api.protocol==='SQL'"/>

View File

@ -98,7 +98,7 @@
<div class="el-step__icon-inner">{{ request.ruleSize }}</div>
</div>
</span>
<ms-jmx-step :request="request" :apiId="request.id" :response="response" @reload="reloadBody"
<ms-jmx-step :request="request" :apiId="request.id" :response="response" :isScenario="isScenario" @reload="reloadBody"
:tab-type="'assertionsRule'" ref="assertionsRule"/>
</el-tab-pane>
@ -123,7 +123,7 @@ import MsCodeEdit from "../../../../../common/components/MsCodeEdit";
import MsApiScenarioVariables from "../../ApiScenarioVariables";
import {parseEnvironment} from "../../../model/EnvironmentModel";
import ApiEnvironmentConfig from "@/business/components/api/test/components/ApiEnvironmentConfig";
import {getCurrentProjectID} from "@/common/js/utils";
import {getCurrentProjectID, objToStrMap} from "@/common/js/utils";
import {getUUID} from "@/common/js/utils";
import MsJsr233Processor from "../../../../automation/scenario/component/Jsr233Processor";
import MsJmxStep from "../../step/JmxStep";
@ -148,6 +148,10 @@ export default {
type: Boolean,
default: true,
},
isCase: {
type: Boolean,
default: false,
},
isScenario: {
type: Boolean,
default: false,
@ -177,12 +181,24 @@ export default {
},
deep: true
},
'$store.state.useEnvironment': function () {
if (!this.isScenario) {
this.request.environmentId = this.$store.state.useEnvironment;
this.getEnvironments();
}
},
'$store.state.scenarioEnvMap': {
handler(v) {
this.getEnvironments();
},
deep: true
}
},
'request.refEevMap': {
handler(v) {
this.getEnvironments();
},
deep: true
},
},
created() {
this.getEnvironments();
@ -265,9 +281,10 @@ export default {
},
getEnvironments() {
let envId = "";
let id = this.request.projectId ? this.request.projectId : this.projectId;
if (this.$store.state.scenarioEnvMap && this.$store.state.scenarioEnvMap instanceof Map
&& this.$store.state.scenarioEnvMap.has(this.projectId)) {
envId = this.$store.state.scenarioEnvMap.get(this.projectId);
&& this.$store.state.scenarioEnvMap.has(id)) {
envId = this.$store.state.scenarioEnvMap.get(id);
}
if (this.request.referenced === 'Created' && this.isScenario && !this.request.isRefEnvironment) {
this.itselfEnvironment();
@ -277,8 +294,18 @@ export default {
return;
}
this.environments = [];
let id = this.request.projectId ? this.request.projectId : this.projectId;
//
if (this.request.environmentEnable && this.request.refEevMap) {
let obj = Object.prototype.toString.call(this.request.refEevMap).match(/\[object (\w+)\]/)[1].toLowerCase();
if (obj !== 'object' && obj !== "map") {
this.request.refEevMap = objToStrMap(JSON.parse(this.request.refEevMap));
} else if (obj === 'object' && obj !== "map") {
this.request.refEevMap = objToStrMap(this.request.refEevMap);
}
if (this.request.refEevMap instanceof Map && this.request.refEevMap.has(id)) {
envId = this.request.refEevMap.get(id);
}
}
let targetDataSourceName = "";
let currentEnvironment = {};
this.result = this.$get('/api/environment/list/' + id, response => {
@ -297,8 +324,10 @@ export default {
}
if (envId && environment.id === envId) {
currentEnvironment = environment;
if (!this.isCase) {
this.environments = [currentEnvironment];
}
}
});
this.initDataSource(envId, currentEnvironment, targetDataSourceName);
});
@ -319,7 +348,7 @@ export default {
}
}
let flag = false;
if (currentEnvironment.config && currentEnvironment.config.databaseConfigs) {
if (currentEnvironment && currentEnvironment.config && currentEnvironment.config.databaseConfigs) {
currentEnvironment.config.databaseConfigs.forEach(item => {
if (item.id === this.request.dataSourceId) {
flag = true;

View File

@ -137,6 +137,10 @@ export default {
tabType: String,
response: {},
apiId: String,
isScenario: {
type: Boolean,
default: false,
},
showScript: {
type: Boolean,
default: true,