fix(接口测试): 修复执行过程更新环境变量丢失问题

--bug=1018123 --user=赵勇 [接口测试]接口case后置脚本中设置环境参数执行后会覆盖掉手动添加的通用配置参数 https://www.tapd.cn/55049933/s/1263602
This commit is contained in:
fit2-zhao 2022-10-17 11:29:36 +08:00 committed by fit2-zhao
parent 5621516b13
commit 21594843d4
4 changed files with 44 additions and 49 deletions

View File

@ -2,7 +2,6 @@ package io.metersphere.api.exec.scenario;
import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs; import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.base.mapper.ApiTestEnvironmentMapper; import io.metersphere.base.mapper.ApiTestEnvironmentMapper;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.JSONUtil; import io.metersphere.commons.utils.JSONUtil;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
@ -26,6 +25,12 @@ import java.util.Map;
@Service @Service
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public class ApiEnvironmentRunningParamService { public class ApiEnvironmentRunningParamService {
public static final String COMMON_CONFIG = "commonConfig";
public static final String VARIABLES = "variables";
public static final String VALUE = "value";
public static final String ENABLE = "enable";
public static final String NAME = "name";
public static final String ENV_STR = "MS.ENV.";
@Resource @Resource
ApiTestEnvironmentMapper testEnvironmentMapper; ApiTestEnvironmentMapper testEnvironmentMapper;
@ -40,10 +45,10 @@ public class ApiEnvironmentRunningParamService {
boolean envNeedUpdate = false; boolean envNeedUpdate = false;
try { try {
JSONObject configObj = JSONUtil.parseObject(environment.getConfig()); JSONObject configObj = JSONUtil.parseObject(environment.getConfig());
if (configObj.has("commonConfig")) { if (configObj.has(COMMON_CONFIG)) {
JSONObject commonConfig = configObj.optJSONObject("commonConfig"); JSONObject commonConfig = configObj.optJSONObject(COMMON_CONFIG);
if (commonConfig.has("variables")) { if (commonConfig.has(VARIABLES)) {
JSONArray variables = commonConfig.optJSONArray("variables"); JSONArray variables = commonConfig.optJSONArray(VARIABLES);
List<JSONObject> variableList = new LinkedList<>(); List<JSONObject> variableList = new LinkedList<>();
for (Map.Entry<String, String> entry : varMap.entrySet()) { for (Map.Entry<String, String> entry : varMap.entrySet()) {
String key = entry.getKey(); String key = entry.getKey();
@ -52,64 +57,45 @@ public class ApiEnvironmentRunningParamService {
boolean contains = false; boolean contains = false;
for (int i = 0; i < variables.length(); i++) { for (int i = 0; i < variables.length(); i++) {
JSONObject jsonObj = variables.optJSONObject(i); JSONObject jsonObj = variables.optJSONObject(i);
if (jsonObj.has("name") && StringUtils.equals(jsonObj.optString("name"), key)) { if (jsonObj.has(NAME) && StringUtils.equals(jsonObj.optString(NAME), key)) {
contains = true; contains = true;
if (jsonObj.has("value") && StringUtils.equals(jsonObj.optString("value"), value)) { if (jsonObj.has(VALUE) && StringUtils.equals(jsonObj.optString(VALUE), value)) {
break; break;
} else { } else {
envNeedUpdate = true; envNeedUpdate = true;
jsonObj.put("value", value); jsonObj.put(VALUE, value);
} }
} }
} }
if (!contains) { if (!contains) {
envNeedUpdate = true; envNeedUpdate = true;
JSONObject itemObj = new JSONObject(); JSONObject itemObj = new JSONObject();
itemObj.put("name", key); itemObj.put(NAME, key);
itemObj.put("value", value); itemObj.put(VALUE, value);
itemObj.put("enable", true); itemObj.put(ENABLE, true);
if (variableList.size() == 0) { if (variableList.size() == 0) {
variableList.add(itemObj); variableList.add(itemObj);
} else { } else {
variableList.add(variables.length() - 1, itemObj); variableList.add(variables.length() - 1, itemObj);
} }
commonConfig.put("variables", new JSONArray(variableList)); commonConfig.put(VARIABLES, variableList);
} }
} }
} else { } else {
List<JSONObject> variables = new LinkedList<>(); List<JSONObject> variables = createArray(varMap);
for (Map.Entry<String, String> entry : varMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
JSONObject itemObj = new JSONObject();
itemObj.put("name", key);
itemObj.put("value", value);
itemObj.put("enable", true);
variables.add(itemObj);
}
JSONObject emptyObj = new JSONObject(); JSONObject emptyObj = new JSONObject();
emptyObj.put("enable", true); emptyObj.put(ENABLE, true);
variables.add(emptyObj); variables.add(emptyObj);
commonConfig.put("variables", new JSONArray(variables)); commonConfig.put(VARIABLES, variables);
} }
} else { } else {
JSONObject commonConfig = new JSONObject(); JSONObject commonConfig = new JSONObject();
List<JSONObject> variables = new LinkedList<>(); List<JSONObject> variables = createArray(varMap);
for (Map.Entry<String, String> entry : varMap.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
JSONObject itemObj = new JSONObject();
itemObj.put("name", key);
itemObj.put("value", value);
itemObj.put("enable", true);
variables.add(itemObj);
}
JSONObject emptyObj = new JSONObject(); JSONObject emptyObj = new JSONObject();
emptyObj.put("enable", true); emptyObj.put(ENABLE, true);
variables.add(emptyObj); variables.add(emptyObj);
commonConfig.put("variables", new JSONArray(variables)); commonConfig.put(VARIABLES, variables);
configObj.put("commonConfig", commonConfig); configObj.put(COMMON_CONFIG, commonConfig);
} }
if (envNeedUpdate) { if (envNeedUpdate) {
environment.setConfig(configObj.toString()); environment.setConfig(configObj.toString());
@ -131,8 +117,8 @@ public class ApiEnvironmentRunningParamService {
continue; continue;
} }
String jmeterVarKey = envItem[0]; String jmeterVarKey = envItem[0];
if (this.checkValidity(jmeterVarKey, "MS.ENV.")) { if (this.checkValidity(jmeterVarKey, ENV_STR)) {
String[] envAndKeyArr = jmeterVarKey.substring("MS.ENV.".length()).split("\\."); String[] envAndKeyArr = jmeterVarKey.substring(ENV_STR.length()).split("\\.");
if (ArrayUtils.isEmpty(envAndKeyArr)) { if (ArrayUtils.isEmpty(envAndKeyArr)) {
continue; continue;
} }
@ -172,6 +158,18 @@ public class ApiEnvironmentRunningParamService {
} }
} }
private List<JSONObject> createArray(Map<String, String> varMap) {
List<JSONObject> variables = new LinkedList<>();
for (Map.Entry<String, String> entry : varMap.entrySet()) {
JSONObject itemObj = new JSONObject();
itemObj.put(NAME, entry.getKey());
itemObj.put(VALUE, entry.getValue());
itemObj.put(ENABLE, true);
variables.add(itemObj);
}
return variables;
}
public boolean checkValidity(String str, String regex) { public boolean checkValidity(String str, String regex) {
if (str == null) { if (str == null) {
return false; return false;

View File

@ -1,7 +1,6 @@
package io.metersphere.commons.utils; package io.metersphere.commons.utils;
import io.metersphere.api.exec.engine.EngineSourceParserFactory; import io.metersphere.api.exec.engine.EngineSourceParserFactory;
import io.metersphere.commons.utils.LogUtil;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document; import org.dom4j.Document;
import org.dom4j.Element; import org.dom4j.Element;
@ -190,7 +189,7 @@ public class XMLUtil {
if (list.size() == 1) { if (list.size() == 1) {
result.put(node.getName(), list.get(0)); result.put(node.getName(), list.get(0));
} else { } else {
result.put(node.getName(), new JSONArray(list)); result.put(node.getName(), list);
} }
} else { } else {
if (!StringUtils.isAllBlank(node.getName(), node.getText())) { if (!StringUtils.isAllBlank(node.getName(), node.getText())) {

View File

@ -354,9 +354,7 @@ public class MockApiUtils {
if (!((JSONObject) paramJson).keySet().isEmpty()) { if (!((JSONObject) paramJson).keySet().isEmpty()) {
JSONArray bodyParams = returnParams.getBodyParams(); JSONArray bodyParams = returnParams.getBodyParams();
if (bodyParams == null) { if (bodyParams == null) {
List<Object> paramsArray = new LinkedList<>(); bodyParams.put(paramJson);
paramsArray.add(paramJson);
bodyParams = new JSONArray(paramsArray);
} else { } else {
bodyParams.put(((JSONObject) paramJson)); bodyParams.put(((JSONObject) paramJson));
} }
@ -392,9 +390,9 @@ public class MockApiUtils {
requestMockParams.setQueryParamsObj(queryParamsObject); requestMockParams.setQueryParamsObj(queryParamsObject);
if (isPostRequest) { if (isPostRequest) {
List<Object> jsonArray = new ArrayList<>(); JSONArray jsonArray = new JSONArray();
jsonArray.add(queryParamsObject); jsonArray.put(queryParamsObject);
requestMockParams.setBodyParams(new JSONArray(jsonArray)); requestMockParams.setBodyParams(jsonArray);
} }
return requestMockParams; return requestMockParams;
} }

View File

@ -150,7 +150,7 @@ public class MsHashTreeService {
if (CollectionUtils.isNotEmpty(rules)) { if (CollectionUtils.isNotEmpty(rules)) {
step.addAll(rules); step.addAll(rules);
} }
element.put(HASH_TREE, new JSONArray(step)); element.put(HASH_TREE, step);
} }
element.put(REFERENCED, REF); element.put(REFERENCED, REF);
element.put(DISABLED, true); element.put(DISABLED, true);