fix(接口测试): 修复全局前后置脚本变更记录为空的问题

--bug=1018234 --user=宋天阳 【接口测试】环境配置-全局前后置脚本-变更记录为空
https://www.tapd.cn/55049933/s/1264490
This commit is contained in:
song-tianyang 2022-10-17 17:49:22 +08:00 committed by f2c-ci-robot[bot]
parent 38364e48c5
commit 76c1c845cf
4 changed files with 120 additions and 5 deletions

View File

@ -4,15 +4,18 @@ import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.base.domain.EnvironmentGroup;
import io.metersphere.commons.constants.OperLogConstants;
import io.metersphere.commons.constants.OperLogModule;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.*;
import io.metersphere.environment.dto.*;
import io.metersphere.environment.service.BaseEnvGroupProjectService;
import io.metersphere.environment.service.BaseEnvironmentService;
import io.metersphere.environment.service.CommandService;
import io.metersphere.environment.service.BaseEnvGroupProjectService;
import io.metersphere.environment.ssl.KeyStoreEntry;
import io.metersphere.environment.utils.TcpTreeTableDataParser;
import io.metersphere.i18n.Translator;
import io.metersphere.log.annotation.MsAuditLog;
import io.metersphere.request.EnvironmentRequest;
import org.json.JSONArray;
import org.json.JSONObject;
@ -77,6 +80,7 @@ public class TestEnvironmentController {
}
@PostMapping(value = "/update")
@MsAuditLog(module = OperLogModule.PROJECT_ENVIRONMENT_SETTING, type = OperLogConstants.UPDATE, beforeEvent = "#msClass.getLogDetails(#apiTestEnvironment.id)", content = "#msClass.getLogDetails(#apiTestEnvironment.id)", msClass = BaseEnvironmentService.class)
public void update(@RequestPart("request") TestEnvironmentDTO apiTestEnvironment, @RequestPart(value = "files", required = false) List<MultipartFile> sslFiles, @RequestPart(value = "variablesFiles", required = false) List<MultipartFile> variableFile) {
checkParams(apiTestEnvironment);
baseEnvironmentService.update(apiTestEnvironment, sslFiles, variableFile);
@ -84,7 +88,7 @@ public class TestEnvironmentController {
private void checkParams(TestEnvironmentDTO apiTestEnvironment) {
try {
Map<Object,Object> map = JSON.parseObject(apiTestEnvironment.getConfig(),Map.class);
Map<Object, Object> map = JSON.parseObject(apiTestEnvironment.getConfig(), Map.class);
JSONObject json = new JSONObject(map);
JSONObject commonConfig = json.getJSONObject("commonConfig");
JSONArray databaseConfigs = json.getJSONArray("databaseConfigs");

View File

@ -12,6 +12,10 @@ import io.metersphere.dto.BaseSystemConfigDTO;
import io.metersphere.dto.ProjectConfig;
import io.metersphere.environment.dto.*;
import io.metersphere.i18n.Translator;
import io.metersphere.log.utils.ReflexObjectUtil;
import io.metersphere.log.vo.DetailColumn;
import io.metersphere.log.vo.OperatingLogDetails;
import io.metersphere.log.vo.system.SystemReference;
import io.metersphere.metadata.service.FileAssociationService;
import io.metersphere.request.BodyFile;
import io.metersphere.request.variable.ScenarioVariable;
@ -74,6 +78,16 @@ public class BaseEnvironmentService extends NodeTreeService<ApiModuleDTO> {
super(ApiModuleDTO.class);
}
public String getLogDetails(String id) {
ApiTestEnvironmentWithBLOBs bloBs = apiTestEnvironmentMapper.selectByPrimaryKey(id);
if (bloBs != null) {
List<DetailColumn> columns = ReflexObjectUtil.getColumns(bloBs, SystemReference.environmentColumns);
OperatingLogDetails details = new OperatingLogDetails(JSON.toJSONString(bloBs.getId()), bloBs.getProjectId(), bloBs.getName(), bloBs.getCreateUser(), columns);
return JSON.toJSONString(details);
}
return null;
}
public EnvironmentGroup add(EnvironmentGroupRequest request) {
request.setWorkspaceId(SessionUtils.getCurrentWorkspaceId());
this.checkEnvironmentGroup(request);

View File

@ -0,0 +1,97 @@
package io.metersphere.log.utils;
import io.metersphere.commons.utils.JSON;
import io.metersphere.commons.utils.LogUtil;
import org.apache.commons.lang3.StringUtils;
import java.util.LinkedHashMap;
import java.util.Map;
public class ApiTestEnvironmentDiffUtil {
public static String diff(String newValue, String oldValue) {
try {
Map<String, Object> bloBsNew = JSON.parseObject(newValue, Map.class);
Map<String, Object> bloBsOld = JSON.parseObject(oldValue, Map.class);
Map<String, String> diffMap = new LinkedHashMap<>();
diffMap.put("type", "preAndPostScript");
// 对比全局脚本配置参数
if (!StringUtils.equals(bloBsNew.get("globalScriptConfig").toString(), bloBsOld.get("globalScriptConfig").toString())) {
Map<String, Object> globalScriptNew = JSON.parseObject(JSON.toJSONString(bloBsNew.get("globalScriptConfig")), Map.class);
Map<String, Object> globalScriptOld = JSON.parseObject(JSON.toJSONString(bloBsOld.get("globalScriptConfig")), Map.class);
// 前置脚本过滤请求类型
if (!StringUtils.equals(globalScriptNew.get("filterRequestPreScript").toString(), globalScriptOld.get("filterRequestPreScript").toString())) {
diffMap.put("filterRequestPreScriptRaw1", JSON.toJSONString(globalScriptNew.get("filterRequestPreScript")));
diffMap.put("filterRequestPreScriptRaw2", JSON.toJSONString(globalScriptOld.get("filterRequestPreScript")));
}
// 后置脚本过滤请求类型
if (!StringUtils.equals(globalScriptNew.get("filterRequestPostScript").toString(), globalScriptOld.get("filterRequestPostScript").toString())) {
diffMap.put("filterRequestPostScriptRaw1", JSON.toJSONString(globalScriptNew.get("filterRequestPostScript")));
diffMap.put("filterRequestPostScriptRaw2", JSON.toJSONString(globalScriptOld.get("filterRequestPostScript")));
}
// 前置脚本执行顺序
if (!StringUtils.equals(globalScriptNew.get("isPreScriptExecAfterPrivateScript").toString(), globalScriptOld.get("isPreScriptExecAfterPrivateScript").toString())) {
diffMap.put("isPreScriptExecAfterPrivateScriptRaw1", JSON.toJSONString(globalScriptNew.get("isPreScriptExecAfterPrivateScript")));
diffMap.put("isPreScriptExecAfterPrivateScriptRaw2", JSON.toJSONString(globalScriptOld.get("isPreScriptExecAfterPrivateScript")));
}
// 后置脚本执行顺序
if (!StringUtils.equals(globalScriptNew.get("isPostScriptExecAfterPrivateScript").toString(), globalScriptOld.get("isPostScriptExecAfterPrivateScript").toString())) {
diffMap.put("isPostScriptExecAfterPrivateScriptRaw1", JSON.toJSONString(globalScriptNew.get("isPostScriptExecAfterPrivateScript")));
diffMap.put("isPostScriptExecAfterPrivateScriptRaw2", JSON.toJSONString(globalScriptOld.get("isPostScriptExecAfterPrivateScript")));
}
// 前置关联场景结果
if (!StringUtils.equals(globalScriptNew.get("connScenarioPreScript").toString(), globalScriptOld.get("connScenarioPreScript").toString())) {
diffMap.put("connScenarioPreScriptRaw1", JSON.toJSONString(globalScriptNew.get("connScenarioPreScript")));
diffMap.put("connScenarioPreScriptRaw2", JSON.toJSONString(globalScriptOld.get("connScenarioPreScript")));
}
// 后置关联场景结果
if (!StringUtils.equals(globalScriptNew.get("connScenarioPostScript").toString(), globalScriptOld.get("connScenarioPostScript").toString())) {
diffMap.put("connScenarioPostScriptRaw1", JSON.toJSONString(globalScriptNew.get("connScenarioPostScript")));
diffMap.put("connScenarioPostScriptRaw2", JSON.toJSONString(globalScriptOld.get("connScenarioPostScript")));
}
// 全局脚本设置
diffMap.put("globalScriptConfigRaw1", JSON.toJSONString(bloBsNew.get("globalScriptConfig")));
diffMap.put("globalScriptConfigRaw2", JSON.toJSONString(bloBsOld.get("globalScriptConfig")));
}
// 对比全局前置脚本(单个请求)
if (!StringUtils.equals(bloBsNew.get("preProcessor").toString(), bloBsOld.get("preProcessor").toString())) {
diffMap.put("preProcessorRaw1", formatting(bloBsNew.get("preProcessor")));
diffMap.put("preProcessorRaw2", formatting(bloBsOld.get("preProcessor")));
}
// 对比全局前置脚本(所有请求)
if (!StringUtils.equals(bloBsNew.get("preStepProcessor").toString(), bloBsOld.get("preStepProcessor").toString())) {
diffMap.put("preStepProcessorRaw1", formatting(bloBsNew.get("preStepProcessor")));
diffMap.put("preStepProcessorRaw2", formatting(bloBsOld.get("preStepProcessor")));
}
// 对比全局后置脚本(单个请求)
if (!StringUtils.equals(bloBsNew.get("postProcessor").toString(), bloBsOld.get("postProcessor").toString())) {
diffMap.put("postProcessorRaw1", formatting(bloBsNew.get("postProcessor")));
diffMap.put("postProcessorRaw2", formatting(bloBsOld.get("postProcessor")));
}
// 对比全局后置脚本(所有请求)
if (!StringUtils.equals(bloBsNew.get("postStepProcessor").toString(), bloBsOld.get("postStepProcessor").toString())) {
diffMap.put("postStepProcessorRaw1", formatting(bloBsNew.get("postStepProcessor")));
diffMap.put("postStepProcessorRaw2", formatting(bloBsOld.get("postStepProcessor")));
}
if (diffMap.size() > 1) {
return JSON.toJSONString(diffMap);
}
} catch (Exception e) {
LogUtil.error(e);
}
return null;
}
private static String formatting(Object target) {
Map<String, Object> result = JSON.parseObject(JSON.toJSONString(target), Map.class);
result.remove("$type");
return JSON.toJSONString(result);
}
}

View File

@ -174,12 +174,12 @@ public class ReflexObjectUtil {
List<String> originalValueArray = JSON.parseArray(originalValue.toString(), String.class);
Collections.sort(originalValueArray);
Object originalObject = JSON.toJSONString(originalValueArray);
oldTags = StringUtils.join(StringUtils.join(JSON_START , ((originalColumns.get(i) != null && originalObject != null) ? originalObject.toString() : "\"\"")) , JSON_END);
oldTags = StringUtils.join(StringUtils.join(JSON_START, ((originalColumns.get(i) != null && originalObject != null) ? originalObject.toString() : "\"\"")), JSON_END);
}
List<String> newValueArray = JSON.parseArray(newValue.toString(), String.class);
Collections.sort(newValueArray);
Object newObject = JSON.toJSONString(newValueArray);
String newTags = StringUtils.join(StringUtils.join(JSON_START , ((newColumns.get(i) != null && newObject != null) ? newObject.toString() : "\"\"")) , JSON_END);
String newTags = StringUtils.join(StringUtils.join(JSON_START, ((newColumns.get(i) != null && newObject != null) ? newObject.toString() : "\"\"")), JSON_END);
String diffValue;
if (oldTags != null) {
String diffStr = diff.diff(oldTags, newTags);
@ -207,7 +207,7 @@ public class ReflexObjectUtil {
if (originalColumns.get(i).getColumnName().equals("config")) {
String newValue = Objects.toString(column.getNewValue().toString(), "");
String oldValue = Objects.toString(column.getOriginalValue(), "");
// column.setDiffValue(ApiTestEnvironmentDiffUtil.diff(newValue, oldValue));
column.setDiffValue(ApiTestEnvironmentDiffUtil.diff(newValue, oldValue));
}
} else {
String newValue = Objects.toString(column.getNewValue().toString(), "");