Merge remote-tracking branch 'origin/master' into master

This commit is contained in:
Captain.B 2020-12-04 18:04:42 +08:00
commit 7bfb226314
6 changed files with 57 additions and 29 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

@ -22,7 +22,6 @@ import lombok.Data;
@JSONType(seeAlso = {HttpRequest.class, DubboRequest.class, SqlRequest.class, TCPRequest.class}, typeKey = "type")
@Data
public abstract class Request {
private String type;
@JSONField(ordinal = 1)
private String id;
@JSONField(ordinal = 2)

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,12 @@ 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
// apiModuleService.addNode();
});
for (int i = 0; i < data.size(); i++) {
ApiDefinitionResult item = data.get(i);
item.setProjectId(importRequest.getProjectId());

View File

@ -105,6 +105,17 @@ public class ApiModuleService {
return node.getId();
}
public ApiModule getNewModule(String name, String projectId, int level) {
ApiModule node = new ApiModule();
node.setCreateTime(System.currentTimeMillis());
node.setUpdateTime(System.currentTimeMillis());
node.setId(UUID.randomUUID().toString());
node.setLevel(level);
node.setName(name);
node.setProjectId(projectId);
return node;
}
private void validateNode(ApiModule node) {
if (node.getLevel() > TestCaseConstants.MAX_NODE_DEPTH) {
throw new RuntimeException(Translator.get("test_case_node_level_tip")
@ -128,7 +139,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());
}
}
}

@ -1 +1 @@
Subproject commit 8a972a198775b3783ed6e4cef27197e53d1ebdc8
Subproject commit a22a3005d9bd254793fcf634d72539cbdf31be3a