diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiEnvironmentRunningParamService.java b/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiEnvironmentRunningParamService.java index d6f6df68b3..771ecff3e5 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiEnvironmentRunningParamService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiEnvironmentRunningParamService.java @@ -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 variableList = new LinkedList<>(); for (Map.Entry 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 variables = new LinkedList<>(); - 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); - } + List 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 variables = new LinkedList<>(); - 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); - } + List 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 createArray(Map varMap) { + List variables = new LinkedList<>(); + for (Map.Entry 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; diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/XMLUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/XMLUtil.java index 2c2f8629f0..8d84f150b0 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/XMLUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/XMLUtil.java @@ -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())) { diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockApiUtils.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockApiUtils.java index 34e0f5a675..6fc26c1a0d 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockApiUtils.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockApiUtils.java @@ -354,9 +354,7 @@ public class MockApiUtils { if (!((JSONObject) paramJson).keySet().isEmpty()) { JSONArray bodyParams = returnParams.getBodyParams(); if (bodyParams == null) { - List 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 jsonArray = new ArrayList<>(); - jsonArray.add(queryParamsObject); - requestMockParams.setBodyParams(new JSONArray(jsonArray)); + JSONArray jsonArray = new JSONArray(); + jsonArray.put(queryParamsObject); + requestMockParams.setBodyParams(jsonArray); } return requestMockParams; } diff --git a/api-test/backend/src/main/java/io/metersphere/service/MsHashTreeService.java b/api-test/backend/src/main/java/io/metersphere/service/MsHashTreeService.java index 1d56c5fada..93f79c75a9 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/MsHashTreeService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/MsHashTreeService.java @@ -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);