fix(接口测试): Mock 中对自定义 jar 加载问题修复

This commit is contained in:
fit2-zhao 2024-12-09 18:25:36 +08:00 committed by Craftsman
parent 2cc965f516
commit 0bb4966f4b
1 changed files with 54 additions and 52 deletions

View File

@ -21,65 +21,68 @@ import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class NewDriverManager { public class NewDriverManager {
// 获取项目的 Jar 配置
public static Map<String, List<ProjectJarConfig>> getJars(List<String> projectIds, BooleanPool pool) { public static Map<String, List<ProjectJarConfig>> getJars(List<String> projectIds, BooleanPool pool) {
FileMetadataExample fileMetadata = new FileMetadataExample(); // 获取 FileMetadataMapper FileMetadataService Bean
fileMetadata.createCriteria().andProjectIdIn(projectIds).andLoadJarEqualTo(true);
FileMetadataMapper fileMetadataMapper = CommonBeanFactory.getBean(FileMetadataMapper.class); FileMetadataMapper fileMetadataMapper = CommonBeanFactory.getBean(FileMetadataMapper.class);
List<FileMetadataWithBLOBs> files = fileMetadataMapper.selectByExampleWithBLOBs(fileMetadata); FileMetadataService fileMetadataService = CommonBeanFactory.getBean(FileMetadataService.class);
// 查询文件元数据
FileMetadataExample fileMetadataExample = new FileMetadataExample();
fileMetadataExample.createCriteria().andProjectIdIn(projectIds).andLoadJarEqualTo(true);
List<FileMetadataWithBLOBs> files = fileMetadataMapper.selectByExampleWithBLOBs(fileMetadataExample);
// 分组并映射为 ProjectJarConfig
Map<String, List<ProjectJarConfig>> jarConfigMap = files.stream() Map<String, List<ProjectJarConfig>> jarConfigMap = files.stream()
.collect(Collectors.groupingBy(FileMetadata::getProjectId, Collectors.mapping( .collect(Collectors.groupingBy(FileMetadata::getProjectId, Collectors.mapping(item -> {
item -> { ProjectJarConfig config = new ProjectJarConfig();
ProjectJarConfig configs = new ProjectJarConfig(); config.setId(item.getId());
configs.setId(item.getId()); config.setName(item.getName());
configs.setName(item.getName()); config.setStorage(item.getStorage());
configs.setStorage(item.getStorage()); config.setHasFile(StringUtils.isEmpty(item.getStorage()) && StringUtils.isEmpty(item.getResourceType()));
//历史数据(存在数据库) config.setUpdateTime(item.getUpdateTime());
if (StringUtils.isEmpty(item.getStorage()) && StringUtils.isEmpty(item.getResourceType())) {
configs.setHasFile(true); if (StringUtils.equals(item.getStorage(), StorageConstants.GIT.name())) {
} else { config.setAttachInfo(item.getAttachInfo());
configs.setHasFile(false); }
} return config;
configs.setUpdateTime(item.getUpdateTime()); }, Collectors.toList())));
if (StringUtils.isNotEmpty(item.getStorage()) && StringUtils.equals(item.getStorage(), StorageConstants.GIT.name())) {
configs.setAttachInfo(item.getAttachInfo()); // 如果不使用池或 K8s下载需要的 JAR 文件
}
return configs;
}, Collectors.toList())));
if (!pool.isPool() && !pool.isK8s()) { if (!pool.isPool() && !pool.isK8s()) {
//获取本地 // 获取本地需要下载的 JAR 文件配置
//获取需要下载的jar的map
Map<String, List<ProjectJarConfig>> map = JarConfigUtils.getJarConfigs(projectIds, jarConfigMap); Map<String, List<ProjectJarConfig>> map = JarConfigUtils.getJarConfigs(projectIds, jarConfigMap);
if (MapUtils.isNotEmpty(map)) { if (MapUtils.isNotEmpty(map)) {
//获取文件内容
List<String> loaderProjectIds = new ArrayList<>(); List<String> loaderProjectIds = new ArrayList<>();
FileMetadataService fileMetadataService = CommonBeanFactory.getBean(FileMetadataService.class);
map.forEach((key, value) -> { map.forEach((key, value) -> {
loaderProjectIds.add(key); loaderProjectIds.add(key);
if (CollectionUtils.isNotEmpty(value)) { if (CollectionUtils.isNotEmpty(value)) {
//历史数据 // 获取历史数据
value.stream().distinct().filter(s -> s.isHasFile()).forEach(s -> { value.stream()
//获取文件内容 .distinct()
byte[] bytes = new byte[0]; .filter(ProjectJarConfig::isHasFile)
// 兼容历史数据 .forEach(s -> {
bytes = fileMetadataService.getContent(s.getId()); // 获取文件内容并保存
ApiFileUtil.createFile(StringUtils.join(LocalPathUtil.JAR_PATH, byte[] bytes = fileMetadataService.getContent(s.getId());
File.separator, ApiFileUtil.createFile(
key, StringUtils.join(LocalPathUtil.JAR_PATH, File.separator, key, File.separator,
File.separator, s.getId(), File.separator, s.getUpdateTime(), ".jar"), bytes);
s.getId(), });
File.separator,
String.valueOf(s.getUpdateTime()), ".jar"), bytes); // 获取需要下载的文件
}); List<String> jarIds = value.stream()
List<String> jarIds = value.stream().distinct().filter(s -> !s.isHasFile()).map(ProjectJarConfig::getId).collect(Collectors.toList()); .distinct()
.filter(config -> !config.isHasFile())
.map(ProjectJarConfig::getId)
.collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(jarIds)) { if (CollectionUtils.isNotEmpty(jarIds)) {
List<FileInfoDTO> fileInfoDTOS = fileMetadataService.downloadFileByIds(jarIds); List<FileInfoDTO> fileInfoDTOS = fileMetadataService.downloadFileByIds(jarIds);
ApiFileUtil.createFiles(fileInfoDTOS, key, value); ApiFileUtil.createFiles(fileInfoDTOS, key, value);
@ -91,29 +94,28 @@ public class NewDriverManager {
ProjectClassLoader.initClassLoader(loaderProjectIds); ProjectClassLoader.initClassLoader(loaderProjectIds);
} }
} }
return jarConfigMap; return jarConfigMap;
} }
// 加载自定义 JAR
public static Map<String, List<ProjectJarConfig>> loadJar(RunDefinitionRequest request, BooleanPool pool) { public static Map<String, List<ProjectJarConfig>> loadJar(RunDefinitionRequest request, BooleanPool pool) {
// 加载自定义JAR
MsTestPlan testPlan = (MsTestPlan) request.getTestElement(); MsTestPlan testPlan = (MsTestPlan) request.getTestElement();
List<String> projectIds = getProjectIds(request); List<String> projectIds = getProjectIds(request);
Map<String, List<ProjectJarConfig>> jars = getJars(projectIds, pool); Map<String, List<ProjectJarConfig>> jars = getJars(projectIds, pool);
testPlan.setProjectJarIds(projectIds); testPlan.setProjectJarIds(projectIds);
return jars; return jars;
} }
// 获取项目 ID 列表
public static List<String> getProjectIds(RunDefinitionRequest request) { public static List<String> getProjectIds(RunDefinitionRequest request) {
List<String> projectIds = new ArrayList<>(); Set<String> projectIds = new HashSet<>();
projectIds.add(request.getProjectId()); projectIds.add(request.getProjectId());
if (MapUtils.isNotEmpty(request.getEnvironmentMap())) { if (MapUtils.isNotEmpty(request.getEnvironmentMap())) {
request.getEnvironmentMap().forEach((k, v) -> { projectIds.addAll(request.getEnvironmentMap().keySet());
if (!projectIds.contains(k)) {
projectIds.add(k);
}
});
} }
return projectIds;
return new ArrayList<>(projectIds);
} }
} }