Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DistSQLDatabaseRuleQueryExecutorTest #33131

Merged
merged 25 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d44de15
Add DistSQLDatabaseRuleQueryExecutorTest
terrymanu Oct 5, 2024
d0f6584
Add DistSQLDatabaseRuleQueryExecutorTest
terrymanu Oct 5, 2024
bb8c59d
Add ShowShardingTableRuleExecutorTest
terrymanu Oct 5, 2024
e1941bd
Add ShowEncryptRuleExecutorTest
terrymanu Oct 5, 2024
0c726c9
Add ShowShardingTableRulesUsedAuditorExecutorTest
terrymanu Oct 5, 2024
88ae999
Add ShowDefaultShardingStrategyExecutorTest
terrymanu Oct 5, 2024
8e89503
Add ShowDefaultShardingStrategyExecutorTest
terrymanu Oct 5, 2024
1b012c1
Add ShowUnusedShardingAuditorsExecutorTest
terrymanu Oct 5, 2024
b9b6251
Add ShowShardingTableRulesUsedAlgorithmExecutorTest
terrymanu Oct 5, 2024
03ac9e1
Add ShowUnusedShardingAlgorithmsExecutorTest
terrymanu Oct 5, 2024
3925468
Add ShowShardingKeyGeneratorExecutorTest
terrymanu Oct 5, 2024
c1a6108
Add ShowMaskRuleExecutorTest
terrymanu Oct 5, 2024
e2acdea
Add ShowDefaultSingleTableStorageUnitExecutorTest
terrymanu Oct 5, 2024
4578ae5
Add ShowSingleTablesExecutorTest
terrymanu Oct 5, 2024
0e89da9
Add ShowReadwriteSplittingRuleExecutorTest
terrymanu Oct 5, 2024
e55ba2b
Add ShowBroadcastTableRuleExecutorTest
terrymanu Oct 5, 2024
982679d
Add ShowStatusFromReadwriteSplittingRulesExecutorTest
terrymanu Oct 5, 2024
a18c704
Add ShowShadowTableRulesExecutorTest
terrymanu Oct 5, 2024
13d0535
Add ShowShadowRuleExecutorTest
terrymanu Oct 5, 2024
7a429f1
Add ShowDefaultShadowAlgorithmExecutorTest
terrymanu Oct 5, 2024
3397f51
Add ShowShardingAuditorsExecutorTest
terrymanu Oct 5, 2024
93fa17e
Add ShowShardingAlgorithmExecutorTest
terrymanu Oct 5, 2024
7a5068d
Add ShowShardingTableReferenceRuleExecutorTest
terrymanu Oct 5, 2024
ba66f93
Add DistSQLDatabaseRuleQueryExecutorTest
terrymanu Oct 5, 2024
b1135a6
Add DistSQLDatabaseRuleQueryExecutorTest
terrymanu Oct 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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