fix(接口测试): 修复场景步骤导入API或CASE顺序错乱问题

--bug=1011762 --user=赵勇 【接口测试】github#12025场景批量导入接口 case 顺序与勾选时不一致 https://www.tapd.cn/55049933/s/1133028
This commit is contained in:
fit2-zhao 2022-04-12 13:38:10 +08:00 committed by 刘瑞斌
parent 1a850984a3
commit cb4db5a073
4 changed files with 36 additions and 21 deletions

View File

@ -51,7 +51,7 @@ public class MsExtract extends MsTestElement {
}
if (CollectionUtils.isNotEmpty(this.getXpath())) {
this.getXpath().stream().filter(MsExtractCommon::isValid).forEach(extractXPath ->
samplerHashTree.add(xPath2Extractor(extractXPath, extract))
samplerHashTree.add(xPathExtractor(extractXPath, extract))
);
}
if (CollectionUtils.isNotEmpty(this.getJson())) {
@ -76,7 +76,7 @@ public class MsExtract extends MsTestElement {
RegexExtractor extractor = new RegexExtractor();
extractor.setEnabled(this.isEnable());
extractor.setName(StringUtils.isNotEmpty(extractRegex.getVariable()) ? extractRegex.getVariable() : this.getName());
if(StringUtils.isEmpty(extractor.getName())){
if (StringUtils.isEmpty(extractor.getName())) {
extractor.setName("RegexExtractor");
}
/*extractor.setName(StringUtils.isNotEmpty(this.getName()) ? this.getName() : " RegexExtractor");*/
@ -94,13 +94,13 @@ public class MsExtract extends MsTestElement {
return extractor;
}
private XPathExtractor xPath2Extractor(MsExtractXPath extractXPath, StringJoiner extract) {
private XPathExtractor xPathExtractor(MsExtractXPath extractXPath, StringJoiner extract) {
XPathExtractor extractor = new XPathExtractor();
extractor.setEnabled(this.isEnable());
extractor.setTolerant(true);
extractor.setName(StringUtils.isNotEmpty(extractXPath.getVariable()) ? extractXPath.getVariable() : this.getName());
if(StringUtils.isEmpty(extractor.getName())){
extractor.setName("XPath2Extractor");
if (StringUtils.isEmpty(extractor.getName())) {
extractor.setName("XPathExtractor");
}
extractor.setProperty(TestElement.TEST_CLASS, XPathExtractor.class.getName());
extractor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("XPathExtractorGui"));
@ -117,7 +117,7 @@ public class MsExtract extends MsTestElement {
JSONPostProcessor extractor = new JSONPostProcessor();
extractor.setEnabled(this.isEnable());
extractor.setName(StringUtils.isNotEmpty(extractJSONPath.getVariable()) ? extractJSONPath.getVariable() : this.getName());
if(StringUtils.isEmpty(extractor.getName())){
if (StringUtils.isEmpty(extractor.getName())) {
extractor.setName("JSONPostProcessor");
}
/*extractor.setName(StringUtils.isNotEmpty(this.getName()) ? this.getName() : " JSONExtractor");*/

View File

@ -151,7 +151,7 @@ public class JMeterService {
}
}
private void send(JmeterRunRequestDTO request, List<TestResource> resources) {
private synchronized void send(JmeterRunRequestDTO request, List<TestResource> resources) {
try {
if (StringUtils.isNotEmpty(request.getPoolId()) && CollectionUtils.isEmpty(resources)) {
resources = GenerateHashTreeUtil.setPoolResource(request.getPoolId());

View File

@ -51,7 +51,9 @@ import io.metersphere.service.*;
import io.metersphere.track.request.testcase.ApiCaseRelevanceRequest;
import io.metersphere.track.request.testcase.QueryTestPlanRequest;
import io.metersphere.track.service.TestPlanService;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.comparators.FixedOrderComparator;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
@ -212,20 +214,20 @@ public class ApiDefinitionService {
Map<String, Map<String, List<ApiDefinition>>> projectIdMap = new HashMap<>();
for (ApiDefinition api : updateApiList) {
String projectId = api.getProjectId();
String protocal = api.getProtocol();
String protocol = api.getProtocol();
if (projectIdMap.containsKey(projectId)) {
if (projectIdMap.get(projectId).containsKey(protocal)) {
projectIdMap.get(projectId).get(protocal).add(api);
if (projectIdMap.get(projectId).containsKey(protocol)) {
projectIdMap.get(projectId).get(protocol).add(api);
} else {
List<ApiDefinition> list = new ArrayList<>();
list.add(api);
projectIdMap.get(projectId).put(protocal, list);
projectIdMap.get(projectId).put(protocol, list);
}
} else {
List<ApiDefinition> list = new ArrayList<>();
list.add(api);
Map<String, List<ApiDefinition>> map = new HashMap<>();
map.put(protocal, list);
map.put(protocol, list);
projectIdMap.put(projectId, map);
}
}
@ -235,8 +237,8 @@ public class ApiDefinitionService {
Map<String, List<ApiDefinition>> map = entry.getValue();
for (Map.Entry<String, List<ApiDefinition>> itemEntry : map.entrySet()) {
String protocal = itemEntry.getKey();
ApiModule node = apiModuleService.getDefaultNodeUnCreateNew(projectId, protocal);
String protocol = itemEntry.getKey();
ApiModule node = apiModuleService.getDefaultNodeUnCreateNew(projectId, protocol);
if (node != null) {
List<ApiDefinition> testCaseList = itemEntry.getValue();
for (ApiDefinition apiDefinition : testCaseList) {
@ -259,6 +261,12 @@ public class ApiDefinitionService {
return new ArrayList<>();
}
List<ApiDefinitionResult> resList = extApiDefinitionMapper.listByIds(request.getIds());
// 排序
FixedOrderComparator<String> fixedOrderComparator = new FixedOrderComparator<String>(request.getIds());
fixedOrderComparator.setUnknownObjectBehavior(FixedOrderComparator.UnknownObjectBehavior.BEFORE);
BeanComparator beanComparator = new BeanComparator("id", fixedOrderComparator);
Collections.sort(resList, beanComparator);
calculateResult(resList, request.getProjectId());
return resList;
}
@ -267,12 +275,12 @@ public class ApiDefinitionService {
* 初始化部分参数
*
* @param request
* @param setDefultOrders
* @param defaultSorting
* @param checkThisWeekData
* @return
*/
private ApiDefinitionRequest initRequest(ApiDefinitionRequest request, boolean setDefultOrders, boolean checkThisWeekData) {
if (setDefultOrders) {
private ApiDefinitionRequest initRequest(ApiDefinitionRequest request, boolean defaultSorting, boolean checkThisWeekData) {
if (defaultSorting) {
request.setOrders(ServiceUtils.getDefaultSortOrder(request.getOrders()));
}
if (checkThisWeekData) {

View File

@ -35,7 +35,9 @@ import io.metersphere.service.FileService;
import io.metersphere.service.UserService;
import io.metersphere.track.request.testcase.ApiCaseRelevanceRequest;
import io.metersphere.track.service.TestPlanService;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.comparators.FixedOrderComparator;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
@ -145,7 +147,7 @@ public class ApiTestCaseService {
if (environment != null) {
apiCase.setEnvironment(environment.getName());
}
if(apiCase.getExecResult() == null && StringUtils.isNotEmpty(apiCase.getStatus()) && !StringUtils.equalsIgnoreCase(apiCase.getStatus(),"trash")){
if (apiCase.getExecResult() == null && StringUtils.isNotEmpty(apiCase.getStatus()) && !StringUtils.equalsIgnoreCase(apiCase.getStatus(), "trash")) {
apiCase.setExecResult(apiCase.getStatus());
}
}
@ -231,7 +233,7 @@ public class ApiTestCaseService {
caseResult.setUpdateUser(updateUser.getName());
}
//检查用例的执行状态是否是null如果报告被删除 如果执行结果是null取记录最后执行状态的status字段
if(caseResult.getExecResult() == null && StringUtils.isNotEmpty(caseResult.getStatus()) && !StringUtils.equalsIgnoreCase(caseResult.getStatus(),"trash")){
if (caseResult.getExecResult() == null && StringUtils.isNotEmpty(caseResult.getStatus()) && !StringUtils.equalsIgnoreCase(caseResult.getStatus(), "trash")) {
caseResult.setExecResult(caseResult.getStatus());
}
});
@ -737,9 +739,9 @@ public class ApiTestCaseService {
ApiTestCaseMapper batchMapper = sqlSession.getMapper(ApiTestCaseMapper.class);
bloBs.forEach(apiTestCase -> {
MsHTTPSamplerProxy req = JSON.parseObject(apiTestCase.getRequest(), MsHTTPSamplerProxy.class,Feature.DisableSpecialKeyDetect);
MsHTTPSamplerProxy req = JSON.parseObject(apiTestCase.getRequest(), MsHTTPSamplerProxy.class, Feature.DisableSpecialKeyDetect);
try {
JSONObject element = JSON.parseObject(apiTestCase.getRequest(),Feature.DisableSpecialKeyDetect);
JSONObject element = JSON.parseObject(apiTestCase.getRequest(), Feature.DisableSpecialKeyDetect);
ElementUtil.dataFormatting(element);
if (element != null && StringUtils.isNotEmpty(element.getString("hashTree"))) {
@ -818,6 +820,11 @@ public class ApiTestCaseService {
esbApiParamService.handleApiEsbParams(model);
}
}
// 排序
FixedOrderComparator<String> fixedOrderComparator = new FixedOrderComparator<String>(request.getIds());
fixedOrderComparator.setUnknownObjectBehavior(FixedOrderComparator.UnknownObjectBehavior.BEFORE);
BeanComparator beanComparator = new BeanComparator("id", fixedOrderComparator);
Collections.sort(list, beanComparator);
return list;
}