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.save.SaveService;
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.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.regex.Matcher;
@ -236,22 +233,6 @@ public class ElementUtil {
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) {
if (requests == null) {
requests = new ArrayList<>();
@ -410,19 +391,17 @@ public class ElementUtil {
}
MsHTTPSamplerProxy httpSamplerProxy = JSON.parseObject(element.toString(), MsHTTPSamplerProxy.class);
ParameterConfig config = (ParameterConfig) msParameter;
if (httpSamplerProxy != null &&
(!httpSamplerProxy.isCustomizeReq() || (httpSamplerProxy.isCustomizeReq()
&& BooleanUtils.isTrue(httpSamplerProxy.getIsRefEnvironment()))) && MapUtils.isNotEmpty(config.getConfig())) {
if (element != null && element.has(ElementConstants.HASH_TREE)) {
httpSamplerProxy.setHashTree(JSONUtil.readValue(element.optString(ElementConstants.HASH_TREE)));
}
if (httpSamplerProxy != null
&& (!httpSamplerProxy.isCustomizeReq() || BooleanUtils.isTrue(httpSamplerProxy.getIsRefEnvironment()))
&& MapUtils.isNotEmpty(config.getConfig())) {
try {
HashTree tmpHashTree = new HashTree();
httpSamplerProxy.toHashTree(tmpHashTree, null, msParameter);
if (tmpHashTree != null && tmpHashTree.getArray().length > 0) {
HTTPSamplerProxy object = (HTTPSamplerProxy) tmpHashTree.getArray()[0];
// 清空Domain
element.put("domain", "");
if (element.has(ElementConstants.HASH_TREE)) {
httpSamplerProxy.setHashTree(JSONUtil.readValue(element.optString(ElementConstants.HASH_TREE)));
}
HashTree testPlan = new HashTree();
httpSamplerProxy.toHashTree(testPlan, null, msParameter);
if (testPlan.getArray().length > 0) {
HTTPSamplerProxy object = (HTTPSamplerProxy) testPlan.getArray()[0];
if (object != null && StringUtils.isNotEmpty(object.getDomain())) {
element.put("domain", StringUtils.isNotEmpty(object.getProtocol()) ?
object.getProtocol() + "://" + object.getDomain() : object.getDomain());
@ -646,30 +625,6 @@ public class ElementUtil {
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>() {{
this.add(ElementConstants.JSR223_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 LABEL = "label";
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,
Boolean enable, String versionName,
Boolean versionEnable, ParameterConfig msParameter) {
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_ENABLE, versionEnable == null ? false : versionEnable);
if (msParameter != null) {
element.put(VERSION_ENABLE, versionEnable != null && versionEnable);
if ((!element.has(MARK) || !element.optBoolean(MARK)) && msParameter != null) {
element.put(DOMAIN, "");
ElementUtil.setDomain(element, msParameter);
}
}
@ -345,17 +348,17 @@ public class MsHashTreeService {
}
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)) {
element.put(MIX_ENABLE, false);
}
ApiScenarioDTO scenario = extApiScenarioMapper.selectById(element.optString(ID));
if (scenario != null && StringUtils.isNotEmpty(scenario.getScenarioDefinition())) {
boolean environmentEnable = element.has(ENV_ENABLE) ? element.optBoolean(ENV_ENABLE) : false;
boolean variableEnable = element.has(VARIABLE_ENABLE) ? element.optBoolean(VARIABLE_ENABLE) : false;
boolean mixEnable = element.has(MIX_ENABLE)
? element.getBoolean(MIX_ENABLE) : false;
boolean environmentEnable = element.has(ENV_ENABLE) && element.optBoolean(ENV_ENABLE);
boolean variableEnable = element.has(VARIABLE_ENABLE) && element.optBoolean(VARIABLE_ENABLE);
boolean mixEnable = element.has(MIX_ENABLE) && element.getBoolean(MIX_ENABLE);
element.put(MARK, false);
if (environmentEnable && StringUtils.isNotEmpty(scenario.getEnvironmentJson())) {
element.put(ENV_MAP, JSON.parseObject(scenario.getEnvironmentJson(), Map.class));
@ -366,6 +369,7 @@ public class MsHashTreeService {
element = object;
element.put(REFERENCED, REF);
element.put(NAME, scenario.getName());
element.put(MARK, environmentEnable);
}
element.put(ID, scenario.getId());
element.put(ENV_ENABLE, environmentEnable);
@ -406,9 +410,17 @@ public class MsHashTreeService {
hashTree.put(i, element);
}
if (element.has(HASH_TREE)) {
JSONArray elementJSONArray = element.optJSONArray(HASH_TREE);
dataFormatting(elementJSONArray, caseIds);
addMark(element);
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))) {
this.addCaseIds(element, caseIds);
}
if (element.has(HASH_TREE)) {
JSONArray elementJSONArray = element.optJSONArray(HASH_TREE);
dataFormatting(elementJSONArray, caseIds);
addMark(element);
dataFormatting(element.optJSONArray(HASH_TREE), caseIds);
}
}
@ -439,6 +450,7 @@ public class MsHashTreeService {
}
if (element.has(HASH_TREE)) {
JSONArray elementJSONArray = element.optJSONArray(HASH_TREE);
addMark(element);
caseFormatting(elementJSONArray, caseMap, apiMap, msParameter);
}
}