diff --git a/kernel/global-clock/distsql/handler/src/test/java/org/apache/shardingsphere/globalclock/distsql/handler/update/AlterGlobalClockRuleExecutorTest.java b/kernel/global-clock/distsql/handler/src/test/java/org/apache/shardingsphere/globalclock/distsql/handler/update/AlterGlobalClockRuleExecutorTest.java index bdbb30bc04124..6a1251a4acff7 100644 --- a/kernel/global-clock/distsql/handler/src/test/java/org/apache/shardingsphere/globalclock/distsql/handler/update/AlterGlobalClockRuleExecutorTest.java +++ b/kernel/global-clock/distsql/handler/src/test/java/org/apache/shardingsphere/globalclock/distsql/handler/update/AlterGlobalClockRuleExecutorTest.java @@ -17,50 +17,45 @@ package org.apache.shardingsphere.globalclock.distsql.handler.update; -import org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; import org.apache.shardingsphere.globalclock.config.GlobalClockRuleConfiguration; import org.apache.shardingsphere.globalclock.distsql.statement.updatable.AlterGlobalClockRuleStatement; import org.apache.shardingsphere.globalclock.rule.GlobalClockRule; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentMatchers; +import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration; +import org.apache.shardingsphere.test.it.distsql.handler.engine.update.GlobalRuleDefinitionExecutorTest; +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.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.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -class AlterGlobalClockRuleExecutorTest { +class AlterGlobalClockRuleExecutorTest extends GlobalRuleDefinitionExecutorTest { - @Test - void assertExecute() throws SQLException { - ContextManager contextManager = mockContextManager(); - new DistSQLUpdateExecuteEngine(new AlterGlobalClockRuleStatement("TSO", "redis", true, new Properties()), null, contextManager).executeUpdate(); - MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getMetaDataManagerPersistService(); - verify(metaDataManagerPersistService).alterGlobalRuleConfiguration(ArgumentMatchers.argThat(this::assertRuleConfiguration)); + AlterGlobalClockRuleExecutorTest() { + super(mock(GlobalClockRule.class)); } - private boolean assertRuleConfiguration(final GlobalClockRuleConfiguration actual) { - assertThat(actual.getType(), is("TSO")); - assertThat(actual.getProvider(), is("redis")); - assertTrue(actual.isEnabled()); - return true; + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteUpdate(final String name, final GlobalRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, final RuleConfiguration matchedRuleConfig) throws SQLException { + assertExecuteUpdate(ruleConfig, sqlStatement, matchedRuleConfig, null); } - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - GlobalClockRule rule = mock(GlobalClockRule.class); - when(rule.getConfiguration()).thenReturn(new GlobalClockRuleConfiguration("TSO", "local", false, new Properties())); - when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).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("normal", + new GlobalClockRuleConfiguration("TSO", "local", false, new Properties()), + new AlterGlobalClockRuleStatement("TSO", "redis", true, new Properties()), + new GlobalClockRuleConfiguration("TSO", "redis", true, new Properties()))); + } } } diff --git a/kernel/sql-federation/distsql/handler/src/test/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutorTest.java b/kernel/sql-federation/distsql/handler/src/test/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutorTest.java index da35a8d0e1614..a768beb84b7fc 100644 --- a/kernel/sql-federation/distsql/handler/src/test/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutorTest.java +++ b/kernel/sql-federation/distsql/handler/src/test/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutorTest.java @@ -17,75 +17,57 @@ package org.apache.shardingsphere.sqlfederation.distsql.handler.update; -import org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExecuteEngine; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; +import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration; import org.apache.shardingsphere.sql.parser.api.CacheOption; import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration; import org.apache.shardingsphere.sqlfederation.distsql.segment.CacheOptionSegment; import org.apache.shardingsphere.sqlfederation.distsql.statement.updatable.AlterSQLFederationRuleStatement; import org.apache.shardingsphere.sqlfederation.rule.SQLFederationRule; import org.apache.shardingsphere.sqlfederation.rule.builder.DefaultSQLFederationRuleConfigurationBuilder; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentMatchers; +import org.apache.shardingsphere.test.it.distsql.handler.engine.update.GlobalRuleDefinitionExecutorTest; +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.Collections; +import java.util.stream.Stream; -import static org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual; -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.verify; -import static org.mockito.Mockito.when; -class AlterSQLFederationRuleExecutorTest { +class AlterSQLFederationRuleExecutorTest extends GlobalRuleDefinitionExecutorTest { - @Test - void assertExecuteUpdate() throws SQLException { - AlterSQLFederationRuleStatement sqlStatement = new AlterSQLFederationRuleStatement(true, true, new CacheOptionSegment(64, 512L)); - ContextManager contextManager = mockContextManager(); - new DistSQLUpdateExecuteEngine(sqlStatement, null, contextManager).executeUpdate(); - MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getMetaDataManagerPersistService(); - verify(metaDataManagerPersistService).alterGlobalRuleConfiguration( - ArgumentMatchers.argThat(x -> assertRuleConfiguration(x, true, true, new CacheOption(64, 512L)))); + AlterSQLFederationRuleExecutorTest() { + super(mock(SQLFederationRule.class)); } - @Test - void assertExecuteUpdateWithNullStatement() throws SQLException { - AlterSQLFederationRuleStatement sqlStatement = new AlterSQLFederationRuleStatement(null, null, null); - ContextManager contextManager = mockContextManager(); - new DistSQLUpdateExecuteEngine(sqlStatement, null, contextManager).executeUpdate(); - MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getMetaDataManagerPersistService(); - verify(metaDataManagerPersistService).alterGlobalRuleConfiguration( - ArgumentMatchers.argThat(x -> assertRuleConfiguration(x, false, false, new CacheOption(2000, 65535L)))); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteUpdate(final String name, final GlobalRuleConfiguration ruleConfig, + final DistSQLStatement sqlStatement, final RuleConfiguration matchedRuleConfig, final Class expectedException) throws SQLException { + assertExecuteUpdate(ruleConfig, sqlStatement, matchedRuleConfig, expectedException); } - @Test - void assertExecuteUpdateWithNullCacheOptionSegment() throws SQLException { - AlterSQLFederationRuleStatement sqlStatement = new AlterSQLFederationRuleStatement(null, null, new CacheOptionSegment(null, null)); - ContextManager contextManager = mockContextManager(); - new DistSQLUpdateExecuteEngine(sqlStatement, null, contextManager).executeUpdate(); - MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getMetaDataManagerPersistService(); - verify(metaDataManagerPersistService).alterGlobalRuleConfiguration( - ArgumentMatchers.argThat(x -> assertRuleConfiguration(x, false, false, new CacheOption(2000, 65535L)))); - } - - private boolean assertRuleConfiguration(final SQLFederationRuleConfiguration actual, - final boolean expectedSQLFederationEnabled, final boolean expectedAllQueryUseSQLFederation, final CacheOption expectedExecutionPlanCache) { - assertThat(actual.isSqlFederationEnabled(), is(expectedSQLFederationEnabled)); - assertThat(actual.isAllQueryUseSQLFederation(), is(expectedAllQueryUseSQLFederation)); - assertThat(actual.getExecutionPlanCache(), deepEqual(expectedExecutionPlanCache)); - return true; - } - - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - SQLFederationRule rule = mock(SQLFederationRule.class); - when(rule.getConfiguration()).thenReturn(new DefaultSQLFederationRuleConfigurationBuilder().build()); - when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).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("normal", + new DefaultSQLFederationRuleConfigurationBuilder().build(), + new AlterSQLFederationRuleStatement(true, true, new CacheOptionSegment(64, 512L)), + new SQLFederationRuleConfiguration(true, true, new CacheOption(64, 512L)), null), + Arguments.arguments("withNotExistedDistributedTransactionType", + new DefaultSQLFederationRuleConfigurationBuilder().build(), + new AlterSQLFederationRuleStatement(null, null, null), + new SQLFederationRuleConfiguration(false, false, new CacheOption(2000, 65535L)), null), + Arguments.arguments("withNotExistedXATransactionProvider", + new DefaultSQLFederationRuleConfigurationBuilder().build(), + new AlterSQLFederationRuleStatement(null, null, new CacheOptionSegment(null, null)), + new SQLFederationRuleConfiguration(false, false, new CacheOption(2000, 65535L)), null)); + } } } diff --git a/kernel/sql-parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleExecutorTest.java b/kernel/sql-parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleExecutorTest.java index 4d63fc54d7f0a..eea3d8a3850f0 100644 --- a/kernel/sql-parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleExecutorTest.java +++ b/kernel/sql-parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleExecutorTest.java @@ -17,72 +17,55 @@ package org.apache.shardingsphere.parser.distsql.handler.update; -import org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExecuteEngine; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; +import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration; import org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration; import org.apache.shardingsphere.parser.distsql.segment.CacheOptionSegment; import org.apache.shardingsphere.parser.distsql.statement.updatable.AlterSQLParserRuleStatement; import org.apache.shardingsphere.parser.rule.SQLParserRule; import org.apache.shardingsphere.parser.rule.builder.DefaultSQLParserRuleConfigurationBuilder; import org.apache.shardingsphere.sql.parser.api.CacheOption; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentMatchers; +import org.apache.shardingsphere.test.it.distsql.handler.engine.update.GlobalRuleDefinitionExecutorTest; +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.Collections; +import java.util.stream.Stream; -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.verify; -import static org.mockito.Mockito.when; -class AlterSQLParserRuleExecutorTest { +class AlterSQLParserRuleExecutorTest extends GlobalRuleDefinitionExecutorTest { - @Test - void assertExecute() throws SQLException { - AlterSQLParserRuleStatement sqlStatement = new AlterSQLParserRuleStatement(new CacheOptionSegment(64, 512L), new CacheOptionSegment(1000, 1000L)); - ContextManager contextManager = mockContextManager(); - new DistSQLUpdateExecuteEngine(sqlStatement, null, contextManager).executeUpdate(); - MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getMetaDataManagerPersistService(); - verify(metaDataManagerPersistService).alterGlobalRuleConfiguration( - ArgumentMatchers.argThat(x -> assertRuleConfiguration(x, new CacheOption(64, 512L), new CacheOption(1000, 1000L)))); + AlterSQLParserRuleExecutorTest() { + super(mock(SQLParserRule.class)); } - @Test - void assertExecuteWithNullStatement() throws SQLException { - AlterSQLParserRuleStatement sqlStatement = new AlterSQLParserRuleStatement(null, null); - ContextManager contextManager = mockContextManager(); - new DistSQLUpdateExecuteEngine(sqlStatement, null, contextManager).executeUpdate(); - MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getMetaDataManagerPersistService(); - verify(metaDataManagerPersistService).alterGlobalRuleConfiguration( - ArgumentMatchers.argThat(x -> assertRuleConfiguration(x, new CacheOption(128, 1024L), new CacheOption(2000, 65535L)))); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteUpdate(final String name, final GlobalRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, final RuleConfiguration matchedRuleConfig) throws SQLException { + assertExecuteUpdate(ruleConfig, sqlStatement, matchedRuleConfig, null); } - @Test - void assertExecuteWithNullCacheOptionSegment() throws SQLException { - AlterSQLParserRuleStatement sqlStatement = new AlterSQLParserRuleStatement(new CacheOptionSegment(null, null), new CacheOptionSegment(null, null)); - ContextManager contextManager = mockContextManager(); - new DistSQLUpdateExecuteEngine(sqlStatement, null, contextManager).executeUpdate(); - MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getMetaDataManagerPersistService(); - verify(metaDataManagerPersistService).alterGlobalRuleConfiguration( - ArgumentMatchers.argThat(x -> assertRuleConfiguration(x, new CacheOption(128, 1024L), new CacheOption(2000, 65535L)))); - } - - private boolean assertRuleConfiguration(final SQLParserRuleConfiguration actual, final CacheOption expectedParseTreeCache, final CacheOption expectedSQLStatementCache) { - assertThat(actual.getParseTreeCache(), deepEqual(expectedParseTreeCache)); - assertThat(actual.getSqlStatementCache(), deepEqual(expectedSQLStatementCache)); - return true; - } - - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - SQLParserRule rule = mock(SQLParserRule.class); - when(rule.getConfiguration()).thenReturn(new DefaultSQLParserRuleConfigurationBuilder().build()); - when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).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("normal", + new DefaultSQLParserRuleConfigurationBuilder().build(), + new AlterSQLParserRuleStatement(new CacheOptionSegment(64, 512L), new CacheOptionSegment(1000, 1000L)), + new SQLParserRuleConfiguration(new CacheOption(64, 512L), new CacheOption(1000, 1000L))), + Arguments.arguments("withNullStatement", + new DefaultSQLParserRuleConfigurationBuilder().build(), + new AlterSQLParserRuleStatement(null, null), + new SQLParserRuleConfiguration(new CacheOption(128, 1024L), new CacheOption(2000, 65535L))), + Arguments.arguments("wthNullCacheOptionSegment", + new DefaultSQLParserRuleConfigurationBuilder().build(), + new AlterSQLParserRuleStatement(new CacheOptionSegment(null, null), new CacheOptionSegment(null, null)), + new SQLParserRuleConfiguration(new CacheOption(128, 1024L), new CacheOption(2000, 65535L)))); + } } } diff --git a/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/update/AlterSQLTranslatorRuleExecutorTest.java b/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/update/AlterSQLTranslatorRuleExecutorTest.java index 2992fbe4beea1..c1c0c8657967f 100644 --- a/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/update/AlterSQLTranslatorRuleExecutorTest.java +++ b/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/update/AlterSQLTranslatorRuleExecutorTest.java @@ -17,65 +17,52 @@ package org.apache.shardingsphere.sqltranslator.distsql.handler.update; -import org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExecuteEngine; import org.apache.shardingsphere.distsql.segment.AlgorithmSegment; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; +import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration; import org.apache.shardingsphere.sqltranslator.config.SQLTranslatorRuleConfiguration; import org.apache.shardingsphere.sqltranslator.distsql.statement.updateable.AlterSQLTranslatorRuleStatement; import org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule; +import org.apache.shardingsphere.test.it.distsql.handler.engine.update.GlobalRuleDefinitionExecutorTest; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentMatchers; +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.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.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -class AlterSQLTranslatorRuleExecutorTest { +class AlterSQLTranslatorRuleExecutorTest extends GlobalRuleDefinitionExecutorTest { - @Test - void assertExecuteWithTrueOriginalSQLWhenTranslatingFailed() throws SQLException { - AlterSQLTranslatorRuleStatement sqlStatement = new AlterSQLTranslatorRuleStatement(new AlgorithmSegment("Native", PropertiesBuilder.build(new Property("foo", "bar"))), true); - ContextManager contextManager = mockContextManager(); - DistSQLUpdateExecuteEngine engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, contextManager); - engine.executeUpdate(); - MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getMetaDataManagerPersistService(); - verify(metaDataManagerPersistService).alterGlobalRuleConfiguration(ArgumentMatchers.argThat(this::assertRuleConfiguration)); + AlterSQLTranslatorRuleExecutorTest() { + super(mock(SQLTranslatorRule.class)); } - @Test - void assertExecuteWithNullOriginalSQLWhenTranslatingFailed() throws SQLException { - AlterSQLTranslatorRuleStatement sqlStatement = new AlterSQLTranslatorRuleStatement(new AlgorithmSegment("Native", PropertiesBuilder.build(new Property("foo", "bar"))), null); - ContextManager contextManager = mockContextManager(); - DistSQLUpdateExecuteEngine engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, contextManager); - engine.executeUpdate(); - MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getMetaDataManagerPersistService(); - verify(metaDataManagerPersistService).alterGlobalRuleConfiguration(ArgumentMatchers.argThat(this::assertRuleConfiguration)); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteUpdate(final String name, final GlobalRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, final RuleConfiguration matchedRuleConfig) throws SQLException { + assertExecuteUpdate(ruleConfig, sqlStatement, matchedRuleConfig, null); } - private boolean assertRuleConfiguration(final SQLTranslatorRuleConfiguration actual) { - assertThat(actual.getProps().size(), is(1)); - assertThat(actual.getProps().getProperty("foo"), is("bar")); - assertTrue(actual.isUseOriginalSQLWhenTranslatingFailed()); - return true; - } - - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - SQLTranslatorRule rule = mock(SQLTranslatorRule.class); - when(rule.getConfiguration()).thenReturn(new SQLTranslatorRuleConfiguration("NATIVE", new Properties(), true)); - when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).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("withTrueOriginalSQLWhenTranslatingFailed", + new SQLTranslatorRuleConfiguration("NATIVE", new Properties(), true), + new AlterSQLTranslatorRuleStatement(new AlgorithmSegment("NATIVE", PropertiesBuilder.build(new Property("foo", "bar"))), true), + new SQLTranslatorRuleConfiguration("NATIVE", PropertiesBuilder.build(new Property("foo", "bar")), true)), + Arguments.arguments("withNullOriginalSQLWhenTranslatingFailed", + new SQLTranslatorRuleConfiguration("NATIVE", new Properties(), true), + new AlterSQLTranslatorRuleStatement(new AlgorithmSegment("NATIVE", PropertiesBuilder.build(new Property("foo", "bar"))), null), + new SQLTranslatorRuleConfiguration("NATIVE", PropertiesBuilder.build(new Property("foo", "bar")), true))); + } } } diff --git a/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleExecutorTest.java b/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleExecutorTest.java index 877cd07fb2c84..8d4623833b6e4 100644 --- a/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleExecutorTest.java +++ b/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleExecutorTest.java @@ -17,74 +17,63 @@ package org.apache.shardingsphere.transaction.distsql.handler.update; -import org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; +import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration; import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.InvalidRuleConfigurationException; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService; +import org.apache.shardingsphere.test.it.distsql.handler.engine.update.GlobalRuleDefinitionExecutorTest; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; import org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration; import org.apache.shardingsphere.transaction.distsql.segment.TransactionProviderSegment; import org.apache.shardingsphere.transaction.distsql.statement.updatable.AlterTransactionRuleStatement; import org.apache.shardingsphere.transaction.rule.TransactionRule; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentMatchers; +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.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.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -class AlterTransactionRuleExecutorTest { +class AlterTransactionRuleExecutorTest extends GlobalRuleDefinitionExecutorTest { - @Test - void assertExecuteUpdateWithInvalidTransactionType() { - AlterTransactionRuleStatement sqlStatement = new AlterTransactionRuleStatement("Invalid", new TransactionProviderSegment("", new Properties())); - assertThrows(InvalidRuleConfigurationException.class, () -> new DistSQLUpdateExecuteEngine(sqlStatement, null, mockContextManager()).executeUpdate()); + AlterTransactionRuleExecutorTest() { + super(mock(TransactionRule.class)); } - @Test - void assertExecuteUpdateWithNotExistedDistributedTransactionType() { - AlterTransactionRuleStatement sqlStatement = new AlterTransactionRuleStatement("BASE", new TransactionProviderSegment("", new Properties())); - assertThrows(InvalidRuleConfigurationException.class, () -> new DistSQLUpdateExecuteEngine(sqlStatement, null, mockContextManager()).executeUpdate()); + @ParameterizedTest(name = "{0}") + @ArgumentsSource(TestCaseArgumentsProvider.class) + void assertExecuteUpdate(final String name, final GlobalRuleConfiguration ruleConfig, + final DistSQLStatement sqlStatement, final RuleConfiguration matchedRuleConfig, final Class expectedException) throws SQLException { + assertExecuteUpdate(ruleConfig, sqlStatement, matchedRuleConfig, expectedException); } - @Test - void assertExecuteUpdateWithNotExistedXATransactionProvider() { - AlterTransactionRuleStatement sqlStatement = new AlterTransactionRuleStatement("XA", new TransactionProviderSegment("Invalid", new Properties())); - assertThrows(InvalidRuleConfigurationException.class, () -> new DistSQLUpdateExecuteEngine(sqlStatement, null, mockContextManager()).executeUpdate()); - } - - @Test - void assertExecuteUpdate() throws SQLException { - AlterTransactionRuleStatement sqlStatement = new AlterTransactionRuleStatement( - "XA", new TransactionProviderSegment("Atomikos", PropertiesBuilder.build(new Property("host", "127.0.0.1"), new Property("databaseName", "jbossts")))); - ContextManager contextManager = mockContextManager(); - DistSQLUpdateExecuteEngine engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, contextManager); - engine.executeUpdate(); - MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getMetaDataManagerPersistService(); - verify(metaDataManagerPersistService).alterGlobalRuleConfiguration(ArgumentMatchers.argThat(this::assertRuleConfiguration)); - } - - private boolean assertRuleConfiguration(final TransactionRuleConfiguration actual) { - assertThat(actual.getProps().size(), is(2)); - assertThat(actual.getProps().getProperty("host"), is("127.0.0.1")); - assertThat(actual.getProps().getProperty("databaseName"), is("jbossts")); - return true; - } - - private ContextManager mockContextManager() { - ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - TransactionRule rule = mock(TransactionRule.class); - when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).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("withInvalidTransactionType", + new TransactionRuleConfiguration("", "", new Properties()), + new AlterTransactionRuleStatement("Invalid", new TransactionProviderSegment("", new Properties())), + null, InvalidRuleConfigurationException.class), + Arguments.arguments("withNotExistedDistributedTransactionType", + new TransactionRuleConfiguration("", "", new Properties()), + new AlterTransactionRuleStatement("BASE", new TransactionProviderSegment("", new Properties())), + null, InvalidRuleConfigurationException.class), + Arguments.arguments("withNotExistedXATransactionProvider", + new TransactionRuleConfiguration("", "", new Properties()), + new AlterTransactionRuleStatement("XA", new TransactionProviderSegment("Invalid", new Properties())), + null, InvalidRuleConfigurationException.class), + Arguments.arguments("normal", + new TransactionRuleConfiguration("XA", "Atomikos", new Properties()), + new AlterTransactionRuleStatement("XA", new TransactionProviderSegment("Atomikos", PropertiesBuilder.build(new Property("k", "v")))), + new TransactionRuleConfiguration("XA", "Atomikos", PropertiesBuilder.build(new Property("k", "v"))), null)); + } } } diff --git a/test/it/distsql/src/main/java/org/apache/shardingsphere/test/it/distsql/handler/engine/update/GlobalRuleDefinitionExecutorTest.java b/test/it/distsql/src/main/java/org/apache/shardingsphere/test/it/distsql/handler/engine/update/GlobalRuleDefinitionExecutorTest.java new file mode 100644 index 0000000000000..2e3bdf4872089 --- /dev/null +++ b/test/it/distsql/src/main/java/org/apache/shardingsphere/test/it/distsql/handler/engine/update/GlobalRuleDefinitionExecutorTest.java @@ -0,0 +1,65 @@ +/* + * 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.update; + +import lombok.RequiredArgsConstructor; +import org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExecuteEngine; +import org.apache.shardingsphere.distsql.statement.DistSQLStatement; +import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; +import org.apache.shardingsphere.infra.config.rule.scope.GlobalRuleConfiguration; +import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.InvalidRuleConfigurationException; +import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; +import org.apache.shardingsphere.infra.rule.scope.GlobalRule; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService; + +import java.sql.SQLException; +import java.util.Collections; + +import static org.apache.shardingsphere.test.matcher.ShardingSphereArgumentVerifyMatchers.deepEq; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RequiredArgsConstructor +public abstract class GlobalRuleDefinitionExecutorTest { + + private final GlobalRule mockedRule; + + protected void assertExecuteUpdate(final GlobalRuleConfiguration ruleConfig, + final DistSQLStatement sqlStatement, final RuleConfiguration matchedRuleConfig, final Class expectedException) throws SQLException { + ContextManager contextManager = mockContextManager(ruleConfig); + DistSQLUpdateExecuteEngine engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, contextManager); + if (null != expectedException) { + assertThrows(InvalidRuleConfigurationException.class, engine::executeUpdate); + return; + } + engine.executeUpdate(); + MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getMetaDataManagerPersistService(); + verify(metaDataManagerPersistService).alterGlobalRuleConfiguration(deepEq(matchedRuleConfig)); + } + + private ContextManager mockContextManager(final GlobalRuleConfiguration ruleConfig) { + ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); + when(mockedRule.getConfiguration()).thenReturn(ruleConfig); + when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(mockedRule))); + return result; + } +}