feat(接口测试): 重构metersphere-tcp导入的逻辑

【【接口测试】接口导入-MeterSphere格式-TCP接口-当前项目下存在TCP接口-选择覆盖导入-报错格式错误】
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001046279
This commit is contained in:
Jianguo-Genius 2024-09-12 19:31:11 +08:00 committed by Craftsman
parent 790c6414f8
commit 2323194f28
8 changed files with 208 additions and 77 deletions

View File

@ -25,8 +25,8 @@ public class ApiImportDataAnalysisResult {
// 接口的mock数据 // 接口的mock数据
Map<String, List<ApiDefinitionMockDTO>> apiIdAndMockMap = new HashMap<>(); Map<String, List<ApiDefinitionMockDTO>> apiIdAndMockMap = new HashMap<>();
public void addExistenceApi(ApiDefinitionDetail importApi, ApiDefinitionDetail exportApi) { public void addExistenceApi(ApiDefinitionDetail importApi, List<ApiDefinitionDetail> exportApiList) {
this.existenceApiList.add(new ExistenceApiDefinitionDetail(importApi, exportApi)); this.existenceApiList.add(new ExistenceApiDefinitionDetail(importApi, exportApiList));
} }
public boolean isEmpty() { public boolean isEmpty() {

View File

@ -3,9 +3,11 @@ package io.metersphere.api.dto.converter;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import java.util.List;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
public class ExistenceApiDefinitionDetail { public class ExistenceApiDefinitionDetail {
private ApiDefinitionDetail importApiDefinition; private ApiDefinitionDetail importApiDefinition;
private ApiDefinitionDetail existenceApiDefinition; private List<ApiDefinitionDetail> existenceApiDefinition;
} }

View File

@ -64,8 +64,7 @@ public class HarParserApiDefinition extends HttpApiDefinitionImportAbstractParse
for (ExistenceApiDefinitionDetail definitionDetail : insertAndUpdateData.getExistenceApiList()) { for (ExistenceApiDefinitionDetail definitionDetail : insertAndUpdateData.getExistenceApiList()) {
ApiDefinitionDetail importApi = definitionDetail.getImportApiDefinition(); ApiDefinitionDetail importApi = definitionDetail.getImportApiDefinition();
ApiDefinitionDetail savedApi = definitionDetail.getExistenceApiDefinition(); ApiDefinitionDetail savedApi = definitionDetail.getExistenceApiDefinition().getFirst();
ApiDefinitionBlob blob = apiDefinitionBlobMapper.selectByPrimaryKey(savedApi.getId()); ApiDefinitionBlob blob = apiDefinitionBlobMapper.selectByPrimaryKey(savedApi.getId());
if (blob != null) { if (blob != null) {
if (blob.getRequest() != null) { if (blob.getRequest() != null) {

View File

@ -40,7 +40,9 @@ public abstract class HttpApiDefinitionImportAbstractParser<T> implements ApiDef
importDataMap.forEach((key, api) -> { importDataMap.forEach((key, api) -> {
if (savedApiDefinitionMap.containsKey(key)) { if (savedApiDefinitionMap.containsKey(key)) {
insertAndUpdateData.addExistenceApi(api, savedApiDefinitionMap.get(key)); insertAndUpdateData.addExistenceApi(api, new ArrayList<>() {{
this.add(savedApiDefinitionMap.get(key));
}});
} else { } else {
insertAndUpdateData.getInsertApiList().add(api); insertAndUpdateData.getInsertApiList().add(api);
} }

View File

@ -184,7 +184,9 @@ public class JmeterParserApiDefinition implements ApiDefinitionImportParser<ApiI
for (Map.Entry<String, ApiDefinitionDetail> entry : importDataMap.entrySet()) { for (Map.Entry<String, ApiDefinitionDetail> entry : importDataMap.entrySet()) {
if (savedApiDefinitionMap.containsKey(entry.getKey())) { if (savedApiDefinitionMap.containsKey(entry.getKey())) {
insertAndUpdateData.addExistenceApi(entry.getValue(), savedApiDefinitionMap.get(entry.getKey())); List<ApiDefinitionDetail> existenceList = new ArrayList<>();
existenceList.add(savedApiDefinitionMap.get(entry.getKey()));
insertAndUpdateData.addExistenceApi(entry.getValue(), existenceList);
} else { } else {
insertAndUpdateData.getInsertApiList().add(entry.getValue()); insertAndUpdateData.getInsertApiList().add(entry.getValue());
} }

View File

@ -6,7 +6,6 @@ import io.metersphere.api.dto.export.ApiExportResponse;
import io.metersphere.api.dto.export.MetersphereApiExportResponse; import io.metersphere.api.dto.export.MetersphereApiExportResponse;
import io.metersphere.api.dto.mockserver.MockMatchRule; import io.metersphere.api.dto.mockserver.MockMatchRule;
import io.metersphere.api.dto.mockserver.MockResponse; import io.metersphere.api.dto.mockserver.MockResponse;
import io.metersphere.api.dto.request.http.MsHTTPElement;
import io.metersphere.api.utils.ApiDataUtils; import io.metersphere.api.utils.ApiDataUtils;
import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.sdk.constants.ModuleConstants; import io.metersphere.sdk.constants.ModuleConstants;
@ -59,7 +58,7 @@ public class MetersphereExportParser {
dto.setStatus(apiTestCaseWithBlob.getStatus()); dto.setStatus(apiTestCaseWithBlob.getStatus());
dto.setLastReportStatus(apiTestCaseWithBlob.getLastReportStatus()); dto.setLastReportStatus(apiTestCaseWithBlob.getLastReportStatus());
dto.setTags(apiTestCaseWithBlob.getTags()); dto.setTags(apiTestCaseWithBlob.getTags());
dto.setRequest(ApiDataUtils.parseObject(new String(apiTestCaseWithBlob.getRequest()), MsHTTPElement.class)); dto.setRequest(ApiDataUtils.parseObject(new String(apiTestCaseWithBlob.getRequest()), AbstractMsTestElement.class));
detail.getApiTestCaseList().add(dto); detail.getApiTestCaseList().add(dto);
} }
} }

View File

@ -1,12 +1,16 @@
package io.metersphere.api.parser.api; package io.metersphere.api.parser.api;
import io.metersphere.api.domain.ApiDefinition;
import io.metersphere.api.dto.converter.ApiDefinitionDetail;
import io.metersphere.api.dto.converter.ApiDefinitionExportDetail; import io.metersphere.api.dto.converter.ApiDefinitionExportDetail;
import io.metersphere.api.dto.converter.ApiImportDataAnalysisResult;
import io.metersphere.api.dto.converter.ApiImportFileParseResult; import io.metersphere.api.dto.converter.ApiImportFileParseResult;
import io.metersphere.api.dto.definition.ApiDefinitionMockDTO; import io.metersphere.api.dto.definition.ApiDefinitionMockDTO;
import io.metersphere.api.dto.definition.ApiTestCaseDTO; import io.metersphere.api.dto.definition.ApiTestCaseDTO;
import io.metersphere.api.dto.export.MetersphereApiExportResponse; import io.metersphere.api.dto.export.MetersphereApiExportResponse;
import io.metersphere.api.dto.request.ImportRequest; import io.metersphere.api.dto.request.ImportRequest;
import io.metersphere.api.parser.ApiDefinitionImportParser;
import io.metersphere.api.utils.ApiDataUtils; import io.metersphere.api.utils.ApiDataUtils;
import io.metersphere.api.utils.ApiDefinitionImportUtils; import io.metersphere.api.utils.ApiDefinitionImportUtils;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
@ -20,8 +24,9 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
public class MetersphereParserApiDefinition extends HttpApiDefinitionImportAbstractParser<ApiImportFileParseResult> { public class MetersphereParserApiDefinition implements ApiDefinitionImportParser<ApiImportFileParseResult> {
@Override @Override
public ApiImportFileParseResult parse(InputStream source, ImportRequest request) throws Exception { public ApiImportFileParseResult parse(InputStream source, ImportRequest request) throws Exception {
@ -38,6 +43,82 @@ public class MetersphereParserApiDefinition extends HttpApiDefinitionImportAbstr
return this.genApiDefinitionImport(metersphereApiExportResponse.getApiDefinitions()); return this.genApiDefinitionImport(metersphereApiExportResponse.getApiDefinitions());
} }
@Override
public ApiImportDataAnalysisResult generateInsertAndUpdateData(ApiImportFileParseResult importParser, List<ApiDefinitionDetail> existenceAllApiList) {
Map<String, List<ApiDefinitionDetail>> existenceProtocolMap = new HashMap<>();
ApiImportDataAnalysisResult insertAndUpdateData = new ApiImportDataAnalysisResult();
for (ApiDefinitionDetail apiDefinitionDetail : existenceAllApiList) {
String protocol = apiDefinitionDetail.getProtocol().toUpperCase();
if (existenceProtocolMap.containsKey(protocol)) {
existenceProtocolMap.get(protocol).add(apiDefinitionDetail);
} else {
existenceProtocolMap.put(protocol, new ArrayList<ApiDefinitionDetail>() {{
this.add(apiDefinitionDetail);
}});
}
}
Map<String, List<ApiDefinitionDetail>> importProtocolMap = new HashMap<>();
for (ApiDefinitionDetail apiDefinitionDetail : importParser.getData()) {
String protocol = apiDefinitionDetail.getProtocol().toUpperCase();
if (importProtocolMap.containsKey(protocol)) {
importProtocolMap.get(protocol).add(apiDefinitionDetail);
} else {
importProtocolMap.put(protocol, new ArrayList<ApiDefinitionDetail>() {{
this.add(apiDefinitionDetail);
}});
}
}
for (Map.Entry<String, List<ApiDefinitionDetail>> entry : importProtocolMap.entrySet()) {
String protocol = entry.getKey();
List<ApiDefinitionDetail> importDetail = entry.getValue();
List<ApiDefinitionDetail> existenceApiDefinitionList = existenceProtocolMap.containsKey(protocol) ? existenceProtocolMap.get(protocol) : new ArrayList<>();
if (StringUtils.equalsIgnoreCase(protocol, "HTTP")) {
//HTTP类型通过 Method & Path 组合判断接口是否存在
Map<String, ApiDefinitionDetail> savedApiDefinitionMap = existenceApiDefinitionList.stream().collect(Collectors.toMap(t -> t.getMethod() + t.getPath(), t -> t, (oldValue, newValue) -> newValue));
Map<String, ApiDefinitionDetail> importDataMap = importDetail.stream().collect(Collectors.toMap(t -> t.getMethod() + t.getPath(), t -> t, (oldValue, newValue) -> newValue));
importDataMap.forEach((key, api) -> {
if (savedApiDefinitionMap.containsKey(key)) {
insertAndUpdateData.addExistenceApi(api, new ArrayList<>() {{
this.add(savedApiDefinitionMap.get(key));
}});
} else {
insertAndUpdateData.getInsertApiList().add(api);
}
this.addTestCaseAndMock(insertAndUpdateData, api.getId(), importParser.getCaseMap().get(api.getId()), importParser.getMockMap().get(api.getId()));
});
} else {
//非HTTP类型通过 name判断后续处理会过滤掉路径不一致的
Map<String, ApiDefinitionDetail> importDataMap = importDetail.stream().collect(Collectors.toMap(ApiDefinition::getName, t -> t, (oldValue, newValue) -> newValue));
Map<String, List<ApiDefinitionDetail>> savedApiDefinitionMap = existenceApiDefinitionList.stream().collect(Collectors.groupingBy(ApiDefinitionDetail::getName));
importDataMap.forEach((key, api) -> {
if (savedApiDefinitionMap.containsKey(key)) {
insertAndUpdateData.addExistenceApi(api, savedApiDefinitionMap.get(key));
} else {
insertAndUpdateData.getInsertApiList().add(api);
}
this.addTestCaseAndMock(insertAndUpdateData, api.getId(), importParser.getCaseMap().get(api.getId()), importParser.getMockMap().get(api.getId()));
});
}
}
return insertAndUpdateData;
}
private void addTestCaseAndMock(ApiImportDataAnalysisResult insertAndUpdateData, String apiId, List<ApiTestCaseDTO> caseList, List<ApiDefinitionMockDTO> mockDTOList) {
if (CollectionUtils.isNotEmpty(caseList)) {
insertAndUpdateData.getApiIdAndTestCaseMap().put(apiId, caseList);
}
if (CollectionUtils.isNotEmpty(mockDTOList)) {
insertAndUpdateData.getApiIdAndMockMap().put(apiId, mockDTOList);
}
}
private ApiImportFileParseResult genApiDefinitionImport(List<ApiDefinitionExportDetail> apiDefinitions) { private ApiImportFileParseResult genApiDefinitionImport(List<ApiDefinitionExportDetail> apiDefinitions) {
List<ApiDefinitionExportDetail> distinctImportList = this.mergeApiCaseWithUniqueIdentification(apiDefinitions); List<ApiDefinitionExportDetail> distinctImportList = this.mergeApiCaseWithUniqueIdentification(apiDefinitions);
ApiImportFileParseResult returnDTO = new ApiImportFileParseResult(); ApiImportFileParseResult returnDTO = new ApiImportFileParseResult();
@ -78,7 +159,11 @@ public class MetersphereParserApiDefinition extends HttpApiDefinitionImportAbstr
Map<String, List<ApiTestCaseDTO>> uniqueCaseMap = new HashMap<>(); Map<String, List<ApiTestCaseDTO>> uniqueCaseMap = new HashMap<>();
Map<String, List<ApiDefinitionMockDTO>> uniqueMockMap = new HashMap<>(); Map<String, List<ApiDefinitionMockDTO>> uniqueMockMap = new HashMap<>();
apiDefinitions.forEach((api) -> { apiDefinitions.forEach((api) -> {
String key = api.getMethod() + StringUtils.SPACE + api.getPath(); String key = api.getProtocol() + StringUtils.SPACE + api.getModulePath() + StringUtils.SPACE + api.getName();
if (StringUtils.equalsIgnoreCase(api.getProtocol(), "http")) {
key = api.getMethod() + StringUtils.SPACE + api.getPath();
}
if (!filterApiMap.containsKey(key)) { if (!filterApiMap.containsKey(key)) {
filterApiMap.put(key, api); filterApiMap.put(key, api);
} }

View File

@ -15,6 +15,7 @@ import io.metersphere.api.parser.ApiDefinitionImportParser;
import io.metersphere.api.parser.ImportParserFactory; import io.metersphere.api.parser.ImportParserFactory;
import io.metersphere.api.utils.ApiDataUtils; import io.metersphere.api.utils.ApiDataUtils;
import io.metersphere.api.utils.ApiDefinitionImportUtils; import io.metersphere.api.utils.ApiDefinitionImportUtils;
import io.metersphere.plugin.api.spi.AbstractMsProtocolTestElement;
import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.project.api.KeyValueEnableParam; import io.metersphere.project.api.KeyValueEnableParam;
import io.metersphere.project.constants.PropertyConstant; import io.metersphere.project.constants.PropertyConstant;
@ -464,16 +465,62 @@ public class ApiDefinitionImportService {
} }
modulePathMap.put(item.getPath(), item); modulePathMap.put(item.getPath(), item);
}); });
// 进一步处理已存在的数据
this.furtherProcessingExistenceApiData(request.getModuleId(), selectModulePath.get(), insertAndUpdateData, preImportAnalysisResult, modulePathMap);
// 新增数据处理 // 新增数据处理
this.inertDataAnalysis(preImportAnalysisResult, request, selectModulePath.get(), modulePathMap, insertAndUpdateData); this.inertDataAnalysis(preImportAnalysisResult, request, selectModulePath.get(), modulePathMap, insertAndUpdateData);
// 已有数据处理 // 已有数据处理
this.existenceDataAnalysis(preImportAnalysisResult, request, selectModulePath.get(), modulePathMap, insertAndUpdateData); this.existenceDataAnalysis(preImportAnalysisResult, request, selectModulePath.get(), modulePathMap, insertAndUpdateData);
return preImportAnalysisResult; return preImportAnalysisResult;
} }
/**
* 进一步处理已存在的数据
* 1.为所有数据添加模块ID
* 2.非HTTP的数据判断是 模块ID + 名称是否一样 如果存在这样的数据要更新到新增数据中
*
* @param selectModuleId
* @param selectModulePath
* @param insertAndUpdateData
* @param modulePathMap
*/
private void furtherProcessingExistenceApiData(String selectModuleId, String selectModulePath,
ApiImportDataAnalysisResult insertAndUpdateData, ApiDefinitionPreImportAnalysisResult apiDefinitionPreImportAnalysisResult, Map<String, BaseTreeNode> modulePathMap) {
for (ApiDefinitionDetail importApi : insertAndUpdateData.getInsertApiList()) {
//为新增数据赋予模块ID
this.updateApiDefinitionModule(importApi, selectModulePath, modulePathMap, selectModuleId, apiDefinitionPreImportAnalysisResult);
}
List<ExistenceApiDefinitionDetail> removeList = new ArrayList<>();
for (ExistenceApiDefinitionDetail existenceApiDefinitionDetail : insertAndUpdateData.getExistenceApiList()) {
ApiDefinitionDetail importApi = existenceApiDefinitionDetail.getImportApiDefinition();
//为修改数据赋予模块ID
this.updateApiDefinitionModule(importApi, selectModulePath, modulePathMap, selectModuleId, apiDefinitionPreImportAnalysisResult);
if (!StringUtils.equalsIgnoreCase(importApi.getProtocol(), ModuleConstants.NODE_PROTOCOL_HTTP)) {
List<ApiDefinitionDetail> existenceApiList = new ArrayList<>();
for (ApiDefinitionDetail existenceApi : existenceApiDefinitionDetail.getExistenceApiDefinition()) {
if (!StringUtils.equalsIgnoreCase(importApi.getProtocol(), ModuleConstants.NODE_PROTOCOL_HTTP)) {
if (StringUtils.equals(importApi.getName(), existenceApi.getName()) && StringUtils.equals(importApi.getModuleId(), existenceApi.getModuleId())) {
existenceApiList.add(existenceApi);
}
}
}
if (CollectionUtils.isEmpty(existenceApiList)) {
//不存在相同用例走新建逻辑
insertAndUpdateData.getInsertApiList().add(importApi);
removeList.add(existenceApiDefinitionDetail);
} else {
//更新相同用例的数据集合
existenceApiDefinitionDetail.setExistenceApiDefinition(existenceApiList);
}
}
}
if (CollectionUtils.isNotEmpty(removeList)) {
insertAndUpdateData.getExistenceApiList().removeAll(removeList);
}
}
/* /*
新增数据准备 新增数据准备
指定了导入模块 直接塞入指定模块中 指定了导入模块 直接塞入指定模块中
@ -481,42 +528,6 @@ public class ApiDefinitionImportService {
*/ */
private void inertDataAnalysis(ApiDefinitionPreImportAnalysisResult apiDefinitionPreImportAnalysisResult, ImportRequest request, String selectModulePath, Map<String, BaseTreeNode> modulePathMap, ApiImportDataAnalysisResult analysisResult) { private void inertDataAnalysis(ApiDefinitionPreImportAnalysisResult apiDefinitionPreImportAnalysisResult, ImportRequest request, String selectModulePath, Map<String, BaseTreeNode> modulePathMap, ApiImportDataAnalysisResult analysisResult) {
for (ApiDefinitionDetail apiData : analysisResult.getInsertApiList()) { for (ApiDefinitionDetail apiData : analysisResult.getInsertApiList()) {
if (StringUtils.isNotEmpty(request.getModuleId())) {
if (StringUtils.equalsIgnoreCase(request.getModuleId(), ModuleConstants.DEFAULT_NODE_ID)) {
apiData.setModuleId(ModuleConstants.DEFAULT_NODE_ID);
} else {
if (apiData.getModulePath() == null) {
apiData.setModulePath(StringUtils.EMPTY);
}
// 指定了导入模块 以当前模块为基准进行操作
String finalModulePath = (StringUtils.isBlank(selectModulePath) ? StringUtils.EMPTY : selectModulePath)
+ "/" + ((StringUtils.startsWith(apiData.getModulePath(), "/") ? StringUtils.substring(apiData.getModulePath(), 1) : apiData.getModulePath()));
if (StringUtils.endsWith(finalModulePath, "/")) {
finalModulePath = StringUtils.substring(finalModulePath, 0, finalModulePath.length() - 1);
}
apiData.setModulePath(finalModulePath);
if (!modulePathMap.containsKey(finalModulePath)) {
apiDefinitionPreImportAnalysisResult.getInsertModuleList().addAll(TreeNodeParseUtils.getInsertNodeByPath(modulePathMap, finalModulePath));
}
apiData.setModuleId(modulePathMap.get(finalModulePath).getId());
}
} else {
// 未指定导入模块 接口有模块就放在那个模块下 接口没模块就放在未规划模块内
if (StringUtils.isEmpty(apiData.getModulePath())) {
apiData.setModuleId(ModuleConstants.DEFAULT_NODE_ID);
} else {
String modulePath = apiData.getModulePath();
if (!StringUtils.startsWith(modulePath, "/")) {
modulePath = "/" + modulePath;
apiData.setModulePath(modulePath);
}
if (!modulePathMap.containsKey(modulePath)) {
apiDefinitionPreImportAnalysisResult.getInsertModuleList().addAll(TreeNodeParseUtils.getInsertNodeByPath(modulePathMap, modulePath));
}
apiData.setModuleId(modulePathMap.get(modulePath).getId());
}
}
apiDefinitionPreImportAnalysisResult.getInsertApiData().add(apiData); apiDefinitionPreImportAnalysisResult.getInsertApiData().add(apiData);
//判断是否更新用例 //判断是否更新用例
if (request.isSyncCase()) { if (request.isSyncCase()) {
@ -539,59 +550,86 @@ public class ApiDefinitionImportService {
if (CollectionUtils.isEmpty(analysisResult.getExistenceApiList()) || !request.isCoverData()) { if (CollectionUtils.isEmpty(analysisResult.getExistenceApiList()) || !request.isCoverData()) {
return; return;
} }
List<ExistenceApiDefinitionDetail> existenceApiList = analysisResult.getExistenceApiList(); List<ExistenceApiDefinitionDetail> allExistenceApiList = analysisResult.getExistenceApiList();
List<String> existenceApiIdList = new ArrayList<>(); List<String> existenceApiIdList = new ArrayList<>();
existenceApiList.forEach(item -> existenceApiIdList.add(item.getExistenceApiDefinition().getId())); allExistenceApiList.forEach(item ->
item.getExistenceApiDefinition().forEach(
existenceApi -> existenceApiIdList.add(existenceApi.getId())));
Map<String, ApiDefinitionBlob> existenceApiDefinitionBlobMap = this.selectApiDefinitionBlobIdMap(existenceApiIdList); Map<String, ApiDefinitionBlob> existenceApiDefinitionBlobMap = this.selectApiDefinitionBlobIdMap(existenceApiIdList);
Map<String, List<ApiTestCase>> existenceApiTestCaseMap = this.selectApiTestCaseIdMap(existenceApiIdList); Map<String, List<ApiTestCase>> existenceApiTestCaseMap = this.selectApiTestCaseIdMap(existenceApiIdList);
Map<String, List<ApiDefinitionMock>> existenceApiMockMap = this.selectApiMockIdMap(existenceApiIdList); Map<String, List<ApiDefinitionMock>> existenceApiMockMap = this.selectApiMockIdMap(existenceApiIdList);
for (ExistenceApiDefinitionDetail existenceApiDefinitionDetail : existenceApiList) { for (ExistenceApiDefinitionDetail existenceApiDefinitionDetail : allExistenceApiList) {
ApiDefinitionDetail importApi = existenceApiDefinitionDetail.getImportApiDefinition(); ApiDefinitionDetail importApi = existenceApiDefinitionDetail.getImportApiDefinition();
ApiDefinitionDetail existenceApi = existenceApiDefinitionDetail.getExistenceApiDefinition(); List<ApiDefinitionDetail> existenceApiList = existenceApiDefinitionDetail.getExistenceApiDefinition();
ApiDefinitionDetail existenceApi = existenceApiList.getFirst();
if (StringUtils.isNotBlank(importApi.getModulePath()) && !StringUtils.startsWith(importApi.getModulePath(), "/")) { boolean isSameApi = false;
importApi.setModulePath("/" + importApi.getModulePath()); AbstractMsProtocolTestElement msProtocolTestElement = null;
} for (ApiDefinitionDetail existenceApiItem : existenceApiList) {
boolean isSameRequest = false;
ApiDefinitionBlob apiDefinitionBlob = existenceApiDefinitionBlobMap.get(existenceApi.getId()); ApiDefinitionBlob apiDefinitionBlob = existenceApiDefinitionBlobMap.get(existenceApi.getId());
MsHTTPElement existenceMsHTTPElement = null;
if (apiDefinitionBlob != null) { if (apiDefinitionBlob != null) {
existenceMsHTTPElement = ApiDataUtils.parseObject(new String(apiDefinitionBlob.getRequest()), MsHTTPElement.class); msProtocolTestElement = ApiDataUtils.parseObject(new String(apiDefinitionBlob.getRequest()), AbstractMsProtocolTestElement.class);
//判断参数是否一样 参数类型有 请求头 请求参数 请求体 if (msProtocolTestElement instanceof MsHTTPElement existenceMsHTTPElement) {
isSameRequest = dataIsSame(existenceMsHTTPElement, (MsHTTPElement) importApi.getRequest()); //判断api是否一样 参数类型 请求头 请求参数 请求体
if (dataIsSame(existenceMsHTTPElement, (MsHTTPElement) importApi.getRequest())) {
isSameApi = true;
existenceApi = existenceApiItem;
break;
}
} else {
//判断api是否一样 同目录下的接口名称
if (StringUtils.equals(importApi.getModuleId(), existenceApi.getModuleId()) && StringUtils.equals(importApi.getName(), existenceApi.getName())) {
isSameApi = true;
existenceApi = existenceApiItem;
break;
}
}
}
} }
String importApiId = importApi.getId(); String importApiId = importApi.getId();
importApi.setId(existenceApi.getId()); importApi.setId(existenceApi.getId());
if (request.isCoverModule()) { if (request.isCoverModule()) {
// 确定api的最终目录
this.updateApiDefinitionModule(importApi, selectModulePath, modulePathMap, request.getModuleId(), apiDefinitionPreImportAnalysisResult);
boolean isSameModule = StringUtils.equals(importApi.getModuleId(), existenceApi.getModuleId()); boolean isSameModule = StringUtils.equals(importApi.getModuleId(), existenceApi.getModuleId());
/* /*
开启模块覆盖并覆盖接口此时有4种情况 开启模块覆盖并覆盖接口此时有4种情况
接口请求一样模块一样 不处理 接口请求一样模块一样 不处理
*/ */
if (!isSameRequest || !isSameModule) { if (!isSameApi || !isSameModule) {
if (!isSameRequest && isSameModule) { if (!isSameApi && isSameModule) {
//接口请求不一样模块一样更新接口的非模块信息 //接口请求不一样模块一样更新接口的非模块信息
this.updateApiDefinitionRequest(importApi, existenceMsHTTPElement, request.getPlatform()); this.updateApiDefinitionRequest(importApi, msProtocolTestElement, request.getPlatform());
apiDefinitionPreImportAnalysisResult.getUpdateApiData().add(importApi); apiDefinitionPreImportAnalysisResult.getUpdateApiData().add(importApi);
} else if (isSameRequest) { } else if (isSameApi) {
/*
非http的要进行更新例如tcp最重要的是请求内容不更新的话就会导致导入的内容丢失
而http判断是否一致主要还是请求里的key不是value只要key还不变就没关系
*/
if (!StringUtils.equalsIgnoreCase(importApi.getProtocol(), "HTTP")) {
apiDefinitionPreImportAnalysisResult.getUpdateApiData().add(importApi);
} else {
//接口请求一样模块不一样只更新接口模块信息 //接口请求一样模块不一样只更新接口模块信息
apiDefinitionPreImportAnalysisResult.getUpdateModuleApiList().add(importApi); apiDefinitionPreImportAnalysisResult.getUpdateModuleApiList().add(importApi);
}
} else { } else {
//接口请求不一样模块不一样更新接口所有信息 //接口请求不一样模块不一样更新接口所有信息
this.updateApiDefinitionRequest(importApi, existenceMsHTTPElement, request.getPlatform()); this.updateApiDefinitionRequest(importApi, msProtocolTestElement, request.getPlatform());
apiDefinitionPreImportAnalysisResult.getUpdateApiData().add(importApi); apiDefinitionPreImportAnalysisResult.getUpdateApiData().add(importApi);
} }
} }
} else { } else {
if (!isSameRequest) { if (!isSameApi) {
//覆盖接口不覆盖模块 接口请求不一样要更新接口的非模块信息;接口请求一样不处理 //覆盖接口不覆盖模块 接口请求不一样要更新接口的非模块信息;接口请求一样不处理
this.updateApiDefinitionRequest(importApi, existenceMsHTTPElement, request.getPlatform()); this.updateApiDefinitionRequest(importApi, msProtocolTestElement, request.getPlatform());
apiDefinitionPreImportAnalysisResult.getUpdateApiData().add(importApi);
} else if (!StringUtils.equalsIgnoreCase(importApi.getProtocol(), "HTTP")) {
/*
非http的要进行更新例如tcp最重要的是请求内容不更新的话就会导致导入的内容丢失
而http判断是否一致主要还是请求里的key不是value只要key还不变就没关系
*/
apiDefinitionPreImportAnalysisResult.getUpdateApiData().add(importApi); apiDefinitionPreImportAnalysisResult.getUpdateApiData().add(importApi);
} }
} }
@ -629,10 +667,14 @@ public class ApiDefinitionImportService {
* @param apiDefinitionPreImportAnalysisResult 如果有需要创建的模块要存储在result中 * @param apiDefinitionPreImportAnalysisResult 如果有需要创建的模块要存储在result中
*/ */
private void updateApiDefinitionModule(ApiDefinitionDetail importApi, String selectModulePath, Map<String, BaseTreeNode> modulePathMap, String importModuleId, ApiDefinitionPreImportAnalysisResult apiDefinitionPreImportAnalysisResult) { private void updateApiDefinitionModule(ApiDefinitionDetail importApi, String selectModulePath, Map<String, BaseTreeNode> modulePathMap, String importModuleId, ApiDefinitionPreImportAnalysisResult apiDefinitionPreImportAnalysisResult) {
if (StringUtils.isNotBlank(importApi.getModulePath()) && !StringUtils.startsWith(importApi.getModulePath(), "/")) {
importApi.setModulePath("/" + importApi.getModulePath());
}
if (StringUtils.equalsIgnoreCase(importModuleId, ModuleConstants.DEFAULT_NODE_ID) || if (StringUtils.equalsIgnoreCase(importModuleId, ModuleConstants.DEFAULT_NODE_ID) ||
(StringUtils.isBlank(importModuleId) && StringUtils.isBlank(importApi.getModulePath()))) { (StringUtils.isBlank(importModuleId) && StringUtils.isBlank(importApi.getModulePath()))) {
//选中的是未规划模块或者没选择模块且接口本身没模块 //选中的是未规划模块或者没选择模块且接口本身没模块
importApi.setModuleId(ModuleConstants.DEFAULT_NODE_ID); importApi.setModuleId(ModuleConstants.DEFAULT_NODE_ID);
importApi.setModulePath("/" + ModuleConstants.DEFAULT_NODE_ID);
} else { } else {
if (importApi.getModulePath() == null) { if (importApi.getModulePath() == null) {
importApi.setModulePath(StringUtils.EMPTY); importApi.setModulePath(StringUtils.EMPTY);
@ -651,7 +693,7 @@ public class ApiDefinitionImportService {
} }
} }
private void updateApiDefinitionRequest(ApiDefinitionDetail importApi, MsHTTPElement existenceMsHTTPElement, String platform) { private void updateApiDefinitionRequest(ApiDefinitionDetail importApi, AbstractMsProtocolTestElement existenceMsHTTPElement, String platform) {
if (StringUtils.equals(platform, ApiImportPlatform.Swagger3.name()) && existenceMsHTTPElement != null) { if (StringUtils.equals(platform, ApiImportPlatform.Swagger3.name()) && existenceMsHTTPElement != null) {
//swagger如果接口已存在并且要覆盖 那么是不能覆盖原来接口的前后置数据的因为swagger文件里没有 //swagger如果接口已存在并且要覆盖 那么是不能覆盖原来接口的前后置数据的因为swagger文件里没有
importApi.getRequest().setChildren(existenceMsHTTPElement.getChildren()); importApi.getRequest().setChildren(existenceMsHTTPElement.getChildren());