refactor(接口测试): 增加文档断言解析

This commit is contained in:
wxg0103 2024-02-06 10:01:15 +08:00 committed by Craftsman
parent e3730b3bba
commit e852795027
11 changed files with 347 additions and 32 deletions

View File

@ -9,7 +9,7 @@ import io.metersphere.api.dto.request.http.*;
import io.metersphere.api.dto.request.http.auth.NoAuth;
import io.metersphere.api.dto.request.http.body.*;
import io.metersphere.api.dto.schema.JsonSchemaItem;
import io.metersphere.api.constants.PropertyConstant;
import io.metersphere.project.constants.PropertyConstant;
import io.metersphere.api.parser.ImportParser;
import io.metersphere.api.utils.ApiDataUtils;
import io.metersphere.sdk.exception.MSException;

View File

@ -1,27 +1,166 @@
package io.metersphere.api.parser.jmeter.processor.assertion.body;
import io.metersphere.project.api.assertion.body.MsDocumentAssertion;
import io.metersphere.assertions.JSONPathAssertion;
import io.metersphere.assertions.XMLAssertion;
import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.project.api.assertion.body.Condition;
import io.metersphere.project.api.assertion.body.ElementCondition;
import io.metersphere.project.api.assertion.body.MsDocumentAssertion;
import io.metersphere.project.api.assertion.body.MsDocumentAssertionElement;
import io.metersphere.project.constants.PropertyConstant;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @Author: jianxing
* @CreateTime: 2024-01-03 10:05
*/
public class DocumentAssertionConverter extends ResponseBodyTypeAssertionConverter<MsDocumentAssertion> {
@Override
public void parse(HashTree hashTree, MsDocumentAssertion msAssertion, ParameterConfig config, boolean isIgnoreStatus, boolean globalEnable) {
if (msAssertion == null) {
return;
}
// todo 定义好 jsonschema 再补充
// String documentType = msAssertion.getDocumentType();
// MsDocumentAssertionElement documentAssertionElement;
// if (StringUtils.equals(documentType, MsDocumentAssertion.DocumentType.XML.name())) {
// documentAssertionElement = msAssertion.getXmlAssertion();
// } else {
// documentAssertionElement = msAssertion.getJsonAssertion();
// }
if (needParse(msAssertion, config)) {
String documentType = msAssertion.getDocumentType();
if (StringUtils.equals(documentType, MsDocumentAssertion.DocumentType.XML.name())) {
hashTree.add(parseAssertion(msAssertion.getXmlAssertion(), globalEnable, new XMLAssertion()));
} else {
hashTree.add(parseAssertion(msAssertion.getJsonAssertion(), globalEnable, new JSONPathAssertion()));
}
}
}
private <T extends TestElement> List<T> parseAssertion(MsDocumentAssertionElement assertion, boolean globalEnable, T t) {
List<T> list = new LinkedList<>();
Map<String, ElementCondition> conditionMap = new java.util.HashMap<>();
List<MsDocumentAssertionElement> dataList = new ArrayList<>();
dataList.add(assertion);
conditions(dataList, conditionMap);
formatting(dataList, list, null, conditionMap, globalEnable, t);
return list;
}
private void conditions(List<MsDocumentAssertionElement> dataList, Map<String, ElementCondition> conditionMap) {
dataList.forEach(item -> {
ElementCondition elementCondition = new ElementCondition(item.getInclude(), item.getTypeVerification(), item.getArrayVerification(), new LinkedList<Condition>() {{
this.add(new Condition(item.getCondition(), item.getExpectedResult()));
}});
elementCondition.setType(item.getType());
conditionMap.put(item.getId(), elementCondition);
if (CollectionUtils.isNotEmpty(item.getChildren())) {
conditions(item.getChildren(), conditionMap);
}
});
}
public <T extends TestElement> void formatting(List<MsDocumentAssertionElement> dataList,
List<T> list,
MsDocumentAssertionElement parentNode,
Map<String, ElementCondition> conditionMap,
boolean globalEnable,
T t) {
for (int i = 0; i < dataList.size(); i++) {
MsDocumentAssertionElement item = dataList.get(i);
if (!item.getId().equals(PropertyConstant.ROOT)) {
if (parentNode != null) {
if (parentNode.getType().equals(PropertyConstant.ARRAY)) {
try {
int index = StringUtils.isNotEmpty(item.getParamName()) ? Integer.parseInt(item.getParamName()) : i;
item.setJsonPath(parentNode.getJsonPath() + "[" + index + "]");
} catch (Exception e) {
item.setJsonPath(parentNode.getJsonPath() + "." + item.getParamName());
}
} else {
item.setJsonPath(parentNode.getJsonPath() + "." + item.getParamName());
}
} else {
item.setJsonPath("$." + item.getParamName());
}
if (!StringUtils.equalsAny(item.getCondition(), PropertyConstant.NONE, null) || item.getInclude() || item.getArrayVerification() || item.getTypeVerification()) {
list.add(newAssertion(item, conditionMap.get(item.getId()), globalEnable, t));
}
if (CollectionUtils.isNotEmpty(item.getChildren())) {
if (item.getType().equals(PropertyConstant.ARRAY) && !item.getArrayVerification()) {
continue;
}
formatting(item.getChildren(), list, item, conditionMap, globalEnable, t);
}
} else {
if (CollectionUtils.isNotEmpty(item.getChildren())) {
formatting(item.getChildren(), list, null, conditionMap, globalEnable, t);
}
}
}
}
private String getConditionStr(MsDocumentAssertionElement item, ElementCondition elementCondition) {
StringBuilder conditionStr = new StringBuilder();
if (elementCondition != null && CollectionUtils.isNotEmpty(elementCondition.getConditions())) {
if (elementCondition.isTypeVerification()) {
conditionStr.append("类型:[ ").append(item.getType()).append(" ]");
conditionStr.append(" and ");
}
elementCondition.getConditions().forEach(condition -> {
if (!StringUtils.equals(PropertyConstant.NONE, condition.getKey())) {
conditionStr.append(getLabel(condition.getKey()).replace("'%'", (condition.getValue() != null ? condition.getValue().toString() : "")));
conditionStr.append(" and ");
}
});
}
String label = "";
if (StringUtils.isNotEmpty(conditionStr.toString())) {
label = conditionStr.substring(0, conditionStr.toString().length() - 4);
}
return label;
}
private <T extends TestElement> T newAssertion(MsDocumentAssertionElement item, ElementCondition elementCondition, boolean globalEnable, T assertion) {
assertion.setEnabled(true);
if (BooleanUtils.isFalse(globalEnable)) {
// 如果整体禁用则禁用
assertion.setEnabled(false);
}
if (assertion instanceof XMLAssertion xmlAssertion) {
xmlAssertion.setJsonValidationBool(true);
}
assertion.setName(String.format("Response date expect %s %s %s", item.getJsonPath(), getConditionStr(item, elementCondition).toLowerCase().replace("_", ""), item.getExpectedResult()));
assertion.setProperty(TestElement.TEST_CLASS, assertion.getClass().getName());
assertion.setProperty(TestElement.GUI_CLASS, "io.metersphere.assertions.gui." + assertion.getClass().getSimpleName() + "Gui");
assertion.setProperty("JSON_PATH", item.getJsonPath());
assertion.setProperty("EXPECTED_VALUE", item.getExpectedResult() != null ? item.getExpectedResult().toString() : "");
assertion.setProperty("CONDITION", item.getCondition());
assertion.setProperty("JSONVALIDATION", true);
return assertion;
}
public String getLabel(String value) {
return switch (value) {
case "EQUALS" -> "值-等于[value='%']";
case "NOT_EQUALS" -> "值-不等于[value!='%']";
case "CONTAINS" -> "值-包含[include='%']";
case "LENGTH_EQUALS" -> "长度-等于[length='%']";
case "LENGTH_NOT_EQUALS" -> "长度-不等于[length!='%']";
case "LENGTH_GT" -> "长度-大于[length>'%']";
case "LENGTH_LT" -> "长度-小于[length<'%']";
case "REGEX" -> "正则匹配";
default -> "不校验[]";
};
}

View File

@ -6,13 +6,13 @@ import io.metersphere.api.domain.ApiDefinition;
import io.metersphere.api.domain.ApiDefinitionBlob;
import io.metersphere.api.domain.ApiDefinitionBlobExample;
import io.metersphere.api.domain.ApiDefinitionModule;
import io.metersphere.api.dto.converter.ApiDefinitionImport;
import io.metersphere.api.dto.converter.ApiDefinitionImportDetail;
import io.metersphere.api.dto.converter.ApiDetailWithData;
import io.metersphere.api.dto.converter.ApiDetailWithDataUpdate;
import io.metersphere.api.dto.definition.ApiDefinitionDTO;
import io.metersphere.api.dto.definition.ApiDefinitionPageRequest;
import io.metersphere.api.dto.definition.ApiModuleRequest;
import io.metersphere.api.dto.converter.ApiDetailWithData;
import io.metersphere.api.dto.converter.ApiDetailWithDataUpdate;
import io.metersphere.api.dto.converter.ApiDefinitionImport;
import io.metersphere.api.dto.converter.ApiDefinitionImportDetail;
import io.metersphere.api.dto.request.ImportRequest;
import io.metersphere.api.dto.request.http.Header;
import io.metersphere.api.dto.request.http.MsHTTPElement;
@ -20,9 +20,9 @@ import io.metersphere.api.dto.request.http.QueryParam;
import io.metersphere.api.dto.request.http.RestParam;
import io.metersphere.api.dto.request.http.body.*;
import io.metersphere.api.dto.schema.JsonSchemaItem;
import io.metersphere.api.constants.PropertyConstant;
import io.metersphere.api.mapper.*;
import io.metersphere.api.utils.ApiDataUtils;
import io.metersphere.project.constants.PropertyConstant;
import io.metersphere.project.domain.Project;
import io.metersphere.project.mapper.ExtBaseProjectVersionMapper;
import io.metersphere.project.mapper.ProjectApplicationMapper;
@ -161,12 +161,12 @@ public class ApiDefinitionImportUtilService {
apiLists.forEach(t -> {
t.setModulePath(idModuleMap.get(t.getModuleId()) != null ? idModuleMap.get(t.getModuleId()).getPath() : StringUtils.EMPTY);
});
ApiDetailWithData apiDeatlWithData = new ApiDetailWithData();
ApiDetailWithData apiDealWithData = new ApiDetailWithData();
//判断数据是否是唯一的
checkApiDataOnly(request, importData, apiLists, apiDeatlWithData);
checkApiDataOnly(request, importData, apiLists, apiDealWithData);
ApiDetailWithDataUpdate apiDetailWithDataUpdate = new ApiDetailWithDataUpdate();
getNeedUpdateData(request, apiDeatlWithData, apiDetailWithDataUpdate);
getNeedUpdateData(request, apiDealWithData, apiDetailWithDataUpdate);
//数据入库
insertData(modulePathMap, idModuleMap, apiDetailWithDataUpdate, request, user);

View File

@ -5,9 +5,9 @@ import io.metersphere.api.controller.result.ApiResultCode;
import io.metersphere.api.domain.ApiDebug;
import io.metersphere.api.domain.ApiDebugBlob;
import io.metersphere.api.domain.ApiFileResource;
import io.metersphere.api.dto.assertion.MsAssertionConfig;
import io.metersphere.api.dto.debug.*;
import io.metersphere.api.dto.request.MsCommonElement;
import io.metersphere.api.dto.assertion.MsAssertionConfig;
import io.metersphere.api.dto.request.http.MsHTTPElement;
import io.metersphere.api.dto.request.http.body.Body;
import io.metersphere.api.mapper.ApiDebugBlobMapper;
@ -275,6 +275,7 @@ public class ApiDebugControllerTests extends BaseTest {
/**
* 校验上传的文件
*
* @param id
* @param fileIds 全部的文件ID
*/
@ -304,6 +305,7 @@ public class ApiDebugControllerTests extends BaseTest {
/**
* 校验上传的文件
*
* @param id
* @param fileIds 全部的文件ID
*/
@ -394,6 +396,20 @@ public class ApiDebugControllerTests extends BaseTest {
request.setRequest(getMsElementParam(msHTTPElement));
this.requestPostWithOk(DEBUG, request);
msAssertionConfig = new MsAssertionConfig();
msAssertionConfig.setEnableGlobal(false);
msAssertionConfig.setAssertions(MsHTTPElementTest.getGeneralXmlAssertions());
msCommonElement = new MsCommonElement();
msCommonElement.setAssertionConfig(msAssertionConfig);
linkedList = new LinkedList();
linkedList.add(msCommonElement);
msHTTPElement = MsHTTPElementTest.getMsHttpElement();
msHTTPElement.setChildren(linkedList);
msHTTPElement.setEnable(true);
request.setRequest(getMsElementParam(msHTTPElement));
this.requestPostWithOk(DEBUG, request);
// 测试请求体
MockMultipartFile file = getMockMultipartFile();
String fileId = doUploadTempFile(file);

View File

@ -11,25 +11,25 @@ import io.metersphere.api.dto.request.http.auth.DigestAuth;
import io.metersphere.api.dto.request.http.auth.HTTPAuth;
import io.metersphere.api.dto.request.http.auth.NoAuth;
import io.metersphere.api.dto.request.http.body.*;
import io.metersphere.api.dto.request.processors.*;
import io.metersphere.project.api.assertion.*;
import io.metersphere.project.api.assertion.body.*;
import io.metersphere.project.api.processor.extract.JSONPathExtract;
import io.metersphere.project.api.processor.extract.RegexExtract;
import io.metersphere.project.api.processor.extract.ResultMatchingExtract;
import io.metersphere.project.api.processor.extract.XPathExtract;
import io.metersphere.project.api.processor.ExtractPostProcessor;
import io.metersphere.api.dto.request.processors.MsProcessorConfig;
import io.metersphere.api.dto.schema.JsonSchemaItem;
import io.metersphere.api.parser.TestElementParser;
import io.metersphere.api.parser.TestElementParserFactory;
import io.metersphere.api.utils.ApiDataUtils;
import io.metersphere.plugin.api.dto.ParameterConfig;
import io.metersphere.plugin.api.spi.AbstractMsTestElement;
import io.metersphere.project.constants.ScriptLanguageType;
import io.metersphere.project.api.KeyValueEnableParam;
import io.metersphere.project.api.assertion.*;
import io.metersphere.project.api.assertion.body.*;
import io.metersphere.project.api.processor.ExtractPostProcessor;
import io.metersphere.project.api.processor.SQLProcessor;
import io.metersphere.project.api.processor.ScriptProcessor;
import io.metersphere.project.api.processor.TimeWaitingProcessor;
import io.metersphere.project.api.processor.extract.JSONPathExtract;
import io.metersphere.project.api.processor.extract.RegexExtract;
import io.metersphere.project.api.processor.extract.ResultMatchingExtract;
import io.metersphere.project.api.processor.extract.XPathExtract;
import io.metersphere.project.constants.ScriptLanguageType;
import io.metersphere.sdk.constants.MsAssertionCondition;
import io.metersphere.sdk.util.BeanUtils;
import org.junit.jupiter.api.Assertions;
@ -268,6 +268,35 @@ public class MsHTTPElementTest {
defaultParser.parse(msTestElement, parameterConfig);
}
@Test
public void msAssertionTestXml() {
MsHTTPElement msHTTPElement = getMsHttpElement();
List<MsAssertion> assertions = getGeneralXmlAssertions();
MsAssertionConfig msAssertionConfig = new MsAssertionConfig();
msAssertionConfig.setEnableGlobal(false);
msAssertionConfig.setAssertions(assertions);
MsCommonElement msCommonElement = new MsCommonElement();
msCommonElement.setAssertionConfig(msAssertionConfig);
LinkedList linkedList = new LinkedList();
linkedList.add(msCommonElement);
msHTTPElement.setChildren(linkedList);
String json = ApiDataUtils.toJSONString(msHTTPElement);
Assertions.assertNotNull(json);
Assertions.assertEquals(ApiDataUtils.parseObject(json, AbstractMsTestElement.class), msHTTPElement);
// 测试脚本解析
ParameterConfig parameterConfig = new ApiParamConfig();
parameterConfig.setReportId("reportId");
TestElementParser defaultParser = TestElementParserFactory.getDefaultParser();
AbstractMsTestElement msTestElement = ApiDataUtils.parseObject(json, AbstractMsTestElement.class);
defaultParser.parse(msTestElement, parameterConfig);
}
public static List<MsAssertion> getGeneralAssertions() {
List<MsAssertion> assertions = new ArrayList<>();
MsResponseCodeAssertion responseCodeAssertion = new MsResponseCodeAssertion();
@ -297,6 +326,39 @@ public class MsHTTPElementTest {
MsResponseBodyAssertion documentResponseBodyAssertion = new MsResponseBodyAssertion();
documentResponseBodyAssertion.setAssertionBodyType(MsResponseBodyAssertion.MsBodyAssertionType.DOCUMENT.name());
MsDocumentAssertion msDocumentAssertion = new MsDocumentAssertion();
MsDocumentAssertionElement item = new MsDocumentAssertionElement();
item.setId("1");
item.setParamName("pet");
item.setInclude(false);
item.setType("object");
item.setCondition("none");
item.setTypeVerification(false);
item.setExpectedResult("");
MsDocumentAssertionElement item1 = new MsDocumentAssertionElement();
item1.setId("2");
item1.setParamName("id");
item1.setInclude(false);
item1.setType("integer");
item1.setCondition("EQUALS");
item1.setExpectedResult("1");
MsDocumentAssertionElement item2 = new MsDocumentAssertionElement();
item2.setId("3");
item2.setParamName("attributes");
item2.setInclude(false);
item2.setType("string");
item2.setCondition("EQUALS");
item2.setExpectedResult("s,2");
MsDocumentAssertionElement item3 = new MsDocumentAssertionElement();
item3.setId("4");
item3.setParamName("array");
item3.setInclude(false);
item3.setArrayVerification(true);
item3.setType("array");
item3.setCondition("EQUALS");
item3.setExpectedResult("s,2");
item.setChildren(List.of(item1, item2, item3));
msDocumentAssertion.setJsonAssertion(item);
msDocumentAssertion.setDocumentType("JSON");
documentResponseBodyAssertion.setDocumentAssertion(msDocumentAssertion);
assertions.add(documentResponseBodyAssertion);
@ -344,6 +406,51 @@ public class MsHTTPElementTest {
return assertions;
}
public static List<MsAssertion> getGeneralXmlAssertions() {
List<MsAssertion> assertions = new ArrayList<>();
MsResponseBodyAssertion documentResponseBodyAssertion = new MsResponseBodyAssertion();
documentResponseBodyAssertion.setAssertionBodyType(MsResponseBodyAssertion.MsBodyAssertionType.DOCUMENT.name());
MsDocumentAssertion msDocumentAssertion = new MsDocumentAssertion();
MsDocumentAssertionElement item = new MsDocumentAssertionElement();
item.setId("1");
item.setParamName("pet");
item.setInclude(false);
item.setType("object");
item.setCondition("none");
item.setTypeVerification(false);
item.setExpectedResult("");
MsDocumentAssertionElement item1 = new MsDocumentAssertionElement();
item1.setId("2");
item1.setParamName("id");
item1.setInclude(false);
item1.setType("integer");
item1.setCondition("EQUALS");
item1.setExpectedResult("1");
MsDocumentAssertionElement item2 = new MsDocumentAssertionElement();
item2.setId("3");
item2.setParamName("attributes");
item2.setInclude(false);
item2.setType("string");
item2.setCondition("EQUALS");
item2.setExpectedResult("s,2");
MsDocumentAssertionElement item3 = new MsDocumentAssertionElement();
item3.setId("4");
item3.setParamName("array");
item3.setInclude(false);
item3.setArrayVerification(true);
item3.setType("array");
item3.setCondition("EQUALS");
item3.setExpectedResult("s,2");
item.setChildren(List.of(item1, item2, item3));
msDocumentAssertion.setXmlAssertion(item);
msDocumentAssertion.setDocumentType("XML");
documentResponseBodyAssertion.setDocumentAssertion(msDocumentAssertion);
assertions.add(documentResponseBodyAssertion);
return assertions;
}
public static MsHTTPElement getMsHttpElement() {
MsHTTPElement msHTTPElement = new MsHTTPElement();
msHTTPElement.setUrl("http://www.test.com");

View File

@ -0,0 +1,18 @@
package io.metersphere.project.api.assertion.body;
import lombok.Data;
@Data
public class Condition {
private String key;
private Object value;
public Condition() {
}
public Condition(String key, Object value) {
this.key = key;
this.value = value;
}
}

View File

@ -0,0 +1,22 @@
package io.metersphere.project.api.assertion.body;
import lombok.Data;
import java.util.List;
@Data
public class ElementCondition {
private boolean include;
private boolean typeVerification;
private boolean arrayVerification;
private String type;
List<Condition> conditions;
public ElementCondition(boolean include, boolean typeVerification, boolean arrayVerification, List<Condition> conditions) {
this.include = include;
this.typeVerification = typeVerification;
this.arrayVerification = arrayVerification;
this.conditions = conditions;
}
}

View File

@ -7,6 +7,7 @@ import lombok.Data;
/**
* 文档断言
*
* @Author: jianxing
* @CreateTime: 2023-11-23 14:19
*/
@ -15,6 +16,7 @@ public class MsDocumentAssertion extends MsBodyAssertionItem {
/**
* 跟随定义的apiId
* 传空为不跟随接口定义
* 如果选择了
*/
@Size(max = 50)
private String followApiId;
@ -25,9 +27,10 @@ public class MsDocumentAssertion extends MsBodyAssertionItem {
* 这里跟前端数据结构有差异
* 后端从设计层面支持多种文档格式前端只支持一种
* 同时切换可以同时持久化两种格式
*
* <p>
* 取值参考 {@link DocumentType}
= */
* =
*/
@EnumValue(enumClass = DocumentType.class)
private String documentType;
/**

View File

@ -12,6 +12,7 @@ import java.util.List;
/**
* 文档断言项
*
* @Author: jianxing
* @CreateTime: 2023-11-23 11:43
*/
@ -52,12 +53,16 @@ public class MsDocumentAssertionElement {
/**
* 组内校验
*/
private Boolean arrayVerification;
private Boolean arrayVerification = false;
/**
* 子对象
*/
@Valid
private List<MsDocumentAssertionElement> children;
/**
* 在执行时组装数据用
*/
private String jsonPath;
/**
* 文档断言类型
@ -80,4 +85,5 @@ public class MsDocumentAssertionElement {
return value;
}
}
}

View File

@ -1,4 +1,4 @@
package io.metersphere.api.constants;
package io.metersphere.project.constants;
public class PropertyConstant {
public final static String REQUIRED = "required";
@ -10,5 +10,9 @@ public class PropertyConstant {
public final static String INTEGER = "integer";
public final static String NULL = "null";
public final static String NONE = "none";
public final static String ROOT = "root";
public final static String EXPECTED_VALUE = "EXPECTED_VALUE";
public final static String ELEMENT_CONDITION = "ElementCondition";
public final static String XML_PATH = "XML_PATH";
}

View File

@ -28,7 +28,7 @@
<guava.version>33.0.0-jre</guava.version>
<pagehelper.version>6.1.0</pagehelper.version>
<metersphere-jmeter-functions.version>3.0</metersphere-jmeter-functions.version>
<metersphere-jmeter-assertions.version>1.0.1</metersphere-jmeter-assertions.version>
<metersphere-jmeter-assertions.version>1.0.2</metersphere-jmeter-assertions.version>
<quartz-starter.version>1.0.7</quartz-starter.version>
<redisson-starter.version>3.26.0</redisson-starter.version>
<mybatis-starter.version>3.0.3</mybatis-starter.version>