yx9o commented on a change in pull request #16162: URL: https://github.com/apache/shardingsphere/pull/16162#discussion_r829763000
########## File path: shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/ImportSchemaConfigurationHandlerTest.java ########## @@ -0,0 +1,153 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.proxy.backend.text.distsql.ral.common.updatable; + +import com.zaxxer.hikari.HikariDataSource; +import org.apache.shardingsphere.distsql.parser.statement.ral.common.updatable.ImportSchemaConfigurationStatement; +import org.apache.shardingsphere.infra.config.RuleConfiguration; +import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration; +import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; +import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema; +import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData; +import org.apache.shardingsphere.infra.metadata.schema.model.IndexMetaData; +import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.proxy.backend.context.ProxyContext; +import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader; +import org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader; +import org.apache.shardingsphere.proxy.backend.session.ConnectionSession; +import org.apache.shardingsphere.proxy.backend.text.distsql.ral.RALBackendHandler; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; +import org.junit.Before; +import org.junit.Test; + +import javax.sql.DataSource; +import java.sql.SQLException; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Properties; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public final class ImportSchemaConfigurationHandlerTest { + + private final String filePath = "/conf/import/config-sharding.yaml"; + + private final String schemaName = "sharding_db"; + + private final String scalingName = "default_scaling"; + + @Before + public void init() throws SQLException { + ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS); + when(contextManager.getMetaDataContexts().getAllSchemaNames()).thenReturn(Collections.singletonList(schemaName)); + ShardingSphereMetaData shardingSphereMetaData = mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS); + when(shardingSphereMetaData.getSchema()).thenReturn(new ShardingSphereSchema(createTableMap())); + when(shardingSphereMetaData.getResource().getDataSources()).thenReturn(createDataSourceMap()); + when(shardingSphereMetaData.getRuleMetaData().getConfigurations()).thenReturn(Collections.singletonList(createShardingRuleConfiguration())); + when(contextManager.getMetaDataContexts().getMetaData(schemaName)).thenReturn(shardingSphereMetaData); + ProxyContext.getInstance().init(contextManager); + } + + @Test + public void assertImportSchemaExecutor() throws SQLException { + Map<String, DataSource> dataSourceMap = ProxyContext.getInstance().getContextManager().getDataSourceMap(schemaName); + assertNotNull(dataSourceMap); + Collection<RuleConfiguration> ruleConfigurations = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData(schemaName).getRuleMetaData().getConfigurations(); + assertNotNull(ruleConfigurations); + ImportSchemaConfigurationHandler handler = new ImportSchemaConfigurationHandler().init(getParameter(createSqlStatement(), mockConnectionSession())); + ResponseHeader responseHeader = handler.execute(); + assertTrue(responseHeader instanceof UpdateResponseHeader); + } + + private ShardingRuleConfiguration createShardingRuleConfiguration() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + result.getTables().add(createTableRuleConfiguration()); + result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "ds_inline")); + result.setDefaultTableShardingStrategy(new NoneShardingStrategyConfiguration()); + result.getKeyGenerators().put("snowflake", new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", new Properties())); + Properties props = new Properties(); + props.setProperty("algorithm-expression", "ds_${order_id % 2}"); + result.getShardingAlgorithms().put("ds_inline", new ShardingSphereAlgorithmConfiguration("INLINE", props)); + result.setScalingName(scalingName); + result.getScaling().put(scalingName, null); + return result; + } + + private Map<String, DataSource> createDataSourceMap() { + Map<String, DataSource> result = new LinkedHashMap<>(2, 1); + result.put("ds_0", createDataSource("demo_ds_0")); + result.put("ds_1", createDataSource("demo_ds_1")); + return result; + } + + private DataSource createDataSource(final String dbName) { + HikariDataSource result = new HikariDataSource(); + result.setJdbcUrl(String.format("jdbc:mysql://127.0.0.1:3306/%s?serverTimezone=UTC&useSSL=false", dbName)); + result.setUsername("root"); + result.setPassword(""); + result.setConnectionTimeout(30000L); + result.setIdleTimeout(60000L); + result.setMaxLifetime(1800000L); + result.setMaximumPoolSize(50); + result.setMinimumIdle(1); + return result; + } Review comment: > @yx9o Using h2 database could solve the exception of ci, please refer https://github.com/apache/shardingsphere/pull/16147/files#diff-48ebddcde35067de8df4e9f553c535a82936f1da029378744ab1d6ced60a502c Thank you. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
