Skip to content

Commit

Permalink
Add GlobalRuleDefinitionExecutorTest (#33135)
Browse files Browse the repository at this point in the history
* Add GlobalRuleDefinitionExecutorTest

* Add GlobalRuleDefinitionExecutorTest
  • Loading branch information
terrymanu authored Oct 6, 2024
1 parent 9b3a8cc commit ddbe920
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends Arguments> 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())));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.<SQLFederationRuleConfiguration>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.<SQLFederationRuleConfiguration>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<? extends Exception> 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.<SQLFederationRuleConfiguration>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<? extends Arguments> 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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.<SQLParserRuleConfiguration>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.<SQLParserRuleConfiguration>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.<SQLParserRuleConfiguration>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<? extends Arguments> 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))));
}
}
}
Loading

0 comments on commit ddbe920

Please sign in to comment.