fix(接口自动化): 添加执行时 mock 处理

This commit is contained in:
fit2-zhao 2020-12-29 09:56:26 +08:00
parent f5ea7d25b9
commit 711c787f8d
3 changed files with 11 additions and 45 deletions

View File

@ -13,6 +13,7 @@ import io.metersphere.api.service.ApiTestEnvironmentService;
import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.ScriptEngineUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
@ -222,7 +223,8 @@ public class MsHTTPSamplerProxy extends MsTestElement {
stringBuffer.append("/");
Map<String, String> keyValueMap = new HashMap<>();
this.getRest().stream().filter(KeyValue::isEnable).filter(KeyValue::isValid).forEach(keyValue ->
keyValueMap.put(keyValue.getName(), keyValue.getValue())
keyValueMap.put(keyValue.getName(), keyValue.getValue() != null && keyValue.getValue().startsWith("@") ?
ScriptEngineUtils.calculate(keyValue.getValue()) : keyValue.getValue())
);
try {
Pattern p = Pattern.compile("(\\{)([\\w]+)(\\})");
@ -244,7 +246,8 @@ public class MsHTTPSamplerProxy extends MsTestElement {
stringBuffer.append(path);
stringBuffer.append("?");
this.getArguments().stream().filter(KeyValue::isEnable).filter(KeyValue::isValid).forEach(keyValue ->
stringBuffer.append(keyValue.getName()).append("=").append(keyValue.getValue()).append("&")
stringBuffer.append(keyValue.getName()).append("=").append(keyValue.getValue() != null && keyValue.getValue().startsWith("@") ?
ScriptEngineUtils.calculate(keyValue.getValue()) : keyValue.getValue()).append("&")
);
return stringBuffer.substring(0, stringBuffer.length() - 1);
}
@ -252,7 +255,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
private Arguments httpArguments(List<KeyValue> list) {
Arguments arguments = new Arguments();
list.stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue -> {
HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), keyValue.getValue());
HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), keyValue.getValue() != null && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.calculate(keyValue.getValue()) : keyValue.getValue());
httpArgument.setAlwaysEncoded(keyValue.isEncode());
if (StringUtils.isNotBlank(keyValue.getContentType())) {
httpArgument.setContentType(keyValue.getContentType());

View File

@ -13,7 +13,6 @@ import io.metersphere.api.dto.definition.request.*;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.parse.old.JmeterDocumentParser;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiScenarioMapper;
import io.metersphere.base.mapper.ApiScenarioReportMapper;
@ -39,7 +38,6 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.jmeter.save.SaveService;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.ListedHashTree;
import org.springframework.stereotype.Service;
@ -47,8 +45,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
@ -353,25 +349,14 @@ public class ApiAutomationService {
} catch (Exception ex) {
LogUtil.error(ex.getMessage());
}
testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), new ParameterConfig());
testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), new ParameterConfig());
String runMode = ApiRunMode.SCENARIO.name();
if (StringUtils.isNotBlank(request.getRunMode()) && StringUtils.equals(request.getRunMode(), ApiRunMode.SCENARIO_PLAN.name())) {
runMode = ApiRunMode.SCENARIO_PLAN.name();
}
// 调用执行方法
String jmx = testPlan.getJmx(jmeterHashTree);
byte[] bytes = JmeterDocumentParser.parse(jmx.getBytes());
InputStream is = new ByteArrayInputStream(bytes);
try {
Object scriptWrapper = SaveService.loadElement(is);
HashTree hashTree = JMeterService.getHashTree(scriptWrapper);
// 调用执行方法
jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), runMode);
} catch (Exception e) {
e.printStackTrace();
LogUtil.error(e.getMessage());
}
jMeterService.runDefinition(request.getId(), jmeterHashTree, request.getReportId(), runMode);
return request.getId();
}
@ -395,20 +380,10 @@ public class ApiAutomationService {
config.setConfig(envConfig);
HashTree hashTree = request.getTestElement().generateHashTree(config);
// 调用执行方法
String jmx = request.getTestElement().getJmx(hashTree);
createScenarioReport(request.getId(), request.getScenarioId(), request.getScenarioName(), ReportTriggerMode.MANUAL.name(), request.getExecuteType(), request.getProjectId(),
SessionUtils.getUserId());
byte[] bytes = JmeterDocumentParser.parse(jmx.getBytes());
InputStream is = new ByteArrayInputStream(bytes);
try {
Object scriptWrapper = SaveService.loadElement(is);
HashTree jmeterHashTree = JMeterService.getHashTree(scriptWrapper);
// 调用执行方法
jMeterService.runDefinition(request.getId(), jmeterHashTree, request.getReportId(), ApiRunMode.SCENARIO.name());
} catch (Exception e) {
e.printStackTrace();
LogUtil.error(e.getMessage());
}
// 调用执行方法
jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), ApiRunMode.SCENARIO.name());
return request.getId();
}

View File

@ -14,7 +14,6 @@ import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.jmeter.TestResult;
import io.metersphere.api.parse.ApiImportParser;
import io.metersphere.api.parse.ApiImportParserFactory;
import io.metersphere.api.parse.old.JmeterDocumentParser;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiDefinitionMapper;
import io.metersphere.base.mapper.ApiTestFileMapper;
@ -35,7 +34,6 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.jmeter.save.SaveService;
import org.apache.jorphan.collections.HashTree;
import org.aspectj.util.FileUtil;
import org.springframework.stereotype.Service;
@ -309,17 +307,7 @@ public class ApiDefinitionService {
runMode = ApiRunMode.API_PLAN.name();
}
// 调用执行方法
String jmx = request.getTestElement().getJmx(hashTree);
byte[] bytes = JmeterDocumentParser.parse(jmx.getBytes());
InputStream is = new ByteArrayInputStream(bytes);
try {
Object scriptWrapper = SaveService.loadElement(is);
HashTree testPlan = JMeterService.getHashTree(scriptWrapper);
// 调用执行方法
jMeterService.runDefinition(request.getId(), testPlan, request.getReportId(), runMode);
} catch (Exception e) {
e.printStackTrace();
}
jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), runMode);
return request.getId();
}