feat(接口定义): 提取处理

This commit is contained in:
fit2-zhao 2020-11-23 13:16:27 +08:00
parent ac0d65ee1e
commit 595c9572de
10 changed files with 270 additions and 8 deletions

View File

@ -7,6 +7,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.metersphere.api.dto.definition.request.assertions.MsAssertions;
import io.metersphere.api.dto.definition.request.auth.MsAuthManager;
import io.metersphere.api.dto.definition.request.configurations.MsHeaderManager;
import io.metersphere.api.dto.definition.request.extract.MsExtract;
import io.metersphere.api.dto.definition.request.processors.post.MsJSR223PostProcessor;
import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
@ -31,9 +32,10 @@ import java.util.List;
@JsonSubTypes.Type(value = MsThreadGroup.class, name = "ThreadGroup"),
@JsonSubTypes.Type(value = MsAuthManager.class, name = "AuthManager"),
@JsonSubTypes.Type(value = MsAssertions.class, name = "Assertions"),
@JsonSubTypes.Type(value = MsExtract.class, name = "Extract"),
})
@JSONType(seeAlso = {MsHTTPSamplerProxy.class, MsHeaderManager.class, MsJSR223PostProcessor.class, MsJSR223PreProcessor.class, MsTestPlan.class, MsThreadGroup.class, AuthManager.class, MsAssertions.class}, typeKey = "type")
@JSONType(seeAlso = {MsHTTPSamplerProxy.class, MsHeaderManager.class, MsJSR223PostProcessor.class, MsJSR223PreProcessor.class, MsTestPlan.class, MsThreadGroup.class, AuthManager.class, MsAssertions.class, MsExtract.class}, typeKey = "type")
@Data
public abstract class MsTestElement {
private String type;

View File

@ -4,6 +4,10 @@ import com.alibaba.fastjson.annotation.JSONType;
import io.metersphere.api.dto.definition.request.MsTestElement;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.jmeter.assertions.*;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.util.List;
@ -20,12 +24,107 @@ public class MsAssertions extends MsTestElement {
private String type = "Assertions";
public void toHashTree(HashTree tree, List<MsTestElement> hashTree) {
// final HashTree testPlanTree = tree.add(getPlan());
// if (CollectionUtils.isNotEmpty(hashTree)) {
// hashTree.forEach(el -> {
// el.toHashTree(testPlanTree, el.getHashTree());
// });
// }
addAssertions(tree);
}
private void addAssertions(HashTree hashTree) {
if (CollectionUtils.isNotEmpty(this.getRegex())) {
this.getRegex().stream().filter(MsAssertionRegex::isValid).forEach(assertion ->
hashTree.add(responseAssertion(assertion))
);
}
if (CollectionUtils.isNotEmpty(this.getJsonPath())) {
this.getJsonPath().stream().filter(MsAssertionJsonPath::isValid).forEach(assertion ->
hashTree.add(jsonPathAssertion(assertion))
);
}
if (CollectionUtils.isNotEmpty(this.getXpath2())) {
this.getXpath2().stream().filter(MsAssertionXPath2::isValid).forEach(assertion ->
hashTree.add(xPath2Assertion(assertion))
);
}
if (CollectionUtils.isNotEmpty(this.getJsr223())) {
this.getJsr223().stream().filter(MsAssertionJSR223::isValid).forEach(assertion ->
hashTree.add(jsr223Assertion(assertion))
);
}
if (this.getDuration().isValid()) {
hashTree.add(durationAssertion(this.getDuration()));
}
}
private ResponseAssertion responseAssertion(MsAssertionRegex assertionRegex) {
ResponseAssertion assertion = new ResponseAssertion();
assertion.setEnabled(true);
assertion.setName(assertionRegex.getDescription());
assertion.setProperty(TestElement.TEST_CLASS, ResponseAssertion.class.getName());
assertion.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("AssertionGui"));
assertion.setAssumeSuccess(assertionRegex.isAssumeSuccess());
assertion.setToContainsType();
switch (assertionRegex.getSubject()) {
case "Response Code":
assertion.setTestFieldResponseCode();
break;
case "Response Headers":
assertion.setTestFieldResponseHeaders();
break;
case "Response Data":
assertion.setTestFieldResponseData();
break;
}
return assertion;
}
private JSONPathAssertion jsonPathAssertion(MsAssertionJsonPath assertionJsonPath) {
JSONPathAssertion assertion = new JSONPathAssertion();
assertion.setEnabled(true);
assertion.setName(assertionJsonPath.getDescription());
assertion.setProperty(TestElement.TEST_CLASS, JSONPathAssertion.class.getName());
assertion.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("JSONPathAssertionGui"));
assertion.setJsonPath(assertionJsonPath.getExpression());
assertion.setExpectedValue(assertionJsonPath.getExpect());
assertion.setJsonValidationBool(true);
assertion.setExpectNull(false);
assertion.setInvert(false);
assertion.setIsRegex(true);
return assertion;
}
private XPath2Assertion xPath2Assertion(MsAssertionXPath2 assertionXPath2) {
XPath2Assertion assertion = new XPath2Assertion();
assertion.setEnabled(true);
assertion.setName(assertionXPath2.getExpression());
assertion.setProperty(TestElement.TEST_CLASS, XPath2Assertion.class.getName());
assertion.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("XPath2AssertionGui"));
assertion.setXPathString(assertionXPath2.getExpression());
assertion.setNegated(false);
return assertion;
}
private DurationAssertion durationAssertion(MsAssertionDuration assertionDuration) {
DurationAssertion assertion = new DurationAssertion();
assertion.setEnabled(true);
assertion.setName("Response In Time: " + assertionDuration.getValue());
assertion.setProperty(TestElement.TEST_CLASS, DurationAssertion.class.getName());
assertion.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("DurationAssertionGui"));
assertion.setAllowedDuration(assertionDuration.getValue());
return assertion;
}
private JSR223Assertion jsr223Assertion(MsAssertionJSR223 assertionJSR223) {
JSR223Assertion assertion = new JSR223Assertion();
assertion.setEnabled(true);
assertion.setName(assertionJSR223.getDesc());
assertion.setProperty(TestElement.TEST_CLASS, JSR223Assertion.class.getName());
assertion.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
assertion.setProperty("cacheKey", "true");
assertion.setProperty("scriptLanguage", assertionJSR223.getLanguage());
assertion.setProperty("script", assertionJSR223.getScript());
return assertion;
}
}

View File

@ -0,0 +1,91 @@
package io.metersphere.api.dto.definition.request.extract;
import com.alibaba.fastjson.annotation.JSONType;
import io.metersphere.api.dto.definition.request.MsTestElement;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.jmeter.extractor.RegexExtractor;
import org.apache.jmeter.extractor.XPath2Extractor;
import org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.util.List;
@Data
@EqualsAndHashCode(callSuper = true)
@JSONType(typeName = "Extract")
public class MsExtract extends MsTestElement {
private List<MsExtractRegex> regex;
private List<MsExtractJSONPath> json;
private List<MsExtractXPath> xpath;
private String type = "Extract";
public void toHashTree(HashTree tree, List<MsTestElement> hashTree) {
addRequestExtractors(tree);
}
private void addRequestExtractors(HashTree samplerHashTree) {
if (CollectionUtils.isNotEmpty(this.getRegex())) {
this.getRegex().stream().filter(MsExtractRegex::isValid).forEach(extractRegex ->
samplerHashTree.add(regexExtractor(extractRegex))
);
}
if (CollectionUtils.isNotEmpty(this.getXpath())) {
this.getXpath().stream().filter(MsExtractCommon::isValid).forEach(extractXPath ->
samplerHashTree.add(xPath2Extractor(extractXPath))
);
}
if (CollectionUtils.isNotEmpty(this.getJson())) {
this.getJson().stream().filter(MsExtractCommon::isValid).forEach(extractJSONPath ->
samplerHashTree.add(jsonPostProcessor(extractJSONPath))
);
}
}
private RegexExtractor regexExtractor(MsExtractRegex extractRegex) {
RegexExtractor extractor = new RegexExtractor();
extractor.setEnabled(true);
extractor.setName(extractRegex.getVariable() + " RegexExtractor");
extractor.setProperty(TestElement.TEST_CLASS, RegexExtractor.class.getName());
extractor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("RegexExtractorGui"));
extractor.setRefName(extractRegex.getVariable());
extractor.setRegex(extractRegex.getExpression());
extractor.setUseField(extractRegex.getUseHeaders());
if (extractRegex.isMultipleMatching()) {
extractor.setMatchNumber(-1);
}
extractor.setTemplate("$1$");
return extractor;
}
private XPath2Extractor xPath2Extractor(MsExtractXPath extractXPath) {
XPath2Extractor extractor = new XPath2Extractor();
extractor.setEnabled(true);
extractor.setName(extractXPath.getVariable() + " XPath2Extractor");
extractor.setProperty(TestElement.TEST_CLASS, XPath2Extractor.class.getName());
extractor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("XPath2ExtractorGui"));
extractor.setRefName(extractXPath.getVariable());
extractor.setXPathQuery(extractXPath.getExpression());
if (extractXPath.isMultipleMatching()) {
extractor.setMatchNumber(-1);
}
return extractor;
}
private JSONPostProcessor jsonPostProcessor(MsExtractJSONPath extractJSONPath) {
JSONPostProcessor extractor = new JSONPostProcessor();
extractor.setEnabled(true);
extractor.setName(extractJSONPath.getVariable() + " JSONExtractor");
extractor.setProperty(TestElement.TEST_CLASS, JSONPostProcessor.class.getName());
extractor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("JSONPostProcessorGui"));
extractor.setRefNames(extractJSONPath.getVariable());
extractor.setJsonPathExpressions(extractJSONPath.getExpression());
if (extractJSONPath.isMultipleMatching()) {
extractor.setMatchNumbers("-1");
}
return extractor;
}
}

View File

@ -0,0 +1,19 @@
package io.metersphere.api.dto.definition.request.extract;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
@EqualsAndHashCode(callSuper = true)
@Data
public class MsExtractCommon extends MsExtractType{
private String variable;
private String value; // value: ${variable}
private String expression;
private String description;
private boolean multipleMatching;
public boolean isValid() {
return StringUtils.isNotBlank(variable) && StringUtils.isNotBlank(expression);
}
}

View File

@ -0,0 +1,12 @@
package io.metersphere.api.dto.definition.request.extract;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class MsExtractJSONPath extends MsExtractCommon {
public MsExtractJSONPath() {
setType(MsExtractType.JSON_PATH);
}
}

View File

@ -0,0 +1,14 @@
package io.metersphere.api.dto.definition.request.extract;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class MsExtractRegex extends MsExtractCommon {
private String useHeaders;
public MsExtractRegex() {
setType(MsExtractType.REGEX);
}
}

View File

@ -0,0 +1,12 @@
package io.metersphere.api.dto.definition.request.extract;
import lombok.Data;
@Data
public class MsExtractType {
public final static String REGEX = "Regex";
public final static String JSON_PATH = "JSONPath";
public final static String XPATH = "XPath";
private String type;
}

View File

@ -0,0 +1,12 @@
package io.metersphere.api.dto.definition.request.extract;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class MsExtractXPath extends MsExtractCommon {
public MsExtractXPath() {
setType(MsExtractType.XPATH);
}
}

View File

@ -8,6 +8,7 @@ import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
@Data
@ColumnWidth(15)
public class TestCaseExcelDataUs extends TestCaseExcelData {

View File

@ -36,7 +36,7 @@ export default {
...requireContext.keys().map(key => requireContext(key).license),
...requireContext.keys().map(key => requireContext(key).display),
{
path: 'organizationmember',
path: 'organizationpmnmember',
component: () => import('@/business/components/settings/organization/OrganizationMember'),
meta: {organization: true, title: 'commons.member'}
},