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