feat(接口自动化): 增加外部导入到场景功能
This commit is contained in:
parent
86c6254d48
commit
a73b202551
|
@ -32,7 +32,7 @@ public class MsParser extends ApiImportAbstractParser {
|
||||||
if (testObject.get("projectName") != null) {
|
if (testObject.get("projectName") != null) {
|
||||||
return parseMsFormat(testStr, request);
|
return parseMsFormat(testStr, request);
|
||||||
} else {
|
} else {
|
||||||
return parsePluginFormat(testObject);
|
return parsePluginFormat(testObject, request.isSaved());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,14 +53,16 @@ public class MsParser extends ApiImportAbstractParser {
|
||||||
return apiDefinitionImport;
|
return apiDefinitionImport;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ApiDefinitionImport parsePluginFormat(JSONObject testObject) {
|
private ApiDefinitionImport parsePluginFormat(JSONObject testObject, boolean isSaved) {
|
||||||
List<ApiDefinitionResult> results = new ArrayList<>();
|
List<ApiDefinitionResult> results = new ArrayList<>();
|
||||||
ApiDefinitionImport apiImport = new ApiDefinitionImport();
|
ApiDefinitionImport apiImport = new ApiDefinitionImport();
|
||||||
apiImport.setProtocol(RequestType.HTTP);
|
apiImport.setProtocol(RequestType.HTTP);
|
||||||
apiImport.setData(results);
|
apiImport.setData(results);
|
||||||
testObject.keySet().forEach(tag -> {
|
testObject.keySet().forEach(tag -> {
|
||||||
ApiModule module = apiModuleService.getNewModule(tag, this.projectId, 1);
|
ApiModule module = apiModuleService.getNewModule(tag, this.projectId, 1);
|
||||||
|
if (isSaved) {
|
||||||
createModule(module);
|
createModule(module);
|
||||||
|
}
|
||||||
JSONObject requests = testObject.getJSONObject(tag);
|
JSONObject requests = testObject.getJSONObject(tag);
|
||||||
requests.keySet().forEach(requestName -> {
|
requests.keySet().forEach(requestName -> {
|
||||||
|
|
||||||
|
|
|
@ -30,17 +30,17 @@ public class PostmanParser extends ApiImportAbstractParser {
|
||||||
List<PostmanKeyValue> variables = postmanCollection.getVariable();
|
List<PostmanKeyValue> variables = postmanCollection.getVariable();
|
||||||
ApiDefinitionImport apiImport = new ApiDefinitionImport();
|
ApiDefinitionImport apiImport = new ApiDefinitionImport();
|
||||||
List<ApiDefinitionResult> results = new ArrayList<>();
|
List<ApiDefinitionResult> results = new ArrayList<>();
|
||||||
parseItem(postmanCollection.getItem(), variables, results, buildModule(postmanCollection.getInfo().getName(), null));
|
parseItem(postmanCollection.getItem(), variables, results, buildModule(postmanCollection.getInfo().getName(), null, request.isSaved()), request.isSaved());
|
||||||
apiImport.setData(results);
|
apiImport.setData(results);
|
||||||
return apiImport;
|
return apiImport;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parseItem(List<PostmanItem> items, List<PostmanKeyValue> variables, List<ApiDefinitionResult> results, ApiModule parentModule) {
|
private void parseItem(List<PostmanItem> items, List<PostmanKeyValue> variables, List<ApiDefinitionResult> results, ApiModule parentModule, boolean isSaved) {
|
||||||
for (PostmanItem item : items) {
|
for (PostmanItem item : items) {
|
||||||
List<PostmanItem> childItems = item.getItem();
|
List<PostmanItem> childItems = item.getItem();
|
||||||
if (childItems != null) {
|
if (childItems != null) {
|
||||||
ApiModule module = buildModule(item.getName(), parentModule);
|
ApiModule module = buildModule(item.getName(), parentModule, isSaved);
|
||||||
parseItem(childItems, variables, results, module);
|
parseItem(childItems, variables, results, module, isSaved);
|
||||||
} else {
|
} else {
|
||||||
ApiDefinitionResult request = parsePostman(item);
|
ApiDefinitionResult request = parsePostman(item);
|
||||||
if (request != null) {
|
if (request != null) {
|
||||||
|
@ -53,7 +53,7 @@ public class PostmanParser extends ApiImportAbstractParser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ApiModule buildModule(String name, ApiModule parentModule) {
|
private ApiModule buildModule(String name, ApiModule parentModule, boolean isSaved) {
|
||||||
apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
|
apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
|
||||||
ApiModule module;
|
ApiModule module;
|
||||||
if (parentModule != null) {
|
if (parentModule != null) {
|
||||||
|
@ -62,7 +62,9 @@ public class PostmanParser extends ApiImportAbstractParser {
|
||||||
} else {
|
} else {
|
||||||
module = apiModuleService.getNewModule(name, this.projectId, 1);
|
module = apiModuleService.getNewModule(name, this.projectId, 1);
|
||||||
}
|
}
|
||||||
|
if (isSaved) {
|
||||||
createModule(module);
|
createModule(module);
|
||||||
|
}
|
||||||
return module;
|
return module;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -39,11 +39,11 @@ public class Swagger2Parser extends ApiImportAbstractParser {
|
||||||
}
|
}
|
||||||
ApiDefinitionImport definitionImport = new ApiDefinitionImport();
|
ApiDefinitionImport definitionImport = new ApiDefinitionImport();
|
||||||
this.projectId = request.getProjectId();
|
this.projectId = request.getProjectId();
|
||||||
definitionImport.setData(parseRequests(swagger));
|
definitionImport.setData(parseRequests(swagger, request.isSaved()));
|
||||||
return definitionImport;
|
return definitionImport;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<ApiDefinitionResult> parseRequests(Swagger swagger) {
|
private List<ApiDefinitionResult> parseRequests(Swagger swagger, boolean isSaved) {
|
||||||
Map<String, Path> paths = swagger.getPaths();
|
Map<String, Path> paths = swagger.getPaths();
|
||||||
Set<String> pathNames = paths.keySet();
|
Set<String> pathNames = paths.keySet();
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ public class Swagger2Parser extends ApiImportAbstractParser {
|
||||||
parseParameters(operation, request);
|
parseParameters(operation, request);
|
||||||
apiDefinition.setRequest(JSON.toJSONString(request));
|
apiDefinition.setRequest(JSON.toJSONString(request));
|
||||||
apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation.getResponses())));
|
apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation.getResponses())));
|
||||||
buildModule(apiDefinition, operation);
|
buildModule(apiDefinition, operation, isSaved);
|
||||||
results.add(apiDefinition);
|
results.add(apiDefinition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,13 +71,15 @@ public class Swagger2Parser extends ApiImportAbstractParser {
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void buildModule(ApiDefinitionResult apiDefinition, Operation operation) {
|
private void buildModule(ApiDefinitionResult apiDefinition, Operation operation, boolean isSaved) {
|
||||||
List<String> tags = operation.getTags();
|
List<String> tags = operation.getTags();
|
||||||
if (tags != null) {
|
if (tags != null) {
|
||||||
tags.forEach(tag -> {
|
tags.forEach(tag -> {
|
||||||
apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
|
apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
|
||||||
ApiModule module = apiModuleService.getNewModule(tag, this.projectId, 1);
|
ApiModule module = apiModuleService.getNewModule(tag, this.projectId, 1);
|
||||||
|
if (isSaved) {
|
||||||
createModule(module);
|
createModule(module);
|
||||||
|
}
|
||||||
apiDefinition.setModuleId(module.getId());
|
apiDefinition.setModuleId(module.getId());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue