chore(接口测试): 执行误报处理移动到资源池中

This commit is contained in:
fit2-zhao 2024-02-04 09:42:55 +08:00 committed by Craftsman
parent 64b22a6d9a
commit 380956f96d
2 changed files with 4 additions and 94 deletions

View File

@ -1,87 +0,0 @@
package io.metersphere.sdk.util.fake.error;
import io.metersphere.sdk.constants.ApiReportStatus;
import io.metersphere.sdk.dto.api.result.MsRegexDTO;
import io.metersphere.sdk.dto.api.result.RequestResult;
import io.metersphere.sdk.util.LogUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 误报解析类
*/
public class FakeErrorUtils {
public static void compute(RequestResult result, List<MsRegexDTO> regexDTOS) {
try {
if (result != null && StringUtils.isNotBlank(result.getProjectId()) &&
CollectionUtils.isNotEmpty(regexDTOS)) {
Map<String, List<MsRegexDTO>> fakeErrorMap = regexDTOS.stream()
.collect(Collectors.groupingBy(MsRegexDTO::getProjectId));
List<MsRegexDTO> regexList = fakeErrorMap.get(result.getProjectId());
//根据配置来筛选断言获取误报编码获取接口状态是否是误报
List<String> errorCodeList = new ArrayList<>();
regexList.forEach(item -> {
if (StringUtils.isNotEmpty(item.getType())) {
switch (item.getRespType()) {
case "Response Code" ->
item.setPass(parseResponseCode(result.getResponseResult().getResponseCode(), item.getExpression(), item.getRelation()));
case "Response Headers" ->
item.setPass(parseResponseCode(result.getResponseResult().getHeaders(), item.getExpression(), item.getRelation()));
case "Response Data" ->
item.setPass(parseResponseCode(result.getResponseResult().getBody(), item.getExpression(), item.getRelation()));
default -> item.setPass(false);
}
}
if (BooleanUtils.isTrue(item.getPass())) {
errorCodeList.add(item.getName());
}
});
if (CollectionUtils.isNotEmpty(errorCodeList)) {
result.setStatus(ApiReportStatus.FAKE_ERROR.name());
result.setFakeErrorCode(getErrorCodeStr(errorCodeList));
}
}
} catch (Exception e) {
LogUtils.error("误报处理错误:", e);
}
}
private static boolean parseResponseCode(String result, String regexDTO, String condition) {
return switch (condition.toUpperCase()) {
case "CONTAINS" -> result.contains(regexDTO);
case "NOT_CONTAINS" -> notContains(result, regexDTO);
case "EQUALS" -> StringUtils.equals(result, regexDTO);
case "START_WITH" -> result.startsWith(regexDTO);
case "END_WITH" -> result.endsWith(regexDTO);
default -> false;
};
}
private static String getErrorCodeStr(List<String> errorCodeList) {
if (CollectionUtils.isNotEmpty(errorCodeList)) {
String errorCodeStr = StringUtils.join(errorCodeList, ";");
return errorCodeStr;
} else {
return StringUtils.EMPTY;
}
}
private static boolean notContains(String result, String regexDTO) {
return !result.contains(regexDTO);
}
}

View File

@ -48,12 +48,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import static io.metersphere.api.controller.result.ApiResultCode.RESOURCE_POOL_EXECUTE_ERROR;
@ -130,7 +125,9 @@ public class ApiExecuteService {
setTaskFileParam(request, taskRequest);
// 误报处理
taskRequest.setMsRegexList(projectApplicationService.get(Arrays.asList(request.getProjectId())));
if (StringUtils.isNotBlank(request.getProjectId())) {
taskRequest.setMsRegexList(projectApplicationService.get(Collections.singletonList(request.getProjectId())));
}
// todo 获取接口插件和jar包
// todo 处理公共脚本