fix(测试跟踪): 修复不同工作空间下相同项目名的情况下测试报告展示运行环境出错的问题

修复不同工作空间下相同项目名的情况下测试报告展示运行环境出错的问题
This commit is contained in:
song-tianyang 2023-03-27 11:28:19 +08:00 committed by fit2-zhao
parent d150ef0e90
commit 8e705e8463
2 changed files with 35 additions and 6 deletions

View File

@ -32,13 +32,13 @@ import io.metersphere.environment.service.BaseEnvironmentService;
import io.metersphere.i18n.Translator; import io.metersphere.i18n.Translator;
import io.metersphere.plugin.core.MsTestElement; import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.definition.ApiDefinitionService; import io.metersphere.service.definition.ApiDefinitionService;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject; import org.json.JSONObject;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -485,9 +485,18 @@ public class ApiScenarioEnvService {
String projectName = this.selectNameById(projectId); String projectName = this.selectNameById(projectId);
List<String> envNameList = apiTestEnvironmentService.selectNameByIds(envIdList); List<String> envNameList = apiTestEnvironmentService.selectNameByIds(envIdList);
if (CollectionUtils.isNotEmpty(envNameList) && StringUtils.isNotEmpty(projectName)) { if (CollectionUtils.isNotEmpty(envNameList) && StringUtils.isNotEmpty(projectName)) {
returnMap.put(projectName, new ArrayList<>() {{ //考虑到存在不同工作空间下有相同名称的项目这里还是要检查一下项目名称是否已被记录
this.addAll(envNameList); if (returnMap.containsKey(projectName)) {
}}); envNameList.forEach(envName -> {
if (!returnMap.get(projectName).contains(envName)) {
returnMap.get(projectName).add(envName);
}
});
} else {
returnMap.put(projectName, new ArrayList<>() {{
this.addAll(envNameList);
}});
}
} }
} }
} }
@ -569,7 +578,7 @@ public class ApiScenarioEnvService {
String envId = entry.getValue(); String envId = entry.getValue();
String projectName = this.selectNameById(projectId); String projectName = this.selectNameById(projectId);
String envName = apiTestEnvironmentService.selectNameById(envId); String envName = apiTestEnvironmentService.selectNameById(envId);
if (StringUtils.isNoneEmpty(projectName, envName)) { if (StringUtils.isNoneEmpty(projectName, envName) && this.isProjectEnvMapNotContainsEnv(returnMap, projectName, envName)) {
returnMap.put(projectName, new ArrayList<>() {{ returnMap.put(projectName, new ArrayList<>() {{
this.add(envName); this.add(envName);
}}); }});
@ -581,6 +590,15 @@ public class ApiScenarioEnvService {
return returnMap; return returnMap;
} }
private boolean isProjectEnvMapNotContainsEnv(LinkedHashMap<String, List<String>> returnMap, String projectName, String envName) {
if (MapUtils.isNotEmpty(returnMap)) {
if (returnMap.containsKey(projectName) && CollectionUtils.isNotEmpty(returnMap.get(projectName)) && returnMap.get(projectName).contains(envName)) {
return false;
}
}
return true;
}
public String selectNameById(String projectId) { public String selectNameById(String projectId) {
Project project = projectMapper.selectByPrimaryKey(projectId); Project project = projectMapper.selectByPrimaryKey(projectId);
if (project == null) { if (project == null) {

View File

@ -1208,7 +1208,18 @@ public class TestPlanReportService {
String envName = apiTestEnvironmentService.selectNameById(envId); String envName = apiTestEnvironmentService.selectNameById(envId);
envNameList.add(envName); envNameList.add(envName);
} }
projectEnvMap.put(projectName, envNameList); //考虑到存在不同工作空间下有相同名称的项目这里还是要检查一下项目名称是否已被记录
if (projectEnvMap.containsKey(projectName)) {
envNameList.forEach(envName -> {
if (!projectEnvMap.get(projectName).contains(envName)) {
projectEnvMap.get(projectName).add(envName);
}
});
} else {
projectEnvMap.put(projectName, new ArrayList<>() {{
this.addAll(envNameList);
}});
}
} }
} }
if (MapUtils.isNotEmpty(projectEnvMap)) { if (MapUtils.isNotEmpty(projectEnvMap)) {