diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java index 76fc62cc0d..e50cf145a8 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java @@ -16,22 +16,21 @@ import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs; import io.metersphere.base.mapper.ApiScenarioMapper; import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.constants.MsTestElementConstants; -import io.metersphere.commons.utils.BeanUtils; -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.commons.utils.*; import io.metersphere.constants.RunModeConstants; import io.metersphere.environment.service.BaseEnvGroupProjectService; import io.metersphere.environment.service.BaseEnvironmentService; import io.metersphere.plugin.core.MsParameter; import io.metersphere.plugin.core.MsTestElement; +import io.metersphere.service.MsHashTreeService; import lombok.Data; import lombok.EqualsAndHashCode; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.config.Arguments; import org.apache.jorphan.collections.HashTree; +import org.json.JSONArray; import org.json.JSONObject; import java.util.HashMap; @@ -194,7 +193,8 @@ public class MsScenario extends MsTestElement { mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); ApiScenarioWithBLOBs scenario = apiAutomationService.selectByPrimaryKey(this.getId()); 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)); this.setName(scenario.getName()); @@ -226,6 +226,37 @@ public class MsScenario extends MsTestElement { 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 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 envConfig, ParameterConfig newConfig) { if (this.isEnvironmentEnable()) { ApiScenarioMapper apiScenarioMapper = CommonBeanFactory.getBean(ApiScenarioMapper.class); diff --git a/api-test/backend/src/main/java/io/metersphere/service/MsHashTreeService.java b/api-test/backend/src/main/java/io/metersphere/service/MsHashTreeService.java index 81657b5933..982c9b1def 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/MsHashTreeService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/MsHashTreeService.java @@ -15,9 +15,11 @@ import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.JSONUtil; +import io.metersphere.commons.utils.LogUtil; import io.metersphere.service.definition.ApiDefinitionService; import io.metersphere.service.definition.ApiTestCaseService; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.json.JSONArray; import org.json.JSONObject; @@ -72,6 +74,7 @@ public class MsHashTreeService { public static final String PROJECT_ID = "projectId"; public static final String ACTIVE = "active"; public static final String ENV_MAP = "environmentMap"; + public static final String REF_ENABLE = "refEnable"; private static final String PRE = "PRE"; private static final String POST = "POST"; private static final String ASSERTIONS = ElementConstants.ASSERTIONS; @@ -203,7 +206,7 @@ public class MsHashTreeService { element.put(ENV_MAP, JSON.parseObject(scenarioWithBLOBs.getEnvironmentJson(), Map.class)); } 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(NAME, scenarioWithBLOBs.getName()); } @@ -222,6 +225,39 @@ public class MsHashTreeService { 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) { for (int i = 0; i < hashTree.length(); i++) { JSONObject element = hashTree.optJSONObject(i); diff --git a/api-test/frontend/src/business/automation/scenario/common/ApiBaseComponent.vue b/api-test/frontend/src/business/automation/scenario/common/ApiBaseComponent.vue index 6ce7add78f..43d6ed5b70 100644 --- a/api-test/frontend/src/business/automation/scenario/common/ApiBaseComponent.vue +++ b/api-test/frontend/src/business/automation/scenario/common/ApiBaseComponent.vue @@ -50,7 +50,7 @@ + :disabled="data.refEnable || !showVersion || isDeleted"/>