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) { 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);
createModule(module); if (isSaved) {
createModule(module);
}
JSONObject requests = testObject.getJSONObject(tag); JSONObject requests = testObject.getJSONObject(tag);
requests.keySet().forEach(requestName -> { requests.keySet().forEach(requestName -> {

View File

@ -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);
} }
createModule(module); if (isSaved) {
createModule(module);
}
return module; return module;
} }

View File

@ -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);
createModule(module); if (isSaved) {
createModule(module);
}
apiDefinition.setModuleId(module.getId()); apiDefinition.setModuleId(module.getId());
}); });
} }
@ -113,8 +115,8 @@ public class Swagger2Parser extends ApiImportAbstractParser {
for (Parameter parameter : parameters) { for (Parameter parameter : parameters) {
switch (parameter.getIn()) { switch (parameter.getIn()) {
case SwaggerParameterType.PATH: case SwaggerParameterType.PATH:
parsePathParameters(parameter, request.getRest()); parsePathParameters(parameter, request.getRest());
break; break;
case SwaggerParameterType.QUERY: case SwaggerParameterType.QUERY:
parseQueryParameters(parameter, request.getArguments()); parseQueryParameters(parameter, request.getArguments());
break; break;
@ -297,14 +299,14 @@ public class Swagger2Parser extends ApiImportAbstractParser {
|| value instanceof DecimalProperty) { || value instanceof DecimalProperty) {
return 0.0; return 0.0;
} else {// todo 其他类型? } else {// todo 其他类型?
return getDefaultStringValue(value.getDescription()); return getDefaultStringValue(value.getDescription());
} }
} }
private void parseFormDataParameters(FormParameter parameter, Body body) { private void parseFormDataParameters(FormParameter parameter, Body body) {
List<KeyValue> keyValues = Optional.ofNullable(body.getKvs()).orElse(new ArrayList<>()); List<KeyValue> keyValues = Optional.ofNullable(body.getKvs()).orElse(new ArrayList<>());
KeyValue kv = new KeyValue(parameter.getName(), "", getDefaultStringValue(parameter.getDescription())); KeyValue kv = new KeyValue(parameter.getName(), "", getDefaultStringValue(parameter.getDescription()));
if (StringUtils.equals(parameter.getType(), "file") ) { if (StringUtils.equals(parameter.getType(), "file")) {
kv.setType("file"); kv.setType("file");
} }
keyValues.add(kv); keyValues.add(kv);