From 9f4eb2a1fabe586cbcfb400f653e8777e7459d84 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Mon, 7 Mar 2022 12:44:23 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AE=BE=E7=BD=AE=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E5=8F=82=E6=95=B0=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化设置环境变量参数的代码 --- .../ApiEnvironmentRunningParamService.java | 111 ++++++++++++------ 1 file changed, 76 insertions(+), 35 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/service/ApiEnvironmentRunningParamService.java b/backend/src/main/java/io/metersphere/api/service/ApiEnvironmentRunningParamService.java index 04fbf08fd1..d0afa5d36e 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiEnvironmentRunningParamService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiEnvironmentRunningParamService.java @@ -5,13 +5,16 @@ import com.alibaba.fastjson.JSONObject; import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs; import io.metersphere.base.mapper.ApiTestEnvironmentMapper; import io.metersphere.utils.LoggerUtil; +import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @author song.tianyang @@ -23,74 +26,97 @@ public class ApiEnvironmentRunningParamService { @Resource ApiTestEnvironmentMapper testEnvironmentMapper; - public void addParam(String environmentId, String key, String value) { - if (StringUtils.isEmpty(key)) { + public void addParam(String environmentId, Map varMap) { + if (MapUtils.isEmpty(varMap)) { return; } ApiTestEnvironmentWithBLOBs environment = testEnvironmentMapper.selectByPrimaryKey(environmentId); if (environment == null) { return; } + boolean envNeedUpdate = false; try { JSONObject configObj = JSONObject.parseObject(environment.getConfig()); if (configObj.containsKey("commonConfig")) { JSONObject commonConfig = configObj.getJSONObject("commonConfig"); if (commonConfig.containsKey("variables")) { JSONArray variables = commonConfig.getJSONArray("variables"); - boolean contains = false; - for (int i = 0; i < variables.size(); i++) { - JSONObject jsonObj = variables.getJSONObject(i); - if (jsonObj.containsKey("name") && StringUtils.equals(jsonObj.getString("name"), key)) { - contains = true; - jsonObj.put("value", value); + + for (Map.Entry entry: varMap.entrySet()){ + String key = entry.getKey(); + String value = entry.getValue(); + + boolean contains = false; + for (int i = 0; i < variables.size(); i++) { + JSONObject jsonObj = variables.getJSONObject(i); + if (jsonObj.containsKey("name") && StringUtils.equals(jsonObj.getString("name"), key)) { + contains = true; + if(jsonObj.containsKey("value") && StringUtils.equals(jsonObj.getString("value"), value)){ + break; + }else { + envNeedUpdate = true; + jsonObj.put("value", value); + } + + } + } + if (!contains) { + envNeedUpdate = true; + JSONObject itemObj = new JSONObject(); + itemObj.put("name", key); + itemObj.put("value", value); + itemObj.put("enable", true); + + if (variables.size() == 0) { + variables.add(itemObj); + } else { + variables.add(variables.size() - 1, itemObj); + } + commonConfig.put("variables", variables); } } - if (!contains) { + } else { + JSONArray variables = new JSONArray(); + for (Map.Entry 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); - - if (variables.size() == 0) { - variables.add(itemObj); - } else { - variables.add(variables.size() - 1, itemObj); - } - commonConfig.put("variables", variables); + variables.add(itemObj); } - } else { - JSONArray variables = new JSONArray(); - JSONObject itemObj = new JSONObject(); - itemObj.put("name", key); - itemObj.put("value", value); - itemObj.put("enable", true); - JSONObject emptyObj = new JSONObject(); emptyObj.put("enable", true); - - variables.add(itemObj); variables.add(emptyObj); + commonConfig.put("variables", variables); + } } else { JSONObject commonConfig = new JSONObject(); JSONArray variables = new JSONArray(); - JSONObject itemObj = new JSONObject(); - itemObj.put("name", key); - itemObj.put("value", value); - itemObj.put("enable", true); - + for (Map.Entry 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(); emptyObj.put("enable", true); - - variables.add(itemObj); variables.add(emptyObj); commonConfig.put("variables", variables); configObj.put("commonConfig", commonConfig); } + if(envNeedUpdate){ + environment.setConfig(configObj.toJSONString()); + testEnvironmentMapper.updateByPrimaryKeyWithBLOBs(environment); + } - environment.setConfig(configObj.toJSONString()); - testEnvironmentMapper.updateByPrimaryKeyWithBLOBs(environment); } catch (Exception ex) { LoggerUtil.error("设置环境变量异常:" + ex.getMessage()); } @@ -98,6 +124,7 @@ public class ApiEnvironmentRunningParamService { public void parseEvn(String envStr) { String[] envStringArr = envStr.split("\n"); + Map> envVarsMap = new HashMap<>(); for (String env : envStringArr) { if (StringUtils.contains(env, "=")) { String[] envItem = env.split("="); @@ -110,12 +137,26 @@ public class ApiEnvironmentRunningParamService { String key = StringUtils.join(keyArr, "."); String value = StringUtils.substring(env, jmeterVarKey.length() + 1); if (StringUtils.isNoneEmpty(envId, key, value)) { - this.addParam(envId, key, value); + if(envVarsMap.containsKey(envId)){ + envVarsMap.get(envId).put(key,value); + }else { + Map varMap = new HashMap<>(); + varMap.put(key,value); + envVarsMap.put(envId,varMap); + } } } } } } + + if(MapUtils.isNotEmpty(envVarsMap)){ + for (Map.Entry> entry: envVarsMap.entrySet()){ + String envId = entry.getKey(); + Map vars = entry.getValue(); + this.addParam(envId, vars); + } + } } public void parseEnvironment(List evnStrList) {