feat(接口自动化): 增加外部导入到场景功能

This commit is contained in:
fit2-zhao 2020-12-16 14:27:35 +08:00
parent 86c6254d48
commit a73b202551
3 changed files with 24 additions and 18 deletions

View File

@ -32,7 +32,7 @@ public class MsParser extends ApiImportAbstractParser {
if (testObject.get("projectName") != null) {
return parseMsFormat(testStr, request);
} else {
return parsePluginFormat(testObject);
return parsePluginFormat(testObject, request.isSaved());
}
}
@ -53,14 +53,16 @@ public class MsParser extends ApiImportAbstractParser {
return apiDefinitionImport;
}
private ApiDefinitionImport parsePluginFormat(JSONObject testObject) {
private ApiDefinitionImport parsePluginFormat(JSONObject testObject, boolean isSaved) {
List<ApiDefinitionResult> results = new ArrayList<>();
ApiDefinitionImport apiImport = new ApiDefinitionImport();
apiImport.setProtocol(RequestType.HTTP);
apiImport.setData(results);
testObject.keySet().forEach(tag -> {
ApiModule module = apiModuleService.getNewModule(tag, this.projectId, 1);
createModule(module);
if (isSaved) {
createModule(module);
}
JSONObject requests = testObject.getJSONObject(tag);
requests.keySet().forEach(requestName -> {

View File

@ -30,17 +30,17 @@ public class PostmanParser extends ApiImportAbstractParser {
List<PostmanKeyValue> variables = postmanCollection.getVariable();
ApiDefinitionImport apiImport = new ApiDefinitionImport();
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);
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) {
List<PostmanItem> childItems = item.getItem();
if (childItems != null) {
ApiModule module = buildModule(item.getName(), parentModule);
parseItem(childItems, variables, results, module);
ApiModule module = buildModule(item.getName(), parentModule, isSaved);
parseItem(childItems, variables, results, module, isSaved);
} else {
ApiDefinitionResult request = parsePostman(item);
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);
ApiModule module;
if (parentModule != null) {
@ -62,7 +62,9 @@ public class PostmanParser extends ApiImportAbstractParser {
} else {
module = apiModuleService.getNewModule(name, this.projectId, 1);
}
createModule(module);
if (isSaved) {
createModule(module);
}
return module;
}

View File

@ -39,11 +39,11 @@ public class Swagger2Parser extends ApiImportAbstractParser {
}
ApiDefinitionImport definitionImport = new ApiDefinitionImport();
this.projectId = request.getProjectId();
definitionImport.setData(parseRequests(swagger));
definitionImport.setData(parseRequests(swagger, request.isSaved()));
return definitionImport;
}
private List<ApiDefinitionResult> parseRequests(Swagger swagger) {
private List<ApiDefinitionResult> parseRequests(Swagger swagger, boolean isSaved) {
Map<String, Path> paths = swagger.getPaths();
Set<String> pathNames = paths.keySet();
@ -62,7 +62,7 @@ public class Swagger2Parser extends ApiImportAbstractParser {
parseParameters(operation, request);
apiDefinition.setRequest(JSON.toJSONString(request));
apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation.getResponses())));
buildModule(apiDefinition, operation);
buildModule(apiDefinition, operation, isSaved);
results.add(apiDefinition);
}
}
@ -71,13 +71,15 @@ public class Swagger2Parser extends ApiImportAbstractParser {
return results;
}
private void buildModule(ApiDefinitionResult apiDefinition, Operation operation) {
private void buildModule(ApiDefinitionResult apiDefinition, Operation operation, boolean isSaved) {
List<String> tags = operation.getTags();
if (tags != null) {
tags.forEach(tag -> {
apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
ApiModule module = apiModuleService.getNewModule(tag, this.projectId, 1);
createModule(module);
if (isSaved) {
createModule(module);
}
apiDefinition.setModuleId(module.getId());
});
}
@ -113,8 +115,8 @@ public class Swagger2Parser extends ApiImportAbstractParser {
for (Parameter parameter : parameters) {
switch (parameter.getIn()) {
case SwaggerParameterType.PATH:
parsePathParameters(parameter, request.getRest());
break;
parsePathParameters(parameter, request.getRest());
break;
case SwaggerParameterType.QUERY:
parseQueryParameters(parameter, request.getArguments());
break;
@ -297,14 +299,14 @@ public class Swagger2Parser extends ApiImportAbstractParser {
|| value instanceof DecimalProperty) {
return 0.0;
} else {// todo 其他类型?
return getDefaultStringValue(value.getDescription());
return getDefaultStringValue(value.getDescription());
}
}
private void parseFormDataParameters(FormParameter parameter, Body body) {
List<KeyValue> keyValues = Optional.ofNullable(body.getKvs()).orElse(new ArrayList<>());
KeyValue kv = new KeyValue(parameter.getName(), "", getDefaultStringValue(parameter.getDescription()));
if (StringUtils.equals(parameter.getType(), "file") ) {
if (StringUtils.equals(parameter.getType(), "file")) {
kv.setType("file");
}
keyValues.add(kv);