From 1c04e19880d00f60547d611becb5091cc5a0a415 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Fri, 19 Feb 2021 16:26:30 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96):=20=E5=AF=BC=E5=85=A5JMX=20=E9=80=9A=E7=94=A8?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=BA=90=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/dto/automation/ImportPoolsDTO.java | 3 ++ .../dto/automation/parse/MsJmeterParser.java | 44 ++++++++++++++----- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/ImportPoolsDTO.java b/backend/src/main/java/io/metersphere/api/dto/automation/ImportPoolsDTO.java index d5067a539b..992b433af3 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/ImportPoolsDTO.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/ImportPoolsDTO.java @@ -1,6 +1,7 @@ package io.metersphere.api.dto.automation; import io.metersphere.api.dto.scenario.DatabaseConfig; +import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs; import lombok.Getter; import lombok.Setter; @@ -14,6 +15,8 @@ public class ImportPoolsDTO { private Boolean isCreate; + private EnvironmentConfig envConfig; + private ApiTestEnvironmentWithBLOBs testEnvironmentWithBLOBs; private Map dataSources; diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/parse/MsJmeterParser.java b/backend/src/main/java/io/metersphere/api/dto/automation/parse/MsJmeterParser.java index 0e49a1e192..d8d4a7e573 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/parse/MsJmeterParser.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/parse/MsJmeterParser.java @@ -260,11 +260,13 @@ public class MsJmeterParser extends ScenarioImportAbstractParser { preCreatePool(hashTree); // 更新数据源 ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class); + dataPools.getEnvConfig().setDatabaseConfigs(new ArrayList<>(dataPools.getDataSources().values())); if (dataPools.getIsCreate()) { - dataPools.getTestEnvironmentWithBLOBs().setConfig(JSON.toJSONString(dataPools.getDataSources().values())); - environmentService.add(dataPools.getTestEnvironmentWithBLOBs()); + dataPools.getTestEnvironmentWithBLOBs().setConfig(JSON.toJSONString(dataPools.getEnvConfig())); + String id = environmentService.add(dataPools.getTestEnvironmentWithBLOBs()); + dataPools.setEnvId(id); } else { - dataPools.getTestEnvironmentWithBLOBs().setConfig(JSON.toJSONString(dataPools.getDataSources().values())); + dataPools.getTestEnvironmentWithBLOBs().setConfig(JSON.toJSONString(dataPools.getEnvConfig())); environmentService.update(dataPools.getTestEnvironmentWithBLOBs()); } } @@ -274,17 +276,32 @@ public class MsJmeterParser extends ScenarioImportAbstractParser { // JDBC 数据池 if (key instanceof DataSourceElement) { DataSourceElement dataSourceElement = (DataSourceElement) key; - if (dataPools != null && dataPools.getDataSources().containsKey(dataSourceElement.getDataSource())) { - DatabaseConfig config = dataPools.getDataSources().get(dataSourceElement.getDataSource()); + if (dataPools != null && dataPools.getDataSources() != null && dataPools.getDataSources().containsKey(dataSourceElement.getPropertyAsString("dataSource"))) { + DatabaseConfig config = dataPools.getDataSources().get(dataSourceElement.getPropertyAsString("dataSource")); DatabaseConfig newConfig = new DatabaseConfig(); - BeanUtils.copyBean(newConfig, dataSourceElement); + newConfig.setUsername(dataSourceElement.getPropertyAsString("username")); + newConfig.setPassword(dataSourceElement.getPropertyAsString("password")); + newConfig.setDriver(dataSourceElement.getPropertyAsString("driver")); + newConfig.setDbUrl(dataSourceElement.getPropertyAsString("dbUrl")); + newConfig.setName(dataSourceElement.getPropertyAsString("dataSource")); + newConfig.setPoolMax(dataSourceElement.getPropertyAsInt("poolMax")); + newConfig.setTimeout(dataSourceElement.getPropertyAsInt("timeout")); newConfig.setId(config.getId()); - dataPools.getDataSources().put(dataSourceElement.getDataSource(), newConfig); + dataPools.getDataSources().put(dataSourceElement.getPropertyAsString("dataSource"), newConfig); } else { DatabaseConfig newConfig = new DatabaseConfig(); - BeanUtils.copyBean(newConfig, dataSourceElement); newConfig.setId(UUID.randomUUID().toString()); - dataPools.getDataSources().put(dataSourceElement.getDataSource(), newConfig); + newConfig.setUsername(dataSourceElement.getPropertyAsString("username")); + newConfig.setPassword(dataSourceElement.getPropertyAsString("password")); + newConfig.setDriver(dataSourceElement.getPropertyAsString("driver")); + newConfig.setDbUrl(dataSourceElement.getPropertyAsString("dbUrl")); + newConfig.setName(dataSourceElement.getPropertyAsString("dataSource")); + newConfig.setPoolMax(dataSourceElement.getPropertyAsInt("poolMax")); + newConfig.setTimeout(dataSourceElement.getPropertyAsInt("timeout")); + if (dataPools.getDataSources() == null) { + dataPools.setDataSources(new HashMap<>()); + } + dataPools.getDataSources().put(dataSourceElement.getPropertyAsString("dataSource"), newConfig); } } @@ -302,7 +319,7 @@ public class MsJmeterParser extends ScenarioImportAbstractParser { ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class); ApiTestEnvironmentExample example = new ApiTestEnvironmentExample(); example.createCriteria().andProjectIdEqualTo(projectId).andNameEqualTo(name); - + // 这里的数据只有一条,如果多条则有问题 List environments = environmentService.selectByExampleWithBLOBs(example); dataPools = new ImportPoolsDTO(); if (CollectionUtils.isNotEmpty(environments)) { @@ -312,6 +329,7 @@ public class MsJmeterParser extends ScenarioImportAbstractParser { environments.forEach(environment -> { if (environment != null && environment.getConfig() != null) { EnvironmentConfig envConfig = JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class); + dataPools.setEnvConfig(envConfig); if (envConfig != null && CollectionUtils.isNotEmpty(envConfig.getDatabaseConfigs())) { envConfig.getDatabaseConfigs().forEach(item -> { dataSources.put(item.getName(), item); @@ -325,6 +343,8 @@ public class MsJmeterParser extends ScenarioImportAbstractParser { dataPools.setIsCreate(true); ApiTestEnvironmentWithBLOBs apiTestEnvironmentWithBLOBs = new ApiTestEnvironmentWithBLOBs(); apiTestEnvironmentWithBLOBs.setId(UUID.randomUUID().toString()); + dataPools.setEnvId(apiTestEnvironmentWithBLOBs.getId()); + dataPools.setEnvConfig(new EnvironmentConfig()); apiTestEnvironmentWithBLOBs.setName(ENV_NAME); apiTestEnvironmentWithBLOBs.setProjectId(projectId); dataPools.setTestEnvironmentWithBLOBs(apiTestEnvironmentWithBLOBs); @@ -339,6 +359,10 @@ public class MsJmeterParser extends ScenarioImportAbstractParser { msJDBCSampler.setQueryTimeout(jdbcSampler.getPropertyAsInt("queryTimeout")); msJDBCSampler.setResultVariable(jdbcSampler.getPropertyAsString("resultVariable")); msJDBCSampler.setVariableNames(jdbcSampler.getPropertyAsString("variableNames")); + msJDBCSampler.setEnvironmentId(dataPools.getEnvId()); + if (dataPools.getDataSources() != null && dataPools.getDataSources().get(jdbcSampler.getPropertyAsString("dataSource")) != null) { + msJDBCSampler.setDataSourceId(dataPools.getDataSources().get(jdbcSampler.getPropertyAsString("dataSource")).getId()); + } msJDBCSampler.setVariables(new LinkedList<>()); }