Skip to content

Commit

Permalink
Add DistSQLDatabaseRuleQueryExecutorTest (#33131)
Browse files Browse the repository at this point in the history
* Add DistSQLDatabaseRuleQueryExecutorTest

* Add DistSQLDatabaseRuleQueryExecutorTest

* Add ShowShardingTableRuleExecutorTest

* Add ShowEncryptRuleExecutorTest

* Add ShowShardingTableRulesUsedAuditorExecutorTest

* Add ShowDefaultShardingStrategyExecutorTest

* Add ShowDefaultShardingStrategyExecutorTest

* Add ShowUnusedShardingAuditorsExecutorTest

* Add ShowShardingTableRulesUsedAlgorithmExecutorTest

* Add ShowUnusedShardingAlgorithmsExecutorTest

* Add ShowShardingKeyGeneratorExecutorTest

* Add ShowMaskRuleExecutorTest

* Add ShowDefaultSingleTableStorageUnitExecutorTest

* Add ShowSingleTablesExecutorTest

* Add ShowReadwriteSplittingRuleExecutorTest

* Add ShowBroadcastTableRuleExecutorTest

* Add ShowStatusFromReadwriteSplittingRulesExecutorTest

* Add ShowShadowTableRulesExecutorTest

* Add ShowShadowRuleExecutorTest

* Add ShowDefaultShadowAlgorithmExecutorTest

* Add ShowShardingAuditorsExecutorTest

* Add ShowShardingAlgorithmExecutorTest

* Add ShowShardingTableReferenceRuleExecutorTest

* Add DistSQLDatabaseRuleQueryExecutorTest

* Add DistSQLDatabaseRuleQueryExecutorTest
  • Loading branch information
terrymanu authored Oct 5, 2024
1 parent 0c5f63a commit 382d5f5
Show file tree
Hide file tree
Showing 31 changed files with 937 additions and 1,207 deletions.
2 changes: 1 addition & 1 deletion features/broadcast/distsql/handler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-test-util</artifactId>
<artifactId>shardingsphere-test-it-distsql</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LocalDataQueryResultRow> 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<LocalDataQueryResultRow> 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<? extends Arguments> 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"))));
}
}
}
2 changes: 1 addition & 1 deletion features/encrypt/distsql/handler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-test-util</artifactId>
<artifactId>shardingsphere-test-it-distsql</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,59 @@

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;
import org.apache.shardingsphere.encrypt.config.rule.EncryptTableRuleConfiguration;
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<LocalDataQueryResultRow> 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<LocalDataQueryResultRow> 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<? extends Arguments> 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));
}
}
}
2 changes: 1 addition & 1 deletion features/mask/distsql/handler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-test-util</artifactId>
<artifactId>shardingsphere-test-it-distsql</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<LocalDataQueryResultRow> expected) throws SQLException {
assertQueryResultRows(ruleConfig, sqlStatement, expected);
}

@Test
void assertGetRowData() throws SQLException {
engine.executeQuery();
Collection<LocalDataQueryResultRow> actual = engine.getRows();
assertThat(actual.size(), is(1));
Iterator<LocalDataQueryResultRow> 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<? extends Arguments> 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));
}
}
}
2 changes: 1 addition & 1 deletion features/readwrite-splitting/distsql/handler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-test-util</artifactId>
<artifactId>shardingsphere-test-it-distsql</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Loading

0 comments on commit 382d5f5

Please sign in to comment.