From 1c13d56d09cdd0a91d6ec37ea7423791bfc4a9ca Mon Sep 17 00:00:00 2001 From: Jianguo-Genius Date: Tue, 20 Aug 2024 16:10:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95postman?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1044990 --user=宋天阳 【接口测试】GitHub #32277postman重复数据生成case时命名优化 https://www.tapd.cn/55049933/s/1565799 --- .../parse/PostmanAbstractParserParser.java | 41 +++++++++----- .../parse/api/PostmanDefinitionParser.java | 56 ++++++++++++++----- .../api/parse/api/Swagger3Parser.java | 29 +++++----- .../ApiDefinitionImportUtilService.java | 2 +- 4 files changed, 84 insertions(+), 44 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java index f255015a62..b6b8db6108 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java @@ -26,13 +26,34 @@ import java.util.regex.Pattern; public abstract class PostmanAbstractParserParser extends ApiImportAbstractParser { 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 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 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) { diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/api/PostmanDefinitionParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/api/PostmanDefinitionParser.java index 275e0be757..77f749e4bb 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/api/PostmanDefinitionParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/api/PostmanDefinitionParser.java @@ -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 { @@ -34,22 +38,21 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser results = new ArrayList<>(); + Map resultMap = new HashMap<>(); String modulePath = null; if (StringUtils.isNotBlank(postmanCollection.getInfo().getName())) { modulePath = "/" + postmanCollection.getInfo().getName(); } List 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 items, String modulePath, List results, - List cases, boolean addCase) { + protected void parseApiDefinition(List items, String modulePath, Map apiResultMap, + List cases) { for (PostmanItem item : items) { List childItems = item.getItem(); if (childItems != null) { @@ -59,7 +62,7 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser 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 existenceNameList) { + String returnName = originalName; + int index = 1; + while (existenceNameList.contains(returnName)) { + returnName = originalName + " - " + index; + index++; + } + return returnName; + } } diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger3Parser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger3Parser.java index 88c18b404d..f0264d8a8b 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger3Parser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/api/Swagger3Parser.java @@ -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; } diff --git a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionImportUtilService.java b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionImportUtilService.java index 81279aa4d5..0b50e0b812 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionImportUtilService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionImportUtilService.java @@ -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);