feat(接口定义): 引用的场景或case支持启用禁用

--story=1010337 --user=王孝刚 【接口自动化】引用的场景或CASE , 支持启用/禁用
https://www.tapd.cn/55049933/s/1295405
This commit is contained in:
wxg0103 2022-11-14 13:53:09 +08:00 committed by wxg0103
parent 59a5e4cd12
commit a21f22f944
3 changed files with 75 additions and 8 deletions

View File

@ -16,22 +16,21 @@ import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.base.mapper.ApiScenarioMapper; import io.metersphere.base.mapper.ApiScenarioMapper;
import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.MsTestElementConstants; import io.metersphere.commons.constants.MsTestElementConstants;
import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.*;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.FileUtils;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.constants.RunModeConstants; import io.metersphere.constants.RunModeConstants;
import io.metersphere.environment.service.BaseEnvGroupProjectService; import io.metersphere.environment.service.BaseEnvGroupProjectService;
import io.metersphere.environment.service.BaseEnvironmentService; import io.metersphere.environment.service.BaseEnvironmentService;
import io.metersphere.plugin.core.MsParameter; import io.metersphere.plugin.core.MsParameter;
import io.metersphere.plugin.core.MsTestElement; import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.MsHashTreeService;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.config.Arguments; import org.apache.jmeter.config.Arguments;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.HashMap; import java.util.HashMap;
@ -194,7 +193,8 @@ public class MsScenario extends MsTestElement {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ApiScenarioWithBLOBs scenario = apiAutomationService.selectByPrimaryKey(this.getId()); ApiScenarioWithBLOBs scenario = apiAutomationService.selectByPrimaryKey(this.getId());
if (scenario != null && StringUtils.isNotEmpty(scenario.getScenarioDefinition())) { if (scenario != null && StringUtils.isNotEmpty(scenario.getScenarioDefinition())) {
JSONObject element = JSONUtil.parseObject(scenario.getScenarioDefinition()); JSONObject elementOrg = JSONUtil.parseObject(scenario.getScenarioDefinition());
JSONObject element = setRefEnable(this, elementOrg);
// 历史数据处理 // 历史数据处理
ElementUtil.dataFormatting(element.optJSONArray(ElementConstants.HASH_TREE)); ElementUtil.dataFormatting(element.optJSONArray(ElementConstants.HASH_TREE));
this.setName(scenario.getName()); this.setName(scenario.getName());
@ -226,6 +226,37 @@ public class MsScenario extends MsTestElement {
return false; return false;
} }
public static JSONObject setRefEnable(MsTestElement targetElement, JSONObject orgElement) {
if (JSONObject.NULL.equals(orgElement) || targetElement == null) {
return orgElement;
}
if (BooleanUtils.isFalse(orgElement.optBoolean(MsHashTreeService.ENABLE))) {
orgElement.put(MsHashTreeService.ENABLE, false);
} else {
orgElement.put(MsHashTreeService.ENABLE, targetElement.isEnable());
}
try {
if (orgElement.has(MsHashTreeService.HASH_TREE)) {
JSONArray orgJSONArray = orgElement.optJSONArray(MsHashTreeService.HASH_TREE);
LinkedList<MsTestElement> hashTree = targetElement.getHashTree();
if (orgJSONArray != null && CollectionUtils.isNotEmpty(hashTree)) {
orgJSONArray.forEach(obj -> {
JSONObject orgJsonObject = (JSONObject) obj;
hashTree.forEach(targetObj -> {
if (StringUtils.equals(orgJsonObject.optString(MsHashTreeService.ID), targetObj.getId())) {
setRefEnable(targetObj, orgJsonObject);
}
});
});
}
}
} catch (Exception ex) {
LogUtil.error(ex, ex.getMessage());
return orgElement;
}
return orgElement;
}
private void setNewConfig(Map<String, EnvironmentConfig> envConfig, ParameterConfig newConfig) { private void setNewConfig(Map<String, EnvironmentConfig> envConfig, ParameterConfig newConfig) {
if (this.isEnvironmentEnable()) { if (this.isEnvironmentEnable()) {
ApiScenarioMapper apiScenarioMapper = CommonBeanFactory.getBean(ApiScenarioMapper.class); ApiScenarioMapper apiScenarioMapper = CommonBeanFactory.getBean(ApiScenarioMapper.class);

View File

@ -15,9 +15,11 @@ import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.JSONUtil; import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.service.definition.ApiDefinitionService; import io.metersphere.service.definition.ApiDefinitionService;
import io.metersphere.service.definition.ApiTestCaseService; import io.metersphere.service.definition.ApiTestCaseService;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -72,6 +74,7 @@ public class MsHashTreeService {
public static final String PROJECT_ID = "projectId"; public static final String PROJECT_ID = "projectId";
public static final String ACTIVE = "active"; public static final String ACTIVE = "active";
public static final String ENV_MAP = "environmentMap"; public static final String ENV_MAP = "environmentMap";
public static final String REF_ENABLE = "refEnable";
private static final String PRE = "PRE"; private static final String PRE = "PRE";
private static final String POST = "POST"; private static final String POST = "POST";
private static final String ASSERTIONS = ElementConstants.ASSERTIONS; private static final String ASSERTIONS = ElementConstants.ASSERTIONS;
@ -203,7 +206,7 @@ public class MsHashTreeService {
element.put(ENV_MAP, JSON.parseObject(scenarioWithBLOBs.getEnvironmentJson(), Map.class)); element.put(ENV_MAP, JSON.parseObject(scenarioWithBLOBs.getEnvironmentJson(), Map.class));
} }
if (StringUtils.equalsIgnoreCase(element.optString(REFERENCED), REF)) { if (StringUtils.equalsIgnoreCase(element.optString(REFERENCED), REF)) {
element = JSONUtil.parseObject(scenarioWithBLOBs.getScenarioDefinition()); element = setRefEnable(element, JSONUtil.parseObject(scenarioWithBLOBs.getScenarioDefinition()));
element.put(REFERENCED, REF); element.put(REFERENCED, REF);
element.put(NAME, scenarioWithBLOBs.getName()); element.put(NAME, scenarioWithBLOBs.getName());
} }
@ -222,6 +225,39 @@ public class MsHashTreeService {
return element; return element;
} }
public static JSONObject setRefEnable(JSONObject targetElement, JSONObject orgElement) {
if (orgElement == null || targetElement == null) {
return orgElement;
}
if (BooleanUtils.isFalse(orgElement.optBoolean(ENABLE))) {
orgElement.put(ENABLE, false);
orgElement.put(REF_ENABLE, true);
} else {
orgElement.put(ENABLE, targetElement.optBoolean(ENABLE));
}
try {
if (orgElement.has(HASH_TREE)) {
JSONArray orgJSONArray = orgElement.optJSONArray(HASH_TREE);
JSONArray targetJSONArray = targetElement.optJSONArray(HASH_TREE);
if (orgJSONArray != null && targetJSONArray != null) {
orgJSONArray.forEach(obj -> {
JSONObject orgJsonObject = (JSONObject) obj;
targetJSONArray.forEach(targetObj -> {
JSONObject targetJsonObject = (JSONObject) targetObj;
if (StringUtils.equals(orgJsonObject.optString(ID), targetJsonObject.optString(ID))) {
setRefEnable(targetJsonObject, orgJsonObject);
}
});
});
}
}
} catch (Exception e) {
LogUtil.error(e, e.getMessage());
return orgElement;
}
return orgElement;
}
public void dataFormatting(JSONArray hashTree) { public void dataFormatting(JSONArray hashTree) {
for (int i = 0; i < hashTree.length(); i++) { for (int i = 0; i < hashTree.length(); i++) {
JSONObject element = hashTree.optJSONObject(i); JSONObject element = hashTree.optJSONObject(i);

View File

@ -50,7 +50,7 @@
<el-tooltip :content="$t('test_resource_pool.enable_disable')" placement="top" v-if="showBtn"> <el-tooltip :content="$t('test_resource_pool.enable_disable')" placement="top" v-if="showBtn">
<el-switch v-model="data.enable" class="enable-switch" size="mini" <el-switch v-model="data.enable" class="enable-switch" size="mini"
:disabled="(data.disabled && !data.root) || !showVersion || isDeleted"/> :disabled="data.refEnable || !showVersion || isDeleted"/>
</el-tooltip> </el-tooltip>
<el-button v-if="showVersion && showCopy" size="mini" icon="el-icon-copy-document" circle @click="copyRow" <el-button v-if="showVersion && showCopy" size="mini" icon="el-icon-copy-document" circle @click="copyRow"