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

This commit is contained in:
CaptainB 2022-01-25 17:46:34 +08:00 committed by jianxing
parent 5d6a400e79
commit 776448ac26
2 changed files with 28 additions and 24 deletions

View File

@ -1220,7 +1220,8 @@ public class ApiAutomationService {
return apiScenarioMapper.selectByExampleWithBLOBs(example); return apiScenarioMapper.selectByExampleWithBLOBs(example);
} }
private void _importCreate(List<ApiScenarioWithBLOBs> sameRequest, ApiScenarioMapper batchMapper, ApiScenarioWithBLOBs scenarioWithBLOBs, ApiTestImportRequest apiTestImportRequest) { private void _importCreate(List<ApiScenarioWithBLOBs> sameRequest, ApiScenarioMapper batchMapper, ExtApiScenarioMapper extApiScenarioMapper,
ApiScenarioWithBLOBs scenarioWithBLOBs, ApiTestImportRequest apiTestImportRequest) {
String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId()); String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId());
if (CollectionUtils.isEmpty(sameRequest)) { if (CollectionUtils.isEmpty(sameRequest)) {
scenarioWithBLOBs.setId(UUID.randomUUID().toString()); scenarioWithBLOBs.setId(UUID.randomUUID().toString());
@ -1234,6 +1235,7 @@ public class ApiAutomationService {
} else { } else {
scenarioWithBLOBs.setVersionId(defaultVersion); scenarioWithBLOBs.setVersionId(defaultVersion);
} }
scenarioWithBLOBs.setLatest(true);
batchMapper.insert(scenarioWithBLOBs); batchMapper.insert(scenarioWithBLOBs);
apiScenarioReferenceIdService.saveByApiScenario(scenarioWithBLOBs); apiScenarioReferenceIdService.saveByApiScenario(scenarioWithBLOBs);
} else { } else {
@ -1265,10 +1267,13 @@ public class ApiAutomationService {
batchMapper.updateByPrimaryKeyWithBLOBs(scenarioWithBLOBs); batchMapper.updateByPrimaryKeyWithBLOBs(scenarioWithBLOBs);
} }
apiScenarioReferenceIdService.saveByApiScenario(scenarioWithBLOBs); apiScenarioReferenceIdService.saveByApiScenario(scenarioWithBLOBs);
extApiScenarioMapper.clearLatestVersion(scenarioWithBLOBs.getRefId());
extApiScenarioMapper.addLatestVersion(scenarioWithBLOBs.getRefId());
} }
} }
private ApiScenarioWithBLOBs importCreate(ApiScenarioWithBLOBs request, ApiScenarioMapper batchMapper, ApiTestImportRequest apiTestImportRequest) { private ApiScenarioWithBLOBs importCreate(ApiScenarioWithBLOBs request, ApiScenarioMapper batchMapper, ExtApiScenarioMapper extApiScenarioMapper,
ApiTestImportRequest apiTestImportRequest) {
final ApiScenarioWithBLOBs scenarioWithBLOBs = new ApiScenarioWithBLOBs(); final ApiScenarioWithBLOBs scenarioWithBLOBs = new ApiScenarioWithBLOBs();
BeanUtils.copyBean(scenarioWithBLOBs, request); BeanUtils.copyBean(scenarioWithBLOBs, request);
scenarioWithBLOBs.setCreateTime(System.currentTimeMillis()); scenarioWithBLOBs.setCreateTime(System.currentTimeMillis());
@ -1317,7 +1322,7 @@ public class ApiAutomationService {
} }
if (StringUtils.equals("fullCoverage", apiTestImportRequest.getModeId())) { if (StringUtils.equals("fullCoverage", apiTestImportRequest.getModeId())) {
_importCreate(sameRequest, batchMapper, scenarioWithBLOBs, apiTestImportRequest); _importCreate(sameRequest, batchMapper, extApiScenarioMapper, scenarioWithBLOBs, apiTestImportRequest);
} else if (StringUtils.equals("incrementalMerge", apiTestImportRequest.getModeId())) { } else if (StringUtils.equals("incrementalMerge", apiTestImportRequest.getModeId())) {
if (CollectionUtils.isEmpty(sameRequest)) { if (CollectionUtils.isEmpty(sameRequest)) {
List<ApiMethodUrlDTO> useUrl = this.parseUrl(scenarioWithBLOBs); List<ApiMethodUrlDTO> useUrl = this.parseUrl(scenarioWithBLOBs);
@ -1331,6 +1336,7 @@ public class ApiAutomationService {
String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId()); String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId());
scenarioWithBLOBs.setVersionId(defaultVersion); scenarioWithBLOBs.setVersionId(defaultVersion);
} }
scenarioWithBLOBs.setLatest(true);
batchMapper.insert(scenarioWithBLOBs); batchMapper.insert(scenarioWithBLOBs);
// 存储依赖关系 // 存储依赖关系
@ -1342,7 +1348,7 @@ public class ApiAutomationService {
} }
} else { } else {
_importCreate(sameRequest, batchMapper, scenarioWithBLOBs, apiTestImportRequest); _importCreate(sameRequest, batchMapper, extApiScenarioMapper, scenarioWithBLOBs, apiTestImportRequest);
} }
return scenarioWithBLOBs; return scenarioWithBLOBs;
} }
@ -1379,9 +1385,7 @@ public class ApiAutomationService {
item.setId(UUID.randomUUID().toString()); item.setId(UUID.randomUUID().toString());
} }
// 导入之后刷新latest // 导入之后刷新latest
ApiScenarioWithBLOBs result = importCreate(item, batchMapper, request); importCreate(item, batchMapper, extApiScenarioMapper, request);
extApiScenarioMapper.clearLatestVersion(result.getRefId());
extApiScenarioMapper.addLatestVersion(result.getRefId());
if (i % 300 == 0) { if (i % 300 == 0) {
sqlSession.flushStatements(); sqlSession.flushStatements();
} }

View File

@ -713,7 +713,8 @@ public class ApiDefinitionService {
} }
private ApiDefinition importCreate(ApiDefinitionWithBLOBs apiDefinition, ApiDefinitionMapper batchMapper, private ApiDefinition importCreate(ApiDefinitionWithBLOBs apiDefinition, ApiDefinitionMapper batchMapper,
ApiTestCaseMapper apiTestCaseMapper, ApiTestImportRequest apiTestImportRequest, List<ApiTestCaseWithBLOBs> cases, List<MockConfigImportDTO> mocks, ApiTestCaseMapper apiTestCaseMapper, ExtApiDefinitionMapper extApiDefinitionMapper,
ApiTestImportRequest apiTestImportRequest, List<ApiTestCaseWithBLOBs> cases, List<MockConfigImportDTO> mocks,
Boolean repeatable) { Boolean repeatable) {
SaveApiDefinitionRequest saveReq = new SaveApiDefinitionRequest(); SaveApiDefinitionRequest saveReq = new SaveApiDefinitionRequest();
BeanUtils.copyBean(saveReq, apiDefinition); BeanUtils.copyBean(saveReq, apiDefinition);
@ -740,7 +741,7 @@ public class ApiDefinitionService {
sameRequest = getSameRequestById(apiDefinition.getId(), apiTestImportRequest.getProjectId()); sameRequest = getSameRequestById(apiDefinition.getId(), apiTestImportRequest.getProjectId());
} }
if (StringUtils.equals("fullCoverage", apiTestImportRequest.getModeId())) { if (StringUtils.equals("fullCoverage", apiTestImportRequest.getModeId())) {
_importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, apiTestImportRequest, cases, mocks); _importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, extApiDefinitionMapper, apiTestImportRequest, cases, mocks);
} else if (StringUtils.equals("incrementalMerge", apiTestImportRequest.getModeId())) { } else if (StringUtils.equals("incrementalMerge", apiTestImportRequest.getModeId())) {
if (CollectionUtils.isEmpty(sameRequest)) { if (CollectionUtils.isEmpty(sameRequest)) {
//postman 可能含有前置脚本接口定义去掉脚本 //postman 可能含有前置脚本接口定义去掉脚本
@ -754,6 +755,7 @@ public class ApiDefinitionService {
String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId()); String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId());
apiDefinition.setVersionId(defaultVersion); apiDefinition.setVersionId(defaultVersion);
} }
apiDefinition.setLatest(true); // 新增的接口 latest = true
batchMapper.insert(apiDefinition); batchMapper.insert(apiDefinition);
String requestStr = setImportHashTree(apiDefinition); String requestStr = setImportHashTree(apiDefinition);
@ -770,7 +772,7 @@ public class ApiDefinitionService {
mocks.clear(); mocks.clear();
} }
} else { } else {
_importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, apiTestImportRequest, cases, mocks); _importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, extApiDefinitionMapper, apiTestImportRequest, cases, mocks);
} }
return apiDefinition; return apiDefinition;
@ -797,10 +799,12 @@ public class ApiDefinitionService {
} }
private void _importCreate(List<ApiDefinition> sameRequest, ApiDefinitionMapper batchMapper, ApiDefinitionWithBLOBs apiDefinition, private void _importCreate(List<ApiDefinition> sameRequest, ApiDefinitionMapper batchMapper, ApiDefinitionWithBLOBs apiDefinition,
ApiTestCaseMapper apiTestCaseMapper, ApiTestImportRequest apiTestImportRequest, List<ApiTestCaseWithBLOBs> cases, List<MockConfigImportDTO> mocks) { ApiTestCaseMapper apiTestCaseMapper, ExtApiDefinitionMapper extApiDefinitionMapper,
ApiTestImportRequest apiTestImportRequest, List<ApiTestCaseWithBLOBs> cases, List<MockConfigImportDTO> mocks) {
String originId = apiDefinition.getId(); String originId = apiDefinition.getId();
String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId()); String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId());
if (CollectionUtils.isEmpty(sameRequest)) {
if (CollectionUtils.isEmpty(sameRequest)) { // 没有这个接口 新增
apiDefinition.setId(UUID.randomUUID().toString()); apiDefinition.setId(UUID.randomUUID().toString());
apiDefinition.setRefId(apiDefinition.getId()); apiDefinition.setRefId(apiDefinition.getId());
if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) { if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) {
@ -808,6 +812,7 @@ public class ApiDefinitionService {
} else { } else {
apiDefinition.setVersionId(defaultVersion); apiDefinition.setVersionId(defaultVersion);
} }
apiDefinition.setLatest(true); // 新增接口 latest = true
// case 设置版本 // case 设置版本
cases.forEach(c -> { cases.forEach(c -> {
c.setVersionId(apiDefinition.getVersionId()); c.setVersionId(apiDefinition.getVersionId());
@ -827,8 +832,7 @@ public class ApiDefinitionService {
batchMapper.insert(apiDefinition); batchMapper.insert(apiDefinition);
} }
} else { } else { //如果存在则修改
//如果存在则修改
if (StringUtils.isEmpty(apiTestImportRequest.getUpdateVersionId())) { if (StringUtils.isEmpty(apiTestImportRequest.getUpdateVersionId())) {
apiTestImportRequest.setUpdateVersionId(defaultVersion); apiTestImportRequest.setUpdateVersionId(defaultVersion);
} }
@ -881,8 +885,8 @@ public class ApiDefinitionService {
apiDefinitionMapper.updateByPrimaryKeyWithBLOBs(apiDefinition); apiDefinitionMapper.updateByPrimaryKeyWithBLOBs(apiDefinition);
} }
} }
extApiDefinitionMapper.clearLatestVersion(apiDefinition.getRefId());
extApiDefinitionMapper.addLatestVersion(apiDefinition.getRefId());
} }
} }
@ -1101,7 +1105,7 @@ public class ApiDefinitionService {
ApiDefinitionImport apiImport = null; ApiDefinitionImport apiImport = null;
try { try {
apiImport = (ApiDefinitionImport) Objects.requireNonNull(runService).parse(file == null ? null : file.getInputStream(), request); apiImport = (ApiDefinitionImport) Objects.requireNonNull(runService).parse(file == null ? null : file.getInputStream(), request);
if(apiImport.getMocks() == null){ if (apiImport.getMocks() == null) {
apiImport.setMocks(new ArrayList<>()); apiImport.setMocks(new ArrayList<>());
} }
} catch (Exception e) { } catch (Exception e) {
@ -1173,7 +1177,6 @@ public class ApiDefinitionService {
} }
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);
ApiDefinition result;
this.setModule(item); this.setModule(item);
if (item.getName().length() > 255) { if (item.getName().length() > 255) {
item.setName(item.getName().substring(0, 255)); item.setName(item.getName().substring(0, 255));
@ -1184,23 +1187,20 @@ public class ApiDefinitionService {
String apiId = item.getId(); String apiId = item.getId();
EsbApiParamsWithBLOBs model = apiImport.getEsbApiParamsMap().get(apiId); EsbApiParamsWithBLOBs model = apiImport.getEsbApiParamsMap().get(apiId);
request.setModeId("fullCoverage");//标准版ESB数据导入不区分是否覆盖默认都为覆盖 request.setModeId("fullCoverage");//标准版ESB数据导入不区分是否覆盖默认都为覆盖
result = importCreate(item, batchMapper, apiTestCaseMapper, request, apiImport.getCases(), apiImport.getMocks(), project.getRepeatable()); importCreate(item, batchMapper, apiTestCaseMapper, extApiDefinitionMapper, request, apiImport.getCases(), apiImport.getMocks(), project.getRepeatable());
if (model != null) { if (model != null) {
apiImport.getEsbApiParamsMap().remove(apiId); apiImport.getEsbApiParamsMap().remove(apiId);
model.setResourceId(item.getId()); model.setResourceId(item.getId());
apiImport.getEsbApiParamsMap().put(item.getId(), model); apiImport.getEsbApiParamsMap().put(item.getId(), model);
} }
} else { } else {
result = importCreate(item, batchMapper, apiTestCaseMapper, request, apiImport.getCases(), apiImport.getMocks(), project.getRepeatable()); importCreate(item, batchMapper, apiTestCaseMapper, extApiDefinitionMapper, request, apiImport.getCases(), apiImport.getMocks(), project.getRepeatable());
} }
// 导入之后刷新latest
extApiDefinitionMapper.clearLatestVersion(result.getRefId());
extApiDefinitionMapper.addLatestVersion(result.getRefId());
if (i % 300 == 0) { if (i % 300 == 0) {
sqlSession.flushStatements(); sqlSession.flushStatements();
} }
sqlSession.flushStatements();
} }
sqlSession.flushStatements();
//判断EsbData是否需要存储 //判断EsbData是否需要存储
if (apiImport.getEsbApiParamsMap() != null && apiImport.getEsbApiParamsMap().size() > 0) { if (apiImport.getEsbApiParamsMap() != null && apiImport.getEsbApiParamsMap().size() > 0) {
EsbApiParamsMapper esbApiParamsMapper = sqlSession.getMapper(EsbApiParamsMapper.class); EsbApiParamsMapper esbApiParamsMapper = sqlSession.getMapper(EsbApiParamsMapper.class);