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.JSONObject;
|
||||||
import com.alibaba.fastjson.parser.Feature;
|
import com.alibaba.fastjson.parser.Feature;
|
||||||
import io.metersphere.api.dto.ApiTestImportRequest;
|
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.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.api.dto.scenario.request.RequestType;
|
||||||
import io.metersphere.commons.constants.MsRequestBodyType;
|
import io.metersphere.api.service.ApiModuleService;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import io.metersphere.base.domain.ApiModule;
|
||||||
|
import io.metersphere.commons.utils.CommonBeanFactory;
|
||||||
|
import org.apache.commons.collections.CollectionUtils;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
public class MsParser extends ApiImportAbstractParser {
|
public class MsParser extends ApiImportAbstractParser {
|
||||||
|
|
||||||
|
@ -18,43 +27,72 @@ public class MsParser extends ApiImportAbstractParser {
|
||||||
public ApiDefinitionImport parse(InputStream source, ApiTestImportRequest request) {
|
public ApiDefinitionImport parse(InputStream source, ApiTestImportRequest request) {
|
||||||
String testStr = getApiTestStr(source);
|
String testStr = getApiTestStr(source);
|
||||||
JSONObject testObject = JSONObject.parseObject(testStr, Feature.OrderedField);
|
JSONObject testObject = JSONObject.parseObject(testStr, Feature.OrderedField);
|
||||||
if (testObject.get("projectName") == null) {
|
apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
|
||||||
testStr = parsePluginFormat(testObject);
|
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;
|
return apiImport;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String parsePluginFormat( JSONObject testObject) {
|
private void parseHeader(JSONObject requestObject, List<KeyValue> msHeaders) {
|
||||||
//插件格式
|
JSONArray headers = requestObject.getJSONArray("headers");
|
||||||
JSONArray scenarios = new JSONArray();
|
if (CollectionUtils.isNotEmpty(headers)) {
|
||||||
testObject.keySet().forEach(scenarioName -> {
|
for (int i = 0; i < headers.size(); i++) {
|
||||||
JSONObject scenario = new JSONObject();
|
JSONObject header = headers.getJSONObject(i);
|
||||||
scenario.put("name", scenarioName);
|
msHeaders.add(new KeyValue(header.getString("name"), header.getString("value")));
|
||||||
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)));
|
private void parseBody(JSONObject requestObject, Body msBody) {
|
||||||
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 parseBody(JSONObject requestObject) {
|
|
||||||
if (requestObject.containsKey("body")) {
|
if (requestObject.containsKey("body")) {
|
||||||
Object body = requestObject.get("body");
|
Object body = requestObject.get("body");
|
||||||
if (body instanceof JSONArray) {
|
if (body instanceof JSONArray) {
|
||||||
|
@ -65,25 +103,18 @@ public class MsParser extends ApiImportAbstractParser {
|
||||||
String tmp = bodies.getString(i);
|
String tmp = bodies.getString(i);
|
||||||
bodyStr.append(tmp);
|
bodyStr.append(tmp);
|
||||||
}
|
}
|
||||||
JSONObject bodyObject = new JSONObject();
|
msBody.setType(Body.RAW);
|
||||||
bodyObject.put("raw", bodyStr);
|
msBody.setRaw(bodyStr.toString());
|
||||||
bodyObject.put("type", MsRequestBodyType.RAW.value());
|
|
||||||
requestObject.put("body", bodyObject);
|
|
||||||
}
|
}
|
||||||
} else if (body instanceof JSONObject) {
|
} else if (body instanceof JSONObject) {
|
||||||
JSONObject bodyObj = requestObject.getJSONObject("body");
|
JSONObject bodyObj = requestObject.getJSONObject("body");
|
||||||
if (bodyObj != null) {
|
if (bodyObj != null) {
|
||||||
JSONArray kvs = new JSONArray();
|
ArrayList<KeyValue> kvs = new ArrayList<>();
|
||||||
bodyObj.keySet().forEach(key -> {
|
bodyObj.keySet().forEach(key -> {
|
||||||
JSONObject kv = new JSONObject();
|
kvs.add(new KeyValue(key, bodyObj.getString(key)));
|
||||||
kv.put("name", key);
|
|
||||||
kv.put("value", bodyObj.getString(key));
|
|
||||||
kvs.add(kv);
|
|
||||||
});
|
});
|
||||||
JSONObject bodyRes = new JSONObject();
|
msBody.setKvs(kvs);
|
||||||
bodyRes.put("kvs", kvs);
|
msBody.setType(Body.WWW_FROM);
|
||||||
bodyRes.put("type", MsRequestBodyType.KV.value());
|
|
||||||
requestObject.put("body", bodyRes);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -156,9 +156,9 @@ public class Swagger2Parser extends ApiImportAbstractParser {
|
||||||
case "application/xml":
|
case "application/xml":
|
||||||
bodyType = Body.XML;
|
bodyType = Body.XML;
|
||||||
break;
|
break;
|
||||||
// case "": //todo binary 啥类型
|
case "":
|
||||||
// bodyType = Body.BINARY;
|
bodyType = Body.BINARY;
|
||||||
// break;
|
break;
|
||||||
default:
|
default:
|
||||||
bodyType = Body.RAW;
|
bodyType = Body.RAW;
|
||||||
}
|
}
|
||||||
|
@ -235,7 +235,6 @@ public class Swagger2Parser extends ApiImportAbstractParser {
|
||||||
refSet.add(simpleRef);
|
refSet.add(simpleRef);
|
||||||
if (model != null) {
|
if (model != null) {
|
||||||
JSONObject bodyParameters = getBodyParameters(model.getProperties(), refSet);
|
JSONObject bodyParameters = getBodyParameters(model.getProperties(), refSet);
|
||||||
//body.setRaw(bodyParameters.toJSONString());
|
|
||||||
return bodyParameters.toJSONString();
|
return bodyParameters.toJSONString();
|
||||||
}
|
}
|
||||||
} else if (schema instanceof ArrayModel) {
|
} else if (schema instanceof ArrayModel) {
|
||||||
|
@ -250,7 +249,6 @@ public class Swagger2Parser extends ApiImportAbstractParser {
|
||||||
Model model = definitions.get(simpleRef);
|
Model model = definitions.get(simpleRef);
|
||||||
JSONArray propertyList = new JSONArray();
|
JSONArray propertyList = new JSONArray();
|
||||||
propertyList.add(getBodyParameters(model.getProperties(), refSet));
|
propertyList.add(getBodyParameters(model.getProperties(), refSet));
|
||||||
// body.setRaw(propertyList.toString());
|
|
||||||
return propertyList.toString();
|
return propertyList.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 8a972a198775b3783ed6e4cef27197e53d1ebdc8
|
Subproject commit a22a3005d9bd254793fcf634d72539cbdf31be3a
|
Loading…
Reference in New Issue