diff --git a/features/broadcast/distsql/handler/pom.xml b/features/broadcast/distsql/handler/pom.xml index c36c23556928f..bd2ffd526035f 100644 --- a/features/broadcast/distsql/handler/pom.xml +++ b/features/broadcast/distsql/handler/pom.xml @@ -50,7 +50,7 @@ org.apache.shardingsphere - shardingsphere-test-util + shardingsphere-test-it-distsql ${project.version} test diff --git a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/query/ShowBroadcastTableRuleExecutorTest.java b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/query/ShowBroadcastTableRuleExecutorTest.java index c7d99639a6d08..f5504032bd7b3 100644 --- a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/query/ShowBroadcastTableRuleExecutorTest.java +++ b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/query/ShowBroadcastTableRuleExecutorTest.java @@ -20,49 +20,42 @@ import org.apache.shardingsphere.broadcast.config.BroadcastRuleConfiguration; import org.apache.shardingsphere.broadcast.distsql.statement.ShowBroadcastTableRulesStatement; import org.apache.shardingsphere.broadcast.rule.BroadcastRule; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.junit.jupiter.api.Test; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; -import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; -import java.util.List; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowBroadcastTableRuleExecutorTest { +class ShowBroadcastTableRuleExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - @Test - void assertGetRowData() throws SQLException { - DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(mock(ShowBroadcastTableRulesStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - engine.executeQuery(); - List actual = new ArrayList<>(engine.getRows()); - assertThat(actual.get(0).getCell(1), is("t_address")); + ShowBroadcastTableRuleExecutorTest() { + super(mock(BroadcastRule.class)); } - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - BroadcastRule rule = mockBroadcastRule(); - when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); - return result; + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private BroadcastRule mockBroadcastRule() { - BroadcastRule result = mock(BroadcastRule.class); - BroadcastRuleConfiguration config = mock(BroadcastRuleConfiguration.class); - when(config.getTables()).thenReturn(Collections.singleton("t_address")); - when(result.getConfiguration()).thenReturn(config); - return result; + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", new BroadcastRuleConfiguration(Collections.singleton("t_address")), new ShowBroadcastTableRulesStatement(null), + Collections.singleton(new LocalDataQueryResultRow("t_address")))); + } } } diff --git a/features/encrypt/distsql/handler/pom.xml b/features/encrypt/distsql/handler/pom.xml index e98129ab9045d..9c0ed7f39d26b 100644 --- a/features/encrypt/distsql/handler/pom.xml +++ b/features/encrypt/distsql/handler/pom.xml @@ -50,7 +50,7 @@ org.apache.shardingsphere - shardingsphere-test-util + shardingsphere-test-it-distsql ${project.version} test diff --git a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutorTest.java b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutorTest.java index 7c1d1afca6e0c..69c173440d5ea 100644 --- a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutorTest.java +++ b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutorTest.java @@ -17,8 +17,7 @@ package org.apache.shardingsphere.encrypt.distsql.handler.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.encrypt.config.EncryptRuleConfiguration; import org.apache.shardingsphere.encrypt.config.rule.EncryptColumnItemRuleConfiguration; import org.apache.shardingsphere.encrypt.config.rule.EncryptColumnRuleConfiguration; @@ -26,61 +25,51 @@ import org.apache.shardingsphere.encrypt.distsql.statement.ShowEncryptRulesStatement; import org.apache.shardingsphere.encrypt.rule.EncryptRule; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.junit.jupiter.api.Test; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; import java.util.Collection; import java.util.Collections; -import java.util.Optional; import java.util.Properties; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowEncryptRuleExecutorTest { +class ShowEncryptRuleExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - @Test - void assertExecuteQuery() throws SQLException { - DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(new ShowEncryptRulesStatement("T_ENCRYPT", null), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - LocalDataQueryResultRow row = actual.iterator().next(); - assertThat(row.getCell(1), is("t_encrypt")); - assertThat(row.getCell(2), is("user_id")); - assertThat(row.getCell(3), is("user_cipher")); - assertThat(row.getCell(4), is("user_assisted")); - assertThat(row.getCell(5), is("user_like")); - assertThat(row.getCell(6), is("md5")); - assertThat(row.getCell(7), is("")); - assertThat(row.getCell(8), is("")); - assertThat(row.getCell(9), is("")); - assertThat(row.getCell(10), is("")); - assertThat(row.getCell(11), is("")); + ShowEncryptRuleExecutorTest() { + super(mock(EncryptRule.class)); } - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - EncryptRule rule = mock(EncryptRule.class); - when(rule.getConfiguration()).thenReturn(getRuleConfiguration()); - when(database.getRuleMetaData().findSingleRule(EncryptRule.class)).thenReturn(Optional.of(rule)); - return result; + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private EncryptRuleConfiguration getRuleConfiguration() { - EncryptColumnRuleConfiguration encryptColumnRuleConfig = new EncryptColumnRuleConfiguration("user_id", new EncryptColumnItemRuleConfiguration("user_cipher", "test")); - encryptColumnRuleConfig.setAssistedQuery(new EncryptColumnItemRuleConfiguration("user_assisted", "foo_assist_query_encryptor")); - encryptColumnRuleConfig.setLikeQuery(new EncryptColumnItemRuleConfiguration("user_like", "foo_like_encryptor")); - EncryptTableRuleConfiguration encryptTableRuleConfig = new EncryptTableRuleConfiguration("t_encrypt", Collections.singleton(encryptColumnRuleConfig)); - AlgorithmConfiguration shardingSphereAlgorithmConfig = new AlgorithmConfiguration("md5", new Properties()); - return new EncryptRuleConfiguration(Collections.singleton(encryptTableRuleConfig), Collections.singletonMap("test", shardingSphereAlgorithmConfig)); + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", createRuleConfiguration(), new ShowEncryptRulesStatement("T_ENCRYPT", null), + Collections.singleton(new LocalDataQueryResultRow("t_encrypt", "user_id", "user_cipher", "user_assisted", "user_like", "md5", "", "", "", "", "")))); + } + + private EncryptRuleConfiguration createRuleConfiguration() { + EncryptColumnRuleConfiguration encryptColumnRuleConfig = new EncryptColumnRuleConfiguration("user_id", new EncryptColumnItemRuleConfiguration("user_cipher", "test")); + encryptColumnRuleConfig.setAssistedQuery(new EncryptColumnItemRuleConfiguration("user_assisted", "foo_assist_query_encryptor")); + encryptColumnRuleConfig.setLikeQuery(new EncryptColumnItemRuleConfiguration("user_like", "foo_like_encryptor")); + EncryptTableRuleConfiguration encryptTableRuleConfig = new EncryptTableRuleConfiguration("t_encrypt", Collections.singleton(encryptColumnRuleConfig)); + AlgorithmConfiguration shardingSphereAlgorithmConfig = new AlgorithmConfiguration("md5", new Properties()); + return new EncryptRuleConfiguration(Collections.singleton(encryptTableRuleConfig), Collections.singletonMap("test", shardingSphereAlgorithmConfig)); + } } } diff --git a/features/mask/distsql/handler/pom.xml b/features/mask/distsql/handler/pom.xml index 09916f001f59f..e4b602e370bbf 100644 --- a/features/mask/distsql/handler/pom.xml +++ b/features/mask/distsql/handler/pom.xml @@ -50,7 +50,7 @@ org.apache.shardingsphere - shardingsphere-test-util + shardingsphere-test-it-distsql ${project.version} test diff --git a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutorTest.java b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutorTest.java index 86db35a26e7be..f4da195b23392 100644 --- a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutorTest.java +++ b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutorTest.java @@ -17,71 +17,56 @@ package org.apache.shardingsphere.mask.distsql.handler.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.mask.config.MaskRuleConfiguration; import org.apache.shardingsphere.mask.config.rule.MaskColumnRuleConfiguration; import org.apache.shardingsphere.mask.config.rule.MaskTableRuleConfiguration; import org.apache.shardingsphere.mask.distsql.statement.ShowMaskRulesStatement; import org.apache.shardingsphere.mask.rule.MaskRule; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; -import java.util.Optional; import java.util.Properties; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowMaskRuleExecutorTest { +class ShowMaskRuleExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - private DistSQLQueryExecuteEngine engine; - - @BeforeEach - void setUp() { - ShowMaskRulesStatement showMaskRulesStatement = mock(ShowMaskRulesStatement.class); - when(showMaskRulesStatement.getTableName()).thenReturn("T_MASK"); - engine = new DistSQLQueryExecuteEngine(showMaskRulesStatement, "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - } - - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - MaskRule rule = mock(MaskRule.class); - when(rule.getConfiguration()).thenReturn(getRuleConfiguration()); - when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.of(rule)); - return result; + ShowMaskRuleExecutorTest() { + super(mock(MaskRule.class)); } - private MaskRuleConfiguration getRuleConfiguration() { - MaskColumnRuleConfiguration maskColumnRuleConfig = new MaskColumnRuleConfiguration("user_id", "t_mask_user_id_md5"); - MaskTableRuleConfiguration maskTableRuleConfig = new MaskTableRuleConfiguration("t_mask", Collections.singleton(maskColumnRuleConfig)); - AlgorithmConfiguration algorithmConfig = new AlgorithmConfiguration("md5", new Properties()); - return new MaskRuleConfiguration(Collections.singleton(maskTableRuleConfig), Collections.singletonMap("t_mask_user_id_md5", algorithmConfig)); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - @Test - void assertGetRowData() throws SQLException { - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("t_mask")); - assertThat(row.getCell(2), is("user_id")); - assertThat(row.getCell(3), is("md5")); - assertThat(row.getCell(4), is("")); + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", createRuleConfiguration(), new ShowMaskRulesStatement("T_MASK", null), + Collections.singleton(new LocalDataQueryResultRow("t_mask", "user_id", "md5", "")))); + } + + private MaskRuleConfiguration createRuleConfiguration() { + MaskColumnRuleConfiguration maskColumnRuleConfig = new MaskColumnRuleConfiguration("user_id", "t_mask_user_id_md5"); + MaskTableRuleConfiguration maskTableRuleConfig = new MaskTableRuleConfiguration("t_mask", Collections.singleton(maskColumnRuleConfig)); + AlgorithmConfiguration algorithmConfig = new AlgorithmConfiguration("md5", new Properties()); + return new MaskRuleConfiguration(Collections.singleton(maskTableRuleConfig), Collections.singletonMap("t_mask_user_id_md5", algorithmConfig)); + } } } diff --git a/features/readwrite-splitting/distsql/handler/pom.xml b/features/readwrite-splitting/distsql/handler/pom.xml index d0a824e165869..500ca1f9ecd23 100644 --- a/features/readwrite-splitting/distsql/handler/pom.xml +++ b/features/readwrite-splitting/distsql/handler/pom.xml @@ -55,7 +55,7 @@ org.apache.shardingsphere - shardingsphere-test-util + shardingsphere-test-it-distsql ${project.version} test diff --git a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowReadwriteSplittingRuleExecutorTest.java b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowReadwriteSplittingRuleExecutorTest.java index 54e68e91496a3..42f18f724250e 100644 --- a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowReadwriteSplittingRuleExecutorTest.java +++ b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowReadwriteSplittingRuleExecutorTest.java @@ -17,124 +17,88 @@ package org.apache.shardingsphere.readwritesplitting.distsql.handler.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes; import org.apache.shardingsphere.infra.rule.attribute.exportable.constant.ExportableConstants; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.readwritesplitting.config.ReadwriteSplittingRuleConfiguration; import org.apache.shardingsphere.readwritesplitting.config.rule.ReadwriteSplittingDataSourceGroupRuleConfiguration; import org.apache.shardingsphere.readwritesplitting.distsql.statement.ShowReadwriteSplittingRulesStatement; -import org.apache.shardingsphere.readwritesplitting.rule.attribute.ReadwriteSplittingExportableRuleAttribute; import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule; +import org.apache.shardingsphere.readwritesplitting.rule.attribute.ReadwriteSplittingExportableRuleAttribute; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; -import java.util.Optional; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -class ShowReadwriteSplittingRuleExecutorTest { - - private DistSQLQueryExecuteEngine engine; - - @Test - void assertGetRowData() throws SQLException { - engine = setUp(mock(ShowReadwriteSplittingRulesStatement.class), createRuleConfiguration()); - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("readwrite_ds")); - assertThat(row.getCell(2), is("ds_primary")); - assertThat(row.getCell(3), is("ds_slave_0,ds_slave_1")); - assertThat(row.getCell(4), is("DYNAMIC")); - assertThat(row.getCell(5), is("random")); - assertThat(row.getCell(6), is("{\"read_weight\":\"2:1\"}")); - } +class ShowReadwriteSplittingRuleExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - @Test - void assertGetRowDataWithSpecifiedRuleName() throws SQLException { - engine = setUp(new ShowReadwriteSplittingRulesStatement("readwrite_ds", null), createRuleConfiguration()); - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("readwrite_ds")); - assertThat(row.getCell(2), is("ds_primary")); - assertThat(row.getCell(3), is("ds_slave_0,ds_slave_1")); - assertThat(row.getCell(4), is("DYNAMIC")); - assertThat(row.getCell(5), is("random")); - assertThat(row.getCell(6), is("{\"read_weight\":\"2:1\"}")); + ShowReadwriteSplittingRuleExecutorTest() { + super(mockRule()); } - private DistSQLQueryExecuteEngine setUp(final ShowReadwriteSplittingRulesStatement statement, final ReadwriteSplittingRuleConfiguration ruleConfig) { - return new DistSQLQueryExecuteEngine(statement, "foo_db", mockContextManager(ruleConfig), mock(DistSQLConnectionContext.class)); - } - - private ContextManager mockContextManager(final ReadwriteSplittingRuleConfiguration ruleConfig) { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ReadwriteSplittingRule rule = mock(ReadwriteSplittingRule.class); + private static ReadwriteSplittingRule mockRule() { + ReadwriteSplittingRule result = mock(ReadwriteSplittingRule.class); ReadwriteSplittingExportableRuleAttribute ruleAttribute = mock(ReadwriteSplittingExportableRuleAttribute.class); when(ruleAttribute.getExportData()).thenReturn(createExportedData()); - when(rule.getConfiguration()).thenReturn(ruleConfig); - when(rule.getAttributes()).thenReturn(new RuleAttributes(ruleAttribute)); - when(database.getRuleMetaData().findSingleRule(ReadwriteSplittingRule.class)).thenReturn(Optional.of(rule)); + when(result.getAttributes()).thenReturn(new RuleAttributes(ruleAttribute)); return result; } - private Map createExportedData() { + private static Map createExportedData() { Map result = new HashMap<>(2, 1F); result.put(ExportableConstants.EXPORT_DYNAMIC_READWRITE_SPLITTING_RULE, Collections.emptyMap()); result.put(ExportableConstants.EXPORT_STATIC_READWRITE_SPLITTING_RULE, Collections.emptyMap()); return result; } - private ReadwriteSplittingRuleConfiguration createRuleConfiguration() { - ReadwriteSplittingDataSourceGroupRuleConfiguration dataSourceGroupConfig = - new ReadwriteSplittingDataSourceGroupRuleConfiguration("readwrite_ds", "ds_primary", Arrays.asList("ds_slave_0", "ds_slave_1"), "test"); - return new ReadwriteSplittingRuleConfiguration( - Collections.singleton(dataSourceGroupConfig), Collections.singletonMap("test", new AlgorithmConfiguration("random", PropertiesBuilder.build(new Property("read_weight", "2:1"))))); - } - - @Test - void assertGetRowDataWithoutLoadBalancer() throws SQLException { - engine = setUp(mock(ShowReadwriteSplittingRulesStatement.class), createRuleConfigurationWithoutLoadBalancer()); - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("readwrite_ds")); - assertThat(row.getCell(2), is("write_ds")); - assertThat(row.getCell(3), is("read_ds_0,read_ds_1")); - assertThat(row.getCell(4), is("DYNAMIC")); - assertThat(row.getCell(5), is("")); - assertThat(row.getCell(6), is("")); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ReadwriteSplittingRuleConfiguration createRuleConfigurationWithoutLoadBalancer() { - ReadwriteSplittingDataSourceGroupRuleConfiguration dataSourceGroupConfig = - new ReadwriteSplittingDataSourceGroupRuleConfiguration("readwrite_ds", "write_ds", Arrays.asList("read_ds_0", "read_ds_1"), null); - return new ReadwriteSplittingRuleConfiguration(Collections.singleton(dataSourceGroupConfig), null); + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("withNull", createRuleConfiguration(), new ShowReadwriteSplittingRulesStatement(null, null), + Collections.singleton(new LocalDataQueryResultRow("readwrite_ds", "ds_primary", "ds_slave_0,ds_slave_1", "DYNAMIC", "random", "{\"read_weight\":\"2:1\"}"))), + Arguments.arguments("withSpecifiedRuleName", createRuleConfiguration(), new ShowReadwriteSplittingRulesStatement("readwrite_ds", null), + Collections.singleton(new LocalDataQueryResultRow("readwrite_ds", "ds_primary", "ds_slave_0,ds_slave_1", "DYNAMIC", "random", "{\"read_weight\":\"2:1\"}"))), + Arguments.arguments("withoutLoadBalancer", createRuleConfigurationWithoutLoadBalancer(), new ShowReadwriteSplittingRulesStatement("readwrite_ds", null), + Collections.singleton(new LocalDataQueryResultRow("readwrite_ds", "write_ds", "read_ds_0,read_ds_1", "DYNAMIC", "", "")))); + } + + private ReadwriteSplittingRuleConfiguration createRuleConfiguration() { + ReadwriteSplittingDataSourceGroupRuleConfiguration dataSourceGroupConfig = + new ReadwriteSplittingDataSourceGroupRuleConfiguration("readwrite_ds", "ds_primary", Arrays.asList("ds_slave_0", "ds_slave_1"), "test"); + return new ReadwriteSplittingRuleConfiguration( + Collections.singleton(dataSourceGroupConfig), Collections.singletonMap("test", new AlgorithmConfiguration("random", PropertiesBuilder.build(new Property("read_weight", "2:1"))))); + } + + private ReadwriteSplittingRuleConfiguration createRuleConfigurationWithoutLoadBalancer() { + ReadwriteSplittingDataSourceGroupRuleConfiguration dataSourceGroupConfig = + new ReadwriteSplittingDataSourceGroupRuleConfiguration("readwrite_ds", "write_ds", Arrays.asList("read_ds_0", "read_ds_1"), null); + return new ReadwriteSplittingRuleConfiguration(Collections.singleton(dataSourceGroupConfig), null); + } } } diff --git a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowStatusFromReadwriteSplittingRulesExecutorTest.java b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowStatusFromReadwriteSplittingRulesExecutorTest.java index afa38bc28f59f..d00e6c2e7a62c 100644 --- a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowStatusFromReadwriteSplittingRulesExecutorTest.java +++ b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowStatusFromReadwriteSplittingRulesExecutorTest.java @@ -17,64 +17,45 @@ package org.apache.shardingsphere.readwritesplitting.distsql.handler.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.readwritesplitting.config.ReadwriteSplittingRuleConfiguration; import org.apache.shardingsphere.readwritesplitting.distsql.statement.ShowStatusFromReadwriteSplittingRulesStatement; import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingDataSourceGroupRule; import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule; -import org.junit.jupiter.api.Test; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; -import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; -import java.util.List; import java.util.Map; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -class ShowStatusFromReadwriteSplittingRulesExecutorTest { +class ShowStatusFromReadwriteSplittingRulesExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - @Test - void assertExecuteQueryWithoutRuleName() throws SQLException { - ContextManager contextManager = mockContextManager(mockRule()); - DistSQLQueryExecuteEngine queryExecuteEngine = new DistSQLQueryExecuteEngine( - new ShowStatusFromReadwriteSplittingRulesStatement(null, null), "foo_db", contextManager, mock(DistSQLConnectionContext.class)); - queryExecuteEngine.executeQuery(); - List rows = new ArrayList<>(queryExecuteEngine.getRows()); - assertThat(rows.size(), is(2)); - assertThat(rows.get(0).getCell(1), is("read_ds_0")); - assertThat(rows.get(0).getCell(2), is("ENABLED")); - assertThat(rows.get(1).getCell(1), is("read_ds_1")); - assertThat(rows.get(1).getCell(2), is("DISABLED")); + ShowStatusFromReadwriteSplittingRulesExecutorTest() { + super(mockRule()); } - @Test - void assertExecuteQueryWithRuleName() throws SQLException { - ContextManager contextManager = mockContextManager(mockRule()); - DistSQLQueryExecuteEngine queryExecuteEngine = new DistSQLQueryExecuteEngine( - new ShowStatusFromReadwriteSplittingRulesStatement(null, "bar_rule"), "foo_db", contextManager, mock(DistSQLConnectionContext.class)); - queryExecuteEngine.executeQuery(); - assertTrue(queryExecuteEngine.getRows().isEmpty()); - } - - private ReadwriteSplittingRule mockRule() { + private static ReadwriteSplittingRule mockRule() { ReadwriteSplittingRule result = mock(ReadwriteSplittingRule.class); Map dataSourceGroupRules = Collections.singletonMap("group_0", mockDataSourceGroupRule()); when(result.getDataSourceRuleGroups()).thenReturn(dataSourceGroupRules); return result; } - private ReadwriteSplittingDataSourceGroupRule mockDataSourceGroupRule() { + private static ReadwriteSplittingDataSourceGroupRule mockDataSourceGroupRule() { ReadwriteSplittingDataSourceGroupRule result = mock(ReadwriteSplittingDataSourceGroupRule.class, RETURNS_DEEP_STUBS); when(result.getName()).thenReturn("foo_rule"); when(result.getReadwriteSplittingGroup().getReadDataSources()).thenReturn(Arrays.asList("read_ds_0", "read_ds_1")); @@ -82,12 +63,21 @@ private ReadwriteSplittingDataSourceGroupRule mockDataSourceGroupRule() { return result; } - private ContextManager mockContextManager(final ReadwriteSplittingRule rule) { - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(database.getName()).thenReturn("foo_db"); - when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - return result; + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); + } + + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("withoutRuleName", mock(ReadwriteSplittingRuleConfiguration.class), new ShowStatusFromReadwriteSplittingRulesStatement(null, null), + Arrays.asList(new LocalDataQueryResultRow("read_ds_0", "ENABLED"), new LocalDataQueryResultRow("read_ds_1", "DISABLED"))), + Arguments.arguments("withRuleName", mock(ReadwriteSplittingRuleConfiguration.class), new ShowStatusFromReadwriteSplittingRulesStatement(null, "bar_rule"), + Collections.emptyList())); + } } } diff --git a/features/shadow/distsql/handler/pom.xml b/features/shadow/distsql/handler/pom.xml index 55c2f74f0ced2..a40c265bdf9a7 100644 --- a/features/shadow/distsql/handler/pom.xml +++ b/features/shadow/distsql/handler/pom.xml @@ -50,7 +50,7 @@ org.apache.shardingsphere - shardingsphere-test-util + shardingsphere-test-it-distsql ${project.version} test diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowDefaultShadowAlgorithmExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowDefaultShadowAlgorithmExecutorTest.java index 31ba4f216f222..89cbb7b4fa68c 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowDefaultShadowAlgorithmExecutorTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowDefaultShadowAlgorithmExecutorTest.java @@ -17,69 +17,57 @@ package org.apache.shardingsphere.shadow.distsql.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.shadow.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.config.table.ShadowTableConfiguration; import org.apache.shardingsphere.shadow.distsql.statement.ShowDefaultShadowAlgorithmStatement; import org.apache.shardingsphere.shadow.rule.ShadowRule; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; 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.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; -import java.util.Optional; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowDefaultShadowAlgorithmExecutorTest { +class ShowDefaultShadowAlgorithmExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - private DistSQLQueryExecuteEngine engine; - - @BeforeEach - void setUp() { - engine = new DistSQLQueryExecuteEngine(mock(ShowDefaultShadowAlgorithmStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - } - - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ShadowRule rule = mock(ShadowRule.class); - when(rule.getConfiguration()).thenReturn(createRuleConfiguration()); - when(database.getRuleMetaData().findSingleRule(ShadowRule.class)).thenReturn(Optional.of(rule)); - return result; + ShowDefaultShadowAlgorithmExecutorTest() { + super(mock(ShadowRule.class)); } - @Test - void assertGetRowData() throws SQLException { - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("shadowAlgorithmName")); - assertThat(row.getCell(2), is("sql_hint")); - assertThat(row.getCell(3), is("{\"foo\":\"bar\"}")); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ShadowRuleConfiguration createRuleConfiguration() { - ShadowRuleConfiguration result = new ShadowRuleConfiguration(); - result.getTables().put("t_order", new ShadowTableConfiguration(Collections.emptyList(), Collections.singleton("shadowAlgorithmName"))); - result.getShadowAlgorithms().put("shadowAlgorithmName", new AlgorithmConfiguration("sql_hint", PropertiesBuilder.build(new Property("foo", "bar")))); - result.setDefaultShadowAlgorithmName("shadowAlgorithmName"); - return result; + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", createRuleConfiguration(), new ShowDefaultShadowAlgorithmStatement(null), + Collections.singleton(new LocalDataQueryResultRow("shadowAlgorithmName", "sql_hint", "{\"foo\":\"bar\"}")))); + } + + private ShadowRuleConfiguration createRuleConfiguration() { + ShadowRuleConfiguration result = new ShadowRuleConfiguration(); + result.getTables().put("t_order", new ShadowTableConfiguration(Collections.emptyList(), Collections.singleton("shadowAlgorithmName"))); + result.getShadowAlgorithms().put("shadowAlgorithmName", new AlgorithmConfiguration("sql_hint", PropertiesBuilder.build(new Property("foo", "bar")))); + result.setDefaultShadowAlgorithmName("shadowAlgorithmName"); + return result; + } } } diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowAlgorithmsExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowAlgorithmsExecutorTest.java index 4237a6d1e0949..810f6cbb9139d 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowAlgorithmsExecutorTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowAlgorithmsExecutorTest.java @@ -17,69 +17,56 @@ package org.apache.shardingsphere.shadow.distsql.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.shadow.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.config.table.ShadowTableConfiguration; import org.apache.shardingsphere.shadow.distsql.statement.ShowShadowAlgorithmsStatement; import org.apache.shardingsphere.shadow.rule.ShadowRule; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; 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.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; -import java.util.Optional; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowShadowAlgorithmsExecutorTest { +class ShowShadowAlgorithmsExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - private DistSQLQueryExecuteEngine engine; - - @BeforeEach - void setUp() { - engine = new DistSQLQueryExecuteEngine(mock(ShowShadowAlgorithmsStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - } - - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ShadowRule rule = mock(ShadowRule.class); - when(rule.getConfiguration()).thenReturn(createRuleConfiguration()); - when(database.getRuleMetaData().findSingleRule(ShadowRule.class)).thenReturn(Optional.of(rule)); - return result; + ShowShadowAlgorithmsExecutorTest() { + super(mock(ShadowRule.class)); } - @Test - void assertGetRowData() throws SQLException { - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("shadowAlgorithmName")); - assertThat(row.getCell(2), is("sql_hint")); - assertThat(row.getCell(3), is("{\"foo\":\"bar\"}")); - assertThat(row.getCell(4), is("false")); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ShadowRuleConfiguration createRuleConfiguration() { - ShadowRuleConfiguration result = new ShadowRuleConfiguration(); - result.getTables().put("t_order", new ShadowTableConfiguration(Collections.emptyList(), Collections.singleton("shadowAlgorithmName"))); - result.getShadowAlgorithms().put("shadowAlgorithmName", new AlgorithmConfiguration("sql_hint", PropertiesBuilder.build(new Property("foo", "bar")))); - return result; + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", createRuleConfiguration(), new ShowShadowAlgorithmsStatement(null), + Collections.singleton(new LocalDataQueryResultRow("shadowAlgorithmName", "sql_hint", "{\"foo\":\"bar\"}", "false")))); + } + + private ShadowRuleConfiguration createRuleConfiguration() { + ShadowRuleConfiguration result = new ShadowRuleConfiguration(); + result.getTables().put("t_order", new ShadowTableConfiguration(Collections.emptyList(), Collections.singleton("shadowAlgorithmName"))); + result.getShadowAlgorithms().put("shadowAlgorithmName", new AlgorithmConfiguration("sql_hint", PropertiesBuilder.build(new Property("foo", "bar")))); + return result; + } } } diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java index 7682080349e81..20269db788553 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java @@ -17,80 +17,60 @@ package org.apache.shardingsphere.shadow.distsql.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.shadow.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.config.datasource.ShadowDataSourceConfiguration; import org.apache.shardingsphere.shadow.config.table.ShadowTableConfiguration; import org.apache.shardingsphere.shadow.distsql.statement.ShowShadowRulesStatement; import org.apache.shardingsphere.shadow.rule.ShadowRule; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; -import java.util.Optional; import java.util.Properties; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowShadowRuleExecutorTest { +class ShowShadowRuleExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - private DistSQLQueryExecuteEngine engine; - - @BeforeEach - void setUp() { - engine = new DistSQLQueryExecuteEngine(mock(ShowShadowRulesStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - } - - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ShadowRule rule = mock(ShadowRule.class); - when(rule.getConfiguration()).thenReturn(createRuleConfiguration()); - when(database.getRuleMetaData().findSingleRule(ShadowRule.class)).thenReturn(Optional.of(rule)); - return result; + ShowShadowRuleExecutorTest() { + super(mock(ShadowRule.class)); } - @Test - void assertGetRowData() throws SQLException { - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(2)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("t_order")); - assertThat(row.getCell(2), is("shadow_rule")); - assertThat(row.getCell(3), is("source")); - assertThat(row.getCell(4), is("shadow")); - assertThat(row.getCell(5), is("REGEX_MATCH")); - assertThat(row.getCell(6), is("")); - row = iterator.next(); - assertThat(row.getCell(1), is("t_order_item")); - assertThat(row.getCell(2), is("shadow_rule")); - assertThat(row.getCell(3), is("source")); - assertThat(row.getCell(4), is("shadow")); - assertThat(row.getCell(5), is("REGEX_MATCH")); - assertThat(row.getCell(6), is("")); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ShadowRuleConfiguration createRuleConfiguration() { - ShadowRuleConfiguration result = new ShadowRuleConfiguration(); - result.getDataSources().add(new ShadowDataSourceConfiguration("shadow_rule", "source", "shadow")); - result.getShadowAlgorithms().put("user_id_select_match_algorithm", new AlgorithmConfiguration("REGEX_MATCH", new Properties())); - result.getTables().put("t_order", new ShadowTableConfiguration(Collections.singleton("shadow_rule"), Collections.singleton("user_id_select_match_algorithm"))); - result.getTables().put("t_order_item", new ShadowTableConfiguration(Collections.singleton("shadow_rule"), Collections.singleton("user_id_select_match_algorithm"))); - return result; + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", createRuleConfiguration(), new ShowShadowRulesStatement(null, null), + Arrays.asList(new LocalDataQueryResultRow("t_order", "shadow_rule", "source", "shadow", "REGEX_MATCH", ""), + new LocalDataQueryResultRow("t_order_item", "shadow_rule", "source", "shadow", "REGEX_MATCH", "")))); + } + + private ShadowRuleConfiguration createRuleConfiguration() { + ShadowRuleConfiguration result = new ShadowRuleConfiguration(); + result.getDataSources().add(new ShadowDataSourceConfiguration("shadow_rule", "source", "shadow")); + result.getShadowAlgorithms().put("user_id_select_match_algorithm", new AlgorithmConfiguration("REGEX_MATCH", new Properties())); + result.getTables().put("t_order", new ShadowTableConfiguration(Collections.singleton("shadow_rule"), Collections.singleton("user_id_select_match_algorithm"))); + result.getTables().put("t_order_item", new ShadowTableConfiguration(Collections.singleton("shadow_rule"), Collections.singleton("user_id_select_match_algorithm"))); + return result; + } } } diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowTableRulesExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowTableRulesExecutorTest.java index 115aa851f7485..dda50c51fc05b 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowTableRulesExecutorTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowTableRulesExecutorTest.java @@ -17,68 +17,57 @@ package org.apache.shardingsphere.shadow.distsql.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.shadow.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.config.table.ShadowTableConfiguration; import org.apache.shardingsphere.shadow.distsql.statement.ShowShadowTableRulesStatement; import org.apache.shardingsphere.shadow.rule.ShadowRule; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; 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.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; -import java.util.Optional; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowShadowTableRulesExecutorTest { +class ShowShadowTableRulesExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - private DistSQLQueryExecuteEngine engine; - - @BeforeEach - void setUp() { - engine = new DistSQLQueryExecuteEngine(mock(ShowShadowTableRulesStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - } - - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ShadowRule rule = mock(ShadowRule.class); - when(rule.getConfiguration()).thenReturn(createRuleConfiguration()); - when(database.getRuleMetaData().findSingleRule(ShadowRule.class)).thenReturn(Optional.of(rule)); - return result; + ShowShadowTableRulesExecutorTest() { + super(mock(ShadowRule.class)); } - @Test - void assertGetRowData() throws SQLException { - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("t_order")); - assertThat(row.getCell(2), is("shadowAlgorithmName_1,shadowAlgorithmName_2")); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ShadowRuleConfiguration createRuleConfiguration() { - ShadowRuleConfiguration result = new ShadowRuleConfiguration(); - result.getTables().put("t_order", new ShadowTableConfiguration(Collections.emptyList(), Arrays.asList("shadowAlgorithmName_1", "shadowAlgorithmName_2"))); - result.getShadowAlgorithms().put("shadowAlgorithmName", new AlgorithmConfiguration("sql_hint", PropertiesBuilder.build(new Property("foo", "bar")))); - return result; + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", createRuleConfiguration(), new ShowShadowTableRulesStatement(null, null), + Collections.singleton(new LocalDataQueryResultRow("t_order", "shadowAlgorithmName_1,shadowAlgorithmName_2")))); + } + + private ShadowRuleConfiguration createRuleConfiguration() { + ShadowRuleConfiguration result = new ShadowRuleConfiguration(); + result.getTables().put("t_order", new ShadowTableConfiguration(Collections.emptyList(), Arrays.asList("shadowAlgorithmName_1", "shadowAlgorithmName_2"))); + result.getShadowAlgorithms().put("shadowAlgorithmName", new AlgorithmConfiguration("sql_hint", PropertiesBuilder.build(new Property("foo", "bar")))); + return result; + } } } diff --git a/features/sharding/distsql/handler/pom.xml b/features/sharding/distsql/handler/pom.xml index 8683ba5e08fc9..4d389dd91836d 100644 --- a/features/sharding/distsql/handler/pom.xml +++ b/features/sharding/distsql/handler/pom.xml @@ -50,13 +50,13 @@ org.apache.shardingsphere - shardingsphere-test-util + shardingsphere-test-fixture-database ${project.version} test org.apache.shardingsphere - shardingsphere-test-fixture-database + shardingsphere-test-it-distsql ${project.version} test diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowDefaultShardingStrategyExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowDefaultShardingStrategyExecutorTest.java index e10d3f94dd0ec..6e6d2568ee00b 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowDefaultShardingStrategyExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowDefaultShardingStrategyExecutorTest.java @@ -17,121 +17,72 @@ package org.apache.shardingsphere.sharding.distsql.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.strategy.sharding.HintShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.ShowDefaultShardingStrategyStatement; import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.util.Arrays; +import java.util.Collection; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowDefaultShardingStrategyExecutorTest { +class ShowDefaultShardingStrategyExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - @Test - void assertExecuteQueryWithNullShardingStrategy() throws SQLException { - DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine( - mock(ShowDefaultShardingStrategyStatement.class), "foo_db", mockContextManager(new ShardingRuleConfiguration()), mock(DistSQLConnectionContext.class)); - engine.executeQuery(); - List actual = new ArrayList<>(engine.getRows()); - assertThat(actual.size(), is(2)); - assertThat(actual.get(0).getCell(1), is("TABLE")); - assertThat(actual.get(0).getCell(2), is("")); - assertThat(actual.get(0).getCell(3), is("")); - assertThat(actual.get(0).getCell(4), is("")); - assertThat(actual.get(0).getCell(5), is("")); - assertThat(actual.get(0).getCell(6), is("")); - assertThat(actual.get(1).getCell(1), is("DATABASE")); - assertThat(actual.get(1).getCell(2), is("")); - assertThat(actual.get(1).getCell(3), is("")); - assertThat(actual.get(1).getCell(4), is("")); - assertThat(actual.get(1).getCell(5), is("")); - assertThat(actual.get(1).getCell(6), is("")); + ShowDefaultShardingStrategyExecutorTest() { + super(mock(ShardingRule.class)); } - @Test - void assertExecuteQueryWithNoneShardingStrategyType() throws SQLException { - DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine( - mock(ShowDefaultShardingStrategyStatement.class), "foo_db", mockContextManager(createRuleConfigurationWithNoneShardingStrategyType()), mock(DistSQLConnectionContext.class)); - engine.executeQuery(); - List actual = new ArrayList<>(engine.getRows()); - assertThat(actual.size(), is(2)); - assertThat(actual.get(0).getCell(1), is("TABLE")); - assertThat(actual.get(0).getCell(2), is("NONE")); - assertThat(actual.get(0).getCell(3), is("")); - assertThat(actual.get(0).getCell(4), is("")); - assertThat(actual.get(0).getCell(5), is("")); - assertThat(actual.get(0).getCell(6), is("")); - assertThat(actual.get(1).getCell(1), is("DATABASE")); - assertThat(actual.get(1).getCell(2), is("NONE")); - assertThat(actual.get(1).getCell(3), is("")); - assertThat(actual.get(1).getCell(4), is("")); - assertThat(actual.get(1).getCell(5), is("")); - assertThat(actual.get(1).getCell(6), is("")); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ShardingRuleConfiguration createRuleConfigurationWithNoneShardingStrategyType() { - ShardingRuleConfiguration result = new ShardingRuleConfiguration(); - result.setDefaultTableShardingStrategy(new NoneShardingStrategyConfiguration()); - result.setDefaultDatabaseShardingStrategy(new NoneShardingStrategyConfiguration()); - return result; - } - - @Test - void assertExecuteQueryWithShardingStrategyType() throws SQLException { - DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine( - mock(ShowDefaultShardingStrategyStatement.class), "foo_db", mockContextManager(createRuleConfigurationWithShardingStrategyType()), mock(DistSQLConnectionContext.class)); - engine.executeQuery(); - List actual = new ArrayList<>(engine.getRows()); - assertThat(actual.size(), is(2)); - assertThat(actual.get(0).getCell(1), is("TABLE")); - assertThat(actual.get(0).getCell(2), is("STANDARD")); - assertThat(actual.get(0).getCell(3), is("use_id")); - assertThat(actual.get(0).getCell(4), is("database_inline")); - assertThat(actual.get(0).getCell(5), is("INLINE")); - assertThat(actual.get(0).getCell(6), is("{\"algorithm-expression\":\"ds_${user_id % 2}\"}")); - assertThat(actual.get(1).getCell(1), is("DATABASE")); - assertThat(actual.get(1).getCell(2), is("HINT")); - assertThat(actual.get(1).getCell(3), is("")); - assertThat(actual.get(1).getCell(4), is("database_inline")); - assertThat(actual.get(1).getCell(5), is("INLINE")); - assertThat(actual.get(1).getCell(6), is("{\"algorithm-expression\":\"ds_${user_id % 2}\"}")); - } - - private ShardingRuleConfiguration createRuleConfigurationWithShardingStrategyType() { - ShardingRuleConfiguration result = new ShardingRuleConfiguration(); - result.getShardingAlgorithms().put("database_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}")))); - result.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("use_id", "database_inline")); - result.setDefaultDatabaseShardingStrategy(new HintShardingStrategyConfiguration("database_inline")); - return result; - } - - private ContextManager mockContextManager(final ShardingRuleConfiguration ruleConfig) { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ShardingRule rule = mock(ShardingRule.class); - when(rule.getConfiguration()).thenReturn(ruleConfig); - when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); - return result; + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("withNullShardingStrategy", new ShardingRuleConfiguration(), new ShowDefaultShardingStrategyStatement(null), + Arrays.asList(new LocalDataQueryResultRow("TABLE", "", "", "", "", ""), new LocalDataQueryResultRow("DATABASE", "", "", "", "", ""))), + Arguments.arguments("withNoneShardingStrategyType", createRuleConfigurationWithNoneShardingStrategyType(), new ShowDefaultShardingStrategyStatement(null), + Arrays.asList(new LocalDataQueryResultRow("TABLE", "NONE", "", "", "", ""), new LocalDataQueryResultRow("DATABASE", "NONE", "", "", "", ""))), + Arguments.arguments("withShardingStrategyType", createRuleConfigurationWithShardingStrategyType(), new ShowDefaultShardingStrategyStatement(null), + Arrays.asList( + new LocalDataQueryResultRow("TABLE", "STANDARD", "use_id", "database_inline", "INLINE", "{\"algorithm-expression\":\"ds_${user_id % 2}\"}"), + new LocalDataQueryResultRow("DATABASE", "HINT", "", "database_inline", "INLINE", "{\"algorithm-expression\":\"ds_${user_id % 2}\"}")))); + } + + private ShardingRuleConfiguration createRuleConfigurationWithNoneShardingStrategyType() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + result.setDefaultTableShardingStrategy(new NoneShardingStrategyConfiguration()); + result.setDefaultDatabaseShardingStrategy(new NoneShardingStrategyConfiguration()); + return result; + } + + private ShardingRuleConfiguration createRuleConfigurationWithShardingStrategyType() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + result.getShardingAlgorithms().put("database_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}")))); + result.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("use_id", "database_inline")); + result.setDefaultDatabaseShardingStrategy(new HintShardingStrategyConfiguration("database_inline")); + return result; + } } } diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingAlgorithmExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingAlgorithmExecutorTest.java index 41d6c18d98917..4eaba562f4180 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingAlgorithmExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingAlgorithmExecutorTest.java @@ -17,65 +17,54 @@ package org.apache.shardingsphere.sharding.distsql.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingAlgorithmsStatement; import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; 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.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; import java.util.Collection; -import java.util.Iterator; -import java.util.Optional; +import java.util.Collections; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowShardingAlgorithmExecutorTest { +class ShowShardingAlgorithmExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - private DistSQLQueryExecuteEngine engine; - - @BeforeEach - void setUp() { - engine = new DistSQLQueryExecuteEngine(mock(ShowShardingAlgorithmsStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - } - - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ShardingRule rule = mock(ShardingRule.class); - when(rule.getConfiguration()).thenReturn(createRuleConfiguration()); - when(database.getRuleMetaData().findSingleRule(ShardingRule.class)).thenReturn(Optional.of(rule)); - return result; + ShowShardingAlgorithmExecutorTest() { + super(mock(ShardingRule.class)); } - @Test - void assertGetRowData() throws SQLException { - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("database_inline")); - assertThat(row.getCell(2), is("INLINE")); - assertThat(row.getCell(3), is("{\"algorithm-expression\":\"ds_${user_id % 2}\"}")); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ShardingRuleConfiguration createRuleConfiguration() { - ShardingRuleConfiguration result = new ShardingRuleConfiguration(); - result.getShardingAlgorithms().put("database_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}")))); - return result; + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", createRuleConfiguration(), new ShowShardingAlgorithmsStatement(null), + Collections.singleton(new LocalDataQueryResultRow("database_inline", "INLINE", "{\"algorithm-expression\":\"ds_${user_id % 2}\"}")))); + } + + private ShardingRuleConfiguration createRuleConfiguration() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + result.getShardingAlgorithms().put("database_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}")))); + return result; + } } } diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingAuditorsExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingAuditorsExecutorTest.java index 75dbfd99af6d8..7a1091670c7a1 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingAuditorsExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingAuditorsExecutorTest.java @@ -17,65 +17,54 @@ package org.apache.shardingsphere.sharding.distsql.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingAuditorsStatement; import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; 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.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; import java.util.Collection; -import java.util.Iterator; -import java.util.Optional; +import java.util.Collections; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowShardingAuditorsExecutorTest { +class ShowShardingAuditorsExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - private DistSQLQueryExecuteEngine engine; - - @BeforeEach - void setUp() { - engine = new DistSQLQueryExecuteEngine(mock(ShowShardingAuditorsStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - } - - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ShardingRule rule = mock(ShardingRule.class); - when(rule.getConfiguration()).thenReturn(createRuleConfiguration()); - when(database.getRuleMetaData().findSingleRule(ShardingRule.class)).thenReturn(Optional.of(rule)); - return result; + ShowShardingAuditorsExecutorTest() { + super(mock(ShardingRule.class)); } - @Test - void assertGetRowData() throws SQLException { - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("sharding_key_required_auditor")); - assertThat(row.getCell(2), is("DML_SHARDING_CONDITIONS")); - assertThat(row.getCell(3).toString(), is("{\"key\":\"value\"}")); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ShardingRuleConfiguration createRuleConfiguration() { - ShardingRuleConfiguration result = new ShardingRuleConfiguration(); - result.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", PropertiesBuilder.build(new Property("key", "value")))); - return result; + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", createRuleConfiguration(), new ShowShardingAuditorsStatement(null), + Collections.singleton(new LocalDataQueryResultRow("sharding_key_required_auditor", "DML_SHARDING_CONDITIONS", "{\"key\":\"value\"}")))); + } + + private ShardingRuleConfiguration createRuleConfiguration() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + result.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", PropertiesBuilder.build(new Property("key", "value")))); + return result; + } } } diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingKeyGeneratorExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingKeyGeneratorExecutorTest.java index 1884f794ce0dc..66fcd3fa11563 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingKeyGeneratorExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingKeyGeneratorExecutorTest.java @@ -17,65 +17,54 @@ package org.apache.shardingsphere.sharding.distsql.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingKeyGeneratorsStatement; import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; 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.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; import java.util.Collection; -import java.util.Iterator; -import java.util.Optional; +import java.util.Collections; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowShardingKeyGeneratorExecutorTest { +class ShowShardingKeyGeneratorExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - private DistSQLQueryExecuteEngine engine; - - @BeforeEach - void setUp() { - engine = new DistSQLQueryExecuteEngine(mock(ShowShardingKeyGeneratorsStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - } - - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ShardingRule rule = mock(ShardingRule.class); - when(rule.getConfiguration()).thenReturn(createRuleConfiguration()); - when(database.getRuleMetaData().findSingleRule(ShardingRule.class)).thenReturn(Optional.of(rule)); - return result; + ShowShardingKeyGeneratorExecutorTest() { + super(mock(ShardingRule.class)); } - @Test - void assertGetRowData() throws SQLException { - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("snowflake")); - assertThat(row.getCell(2), is("SNOWFLAKE")); - assertThat(row.getCell(3), is("{\"key\":\"value\"}")); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ShardingRuleConfiguration createRuleConfiguration() { - ShardingRuleConfiguration result = new ShardingRuleConfiguration(); - result.getKeyGenerators().put("snowflake", new AlgorithmConfiguration("SNOWFLAKE", PropertiesBuilder.build(new Property("key", "value")))); - return result; + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", createRuleConfiguration(), new ShowShardingKeyGeneratorsStatement(null), + Collections.singleton(new LocalDataQueryResultRow("snowflake", "SNOWFLAKE", "{\"key\":\"value\"}")))); + } + + private ShardingRuleConfiguration createRuleConfiguration() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + result.getKeyGenerators().put("snowflake", new AlgorithmConfiguration("SNOWFLAKE", PropertiesBuilder.build(new Property("key", "value")))); + return result; + } } } diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableReferenceRuleExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableReferenceRuleExecutorTest.java index 17649d8893349..9505f09acde6b 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableReferenceRuleExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableReferenceRuleExecutorTest.java @@ -17,78 +17,59 @@ package org.apache.shardingsphere.sharding.distsql.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableReferenceRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingTableReferenceRulesStatement; import org.apache.shardingsphere.sharding.rule.ShardingRule; -import org.junit.jupiter.api.Test; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; import java.util.Collection; -import java.util.Iterator; -import java.util.Optional; +import java.util.Collections; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowShardingTableReferenceRuleExecutorTest { +class ShowShardingTableReferenceRuleExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - private DistSQLQueryExecuteEngine engine; - - DistSQLQueryExecuteEngine setUp(final ShowShardingTableReferenceRulesStatement statement) { - return new DistSQLQueryExecuteEngine(statement, "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - } - - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ShardingRule rule = mock(ShardingRule.class); - when(rule.getConfiguration()).thenReturn(createRuleConfiguration()); - when(database.getRuleMetaData().findSingleRule(ShardingRule.class)).thenReturn(Optional.of(rule)); - return result; - } - - @Test - void assertGetRowData() throws SQLException { - engine = setUp(mock(ShowShardingTableReferenceRulesStatement.class)); - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("foo")); - assertThat(row.getCell(2), is("t_order,t_order_item")); + ShowShardingTableReferenceRuleExecutorTest() { + super(mock(ShardingRule.class)); } - @Test - void assertGetRowDataWithSpecifiedRuleName() throws SQLException { - engine = setUp(new ShowShardingTableReferenceRulesStatement("foo", null)); - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("foo")); - assertThat(row.getCell(2), is("t_order,t_order_item")); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ShardingRuleConfiguration createRuleConfiguration() { - ShardingRuleConfiguration result = new ShardingRuleConfiguration(); - result.getTables().add(new ShardingTableRuleConfiguration("t_order", null)); - result.getTables().add(new ShardingTableRuleConfiguration("t_order_item", null)); - result.getTables().add(new ShardingTableRuleConfiguration("t_1", null)); - result.getTables().add(new ShardingTableRuleConfiguration("t_2", null)); - result.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("foo", "t_order,t_order_item")); - return result; + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", createRuleConfiguration(), new ShowShardingTableReferenceRulesStatement(null, null), + Collections.singleton(new LocalDataQueryResultRow("foo", "t_order,t_order_item"))), + Arguments.arguments("withSpecifiedRuleName", createRuleConfiguration(), new ShowShardingTableReferenceRulesStatement("foo", null), + Collections.singleton(new LocalDataQueryResultRow("foo", "t_order,t_order_item")))); + } + + private ShardingRuleConfiguration createRuleConfiguration() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + result.getTables().add(new ShardingTableRuleConfiguration("t_order", null)); + result.getTables().add(new ShardingTableRuleConfiguration("t_order_item", null)); + result.getTables().add(new ShardingTableRuleConfiguration("t_1", null)); + result.getTables().add(new ShardingTableRuleConfiguration("t_2", null)); + result.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("foo", "t_order,t_order_item")); + return result; + } } } diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRuleExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRuleExecutorTest.java index 53a215ced3229..0955f3233c975 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRuleExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRuleExecutorTest.java @@ -17,12 +17,10 @@ package org.apache.shardingsphere.sharding.distsql.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.mode.manager.ContextManager; 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.audit.ShardingAuditStrategyConfiguration; @@ -31,88 +29,67 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingTableRulesStatement; import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; import java.util.Collection; import java.util.Collections; -import java.util.Optional; import java.util.Properties; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowShardingTableRuleExecutorTest { +class ShowShardingTableRuleExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - @Test - void assertExecuteQuery() throws SQLException { - DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(mock(ShowShardingTableRulesStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - LocalDataQueryResultRow row = actual.iterator().next(); - assertThat(row.getCell(1), is("t_order")); - assertThat(row.getCell(2), is("ds_${0..1}.t_order_${0..1}")); - assertThat(row.getCell(3), is("")); - assertThat(row.getCell(4), is("STANDARD")); - assertThat(row.getCell(5), is("user_id")); - assertThat(row.getCell(6), is("INLINE")); - assertThat(row.getCell(7), is("{\"algorithm-expression\":\"ds_${user_id % 2}\"}")); - assertThat(row.getCell(8), is("STANDARD")); - assertThat(row.getCell(9), is("order_id")); - assertThat(row.getCell(10), is("INLINE")); - assertThat(row.getCell(11), is("{\"algorithm-expression\":\"t_order_${order_id % 2}\"}")); - assertThat(row.getCell(12), is("order_id")); - assertThat(row.getCell(13), is("SNOWFLAKE")); - assertThat(row.getCell(14), is("")); - assertThat(row.getCell(15), is("DML_SHARDING_CONDITIONS")); - assertThat(row.getCell(16), is("true")); + ShowShardingTableRuleExecutorTest() { + super(mock(ShardingRule.class)); } - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ShardingRule rule = mock(ShardingRule.class); - when(rule.getConfiguration()).thenReturn(createRuleConfiguration()); - when(database.getRuleMetaData().findSingleRule(ShardingRule.class)).thenReturn(Optional.of(rule)); - return result; + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ShardingRuleConfiguration createRuleConfiguration() { - ShardingRuleConfiguration result = new ShardingRuleConfiguration(); - result.getTables().add(createShardingTableRuleConfiguration()); - result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "database_inline")); - result.setDefaultTableShardingStrategy(new NoneShardingStrategyConfiguration()); - result.getShardingAlgorithms().put("database_inline", createShardingInlineAlgorithmConfiguration("ds_${user_id % 2}")); - result.getShardingAlgorithms().put("t_order_inline", createShardingInlineAlgorithmConfiguration("t_order_${order_id % 2}")); - result.getKeyGenerators().put("snowflake", createKeyGeneratorConfiguration()); - result.getAuditors().put("sharding_key_required_auditor", createAuditorConfiguration()); - return result; - } - - private ShardingTableRuleConfiguration createShardingTableRuleConfiguration() { - ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order", "ds_${0..1}.t_order_${0..1}"); - result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "t_order_inline")); - result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake")); - result.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("sharding_key_required_auditor"), true)); - return result; - } - - private AlgorithmConfiguration createShardingInlineAlgorithmConfiguration(final String algorithmExpression) { - return new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", algorithmExpression))); - } - - private AlgorithmConfiguration createKeyGeneratorConfiguration() { - return new AlgorithmConfiguration("SNOWFLAKE", new Properties()); - } - - private AlgorithmConfiguration createAuditorConfiguration() { - return new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", new Properties()); + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", createRuleConfiguration(), new ShowShardingTableRulesStatement(null, null), + Collections.singleton(new LocalDataQueryResultRow("t_order", "ds_${0..1}.t_order_${0..1}", "", + "STANDARD", "user_id", + "INLINE", "{\"algorithm-expression\":\"ds_${user_id % 2}\"}", "STANDARD", "order_id", + "INLINE", "{\"algorithm-expression\":\"t_order_${order_id % 2}\"}", + "order_id", "SNOWFLAKE", + "", "DML_SHARDING_CONDITIONS", "true")))); + } + + private ShardingRuleConfiguration createRuleConfiguration() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + result.getTables().add(createShardingTableRuleConfiguration()); + result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "database_inline")); + result.setDefaultTableShardingStrategy(new NoneShardingStrategyConfiguration()); + result.getShardingAlgorithms().put("database_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}")))); + result.getShardingAlgorithms().put("t_order_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "t_order_${order_id % 2}")))); + result.getKeyGenerators().put("snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties())); + result.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", new Properties())); + return result; + } + + private ShardingTableRuleConfiguration createShardingTableRuleConfiguration() { + ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order", "ds_${0..1}.t_order_${0..1}"); + result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "t_order_inline")); + result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake")); + result.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("sharding_key_required_auditor"), true)); + return result; + } } } diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRulesUsedAlgorithmExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRulesUsedAlgorithmExecutorTest.java index d51872f715af6..f3f65b23ed004 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRulesUsedAlgorithmExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRulesUsedAlgorithmExecutorTest.java @@ -17,12 +17,10 @@ package org.apache.shardingsphere.sharding.distsql.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; @@ -31,96 +29,71 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingTableRulesUsedAlgorithmStatement; import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; import java.util.Collection; -import java.util.Iterator; -import java.util.Optional; +import java.util.Collections; import java.util.Properties; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowShardingTableRulesUsedAlgorithmExecutorTest { +class ShowShardingTableRulesUsedAlgorithmExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - @Test - void assertGetRowData1() throws SQLException { - DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine( - new ShowShardingTableRulesUsedAlgorithmStatement("t_order_inline", null), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("table")); - assertThat(row.getCell(2), is("t_order")); + ShowShardingTableRulesUsedAlgorithmExecutorTest() { + super(mock(ShardingRule.class)); } - @Test - void assertGetRowData2() throws SQLException { - DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine( - new ShowShardingTableRulesUsedAlgorithmStatement("auto_mod", null), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("auto_table")); - assertThat(row.getCell(2), is("t_order_auto")); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ShardingRule rule = mock(ShardingRule.class); - when(rule.getConfiguration()).thenReturn(createRuleConfiguration()); - when(database.getRuleMetaData().findSingleRule(ShardingRule.class)).thenReturn(Optional.of(rule)); - return result; - } - - private ShardingRuleConfiguration createRuleConfiguration() { - ShardingRuleConfiguration result = new ShardingRuleConfiguration(); - result.getTables().add(createShardingTableRuleConfiguration()); - result.getAutoTables().add(createShardingAutoTableRuleConfiguration()); - result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "database_inline")); - result.setDefaultTableShardingStrategy(new NoneShardingStrategyConfiguration()); - result.getShardingAlgorithms().put("database_inline", createShardingInlineAlgorithmConfiguration("ds_${user_id % 2}")); - result.getShardingAlgorithms().put("t_order_inline", createShardingInlineAlgorithmConfiguration("t_order_${order_id % 2}")); - result.getShardingAlgorithms().put("auto_mod", createShardingAutoModAlgorithmConfiguration()); - result.getKeyGenerators().put("snowflake", createKeyGeneratorConfiguration()); - return result; - } - - private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() { - ShardingAutoTableRuleConfiguration result = new ShardingAutoTableRuleConfiguration("t_order_auto", "ds_0, ds_1"); - result.setShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "auto_mod")); - result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake")); - return result; - } - - private ShardingTableRuleConfiguration createShardingTableRuleConfiguration() { - ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order", "ds_${0..1}.t_order_${0..1}"); - result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "t_order_inline")); - result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake")); - return result; - } - - private AlgorithmConfiguration createShardingInlineAlgorithmConfiguration(final String algorithmExpression) { - return new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", algorithmExpression))); - } - - private AlgorithmConfiguration createShardingAutoModAlgorithmConfiguration() { - return new AlgorithmConfiguration("MOD", PropertiesBuilder.build(new Property("sharding-count", "4"))); - } - - private AlgorithmConfiguration createKeyGeneratorConfiguration() { - return new AlgorithmConfiguration("SNOWFLAKE", new Properties()); + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("withTable", createRuleConfiguration(), new ShowShardingTableRulesUsedAlgorithmStatement("t_order_inline", null), + Collections.singleton(new LocalDataQueryResultRow("table", "t_order"))), + Arguments.arguments("withAutoTable", createRuleConfiguration(), new ShowShardingTableRulesUsedAlgorithmStatement("auto_mod", null), + Collections.singleton(new LocalDataQueryResultRow("auto_table", "t_order_auto")))); + } + + private ShardingRuleConfiguration createRuleConfiguration() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + result.getTables().add(createShardingTableRuleConfiguration()); + result.getAutoTables().add(createShardingAutoTableRuleConfiguration()); + result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "database_inline")); + result.setDefaultTableShardingStrategy(new NoneShardingStrategyConfiguration()); + result.getShardingAlgorithms().put("database_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}")))); + result.getShardingAlgorithms().put("t_order_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "t_order_${order_id % 2}")))); + result.getShardingAlgorithms().put("auto_mod", new AlgorithmConfiguration("MOD", PropertiesBuilder.build(new Property("sharding-count", "4")))); + result.getKeyGenerators().put("snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties())); + return result; + } + + private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() { + ShardingAutoTableRuleConfiguration result = new ShardingAutoTableRuleConfiguration("t_order_auto", "ds_0, ds_1"); + result.setShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "auto_mod")); + result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake")); + return result; + } + + private ShardingTableRuleConfiguration createShardingTableRuleConfiguration() { + ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order", "ds_${0..1}.t_order_${0..1}"); + result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "t_order_inline")); + result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake")); + return result; + } } } diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRulesUsedAuditorExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRulesUsedAuditorExecutorTest.java index 472d526dca1d2..ea76e9807b2ce 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRulesUsedAuditorExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRulesUsedAuditorExecutorTest.java @@ -17,13 +17,10 @@ package org.apache.shardingsphere.sharding.distsql.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; @@ -32,71 +29,70 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingTableRulesUsedAuditorStatement; import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; -import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; import java.util.Collections; -import java.util.List; import java.util.Properties; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowShardingTableRulesUsedAuditorExecutorTest { +class ShowShardingTableRulesUsedAuditorExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - @Test - void assertExecuteQuery() throws SQLException { - DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine( - new ShowShardingTableRulesUsedAuditorStatement("shardingKeyAudit", null), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - engine.executeQuery(); - List actual = new ArrayList<>(engine.getRows()); - assertThat(actual.size(), is(2)); - assertThat(actual.get(0).getCell(1), is("table")); - assertThat(actual.get(0).getCell(2), is("t_order")); - assertThat(actual.get(1).getCell(1), is("auto_table")); - assertThat(actual.get(1).getCell(2), is("t_order_auto")); + ShowShardingTableRulesUsedAuditorExecutorTest() { + super(mock(ShardingRule.class)); } - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ShardingRule rule = mock(ShardingRule.class); - when(rule.getConfiguration()).thenReturn(createRuleConfiguration()); - when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); - return result; + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ShardingRuleConfiguration createRuleConfiguration() { - ShardingRuleConfiguration result = new ShardingRuleConfiguration(); - result.getTables().add(createShardingTableRuleConfiguration()); - result.getAutoTables().add(createShardingAutoTableRuleConfiguration()); - result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "database_inline")); - result.setDefaultTableShardingStrategy(new NoneShardingStrategyConfiguration()); - result.getShardingAlgorithms().put("database_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}")))); - result.getShardingAlgorithms().put("t_order_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "t_order_${order_id % 2}")))); - result.getShardingAlgorithms().put("auto_mod", new AlgorithmConfiguration("MOD", PropertiesBuilder.build(new Property("sharding-count", "4")))); - result.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", new Properties())); - return result; - } - - private ShardingTableRuleConfiguration createShardingTableRuleConfiguration() { - ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order", "ds_${0..1}.t_order_${0..1}"); - result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "t_order_inline")); - result.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("shardingKeyAudit"), true)); - return result; - } - - private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() { - ShardingAutoTableRuleConfiguration result = new ShardingAutoTableRuleConfiguration("t_order_auto", "ds_0, ds_1"); - result.setShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "auto_mod")); - result.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("shardingKeyAudit"), true)); - return result; + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", createRuleConfiguration(), new ShowShardingTableRulesUsedAuditorStatement("shardingKeyAudit", null), + Arrays.asList(new LocalDataQueryResultRow("table", "t_order"), new LocalDataQueryResultRow("auto_table", "t_order_auto")))); + } + + private ShardingRuleConfiguration createRuleConfiguration() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + result.getTables().add(createShardingTableRuleConfiguration()); + result.getAutoTables().add(createShardingAutoTableRuleConfiguration()); + result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "database_inline")); + result.setDefaultTableShardingStrategy(new NoneShardingStrategyConfiguration()); + result.getShardingAlgorithms().put("database_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}")))); + result.getShardingAlgorithms().put("t_order_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "t_order_${order_id % 2}")))); + result.getShardingAlgorithms().put("auto_mod", new AlgorithmConfiguration("MOD", PropertiesBuilder.build(new Property("sharding-count", "4")))); + result.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", new Properties())); + return result; + } + + private ShardingTableRuleConfiguration createShardingTableRuleConfiguration() { + ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order", "ds_${0..1}.t_order_${0..1}"); + result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "t_order_inline")); + result.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("shardingKeyAudit"), true)); + return result; + } + + private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() { + ShardingAutoTableRuleConfiguration result = new ShardingAutoTableRuleConfiguration("t_order_auto", "ds_0, ds_1"); + result.setShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "auto_mod")); + result.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("shardingKeyAudit"), true)); + return result; + } } } diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRulesUsedKeyGeneratorExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRulesUsedKeyGeneratorExecutorTest.java index eab7aa109eb48..73b9f3f6a7822 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRulesUsedKeyGeneratorExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRulesUsedKeyGeneratorExecutorTest.java @@ -17,13 +17,10 @@ package org.apache.shardingsphere.sharding.distsql.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; @@ -32,71 +29,69 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingTableRulesUsedKeyGeneratorStatement; import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.util.Arrays; +import java.util.Collection; import java.util.Properties; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowShardingTableRulesUsedKeyGeneratorExecutorTest { +class ShowShardingTableRulesUsedKeyGeneratorExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - @Test - void assertExecuteQuery() throws SQLException { - DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine( - new ShowShardingTableRulesUsedKeyGeneratorStatement("snowflake", null), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - engine.executeQuery(); - List actual = new ArrayList<>(engine.getRows()); - assertThat(actual.size(), is(2)); - assertThat(actual.get(0).getCell(1), is("table")); - assertThat(actual.get(0).getCell(2), is("t_order")); - assertThat(actual.get(1).getCell(1), is("auto_table")); - assertThat(actual.get(1).getCell(2), is("t_order_auto")); + ShowShardingTableRulesUsedKeyGeneratorExecutorTest() { + super(mock(ShardingRule.class)); } - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ShardingRule rule = mock(ShardingRule.class); - when(rule.getConfiguration()).thenReturn(createRuleConfiguration()); - when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); - return result; + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ShardingRuleConfiguration createRuleConfiguration() { - ShardingRuleConfiguration result = new ShardingRuleConfiguration(); - result.getTables().add(createShardingTableRuleConfiguration()); - result.getAutoTables().add(createShardingAutoTableRuleConfiguration()); - result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "database_inline")); - result.setDefaultTableShardingStrategy(new NoneShardingStrategyConfiguration()); - result.getShardingAlgorithms().put("database_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}")))); - result.getShardingAlgorithms().put("t_order_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "t_order_${order_id % 2}")))); - result.getShardingAlgorithms().put("auto_mod", new AlgorithmConfiguration("MOD", PropertiesBuilder.build(new Property("sharding-count", "4")))); - result.getKeyGenerators().put("snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties())); - return result; - } - - private ShardingTableRuleConfiguration createShardingTableRuleConfiguration() { - ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order", "ds_${0..1}.t_order_${0..1}"); - result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "t_order_inline")); - result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake")); - return result; - } - - private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() { - ShardingAutoTableRuleConfiguration result = new ShardingAutoTableRuleConfiguration("t_order_auto", "ds_0, ds_1"); - result.setShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "auto_mod")); - result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake")); - return result; + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", createRuleConfiguration(), new ShowShardingTableRulesUsedKeyGeneratorStatement("snowflake", null), + Arrays.asList(new LocalDataQueryResultRow("table", "t_order"), new LocalDataQueryResultRow("auto_table", "t_order_auto")))); + } + + private ShardingRuleConfiguration createRuleConfiguration() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + result.getTables().add(createShardingTableRuleConfiguration()); + result.getAutoTables().add(createShardingAutoTableRuleConfiguration()); + result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "database_inline")); + result.setDefaultTableShardingStrategy(new NoneShardingStrategyConfiguration()); + result.getShardingAlgorithms().put("database_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}")))); + result.getShardingAlgorithms().put("t_order_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "t_order_${order_id % 2}")))); + result.getShardingAlgorithms().put("auto_mod", new AlgorithmConfiguration("MOD", PropertiesBuilder.build(new Property("sharding-count", "4")))); + result.getKeyGenerators().put("snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties())); + return result; + } + + private ShardingTableRuleConfiguration createShardingTableRuleConfiguration() { + ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order", "ds_${0..1}.t_order_${0..1}"); + result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "t_order_inline")); + result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake")); + return result; + } + + private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() { + ShardingAutoTableRuleConfiguration result = new ShardingAutoTableRuleConfiguration("t_order_auto", "ds_0, ds_1"); + result.setShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "auto_mod")); + result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake")); + return result; + } } } diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingAlgorithmsExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingAlgorithmsExecutorTest.java index a96e93512c403..66baf55e03499 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingAlgorithmsExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingAlgorithmsExecutorTest.java @@ -17,83 +17,64 @@ package org.apache.shardingsphere.sharding.distsql.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.ShowUnusedShardingAlgorithmsStatement; import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; 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.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; import java.util.Collection; -import java.util.Iterator; -import java.util.Optional; +import java.util.Collections; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowUnusedShardingAlgorithmsExecutorTest { +class ShowUnusedShardingAlgorithmsExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - private DistSQLQueryExecuteEngine engine; - - @BeforeEach - void setUp() { - engine = new DistSQLQueryExecuteEngine(mock(ShowUnusedShardingAlgorithmsStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - } - - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ShardingRule rule = mock(ShardingRule.class); - when(rule.getConfiguration()).thenReturn(createRuleConfiguration()); - when(database.getRuleMetaData().findSingleRule(ShardingRule.class)).thenReturn(Optional.of(rule)); - return result; - } - - @Test - void assertGetRowData() throws SQLException { - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("database_inline")); - assertThat(row.getCell(2), is("INLINE")); - assertThat(row.getCell(3), is("{\"algorithm-expression\":\"ds_${user_id % 2}\"}")); - } - - private ShardingRuleConfiguration createRuleConfiguration() { - ShardingRuleConfiguration result = new ShardingRuleConfiguration(); - result.getShardingAlgorithms().put("database_inline", createShardingInlineAlgorithmConfiguration()); - result.getShardingAlgorithms().put("t_order_hash_mod", createShardingHashModAlgorithmConfiguration()); - result.getAutoTables().add(createShardingAutoTableRuleConfiguration()); - return result; - } - - private AlgorithmConfiguration createShardingInlineAlgorithmConfiguration() { - return new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}"))); + ShowUnusedShardingAlgorithmsExecutorTest() { + super(mock(ShardingRule.class)); } - private AlgorithmConfiguration createShardingHashModAlgorithmConfiguration() { - return new AlgorithmConfiguration("hash_mod", PropertiesBuilder.build(new Property("sharding-count", "4"))); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() { - ShardingAutoTableRuleConfiguration result = new ShardingAutoTableRuleConfiguration("auto_table", null); - result.setShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "t_order_hash_mod")); - return result; + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", createRuleConfiguration(), new ShowUnusedShardingAlgorithmsStatement(null), + Collections.singleton(new LocalDataQueryResultRow("database_inline", "INLINE", "{\"algorithm-expression\":\"ds_${user_id % 2}\"}")))); + } + + private ShardingRuleConfiguration createRuleConfiguration() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + result.getShardingAlgorithms().put("database_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}")))); + result.getShardingAlgorithms().put("t_order_hash_mod", new AlgorithmConfiguration("hash_mod", PropertiesBuilder.build(new Property("sharding-count", "4")))); + result.getAutoTables().add(createShardingAutoTableRuleConfiguration()); + return result; + } + + private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() { + ShardingAutoTableRuleConfiguration result = new ShardingAutoTableRuleConfiguration("auto_table", null); + result.setShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "t_order_hash_mod")); + return result; + } } } diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingAuditorsExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingAuditorsExecutorTest.java index d2b5801f42232..4f0f49de21de0 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingAuditorsExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingAuditorsExecutorTest.java @@ -17,70 +17,65 @@ package org.apache.shardingsphere.sharding.distsql.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.ShowUnusedShardingAuditorsStatement; import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; import java.util.Collection; import java.util.Collections; -import java.util.Iterator; import java.util.Properties; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowUnusedShardingAuditorsExecutorTest { +class ShowUnusedShardingAuditorsExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - @Test - void assertExecuteQuery() throws SQLException { - DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(mock(ShowUnusedShardingAuditorsStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - Iterator iterator = actual.iterator(); - LocalDataQueryResultRow row = iterator.next(); - assertThat(row.getCell(1), is("fixture")); - assertThat(row.getCell(2), is("FIXTURE")); - assertThat(row.getCell(3), is("{\"key\":\"value\"}")); + ShowUnusedShardingAuditorsExecutorTest() { + super(mock(ShardingRule.class)); } - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ShardingRule rule = mock(ShardingRule.class); - when(rule.getConfiguration()).thenReturn(createRuleConfiguration()); - when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); - return result; + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ShardingRuleConfiguration createRuleConfiguration() { - ShardingRuleConfiguration result = new ShardingRuleConfiguration(); - result.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", new Properties())); - result.getAuditors().put("fixture", new AlgorithmConfiguration("FIXTURE", PropertiesBuilder.build(new Property("key", "value")))); - result.getAutoTables().add(createShardingAutoTableRuleConfiguration()); - return result; - } - - private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() { - ShardingAutoTableRuleConfiguration result = new ShardingAutoTableRuleConfiguration("auto_table", null); - result.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("sharding_key_required_auditor"), false)); - return result; + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", createRuleConfiguration(), new ShowUnusedShardingAuditorsStatement(null), + Collections.singleton(new LocalDataQueryResultRow("fixture", "FIXTURE", "{\"key\":\"value\"}")))); + } + + private ShardingRuleConfiguration createRuleConfiguration() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + result.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", new Properties())); + result.getAuditors().put("fixture", new AlgorithmConfiguration("FIXTURE", PropertiesBuilder.build(new Property("key", "value")))); + result.getAutoTables().add(createShardingAutoTableRuleConfiguration()); + return result; + } + + private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() { + ShardingAutoTableRuleConfiguration result = new ShardingAutoTableRuleConfiguration("auto_table", null); + result.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("sharding_key_required_auditor"), false)); + return result; + } } } diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingKeyGeneratorExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingKeyGeneratorExecutorTest.java index f563ae22c6aa1..dfcc67b4e44e7 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingKeyGeneratorExecutorTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingKeyGeneratorExecutorTest.java @@ -17,77 +17,74 @@ package org.apache.shardingsphere.sharding.distsql.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.statement.ShowUnusedShardingKeyGeneratorsStatement; import org.apache.shardingsphere.sharding.rule.ShardingRule; -import org.junit.jupiter.api.Test; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; import java.util.Collection; import java.util.Collections; import java.util.Properties; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowUnusedShardingKeyGeneratorExecutorTest { +class ShowUnusedShardingKeyGeneratorExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - @Test - void assertExecuteQuery() throws SQLException { - DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(mock(ShowUnusedShardingKeyGeneratorsStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - engine.executeQuery(); - Collection actual = engine.getRows(); - assertThat(actual.size(), is(1)); - LocalDataQueryResultRow row = actual.iterator().next(); - assertThat(row.getCell(1), is("uuid_key_generator")); - assertThat(row.getCell(2), is("UUID")); - assertThat(row.getCell(3), is("")); + ShowUnusedShardingKeyGeneratorExecutorTest() { + super(mock(ShardingRule.class)); } - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - ShardingRule rule = mock(ShardingRule.class); - when(rule.getConfiguration()).thenReturn(createRuleConfiguration()); - when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); - return result; + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); } - private ShardingRuleConfiguration createRuleConfiguration() { - ShardingRuleConfiguration result = new ShardingRuleConfiguration(); - result.getKeyGenerators().put("snowflake_key_generator", new AlgorithmConfiguration("SNOWFLAKE", new Properties())); - result.getKeyGenerators().put("uuid_key_generator", new AlgorithmConfiguration("UUID", null)); - result.getTables().add(createShardingTableRuleConfiguration()); - result.getTables().add(new ShardingTableRuleConfiguration("bar_table", null)); - result.getAutoTables().add(createShardingAutoTableRuleConfiguration()); - result.getAutoTables().add(new ShardingAutoTableRuleConfiguration("bar_auto_table", null)); - result.setDefaultKeyGenerateStrategy(new KeyGenerateStrategyConfiguration(null, "snowflake_key_generator")); - return result; - } - - private ShardingTableRuleConfiguration createShardingTableRuleConfiguration() { - ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("foo_table", null); - result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("foo_col", "snowflake_key_generator")); - return result; - } - - private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() { - ShardingAutoTableRuleConfiguration result = new ShardingAutoTableRuleConfiguration("foo_auto_table", null); - result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("foo_col", "snowflake_key_generator")); - return result; + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", createRuleConfiguration(), new ShowUnusedShardingKeyGeneratorsStatement(null), + Collections.singleton(new LocalDataQueryResultRow("uuid_key_generator", "UUID", "")))); + } + + private ShardingRuleConfiguration createRuleConfiguration() { + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + result.getKeyGenerators().put("snowflake_key_generator", new AlgorithmConfiguration("SNOWFLAKE", new Properties())); + result.getKeyGenerators().put("uuid_key_generator", new AlgorithmConfiguration("UUID", null)); + result.getTables().add(createShardingTableRuleConfiguration()); + result.getTables().add(new ShardingTableRuleConfiguration("bar_table", null)); + result.getAutoTables().add(createShardingAutoTableRuleConfiguration()); + result.getAutoTables().add(new ShardingAutoTableRuleConfiguration("bar_auto_table", null)); + result.setDefaultKeyGenerateStrategy(new KeyGenerateStrategyConfiguration(null, "snowflake_key_generator")); + return result; + } + + private ShardingTableRuleConfiguration createShardingTableRuleConfiguration() { + ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("foo_table", null); + result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("foo_col", "snowflake_key_generator")); + return result; + } + + private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() { + ShardingAutoTableRuleConfiguration result = new ShardingAutoTableRuleConfiguration("foo_auto_table", null); + result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("foo_col", "snowflake_key_generator")); + return result; + } } } diff --git a/kernel/single/distsql/handler/pom.xml b/kernel/single/distsql/handler/pom.xml index 759ab6fb35b20..9c4dd9dbb93d9 100644 --- a/kernel/single/distsql/handler/pom.xml +++ b/kernel/single/distsql/handler/pom.xml @@ -56,7 +56,7 @@ org.apache.shardingsphere - shardingsphere-test-util + shardingsphere-test-it-distsql ${project.version} test diff --git a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutorTest.java b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutorTest.java index 887bf78bf8867..4d7ab13c06365 100644 --- a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutorTest.java +++ b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutorTest.java @@ -17,46 +17,45 @@ package org.apache.shardingsphere.single.distsql.handler.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.single.config.SingleRuleConfiguration; import org.apache.shardingsphere.single.distsql.statement.rql.ShowDefaultSingleTableStorageUnitStatement; import org.apache.shardingsphere.single.rule.SingleRule; -import org.junit.jupiter.api.Test; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; -import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; -import java.util.List; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -class ShowDefaultSingleTableStorageUnitExecutorTest { +class ShowDefaultSingleTableStorageUnitExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - @Test - void assertGetRowData() throws SQLException { - DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(mock(ShowDefaultSingleTableStorageUnitStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - engine.executeQuery(); - List actual = new ArrayList<>(engine.getRows()); - assertThat(actual.size(), is(1)); - assertThat((String) actual.get(0).getCell(1), is("foo_ds")); + ShowDefaultSingleTableStorageUnitExecutorTest() { + super(mock(SingleRule.class)); } - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - SingleRule rule = mock(SingleRule.class); - when(rule.getConfiguration()).thenReturn(new SingleRuleConfiguration(Collections.emptyList(), "foo_ds")); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); - return result; + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); + } + + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", new SingleRuleConfiguration(Collections.emptyList(), "foo_ds"), new ShowDefaultSingleTableStorageUnitStatement(null), + Collections.singleton(new LocalDataQueryResultRow("foo_ds")))); + } } } diff --git a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTablesExecutorTest.java b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTablesExecutorTest.java index ddee5bb1c07ae..b1d04d5260f3f 100644 --- a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTablesExecutorTest.java +++ b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTablesExecutorTest.java @@ -17,67 +17,40 @@ package org.apache.shardingsphere.single.distsql.handler.query; -import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; -import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.datanode.DataNode; import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes; import org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute; -import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.single.config.SingleRuleConfiguration; import org.apache.shardingsphere.single.distsql.statement.rql.ShowSingleTablesStatement; import org.apache.shardingsphere.single.rule.SingleRule; -import org.junit.jupiter.api.Test; +import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLDatabaseRuleQueryExecutorTest; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; import java.sql.SQLException; -import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; +import java.util.stream.Stream; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -class ShowSingleTablesExecutorTest { +class ShowSingleTablesExecutorTest extends DistSQLDatabaseRuleQueryExecutorTest { - @Test - void assertExecuteQuery() throws SQLException { - DistSQLQueryExecuteEngine executeEngine = new DistSQLQueryExecuteEngine(mock(ShowSingleTablesStatement.class), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - executeEngine.executeQuery(); - List actual = new ArrayList<>(executeEngine.getRows()); - assertThat(actual.size(), is(2)); - assertThat(actual.get(0).getCell(1), is("t_order")); - assertThat(actual.get(0).getCell(2), is("ds_1")); - assertThat(actual.get(1).getCell(1), is("t_order_item")); - assertThat(actual.get(1).getCell(2), is("ds_2")); + ShowSingleTablesExecutorTest() { + super(mockRule()); } - @Test - void assertExecuteQueryWithLikeLiteral() throws SQLException { - DistSQLQueryExecuteEngine executeEngine = new DistSQLQueryExecuteEngine(new ShowSingleTablesStatement(null, "%item"), "foo_db", mockContextManager(), mock(DistSQLConnectionContext.class)); - executeEngine.executeQuery(); - List actual = new ArrayList<>(executeEngine.getRows()); - assertThat(actual.size(), is(1)); - assertThat(actual.get(0).getCell(1), is("t_order_item")); - assertThat(actual.get(0).getCell(2), is("ds_2")); - } - - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(result.getDatabase("foo_db")).thenReturn(database); - SingleRule rule = mockSingleRule(); - when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule))); - return result; - } - - private SingleRule mockSingleRule() { + private static SingleRule mockRule() { Map> singleTableDataNodeMap = new HashMap<>(2, 1F); singleTableDataNodeMap.put("t_order", Collections.singleton(new DataNode("ds_1", "t_order"))); singleTableDataNodeMap.put("t_order_item", Collections.singleton(new DataNode("ds_2", "t_order_item"))); @@ -87,4 +60,22 @@ private SingleRule mockSingleRule() { when(result.getAttributes()).thenReturn(new RuleAttributes(ruleAttribute)); return result; } + + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteQuery(final String name, final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, + final Collection expected) throws SQLException { + assertQueryResultRows(ruleConfig, sqlStatement, expected); + } + + private static class TestCaseArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(final ExtensionContext extensionContext) { + return Stream.of(Arguments.arguments("normal", new SingleRuleConfiguration(Collections.emptyList(), "foo_ds"), new ShowSingleTablesStatement(null, null), + Arrays.asList(new LocalDataQueryResultRow("t_order", "ds_1"), new LocalDataQueryResultRow("t_order_item", "ds_2"))), + Arguments.arguments("withLikeLiteral", new SingleRuleConfiguration(Collections.emptyList(), "foo_ds"), new ShowSingleTablesStatement(null, "%item"), + Collections.singleton(new LocalDataQueryResultRow("t_order_item", "ds_2")))); + } + } } diff --git a/test/it/distsql/src/main/java/org/apache/shardingsphere/test/it/distsql/handler/engine/query/DistSQLDatabaseRuleQueryExecutorTest.java b/test/it/distsql/src/main/java/org/apache/shardingsphere/test/it/distsql/handler/engine/query/DistSQLDatabaseRuleQueryExecutorTest.java new file mode 100644 index 0000000000000..3eb107132172d --- /dev/null +++ b/test/it/distsql/src/main/java/org/apache/shardingsphere/test/it/distsql/handler/engine/query/DistSQLDatabaseRuleQueryExecutorTest.java @@ -0,0 +1,62 @@ +/* + * 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.test.it.distsql.handler.engine.query; + +import lombok.RequiredArgsConstructor; +import org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext; +import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; +import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; +import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; +import org.apache.shardingsphere.infra.rule.scope.DatabaseRule; +import org.apache.shardingsphere.mode.manager.ContextManager; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; + +import static org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@RequiredArgsConstructor +public abstract class DistSQLDatabaseRuleQueryExecutorTest { + + private final DatabaseRule mockedRule; + + protected void assertQueryResultRows(final DatabaseRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, final Collection expected) throws SQLException { + DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(sqlStatement, "foo_db", mockContextManager(ruleConfig), mock(DistSQLConnectionContext.class)); + engine.executeQuery(); + Collection actual = new ArrayList<>(engine.getRows()); + assertThat(actual, deepEqual(new ArrayList<>(expected))); + } + + private ContextManager mockContextManager(final DatabaseRuleConfiguration ruleConfig) { + ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); + when(mockedRule.getConfiguration()).thenReturn(ruleConfig); + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(mockedRule))); + when(result.getDatabase("foo_db")).thenReturn(database); + return result; + } +}