Skip to content

Commit

Permalink
Refactor ShowGlobalClockRuleExecutorTest
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Oct 5, 2024
1 parent 3627929 commit 0ccf918
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
7 changes: 7 additions & 0 deletions kernel/authority/distsql/handler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,12 @@
<artifactId>shardingsphere-authority-distsql-parser</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-test-it-distsql</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Collection<String> getColumnNames(final ShowAuthorityRuleStatement sqlSta
public Collection<LocalDataQueryResultRow> getRows(final ShowAuthorityRuleStatement sqlStatement, final ContextManager contextManager) {
String grantees = rule.getGrantees().stream().map(Grantee::toString).collect(Collectors.joining("; "));
String provider = rule.getConfiguration().getPrivilegeProvider().getType();
Properties props = rule.getConfiguration().getPrivilegeProvider().getProps().isEmpty() ? new Properties() : rule.getConfiguration().getPrivilegeProvider().getProps();
Properties props = rule.getConfiguration().getPrivilegeProvider().getProps();
return Collections.singleton(new LocalDataQueryResultRow(grantees, provider, props));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,57 +21,58 @@
import org.apache.shardingsphere.authority.config.UserConfiguration;
import org.apache.shardingsphere.authority.distsql.statement.ShowAuthorityRuleStatement;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
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.GlobalRuleConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.test.it.distsql.handler.engine.query.DistSQLGlobalRuleQueryExecutorTest;
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 ShowAuthorityRuleExecutorTest {
class ShowAuthorityRuleExecutorTest extends DistSQLGlobalRuleQueryExecutorTest {

private DistSQLQueryExecuteEngine engine;

@BeforeEach
void setUp() {
engine = new DistSQLQueryExecuteEngine(new ShowAuthorityRuleStatement(), null, mockContextManager(), mock(DistSQLConnectionContext.class));
ShowAuthorityRuleExecutorTest() {
super(mockRule());
}

private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(AuthorityRule.class)).thenReturn(Optional.of(new AuthorityRule(createRuleConfiguration())));
private static AuthorityRule mockRule() {
AuthorityRule result = mock(AuthorityRule.class);
when(result.getGrantees()).thenReturn(Collections.singleton(new Grantee("root", "localhost")));
return result;
}

private AuthorityRuleConfiguration createRuleConfiguration() {
UserConfiguration userConfig = new UserConfiguration("root", "", "localhost", null, false);
AlgorithmConfiguration privilegeProvider = new AlgorithmConfiguration("ALL_PERMITTED", new Properties());
return new AuthorityRuleConfiguration(Collections.singleton(userConfig), privilegeProvider, Collections.emptyMap(), null);
@ParameterizedTest(name = "{0}")
@ArgumentsSource(TestCaseArgumentsProvider.class)
void assertExecuteQuery(final String name, final GlobalRuleConfiguration ruleConfig, final DistSQLStatement sqlStatement, final Collection<LocalDataQueryResultRow> expected) throws SQLException {
assertQueryResultRows(ruleConfig, sqlStatement, expected);
}

@Test
void assertGetRows() 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("root@localhost"));
assertThat(row.getCell(2), is("ALL_PERMITTED"));
assertThat(row.getCell(3), is(""));
private static class TestCaseArgumentsProvider implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(final ExtensionContext extensionContext) {
return Stream.of(
Arguments.arguments("normal", createRuleConfiguration(), new ShowAuthorityRuleStatement(),
Collections.singleton(new LocalDataQueryResultRow("root@localhost", "ALL_PERMITTED", ""))));
}

private AuthorityRuleConfiguration createRuleConfiguration() {
UserConfiguration userConfig = new UserConfiguration("root", "", "localhost", null, false);
AlgorithmConfiguration privilegeProvider = new AlgorithmConfiguration("ALL_PERMITTED", new Properties());
return new AuthorityRuleConfiguration(Collections.singleton(userConfig), privilegeProvider, Collections.emptyMap(), null);
}
}
}

0 comments on commit 0ccf918

Please sign in to comment.