From 45f791a4b1240f172c0c901f841a312b7f07dd66 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Fri, 4 Dec 2020 15:09:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):?= =?UTF-8?q?=20swagger=20=E8=8E=B7=E5=8F=96tag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../definition/parse/ApiDefinitionImport.java | 2 + .../metersphere/api/parse/Swagger2Parser.java | 60 +++++++++++-------- .../api/service/ApiDefinitionService.java | 6 ++ .../api/service/ApiModuleService.java | 2 +- 4 files changed, 43 insertions(+), 27 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/ApiDefinitionImport.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/ApiDefinitionImport.java index f4475d097f..572deba927 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/ApiDefinitionImport.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/ApiDefinitionImport.java @@ -3,6 +3,7 @@ package io.metersphere.api.dto.definition.parse; import io.metersphere.api.dto.definition.ApiDefinitionResult; import lombok.Data; +import java.util.HashMap; import java.util.List; @Data @@ -10,4 +11,5 @@ public class ApiDefinitionImport { private String projectName; private String protocol; private List data; + private HashMap> resultMap; } diff --git a/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java b/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java index a12ca60fd9..cecada4f6a 100644 --- a/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java +++ b/backend/src/main/java/io/metersphere/api/parse/Swagger2Parser.java @@ -11,6 +11,8 @@ import io.metersphere.api.dto.definition.response.HttpResponse; import io.metersphere.api.dto.parse.ApiImport; import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.KeyValue; +import io.metersphere.api.dto.scenario.Scenario; +import io.metersphere.api.dto.scenario.request.Request; import io.metersphere.api.dto.scenario.request.RequestType; import io.metersphere.commons.constants.SwaggerParameterType; import io.swagger.models.*; @@ -36,7 +38,7 @@ public class Swagger2Parser extends ApiImportAbstractParser { swagger = new SwaggerParser().readWithInfo(getApiTestStr(source)).getSwagger(); } ApiDefinitionImport definitionImport = new ApiDefinitionImport(); - definitionImport.setData(parseRequests(swagger)); + definitionImport.setResultMap(parseRequests(swagger)); return definitionImport; } @@ -45,13 +47,15 @@ public class Swagger2Parser extends ApiImportAbstractParser { return null; } - private List parseRequests(Swagger swagger) { - List results = new LinkedList<>(); + private HashMap> parseRequests(Swagger swagger) { +// List results = new LinkedList<>(); Map paths = swagger.getPaths(); Set pathNames = paths.keySet(); this.definitions = swagger.getDefinitions(); + HashMap> moduleMap = new HashMap<>(); + for (String pathName : pathNames) { Path path = paths.get(pathName); Map operationMap = path.getOperationMap(); @@ -64,33 +68,36 @@ public class Swagger2Parser extends ApiImportAbstractParser { parseParameters(operation, request); apiDefinition.setRequest(JSON.toJSONString(request)); apiDefinition.setId(request.getId()); - results.add(apiDefinition); - parseResponse(operation.getResponses()); - -// List tags = operation.getTags(); -// if (tags != null) { -// tags.forEach(tag -> { -// Scenario scenario = Optional.ofNullable(scenarioMap.get(tag)).orElse(new Scenario()); -// List requests = Optional.ofNullable(scenario.getRequests()).orElse(new ArrayList<>()); -// requests.add(request); -// scenario.setRequests(requests); -// scenario.setName(tag); -// scenarioMap.put(tag, scenario); -// }); -// } else { -// Scenario scenario = Optional.ofNullable(scenarioMap.get("default")).orElse(new Scenario()); -// List requests = Optional.ofNullable(scenario.getRequests()).orElse(new ArrayList<>()); -// requests.add(request); -// scenario.setRequests(requests); -// scenarioMap.put("default", scenario); -// } - +// results.add(apiDefinition); + apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation.getResponses()))); + buildResultMap(moduleMap, apiDefinition, operation); } } this.definitions = null; + return moduleMap; + } - return results; + private void buildResultMap(HashMap> moduleMap, + ApiDefinitionResult apiDefinition, Operation operation) { + List tags = operation.getTags(); + if (tags != null) { + tags.forEach(tag -> { + List list = moduleMap.get(tag); + if (list == null) { + list = new ArrayList<>(); + moduleMap.put(tag, list); + } + list.add(apiDefinition); + }); + } else { + List list = moduleMap.get("default"); + if (list == null) { + list = new ArrayList<>(); + moduleMap.put("default", list); + } + list.add(apiDefinition); + } } private ApiDefinitionResult buildApiDefinition(Operation operation, String path, String method) { @@ -207,7 +214,7 @@ public class Swagger2Parser extends ApiImportAbstractParser { addHeader(headers, headerParameter.getName(), "", getDefaultStringValue(headerParameter.getDescription())); } - private void parseResponse(Map responses) { + private HttpResponse parseResponse(Map responses) { HttpResponse msResponse = new HttpResponse(); msResponse.setBody(new Body()); msResponse.setHeaders(new ArrayList<>()); @@ -221,6 +228,7 @@ public class Swagger2Parser extends ApiImportAbstractParser { parseResponseBodyParameters(response, msResponse.getBody()); }); } + return msResponse; } private void parseResponseHeader(Response response, List msHeaders) { diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index 8786acb243..ae842472e7 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -61,6 +61,8 @@ public class ApiDefinitionService { private JMeterService jMeterService; @Resource private SqlSessionFactory sqlSessionFactory; + @Resource + private ApiModuleService apiModuleService; private static Cache cache = Cache.newHardMemoryCache(0, 3600 * 24); @@ -333,6 +335,10 @@ public class ApiDefinitionService { SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); ApiDefinitionMapper batchMapper = sqlSession.getMapper(ApiDefinitionMapper.class); List data = apiImport.getData(); + HashMap> resultMap = apiImport.getResultMap(); + resultMap.forEach((module, apiDefinition) -> { +// apiModuleService.addNode(); + }); for (int i = 0; i < data.size(); i++) { ApiDefinitionResult item = data.get(i); item.setProjectId(importRequest.getProjectId()); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java index 8ebfd85ab5..a6dd6688d1 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java @@ -128,7 +128,7 @@ public class ApiModuleService { criteria.andIdNotEqualTo(node.getId()); } if (apiModuleMapper.selectByExample(example).size() > 0) { - MSException.throwException(Translator.get("test_case_module_already_exists")); + MSException.throwException(Translator.get("test_case_module_already_exists") + ": " + node.getName()); } } }