You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2024/02/09 14:27:56 UTC

(shardingsphere) branch master updated: Refactor ShowAuthorityRuleExecutor and test case (#30082)

This is an automated email from the ASF dual-hosted git repository.

zhaojinchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 9515414faa3 Refactor ShowAuthorityRuleExecutor and test case (#30082)
9515414faa3 is described below

commit 9515414faa3fa36b9e8bbb91f8e9270b466c7507
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri Feb 9 22:27:49 2024 +0800

    Refactor ShowAuthorityRuleExecutor and test case (#30082)
    
    * Refactor ShowAuthorityRuleExecutor and test case
    
    * Refactor ShowAuthorityRuleExecutor and test case
    
    * Refactor ShowAuthorityRuleExecutor and test case
    
    * Refactor ShowAuthorityRuleExecutor and test case
---
 .../handler/query/ShowAuthorityRuleExecutor.java   |  9 ++---
 .../query/ShowAuthorityRuleExecutorTest.java       | 41 +++++++++++++++-------
 2 files changed, 34 insertions(+), 16 deletions(-)

diff --git a/kernel/authority/distsql/handler/src/main/java/org/apache/shardingsphere/authority/distsql/handler/query/ShowAuthorityRuleExecutor.java b/kernel/authority/distsql/handler/src/main/java/org/apache/shardingsphere/authority/distsql/handler/query/ShowAuthorityRuleExecutor.java
index c8fa8eedb45..fe02aacce0b 100644
--- a/kernel/authority/distsql/handler/src/main/java/org/apache/shardingsphere/authority/distsql/handler/query/ShowAuthorityRuleExecutor.java
+++ b/kernel/authority/distsql/handler/src/main/java/org/apache/shardingsphere/authority/distsql/handler/query/ShowAuthorityRuleExecutor.java
@@ -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;
@@ -29,6 +28,7 @@ import org.apache.shardingsphere.mode.manager.ContextManager;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Properties;
 import java.util.stream.Collectors;
 
 /**
@@ -46,9 +46,10 @@ public final class ShowAuthorityRuleExecutor implements DistSQLQueryExecutor<Sho
     
     @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
diff --git a/kernel/authority/distsql/handler/src/test/java/org/apache/shardingsphere/authority/distsql/handler/query/ShowAuthorityRuleExecutorTest.java b/kernel/authority/distsql/handler/src/test/java/org/apache/shardingsphere/authority/distsql/handler/query/ShowAuthorityRuleExecutorTest.java
index 2e3f3774cde..ec0e3e42817 100644
--- a/kernel/authority/distsql/handler/src/test/java/org/apache/shardingsphere/authority/distsql/handler/query/ShowAuthorityRuleExecutorTest.java
+++ b/kernel/authority/distsql/handler/src/test/java/org/apache/shardingsphere/authority/distsql/handler/query/ShowAuthorityRuleExecutorTest.java
@@ -20,41 +20,58 @@ package org.apache.shardingsphere.authority.distsql.handler.query;
 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()));
     }
 }