fix(接口测试): 修复迁移后jar包不加载问题

--bug=1017914 --user=赵勇 【接口测试】接口自动化调用jar包提示异常 https://www.tapd.cn/55049933/s/1260190
This commit is contained in:
fit2-zhao 2022-10-13 11:03:17 +08:00 committed by 建国
parent 660df9a548
commit 7649f3c057
7 changed files with 48 additions and 110 deletions

View File

@ -2,22 +2,33 @@ package io.metersphere.api.jmeter;
import io.metersphere.api.dto.definition.RunDefinitionRequest; import io.metersphere.api.dto.definition.RunDefinitionRequest;
import io.metersphere.api.dto.definition.request.MsTestPlan; import io.metersphere.api.dto.definition.request.MsTestPlan;
import io.metersphere.base.domain.FileMetadata;
import io.metersphere.base.domain.FileMetadataExample;
import io.metersphere.base.mapper.FileMetadataMapper;
import io.metersphere.commons.utils.ApiFileUtil;
import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.dto.FileInfoDTO;
import io.metersphere.metadata.service.FileMetadataService; import io.metersphere.metadata.service.FileMetadataService;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public class NewDriverManager { public class NewDriverManager {
public static List<String> getJars(List<String> projectIds) { public static List<String> getJars(List<String> projectIds) {
FileMetadataService service = CommonBeanFactory.getBean(FileMetadataService.class); FileMetadataExample fileMetadata = new FileMetadataExample();
if (service!=null) { fileMetadata.createCriteria().andProjectIdIn(projectIds).andLoadJarEqualTo(true);
return service.getJar(projectIds); FileMetadataMapper fileMetadataMapper = CommonBeanFactory.getBean(FileMetadataMapper.class);
} else { List<FileMetadata> files = fileMetadataMapper.selectByExample(fileMetadata);
return new LinkedList<>(); files = files.stream().filter(s -> StringUtils.isNotEmpty(s.getPath())).collect(Collectors.toList());
} // 获取文件内容
FileMetadataService fileMetadataService = CommonBeanFactory.getBean(FileMetadataService.class);
List<FileInfoDTO> fileInfoDTOS = fileMetadataService.downloadFileByIds(files.stream().map(FileMetadata::getId).collect(Collectors.toList()));
ApiFileUtil.createFiles(fileInfoDTOS);
return files.stream().map(FileMetadata::getPath).collect(Collectors.toList());
} }
public static void loadJar(RunDefinitionRequest request) { public static void loadJar(RunDefinitionRequest request) {
@ -25,8 +36,8 @@ public class NewDriverManager {
MsTestPlan testPlan = (MsTestPlan) request.getTestElement(); MsTestPlan testPlan = (MsTestPlan) request.getTestElement();
List<String> projectIds = new ArrayList<>(); List<String> projectIds = new ArrayList<>();
projectIds.add(request.getProjectId()); projectIds.add(request.getProjectId());
if(MapUtils.isNotEmpty(request.getEnvironmentMap())){ if (MapUtils.isNotEmpty(request.getEnvironmentMap())) {
request.getEnvironmentMap().forEach((k,v) ->{ request.getEnvironmentMap().forEach((k, v) -> {
projectIds.add(k); projectIds.add(k);
}); });
} }

View File

@ -1,62 +1,13 @@
package io.metersphere.commons.utils; package io.metersphere.commons.utils;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.dto.FileInfoDTO;
import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.request.BodyFile; import io.metersphere.request.BodyFile;
import io.metersphere.base.domain.FileMetadata;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.config.CSVDataSet;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.protocol.http.util.HTTPFileArg;
import org.apache.jorphan.collections.HashTree;
import org.aspectj.util.FileUtil;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public class ApiFileUtil extends FileUtils { public class ApiFileUtil extends FileUtils {
public static void deleteBodyFiles(MsTestElement request) {
if (request != null && StringUtils.isNotBlank(request.getId())) {
String path = BODY_FILE_DIR + File.separator + request.getId();
File testDir = new File(path);
if (!testDir.exists()) {
return;
}
List<String> files = new ArrayList<>();
if (request != null && StringUtils.equalsIgnoreCase(request.getType(), HTTPSamplerProxy.class.getSimpleName())) {
MsHTTPSamplerProxy samplerProxy = (MsHTTPSamplerProxy) request;
Body body = samplerProxy.getBody();
if (body != null && !CollectionUtils.isEmpty(body.getKvs())) {
body.getKvs().stream().filter(KeyValue::isFile).forEach(keyValue -> {
files.addAll(keyValue.getFiles().stream().map(BodyFile::getName).collect(Collectors.toList()));
});
}
if (body != null && !CollectionUtils.isEmpty(body.getBinary())) {
body.getBinary().stream().filter(KeyValue::isFile).filter(KeyValue::isEnable).forEach(keyValue -> {
files.addAll(keyValue.getFiles().stream().map(BodyFile::getName).collect(Collectors.toList()));
});
}
}
File[] optFilesName = testDir.listFiles();
if (CollectionUtils.isNotEmpty(files)) {
for (File f : optFilesName) {
if (!files.contains(f.getName())) {
f.delete();
}
}
} else {
FileUtil.deleteContents(testDir);
testDir.delete();
}
}
}
public static String getFilePath(BodyFile file) { public static String getFilePath(BodyFile file) {
String type = StringUtils.isNotEmpty(file.getFileType()) ? file.getFileType().toLowerCase() : null; String type = StringUtils.isNotEmpty(file.getFileType()) ? file.getFileType().toLowerCase() : null;
String name = file.getName(); String name = file.getName();
@ -66,45 +17,9 @@ public class ApiFileUtil extends FileUtils {
return StringUtils.join(ApiFileUtil.BODY_FILE_DIR, File.separator, file.getProjectId(), File.separator, name); return StringUtils.join(ApiFileUtil.BODY_FILE_DIR, File.separator, file.getProjectId(), File.separator, name);
} }
public static String getFilePath(FileMetadata fileMetadata) { public static void createFiles(List<FileInfoDTO> infoDTOS) {
String type = StringUtils.isNotEmpty(fileMetadata.getType()) ? fileMetadata.getType().toLowerCase() : null; infoDTOS.forEach(item -> {
String name = fileMetadata.getName(); createFile(item.getPath(), item.getFileByte());
if (type != null && !name.endsWith(type)) { });
name = StringUtils.join(name, ".", type);
}
return StringUtils.join(ApiFileUtil.BODY_FILE_DIR, File.separator, fileMetadata.getProjectId(), File.separator, name);
}
/**
* 获取当前jmx 涉及到的文件
*
* @param tree
*/
public static void getFiles(HashTree tree, List<BodyFile> files) {
for (Object key : tree.keySet()) {
HashTree node = tree.get(key);
if (key instanceof HTTPSamplerProxy) {
HTTPSamplerProxy source = (HTTPSamplerProxy) key;
if (source != null && source.getHTTPFiles().length > 0) {
for (HTTPFileArg arg : source.getHTTPFiles()) {
BodyFile file = new BodyFile();
file.setId(arg.getParamName());
file.setName(arg.getPath());
files.add(file);
}
}
} else if (key instanceof CSVDataSet) {
CSVDataSet source = (CSVDataSet) key;
if (source != null && StringUtils.isNotEmpty(source.getPropertyAsString("filename"))) {
BodyFile file = new BodyFile();
file.setId(source.getPropertyAsString("filename"));
file.setName(source.getPropertyAsString("filename"));
files.add(file);
}
}
if (node != null) {
getFiles(node, files);
}
}
} }
} }

View File

@ -9,5 +9,6 @@ public class FileInfoDTO {
private String id; private String id;
private String fileName; private String fileName;
private String storage; private String storage;
private String path;
private byte[] fileByte; private byte[] fileByte;
} }

View File

@ -85,14 +85,18 @@ public class GitFileRepository implements FileRepository {
} }
GitRepositoryUtil repositoryUtils = new GitRepositoryUtil( GitRepositoryUtil repositoryUtils = new GitRepositoryUtil(
baseGitFileInfo.getRepositoryPath(), baseGitFileInfo.getUserName(), baseGitFileInfo.getToken()); baseGitFileInfo.getRepositoryPath(),
baseGitFileInfo.getUserName(), baseGitFileInfo.getToken());
Map<String, byte[]> fileByteMap = repositoryUtils.getFiles(repositoryRequestList); Map<String, byte[]> fileByteMap = repositoryUtils.getFiles(repositoryRequestList);
repositoryRequestList.forEach(repositoryFile -> { repositoryRequestList.forEach(repositoryFile -> {
if (fileByteMap.get(repositoryFile.getFileMetadataId()) != null) { if (fileByteMap.get(repositoryFile.getFileMetadataId()) != null) {
FileInfoDTO repositoryFileDTO = new FileInfoDTO( FileInfoDTO repositoryFileDTO = new FileInfoDTO(
repositoryFile.getFileMetadataId(), MetadataUtils.getFileNameByRemotePath(repositoryFile.getFilePath()), StorageConstants.GIT.name(), fileByteMap.get(repositoryFile.getFileMetadataId())); repositoryFile.getFileMetadataId(),
MetadataUtils.getFileNameByRemotePath(repositoryFile.getFilePath()),
StorageConstants.GIT.name(),
repositoryFile.getFilePath(),
fileByteMap.get(repositoryFile.getFileMetadataId()));
list.add(repositoryFileDTO); list.add(repositoryFileDTO);
} }
}); });

View File

@ -15,6 +15,7 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.*; import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Component @Component
public class LocalFileRepository implements FileRepository { public class LocalFileRepository implements FileRepository {
@ -108,14 +109,20 @@ public class LocalFileRepository implements FileRepository {
@Override @Override
public List<FileInfoDTO> getFileBatch(List<FileRequest> requestList) throws Exception { public List<FileInfoDTO> getFileBatch(List<FileRequest> requestList) throws Exception {
List<FileInfoDTO> list = new ArrayList<>();
if (CollectionUtils.isNotEmpty(requestList)) { if (CollectionUtils.isNotEmpty(requestList)) {
for (FileRequest fileRequest : requestList) { return requestList.stream().map(fileRequest -> {
FileInfoDTO fileInfoDTO = new FileInfoDTO(fileRequest.getResourceId(), fileRequest.getFileName(), fileRequest.getStorage(), this.getFile(fileRequest)); byte[] content = new byte[0];
list.add(fileInfoDTO); try {
} content = this.getFile(fileRequest);
} catch (Exception e) {
throw new RuntimeException(e);
}
return new FileInfoDTO(
fileRequest.getResourceId(), fileRequest.getFileName(),
fileRequest.getStorage(), fileRequest.getPath(), content);
}).collect(Collectors.toList());
} }
return list; return new ArrayList<>();
} }

View File

@ -85,7 +85,7 @@ public class MinIOFileRepository implements FileRepository {
List<FileInfoDTO> list = new ArrayList<>(); List<FileInfoDTO> list = new ArrayList<>();
if (CollectionUtils.isNotEmpty(requestList)) { if (CollectionUtils.isNotEmpty(requestList)) {
for (FileRequest fileRequest : requestList) { for (FileRequest fileRequest : requestList) {
FileInfoDTO fileInfoDTO = new FileInfoDTO(fileRequest.getResourceId(), fileRequest.getFileName(), fileRequest.getStorage(), this.getFile(fileRequest)); FileInfoDTO fileInfoDTO = new FileInfoDTO(fileRequest.getResourceId(), fileRequest.getFileName(), fileRequest.getStorage(), fileRequest.getPath(), this.getFile(fileRequest));
list.add(fileInfoDTO); list.add(fileInfoDTO);
} }
} }

View File

@ -103,7 +103,7 @@ public class FileManagerService {
try { try {
list.addAll(FileCenter.getRepository(requestByStorageEntry.getKey()).getFileBatch(requestByStorageEntry.getValue())); list.addAll(FileCenter.getRepository(requestByStorageEntry.getKey()).getFileBatch(requestByStorageEntry.getValue()));
} catch (Exception e) { } catch (Exception e) {
LogUtil.error("下载GIT文件失败!", e); LogUtil.error("下载文件失败!", e);
return list; return list;
} }
} }