Skip to content

Commit

Permalink
Refactor ShowAuthorityRuleExecutor and test case (#30082)
Browse files Browse the repository at this point in the history
* Refactor ShowAuthorityRuleExecutor and test case

* Refactor ShowAuthorityRuleExecutor and test case

* Refactor ShowAuthorityRuleExecutor and test case

* Refactor ShowAuthorityRuleExecutor and test case
  • Loading branch information
terrymanu authored Feb 9, 2024
1 parent 2895813 commit 9515414
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.shardingsphere.authority.distsql.handler.query;

import lombok.Setter;
import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
import org.apache.shardingsphere.authority.distsql.statement.ShowAuthorityRuleStatement;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorRuleAware;
Expand All @@ -29,6 +28,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;
import java.util.stream.Collectors;

/**
Expand All @@ -46,9 +46,10 @@ public Collection<String> getColumnNames() {

@Override
public Collection<LocalDataQueryResultRow> getRows(final ShowAuthorityRuleStatement sqlStatement, final ContextManager contextManager) {
AuthorityRuleConfiguration ruleConfig = rule.getConfiguration();
return Collections.singleton(new LocalDataQueryResultRow(ruleConfig.getUsers().stream().map(each -> each.getGrantee().toString()).collect(Collectors.joining("; ")),
ruleConfig.getPrivilegeProvider().getType(), ruleConfig.getPrivilegeProvider().getProps().isEmpty() ? "" : ruleConfig.getPrivilegeProvider().getProps()));
String users = rule.getConfiguration().getUsers().stream().map(each -> each.getGrantee().toString()).collect(Collectors.joining("; "));
String provider = rule.getConfiguration().getPrivilegeProvider().getType();
Properties props = rule.getConfiguration().getPrivilegeProvider().getProps().isEmpty() ? new Properties() : rule.getConfiguration().getPrivilegeProvider().getProps();
return Collections.singleton(new LocalDataQueryResultRow(users, provider, props));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,58 @@
import org.apache.shardingsphere.authority.config.AuthorityRuleConfiguration;
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.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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 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 {

private DistSQLQueryExecuteEngine engine;

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

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())));
return result;
}

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

@Test
void assertExecute() {
AuthorityRule rule = mock(AuthorityRule.class);
when(rule.getConfiguration()).thenReturn(createAuthorityRuleConfiguration());
ShowAuthorityRuleExecutor executor = new ShowAuthorityRuleExecutor();
executor.setRule(rule);
Collection<LocalDataQueryResultRow> actual = executor.getRows(mock(ShowAuthorityRuleStatement.class), mock(ContextManager.class));
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 AuthorityRuleConfiguration createAuthorityRuleConfiguration() {
ShardingSphereUser root = new ShardingSphereUser("root", "", "localhost");
return new AuthorityRuleConfiguration(Collections.singleton(root), new AlgorithmConfiguration("ALL_PERMITTED", new Properties()), Collections.emptyMap(), null);
assertThat(row.getCell(3), is(new Properties()));
}
}

0 comments on commit 9515414

Please sign in to comment.