fix(接口测试): 修复接口变更记录相关问题

--bug=1026202 --user=郭雨琦
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001026202
This commit is contained in:
guoyuqi 2023-05-18 19:56:58 +08:00 committed by fit2-zhao
parent 815ae0f39f
commit b1025dad23
3 changed files with 61 additions and 35 deletions

View File

@ -15,13 +15,11 @@ import io.metersphere.log.vo.DetailColumn;
import io.metersphere.log.vo.OperatingLogDetails;
import io.metersphere.log.vo.api.DefinitionReference;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
public static final String JSON_START = "{\"root\":";
@ -54,7 +52,10 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
diffMap.put(TYPE, bloBsIsNew.getString(TYPE));
}
}
return JSON.toJSONString(diffMap);
if (MapUtils.isNotEmpty(diffMap)) {
return JSON.toJSONString(diffMap);
}
return null;
}
@Override
@ -107,7 +108,7 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
if (!StringUtils.equals(headerNew, headerOld)) {
String patch = jsonDiff.diff(headerOld, headerNew);
String diffPatch = jsonDiff.apply(headerOld, patch);
if (StringUtils.isNotEmpty(diffPatch)) {
if (StringUtils.isNotBlank(diffPatch)) {
diffMap.put("header", diffPatch);
}
}
@ -123,7 +124,7 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
if (!StringUtils.equals(queryNew, queryOld)) {
String patch = jsonDiff.diff(queryOld, queryNew);
String diff = jsonDiff.apply(queryOld, patch);
if (StringUtils.isNotEmpty(diff)) {
if (StringUtils.isNotBlank(diff)) {
diffMap.put(QUERY, diff);
}
}
@ -139,7 +140,7 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
if (!StringUtils.equals(restNew, restOld)) {
String patch = jsonDiff.diff(restOld, restNew);
String diff = jsonDiff.apply(restOld, patch);
if (StringUtils.isNotEmpty(diff)) {
if (StringUtils.isNotBlank(diff)) {
diffMap.put("rest", diff);
}
}
@ -150,7 +151,7 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
if (!StringUtils.equals(bodyNew, bodyOld)) {
String patch = jsonDiff.diff(bodyOld, bodyNew);
String diff = jsonDiff.apply(bodyOld, patch);
if (StringUtils.isNotEmpty(diff)) {
if (StringUtils.isNotBlank(diff)) {
diffMap.put(BODY, diff);
}
}
@ -166,7 +167,7 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
if (!StringUtils.equals(bodyFormNew, bodyFormOld)) {
String patch = jsonDiff.diff(bodyFormOld, bodyFormNew);
String diff = jsonDiff.apply(bodyFormOld, patch);
if (StringUtils.isNotEmpty(diff)) {
if (StringUtils.isNotBlank(diff)) {
diffMap.put(BODY_FORM, diff);
}
}
@ -221,7 +222,7 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
if (!StringUtils.equals(headerNew, headerOld)) {
String patch = jsonDiff.diff(headerOld, headerNew);
String diffPatch = jsonDiff.apply(headerNew, patch);
if (StringUtils.isNotEmpty(diffPatch)) {
if (StringUtils.isNotBlank(diffPatch)) {
diffMap.put("header", diffPatch);
}
}
@ -233,7 +234,7 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
if (!StringUtils.equals(statusCodeNew, statusCodeOld)) {
String patch = jsonDiff.diff(statusCodeOld, statusCodeNew);
String diff = jsonDiff.apply(statusCodeNew, patch);
if (StringUtils.isNotEmpty(diff)) {
if (StringUtils.isNotBlank(diff)) {
diffMap.put(STATUS_CODE, diff);
}
}
@ -245,7 +246,7 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
if (!StringUtils.equals(bodyStrNew, bodyStrOld)) {
String patch = jsonDiff.diff(bodyStrOld, bodyStrNew);
String diff = jsonDiff.apply(bodyStrNew, patch);
if (StringUtils.isNotEmpty(diff)) {
if (StringUtils.isNotBlank(diff)) {
diffMap.put(BODY, diff);
}
}
@ -253,16 +254,19 @@ public class ApiTestDefinitionDiffUtilImpl implements ApiDefinitionDiffUtil {
Body bodyNew = JSON.parseObject(bodyStrNew, Body.class);
Body bodyOld = JSON.parseObject(bodyStrOld, Body.class);
if (CollectionUtils.isNotEmpty(bodyNew.getKvs()) && CollectionUtils.isNotEmpty(bodyOld.getKvs())) {
bodyNew.getKvs().remove(bodyNew.getKvs().size() - 1);
bodyOld.getKvs().remove(bodyOld.getKvs().size() - 1);
// 对比BODY-FORM参数
if (CollectionUtils.isNotEmpty(bodyNew.getKvs())) {
removeSpaceName(bodyNew.getKvs());
}
if (CollectionUtils.isNotEmpty(bodyOld.getKvs())) {
removeSpaceName(bodyOld.getKvs());
}
String bodyFormNew = StringUtils.join(JSON_START, JSON.toJSONString(bodyNew.getKvs()), JSON_END);
String bodyFormOld = StringUtils.join(JSON_START, JSON.toJSONString(bodyOld.getKvs()), JSON_END);
if (!StringUtils.equals(bodyFormNew, bodyFormOld)) {
String patch = jsonDiff.diff(bodyFormOld, bodyFormNew);
String diff = jsonDiff.apply(bodyFormNew, patch);
if (StringUtils.isNotEmpty(diff)) {
if (StringUtils.isNotBlank(diff)) {
diffMap.put(BODY_FORM, diff);
}
}

View File

@ -178,20 +178,28 @@ public class ReflexObjectUtil {
Object originalObject = JSON.toJSONString(originalValueArray);
oldTags = StringUtils.join(JSON_START, ((originalColumns.get(i) != null && originalObject != null) ? originalObject.toString() : "\"\""), JSON_END);
}
List<String> newValueArray = JSON.parseArray(newValue.toString(), String.class);
if (CollectionUtils.isNotEmpty(newValueArray)) {
Collections.sort(newValueArray);
String newTags = null;
if (newValue != null && !StringUtils.equals("null", newValue.toString())) {
List<String> newValueArray = JSON.parseArray(newValue.toString(), String.class);
if (CollectionUtils.isNotEmpty(newValueArray)) {
Collections.sort(newValueArray);
}
Object newObject = JSON.toJSONString(newValueArray);
newTags = StringUtils.join(JSON_START, ((newColumns.get(i) != null && newObject != null) ? newObject.toString() : "\"\""), JSON_END);
}
Object newObject = JSON.toJSONString(newValueArray);
String newTags = StringUtils.join(JSON_START, ((newColumns.get(i) != null && newObject != null) ? newObject.toString() : "\"\""), JSON_END);
String diffValue;
if (oldTags != null) {
if (StringUtils.isBlank(oldTags) && StringUtils.isBlank(newTags)) {
continue;
}
if (StringUtils.isNotBlank(oldTags) && StringUtils.isNotBlank(newTags)) {
String diffStr = diff.diff(oldTags, newTags);
diffValue = diff.apply(newTags, diffStr);
} else {
diffValue = reviverJson(newTags, "root", DIFF_ADD);
String diffValue = diff.apply(newTags, diffStr);
column.setDiffValue(diffValue);
} else if (StringUtils.isNotBlank(newTags)) {
String diffValue = reviverJson(newTags, "root", DIFF_ADD);
column.setDiffValue(diffValue);
}
column.setDiffValue(diffValue);
}
// 深度对比
else if (StringUtils.equals(module, "API_DEFINITION")) {
@ -199,11 +207,19 @@ public class ReflexObjectUtil {
if (originalColumns.get(i).getColumnName().equals("request")) {
String newValue = Objects.toString(column.getNewValue().toString(), "");
String oldValue = Objects.toString(column.getOriginalValue(), "");
column.setDiffValue(apiDefinitionDiffUtilClass.diff(newValue, oldValue));
String diff = apiDefinitionDiffUtilClass.diff(newValue, oldValue);
if (StringUtils.isBlank(diff)) {
continue;
}
column.setDiffValue(diff);
} else if (originalColumns.get(i).getColumnName().equals("response")) {
String newValue = Objects.toString(column.getNewValue().toString(), "");
String oldValue = Objects.toString(column.getOriginalValue(), "");
column.setDiffValue(apiDefinitionDiffUtilClass.diffResponse(newValue, oldValue));
String diff = apiDefinitionDiffUtilClass.diffResponse(newValue, oldValue);
if (StringUtils.isBlank(diff)) {
continue;
}
column.setDiffValue(diff);
}
}
// 环境全局前后置脚本深度对比
@ -237,15 +253,22 @@ public class ReflexObjectUtil {
}
}
} else {
String newValue = Objects.toString(column.getNewValue(), "");
if (StringUtils.isNotEmpty(newValue)) {
String newValue = Objects.toString(column.getNewValue(), StringUtils.EMPTY);
if (StringUtils.isNotBlank(newValue)) {
column.setNewValue(newValue.replaceAll("\\n", StringUtils.SPACE));
}
String oldValue = Objects.toString(column.getOriginalValue(), StringUtils.EMPTY);
if (StringUtils.isNotEmpty(oldValue)) {
if (StringUtils.isNotBlank(oldValue)) {
column.setOriginalValue(oldValue.replaceAll("\\n", StringUtils.SPACE));
}
if (StringUtils.isBlank(newValue) && StringUtils.isBlank(oldValue)) {
continue;
}
}
if (column.getDiffValue() == null || StringUtils.isBlank(JSON.toJSONString(column.getDiffValue())) || (StringUtils.isBlank(JSON.toJSONString(column.getNewValue())) && StringUtils.isBlank(JSON.toJSONString(column.getOriginalValue())))) {
continue;
}
comparedColumns.add(column);
}
}

View File

@ -339,8 +339,7 @@ public class JsonDiff {
Instruction instruction = new Instruction();
if (childKey.startsWith("-")) {
instruction.key = childKey.substring(1);
//如果是删除多列 diff数据的key都是-1 会把数据给覆盖所以这里 ke+下标 做新的index
instruction.index = isIndexed(instruction.key) == 1 ? isIndexed(instruction.key) : isIndexed(instruction.key) + i;
instruction.index = isIndexed(instruction.key);
instruction.oper = Oper.DELETE;
} else if (childKey.startsWith("+")) {
instruction.key = childKey.substring(1);