Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
c52f7abeb0
|
@ -96,10 +96,10 @@ public class APITestController {
|
|||
return apiTestService.run(request);
|
||||
}
|
||||
|
||||
@PostMapping("/import/{platform}/{projectId}")
|
||||
@PostMapping("/import/{platform}")
|
||||
@RequiresRoles(value = {RoleConstants.TEST_USER, RoleConstants.TEST_MANAGER}, logical = Logical.OR)
|
||||
public ApiTest testCaseImport(MultipartFile file, @PathVariable String platform, @PathVariable String projectId) {
|
||||
return apiTestService.apiTestImport(file, platform, projectId);
|
||||
public ApiTest testCaseImport(MultipartFile file, @PathVariable String platform) {
|
||||
return apiTestService.apiTestImport(file, platform);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package io.metersphere.api.dto.parse.postman;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class PostmanCollection {
|
||||
|
||||
private PostmanCollectionInfo info;
|
||||
|
||||
private List<PostmanItem> item;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package io.metersphere.api.dto.parse.postman;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class PostmanCollectionInfo {
|
||||
|
||||
@JSONField(name = "_postman_id")
|
||||
private String postmanId;
|
||||
private String name;
|
||||
private String schema;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package io.metersphere.api.dto.parse.postman;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class PostmanItem {
|
||||
|
||||
private String name;
|
||||
private PostmanRequest request;
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package io.metersphere.api.dto.parse.postman;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class PostmanKeyValue {
|
||||
private String key;
|
||||
private String value;
|
||||
private String type;
|
||||
|
||||
public PostmanKeyValue() {
|
||||
}
|
||||
|
||||
public PostmanKeyValue(String key, String value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package io.metersphere.api.dto.parse.postman;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class PostmanRequest {
|
||||
|
||||
private String method;
|
||||
private String schema;
|
||||
private List<PostmanKeyValue> header;
|
||||
private JSONObject body;
|
||||
private JSONObject auth;
|
||||
private PostmanUrl url;
|
||||
private String description;
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package io.metersphere.api.dto.parse.postman;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class PostmanUrl {
|
||||
|
||||
private String raw;
|
||||
private String protocol;
|
||||
private String port;
|
||||
private List<PostmanKeyValue> query;
|
||||
}
|
|
@ -6,4 +6,12 @@ import lombok.Data;
|
|||
public class KeyValue {
|
||||
private String name;
|
||||
private String value;
|
||||
|
||||
public KeyValue() {
|
||||
}
|
||||
|
||||
public KeyValue(String name, String value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
package io.metersphere.api.parse;
|
||||
|
||||
import io.metersphere.commons.exception.MSException;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public abstract class ApiImportAbstractParser implements ApiImportParser {
|
||||
|
||||
protected String getApiTestStr(InputStream source) {
|
||||
BufferedReader bufferedReader = null;
|
||||
StringBuilder testStr = null;
|
||||
try {
|
||||
bufferedReader = new BufferedReader(new InputStreamReader(source, "UTF-8"));
|
||||
testStr = new StringBuilder();
|
||||
String inputStr = null;
|
||||
while ((inputStr = bufferedReader.readLine()) != null) {
|
||||
testStr.append(inputStr);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
MSException.throwException(e.getMessage());
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
} finally {
|
||||
try {
|
||||
source.close();
|
||||
} catch (IOException e) {
|
||||
MSException.throwException(e.getMessage());
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
return testStr.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -10,6 +10,8 @@ public class ApiImportParserFactory {
|
|||
public static ApiImportParser getApiImportParser(String platform) {
|
||||
if (StringUtils.equals(ApiImportPlatform.Metersphere.name(), platform)) {
|
||||
return new MsParser();
|
||||
} else if (StringUtils.equals(ApiImportPlatform.Postman.name(), platform)) {
|
||||
return new PostmanParser();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -7,29 +7,11 @@ import io.metersphere.commons.utils.LogUtil;
|
|||
|
||||
import java.io.*;
|
||||
|
||||
public class MsParser implements ApiImportParser {
|
||||
public class MsParser extends ApiImportAbstractParser {
|
||||
|
||||
@Override
|
||||
public ApiImport parse(InputStream source) {BufferedReader bufferedReader = null;
|
||||
StringBuilder testStr = null;
|
||||
try {
|
||||
bufferedReader = new BufferedReader(new InputStreamReader(source, "UTF-8"));
|
||||
testStr = new StringBuilder();
|
||||
String inputStr = null;
|
||||
while ((inputStr = bufferedReader.readLine()) != null) {
|
||||
testStr.append(inputStr);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
MSException.throwException(e.getMessage());
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
} finally {
|
||||
try {
|
||||
source.close();
|
||||
} catch (IOException e) {
|
||||
MSException.throwException(e.getMessage());
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
public ApiImport parse(InputStream source) {
|
||||
String testStr = getApiTestStr(source);
|
||||
return JSON.parseObject(testStr.toString(), ApiImport.class);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,262 @@
|
|||
package io.metersphere.api.parse;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import io.metersphere.api.dto.parse.ApiImport;
|
||||
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.dto.scenario.Request;
|
||||
import io.metersphere.api.dto.scenario.Scenario;
|
||||
import io.metersphere.commons.constants.MsRequestBodyType;
|
||||
import io.metersphere.commons.constants.PostmanRequestBodyMode;
|
||||
import io.metersphere.commons.exception.MSException;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PostmanParser extends ApiImportAbstractParser {
|
||||
|
||||
@Override
|
||||
public ApiImport parse(InputStream source) {
|
||||
String testStr = getApiTestStr(source);
|
||||
|
||||
// String testStr = "{\n" +
|
||||
// "\t\"info\": {\n" +
|
||||
// "\t\t\"_postman_id\": \"9721cd51-8626-4f61-9ac1-e77b8399cca8\",\n" +
|
||||
// "\t\t\"name\": \"test\",\n" +
|
||||
// "\t\t\"schema\": \"https://schema.getpostman.com/json/collection/v2.1.0/collection.json\"\n" +
|
||||
// "\t},\n" +
|
||||
// "\t\"item\": [\n" +
|
||||
// "\t\t{\n" +
|
||||
// "\t\t\t\"name\": \"test\",\n" +
|
||||
// "\t\t\t\"request\": {\n" +
|
||||
// "\t\t\t\t\"auth\": {\n" +
|
||||
// "\t\t\t\t\t\"type\": \"basic\",\n" +
|
||||
// "\t\t\t\t\t\"basic\": [\n" +
|
||||
// "\t\t\t\t\t\t{\n" +
|
||||
// "\t\t\t\t\t\t\t\"key\": \"password\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"value\": \"test\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
|
||||
// "\t\t\t\t\t\t},\n" +
|
||||
// "\t\t\t\t\t\t{\n" +
|
||||
// "\t\t\t\t\t\t\t\"key\": \"username\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"value\": \"test\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
|
||||
// "\t\t\t\t\t\t}\n" +
|
||||
// "\t\t\t\t\t]\n" +
|
||||
// "\t\t\t\t},\n" +
|
||||
// "\t\t\t\t\"method\": \"POST\",\n" +
|
||||
// "\t\t\t\t\"header\": [\n" +
|
||||
// "\t\t\t\t\t{\n" +
|
||||
// "\t\t\t\t\t\t\"key\": \"aaa\",\n" +
|
||||
// "\t\t\t\t\t\t\"value\": \"testH\",\n" +
|
||||
// "\t\t\t\t\t\t\"type\": \"text\"\n" +
|
||||
// "\t\t\t\t\t}\n" +
|
||||
// "\t\t\t\t],\n" +
|
||||
// "\t\t\t\t\"body\": {\n" +
|
||||
// "\t\t\t\t\t\"mode\": \"raw\",\n" +
|
||||
// "\t\t\t\t\t\"raw\": \"<html>\\n</html>\\n\",\n" +
|
||||
// "\t\t\t\t\t\"options\": {\n" +
|
||||
// "\t\t\t\t\t\t\"raw\": {\n" +
|
||||
// "\t\t\t\t\t\t\t\"language\": \"html\"\n" +
|
||||
// "\t\t\t\t\t\t}\n" +
|
||||
// "\t\t\t\t\t}\n" +
|
||||
// "\t\t\t\t},\n" +
|
||||
// "\t\t\t\t\"url\": {\n" +
|
||||
// "\t\t\t\t\t\"raw\": \"https://localhost:8080?tset=test\",\n" +
|
||||
// "\t\t\t\t\t\"protocol\": \"https\",\n" +
|
||||
// "\t\t\t\t\t\"host\": [\n" +
|
||||
// "\t\t\t\t\t\t\"localhost\"\n" +
|
||||
// "\t\t\t\t\t],\n" +
|
||||
// "\t\t\t\t\t\"port\": \"8080\",\n" +
|
||||
// "\t\t\t\t\t\"query\": [\n" +
|
||||
// "\t\t\t\t\t\t{\n" +
|
||||
// "\t\t\t\t\t\t\t\"key\": \"tset\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"value\": \"test\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"description\": \"test\"\n" +
|
||||
// "\t\t\t\t\t\t}\n" +
|
||||
// "\t\t\t\t\t]\n" +
|
||||
// "\t\t\t\t},\n" +
|
||||
// "\t\t\t\t\"description\": \"dd\"\n" +
|
||||
// "\t\t\t},\n" +
|
||||
// "\t\t\t\"response\": []\n" +
|
||||
// "\t\t},\n" +
|
||||
// "\t\t{\n" +
|
||||
// "\t\t\t\"name\": \"test Copy\",\n" +
|
||||
// "\t\t\t\"request\": {\n" +
|
||||
// "\t\t\t\t\"auth\": {\n" +
|
||||
// "\t\t\t\t\t\"type\": \"basic\",\n" +
|
||||
// "\t\t\t\t\t\"basic\": [\n" +
|
||||
// "\t\t\t\t\t\t{\n" +
|
||||
// "\t\t\t\t\t\t\t\"key\": \"password\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"value\": \"test\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
|
||||
// "\t\t\t\t\t\t},\n" +
|
||||
// "\t\t\t\t\t\t{\n" +
|
||||
// "\t\t\t\t\t\t\t\"key\": \"username\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"value\": \"test\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
|
||||
// "\t\t\t\t\t\t}\n" +
|
||||
// "\t\t\t\t\t]\n" +
|
||||
// "\t\t\t\t},\n" +
|
||||
// "\t\t\t\t\"method\": \"POST\",\n" +
|
||||
// "\t\t\t\t\"header\": [\n" +
|
||||
// "\t\t\t\t\t{\n" +
|
||||
// "\t\t\t\t\t\t\"key\": \"testH\",\n" +
|
||||
// "\t\t\t\t\t\t\"value\": \"testH\",\n" +
|
||||
// "\t\t\t\t\t\t\"type\": \"text\"\n" +
|
||||
// "\t\t\t\t\t}\n" +
|
||||
// "\t\t\t\t],\n" +
|
||||
// "\t\t\t\t\"body\": {\n" +
|
||||
// "\t\t\t\t\t\"mode\": \"raw\",\n" +
|
||||
// "\t\t\t\t\t\"raw\": \"{\\\"name\\\":\\\"test\\\"}\\n\",\n" +
|
||||
// "\t\t\t\t\t\"options\": {\n" +
|
||||
// "\t\t\t\t\t\t\"raw\": {\n" +
|
||||
// "\t\t\t\t\t\t\t\"language\": \"json\"\n" +
|
||||
// "\t\t\t\t\t\t}\n" +
|
||||
// "\t\t\t\t\t}\n" +
|
||||
// "\t\t\t\t},\n" +
|
||||
// "\t\t\t\t\"url\": {\n" +
|
||||
// "\t\t\t\t\t\"raw\": \"http://localhost:8081\",\n" +
|
||||
// "\t\t\t\t\t\"protocol\": \"http\",\n" +
|
||||
// "\t\t\t\t\t\"host\": [\n" +
|
||||
// "\t\t\t\t\t\t\"localhost\"\n" +
|
||||
// "\t\t\t\t\t],\n" +
|
||||
// "\t\t\t\t\t\"port\": \"8081\"\n" +
|
||||
// "\t\t\t\t},\n" +
|
||||
// "\t\t\t\t\"description\": \"dd\"\n" +
|
||||
// "\t\t\t},\n" +
|
||||
// "\t\t\t\"response\": []\n" +
|
||||
// "\t\t},\n" +
|
||||
// "\t\t{\n" +
|
||||
// "\t\t\t\"name\": \"test Copy Copy\",\n" +
|
||||
// "\t\t\t\"request\": {\n" +
|
||||
// "\t\t\t\t\"auth\": {\n" +
|
||||
// "\t\t\t\t\t\"type\": \"basic\",\n" +
|
||||
// "\t\t\t\t\t\"basic\": [\n" +
|
||||
// "\t\t\t\t\t\t{\n" +
|
||||
// "\t\t\t\t\t\t\t\"key\": \"password\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"value\": \"test\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
|
||||
// "\t\t\t\t\t\t},\n" +
|
||||
// "\t\t\t\t\t\t{\n" +
|
||||
// "\t\t\t\t\t\t\t\"key\": \"username\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"value\": \"test\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"type\": \"string\"\n" +
|
||||
// "\t\t\t\t\t\t}\n" +
|
||||
// "\t\t\t\t\t]\n" +
|
||||
// "\t\t\t\t},\n" +
|
||||
// "\t\t\t\t\"method\": \"POST\",\n" +
|
||||
// "\t\t\t\t\"header\": [\n" +
|
||||
// "\t\t\t\t\t{\n" +
|
||||
// "\t\t\t\t\t\t\"key\": \"testH\",\n" +
|
||||
// "\t\t\t\t\t\t\"value\": \"testH\",\n" +
|
||||
// "\t\t\t\t\t\t\"type\": \"text\"\n" +
|
||||
// "\t\t\t\t\t}\n" +
|
||||
// "\t\t\t\t],\n" +
|
||||
// "\t\t\t\t\"body\": {\n" +
|
||||
// "\t\t\t\t\t\"mode\": \"urlencoded\",\n" +
|
||||
// "\t\t\t\t\t\"urlencoded\": [\n" +
|
||||
// "\t\t\t\t\t\t{\n" +
|
||||
// "\t\t\t\t\t\t\t\"key\": \"test\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"value\": \"tset\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"description\": \"dd\",\n" +
|
||||
// "\t\t\t\t\t\t\t\"type\": \"text\"\n" +
|
||||
// "\t\t\t\t\t\t}\n" +
|
||||
// "\t\t\t\t\t],\n" +
|
||||
// "\t\t\t\t\t\"options\": {\n" +
|
||||
// "\t\t\t\t\t\t\"raw\": {\n" +
|
||||
// "\t\t\t\t\t\t\t\"language\": \"json\"\n" +
|
||||
// "\t\t\t\t\t\t}\n" +
|
||||
// "\t\t\t\t\t}\n" +
|
||||
// "\t\t\t\t},\n" +
|
||||
// "\t\t\t\t\"url\": {\n" +
|
||||
// "\t\t\t\t\t\"raw\": \"http://localhost:8081\",\n" +
|
||||
// "\t\t\t\t\t\"protocol\": \"http\",\n" +
|
||||
// "\t\t\t\t\t\"host\": [\n" +
|
||||
// "\t\t\t\t\t\t\"localhost\"\n" +
|
||||
// "\t\t\t\t\t],\n" +
|
||||
// "\t\t\t\t\t\"port\": \"8081\"\n" +
|
||||
// "\t\t\t\t},\n" +
|
||||
// "\t\t\t\t\"description\": \"dd\"\n" +
|
||||
// "\t\t\t},\n" +
|
||||
// "\t\t\t\"response\": []\n" +
|
||||
// "\t\t}\n" +
|
||||
// "\t],\n" +
|
||||
// "\t\"protocolProfileBehavior\": {}\n" +
|
||||
// "}";
|
||||
|
||||
PostmanCollection postmanCollection = JSON.parseObject(testStr.toString(), PostmanCollection.class);
|
||||
PostmanCollectionInfo info = postmanCollection.getInfo();
|
||||
List<Request> requests = parseRequests(postmanCollection);
|
||||
ApiImport apiImport = new ApiImport();
|
||||
List<Scenario> scenarios = new ArrayList<>();
|
||||
Scenario scenario = new Scenario();
|
||||
scenario.setRequests(requests);
|
||||
scenario.setName(info.getName());
|
||||
scenarios.add(scenario);
|
||||
apiImport.setScenarios(scenarios);
|
||||
return apiImport;
|
||||
}
|
||||
|
||||
private List<KeyValue> parseKeyValue(List<PostmanKeyValue> postmanKeyValues) {
|
||||
if (postmanKeyValues == null) {
|
||||
return null;
|
||||
}
|
||||
List<KeyValue> keyValues = new ArrayList<>();
|
||||
postmanKeyValues.forEach(item -> {
|
||||
keyValues.add(new KeyValue(item.getKey(), item.getValue()));
|
||||
});
|
||||
return keyValues;
|
||||
}
|
||||
|
||||
private List<Request> parseRequests(PostmanCollection postmanCollection) {
|
||||
List<PostmanItem> item = postmanCollection.getItem();
|
||||
List<Request> requests = new ArrayList<>();
|
||||
for (PostmanItem requestItem : item) {
|
||||
Request request = new Request();
|
||||
PostmanRequest requestDesc = requestItem.getRequest();
|
||||
PostmanUrl url = requestDesc.getUrl();
|
||||
request.setName(requestItem.getName());
|
||||
request.setUrl(url.getRaw());
|
||||
request.setUseEnvironment(false);
|
||||
request.setMethod(requestDesc.getMethod());
|
||||
request.setHeaders(parseKeyValue(requestDesc.getHeader()));
|
||||
request.setParameters(parseKeyValue(url.getQuery()));
|
||||
Body body = new Body();
|
||||
JSONObject postmanBody = requestDesc.getBody();
|
||||
String bodyMode = postmanBody.getString("mode");
|
||||
if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.RAW.value())) {
|
||||
body.setRaw(postmanBody.getString(bodyMode));
|
||||
body.setType(MsRequestBodyType.RAW.value());
|
||||
String contentType = postmanBody.getJSONObject("options").getJSONObject("raw").getString("language");
|
||||
List<KeyValue> headers = request.getHeaders();
|
||||
boolean hasContentType = false;
|
||||
for (KeyValue header : headers) {
|
||||
if (StringUtils.equalsIgnoreCase(header.getName(), "Content-Type")) {
|
||||
hasContentType = true;
|
||||
}
|
||||
}
|
||||
if (!hasContentType) {
|
||||
headers.add(new KeyValue("Content-Type", contentType));
|
||||
}
|
||||
} else if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.FORM_DATA.value()) || StringUtils.equals(bodyMode, PostmanRequestBodyMode.URLENCODED.value())) {
|
||||
List<PostmanKeyValue> postmanKeyValues = JSON.parseArray(postmanBody.getString(bodyMode), PostmanKeyValue.class);
|
||||
body.setType(MsRequestBodyType.KV.value());
|
||||
body.setKvs(parseKeyValue(postmanKeyValues));
|
||||
}
|
||||
request.setBody(body);
|
||||
requests.add(request);
|
||||
}
|
||||
return requests;
|
||||
}
|
||||
|
||||
}
|
|
@ -19,6 +19,7 @@ import io.metersphere.commons.constants.ScheduleGroup;
|
|||
import io.metersphere.commons.constants.ScheduleType;
|
||||
import io.metersphere.commons.exception.MSException;
|
||||
import io.metersphere.commons.utils.BeanUtils;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.commons.utils.ServiceUtils;
|
||||
import io.metersphere.commons.utils.SessionUtils;
|
||||
import io.metersphere.i18n.Translator;
|
||||
|
@ -260,24 +261,23 @@ public class APITestService {
|
|||
scheduleService.addOrUpdateCronJob(request, ApiTestJob.getJobKey(request.getResourceId()), ApiTestJob.getTriggerKey(request.getResourceId()), ApiTestJob.class);
|
||||
}
|
||||
|
||||
public ApiTest apiTestImport(MultipartFile file, String platform, String projectId) {
|
||||
public ApiTest apiTestImport(MultipartFile file, String platform) {
|
||||
ApiImportParser apiImportParser = ApiImportParserFactory.getApiImportParser(platform);
|
||||
ApiImport apiImport = null;
|
||||
try {
|
||||
|
||||
ApiImportParser apiImportParser = ApiImportParserFactory.getApiImportParser(platform);
|
||||
ApiImport apiImport = apiImportParser.parse(file.getInputStream());
|
||||
SaveAPITestRequest request = getImportApiTest(file, projectId, apiImport);
|
||||
ApiTest test = createTest(request);
|
||||
return test;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
apiImport = apiImportParser.parse(file.getInputStream());
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
MSException.throwException(Translator.get("parse_data_error"));
|
||||
}
|
||||
return null;
|
||||
SaveAPITestRequest request = getImportApiTest(file, apiImport);
|
||||
return createTest(request);
|
||||
}
|
||||
|
||||
private SaveAPITestRequest getImportApiTest(MultipartFile file, String projectId, ApiImport apiImport) {
|
||||
private SaveAPITestRequest getImportApiTest(MultipartFile file, ApiImport apiImport) {
|
||||
SaveAPITestRequest request = new SaveAPITestRequest();
|
||||
request.setName(file.getOriginalFilename());
|
||||
request.setProjectId(projectId);
|
||||
request.setProjectId("");
|
||||
request.setScenarioDefinition(apiImport.getScenarios());
|
||||
request.setUserId(SessionUtils.getUser().getId());
|
||||
request.setId(UUID.randomUUID().toString());
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package io.metersphere.commons.constants;
|
||||
|
||||
public enum MsRequestBodyType {
|
||||
|
||||
|
||||
KV("KeyValue"), FORM_DATA("Form Data"), RAW("Raw");
|
||||
|
||||
private String value;
|
||||
|
||||
MsRequestBodyType(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String value() {
|
||||
return this.value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package io.metersphere.commons.constants;
|
||||
|
||||
public enum PostmanRequestBodyMode {
|
||||
|
||||
RAW("raw"), FORM_DATA("formdata"), URLENCODED("urlencoded"), FILE("file");
|
||||
|
||||
private String value;
|
||||
|
||||
PostmanRequestBodyMode(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String value() {
|
||||
return this.value;
|
||||
}
|
||||
}
|
|
@ -43,13 +43,12 @@
|
|||
{{$t('api_test.export_config')}}
|
||||
</el-dropdown-item>
|
||||
<el-dropdown-item command="import" :disabled="isReadOnly">
|
||||
导入
|
||||
<!-- {{$t('api_test.export_config')}}-->
|
||||
{{$t('api_test.api_import.label')}}
|
||||
</el-dropdown-item>
|
||||
</el-dropdown-menu>
|
||||
</el-dropdown>
|
||||
|
||||
<api-import :project-id="test.projectId" ref="apiImport"/>
|
||||
<api-import ref="apiImport"/>
|
||||
|
||||
<ms-api-report-dialog :test-id="id" ref="reportDialog"/>
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
<template>
|
||||
<el-dialog :title="'接口测试导入'" :visible.sync="visible" class="api-import" v-loading="result.loading">
|
||||
<el-dialog :title="$t('api_test.api_import.title')" :visible.sync="visible" class="api-import" v-loading="result.loading">
|
||||
|
||||
<div class="data-format">
|
||||
<div>数据格式</div>
|
||||
<div>{{$t('api_test.api_import.data_format')}}</div>
|
||||
<el-radio-group v-model="selectedPlatformValue">
|
||||
<el-radio v-for="(item, index) in platforms" :key="index" :label="item.value">{{item.name}}</el-radio>
|
||||
</el-radio-group>
|
||||
|
@ -19,17 +19,17 @@
|
|||
:file-list="fileList"
|
||||
multiple>
|
||||
<i class="el-icon-upload"></i>
|
||||
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
|
||||
<div class="el-upload__tip" slot="tip">文件大小不超过 20 M</div>
|
||||
<div class="el-upload__text" v-html="$t('load_test.upload_tips')"></div>
|
||||
<div class="el-upload__tip" slot="tip">{{$t('api_test.api_import.file_size_limit')}}</div>
|
||||
</el-upload>
|
||||
</div>
|
||||
|
||||
<div class="format-tip">
|
||||
<div>
|
||||
<span>说明:{{selectedPlatform.tip}}</span>
|
||||
<span>{{$t('api_test.api_import.tip')}}:{{selectedPlatform.tip}}</span>
|
||||
</div>
|
||||
<div>
|
||||
<span>导出方法:{{selectedPlatform.exportTip}}</span>
|
||||
<span>{{$t('api_test.api_import.export_tip')}}:{{selectedPlatform.exportTip}}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
@ -41,7 +41,6 @@
|
|||
export default {
|
||||
name: "ApiImport",
|
||||
components: {MsDialogFooter},
|
||||
props: ['projectId'],
|
||||
data() {
|
||||
return {
|
||||
visible: false,
|
||||
|
@ -49,8 +48,15 @@
|
|||
{
|
||||
name: 'Metersphere',
|
||||
value: 'Metersphere',
|
||||
tip: '支持 Metersphere json 格式',
|
||||
exportTip: '通过 Metersphere Api 测试页面或者浏览器插件导出 json 格式文件',
|
||||
tip: this.$t('api_test.api_import.ms_tip'),
|
||||
exportTip: this.$t('api_test.api_import.ms_export_tip'),
|
||||
suffixes: new Set(['json'])
|
||||
},
|
||||
{
|
||||
name: 'Postman',
|
||||
value: 'Postman',
|
||||
tip: this.$t('api_test.api_import.postman_tip'),
|
||||
exportTip: this.$t('api_test.api_import.post_man_export_tip'),
|
||||
suffixes: new Set(['json'])
|
||||
}
|
||||
],
|
||||
|
@ -64,9 +70,9 @@
|
|||
this.selectedPlatform = this.platforms[0];
|
||||
},
|
||||
watch: {
|
||||
selectedPlatformId() {
|
||||
selectedPlatformValue() {
|
||||
for (let i in this.platforms) {
|
||||
if (this.platforms[i].id === this.selectedPlatformValue) {
|
||||
if (this.platforms[i].value === this.selectedPlatformValue) {
|
||||
this.selectedPlatform = this.platforms[i];
|
||||
break;
|
||||
}
|
||||
|
@ -79,7 +85,7 @@
|
|||
},
|
||||
upload(file) {
|
||||
this.fileList.push(file.file);
|
||||
this.result = this.$fileUpload('/api/import/' + this.selectedPlatformValue + '/' + this.projectId, this.fileList, response => {
|
||||
this.result = this.$fileUpload('/api/import/' + this.selectedPlatformValue, this.fileList, response => {
|
||||
let res = response.data;
|
||||
this.$success(this.$t('test_track.case.import.success'));
|
||||
this.visible = false;
|
||||
|
@ -90,7 +96,7 @@
|
|||
uploadValidate(file, fileList) {
|
||||
let suffix = file.name.substring(file.name.lastIndexOf('.') + 1);
|
||||
if (!this.selectedPlatform.suffixes.has(suffix)) {
|
||||
this.$warning("格式错误");
|
||||
this.$warning(this.$t('api_test.api_import.suffixFormatErr'));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -364,6 +364,19 @@ export default {
|
|||
json_path_expression: "JSONPath expression",
|
||||
xpath_expression: "XPath expression",
|
||||
}
|
||||
},
|
||||
api_import: {
|
||||
label: "Import",
|
||||
title: "API test import",
|
||||
data_format: "Data format",
|
||||
file_size_limit: "The file size does not exceed 20 M",
|
||||
tip: "Instructions",
|
||||
export_tip: "Export Tip",
|
||||
ms_tip: "Support for Metersphere JSON format",
|
||||
ms_export_tip: "Export jSON-formatted files via Metersphere website or browser plug-ins",
|
||||
postman_tip: "Only Postman Collection V2.1 json files are supported",
|
||||
post_man_export_tip: "Export the test collection by Postman",
|
||||
suffixFormatErr: "The file format does not meet the requirements",
|
||||
}
|
||||
},
|
||||
api_report: {
|
||||
|
|
|
@ -362,6 +362,19 @@ export default {
|
|||
json_path_expression: "JSONPath表达式",
|
||||
xpath_expression: "XPath表达式",
|
||||
}
|
||||
},
|
||||
api_import: {
|
||||
label: "导入",
|
||||
title: "接口测试导入",
|
||||
data_format: "数据格式",
|
||||
file_size_limit: "文件大小不超过 20 M",
|
||||
tip: "说明",
|
||||
export_tip: "导出方法",
|
||||
ms_tip: "支持 Metersphere json 格式",
|
||||
ms_export_tip: "通过 Metersphere Api 测试页面或者浏览器插件导出 json 格式文件",
|
||||
postman_tip: "只支持 Postman Collection v2.1 格式的 json 文件",
|
||||
post_man_export_tip: "通过 Postman 导出测试集合",
|
||||
suffixFormatErr: "文件格式不符合要求",
|
||||
}
|
||||
},
|
||||
api_report: {
|
||||
|
|
|
@ -362,6 +362,19 @@ export default {
|
|||
json_path_expression: "JSONPath運算式",
|
||||
xpath_expression: "XPath運算式",
|
||||
}
|
||||
},
|
||||
api_import: {
|
||||
label: "導入",
|
||||
title: "接口測試導入",
|
||||
data_format: "數據格式",
|
||||
file_size_limit: "文件大小不超過 20 M",
|
||||
tip: "說明",
|
||||
export_tip: "導出方法",
|
||||
ms_tip: "支持 Metersphere json 格式",
|
||||
ms_export_tip: "通過 Metersphere Api 測試頁面或者瀏覽器插件導出 json 格式文件",
|
||||
postman_tip: "只支持 Postman Collection v2.1 格式的 json 文件",
|
||||
post_man_export_tip: "通過 Postman 導出測試集合",
|
||||
suffixFormatErr: "文件格式不符合要求",
|
||||
}
|
||||
},
|
||||
api_report: {
|
||||
|
|
Loading…
Reference in New Issue