From 9dac2f80603fc09b9b9137a9f5eaadf7103f5f1d Mon Sep 17 00:00:00 2001 From: jiangML <1060319118@qq.com> Date: Tue, 24 Dec 2024 20:37:53 +0800 Subject: [PATCH] Fix test error --- .../util/ClusterExportMetaDataGenerator.java | 12 +- .../backend/util/MetaDataImportExecutor.java | 7 +- ...mlDatabaseConfigurationImportExecutor.java | 22 +-- ...portDatabaseConfigurationExecutorTest.java | 4 +- .../queryable/ExportMetaDataExecutorTest.java | 98 +++++------ ...portDatabaseConfigurationExecutorTest.java | 69 +++----- .../updatable/ImportMetaDataExecutorTest.java | 156 ++++++++++-------- 7 files changed, 175 insertions(+), 193 deletions(-) diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/ClusterExportMetaDataGenerator.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/ClusterExportMetaDataGenerator.java index 7746420f10650..f788c8967f633 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/ClusterExportMetaDataGenerator.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/ClusterExportMetaDataGenerator.java @@ -40,7 +40,6 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Properties; -import java.util.stream.Collectors; /** * Cluster export metadata generator. @@ -68,15 +67,12 @@ public String generateJsonFormat() { } private Map generatorDatabasesExportData() { - Collection databaseNames = contextManager.getMetaDataContexts().getMetaData().getAllDatabases().stream() - .map(ShardingSphereDatabase::getName).collect(Collectors.toList()); - Map result = new LinkedHashMap<>(databaseNames.size(), 1F); - for (String each : databaseNames) { - ShardingSphereDatabase database = contextManager.getDatabase(each); - if (database.getResourceMetaData().getAllInstanceDataSourceNames().isEmpty()) { + Map result = new LinkedHashMap<>(contextManager.getMetaDataContexts().getMetaData().getAllDatabases().size(), 1F); + for (ShardingSphereDatabase each : contextManager.getMetaDataContexts().getMetaData().getAllDatabases()) { + if (each.getResourceMetaData().getAllInstanceDataSourceNames().isEmpty()) { continue; } - result.put(each, new DatabaseExportMetaDataGenerator(database).generateYAMLFormat()); + result.put(each.getName(), new DatabaseExportMetaDataGenerator(each).generateYAMLFormat()); } return result; } diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/MetaDataImportExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/MetaDataImportExecutor.java index d65d9498348ce..a7e22f3d8298e 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/MetaDataImportExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/MetaDataImportExecutor.java @@ -38,10 +38,15 @@ public final class MetaDataImportExecutor { private final YamlRuleConfigurationSwapperEngine ruleConfigSwapperEngine = new YamlRuleConfigurationSwapperEngine(); - private final YamlDatabaseConfigurationImportExecutor databaseConfigImportExecutor = new YamlDatabaseConfigurationImportExecutor(); + private final YamlDatabaseConfigurationImportExecutor databaseConfigImportExecutor; private final ContextManager contextManager; + public MetaDataImportExecutor(final ContextManager contextManager) { + this.contextManager = contextManager; + this.databaseConfigImportExecutor = new YamlDatabaseConfigurationImportExecutor(contextManager); + } + /** * Import cluster configurations. * diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java index 33078e9e56ad7..54c141f8a640b 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.proxy.backend.util; +import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.distsql.handler.validate.DistSQLDataSourcePoolPropertiesValidator; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationCheckEngine; @@ -47,7 +48,6 @@ import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDataSourceConfiguration; import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDatabaseConfiguration; import org.apache.shardingsphere.proxy.backend.config.yaml.swapper.YamlProxyDataSourceConfigurationSwapper; -import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import java.sql.SQLException; import java.util.Collection; @@ -62,12 +62,15 @@ /** * Yaml database configuration import executor. */ +@RequiredArgsConstructor public final class YamlDatabaseConfigurationImportExecutor { private final YamlProxyDataSourceConfigurationSwapper dataSourceConfigSwapper = new YamlProxyDataSourceConfigurationSwapper(); private final DistSQLDataSourcePoolPropertiesValidator validateHandler = new DistSQLDataSourcePoolPropertiesValidator(); + private final ContextManager contextManager; + /** * Import proxy database from yaml configuration. * @@ -94,11 +97,11 @@ private void checkDataSources(final String databaseName, final Map new DatabaseCreateExistsException(databaseName)); + ShardingSpherePreconditions.checkNotEmpty(databaseName, MissingRequiredDatabaseException::new); + ShardingSpherePreconditions.checkState(!contextManager.getMetaDataContexts().getMetaData().containsDatabase(databaseName), () -> new DatabaseCreateExistsException(databaseName)); } private void addDatabase(final String databaseName) throws SQLException { - ContextManager contextManager = ProxyContext.getInstance().getContextManager(); contextManager.getPersistServiceFacade().getMetaDataManagerPersistService().createDatabase(databaseName); DatabaseType protocolType = DatabaseTypeEngine.getProtocolType(Collections.emptyMap(), contextManager.getMetaDataContexts().getMetaData().getProps()); contextManager.getMetaDataContexts().getMetaData().addDatabase(databaseName, protocolType, contextManager.getMetaDataContexts().getMetaData().getProps()); @@ -112,12 +115,11 @@ private void importDataSources(final String databaseName, final Map storageUnits = ProxyContext.getInstance().getContextManager() - .getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnits(); + Map storageUnits = contextManager.getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnits(); Map toBeAddedStorageNode = StorageUnitNodeMapCreator.create(propsMap); for (Entry entry : propsMap.entrySet()) { storageUnits.put(entry.getKey(), new StorageUnit(toBeAddedStorageNode.get(entry.getKey()), entry.getValue(), DataSourcePoolCreator.create(entry.getValue()))); @@ -129,10 +131,10 @@ private void importRules(final String databaseName, final Collection ruleConfigs = new LinkedList<>(); - MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts(); + MetaDataContexts metaDataContexts = contextManager.getMetaDataContexts(); ShardingSphereDatabase database = metaDataContexts.getMetaData().getDatabase(databaseName); swapToRuleConfigs(yamlRuleConfigs).values().forEach(each -> addRule(ruleConfigs, each, database)); - ProxyContext.getInstance().getContextManager().getPersistServiceFacade().getMetaDataPersistService().getDatabaseRulePersistService() + contextManager.getPersistServiceFacade().getMetaDataPersistService().getDatabaseRulePersistService() .persist(metaDataContexts.getMetaData().getDatabase(databaseName).getName(), ruleConfigs); } @@ -145,7 +147,7 @@ private void addRule(final Collection ruleConfigs, final Rule @SuppressWarnings({"unchecked", "rawtypes"}) private ShardingSphereRule buildRule(final RuleConfiguration ruleConfig, final ShardingSphereDatabase database) { DatabaseRuleBuilder ruleBuilder = OrderedSPILoader.getServices(DatabaseRuleBuilder.class, Collections.singleton(ruleConfig)).get(ruleConfig); - ComputeNodeInstanceContext computeNodeInstanceContext = ProxyContext.getInstance().getContextManager().getComputeNodeInstanceContext(); + ComputeNodeInstanceContext computeNodeInstanceContext = contextManager.getComputeNodeInstanceContext(); return ruleBuilder.build(ruleConfig, database.getName(), database.getProtocolType(), database.getResourceMetaData(), database.getRuleMetaData().getRules(), computeNodeInstanceContext); } @@ -161,6 +163,6 @@ private Map swapToRuleConfigs(final Collection storageUnits = createStorageUnits(); when(database.getResourceMetaData().getStorageUnits()).thenReturn(storageUnits); @@ -91,6 +90,7 @@ private Map createStorageUnits() { @Test void assertExecuteWithEmptyDatabase() { + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getName()).thenReturn("empty_db"); when(database.getResourceMetaData().getStorageUnits()).thenReturn(Collections.emptyMap()); when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.emptyList()); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java index 908fd91169ef7..6967b4e51f3c8 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java @@ -52,26 +52,18 @@ import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDataSourceConfiguration; import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDatabaseConfiguration; import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyServerConfiguration; -import org.apache.shardingsphere.proxy.backend.context.ProxyContext; import org.apache.shardingsphere.proxy.backend.distsql.export.ExportedClusterInfo; import org.apache.shardingsphere.proxy.backend.distsql.export.ExportedMetaData; import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; -import org.apache.shardingsphere.test.mock.AutoMockExtension; -import org.apache.shardingsphere.test.mock.StaticMockSettings; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; import javax.sql.DataSource; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.LinkedHashMap; -import java.util.LinkedList; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; @@ -87,36 +79,21 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -@ExtendWith(AutoMockExtension.class) -@StaticMockSettings(ProxyContext.class) class ExportMetaDataExecutorTest { - private static final String EXPECTED_EMPTY_METADATA_VALUE = "eyJtZXRhX2RhdGEiOnsiZGF0YWJhc2VzIjp7ImVtcHR5X21ldGFkYXRhIjoiZGF0YWJhc2VOYW1lOiBudWxsXG5kYXRhU291cmNlczpcbn" - + "J1bGVzOlxuIn0sInByb3BzIjoiIiwicnVsZXMiOiJydWxlczpcbi0gIUdMT0JBTF9DTE9DS1xuICBlbmFibGVkOiBmYWxzZVxuICBwcm92aWRlcjogbG9jYWxcbiAgdHlwZTogVFNPXG4ifX0="; + private static final String EXPECTED_EMPTY_METADATA_VALUE = "eyJtZXRhX2RhdGEiOnsiZGF0YWJhc2VzIjp7ImVtcHR5X21ldGFkYXRhIjoiZGF0YWJhc2VOYW1lOiBlbXB0eV9tZXRhZGF0YVxuI" + + "n0sInByb3BzIjoiIiwicnVsZXMiOiJydWxlczpcbi0gIUdMT0JBTF9DTE9DS1xuICBlbmFibGVkOiBmYWxzZVxuICBwcm92aWRlcjogbG9jYWxcbiAgdHlwZTogVFNPXG4ifX0="; - private static final String EXPECTED_NOT_EMPTY_METADATA_VALUE = "eyJtZXRhX2RhdGEiOnsiZGF0YWJhc2VzIjp7Im5vcm1hbF9kYiI6ImRhdGFiYXNlTmFtZTogbm9ybWFsX2RiXG5kYXRhU291cm" - + "NlczpcbiAgZHNfMDpcbiAgICBwYXNzd29yZDogXG4gICAgdXJsOiBqZGJjOm9wZW5nYXVzczovLzEyNy4wLjAuMTo1NDMyL2RlbW9fZHNfMFxuICAgIHVzZXJuYW1lOiByb290XG4gICAgbWluUG9vb" - + "FNpemU6IDFcbiAgICBtYXhQb29sU2l6ZTogNTBcbiAgZHNfMTpcbiAgICBwYXNzd29yZDogXG4gICAgdXJsOiBqZGJjOm9wZW5nYXVzczovLzEyNy4wLjAuMTo1NDMyL2RlbW9fZHNfMVxuICAgIHVzZ" - + "XJuYW1lOiByb290XG4gICAgbWluUG9vbFNpemU6IDFcbiAgICBtYXhQb29sU2l6ZTogNTBcbiJ9LCJwcm9wcyI6InByb3BzOlxuICBzcWwtc2hvdzogdHJ1ZVxuIiwicnVsZXMiOiJydWxlczpcbi0g" - + "IUFVVEhPUklUWVxuICBwcml2aWxlZ2U6XG4gICAgdHlwZTogQUxMX1BFUk1JVFRFRFxuICB1c2VyczpcbiAgLSBhdXRoZW50aWNhdGlvbk1ldGhvZE5hbWU6ICcnXG4gICAgcGFzc3dvcmQ6IHJvb3Rc" - + "biAgICB1c2VyOiByb290QCVcbi0gIUdMT0JBTF9DTE9DS1xuICBlbmFibGVkOiBmYWxzZVxuICBwcm92aWRlcjogbG9jYWxcbiAgdHlwZTogVFNPXG4ifX0="; - - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; - - @BeforeEach - void setUp() { - when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); - } + private static final String EXPECTED_NOT_EMPTY_METADATA_VALUE = "eyJtZXRhX2RhdGEiOnsiZGF0YWJhc2VzIjp7Im5vcm1hbF9kYiI6ImRhdGFiYXNlTmFtZTogbm9ybWFsX2RiXG5kYXRhU291cmNlczpcbiAgZHNfMDpcbiA" + + "gICBwYXNzd29yZDogXG4gICAgdXJsOiBqZGJjOmgyOm1lbTpkZW1vX2RzXzA7REJfQ0xPU0VfREVMQVk9LTE7REFUQUJBU0VfVE9fVVBQRVI9ZmFsc2U7TU9ERT1NeVNRTFxuICAgIHVzZXJuYW1lOiByb290XG4gICAgbWluUG9" + + "vbFNpemU6IDFcbiAgICBtYXhQb29sU2l6ZTogNTBcbiAgZHNfMTpcbiAgICBwYXNzd29yZDogXG4gICAgdXJsOiBqZGJjOmgyOm1lbTpkZW1vX2RzXzE7REJfQ0xPU0VfREVMQVk9LTE7REFUQUJBU0VfVE9fVVBQRVI9ZmFsc2" + + "U7TU9ERT1NeVNRTFxuICAgIHVzZXJuYW1lOiByb290XG4gICAgbWluUG9vbFNpemU6IDFcbiAgICBtYXhQb29sU2l6ZTogNTBcbiJ9LCJwcm9wcyI6InByb3BzOlxuICBzcWwtc2hvdzogdHJ1ZVxuIiwicnVsZXMiOiJydWxlczpcbi0g" + + "IUFVVEhPUklUWVxuICBwcml2aWxlZ2U6XG4gICAgdHlwZTogQUxMX1BFUk1JVFRFRFxuICB1c2VyczpcbiAgLSBhZG1pbjogdHJ1ZVxuICAgIGF1dGhlbnRpY2F0aW9uTWV0aG9kTmFtZTogJydcbiAgIC" + + "BwYXNzd29yZDogcm9vdFxuICAgIHVzZXI6IHJvb3RAJVxuLSAhR0xPQkFMX0NMT0NLXG4gIGVuYWJsZWQ6IGZhbHNlXG4gIHByb3ZpZGVyOiBsb2NhbFxuICB0eXBlOiBUU09cbiJ9fQ=="; @Test void assertExecuteWithEmptyMetaData() { ContextManager contextManager = mockEmptyContextManager(); - when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); - when(ProxyContext.getInstance().getAllDatabaseNames()).thenReturn(Collections.singleton("empty_metadata")); - when(database.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(Collections.singleton("empty_metadata")); - when(database.getResourceMetaData().getStorageUnits()).thenReturn(Collections.emptyMap()); - when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.emptyList()); ExportMetaDataStatement sqlStatement = new ExportMetaDataStatement(null); Collection actual = new ExportMetaDataExecutor().getRows(sqlStatement, contextManager); assertThat(actual.size(), is(1)); @@ -125,43 +102,36 @@ void assertExecuteWithEmptyMetaData() { } private ContextManager mockEmptyContextManager() { + ShardingSphereDatabase database = mockEmptyShardingSphereDatabase(); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(new LinkedList<>(), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singleton(database), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.singleton(new GlobalClockRule(new DefaultGlobalClockRuleConfigurationBuilder().build()))), new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; } + private ShardingSphereDatabase mockEmptyShardingSphereDatabase() { + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(result.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); + when(result.getName()).thenReturn("empty_metadata"); + when(result.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(Collections.singleton("empty_metadata")); + when(result.getResourceMetaData().getStorageUnits()).thenReturn(Collections.emptyMap()); + when(result.getRuleMetaData().getConfigurations()).thenReturn(Collections.emptyList()); + return result; + } + @Test void assertExecute() { - when(database.getName()).thenReturn("normal_db"); - when(database.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(Collections.singleton("empty_metadata")); - Map storageUnits = createStorageUnits(); - when(database.getResourceMetaData().getStorageUnits()).thenReturn(storageUnits); - when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.emptyList()); ContextManager contextManager = mockContextManager(); - when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); - when(ProxyContext.getInstance().getAllDatabaseNames()).thenReturn(Collections.singleton("normal_db")); Collection actual = new ExportMetaDataExecutor().getRows(new ExportMetaDataStatement(null), contextManager); assertThat(actual.size(), is(1)); LocalDataQueryResultRow row = actual.iterator().next(); assertMetaData(row.getCell(3), EXPECTED_NOT_EMPTY_METADATA_VALUE); } - private Map createStorageUnits() { - Map propsMap = createDataSourceMap().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); - Map result = new LinkedHashMap<>(propsMap.size(), 1F); - for (Entry entry : propsMap.entrySet()) { - StorageUnit storageUnit = mock(StorageUnit.class, RETURNS_DEEP_STUBS); - when(storageUnit.getDataSourcePoolProperties()).thenReturn(entry.getValue()); - result.put(entry.getKey(), storageUnit); - } - return result; - } - private ContextManager mockContextManager() { + ShardingSphereDatabase database = mockShardingSphereDatabase(); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singleton(database), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Arrays.asList(new AuthorityRule(new DefaultAuthorityRuleConfigurationBuilder().build()), @@ -173,7 +143,29 @@ private ContextManager mockContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); when(result.getComputeNodeInstanceContext()).thenReturn(computeNodeInstanceContext); - when(result.getDatabase("normal_db")).thenReturn(database); + return result; + } + + private ShardingSphereDatabase mockShardingSphereDatabase() { + Map storageUnits = createStorageUnits(); + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(result.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); + when(result.getName()).thenReturn("normal_db"); + when(result.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(storageUnits.keySet()); + when(result.getResourceMetaData().getStorageUnits()).thenReturn(storageUnits); + when(result.getRuleMetaData().getConfigurations()).thenReturn(Collections.emptyList()); + return result; + } + + private Map createStorageUnits() { + Map propsMap = createDataSourceMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); + Map result = new LinkedHashMap<>(propsMap.size(), 1F); + for (Entry entry : propsMap.entrySet()) { + StorageUnit storageUnit = mock(StorageUnit.class, RETURNS_DEEP_STUBS); + when(storageUnit.getDataSourcePoolProperties()).thenReturn(entry.getValue()); + result.put(entry.getKey(), storageUnit); + } return result; } @@ -186,7 +178,7 @@ private Map createDataSourceMap() { private DataSource createDataSource(final String name) { MockedDataSource result = new MockedDataSource(); - result.setUrl(String.format("jdbc:opengauss://127.0.0.1:5432/%s", name)); + result.setUrl(String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL", name)); result.setUsername("root"); result.setPassword(""); result.setMaxPoolSize(50); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationExecutorTest.java index 7d49a7ac63914..204ddfbec561c 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportDatabaseConfigurationExecutorTest.java @@ -17,8 +17,6 @@ package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable; -import lombok.SneakyThrows; -import org.apache.shardingsphere.distsql.handler.validate.DistSQLDataSourcePoolPropertiesValidator; import org.apache.shardingsphere.distsql.statement.ral.updatable.ImportDatabaseConfigurationStatement; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; @@ -33,18 +31,8 @@ import org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.proxy.backend.context.ProxyContext; -import org.apache.shardingsphere.proxy.backend.util.YamlDatabaseConfigurationImportExecutor; import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; -import org.apache.shardingsphere.test.fixture.jdbc.MockedDriver; -import org.apache.shardingsphere.test.mock.AutoMockExtension; -import org.apache.shardingsphere.test.mock.StaticMockSettings; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.internal.configuration.plugins.Plugins; -import org.mockito.junit.jupiter.MockitoSettings; -import org.mockito.quality.Strictness; import javax.sql.DataSource; import java.net.URL; @@ -60,81 +48,68 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -@ExtendWith(AutoMockExtension.class) -@StaticMockSettings(ProxyContext.class) -@MockitoSettings(strictness = Strictness.LENIENT) class ImportDatabaseConfigurationExecutorTest { - private ImportDatabaseConfigurationExecutor executor; - - @BeforeAll - static void setUp() throws ClassNotFoundException { - Class.forName(MockedDriver.class.getName()); - } - @Test void assertImportDatabaseExecutorForSharding() throws SQLException { - assertExecute("sharding_db", "/conf/import/database-sharding.yaml"); + ContextManager contextManager = mockContextManager("sharding_db"); + assertExecute(contextManager, "/conf/import/database-sharding.yaml"); } @Test void assertImportDatabaseExecutorForReadwriteSplitting() throws SQLException { - assertExecute("readwrite_splitting_db", "/conf/import/database-readwrite-splitting.yaml"); + ContextManager contextManager = mockContextManager("readwrite_splitting_db"); + assertExecute(contextManager, "/conf/import/database-readwrite-splitting.yaml"); } @Test void assertImportDatabaseExecutorForEncrypt() throws SQLException { - assertExecute("encrypt_db", "/conf/import/database-encrypt.yaml"); + ContextManager contextManager = mockContextManager("encrypt_db"); + assertExecute(contextManager, "/conf/import/database-encrypt.yaml"); } @Test void assertImportDatabaseExecutorForShadow() throws SQLException { - assertExecute("shadow_db", "/conf/import/database-shadow.yaml"); + ContextManager contextManager = mockContextManager("shadow_db"); + assertExecute(contextManager, "/conf/import/database-shadow.yaml"); } @Test void assertImportDatabaseExecutorForMask() throws SQLException { - assertExecute("mask_db", "/conf/import/database-mask.yaml"); + ContextManager contextManager = mockContextManager("mask_db"); + assertExecute(contextManager, "/conf/import/database-mask.yaml"); } @Test void assertImportExistedDatabase() { - String databaseName = "sharding_db"; - when(ProxyContext.getInstance().databaseExists(databaseName)).thenReturn(true); - assertThrows(DatabaseCreateExistsException.class, () -> assertExecute(databaseName, "/conf/import/database-sharding.yaml")); + ContextManager contextManager = mockContextManager("sharding_db"); + when(contextManager.getMetaDataContexts().getMetaData().containsDatabase("sharding_db")).thenReturn(true); + assertThrows(DatabaseCreateExistsException.class, () -> assertExecute(contextManager, "/conf/import/database-sharding.yaml")); } @Test void assertImportEmptyDatabaseName() { - assertThrows(MissingRequiredDatabaseException.class, () -> assertExecute("sharding_db", "/conf/import/database-empty-database-name.yaml")); + ContextManager contextManager = mockContextManager("sharding_db"); + assertThrows(MissingRequiredDatabaseException.class, () -> assertExecute(contextManager, "/conf/import/database-empty-database-name.yaml")); } @Test void assertImportDuplicatedLogicTable() { - assertThrows(DuplicateRuleException.class, () -> assertExecute("sharding_db", "/conf/import/database-duplicated-logic-table.yaml")); + ContextManager contextManager = mockContextManager("sharding_db"); + assertThrows(DuplicateRuleException.class, () -> assertExecute(contextManager, "/conf/import/database-duplicated-logic-table.yaml")); } @Test void assertImportInvalidAlgorithm() { - assertThrows(ServiceProviderNotFoundException.class, () -> assertExecute("sharding_db", "/conf/import/database-invalid-algorithm.yaml")); + ContextManager contextManager = mockContextManager("sharding_db"); + assertThrows(ServiceProviderNotFoundException.class, () -> assertExecute(contextManager, "/conf/import/database-invalid-algorithm.yaml")); } - private void assertExecute(final String databaseName, final String filePath) throws SQLException { - init(databaseName); + private void assertExecute(final ContextManager contextManager, final String filePath) throws SQLException { + ImportDatabaseConfigurationExecutor executor = new ImportDatabaseConfigurationExecutor(); URL url = ImportDatabaseConfigurationExecutorTest.class.getResource(filePath); assertNotNull(url); - executor.executeUpdate(new ImportDatabaseConfigurationStatement(url.getPath()), mock(ContextManager.class)); - } - - @SneakyThrows({IllegalAccessException.class, NoSuchFieldException.class}) - private void init(final String databaseName) { - ContextManager contextManager = mockContextManager(databaseName); - when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); - executor = new ImportDatabaseConfigurationExecutor(); - YamlDatabaseConfigurationImportExecutor databaseConfigImportExecutor = new YamlDatabaseConfigurationImportExecutor(); - Plugins.getMemberAccessor().set(ImportDatabaseConfigurationExecutor.class.getDeclaredField("databaseConfigImportExecutor"), executor, databaseConfigImportExecutor); - Plugins.getMemberAccessor().set( - YamlDatabaseConfigurationImportExecutor.class.getDeclaredField("validateHandler"), databaseConfigImportExecutor, mock(DistSQLDataSourcePoolPropertiesValidator.class)); + executor.executeUpdate(new ImportDatabaseConfigurationStatement(url.getPath()), contextManager); } private ContextManager mockContextManager(final String databaseName) { diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataExecutorTest.java index 89c353b4e5d96..f0a7cb00bb60d 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataExecutorTest.java @@ -17,44 +17,51 @@ package org.apache.shardingsphere.proxy.backend.handler.distsql.ral.updatable; -import lombok.SneakyThrows; -import org.apache.groovy.util.Maps; -import org.apache.shardingsphere.distsql.handler.validate.DistSQLDataSourcePoolPropertiesValidator; +import org.apache.shardingsphere.authority.rule.AuthorityRule; +import org.apache.shardingsphere.authority.rule.builder.DefaultAuthorityRuleConfigurationBuilder; import org.apache.shardingsphere.distsql.statement.ral.updatable.ImportMetaDataStatement; +import org.apache.shardingsphere.globalclock.rule.GlobalClockRule; +import org.apache.shardingsphere.globalclock.rule.builder.DefaultGlobalClockRuleConfigurationBuilder; +import org.apache.shardingsphere.infra.config.mode.ModeConfiguration; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.DatabaseCreateExistsException; import org.apache.shardingsphere.infra.exception.kernel.metadata.resource.storageunit.EmptyStorageUnitException; +import org.apache.shardingsphere.infra.instance.ComputeNodeInstance; +import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext; +import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData; +import org.apache.shardingsphere.infra.lock.LockContext; +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; +import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereIndex; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; -import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable; +import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; +import org.apache.shardingsphere.infra.util.eventbus.EventBusContext; +import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.proxy.backend.context.ProxyContext; -import org.apache.shardingsphere.proxy.backend.util.YamlDatabaseConfigurationImportExecutor; +import org.apache.shardingsphere.mode.manager.standalone.workerid.StandaloneWorkerIdGenerator; +import org.apache.shardingsphere.mode.metadata.MetaDataContexts; +import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; -import org.apache.shardingsphere.test.mock.AutoMockExtension; -import org.apache.shardingsphere.test.mock.StaticMockSettings; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.internal.configuration.plugins.Plugins; -import org.mockito.junit.jupiter.MockitoSettings; -import org.mockito.quality.Strictness; +import javax.sql.DataSource; import java.sql.SQLException; -import java.util.Collection; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Map.Entry; import java.util.Objects; +import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -62,96 +69,101 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -@ExtendWith(AutoMockExtension.class) -@StaticMockSettings(ProxyContext.class) -@MockitoSettings(strictness = Strictness.LENIENT) class ImportMetaDataExecutorTest { - private static final String METADATA_VALUE = "eyJtZXRhX2RhdGEiOnsiZGF0YWJhc2VzIjp7InNoYXJkaW5nX2RiIjoiZGF0YWJhc2VOYW1lOiBzaGFyZGluZ19kYlxuZGF0YVNvdXJjZXM6XG4gIGRzXzA6XG4gICAgcGFzc3dvcmQ6IFxu" - + "ICAgIGRhdGFTb3VyY2VDbGFzc05hbWU6IG51bGxcbiAgICB1cmw6IGpkYmM6bXlzcWw6Ly8xMjcuMC4wLjE6MzMwNi9kZW1vX2RzXzA/dXNlU1NMPWZhbHNlXG4gICAgdXNlcm5hbWU6IHJvb3RcbiAgICBtaW5Qb29sU2l6ZTogMVxuICAgI" - + "GNvbm5lY3Rpb25UaW1lb3V0TWlsbGlzZWNvbmRzOiAzMDAwMFxuICAgIG1heExpZmV0aW1lTWlsbGlzZWNvbmRzOiAxODAwMDAwXG4gICAgaWRsZVRpbWVvdXRNaWxsaXNlY29uZHM6IDYwMDAwXG4gICAgbWF4UG9vbFNpemU6IDUwXG4gIG" - + "RzXzE6XG4gICAgcGFzc3dvcmQ6IFxuICAgIGRhdGFTb3VyY2VDbGFzc05hbWU6IG51bGxcbiAgICB1cmw6IGpkYmM6bXlzcWw6Ly8xMjcuMC4wLjE6MzMwNi9kZW1vX2RzXzE/dXNlU1NMPWZhbHNlXG4gICAgdXNlcm5hbWU6IHJvb3RcbiA" - + "gICBtaW5Qb29sU2l6ZTogMVxuICAgIGNvbm5lY3Rpb25UaW1lb3V0TWlsbGlzZWNvbmRzOiAzMDAwMFxuICAgIG1heExpZmV0aW1lTWlsbGlzZWNvbmRzOiAxODAwMDAwXG4gICAgaWRsZVRpbWVvdXRNaWxsaXNlY29uZHM6IDYwMDAwXG4g" - + "ICAgbWF4UG9vbFNpemU6IDUwXG5ydWxlczpcbiJ9LCJwcm9wcyI6InByb3BzOlxuICBzeXN0ZW0tbG9nLWxldmVsOiBJTkZPXG4gIHNxbC1zaG93OiBmYWxzZVxuIiwicnVsZXMiOiJydWxlczpcbi0gIUFVVEhPUklUWVxuICBwcml2aWxlZ" - + "2U6XG4gICAgdHlwZTogQUxMX1BFUk1JVFRFRFxuICB1c2VyczpcbiAgLSBhdXRoZW50aWNhdGlvbk1ldGhvZE5hbWU6ICcnXG4gICAgcGFzc3dvcmQ6IHJvb3RcbiAgICB1c2VyOiByb290QCVcbiJ9fQ=="; + private static final String METADATA_VALUE = "eyJtZXRhX2RhdGEiOnsiZGF0YWJhc2VzIjp7Im5vcm1hbF9kYiI6ImRhdGFiYXNlTmFtZTogbm9ybWFsX2RiXG5kYXRhU291cmNlczpcbiAgZHNfMDpcbiA" + + "gICBwYXNzd29yZDogXG4gICAgdXJsOiBqZGJjOmgyOm1lbTpkZW1vX2RzXzA7REJfQ0xPU0VfREVMQVk9LTE7REFUQUJBU0VfVE9fVVBQRVI9ZmFsc2U7TU9ERT1NeVNRTFxuICAgIHVzZXJuYW1lOiByb290XG4gICAgbWluUG9" + + "vbFNpemU6IDFcbiAgICBtYXhQb29sU2l6ZTogNTBcbiAgZHNfMTpcbiAgICBwYXNzd29yZDogXG4gICAgdXJsOiBqZGJjOmgyOm1lbTpkZW1vX2RzXzE7REJfQ0xPU0VfREVMQVk9LTE7REFUQUJBU0VfVE9fVVBQRVI9ZmFsc2" + + "U7TU9ERT1NeVNRTFxuICAgIHVzZXJuYW1lOiByb290XG4gICAgbWluUG9vbFNpemU6IDFcbiAgICBtYXhQb29sU2l6ZTogNTBcbiJ9LCJwcm9wcyI6InByb3BzOlxuICBzcWwtc2hvdzogdHJ1ZVxuIiwicnVsZXMiOiJydWxlczpcbi0g" + + "IUFVVEhPUklUWVxuICBwcml2aWxlZ2U6XG4gICAgdHlwZTogQUxMX1BFUk1JVFRFRFxuICB1c2VyczpcbiAgLSBhZG1pbjogdHJ1ZVxuICAgIGF1dGhlbnRpY2F0aW9uTWV0aG9kTmFtZTogJydcbiAgIC" + + "BwYXNzd29yZDogcm9vdFxuICAgIHVzZXI6IHJvb3RAJVxuLSAhR0xPQkFMX0NMT0NLXG4gIGVuYWJsZWQ6IGZhbHNlXG4gIHByb3ZpZGVyOiBsb2NhbFxuICB0eXBlOiBUU09cbiJ9fQ=="; - private static final String EMPTY = "empty_metadata"; - - private ImportMetaDataExecutor executor; + private static final String EMPTY_DATABASE_NAME = "empty_metadata"; private final Map featureMap = new HashMap<>(1, 1F); @BeforeEach void setup() { - featureMap.put(EMPTY, "/conf/import/empty-metadata.json"); + featureMap.put(EMPTY_DATABASE_NAME, "/conf/import/empty-metadata.json"); } @Test void assertImportEmptyMetaData() { - init(null); + ImportMetaDataExecutor executor = new ImportMetaDataExecutor(); ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS); assertThrows(EmptyStorageUnitException.class, () -> executor.executeUpdate( - new ImportMetaDataStatement(null, Objects.requireNonNull(ImportMetaDataExecutorTest.class.getResource(featureMap.get(EMPTY))).getPath()), contextManager)); + new ImportMetaDataStatement(null, Objects.requireNonNull(ImportMetaDataExecutorTest.class.getResource(featureMap.get(EMPTY_DATABASE_NAME))).getPath()), contextManager)); } @Test void assertImportMetaDataFromJsonValue() throws SQLException { - init(EMPTY); ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS); + ImportMetaDataExecutor executor = new ImportMetaDataExecutor(); executor.executeUpdate(new ImportMetaDataStatement(METADATA_VALUE, null), contextManager); - assertNotNull(contextManager.getDatabase("sharding_db")); + assertNotNull(contextManager.getDatabase("normal_db")); } @Test void assertImportExistedMetaDataFromFile() { - init(EMPTY); - ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS); + ImportMetaDataExecutor executor = new ImportMetaDataExecutor(); + ContextManager contextManager = mockContextManager(); assertThrows(DatabaseCreateExistsException.class, () -> executor.executeUpdate( - new ImportMetaDataStatement(null, Objects.requireNonNull(ImportMetaDataExecutorTest.class.getResource(featureMap.get(EMPTY))).getPath()), contextManager)); + new ImportMetaDataStatement(null, Objects.requireNonNull(ImportMetaDataExecutorTest.class.getResource(featureMap.get(EMPTY_DATABASE_NAME))).getPath()), contextManager)); } - @SneakyThrows({IllegalAccessException.class, NoSuchFieldException.class}) - private void init(final String feature) { - executor = new ImportMetaDataExecutor(); - ContextManager contextManager = mockContextManager(feature); - when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); - when(ProxyContext.getInstance().databaseExists(feature)).thenReturn(true); - YamlDatabaseConfigurationImportExecutor databaseConfigImportExecutor = new YamlDatabaseConfigurationImportExecutor(); - Plugins.getMemberAccessor().set(ImportMetaDataExecutor.class.getDeclaredField("databaseConfigImportExecutor"), executor, databaseConfigImportExecutor); - Plugins.getMemberAccessor().set( - YamlDatabaseConfigurationImportExecutor.class.getDeclaredField("validateHandler"), databaseConfigImportExecutor, mock(DistSQLDataSourcePoolPropertiesValidator.class)); + private ContextManager mockContextManager() { + ShardingSphereDatabase database = mockShardingSphereDatabase(); + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singleton(database), + new ResourceMetaData(Collections.emptyMap()), + new RuleMetaData(Arrays.asList(new AuthorityRule(new DefaultAuthorityRuleConfigurationBuilder().build()), + new GlobalClockRule(new DefaultGlobalClockRuleConfigurationBuilder().build()))), + new ConfigurationProperties(PropertiesBuilder.build(new Property(ConfigurationPropertyKey.SQL_SHOW.getKey(), "true"))))); + ComputeNodeInstanceContext computeNodeInstanceContext = new ComputeNodeInstanceContext( + new ComputeNodeInstance(mock(InstanceMetaData.class)), new ModeConfiguration("Standalone", null), new EventBusContext()); + computeNodeInstanceContext.init(new StandaloneWorkerIdGenerator(), mock(LockContext.class)); + ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); + when(result.getMetaDataContexts()).thenReturn(metaDataContexts); + when(result.getComputeNodeInstanceContext()).thenReturn(computeNodeInstanceContext); + return result; } - private ContextManager mockContextManager(final String feature) { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - when(result.getMetaDataContexts().getMetaData().getProps()) - .thenReturn(new ConfigurationProperties(PropertiesBuilder.build(new Property(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE.getKey(), "MySQL")))); - if (null != feature) { - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(database.getName()).thenReturn(feature); - when(database.getSchema("foo_db")).thenReturn(new ShardingSphereSchema("foo_db", createTables(), Collections.emptyList())); - Map storageUnits = createStorageUnits(); - when(database.getResourceMetaData().getStorageUnits()).thenReturn(storageUnits); - when(result.getMetaDataContexts().getMetaData().getAllDatabases()).thenReturn(Collections.singleton(database)); - when(result.getMetaDataContexts().getMetaData().getDatabase(feature)).thenReturn(database); - } + private ShardingSphereDatabase mockShardingSphereDatabase() { + Map storageUnits = createStorageUnits(); + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(result.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); + when(result.getName()).thenReturn(EMPTY_DATABASE_NAME); + when(result.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(storageUnits.keySet()); + when(result.getResourceMetaData().getStorageUnits()).thenReturn(storageUnits); + when(result.getRuleMetaData().getConfigurations()).thenReturn(Collections.emptyList()); return result; } private Map createStorageUnits() { - Map result = new LinkedHashMap<>(2, 1F); - DataSourcePoolProperties dataSourcePoolProps0 = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); - when(dataSourcePoolProps0.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Maps.of("url", "jdbc:mock://127.0.0.1/ds_0", "username", "test")); - result.put("ds_0", new StorageUnit(mock(StorageNode.class), dataSourcePoolProps0, new MockedDataSource())); - DataSourcePoolProperties dataSourcePoolProps1 = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); - when(dataSourcePoolProps1.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Maps.of("url", "jdbc:mock://127.0.0.1/ds_1", "username", "test")); - result.put("ds_1", new StorageUnit(mock(StorageNode.class), dataSourcePoolProps1, new MockedDataSource())); + Map propsMap = createDataSourceMap().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); + Map result = new LinkedHashMap<>(propsMap.size(), 1F); + for (Entry entry : propsMap.entrySet()) { + StorageUnit storageUnit = mock(StorageUnit.class, RETURNS_DEEP_STUBS); + when(storageUnit.getDataSourcePoolProperties()).thenReturn(entry.getValue()); + result.put(entry.getKey(), storageUnit); + } return result; } - private Collection createTables() { - Collection columns = Collections.singleton(new ShardingSphereColumn("order_id", 0, false, false, false, true, false, false)); - Collection indexes = Collections.singleton(new ShardingSphereIndex("primary", Collections.emptyList(), false)); - return Collections.singletonList(new ShardingSphereTable("t_order", columns, indexes, Collections.emptyList())); + private Map createDataSourceMap() { + Map result = new LinkedHashMap<>(2, 1F); + result.put("ds_0", createDataSource("demo_ds_0")); + result.put("ds_1", createDataSource("demo_ds_1")); + return result; + } + + private DataSource createDataSource(final String name) { + MockedDataSource result = new MockedDataSource(); + result.setUrl(String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL", name)); + result.setUsername("root"); + result.setPassword(""); + result.setMaxPoolSize(50); + result.setMinPoolSize(1); + return result; } }