fix(接口测试): 修复执行过程更新环境变量丢失问题
--bug=1018123 --user=赵勇 [接口测试]接口case后置脚本中设置环境参数执行后会覆盖掉手动添加的通用配置参数 https://www.tapd.cn/55049933/s/1263602
This commit is contained in:
parent
5621516b13
commit
21594843d4
|
@ -2,7 +2,6 @@ package io.metersphere.api.exec.scenario;
|
|||
|
||||
import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
|
||||
import io.metersphere.base.mapper.ApiTestEnvironmentMapper;
|
||||
import io.metersphere.commons.utils.JSON;
|
||||
import io.metersphere.commons.utils.JSONUtil;
|
||||
import io.metersphere.utils.LoggerUtil;
|
||||
import org.apache.commons.collections.MapUtils;
|
||||
|
@ -26,6 +25,12 @@ import java.util.Map;
|
|||
@Service
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
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
|
||||
ApiTestEnvironmentMapper testEnvironmentMapper;
|
||||
|
||||
|
@ -40,10 +45,10 @@ public class ApiEnvironmentRunningParamService {
|
|||
boolean envNeedUpdate = false;
|
||||
try {
|
||||
JSONObject configObj = JSONUtil.parseObject(environment.getConfig());
|
||||
if (configObj.has("commonConfig")) {
|
||||
JSONObject commonConfig = configObj.optJSONObject("commonConfig");
|
||||
if (commonConfig.has("variables")) {
|
||||
JSONArray variables = commonConfig.optJSONArray("variables");
|
||||
if (configObj.has(COMMON_CONFIG)) {
|
||||
JSONObject commonConfig = configObj.optJSONObject(COMMON_CONFIG);
|
||||
if (commonConfig.has(VARIABLES)) {
|
||||
JSONArray variables = commonConfig.optJSONArray(VARIABLES);
|
||||
List<JSONObject> variableList = new LinkedList<>();
|
||||
for (Map.Entry<String, String> entry : varMap.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
|
@ -52,64 +57,45 @@ public class ApiEnvironmentRunningParamService {
|
|||
boolean contains = false;
|
||||
for (int i = 0; i < variables.length(); 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;
|
||||
if (jsonObj.has("value") && StringUtils.equals(jsonObj.optString("value"), value)) {
|
||||
if (jsonObj.has(VALUE) && StringUtils.equals(jsonObj.optString(VALUE), value)) {
|
||||
break;
|
||||
} else {
|
||||
envNeedUpdate = true;
|
||||
jsonObj.put("value", value);
|
||||
jsonObj.put(VALUE, value);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
if (!contains) {
|
||||
envNeedUpdate = true;
|
||||
JSONObject itemObj = new JSONObject();
|
||||
itemObj.put("name", key);
|
||||
itemObj.put("value", value);
|
||||
itemObj.put("enable", true);
|
||||
itemObj.put(NAME, key);
|
||||
itemObj.put(VALUE, value);
|
||||
itemObj.put(ENABLE, true);
|
||||
if (variableList.size() == 0) {
|
||||
variableList.add(itemObj);
|
||||
} else {
|
||||
variableList.add(variables.length() - 1, itemObj);
|
||||
}
|
||||
commonConfig.put("variables", new JSONArray(variableList));
|
||||
commonConfig.put(VARIABLES, variableList);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
List<JSONObject> variables = new LinkedList<>();
|
||||
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);
|
||||
}
|
||||
List<JSONObject> variables = createArray(varMap);
|
||||
JSONObject emptyObj = new JSONObject();
|
||||
emptyObj.put("enable", true);
|
||||
emptyObj.put(ENABLE, true);
|
||||
variables.add(emptyObj);
|
||||
commonConfig.put("variables", new JSONArray(variables));
|
||||
commonConfig.put(VARIABLES, variables);
|
||||
}
|
||||
} else {
|
||||
JSONObject commonConfig = new JSONObject();
|
||||
List<JSONObject> variables = new LinkedList<>();
|
||||
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);
|
||||
}
|
||||
List<JSONObject> variables = createArray(varMap);
|
||||
JSONObject emptyObj = new JSONObject();
|
||||
emptyObj.put("enable", true);
|
||||
emptyObj.put(ENABLE, true);
|
||||
variables.add(emptyObj);
|
||||
commonConfig.put("variables", new JSONArray(variables));
|
||||
configObj.put("commonConfig", commonConfig);
|
||||
commonConfig.put(VARIABLES, variables);
|
||||
configObj.put(COMMON_CONFIG, commonConfig);
|
||||
}
|
||||
if (envNeedUpdate) {
|
||||
environment.setConfig(configObj.toString());
|
||||
|
@ -131,8 +117,8 @@ public class ApiEnvironmentRunningParamService {
|
|||
continue;
|
||||
}
|
||||
String jmeterVarKey = envItem[0];
|
||||
if (this.checkValidity(jmeterVarKey, "MS.ENV.")) {
|
||||
String[] envAndKeyArr = jmeterVarKey.substring("MS.ENV.".length()).split("\\.");
|
||||
if (this.checkValidity(jmeterVarKey, ENV_STR)) {
|
||||
String[] envAndKeyArr = jmeterVarKey.substring(ENV_STR.length()).split("\\.");
|
||||
if (ArrayUtils.isEmpty(envAndKeyArr)) {
|
||||
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) {
|
||||
if (str == null) {
|
||||
return false;
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package io.metersphere.commons.utils;
|
||||
|
||||
import io.metersphere.api.exec.engine.EngineSourceParserFactory;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.Element;
|
||||
|
@ -190,7 +189,7 @@ public class XMLUtil {
|
|||
if (list.size() == 1) {
|
||||
result.put(node.getName(), list.get(0));
|
||||
} else {
|
||||
result.put(node.getName(), new JSONArray(list));
|
||||
result.put(node.getName(), list);
|
||||
}
|
||||
} else {
|
||||
if (!StringUtils.isAllBlank(node.getName(), node.getText())) {
|
||||
|
|
|
@ -354,9 +354,7 @@ public class MockApiUtils {
|
|||
if (!((JSONObject) paramJson).keySet().isEmpty()) {
|
||||
JSONArray bodyParams = returnParams.getBodyParams();
|
||||
if (bodyParams == null) {
|
||||
List<Object> paramsArray = new LinkedList<>();
|
||||
paramsArray.add(paramJson);
|
||||
bodyParams = new JSONArray(paramsArray);
|
||||
bodyParams.put(paramJson);
|
||||
} else {
|
||||
bodyParams.put(((JSONObject) paramJson));
|
||||
}
|
||||
|
@ -392,9 +390,9 @@ public class MockApiUtils {
|
|||
requestMockParams.setQueryParamsObj(queryParamsObject);
|
||||
|
||||
if (isPostRequest) {
|
||||
List<Object> jsonArray = new ArrayList<>();
|
||||
jsonArray.add(queryParamsObject);
|
||||
requestMockParams.setBodyParams(new JSONArray(jsonArray));
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
jsonArray.put(queryParamsObject);
|
||||
requestMockParams.setBodyParams(jsonArray);
|
||||
}
|
||||
return requestMockParams;
|
||||
}
|
||||
|
|
|
@ -150,7 +150,7 @@ public class MsHashTreeService {
|
|||
if (CollectionUtils.isNotEmpty(rules)) {
|
||||
step.addAll(rules);
|
||||
}
|
||||
element.put(HASH_TREE, new JSONArray(step));
|
||||
element.put(HASH_TREE, step);
|
||||
}
|
||||
element.put(REFERENCED, REF);
|
||||
element.put(DISABLED, true);
|
||||
|
|
Loading…
Reference in New Issue