fix(接口测试): 场景执行选mock环境,执行出错

This commit is contained in:
AgAngle 2024-04-11 20:33:47 +08:00 committed by Craftsman
parent 05f4965813
commit 9c00d8d309
1 changed files with 40 additions and 33 deletions

View File

@ -201,33 +201,12 @@ public class EnvironmentService extends MoveNodeService {
environmentInfoDTO.setMock(environment.getMock()); environmentInfoDTO.setMock(environment.getMock());
BeanUtils.copyBean(environmentInfoDTO, environment); BeanUtils.copyBean(environmentInfoDTO, environment);
environmentInfoDTO.setConfig(getEnvironmentConfig(environmentId)); environmentInfoDTO.setConfig(getEnvironmentConfig(environmentId));
if (BooleanUtils.isTrue(environment.getMock())) {
SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class); SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class);
if (systemParameterService != null) {
BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo(); BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo();
String baseUrl = baseSystemConfigDTO.getUrl(); String baseUrl = baseSystemConfigDTO.getUrl();
if (StringUtils.isNotEmpty(baseUrl)) {
Project project = projectMapper.selectByPrimaryKey(environment.getProjectId()); handleMockEnv(baseUrl, environment, environmentInfoDTO);
String domain = baseUrl.replace(HTTP, StringUtils.EMPTY).replace(HTTPS, StringUtils.EMPTY);
String protocol = baseUrl.substring(0, baseUrl.indexOf(domain) -3 );
if (environmentInfoDTO.getConfig() != null && CollectionUtils.isNotEmpty(environmentInfoDTO.getConfig().getHttpConfig())) {
environmentInfoDTO.getConfig().getHttpConfig().getFirst().setId(IDGenerator.nextStr());
environmentInfoDTO.getConfig().getHttpConfig().getFirst().setProtocol(protocol);
environmentInfoDTO.getConfig().getHttpConfig().getFirst().setHostname(StringUtils.join(domain, MOCK_EVN_SOCKET, project.getNum()));
environmentInfoDTO.getConfig().getHttpConfig().getFirst().setUrl(StringUtils.join(baseUrl, MOCK_EVN_SOCKET, project.getNum()));
} else {
List<HttpConfig> httpConfigs = new ArrayList<>();
HttpConfig httpConfig = new HttpConfig();
httpConfig.setId(IDGenerator.nextStr());
httpConfig.setProtocol(protocol);
httpConfig.setHostname(StringUtils.join(domain, MOCK_EVN_SOCKET, project.getNum()));
httpConfig.setUrl(StringUtils.join(baseUrl, MOCK_EVN_SOCKET, project.getNum()));
httpConfigs.add(httpConfig);
environmentInfoDTO.getConfig().setHttpConfig(httpConfigs);
}
}
}
}
return environmentInfoDTO; return environmentInfoDTO;
} }
@ -466,17 +445,45 @@ public class EnvironmentService extends MoveNodeService {
environmentInfo.setConfig(JSON.parseObject(new String(environmentBlob.getConfig()), EnvironmentConfig.class)); environmentInfo.setConfig(JSON.parseObject(new String(environmentBlob.getConfig()), EnvironmentConfig.class));
} }
if (BooleanUtils.isTrue(environment.getMock())) { handleMockEnv(baseUrl, environment, environmentInfo, projectMap);
if (StringUtils.isNotEmpty(baseUrl)) {
Long projectNum = projectMap.get(environment.getProjectId()).getNum();
environmentInfo.getConfig().getHttpConfig().getFirst().setHostname(StringUtils.join(baseUrl, MOCK_EVN_SOCKET, projectNum));
}
}
environmentInfos.add(environmentInfo); environmentInfos.add(environmentInfo);
} }
return environmentInfos; return environmentInfos;
} }
private void handleMockEnv(String baseUrl, Environment environment, EnvironmentInfoDTO environmentInfo) {
if (BooleanUtils.isTrue(environment.getMock())) {
Project project = projectMapper.selectByPrimaryKey(environment.getProjectId());
handleMockEnv(baseUrl, environment, environmentInfo, Map.of(project.getId(), project));
}
}
private void handleMockEnv(String baseUrl, Environment environment, EnvironmentInfoDTO environmentInfo, Map<String, Project> projectMap) {
if (BooleanUtils.isTrue(environment.getMock())) {
if (StringUtils.isNotEmpty(baseUrl)) {
Project project = projectMap.get(environment.getProjectId());
String domain = baseUrl.replace(HTTP, StringUtils.EMPTY).replace(HTTPS, StringUtils.EMPTY);
String protocol = baseUrl.substring(0, baseUrl.indexOf(domain) - 3);
if (environmentInfo.getConfig() != null && CollectionUtils.isNotEmpty(environmentInfo.getConfig().getHttpConfig())) {
environmentInfo.getConfig().getHttpConfig().getFirst().setId(IDGenerator.nextStr());
environmentInfo.getConfig().getHttpConfig().getFirst().setProtocol(protocol);
environmentInfo.getConfig().getHttpConfig().getFirst().setHostname(StringUtils.join(domain, MOCK_EVN_SOCKET, project.getNum()));
environmentInfo.getConfig().getHttpConfig().getFirst().setUrl(StringUtils.join(baseUrl, MOCK_EVN_SOCKET, project.getNum()));
} else {
List<HttpConfig> httpConfigs = new ArrayList<>();
HttpConfig httpConfig = new HttpConfig();
httpConfig.setId(IDGenerator.nextStr());
httpConfig.setProtocol(protocol);
httpConfig.setHostname(StringUtils.join(domain, MOCK_EVN_SOCKET, project.getNum()));
httpConfig.setUrl(StringUtils.join(baseUrl, MOCK_EVN_SOCKET, project.getNum()));
httpConfigs.add(httpConfig);
environmentInfo.getConfig().setHttpConfig(httpConfigs);
}
}
}
}
private List<Project> getProjects(List<String> projectIds) { private List<Project> getProjects(List<String> projectIds) {
if (CollectionUtils.isEmpty(projectIds)) { if (CollectionUtils.isEmpty(projectIds)) {
return Collections.emptyList(); return Collections.emptyList();