feat(接口测试): 接口定义插件格式导入
This commit is contained in:
parent
0d8d12b17b
commit
91d37ccafe
|
@ -5,12 +5,21 @@ import com.alibaba.fastjson.JSONArray;
|
|||
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.parse.ApiDefinitionImport;
|
||||
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
|
||||
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.commons.constants.MsRequestBodyType;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import io.metersphere.api.service.ApiModuleService;
|
||||
import io.metersphere.base.domain.ApiModule;
|
||||
import io.metersphere.commons.utils.CommonBeanFactory;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class MsParser extends ApiImportAbstractParser {
|
||||
|
||||
|
@ -18,43 +27,72 @@ public class MsParser extends ApiImportAbstractParser {
|
|||
public ApiDefinitionImport parse(InputStream source, ApiTestImportRequest request) {
|
||||
String testStr = getApiTestStr(source);
|
||||
JSONObject testObject = JSONObject.parseObject(testStr, Feature.OrderedField);
|
||||
if (testObject.get("projectName") == null) {
|
||||
testStr = parsePluginFormat(testObject);
|
||||
apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
|
||||
this.projectId = request.getProjectId();
|
||||
if (testObject.get("projectName") != null) {
|
||||
return parseMsFormat(testStr, request);
|
||||
} else {
|
||||
return parsePluginFormat(testObject);
|
||||
}
|
||||
ApiDefinitionImport apiImport = JSON.parseObject(testStr, ApiDefinitionImport.class);
|
||||
}
|
||||
|
||||
private ApiDefinitionImport parseMsFormat(String testStr, ApiTestImportRequest importRequest) {
|
||||
|
||||
ApiDefinitionImport apiDefinitionImport = JSON.parseObject(testStr, ApiDefinitionImport.class);
|
||||
List<ApiDefinitionResult> data = apiDefinitionImport.getData();
|
||||
data.forEach(apiDefinition -> {
|
||||
String id = UUID.randomUUID().toString();
|
||||
apiDefinition.setModuleId(null);
|
||||
apiDefinition.setId(id);
|
||||
apiDefinition.setProjectId(this.projectId);
|
||||
String request = apiDefinition.getRequest();
|
||||
JSONObject requestObj = JSONObject.parseObject(request);
|
||||
requestObj.put("id", id);
|
||||
apiDefinition.setRequest(JSONObject.toJSONString(requestObj));
|
||||
});
|
||||
return apiDefinitionImport;
|
||||
}
|
||||
|
||||
private ApiDefinitionImport parsePluginFormat(JSONObject testObject) {
|
||||
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);
|
||||
JSONObject requests = testObject.getJSONObject(tag);
|
||||
requests.keySet().forEach(requestName -> {
|
||||
|
||||
JSONObject requestObject = requests.getJSONObject(requestName);
|
||||
String path = requestObject.getString("url");
|
||||
String method = requestObject.getString("method");
|
||||
|
||||
MsHTTPSamplerProxy request = buildRequest(requestName, path, method);
|
||||
ApiDefinitionResult apiDefinition = buildApiDefinition(request.getId(), requestName, path, method);
|
||||
apiDefinition.setModuleId(module.getId());
|
||||
apiDefinition.setProjectId(this.projectId);
|
||||
|
||||
parseBody(requestObject, request.getBody());
|
||||
parseHeader(requestObject, request.getHeaders());
|
||||
apiDefinition.setRequest(JSONObject.toJSONString(request));
|
||||
results.add(apiDefinition);
|
||||
});
|
||||
});
|
||||
return apiImport;
|
||||
}
|
||||
|
||||
private String parsePluginFormat( JSONObject testObject) {
|
||||
//插件格式
|
||||
JSONArray scenarios = new JSONArray();
|
||||
testObject.keySet().forEach(scenarioName -> {
|
||||
JSONObject scenario = new JSONObject();
|
||||
scenario.put("name", scenarioName);
|
||||
JSONArray requestsObjects = new JSONArray();
|
||||
JSONObject requestsObject = testObject.getJSONObject(scenarioName);
|
||||
requestsObject.keySet().forEach(requestName -> {
|
||||
JSONObject requestObject = new JSONObject(true);
|
||||
JSONObject requestTmpObject = requestsObject.getJSONObject(requestName);
|
||||
//排序,确保type在第一个,否则转换失败
|
||||
if (StringUtils.isBlank(requestTmpObject.getString("type"))) {
|
||||
requestObject.put("type", RequestType.HTTP);
|
||||
}
|
||||
|
||||
requestTmpObject.keySet().forEach(key -> requestObject.put(key, requestTmpObject.get(key)));
|
||||
requestObject.put("name", requestName);
|
||||
parseBody(requestObject);
|
||||
requestsObjects.add(requestObject);
|
||||
});
|
||||
scenario.put("requests", requestsObjects);
|
||||
scenarios.add(scenario);
|
||||
});
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("scenarios", scenarios);
|
||||
return result.toJSONString();
|
||||
private void parseHeader(JSONObject requestObject, List<KeyValue> msHeaders) {
|
||||
JSONArray headers = requestObject.getJSONArray("headers");
|
||||
if (CollectionUtils.isNotEmpty(headers)) {
|
||||
for (int i = 0; i < headers.size(); i++) {
|
||||
JSONObject header = headers.getJSONObject(i);
|
||||
msHeaders.add(new KeyValue(header.getString("name"), header.getString("value")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void parseBody(JSONObject requestObject) {
|
||||
private void parseBody(JSONObject requestObject, Body msBody) {
|
||||
if (requestObject.containsKey("body")) {
|
||||
Object body = requestObject.get("body");
|
||||
if (body instanceof JSONArray) {
|
||||
|
@ -65,25 +103,18 @@ public class MsParser extends ApiImportAbstractParser {
|
|||
String tmp = bodies.getString(i);
|
||||
bodyStr.append(tmp);
|
||||
}
|
||||
JSONObject bodyObject = new JSONObject();
|
||||
bodyObject.put("raw", bodyStr);
|
||||
bodyObject.put("type", MsRequestBodyType.RAW.value());
|
||||
requestObject.put("body", bodyObject);
|
||||
msBody.setType(Body.RAW);
|
||||
msBody.setRaw(bodyStr.toString());
|
||||
}
|
||||
} else if (body instanceof JSONObject) {
|
||||
JSONObject bodyObj = requestObject.getJSONObject("body");
|
||||
if (bodyObj != null) {
|
||||
JSONArray kvs = new JSONArray();
|
||||
ArrayList<KeyValue> kvs = new ArrayList<>();
|
||||
bodyObj.keySet().forEach(key -> {
|
||||
JSONObject kv = new JSONObject();
|
||||
kv.put("name", key);
|
||||
kv.put("value", bodyObj.getString(key));
|
||||
kvs.add(kv);
|
||||
kvs.add(new KeyValue(key, bodyObj.getString(key)));
|
||||
});
|
||||
JSONObject bodyRes = new JSONObject();
|
||||
bodyRes.put("kvs", kvs);
|
||||
bodyRes.put("type", MsRequestBodyType.KV.value());
|
||||
requestObject.put("body", bodyRes);
|
||||
msBody.setKvs(kvs);
|
||||
msBody.setType(Body.WWW_FROM);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -156,9 +156,9 @@ public class Swagger2Parser extends ApiImportAbstractParser {
|
|||
case "application/xml":
|
||||
bodyType = Body.XML;
|
||||
break;
|
||||
// case "": //todo binary 啥类型
|
||||
// bodyType = Body.BINARY;
|
||||
// break;
|
||||
case "":
|
||||
bodyType = Body.BINARY;
|
||||
break;
|
||||
default:
|
||||
bodyType = Body.RAW;
|
||||
}
|
||||
|
@ -235,7 +235,6 @@ public class Swagger2Parser extends ApiImportAbstractParser {
|
|||
refSet.add(simpleRef);
|
||||
if (model != null) {
|
||||
JSONObject bodyParameters = getBodyParameters(model.getProperties(), refSet);
|
||||
//body.setRaw(bodyParameters.toJSONString());
|
||||
return bodyParameters.toJSONString();
|
||||
}
|
||||
} else if (schema instanceof ArrayModel) {
|
||||
|
@ -250,7 +249,6 @@ public class Swagger2Parser extends ApiImportAbstractParser {
|
|||
Model model = definitions.get(simpleRef);
|
||||
JSONArray propertyList = new JSONArray();
|
||||
propertyList.add(getBodyParameters(model.getProperties(), refSet));
|
||||
// body.setRaw(propertyList.toString());
|
||||
return propertyList.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 8a972a198775b3783ed6e4cef27197e53d1ebdc8
|
||||
Subproject commit a22a3005d9bd254793fcf634d72539cbdf31be3a
|
Loading…
Reference in New Issue