refactor(接口定义): 提取相同代码

This commit is contained in:
wxg0103 2023-01-06 14:55:49 +08:00 committed by wxg0103
parent a22418638b
commit f991fccd8e
4 changed files with 79 additions and 113 deletions

View File

@ -29,7 +29,6 @@ import io.metersphere.base.mapper.ApiScenarioMapper;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.constants.StorageConstants;
import io.metersphere.commons.enums.StorageEnums;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*;
import io.metersphere.commons.vo.JDBCProcessorVO;
@ -105,97 +104,64 @@ public class ElementUtil {
list = config.getTransferVariables().stream().filter(ScenarioVariable::isCSVValid).filter(ScenarioVariable::isEnable).collect(Collectors.toList());
}
if (CollectionUtils.isNotEmpty(list)) {
FileMetadataService fileMetadataService = CommonBeanFactory.getBean(FileMetadataService.class);
list.forEach(item -> {
CSVDataSet csvDataSet = new CSVDataSet();
csvDataSet.setEnabled(true);
csvDataSet.setProperty(TestElement.TEST_CLASS, CSVDataSet.class.getName());
csvDataSet.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(TEST_BEAN_GUI));
csvDataSet.setName(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName());
csvDataSet.setProperty("fileEncoding", StringUtils.isEmpty(item.getEncoding()) ? StandardCharsets.UTF_8.name() : item.getEncoding());
if (CollectionUtils.isEmpty(item.getFiles())) {
MSException.throwException(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName() + "[ " + Translator.get("csv_no_exist") + " ]");
} else {
boolean isRef = false;
String fileId = null;
boolean isRepository = false;
BodyFile file = item.getFiles().get(0);
String path = BODY_FILE_DIR + "/" + item.getFiles().get(0).getId() + "_" + item.getFiles().get(0).getName();
if (StringUtils.equalsIgnoreCase(file.getStorage(), StorageConstants.FILE_REF.name())) {
isRef = true;
fileId = file.getFileId();
if (fileMetadataService != null) {
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(fileId);
if (fileMetadata != null && !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) {
isRepository = true;
}
}
path = FileUtils.getFilePath(file);
}
if (!config.isOperating() && !isRepository && !new File(path).exists()) {
MSException.throwException(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName() + "[ " + Translator.get("csv_no_exist") + " ]");
}
csvDataSet.setProperty(ElementConstants.FILENAME, path);
csvDataSet.setProperty(ElementConstants.IS_REF, isRef);
csvDataSet.setProperty(ElementConstants.FILE_ID, fileId);
csvDataSet.setProperty(ElementConstants.RESOURCE_ID, file.getId());
}
csvDataSet.setIgnoreFirstLine(false);
csvDataSet.setProperty("shareMode", shareMode);
csvDataSet.setProperty("recycle", true);
csvDataSet.setProperty("delimiter", item.getDelimiter());
csvDataSet.setProperty("quotedData", item.isQuotedData());
csvDataSet.setComment(StringUtils.isEmpty(item.getDescription()) ? "" : item.getDescription());
tree.add(csvDataSet);
});
addCsv(tree, config, shareMode, list);
}
}
}
private static void addCsv(HashTree tree, ParameterConfig config, String shareMode, List<ScenarioVariable> list) {
FileMetadataService fileMetadataService = CommonBeanFactory.getBean(FileMetadataService.class);
list.forEach(item -> {
CSVDataSet csvDataSet = new CSVDataSet();
csvDataSet.setEnabled(true);
csvDataSet.setProperty(TestElement.TEST_CLASS, CSVDataSet.class.getName());
csvDataSet.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(TEST_BEAN_GUI));
csvDataSet.setName(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName());
csvDataSet.setProperty("fileEncoding", StringUtils.isEmpty(item.getEncoding()) ? StandardCharsets.UTF_8.name() : item.getEncoding());
if (CollectionUtils.isEmpty(item.getFiles())) {
MSException.throwException(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName() + "[ " + Translator.get("csv_no_exist") + " ]");
} else {
String fileId = null;
boolean isRepository = false;
BodyFile file = item.getFiles().get(0);
boolean isRef = StringUtils.equalsIgnoreCase(file.getStorage(), StorageConstants.FILE_REF.name());
String path = StringUtils.join(BODY_FILE_DIR, File.pathSeparator, item.getFiles().get(0).getId(), "_", item.getFiles().get(0).getName());
if (StringUtils.equalsIgnoreCase(file.getStorage(), StorageConstants.FILE_REF.name())) {
fileId = file.getFileId();
FileMetadata fileMetadata = fileMetadataService.getFileMetadataById(fileId);
if (fileMetadata != null
&& !StringUtils.equals(fileMetadata.getStorage(), StorageConstants.LOCAL.name())) {
isRepository = true;
}
path = FileUtils.getFilePath(file);
}
if (!config.isOperating() && !isRepository && !new File(path).exists()) {
// 从MinIO下载
ApiFileUtil.downloadFile(file.getId(), path);
if (!new File(path).exists()) {
MSException.throwException(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName() + "[ " + Translator.get("csv_no_exist") + " ]");
}
}
csvDataSet.setProperty(ElementConstants.FILENAME, path);
csvDataSet.setProperty(ElementConstants.IS_REF, isRef);
csvDataSet.setProperty(ElementConstants.FILE_ID, fileId);
csvDataSet.setProperty(ElementConstants.RESOURCE_ID, file.getId());
}
csvDataSet.setIgnoreFirstLine(false);
csvDataSet.setProperty("shareMode", shareMode);
csvDataSet.setProperty("recycle", true);
csvDataSet.setProperty("delimiter", item.getDelimiter());
csvDataSet.setProperty("quotedData", item.isQuotedData());
csvDataSet.setComment(StringUtils.isEmpty(item.getDescription()) ? "" : item.getDescription());
tree.add(csvDataSet);
});
}
public static void addApiCsvDataSet(HashTree tree, List<ScenarioVariable> variables, ParameterConfig config, String shareMode) {
if (CollectionUtils.isNotEmpty(variables)) {
List<ScenarioVariable> list = variables.stream().filter(ScenarioVariable::isCSVValid).filter(ScenarioVariable::isEnable).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(list)) {
list.forEach(item -> {
CSVDataSet csvDataSet = new CSVDataSet();
csvDataSet.setEnabled(true);
csvDataSet.setProperty(TestElement.TEST_CLASS, CSVDataSet.class.getName());
csvDataSet.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(TEST_BEAN_GUI));
csvDataSet.setName(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName());
csvDataSet.setProperty("fileEncoding", StringUtils.isEmpty(item.getEncoding()) ? StandardCharsets.UTF_8.name() : item.getEncoding());
if (CollectionUtils.isEmpty(item.getFiles())) {
MSException.throwException(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName() + "[ " + Translator.get("csv_no_exist") + " ]");
} else {
BodyFile file = item.getFiles().get(0);
String fileId = item.getId();
boolean isRef = false;
String path;
if (StringUtils.equalsIgnoreCase(file.getStorage(), StorageConstants.FILE_REF.name())) {
isRef = true;
fileId = file.getFileId();
path = FileUtils.getFilePath(file);
} else {
path = BODY_FILE_DIR + "/" + item.getFiles().get(0).getId() + "_" + item.getFiles().get(0).getName();
if (StringUtils.equalsIgnoreCase(file.getStorage(), StorageEnums.FILE_REF.name())) {
path = ApiFileUtil.getFilePath(file);
}
if (!config.isOperating() && !new File(path).exists()) {
MSException.throwException(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName() + "[ " + Translator.get("csv_no_exist") + " ]");
}
}
csvDataSet.setProperty(ElementConstants.FILENAME, path);
csvDataSet.setProperty(ElementConstants.IS_REF, isRef);
csvDataSet.setProperty(ElementConstants.FILE_ID, fileId);
csvDataSet.setProperty(ElementConstants.RESOURCE_ID, file.getId());
}
csvDataSet.setIgnoreFirstLine(false);
csvDataSet.setProperty("shareMode", shareMode);
csvDataSet.setProperty("recycle", true);
csvDataSet.setProperty("delimiter", item.getDelimiter());
csvDataSet.setProperty("quotedData", item.isQuotedData());
csvDataSet.setComment(StringUtils.isEmpty(item.getDescription()) ? "" : item.getDescription());
tree.add(csvDataSet);
});
addCsv(tree, config, shareMode, list);
}
}
}

View File

@ -554,7 +554,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
try {
String value = keyValue.getValue() != null && keyValue.getValue().startsWith("@") ?
ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue();
value = keyValue.isUrlEncode() ? StringUtils.join(StringUtils.join("${__urlencode(", value), ")}") : value;
value = keyValue.isUrlEncode() ? StringUtils.join("${__urlencode(", value, ")}") : value;
keyValueMap.put(keyValue.getName(), value);
} catch (Exception e) {
LogUtil.error(e);
@ -585,11 +585,11 @@ public class MsHTTPSamplerProxy extends MsTestElement {
stringBuffer.append("?");
}
this.getArguments().stream().filter(KeyValue::isEnable).filter(KeyValue::isValid).forEach(keyValue -> {
stringBuffer.append(keyValue.isUrlEncode() ? StringUtils.join(StringUtils.join("${__urlencode(", keyValue.getName()), ")}") : keyValue.getName());
stringBuffer.append(keyValue.isUrlEncode() ? StringUtils.join("${__urlencode(", keyValue.getName(), ")}") : keyValue.getName());
if (keyValue.getValue() != null) {
try {
String value = keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue();
value = keyValue.isUrlEncode() ? StringUtils.join(StringUtils.join("${__urlencode(", value), ")}") : value;
value = keyValue.isUrlEncode() ? StringUtils.join("${__urlencode(", value, ")}") : value;
if (StringUtils.isNotEmpty(value) && value.contains(StringUtils.CR)) {
value = value.replaceAll(StringUtils.CR, StringUtils.EMPTY);
}

View File

@ -99,8 +99,8 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
httpOld.getHeaders().remove(httpOld.getHeaders().size() - 1);
}
String headerNew = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(httpNew.getHeaders())), JSON_END);
String headerOld = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(httpOld.getHeaders())), JSON_END);
String headerNew = StringUtils.join(JSON_START, JSON.toJSONString(httpNew.getHeaders()), JSON_END);
String headerOld = StringUtils.join(JSON_START, JSON.toJSONString(httpOld.getHeaders()), JSON_END);
if (!StringUtils.equals(headerNew, headerOld)) {
String patch = jsonDiff.diff(headerOld, headerNew);
String diffPatch = jsonDiff.apply(headerOld, patch);
@ -113,8 +113,8 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
httpNew.getArguments().remove(httpNew.getArguments().size() - 1);
httpOld.getArguments().remove(httpOld.getArguments().size() - 1);
}
String queryNew = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(httpNew.getArguments())), JSON_END);
String queryOld = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(httpOld.getArguments())), JSON_END);
String queryNew = StringUtils.join(JSON_START, JSON.toJSONString(httpNew.getArguments()), JSON_END);
String queryOld = StringUtils.join(JSON_START, JSON.toJSONString(httpOld.getArguments()), JSON_END);
if (!StringUtils.equals(queryNew, queryOld)) {
String patch = jsonDiff.diff(queryOld, queryNew);
String diff = jsonDiff.apply(queryOld, patch);
@ -127,8 +127,8 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
httpNew.getRest().remove(httpNew.getRest().size() - 1);
httpOld.getRest().remove(httpOld.getRest().size() - 1);
}
String restNew = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(httpNew.getRest())), JSON_END);
String restOld = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(httpOld.getRest())), JSON_END);
String restNew = StringUtils.join(JSON_START, JSON.toJSONString(httpNew.getRest()), JSON_END);
String restOld = StringUtils.join(JSON_START, JSON.toJSONString(httpOld.getRest()), JSON_END);
if (!StringUtils.equals(restNew, restOld)) {
String patch = jsonDiff.diff(restOld, restNew);
String diff = jsonDiff.apply(restOld, patch);
@ -152,8 +152,8 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
httpNew.getBody().getKvs().remove(httpNew.getBody().getKvs().size() - 1);
httpOld.getBody().getKvs().remove(httpOld.getBody().getKvs().size() - 1);
}
String bodyFormNew = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(httpNew.getBody().getKvs())), JSON_END);
String bodyFormOld = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(httpOld.getBody().getKvs())), JSON_END);
String bodyFormNew = StringUtils.join(JSON_START, JSON.toJSONString(httpNew.getBody().getKvs()), JSON_END);
String bodyFormOld = StringUtils.join(JSON_START, JSON.toJSONString(httpOld.getBody().getKvs()), JSON_END);
if (!StringUtils.equals(bodyFormNew, bodyFormOld)) {
String patch = jsonDiff.diff(bodyFormOld, bodyFormNew);
String diff = jsonDiff.apply(bodyFormNew, patch);
@ -197,8 +197,8 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
private static void diffHttpResponse(JSONObject httpNew, JSONObject httpOld, JsonDiff jsonDiff, Map<String, String> diffMap) {
// 请求头对比 old/new
if (httpNew.get(HEADS) != null && httpOld.get(HEADS) != null) {
String headerNew = StringUtils.join(StringUtils.join(JSON_START, httpNew.get(HEADS).toString()), JSON_END);
String headerOld = StringUtils.join(StringUtils.join(JSON_START, httpOld.get(HEADS).toString()), JSON_END);
String headerNew = StringUtils.join(JSON_START, httpNew.get(HEADS).toString(), JSON_END);
String headerOld = StringUtils.join(JSON_START, httpOld.get(HEADS).toString(), JSON_END);
if (!StringUtils.equals(headerNew, headerOld)) {
String patch = jsonDiff.diff(headerOld, headerNew);
String diffPatch = jsonDiff.apply(headerNew, patch);
@ -209,8 +209,8 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
}
// 对比statusCode参数
if (httpNew.get(STATUS_CODE) != null && httpOld.get(STATUS_CODE) != null) {
String statusCodeNew = StringUtils.join(StringUtils.join(JSON_START, httpNew.get(STATUS_CODE).toString()), JSON_END);
String statusCodeOld = StringUtils.join(StringUtils.join(JSON_START, httpOld.get(STATUS_CODE).toString()), JSON_END);
String statusCodeNew = StringUtils.join(JSON_START, httpNew.get(STATUS_CODE).toString(), JSON_END);
String statusCodeOld = StringUtils.join(JSON_START, httpOld.get(STATUS_CODE).toString(), JSON_END);
if (!StringUtils.equals(statusCodeNew, statusCodeOld)) {
String patch = jsonDiff.diff(statusCodeOld, statusCodeNew);
String diff = jsonDiff.apply(statusCodeNew, patch);
@ -238,8 +238,8 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
bodyNew.getKvs().remove(bodyNew.getKvs().size() - 1);
bodyOld.getKvs().remove(bodyOld.getKvs().size() - 1);
}
String bodyFormNew = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(bodyNew.getKvs())), JSON_END);
String bodyFormOld = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(bodyOld.getKvs())), JSON_END);
String bodyFormNew = StringUtils.join(JSON_START, JSON.toJSONString(bodyNew.getKvs()), JSON_END);
String bodyFormOld = StringUtils.join(JSON_START, JSON.toJSONString(bodyOld.getKvs()), JSON_END);
if (!StringUtils.equals(bodyFormNew, bodyFormOld)) {
String patch = jsonDiff.diff(bodyFormOld, bodyFormNew);
String diff = jsonDiff.apply(bodyFormNew, patch);
@ -261,8 +261,8 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
tcpNew.getParameters().remove(tcpNew.getParameters().size() - 1);
tcpOld.getParameters().remove(tcpOld.getParameters().size() - 1);
}
String queryNew = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(tcpNew.getParameters())), JSON_END);
String queryOld = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(tcpOld.getParameters())), JSON_END);
String queryNew = StringUtils.join(JSON_START, JSON.toJSONString(tcpNew.getParameters()), JSON_END);
String queryOld = StringUtils.join(JSON_START, JSON.toJSONString(tcpOld.getParameters()), JSON_END);
if (!StringUtils.equals(queryNew, queryOld)) {
String patch = jsonDiff.diff(queryOld, queryNew);
String diff = jsonDiff.apply(queryNew, patch);
@ -279,8 +279,8 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
}
}
// 对比BODY-XML参数
String xmlNew = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(tcpNew.getXmlDataStruct())), JSON_END);
String xmlOld = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(tcpOld.getXmlDataStruct())), JSON_END);
String xmlNew = StringUtils.join(JSON_START, JSON.toJSONString(tcpNew.getXmlDataStruct()), JSON_END);
String xmlOld = StringUtils.join(JSON_START, JSON.toJSONString(tcpOld.getXmlDataStruct()), JSON_END);
if (!StringUtils.equals(xmlNew, xmlOld)) {
diffMap.put(StringUtils.join(BODY_XML, "_1"), JSON.toJSONString(tcpNew.getXmlDataStruct()));
diffMap.put(StringUtils.join(BODY_XML, "_2"), JSON.toJSONString(tcpOld.getXmlDataStruct()));
@ -326,8 +326,8 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
jdbcNew.getVariables().remove(jdbcNew.getVariables().size() - 1);
jdbcOld.getVariables().remove(jdbcOld.getVariables().size() - 1);
}
String variablesNew = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(jdbcNew.getVariables())), JSON_END);
String variablesOld = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(jdbcOld.getVariables())), JSON_END);
String variablesNew = StringUtils.join(JSON_START, JSON.toJSONString(jdbcNew.getVariables()), JSON_END);
String variablesOld = StringUtils.join(JSON_START, JSON.toJSONString(jdbcOld.getVariables()), JSON_END);
if (!StringUtils.equals(variablesNew, variablesOld)) {
String patch = jsonDiff.diff(variablesOld, variablesNew);
String diffPatch = jsonDiff.apply(variablesNew, patch);
@ -367,8 +367,8 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
dubboNew.getArgs().remove(dubboNew.getArgs().size() - 1);
dubboOld.getArgs().remove(dubboOld.getArgs().size() - 1);
}
String argsNew = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(dubboNew.getArgs())), JSON_END);
String argsOld = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(dubboOld.getArgs())), JSON_END);
String argsNew = StringUtils.join(JSON_START, JSON.toJSONString(dubboNew.getArgs()), JSON_END);
String argsOld = StringUtils.join(JSON_START, JSON.toJSONString(dubboOld.getArgs()), JSON_END);
if (!StringUtils.equals(argsNew, argsOld)) {
String patch = jsonDiff.diff(argsOld, argsNew);
String diffPatch = jsonDiff.apply(argsNew, patch);
@ -381,8 +381,8 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
dubboNew.getAttachmentArgs().remove(dubboNew.getAttachmentArgs().size() - 1);
dubboOld.getAttachmentArgs().remove(dubboOld.getAttachmentArgs().size() - 1);
}
String attachmentNew = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(dubboNew.getAttachmentArgs())), JSON_END);
String attachmentOld = StringUtils.join(StringUtils.join(JSON_START, JSON.toJSONString(dubboOld.getAttachmentArgs())), JSON_END);
String attachmentNew = StringUtils.join(JSON_START, JSON.toJSONString(dubboNew.getAttachmentArgs()), JSON_END);
String attachmentOld = StringUtils.join(JSON_START, JSON.toJSONString(dubboOld.getAttachmentArgs()), JSON_END);
if (!StringUtils.equals(attachmentNew, attachmentOld)) {
String patch = jsonDiff.diff(attachmentOld, attachmentNew);
String diffPatch = jsonDiff.apply(attachmentNew, patch);

View File

@ -174,14 +174,14 @@ public class ReflexObjectUtil {
List<String> originalValueArray = JSON.parseArray(originalValue.toString(), String.class);
Collections.sort(originalValueArray);
Object originalObject = JSON.toJSONString(originalValueArray);
oldTags = StringUtils.join(StringUtils.join(JSON_START, ((originalColumns.get(i) != null && originalObject != null) ? originalObject.toString() : "\"\"")), JSON_END);
oldTags = StringUtils.join(JSON_START, ((originalColumns.get(i) != null && originalObject != null) ? originalObject.toString() : "\"\""), JSON_END);
}
List<String> newValueArray = JSON.parseArray(newValue.toString(), String.class);
if (CollectionUtils.isNotEmpty(newValueArray)) {
Collections.sort(newValueArray);
}
Object newObject = JSON.toJSONString(newValueArray);
String newTags = StringUtils.join(StringUtils.join(JSON_START, ((newColumns.get(i) != null && newObject != null) ? newObject.toString() : "\"\"")), JSON_END);
String newTags = StringUtils.join(JSON_START, ((newColumns.get(i) != null && newObject != null) ? newObject.toString() : "\"\""), JSON_END);
String diffValue;
if (oldTags != null) {
String diffStr = diff.diff(oldTags, newTags);