refactor(接口测试): 优化前后置sql提取生成脚本

This commit is contained in:
wxg0103 2024-04-15 17:39:41 +08:00 committed by Craftsman
parent b1ea71ecfa
commit a1b67bff16
3 changed files with 43 additions and 27 deletions

View File

@ -138,7 +138,7 @@
and api_report.create_user in
<include refid="io.metersphere.system.mapper.BaseMapper.filterInWrapper"/>
</when>
<when test="key=='updateUSer'">
<when test="key=='updateUser'">
and api_report.update_user in
<include refid="io.metersphere.system.mapper.BaseMapper.filterInWrapper"/>
</when>

View File

@ -264,7 +264,7 @@
and api_scenario_report.create_user in
<include refid="io.metersphere.system.mapper.BaseMapper.filterInWrapper"/>
</when>
<when test="key=='updateUSer'">
<when test="key=='updateUser'">
and api_scenario_report.update_user in
<include refid="io.metersphere.system.mapper.BaseMapper.filterInWrapper"/>
</when>

View File

@ -13,8 +13,10 @@ import io.metersphere.sdk.util.LogUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.extractor.JSR223PostProcessor;
import org.apache.jmeter.modifiers.JSR223PreProcessor;
import org.apache.jmeter.protocol.jdbc.config.DataSourceElement;
import org.apache.jmeter.protocol.jdbc.processor.AbstractJDBCProcessor;
import org.apache.jmeter.protocol.jdbc.processor.JDBCPreProcessor;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
@ -53,39 +55,53 @@ public abstract class SqlProcessorConverter extends MsProcessorConverter<SQLProc
T jdbcProcessor = jdbcProcessorClass.getDeclaredConstructor().newInstance();
getJdbcProcessor(sqlProcessor, jdbcProcessor, dataSource);
hashTree.add(jdbcProcessor);
List<KeyValueParam> extractParams = sqlProcessor.getExtractParams()
.stream()
.filter(KeyValueParam::isValid)
.toList();
// 添加提取的变量
TestElement jdbcPostProcessor;
if (jdbcProcessor instanceof JDBCPreProcessor) {
jdbcPostProcessor = getJdbcProcessor(sqlProcessor.getName(), extractParams, JSR223PreProcessor.class);
} else {
jdbcPostProcessor = getJdbcProcessor(sqlProcessor.getName(), extractParams, JSR223PostProcessor.class);
}
if (jdbcPostProcessor != null) {
hashTree.add(jdbcPostProcessor);
}
} catch (Exception e) {
LogUtils.error(e);
}
List<KeyValueParam> extractParams = sqlProcessor.getExtractParams()
.stream()
.filter(KeyValueParam::isValid)
.toList();
// 添加提取的变量
JSR223PostProcessor jdbcPostProcessor = getJdbcPostProcessor(sqlProcessor.getName(), extractParams);
if (jdbcPostProcessor != null ) {
hashTree.add(jdbcPostProcessor);
}
}
public JSR223PostProcessor getJdbcPostProcessor(String name, List<KeyValueParam> extractParams) {
public <T extends TestElement> T getJdbcProcessor(String name, List<KeyValueParam> extractParams, Class<T> elementType) {
if (CollectionUtils.isNotEmpty(extractParams)) {
JSR223PostProcessor jsr223PostProcessor = new JSR223PostProcessor();
jsr223PostProcessor.setName(name);
jsr223PostProcessor.setProperty(TestElement.TEST_CLASS, jsr223PostProcessor.getClass().getSimpleName());
jsr223PostProcessor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(JmeterAlias.TEST_BEAN_GUI));
jsr223PostProcessor.setProperty(JmeterProperty.SCRIPT_LANGUAGE, ScriptLanguageType.BEANSHELL.name().toLowerCase());
T processor;
try {
processor = elementType.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new IllegalArgumentException("Failed to create processor: " + elementType.getSimpleName(), e);
}
processor.setName(name);
processor.setProperty(TestElement.TEST_CLASS, elementType.getName());
processor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(JmeterAlias.TEST_BEAN_GUI));
processor.setProperty(JmeterProperty.SCRIPT_LANGUAGE, ScriptLanguageType.BEANSHELL.name().toLowerCase());
StringBuilder scriptBuilder = new StringBuilder();
extractParams.stream().filter(KeyValueParam::isValid)
.forEach(keyValue -> {
String script = """
vars.put("%s","${%s}");
""";
scriptBuilder.append(String.format(script, keyValue.getKey(), keyValue.getValue()));
});
jsr223PostProcessor.setProperty(JmeterProperty.SCRIPT, scriptBuilder.toString());
return jsr223PostProcessor;
for (KeyValueParam keyValue : extractParams) {
if (keyValue.isValid()) {
String script = "vars.put(\"%s\", \"%s\");\n";
scriptBuilder.append(String.format(script, keyValue.getKey(), keyValue.getValue()));
}
}
processor.setProperty(JmeterProperty.SCRIPT, scriptBuilder.toString());
return processor;
}
return null;
}