refactor(接口测试): 修改接口导入代码

This commit is contained in:
CaptainB 2022-01-25 18:57:01 +08:00 committed by song-tianyang
parent 4205161a14
commit 90d25c111c
3 changed files with 12 additions and 10 deletions

View File

@ -4,6 +4,7 @@ import io.metersphere.api.dto.definition.request.auth.MsAuthManager;
import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.KeyValue;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.List; import java.util.List;
@Setter @Setter
@ -25,6 +26,7 @@ public class ApiTestImportRequest {
private String userId; private String userId;
private String versionId; // 新导入选择的版本 private String versionId; // 新导入选择的版本
private String updateVersionId; // 覆盖导入已存在的接口选择的版本 private String updateVersionId; // 覆盖导入已存在的接口选择的版本
private String defaultVersion;
//调用类型 //调用类型
private String type; private String type;
// 是否开启自定义ID // 是否开启自定义ID

View File

@ -1222,7 +1222,6 @@ public class ApiAutomationService {
private void _importCreate(List<ApiScenarioWithBLOBs> sameRequest, ApiScenarioMapper batchMapper, ExtApiScenarioMapper extApiScenarioMapper, private void _importCreate(List<ApiScenarioWithBLOBs> sameRequest, ApiScenarioMapper batchMapper, ExtApiScenarioMapper extApiScenarioMapper,
ApiScenarioWithBLOBs scenarioWithBLOBs, ApiTestImportRequest apiTestImportRequest) { ApiScenarioWithBLOBs scenarioWithBLOBs, ApiTestImportRequest apiTestImportRequest) {
String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId());
if (CollectionUtils.isEmpty(sameRequest)) { if (CollectionUtils.isEmpty(sameRequest)) {
scenarioWithBLOBs.setId(UUID.randomUUID().toString()); scenarioWithBLOBs.setId(UUID.randomUUID().toString());
List<ApiMethodUrlDTO> useUrl = this.parseUrl(scenarioWithBLOBs); List<ApiMethodUrlDTO> useUrl = this.parseUrl(scenarioWithBLOBs);
@ -1233,7 +1232,7 @@ public class ApiAutomationService {
if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) { if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) {
scenarioWithBLOBs.setVersionId(apiTestImportRequest.getVersionId()); scenarioWithBLOBs.setVersionId(apiTestImportRequest.getVersionId());
} else { } else {
scenarioWithBLOBs.setVersionId(defaultVersion); scenarioWithBLOBs.setVersionId(apiTestImportRequest.getDefaultVersion());
} }
scenarioWithBLOBs.setLatest(true); scenarioWithBLOBs.setLatest(true);
batchMapper.insert(scenarioWithBLOBs); batchMapper.insert(scenarioWithBLOBs);
@ -1241,7 +1240,7 @@ public class ApiAutomationService {
} else { } else {
//如果存在则修改 //如果存在则修改
if (StringUtils.isEmpty(apiTestImportRequest.getUpdateVersionId())) { if (StringUtils.isEmpty(apiTestImportRequest.getUpdateVersionId())) {
apiTestImportRequest.setUpdateVersionId(defaultVersion); apiTestImportRequest.setUpdateVersionId(apiTestImportRequest.getDefaultVersion());
} }
Optional<ApiScenarioWithBLOBs> scenarioOp = sameRequest.stream() Optional<ApiScenarioWithBLOBs> scenarioOp = sameRequest.stream()
.filter(api -> StringUtils.equals(api.getVersionId(), apiTestImportRequest.getUpdateVersionId())) .filter(api -> StringUtils.equals(api.getVersionId(), apiTestImportRequest.getUpdateVersionId()))
@ -1333,8 +1332,7 @@ public class ApiAutomationService {
if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) { if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) {
scenarioWithBLOBs.setVersionId(apiTestImportRequest.getVersionId()); scenarioWithBLOBs.setVersionId(apiTestImportRequest.getVersionId());
} else { } else {
String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId()); scenarioWithBLOBs.setVersionId(apiTestImportRequest.getDefaultVersion());
scenarioWithBLOBs.setVersionId(defaultVersion);
} }
scenarioWithBLOBs.setLatest(true); scenarioWithBLOBs.setLatest(true);
batchMapper.insert(scenarioWithBLOBs); batchMapper.insert(scenarioWithBLOBs);
@ -1366,6 +1364,8 @@ public class ApiAutomationService {
num = getNextNum(data.get(0).getProjectId()); num = getNextNum(data.get(0).getProjectId());
request.setOpenCustomNum(project.getScenarioCustomNum()); request.setOpenCustomNum(project.getScenarioCustomNum());
} }
String defaultVersion = extProjectVersionMapper.getDefaultVersion(request.getProjectId());
request.setDefaultVersion(defaultVersion);
for (int i = 0; i < data.size(); i++) { for (int i = 0; i < data.size(); i++) {
ApiScenarioWithBLOBs item = data.get(i); ApiScenarioWithBLOBs item = data.get(i);
if (item.getName().length() > 255) { if (item.getName().length() > 255) {

View File

@ -752,8 +752,7 @@ public class ApiDefinitionService {
if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) { if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) {
apiDefinition.setVersionId(apiTestImportRequest.getVersionId()); apiDefinition.setVersionId(apiTestImportRequest.getVersionId());
} else { } else {
String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId()); apiDefinition.setVersionId(apiTestImportRequest.getDefaultVersion());
apiDefinition.setVersionId(defaultVersion);
} }
apiDefinition.setLatest(true); // 新增的接口 latest = true apiDefinition.setLatest(true); // 新增的接口 latest = true
batchMapper.insert(apiDefinition); batchMapper.insert(apiDefinition);
@ -802,7 +801,6 @@ public class ApiDefinitionService {
ApiTestCaseMapper apiTestCaseMapper, ExtApiDefinitionMapper extApiDefinitionMapper, ApiTestCaseMapper apiTestCaseMapper, ExtApiDefinitionMapper extApiDefinitionMapper,
ApiTestImportRequest apiTestImportRequest, List<ApiTestCaseWithBLOBs> cases, List<MockConfigImportDTO> mocks) { ApiTestImportRequest apiTestImportRequest, List<ApiTestCaseWithBLOBs> cases, List<MockConfigImportDTO> mocks) {
String originId = apiDefinition.getId(); String originId = apiDefinition.getId();
String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId());
if (CollectionUtils.isEmpty(sameRequest)) { // 没有这个接口 新增 if (CollectionUtils.isEmpty(sameRequest)) { // 没有这个接口 新增
apiDefinition.setId(UUID.randomUUID().toString()); apiDefinition.setId(UUID.randomUUID().toString());
@ -810,7 +808,7 @@ public class ApiDefinitionService {
if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) { if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) {
apiDefinition.setVersionId(apiTestImportRequest.getVersionId()); apiDefinition.setVersionId(apiTestImportRequest.getVersionId());
} else { } else {
apiDefinition.setVersionId(defaultVersion); apiDefinition.setVersionId(apiTestImportRequest.getDefaultVersion());
} }
apiDefinition.setLatest(true); // 新增接口 latest = true apiDefinition.setLatest(true); // 新增接口 latest = true
// case 设置版本 // case 设置版本
@ -834,7 +832,7 @@ public class ApiDefinitionService {
} else { //如果存在则修改 } else { //如果存在则修改
if (StringUtils.isEmpty(apiTestImportRequest.getUpdateVersionId())) { if (StringUtils.isEmpty(apiTestImportRequest.getUpdateVersionId())) {
apiTestImportRequest.setUpdateVersionId(defaultVersion); apiTestImportRequest.setUpdateVersionId(apiTestImportRequest.getDefaultVersion());
} }
Optional<ApiDefinition> apiOp = sameRequest.stream() Optional<ApiDefinition> apiOp = sameRequest.stream()
.filter(api -> StringUtils.equals(api.getVersionId(), apiTestImportRequest.getUpdateVersionId())) .filter(api -> StringUtils.equals(api.getVersionId(), apiTestImportRequest.getUpdateVersionId()))
@ -1175,6 +1173,8 @@ public class ApiDefinitionService {
if (!CollectionUtils.isEmpty(data) && data.get(0) != null && data.get(0).getProjectId() != null) { if (!CollectionUtils.isEmpty(data) && data.get(0) != null && data.get(0).getProjectId() != null) {
num = getNextNum(data.get(0).getProjectId()); num = getNextNum(data.get(0).getProjectId());
} }
String defaultVersion = extProjectVersionMapper.getDefaultVersion(request.getProjectId());
request.setDefaultVersion(defaultVersion);
for (int i = 0; i < data.size(); i++) { for (int i = 0; i < data.size(); i++) {
ApiDefinitionWithBLOBs item = data.get(i); ApiDefinitionWithBLOBs item = data.get(i);
this.setModule(item); this.setModule(item);