fix(接口测试): 优化接口测试postman导入逻辑

--bug=1044990 --user=宋天阳 【接口测试】GitHub #32277postman重复数据生成case时命名优化 https://www.tapd.cn/55049933/s/1565799
This commit is contained in:
Jianguo-Genius 2024-08-20 16:10:34 +08:00 committed by 建国
parent 9188df0e85
commit 1c13d56d09
4 changed files with 84 additions and 44 deletions

View File

@ -26,13 +26,34 @@ import java.util.regex.Pattern;
public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractParser<T> {
protected MsHTTPSamplerProxy parsePostman(PostmanItem requestItem) {
PostmanRequest requestDesc = requestItem.getRequest();
if (requestDesc == null) {
if (requestItem.getRequest() == null) {
return null;
} else {
MsHTTPSamplerProxy request = parseHttpSampler(requestItem.getName(), requestItem.getRequest());
addBodyHeader(request);
PostmanItem.ProtocolProfileBehavior protocolProfileBehavior = requestItem.getProtocolProfileBehavior();
request.setFollowRedirects(protocolProfileBehavior == null ||
protocolProfileBehavior.getFollowRedirects());
request.setResponseTimeout("60000");
request.setConnectTimeout("60000");
return request;
}
requestDesc.getAuth(); // todo 认证方式等待优化
}
protected MsHTTPSamplerProxy parseResponse(PostmanResponse requestItem) {
if (requestItem.getOriginalRequest() == null) {
return null;
} else {
MsHTTPSamplerProxy request = parseHttpSampler(requestItem.getName(), requestItem.getOriginalRequest());
addBodyHeader(request);
return request;
}
}
private MsHTTPSamplerProxy parseHttpSampler(String requestName, PostmanRequest requestDesc) {
// requestDesc.getAuth(); // todo 认证方式等待优化
PostmanUrl url = requestDesc.getUrl();
MsHTTPSamplerProxy request = buildRequest(requestItem.getName(), url == null ? StringUtils.EMPTY : url.getRaw(), requestDesc.getMethod(),
MsHTTPSamplerProxy request = buildRequest(requestName, url == null ? StringUtils.EMPTY : url.getRaw(), requestDesc.getMethod(),
(requestDesc.getBody() == null || requestDesc.getBody().get("jsonSchema") == null) ? StringUtils.EMPTY : requestDesc.getBody().get("jsonSchema").textValue());
request.setRest(parseKeyValue(url != null && CollectionUtils.isNotEmpty(url.getVariable()) ?
url.getVariable() : new ArrayList<>()));
@ -47,16 +68,6 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
parseBody(request.getBody(), requestDesc);
request.setArguments(parseKeyValue(url == null ? new ArrayList<>() : url.getQuery()));
request.setHeaders(parseKeyValue(requestDesc.getHeader()));
addBodyHeader(request);
PostmanItem.ProtocolProfileBehavior protocolProfileBehavior = requestItem.getProtocolProfileBehavior();
if (protocolProfileBehavior != null &&
!protocolProfileBehavior.getFollowRedirects()) {
request.setFollowRedirects(false);
} else {
request.setFollowRedirects(true);
}
request.setResponseTimeout("60000");
request.setConnectTimeout("60000");
return request;
}
@ -169,7 +180,7 @@ public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractPa
}
private void parseRawBody(Body body, ObjectNode postmanBody, String bodyMode) {
body.setRaw(parseVariable(postmanBody.get(bodyMode).textValue()));
body.setRaw(parseVariable(postmanBody.get(bodyMode) == null ? StringUtils.EMPTY : postmanBody.get(bodyMode).textValue()));
body.setType(MsRequestBodyType.RAW.value());
JsonNode options = postmanBody.get("options");
if (options != null) {

View File

@ -4,21 +4,25 @@ package io.metersphere.api.parse.api;
import io.metersphere.api.dto.ApiTestImportRequest;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.definition.response.HttpResponse;
import io.metersphere.api.parse.PostmanAbstractParserParser;
import io.metersphere.api.parse.postman.PostmanCollection;
import io.metersphere.api.parse.postman.PostmanItem;
import io.metersphere.api.parse.PostmanAbstractParserParser;
import io.metersphere.api.parse.postman.PostmanResponse;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.JSONUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class PostmanDefinitionParser extends PostmanAbstractParserParser<ApiDefinitionImport> {
@ -34,22 +38,21 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser<ApiDefi
}
PostmanCollection postmanCollection = JSON.parseObject(testStr, PostmanCollection.class);
ApiDefinitionImport apiImport = new ApiDefinitionImport();
List<ApiDefinitionWithBLOBs> results = new ArrayList<>();
Map<String, ApiDefinitionWithBLOBs> resultMap = new HashMap<>();
String modulePath = null;
if (StringUtils.isNotBlank(postmanCollection.getInfo().getName())) {
modulePath = "/" + postmanCollection.getInfo().getName();
}
List<ApiTestCaseWithBLOBs> cases = new ArrayList<>();
parseItem(postmanCollection.getItem(), modulePath, results,
cases, addCase);
apiImport.setData(results);
parseApiDefinition(postmanCollection.getItem(), modulePath, resultMap, cases);
apiImport.setData(new ArrayList<>(resultMap.values()));
apiImport.setCases(cases);
return apiImport;
}
protected void parseItem(List<PostmanItem> items, String modulePath, List<ApiDefinitionWithBLOBs> results,
List<ApiTestCaseWithBLOBs> cases, boolean addCase) {
protected void parseApiDefinition(List<PostmanItem> items, String modulePath, Map<String, ApiDefinitionWithBLOBs> apiResultMap,
List<ApiTestCaseWithBLOBs> cases) {
for (PostmanItem item : items) {
List<PostmanItem> childItems = item.getItem();
if (childItems != null) {
@ -59,7 +62,7 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser<ApiDefi
} else {
itemModulePath = item.getName();
}
parseItem(childItems, itemModulePath, results, cases, addCase);
parseApiDefinition(childItems, itemModulePath, apiResultMap, cases);
} else {
MsHTTPSamplerProxy msHTTPSamplerProxy = parsePostman(item);
HttpResponse response = parsePostmanResponse(item);
@ -72,15 +75,38 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser<ApiDefi
if (StringUtils.isNotBlank(modulePath)) {
request.setModulePath(modulePath);
}
results.add(request);
if (addCase) {
ApiTestCaseWithBLOBs apiTestCase = new ApiTestCaseWithBLOBs();
BeanUtils.copyBean(apiTestCase, request);
apiTestCase.setApiDefinitionId(request.getId());
apiTestCase.setPriority("P0");
cases.add(apiTestCase);
String apiPath = request.getMethod() + "_" + request.getPath();
if (apiResultMap.containsKey(apiPath)) {
request = apiResultMap.get(apiPath);
} else {
apiResultMap.put(apiPath, request);
}
if (CollectionUtils.isNotEmpty(item.getResponse())) {
List<String> existenceNameList = new ArrayList<>();
for (PostmanResponse postmanResponse : item.getResponse()) {
ApiTestCaseWithBLOBs apiTestCase = new ApiTestCaseWithBLOBs();
BeanUtils.copyBean(apiTestCase, request);
apiTestCase.setApiDefinitionId(request.getId());
apiTestCase.setPriority("P0");
apiTestCase.setName(this.getUniqueName(apiTestCase.getName(), existenceNameList));
MsHTTPSamplerProxy httpResponse = parseResponse(postmanResponse);
apiTestCase.setRequest(JSON.toJSONString(httpResponse));
cases.add(apiTestCase);
existenceNameList.add(apiTestCase.getName());
}
}
}
}
}
public String getUniqueName(String originalName, List<String> existenceNameList) {
String returnName = originalName;
int index = 1;
while (existenceNameList.contains(returnName)) {
returnName = originalName + " - " + index;
index++;
}
return returnName;
}
}

View File

@ -325,7 +325,7 @@ public class Swagger3Parser extends SwaggerAbstractParser {
}
private void parseResponseBody(ApiResponse response, Body body) {
body.setRaw(response.getDescription());
// body.setRaw(response.getDescription());
Content content = response.getContent();
if (content == null) {
body.setType(Body.RAW);
@ -1197,22 +1197,25 @@ public class Swagger3Parser extends SwaggerAbstractParser {
}
}
JSONObject statusCodeInfo = new JSONObject();
statusCodeInfo.put("headers", headers);
statusCodeInfo.put("content", buildContent(response, schemas));
statusCodeInfo.put("description", StringUtils.EMPTY);
// 返回code
JSONArray statusCode = response.optJSONArray("statusCode");
if (statusCode != null) {
for (int i = 0; i < statusCode.length(); i++) {
JSONObject statusCodeInfo = new JSONObject();
statusCodeInfo.put("headers", headers);
statusCodeInfo.put("content", buildContent(response, schemas));
statusCodeInfo.put("description", StringUtils.EMPTY);
JSONObject jsonObject = statusCode.getJSONObject(i);
if (StringUtils.isNotBlank(jsonObject.optString("value"))) {
statusCodeInfo.put("description", jsonObject.optString("value"));
}
if (StringUtils.isNotBlank(jsonObject.optString("name"))) {
responseBody.put(jsonObject.optString("name"), statusCodeInfo);
}
JSONObject jsonObject = statusCode.getJSONObject(0);
if (StringUtils.isNotBlank(jsonObject.optString("value"))) {
statusCodeInfo.put("description", jsonObject.optString("value"));
}
if (StringUtils.isNotBlank(jsonObject.optString("name"))) {
responseBody.put(jsonObject.optString("name"), statusCodeInfo);
} else {
responseBody.put("200", statusCodeInfo);
}
} else {
responseBody.put("200", statusCodeInfo);
}
return responseBody;
}

View File

@ -760,7 +760,7 @@ public class ApiDefinitionImportUtilService {
} else {
apiDefinition.setVersionId(apiTestImportRequest.getDefaultVersion());
}
boolean newCreate = !StringUtils.equals(ApiImportPlatform.Swagger2.name(), apiDefinitionImportParamDTO.getApiTestImportRequest().getPlatform())
boolean newCreate = !StringUtils.equalsAnyIgnoreCase(apiDefinitionImportParamDTO.getApiTestImportRequest().getPlatform(), ApiImportPlatform.Swagger2.name(), ApiImportPlatform.Postman.name())
&& !StringUtils.isNotBlank(apiDefinitionImportParamDTO.getApiTestImportRequest().getSwaggerUrl())
&& !StringUtils.equals("idea", apiDefinitionImportParamDTO.getApiTestImportRequest().getOrigin());
caseList = setRequestAndAddNewCase(apiDefinition, caseList, newCreate);