refactor(接口测试): 场景步骤URL展示优化

Signed-off-by: fit2-zhao <yong.zhao@fit2cloud.com>
This commit is contained in:
fit2-zhao 2023-08-16 10:28:52 +08:00 committed by fit2-zhao
parent ee176ad091
commit de35f2d65a
2 changed files with 35 additions and 68 deletions

View File

@ -64,15 +64,12 @@ import org.apache.jmeter.protocol.jdbc.AbstractJDBCTestElement;
import org.apache.jmeter.protocol.jdbc.config.DataSourceElement; import org.apache.jmeter.protocol.jdbc.config.DataSourceElement;
import org.apache.jmeter.save.SaveService; import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement; import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -236,22 +233,6 @@ public class ElementUtil {
return getFullIndexPath(element.getParent(), path); return getFullIndexPath(element.getParent(), path);
} }
public static boolean isURL(String str) {
try {
if (StringUtils.isEmpty(str)) {
return false;
}
new URL(str);
return true;
} catch (Exception e) {
// 支持包含变量的url
if (str.matches("^(http|https|ftp)://.*$") && str.matches(".*://\\$\\{.*$")) {
return true;
}
return false;
}
}
public static <T> List<T> findFromHashTreeByType(MsTestElement hashTree, Class<T> clazz, List<T> requests) { public static <T> List<T> findFromHashTreeByType(MsTestElement hashTree, Class<T> clazz, List<T> requests) {
if (requests == null) { if (requests == null) {
requests = new ArrayList<>(); requests = new ArrayList<>();
@ -410,19 +391,17 @@ public class ElementUtil {
} }
MsHTTPSamplerProxy httpSamplerProxy = JSON.parseObject(element.toString(), MsHTTPSamplerProxy.class); MsHTTPSamplerProxy httpSamplerProxy = JSON.parseObject(element.toString(), MsHTTPSamplerProxy.class);
ParameterConfig config = (ParameterConfig) msParameter; ParameterConfig config = (ParameterConfig) msParameter;
if (httpSamplerProxy != null && if (httpSamplerProxy != null
(!httpSamplerProxy.isCustomizeReq() || (httpSamplerProxy.isCustomizeReq() && (!httpSamplerProxy.isCustomizeReq() || BooleanUtils.isTrue(httpSamplerProxy.getIsRefEnvironment()))
&& BooleanUtils.isTrue(httpSamplerProxy.getIsRefEnvironment()))) && MapUtils.isNotEmpty(config.getConfig())) { && MapUtils.isNotEmpty(config.getConfig())) {
if (element != null && element.has(ElementConstants.HASH_TREE)) {
httpSamplerProxy.setHashTree(JSONUtil.readValue(element.optString(ElementConstants.HASH_TREE)));
}
try { try {
HashTree tmpHashTree = new HashTree(); if (element.has(ElementConstants.HASH_TREE)) {
httpSamplerProxy.toHashTree(tmpHashTree, null, msParameter); httpSamplerProxy.setHashTree(JSONUtil.readValue(element.optString(ElementConstants.HASH_TREE)));
if (tmpHashTree != null && tmpHashTree.getArray().length > 0) { }
HTTPSamplerProxy object = (HTTPSamplerProxy) tmpHashTree.getArray()[0]; HashTree testPlan = new HashTree();
// 清空Domain httpSamplerProxy.toHashTree(testPlan, null, msParameter);
element.put("domain", ""); if (testPlan.getArray().length > 0) {
HTTPSamplerProxy object = (HTTPSamplerProxy) testPlan.getArray()[0];
if (object != null && StringUtils.isNotEmpty(object.getDomain())) { if (object != null && StringUtils.isNotEmpty(object.getDomain())) {
element.put("domain", StringUtils.isNotEmpty(object.getProtocol()) ? element.put("domain", StringUtils.isNotEmpty(object.getProtocol()) ?
object.getProtocol() + "://" + object.getDomain() : object.getDomain()); object.getProtocol() + "://" + object.getDomain() : object.getDomain());
@ -646,30 +625,6 @@ public class ElementUtil {
sampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(id, config, parent, indexPath)); sampler.setProperty("MS-RESOURCE-ID", ElementUtil.getResourceId(id, config, parent, indexPath));
} }
public static void accuracyHashTree(HashTree hashTree) {
Map<Object, HashTree> objects = new LinkedHashMap<>();
Object groupHashTree = hashTree;
if (hashTree != null && hashTree.size() > 0) {
for (Object key : hashTree.keySet()) {
if (key instanceof TestPlan) {
for (Object node : hashTree.get(key).keySet()) {
if (node instanceof ThreadGroup) {
groupHashTree = hashTree.get(key).get(node);
}
}
} else {
objects.put(key, hashTree.get(key));
}
}
}
if (!objects.isEmpty() && groupHashTree instanceof HashTree) {
for (Object key : objects.keySet()) {
hashTree.remove(key);
((HashTree) groupHashTree).add(key, objects.get(key));
}
}
}
private static final List<String> preOperates = new ArrayList<String>() {{ private static final List<String> preOperates = new ArrayList<String>() {{
this.add(ElementConstants.JSR223_PRE); this.add(ElementConstants.JSR223_PRE);
this.add(ElementConstants.JDBC_PRE); this.add(ElementConstants.JDBC_PRE);

View File

@ -106,15 +106,18 @@ public class MsHashTreeService {
private final static String DOCUMENT = "document"; private final static String DOCUMENT = "document";
private final static String LABEL = "label"; private final static String LABEL = "label";
private final static String SCENARIO_REF = "SCENARIO-REF-STEP"; private final static String SCENARIO_REF = "SCENARIO-REF-STEP";
private final static String DOMAIN = "domain";
private final static String MARK = "markStep";
private void setElement(JSONObject element, Integer num, private void setElement(JSONObject element, Integer num,
Boolean enable, String versionName, Boolean enable, String versionName,
Boolean versionEnable, ParameterConfig msParameter) { Boolean versionEnable, ParameterConfig msParameter) {
element.put(NUM, num); element.put(NUM, num);
element.put(ENABLE, enable == null ? false : enable); element.put(ENABLE, enable != null && enable);
element.put(VERSION_NAME, versionName); element.put(VERSION_NAME, versionName);
element.put(VERSION_ENABLE, versionEnable == null ? false : versionEnable); element.put(VERSION_ENABLE, versionEnable != null && versionEnable);
if (msParameter != null) { if ((!element.has(MARK) || !element.optBoolean(MARK)) && msParameter != null) {
element.put(DOMAIN, "");
ElementUtil.setDomain(element, msParameter); ElementUtil.setDomain(element, msParameter);
} }
} }
@ -345,17 +348,17 @@ public class MsHashTreeService {
} }
private JSONObject setRefScenario(JSONObject element) { private JSONObject setRefScenario(JSONObject element) {
boolean enable = element.has(ENABLE) ? element.optBoolean(ENABLE) : true; boolean enable = !element.has(ENABLE) || element.optBoolean(ENABLE);
if (!element.has(MIX_ENABLE)) { if (!element.has(MIX_ENABLE)) {
element.put(MIX_ENABLE, false); element.put(MIX_ENABLE, false);
} }
ApiScenarioDTO scenario = extApiScenarioMapper.selectById(element.optString(ID)); ApiScenarioDTO scenario = extApiScenarioMapper.selectById(element.optString(ID));
if (scenario != null && StringUtils.isNotEmpty(scenario.getScenarioDefinition())) { if (scenario != null && StringUtils.isNotEmpty(scenario.getScenarioDefinition())) {
boolean environmentEnable = element.has(ENV_ENABLE) ? element.optBoolean(ENV_ENABLE) : false; boolean environmentEnable = element.has(ENV_ENABLE) && element.optBoolean(ENV_ENABLE);
boolean variableEnable = element.has(VARIABLE_ENABLE) ? element.optBoolean(VARIABLE_ENABLE) : false; boolean variableEnable = element.has(VARIABLE_ENABLE) && element.optBoolean(VARIABLE_ENABLE);
boolean mixEnable = element.has(MIX_ENABLE) boolean mixEnable = element.has(MIX_ENABLE) && element.getBoolean(MIX_ENABLE);
? element.getBoolean(MIX_ENABLE) : false; element.put(MARK, false);
if (environmentEnable && StringUtils.isNotEmpty(scenario.getEnvironmentJson())) { if (environmentEnable && StringUtils.isNotEmpty(scenario.getEnvironmentJson())) {
element.put(ENV_MAP, JSON.parseObject(scenario.getEnvironmentJson(), Map.class)); element.put(ENV_MAP, JSON.parseObject(scenario.getEnvironmentJson(), Map.class));
@ -366,6 +369,7 @@ public class MsHashTreeService {
element = object; element = object;
element.put(REFERENCED, REF); element.put(REFERENCED, REF);
element.put(NAME, scenario.getName()); element.put(NAME, scenario.getName());
element.put(MARK, environmentEnable);
} }
element.put(ID, scenario.getId()); element.put(ID, scenario.getId());
element.put(ENV_ENABLE, environmentEnable); element.put(ENV_ENABLE, environmentEnable);
@ -406,9 +410,17 @@ public class MsHashTreeService {
hashTree.put(i, element); hashTree.put(i, element);
} }
if (element.has(HASH_TREE)) { if (element.has(HASH_TREE)) {
JSONArray elementJSONArray = element.optJSONArray(HASH_TREE); addMark(element);
dataFormatting(elementJSONArray, caseIds); dataFormatting(element.optJSONArray(HASH_TREE), caseIds);
} }
}
}
private void addMark(JSONObject element) {
JSONArray objects = element.optJSONArray(HASH_TREE);
for (int j = 0; j < objects.length(); j++) {
objects.optJSONObject(j).put(MARK, element.optBoolean(MARK));
} }
} }
@ -422,10 +434,9 @@ public class MsHashTreeService {
} else if (ElementConstants.REQUESTS.contains(element.optString(TYPE))) { } else if (ElementConstants.REQUESTS.contains(element.optString(TYPE))) {
this.addCaseIds(element, caseIds); this.addCaseIds(element, caseIds);
} }
if (element.has(HASH_TREE)) { if (element.has(HASH_TREE)) {
JSONArray elementJSONArray = element.optJSONArray(HASH_TREE); addMark(element);
dataFormatting(elementJSONArray, caseIds); dataFormatting(element.optJSONArray(HASH_TREE), caseIds);
} }
} }
@ -439,6 +450,7 @@ public class MsHashTreeService {
} }
if (element.has(HASH_TREE)) { if (element.has(HASH_TREE)) {
JSONArray elementJSONArray = element.optJSONArray(HASH_TREE); JSONArray elementJSONArray = element.optJSONArray(HASH_TREE);
addMark(element);
caseFormatting(elementJSONArray, caseMap, apiMap, msParameter); caseFormatting(elementJSONArray, caseMap, apiMap, msParameter);
} }
} }