refactor(测试跟踪): swagger 获取tag

This commit is contained in:
chenjianxing 2020-12-04 15:09:34 +08:00
parent e95efe4e41
commit 45f791a4b1
4 changed files with 43 additions and 27 deletions

View File

@ -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<ApiDefinitionResult> data;
private HashMap<String, List<ApiDefinitionResult>> resultMap;
}

View File

@ -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<ApiDefinitionResult> parseRequests(Swagger swagger) {
List<ApiDefinitionResult> results = new LinkedList<>();
private HashMap<String, List<ApiDefinitionResult>> parseRequests(Swagger swagger) {
// List<ApiDefinitionResult> results = new LinkedList<>();
Map<String, Path> paths = swagger.getPaths();
Set<String> pathNames = paths.keySet();
this.definitions = swagger.getDefinitions();
HashMap<String, List<ApiDefinitionResult>> moduleMap = new HashMap<>();
for (String pathName : pathNames) {
Path path = paths.get(pathName);
Map<HttpMethod, Operation> 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<String> tags = operation.getTags();
// if (tags != null) {
// tags.forEach(tag -> {
// Scenario scenario = Optional.ofNullable(scenarioMap.get(tag)).orElse(new Scenario());
// List<Request> 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<Request> 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<String, List<ApiDefinitionResult>> moduleMap,
ApiDefinitionResult apiDefinition, Operation operation) {
List<String> tags = operation.getTags();
if (tags != null) {
tags.forEach(tag -> {
List<ApiDefinitionResult> list = moduleMap.get(tag);
if (list == null) {
list = new ArrayList<>();
moduleMap.put(tag, list);
}
list.add(apiDefinition);
});
} else {
List<ApiDefinitionResult> 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<String, Response> responses) {
private HttpResponse parseResponse(Map<String, Response> 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<KeyValue> msHeaders) {

View File

@ -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<ApiDefinitionResult> data = apiImport.getData();
HashMap<String, List<ApiDefinitionResult>> 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());

View File

@ -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());
}
}
}