fix(接口测试): 设置环境变量参数时允许设置为空

设置环境变量参数时允许设置为空
This commit is contained in:
song-tianyang 2022-03-07 13:12:48 +08:00 committed by fit2-zhao
parent 9f4eb2a1fa
commit cd41d140e1
1 changed files with 25 additions and 27 deletions

View File

@ -26,7 +26,7 @@ public class ApiEnvironmentRunningParamService {
@Resource @Resource
ApiTestEnvironmentMapper testEnvironmentMapper; ApiTestEnvironmentMapper testEnvironmentMapper;
public void addParam(String environmentId, Map<String,String> varMap) { public void addParam(String environmentId, Map<String, String> varMap) {
if (MapUtils.isEmpty(varMap)) { if (MapUtils.isEmpty(varMap)) {
return; return;
} }
@ -42,7 +42,7 @@ public class ApiEnvironmentRunningParamService {
if (commonConfig.containsKey("variables")) { if (commonConfig.containsKey("variables")) {
JSONArray variables = commonConfig.getJSONArray("variables"); JSONArray variables = commonConfig.getJSONArray("variables");
for (Map.Entry<String, String> entry: varMap.entrySet()){ for (Map.Entry<String, String> entry : varMap.entrySet()) {
String key = entry.getKey(); String key = entry.getKey();
String value = entry.getValue(); String value = entry.getValue();
@ -51,9 +51,9 @@ public class ApiEnvironmentRunningParamService {
JSONObject jsonObj = variables.getJSONObject(i); JSONObject jsonObj = variables.getJSONObject(i);
if (jsonObj.containsKey("name") && StringUtils.equals(jsonObj.getString("name"), key)) { if (jsonObj.containsKey("name") && StringUtils.equals(jsonObj.getString("name"), key)) {
contains = true; contains = true;
if(jsonObj.containsKey("value") && StringUtils.equals(jsonObj.getString("value"), value)){ if (jsonObj.containsKey("value") && StringUtils.equals(jsonObj.getString("value"), value)) {
break; break;
}else { } else {
envNeedUpdate = true; envNeedUpdate = true;
jsonObj.put("value", value); jsonObj.put("value", value);
} }
@ -77,7 +77,7 @@ public class ApiEnvironmentRunningParamService {
} }
} else { } else {
JSONArray variables = new JSONArray(); JSONArray variables = new JSONArray();
for (Map.Entry<String, String> entry: varMap.entrySet()){ for (Map.Entry<String, String> entry : varMap.entrySet()) {
String key = entry.getKey(); String key = entry.getKey();
String value = entry.getValue(); String value = entry.getValue();
@ -97,7 +97,7 @@ public class ApiEnvironmentRunningParamService {
} else { } else {
JSONObject commonConfig = new JSONObject(); JSONObject commonConfig = new JSONObject();
JSONArray variables = new JSONArray(); JSONArray variables = new JSONArray();
for (Map.Entry<String, String> entry: varMap.entrySet()) { for (Map.Entry<String, String> entry : varMap.entrySet()) {
String key = entry.getKey(); String key = entry.getKey();
String value = entry.getValue(); String value = entry.getValue();
JSONObject itemObj = new JSONObject(); JSONObject itemObj = new JSONObject();
@ -112,7 +112,7 @@ public class ApiEnvironmentRunningParamService {
commonConfig.put("variables", variables); commonConfig.put("variables", variables);
configObj.put("commonConfig", commonConfig); configObj.put("commonConfig", commonConfig);
} }
if(envNeedUpdate){ if (envNeedUpdate) {
environment.setConfig(configObj.toJSONString()); environment.setConfig(configObj.toJSONString());
testEnvironmentMapper.updateByPrimaryKeyWithBLOBs(environment); testEnvironmentMapper.updateByPrimaryKeyWithBLOBs(environment);
} }
@ -124,36 +124,34 @@ public class ApiEnvironmentRunningParamService {
public void parseEvn(String envStr) { public void parseEvn(String envStr) {
String[] envStringArr = envStr.split("\n"); String[] envStringArr = envStr.split("\n");
Map<String, Map<String,String>> envVarsMap = new HashMap<>(); Map<String, Map<String, String>> envVarsMap = new HashMap<>();
for (String env : envStringArr) { for (String env : envStringArr) {
if (StringUtils.contains(env, "=")) { if (StringUtils.contains(env, "=")) {
String[] envItem = env.split("="); String[] envItem = env.split("=");
if (envItem.length > 1) { String jmeterVarKey = envItem[0];
String jmeterVarKey = envItem[0]; if (this.checkValidity(jmeterVarKey, "MS.ENV.")) {
if (this.checkValidity(jmeterVarKey, "MS.ENV.")) { String[] envAndKeyArr = jmeterVarKey.substring("MS.ENV.".length()).split("\\.");
String[] envAndKeyArr = jmeterVarKey.substring("MS.ENV.".length()).split("\\."); String envId = envAndKeyArr[0];
String envId = envAndKeyArr[0]; String[] keyArr = ArrayUtils.remove(envAndKeyArr, 0);
String[] keyArr = ArrayUtils.remove(envAndKeyArr, 0); String key = StringUtils.join(keyArr, ".");
String key = StringUtils.join(keyArr, "."); String value = StringUtils.substring(env, jmeterVarKey.length() + 1);
String value = StringUtils.substring(env, jmeterVarKey.length() + 1); if (StringUtils.isNoneEmpty(envId, key)) {
if (StringUtils.isNoneEmpty(envId, key, value)) { if (envVarsMap.containsKey(envId)) {
if(envVarsMap.containsKey(envId)){ envVarsMap.get(envId).put(key, value);
envVarsMap.get(envId).put(key,value); } else {
}else { Map<String, String> varMap = new HashMap<>();
Map<String,String> varMap = new HashMap<>(); varMap.put(key, value);
varMap.put(key,value); envVarsMap.put(envId, varMap);
envVarsMap.put(envId,varMap);
}
} }
} }
} }
} }
} }
if(MapUtils.isNotEmpty(envVarsMap)){ if (MapUtils.isNotEmpty(envVarsMap)) {
for (Map.Entry<String, Map<String, String>> entry: envVarsMap.entrySet()){ for (Map.Entry<String, Map<String, String>> entry : envVarsMap.entrySet()) {
String envId = entry.getKey(); String envId = entry.getKey();
Map<String,String> vars = entry.getValue(); Map<String, String> vars = entry.getValue();
this.addParam(envId, vars); this.addParam(envId, vars);
} }
} }