refactor(接口测试): 修复含有7z文件的接口导出jmx文件失败的问题

修复含有7z文件的接口导出jmx文件失败的问题
This commit is contained in:
song-tianyang 2022-06-17 18:18:51 +08:00 committed by f2c-ci-robot[bot]
parent eb78063a85
commit 102a981b7b
2 changed files with 47 additions and 44 deletions

View File

@ -41,7 +41,6 @@ import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.jorphan.collections.HashTree;
import org.aspectj.util.FileUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -460,53 +459,57 @@ public class APITestService {
* @return
* @author song tianyang
*/
public JmxInfoDTO updateJmxString(String jmx, String projectId) {
public JmxInfoDTO updateJmxString(String jmx, String projectId, boolean saveFile) {
jmx = this.updateJmxMessage(jmx);
//获取要转化的文件
List<String> attachmentFilePathList = new ArrayList<>();
try {
Document doc = EngineSourceParserFactory.getDocument(new ByteArrayInputStream(jmx.getBytes("utf-8")));
Element root = doc.getRootElement();
Element rootHashTreeElement = root.element("hashTree");
List<Element> innerHashTreeElementList = rootHashTreeElement.elements("hashTree");
for (Element innerHashTreeElement : innerHashTreeElementList) {
List<Element> thirdHashTreeElementList = innerHashTreeElement.elements();
for (Element element : thirdHashTreeElementList) {
//HTTPSamplerProxy 进行附件转化 1.elementProp里去掉路径 2elementProp->filePath获取路径并读出来
attachmentFilePathList.addAll(this.parseAttachmentFileInfo(element));
}
//如果存在证书文件也要匹配出来
attachmentFilePathList.addAll(this.parseAttachmentFileInfo(rootHashTreeElement));
}
} catch (Exception e) {
LogUtil.error(e);
}
if (!jmx.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
jmx = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + jmx;
}
//处理附件
Map<String, String> attachmentFiles = new HashMap<>();
//去重处理
if (!CollectionUtils.isEmpty(attachmentFilePathList)) {
attachmentFilePathList = attachmentFilePathList.stream().distinct().collect(Collectors.toList());
}
List<FileMetadata> fileMetadataList = new ArrayList<>();
for (String filePath : attachmentFilePathList) {
File file = new File(filePath);
if (file.exists() && file.isFile()) {
try {
FileMetadata fileMetadata = fileService.insertFileByFileName(file, FileUtil.readAsByteArray(file), projectId);
if (fileMetadata != null) {
fileMetadataList.add(fileMetadata);
attachmentFiles.put(fileMetadata.getId(), fileMetadata.getName());
if (saveFile) {
//获取要转化的文件
List<String> attachmentFilePathList = new ArrayList<>();
try {
Document doc = EngineSourceParserFactory.getDocument(new ByteArrayInputStream(jmx.getBytes("utf-8")));
Element root = doc.getRootElement();
Element rootHashTreeElement = root.element("hashTree");
List<Element> innerHashTreeElementList = rootHashTreeElement.elements("hashTree");
for (Element innerHashTreeElement : innerHashTreeElementList) {
List<Element> thirdHashTreeElementList = innerHashTreeElement.elements();
for (Element element : thirdHashTreeElementList) {
//HTTPSamplerProxy 进行附件转化 1.elementProp里去掉路径 2elementProp->filePath获取路径并读出来
attachmentFilePathList.addAll(this.parseAttachmentFileInfo(element));
}
//如果存在证书文件也要匹配出来
attachmentFilePathList.addAll(this.parseAttachmentFileInfo(rootHashTreeElement));
}
} catch (Exception e) {
LogUtil.error(e);
}
//去重处理
if (!CollectionUtils.isEmpty(attachmentFilePathList)) {
attachmentFilePathList = attachmentFilePathList.stream().distinct().collect(Collectors.toList());
}
for (String filePath : attachmentFilePathList) {
File file = new File(filePath);
if (file.exists() && file.isFile()) {
try {
FileMetadata fileMetadata = fileService.insertFileByFileName(file, FileUtil.readAsByteArray(file), projectId);
if (fileMetadata != null) {
fileMetadataList.add(fileMetadata);
attachmentFiles.put(fileMetadata.getId(), fileMetadata.getName());
}
} catch (Exception e) {
LogUtil.error(e);
}
} catch (Exception e) {
LogUtil.error(e);
}
}
}
if (!jmx.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")) {
jmx = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + jmx;
}
JmxInfoDTO returnDTO = new JmxInfoDTO("Demo.jmx", jmx, attachmentFiles);
returnDTO.setFileMetadataList(fileMetadataList);
return returnDTO;
@ -628,7 +631,7 @@ public class APITestService {
HashTree hashTree = runRequest.getTestElement().generateHashTree(config);
String jmxString = runRequest.getTestElement().getJmx(hashTree);
//将jmx处理封装为通用方法
JmxInfoDTO dto = updateJmxString(jmxString, runRequest.getProjectId());
JmxInfoDTO dto = updateJmxString(jmxString, runRequest.getProjectId(), true);
dto.setName(runRequest.getName() + ".jmx");
return dto;
}

View File

@ -1102,7 +1102,7 @@ public class ApiAutomationService {
MsTestPlan testPlan = new MsTestPlan();
testPlan.setHashTree(new LinkedList<>());
ApiScenarioDTO scenario = apiScenarios.get(0);
JmxInfoDTO dto = apiTestService.updateJmxString(generateJmx(scenario), scenario.getProjectId());
JmxInfoDTO dto = apiTestService.updateJmxString(generateJmx(scenario), scenario.getProjectId(), true);
String name = request.getName() + ".jmx";
dto.setName(name);
@ -1512,8 +1512,8 @@ public class ApiAutomationService {
if (StringUtils.isNotEmpty(item.getScenarioDefinition())) {
String jmx = generateJmx(item);
if (StringUtils.isNotEmpty(jmx)) {
ApiScenarioExportJmxDTO scenariosExportJmx = new ApiScenarioExportJmxDTO(item.getName(), apiTestService.updateJmxString(jmx, item.getProjectId()).getXml());
JmxInfoDTO dto = apiTestService.updateJmxString(jmx, item.getProjectId());
ApiScenarioExportJmxDTO scenariosExportJmx = new ApiScenarioExportJmxDTO(item.getName(), apiTestService.updateJmxString(jmx, item.getProjectId(), false).getXml());
JmxInfoDTO dto = apiTestService.updateJmxString(jmx, item.getProjectId(), true);
scenariosExportJmx.setId(item.getId());
scenariosExportJmx.setVersion(item.getVersion());
//扫描需要哪些文件
@ -1541,7 +1541,7 @@ public class ApiAutomationService {
if (StringUtils.isNotEmpty(item.getScenarioDefinition())) {
String jmx = generateJmx(item);
if (StringUtils.isNotEmpty(jmx)) {
ApiScenarioExportJmxDTO scenariosExportJmx = new ApiScenarioExportJmxDTO(item.getName(), apiTestService.updateJmxString(jmx, item.getProjectId()).getXml());
ApiScenarioExportJmxDTO scenariosExportJmx = new ApiScenarioExportJmxDTO(item.getName(), apiTestService.updateJmxString(jmx, item.getProjectId(), false).getXml());
String fileName = item.getName() + ".jmx";
String jmxStr = scenariosExportJmx.getJmx();
files.put(fileName, jmxStr.getBytes(StandardCharsets.UTF_8));
@ -1820,7 +1820,7 @@ public class ApiAutomationService {
apiScenarioList.forEach(item -> {
MsTestPlan testPlan = new MsTestPlan();
testPlan.setHashTree(new LinkedList<>());
JmxInfoDTO dto = apiTestService.updateJmxString(generateJmx(item), item.getProjectId());
JmxInfoDTO dto = apiTestService.updateJmxString(generateJmx(item), item.getProjectId(), true);
String name = item.getName() + ".jmx";
dto.setId(item.getId());
dto.setName(name);