fix(接口测试): 修复环境变量更新被覆盖问题

--bug=1018123 --user=赵勇 [接口测试]接口case后置脚本中设置环境参数执行后会覆盖掉手动添加的通用配置参数 https://www.tapd.cn/55049933/s/1277557
This commit is contained in:
fit2-zhao 2022-10-26 10:13:47 +08:00 committed by 刘瑞斌
parent 8d7847522c
commit 0382323ff7
1 changed files with 47 additions and 45 deletions

View File

@ -42,16 +42,32 @@ public class ApiEnvironmentRunningParamService {
if (environment == null) {
return;
}
boolean envNeedUpdate = false;
try {
JSONObject configObj = JSONUtil.parseObject(environment.getConfig());
if (configObj.has(COMMON_CONFIG)) {
if (!configObj.has(COMMON_CONFIG)) {
JSONObject commonConfig = new JSONObject();
List<JSONObject> variables = createArray(varMap);
JSONObject emptyObj = new JSONObject();
emptyObj.put(ENABLE, true);
variables.add(emptyObj);
commonConfig.put(VARIABLES, variables);
configObj.put(COMMON_CONFIG, commonConfig);
return;
}
JSONObject commonConfig = configObj.optJSONObject(COMMON_CONFIG);
if (commonConfig.has(VARIABLES)) {
if (!commonConfig.has(VARIABLES)) {
List<JSONObject> variables = createArray(varMap);
JSONObject emptyObj = new JSONObject();
emptyObj.put(ENABLE, true);
variables.add(emptyObj);
commonConfig.put(VARIABLES, variables);
return;
}
JSONArray variables = commonConfig.optJSONArray(VARIABLES);
if (variables == null) {
return;
}
boolean envNeedUpdate = false;
for (Map.Entry<String, String> entry : varMap.entrySet()) {
boolean contains = false;
for (int i = 0; i < variables.length(); i++) {
@ -72,35 +88,21 @@ public class ApiEnvironmentRunningParamService {
itemObj.put(NAME, entry.getKey());
itemObj.put(VALUE, entry.getValue());
itemObj.put(ENABLE, true);
if (variables.length() == 0) {
variables.put(itemObj);
} else {
if (variables.length() > 0 && StringUtils.isEmpty(variables.optJSONObject(variables.length() - 1).optString(NAME))) {
variables.put(variables.length() - 1, itemObj);
}
commonConfig.put(VARIABLES, variables);
}
}
} else {
List<JSONObject> variables = createArray(varMap);
variables.put(itemObj);
}
JSONObject emptyObj = new JSONObject();
emptyObj.put(ENABLE, true);
variables.add(emptyObj);
variables.put(emptyObj);
commonConfig.put(VARIABLES, variables);
}
} else {
JSONObject commonConfig = new JSONObject();
List<JSONObject> variables = createArray(varMap);
JSONObject emptyObj = new JSONObject();
emptyObj.put(ENABLE, true);
variables.add(emptyObj);
commonConfig.put(VARIABLES, variables);
configObj.put(COMMON_CONFIG, commonConfig);
}
if (envNeedUpdate) {
environment.setConfig(configObj.toString());
testEnvironmentMapper.updateByPrimaryKeyWithBLOBs(environment);
}
} catch (Exception ex) {
LoggerUtil.error("设置环境变量异常", ex);
}