Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wan92hen committed Jul 21, 2021
2 parents 50e3158 + 7e63555 commit c19e45d
Show file tree
Hide file tree
Showing 63 changed files with 736 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private ScenarioImport parseMsFormat(String testStr, ApiTestImportRequest import
}
}

if (finalNodeMap != null) {
if (finalNodeMap != null && finalNodeMap.get(item.getApiScenarioModuleId()) != null) {
NodeTree node = finalNodeMap.get(item.getApiScenarioModuleId());
item.setApiScenarioModuleId(node.getNewId());
item.setModulePath(node.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private void parseApiDefinition(ApiDefinitionWithBLOBs apiDefinition, ApiTestImp
String originId = apiDefinition.getId();
String id = UUID.randomUUID().toString();

if (nodeMap != null) {
if (nodeMap != null && nodeMap.get(apiDefinition.getModuleId()) != null) {
NodeTree nodeTree = nodeMap.get(apiDefinition.getModuleId());
apiDefinition.setModuleId(nodeTree.getNewId());
apiDefinition.setModulePath(nodeTree.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterCon

if (CollectionUtils.isNotEmpty(hashTree)) {
for (MsTestElement el : hashTree) {
el.setEnvironmentId(useEnvironment);
if(this.getEnvironmentId() == null){
el.setEnvironmentId(useEnvironment);
}
el.toHashTree(httpSamplerTree, el.getHashTree(), config);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ protected ApiDefinitionWithBLOBs buildApiDefinition(String id, String name, Stri
private String formatPath(String url) {
try {
URL urlObject = new URL(url);
StringBuffer pathBuffer = new StringBuffer(urlObject.getPath());
String path = StringUtils.isBlank(urlObject.getPath()) ? "/" : urlObject.getPath();
StringBuffer pathBuffer = new StringBuffer(path);
if (StringUtils.isNotEmpty(urlObject.getQuery())) {
pathBuffer.append("?").append(urlObject.getQuery());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.commons.constants.MsRequestBodyType;
import io.metersphere.commons.constants.PostmanRequestBodyMode;
import io.metersphere.commons.utils.LogUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public abstract class PostmanAbstractParserParser<T> extends ApiImportAbstractParser<T> {
Expand All @@ -30,8 +33,7 @@ protected MsHTTPSamplerProxy parsePostman(PostmanItem requestItem) {
MsHTTPSamplerProxy request = buildRequest(requestItem.getName(), url.getRaw(), requestDesc.getMethod());
if (StringUtils.isNotBlank(request.getPath())) {
String path = request.getPath().split("\\?")[0];
path = path.replace("{{", "${");
path = path.replace("}}", "}");
path = parseVariable(path);
request.setPath(path);
} else {
request.setPath("/");
Expand All @@ -45,6 +47,26 @@ protected MsHTTPSamplerProxy parsePostman(PostmanItem requestItem) {
}


/**
* 将postman的变量转换成ms变量
* {{xxx}} -> ${xxx}
* @param value
* @return
*/
public String parseVariable(String value) {
try {
Pattern pattern = Pattern.compile("(\\{\\{(.*?)\\}\\})");
Matcher matcher = pattern.matcher(value);
while (matcher.find()) {
value = matcher.replaceFirst("\\$\\{" + matcher.group(2) + "\\}");
matcher = pattern.matcher(value);
}
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
}
return value;
}

private void addPreScript(MsHTTPSamplerProxy request, List<PostmanEvent> event) {
if (request != null && CollectionUtils.isNotEmpty(event)) {
StringBuilder scriptStr = new StringBuilder();
Expand All @@ -68,7 +90,7 @@ private void addPreScript(MsHTTPSamplerProxy request, List<PostmanEvent> event)
MsJSR223PreProcessor jsr223PreProcessor = new MsJSR223PreProcessor();
jsr223PreProcessor.setName("JSR223PreProcessor");
jsr223PreProcessor.setScriptLanguage("nashornScript");
jsr223PreProcessor.setScript(scriptStr.toString());
jsr223PreProcessor.setScript(parseVariable(scriptStr.toString()));
LinkedList<MsTestElement> hashTree = new LinkedList<>();
hashTree.add(jsr223PreProcessor);
request.setHashTree(hashTree);
Expand All @@ -81,7 +103,12 @@ private List<KeyValue> parseKeyValue(List<PostmanKeyValue> postmanKeyValues) {
return null;
}
List<KeyValue> keyValues = new ArrayList<>();
postmanKeyValues.forEach(item -> keyValues.add(new KeyValue(item.getKey(), item.getValue(), item.getDescription(), item.getContentType())));
postmanKeyValues.forEach(item -> {
String k = parseVariable(item.getKey());
String v = parseVariable(item.getValue());
String desc = parseVariable(item.getDescription());
keyValues.add(new KeyValue(k, v, desc, item.getContentType()));
});
return keyValues;
}

Expand All @@ -97,7 +124,7 @@ private void parseBody(Body body, PostmanRequest requestDesc) {
if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.RAW.value())) {
parseRawBody(body, postmanBody, bodyMode);
} else if (StringUtils.equalsAny(bodyMode, PostmanRequestBodyMode.FORM_DATA.value(), PostmanRequestBodyMode.URLENCODED.value())) {
List<PostmanKeyValue> postmanKeyValues = JSON.parseArray(postmanBody.getString(bodyMode), PostmanKeyValue.class);
List<PostmanKeyValue> postmanKeyValues = JSON.parseArray(parseVariable(postmanBody.getString(bodyMode)), PostmanKeyValue.class);
body.setKvs(parseKeyValue(postmanKeyValues));
if (StringUtils.equals(bodyMode, PostmanRequestBodyMode.FORM_DATA.value())) {
body.setType(Body.FORM_DATA);
Expand All @@ -111,7 +138,7 @@ private void parseBody(Body body, PostmanRequest requestDesc) {
}

private void parseRawBody(Body body, JSONObject postmanBody, String bodyMode) {
body.setRaw(postmanBody.getString(bodyMode));
body.setRaw(parseVariable(postmanBody.getString(bodyMode)));
body.setType(MsRequestBodyType.RAW.value());
JSONObject options = postmanBody.getJSONObject("options");
if (options != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,38 @@ public void removeToGc(List<String> apiIds) {
}

public void reduction(List<String> ids) {
extApiScenarioMapper.reduction(ids);
if(CollectionUtils.isNotEmpty(ids)){
extApiScenarioMapper.checkOriginalStatusByIds(ids);
//检查原来模块是否还在
ApiScenarioExample example = new ApiScenarioExample();
example.createCriteria().andIdIn(ids);
List<ApiScenario> scenarioList = apiScenarioMapper.selectByExample(example);
Map<String,List<ApiScenario>> nodeMap = scenarioList.stream().collect(Collectors.groupingBy(ApiScenario :: getApiScenarioModuleId));
ApiScenarioModuleService apiScenarioModuleService = CommonBeanFactory.getBean(ApiScenarioModuleService.class);
for(Map.Entry<String,List<ApiScenario>> entry : nodeMap.entrySet()){
String nodeId = entry.getKey();
List<ApiScenario> scenariosListItem = entry.getValue();
Map<String,List<ApiScenario>> projectMap = scenariosListItem.stream().collect(Collectors.groupingBy(ApiScenario :: getProjectId));
for(Map.Entry<String,List<ApiScenario>> projectEntry : projectMap.entrySet()){
String projectId = projectEntry.getKey();
List<ApiScenario> checkList = projectEntry.getValue();
if(StringUtils.isNotEmpty(projectId)){
long nodeCount = apiScenarioModuleService.countById(nodeId);
if(nodeCount <= 0){
ApiScenarioModule node = apiScenarioModuleService.getDefaultNode(projectId);
for (ApiScenario testCase: checkList) {
ApiScenarioWithBLOBs updateCase = new ApiScenarioWithBLOBs();
updateCase.setId(testCase.getId());
updateCase.setApiScenarioModuleId(node.getId());
updateCase.setModulePath("/"+node.getName());
apiScenarioMapper.updateByPrimaryKeySelective(updateCase);
}
}
}
}
}
extApiScenarioMapper.reduction(ids);
}
}

private void checkNameExist(SaveApiScenarioRequest request) {
Expand Down Expand Up @@ -1108,7 +1139,7 @@ public void run() {
//存储报告
APIScenarioReportResult report = executeQueue.get(reportId).getReport();
batchMapper.insert(report);
executorService.submit(new ParallelScenarioExecTask(jMeterService, executeQueue.get(report), request));
executorService.submit(new ParallelScenarioExecTask(jMeterService, executeQueue.get(reportId), request));
}
sqlSession.flushStatements();
}
Expand Down Expand Up @@ -1393,9 +1424,11 @@ public String debugRun(RunDefinitionRequest request, List<MultipartFile> bodyFil
if (map != null) {
map.keySet().forEach(id -> {
ApiTestEnvironmentWithBLOBs environment = environmentService.get(map.get(id));
EnvironmentConfig env = JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class);
env.setApiEnvironmentid(environment.getId());
envConfig.put(id, env);
if (environment != null && environment.getConfig() != null) {
EnvironmentConfig env = JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class);
env.setApiEnvironmentid(environment.getId());
envConfig.put(id, env);
}
});
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private String getName(String type, String id, String status, Long time) {
if (id.indexOf(DelimiterConstants.SEPARATOR.toString()) != -1) {
return id.substring(0, id.indexOf(DelimiterConstants.SEPARATOR.toString()));
}
if (StringUtils.equals(type, ApiRunMode.API_PLAN.name())) {
if (StringUtils.equalsAnyIgnoreCase(type, ApiRunMode.API_PLAN.name(),ApiRunMode.SCHEDULE_API_PLAN.name(),ApiRunMode.JENKINS_API_PLAN.name())) {
TestPlanApiCase testPlanApiCase = testPlanApiCaseService.getById(id);
ApiTestCaseWithBLOBs caseWithBLOBs = null;
if (testPlanApiCase != null) {
Expand Down Expand Up @@ -174,7 +174,8 @@ public void saveApiResultByScheduleTask(TestResult result,String testPlanReportI
ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult();
saveResult.setId(UUID.randomUUID().toString());
saveResult.setCreateTime(System.currentTimeMillis());
saveResult.setName(item.getName());
// saveResult.setName(item.getName());
saveResult.setName(getName(type, item.getName(), status, saveResult.getCreateTime()));
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = apiDefinitionMapper.selectByPrimaryKey(item.getName());
if (apiDefinitionWithBLOBs != null) {
saveResult.setName(apiDefinitionWithBLOBs.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ public void deleteBatch(List<String> apiIds) {
}

public void removeToGc(List<String> apiIds) {
if(CollectionUtils.isEmpty(apiIds)){
return;
}
ApiDefinitionExampleWithOperation example = new ApiDefinitionExampleWithOperation();
example.createCriteria().andIdIn(apiIds);
example.setOperator(SessionUtils.getUserId());
Expand All @@ -232,6 +235,32 @@ public void reduction(ApiBatchRequest request) {
ServiceUtils.getSelectAllIds(request, request.getCondition(),
(query) -> extApiDefinitionMapper.selectIds(query));
if (request.getIds() != null || !request.getIds().isEmpty()) {
//检查模块是否还在

//检查原来模块是否还在
ApiDefinitionExample example = new ApiDefinitionExample();
example.createCriteria().andIdIn(request.getIds());
List<ApiDefinition> reductionCaseList = apiDefinitionMapper.selectByExample(example);
Map<String,List<ApiDefinition>> nodeMap = reductionCaseList.stream().collect(Collectors.groupingBy(ApiDefinition :: getModuleId));
ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class);
for(Map.Entry<String,List<ApiDefinition>> entry : nodeMap.entrySet()){
String nodeId = entry.getKey();
long nodeCount = apiModuleService.countById(nodeId);
if(nodeCount <= 0){
String projectId = request.getProjectId();
ApiModule node = apiModuleService.getDefaultNode(projectId,request.getProtocol());
List<ApiDefinition> testCaseList = entry.getValue();
for (ApiDefinition apiDefinition: testCaseList) {
ApiDefinitionWithBLOBs updateCase = new ApiDefinitionWithBLOBs();
updateCase.setId(apiDefinition.getId());
updateCase.setModuleId(node.getId());
updateCase.setModulePath("/"+node.getName());

apiDefinitionMapper.updateByPrimaryKeySelective(updateCase);
}
}
}
extApiDefinitionMapper.checkOriginalStatusByIds(request.getIds());
extApiDefinitionMapper.reduction(request.getIds());

List<String> apiCaseIds = apiTestCaseService.selectCaseIdsByApiIds(request.getIds());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,7 @@ public ApiModuleService() {

public List<ApiModuleDTO> getNodeTreeByProjectId(String projectId, String protocol) {
// 判断当前项目下是否有默认模块,没有添加默认模块
ApiModuleExample example = new ApiModuleExample();
example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo("默认模块");
long count = apiModuleMapper.countByExample(example);
if (count <= 0) {
ApiModule record = new ApiModule();
record.setId(UUID.randomUUID().toString());
record.setName("默认模块");
record.setProtocol(protocol);
record.setPos(1.0);
record.setLevel(1);
record.setCreateTime(System.currentTimeMillis());
record.setUpdateTime(System.currentTimeMillis());
record.setProjectId(projectId);
apiModuleMapper.insert(record);
}
this.getDefaultNode(projectId,protocol);
List<ApiModuleDTO> apiModules = extApiModuleMapper.getNodeTreeByProjectId(projectId, protocol);
ApiDefinitionRequest request = new ApiDefinitionRequest();
request.setProjectId(projectId);
Expand Down Expand Up @@ -509,4 +495,31 @@ public String getLogDetails(ApiModule node) {
}
return null;
}

public long countById(String nodeId) {
ApiModuleExample example = new ApiModuleExample();
example.createCriteria().andIdEqualTo(nodeId);
return apiModuleMapper.countByExample(example);
}

public ApiModule getDefaultNode(String projectId,String protocol) {
ApiModuleExample example = new ApiModuleExample();
example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo("默认模块").andParentIdIsNull();;
List<ApiModule> list = apiModuleMapper.selectByExample(example);
if (CollectionUtils.isEmpty(list)) {
ApiModule record = new ApiModule();
record.setId(UUID.randomUUID().toString());
record.setName("默认模块");
record.setProtocol(protocol);
record.setPos(1.0);
record.setLevel(1);
record.setCreateTime(System.currentTimeMillis());
record.setUpdateTime(System.currentTimeMillis());
record.setProjectId(projectId);
apiModuleMapper.insert(record);
return record;
}else {
return list.get(0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,7 @@ public ApiScenarioModuleService() {

public List<ApiScenarioModuleDTO> getNodeTreeByProjectId(String projectId) {
// 判断当前项目下是否有默认模块,没有添加默认模块
ApiScenarioModuleExample example = new ApiScenarioModuleExample();
example.createCriteria().andProjectIdEqualTo(projectId).andNameEqualTo("默认模块");
long count = apiScenarioModuleMapper.countByExample(example);
if (count <= 0) {
ApiScenarioModule record = new ApiScenarioModule();
record.setId(UUID.randomUUID().toString());
record.setName("默认模块");
record.setPos(1.0);
record.setLevel(1);
record.setCreateTime(System.currentTimeMillis());
record.setUpdateTime(System.currentTimeMillis());
record.setProjectId(projectId);
apiScenarioModuleMapper.insert(record);
}
this.getDefaultNode(projectId);

List<ApiScenarioModuleDTO> nodes = extApiScenarioModuleMapper.getNodeTreeByProjectId(projectId);
ApiScenarioRequest request = new ApiScenarioRequest();
Expand Down Expand Up @@ -451,4 +438,30 @@ public String getLogDetails(ApiScenarioModule node) {
}
return null;
}

public long countById(String id) {
ApiScenarioModuleExample example = new ApiScenarioModuleExample();
example.createCriteria().andIdEqualTo(id);
return apiScenarioModuleMapper.countByExample(example);
}

public ApiScenarioModule getDefaultNode(String projectId) {
ApiScenarioModuleExample example = new ApiScenarioModuleExample();
example.createCriteria().andProjectIdEqualTo(projectId).andNameEqualTo("默认模块").andParentIdIsNull();
List<ApiScenarioModule> list = apiScenarioModuleMapper.selectByExample(example);
if (CollectionUtils.isEmpty(list)) {
ApiScenarioModule record = new ApiScenarioModule();
record.setId(UUID.randomUUID().toString());
record.setName("默认模块");
record.setPos(1.0);
record.setLevel(1);
record.setCreateTime(System.currentTimeMillis());
record.setUpdateTime(System.currentTimeMillis());
record.setProjectId(projectId);
apiScenarioModuleMapper.insert(record);
return record;
}else {
return list.get(0);
}
}
}
Loading

0 comments on commit c19e45d

Please sign in to comment.