refactor: 导入接口到指定目录下

This commit is contained in:
chenjianxing 2021-01-06 12:04:21 +08:00
parent 2c2f8b8476
commit 55e8d9fec6
6 changed files with 73 additions and 84 deletions

View File

@ -2,6 +2,7 @@ package io.metersphere.api.parse;
import io.metersphere.api.dto.ApiTestImportRequest;
import io.metersphere.api.dto.definition.ApiDefinitionResult;
import io.metersphere.api.dto.definition.ApiModuleDTO;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.KeyValue;
@ -11,6 +12,8 @@ import io.metersphere.api.dto.scenario.request.RequestType;
import io.metersphere.api.service.ApiModuleService;
import io.metersphere.base.domain.ApiModule;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.SessionUtils;
import org.apache.commons.collections.CollectionUtils;
@ -60,6 +63,32 @@ public abstract class ApiImportAbstractParser implements ApiImportParser {
}
}
protected ApiModule getSelectModule(String moduleId) {
apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
if (StringUtils.isNotBlank(moduleId)) {
ApiModule module = new ApiModule();
ApiModuleDTO moduleDTO = apiModuleService.getNode(moduleId);
BeanUtils.copyBean(module, moduleDTO);
return module;
}
return null;
}
protected ApiModule buildModule(ApiModule parentModule, String name, boolean isSaved) {
apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
ApiModule module;
if (parentModule != null) {
module = apiModuleService.getNewModule(name, this.projectId, parentModule.getLevel() + 1);
module.setParentId(parentModule.getId());
} else {
module = apiModuleService.getNewModule(name, this.projectId, 1);
}
if (isSaved) {
createModule(module);
}
return module;
}
protected void createModule(ApiModule module) {
module.setProtocol(RequestType.HTTP);
List<ApiModule> apiModules = apiModuleService.selectSameModule(module);

View File

@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import io.metersphere.api.dto.ApiTestImportRequest;
import io.metersphere.api.dto.definition.ApiDefinitionResult;
import io.metersphere.api.dto.definition.ApiModuleDTO;
import io.metersphere.api.dto.definition.parse.ApiDefinitionImport;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.scenario.Body;
@ -31,7 +32,7 @@ public class MsParser extends ApiImportAbstractParser {
if (testObject.get("projectName") != null) {
return parseMsFormat(testStr, request);
} else {
return parsePluginFormat(testObject, request.isSaved());
return parsePluginFormat(testObject, request);
}
}
@ -44,7 +45,7 @@ public class MsParser extends ApiImportAbstractParser {
if (StringUtils.isBlank(apiDefinition.getModulePath())) {
apiDefinition.setModuleId(null);
}
parseModule(apiDefinition, importRequest.isSaved());
parseModule(apiDefinition, importRequest);
apiDefinition.setId(id);
apiDefinition.setProjectId(this.projectId);
String request = apiDefinition.getRequest();
@ -55,16 +56,16 @@ public class MsParser extends ApiImportAbstractParser {
return apiDefinitionImport;
}
private ApiDefinitionImport parsePluginFormat(JSONObject testObject, boolean isSaved) {
private ApiDefinitionImport parsePluginFormat(JSONObject testObject, ApiTestImportRequest importRequest) {
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);
if (isSaved) {
createModule(module);
}
ApiModule parentModule = getSelectModule(importRequest.getModuleId());
ApiModule module = buildModule(parentModule, tag, importRequest.isSaved());
JSONObject requests = testObject.getJSONObject(tag);
requests.keySet().forEach(requestName -> {
@ -125,7 +126,7 @@ public class MsParser extends ApiImportAbstractParser {
}
private void parseModule(ApiDefinitionResult apiDefinition, Boolean isSaved) {
private void parseModule(ApiDefinitionResult apiDefinition, ApiTestImportRequest importRequest) {
String modulePath = apiDefinition.getModulePath();
if (StringUtils.isBlank(modulePath)) {
return;
@ -137,29 +138,14 @@ public class MsParser extends ApiImportAbstractParser {
modulePath = modulePath.substring(0, modulePath.length() - 1);
}
List<String> modules = Arrays.asList(modulePath.split("/"));
ApiModule parent = null;
ApiModule parent = getSelectModule(importRequest.getModuleId());
Iterator<String> iterator = modules.iterator();
while (iterator.hasNext()) {
String item = iterator.next();
parent = buildModule(item, parent, isSaved);
parent = buildModule(parent, item, importRequest.isSaved());
if (!iterator.hasNext()) {
apiDefinition.setModuleId(parent.getId());
}
}
}
private ApiModule buildModule(String name, ApiModule parentModule, boolean isSaved) {
apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
ApiModule module;
if (parentModule != null) {
module = apiModuleService.getNewModule(name, this.projectId, parentModule.getLevel() + 1);
module.setParentId(parentModule.getId());
} else {
module = apiModuleService.getNewModule(name, this.projectId, 1);
}
if (isSaved) {
createModule(module);
}
return module;
}
}

View File

@ -9,11 +9,9 @@ import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.parse.postman.*;
import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.service.ApiModuleService;
import io.metersphere.base.domain.ApiModule;
import io.metersphere.commons.constants.MsRequestBodyType;
import io.metersphere.commons.constants.PostmanRequestBodyMode;
import io.metersphere.commons.utils.CommonBeanFactory;
import org.apache.commons.lang3.StringUtils;
import java.io.InputStream;
@ -30,7 +28,7 @@ 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, request.isSaved()), request.isSaved());
parseItem(postmanCollection.getItem(), variables, results, buildModule(getSelectModule(request.getModuleId()), postmanCollection.getInfo().getName(), request.isSaved()), request.isSaved());
apiImport.setData(results);
return apiImport;
}
@ -39,7 +37,7 @@ public class PostmanParser extends ApiImportAbstractParser {
for (PostmanItem item : items) {
List<PostmanItem> childItems = item.getItem();
if (childItems != null) {
ApiModule module = buildModule(item.getName(), parentModule, isSaved);
ApiModule module = buildModule(parentModule, item.getName() , isSaved);
parseItem(childItems, variables, results, module, isSaved);
} else {
ApiDefinitionResult request = parsePostman(item);
@ -53,21 +51,6 @@ public class PostmanParser extends ApiImportAbstractParser {
}
}
private ApiModule buildModule(String name, ApiModule parentModule, boolean isSaved) {
apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
ApiModule module;
if (parentModule != null) {
module = apiModuleService.getNewModule(name, this.projectId, parentModule.getLevel() + 1);
module.setParentId(parentModule.getId());
} else {
module = apiModuleService.getNewModule(name, this.projectId, 1);
}
if (isSaved) {
createModule(module);
}
return module;
}
private ApiDefinitionResult parsePostman(PostmanItem requestItem) {
PostmanRequest requestDesc = requestItem.getRequest();
if (requestDesc == null) {

View File

@ -11,10 +11,8 @@ import io.metersphere.api.dto.definition.response.HttpResponse;
import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.request.RequestType;
import io.metersphere.api.service.ApiModuleService;
import io.metersphere.base.domain.ApiModule;
import io.metersphere.commons.constants.SwaggerParameterType;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.swagger.models.*;
import io.swagger.models.parameters.*;
import io.swagger.models.properties.*;
@ -25,7 +23,7 @@ import org.apache.commons.lang3.StringUtils;
import java.io.InputStream;
import java.util.*;
public class Swagger2Parser extends ApiImportAbstractParser {
public class Swagger2Parser extends SwaggerAbstractParser {
private Map<String, Model> definitions = null;
@ -47,11 +45,11 @@ public class Swagger2Parser extends ApiImportAbstractParser {
ApiDefinitionImport definitionImport = new ApiDefinitionImport();
this.projectId = request.getProjectId();
definitionImport.setData(parseRequests(swagger, request.isSaved()));
definitionImport.setData(parseRequests(swagger, request));
return definitionImport;
}
private List<ApiDefinitionResult> parseRequests(Swagger swagger, boolean isSaved) {
private List<ApiDefinitionResult> parseRequests(Swagger swagger, ApiTestImportRequest importRequest) {
Map<String, Path> paths = swagger.getPaths();
Set<String> pathNames = paths.keySet();
@ -59,6 +57,8 @@ public class Swagger2Parser extends ApiImportAbstractParser {
List<ApiDefinitionResult> results = new ArrayList<>();
ApiModule parentNode = getSelectModule(importRequest.getModuleId());
for (String pathName : pathNames) {
Path path = paths.get(pathName);
Map<HttpMethod, Operation> operationMap = path.getOperationMap();
@ -70,7 +70,7 @@ public class Swagger2Parser extends ApiImportAbstractParser {
parseParameters(operation, request);
apiDefinition.setRequest(JSON.toJSONString(request));
apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation.getResponses())));
buildModule(apiDefinition, operation, isSaved);
buildModule(parentNode, apiDefinition, operation.getTags(), importRequest.isSaved());
results.add(apiDefinition);
}
}
@ -79,20 +79,6 @@ public class Swagger2Parser extends ApiImportAbstractParser {
return results;
}
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);
if (isSaved) {
createModule(module);
}
apiDefinition.setModuleId(module.getId());
});
}
}
private ApiDefinitionResult buildApiDefinition(String id, Operation operation, String path, String method) {
String name = "";
if (StringUtils.isNotBlank(operation.getSummary())) {

View File

@ -11,10 +11,8 @@ import io.metersphere.api.dto.definition.response.HttpResponse;
import io.metersphere.api.dto.scenario.Body;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.request.RequestType;
import io.metersphere.api.service.ApiModuleService;
import io.metersphere.base.domain.ApiModule;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.XMLUtils;
import io.swagger.parser.OpenAPIParser;
@ -34,7 +32,7 @@ import java.io.InputStream;
import java.util.*;
public class Swagger3Parser extends ApiImportAbstractParser {
public class Swagger3Parser extends SwaggerAbstractParser {
private Components components;
@ -67,11 +65,11 @@ public class Swagger3Parser extends ApiImportAbstractParser {
ApiDefinitionImport definitionImport = new ApiDefinitionImport();
this.projectId = request.getProjectId();
definitionImport.setData(parseRequests(openAPI, request.isSaved()));
definitionImport.setData(parseRequests(openAPI, request));
return definitionImport;
}
private List<ApiDefinitionResult> parseRequests(OpenAPI openAPI, boolean isSaved) {
private List<ApiDefinitionResult> parseRequests(OpenAPI openAPI, ApiTestImportRequest importRequest) {
Paths paths = openAPI.getPaths();
Set<String> pathNames = paths.keySet();
@ -80,6 +78,8 @@ public class Swagger3Parser extends ApiImportAbstractParser {
List<ApiDefinitionResult> results = new ArrayList<>();
ApiModule parentNode = getSelectModule(importRequest.getModuleId());
for (String pathName : pathNames) {
PathItem pathItem = paths.get(pathName);
@ -102,7 +102,7 @@ public class Swagger3Parser extends ApiImportAbstractParser {
parseRequestBody(operation.getRequestBody(), request.getBody());
apiDefinition.setRequest(JSON.toJSONString(request));
apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation.getResponses())));
buildModule(apiDefinition, operation, isSaved);
buildModule(parentNode, apiDefinition, operation.getTags(), importRequest.isSaved());
results.add(apiDefinition);
}
}
@ -111,20 +111,6 @@ public class Swagger3Parser extends ApiImportAbstractParser {
return results;
}
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);
if (isSaved) {
createModule(module);
}
apiDefinition.setModuleId(module.getId());
});
}
}
private ApiDefinitionResult buildApiDefinition(String id, Operation operation, String path, String method) {
String name = "";
if (StringUtils.isNotBlank(operation.getSummary())) {

View File

@ -0,0 +1,19 @@
package io.metersphere.api.parse;
import io.metersphere.api.dto.definition.ApiDefinitionResult;
import io.metersphere.base.domain.ApiModule;
import java.util.List;
public abstract class SwaggerAbstractParser extends ApiImportAbstractParser {
protected void buildModule(ApiModule parentModule, ApiDefinitionResult apiDefinition, List<String> tags, boolean isSaved) {
if (tags != null) {
tags.forEach(tag -> {
ApiModule module = buildModule(parentModule, tag, isSaved);
apiDefinition.setModuleId(module.getId());
});
}
}
}