This is an automated email from the ASF dual-hosted git repository.
wuweijie 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 609c28e Refactor AuthorityRule to hide getProvider() (#10033)
609c28e is described below
commit 609c28ea2eb3e7d86d6a0e46fef096bef1498119
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Apr 11 00:44:17 2021 +0800
Refactor AuthorityRule to hide getProvider() (#10033)
* Refactor AuthorityRule
* Fix test case
* Fix test cases
---
.../authority/spi/AuthorityProvideAlgorithm.java | 2 +-
.../authority/checker/AuthorityChecker.java | 4 +--
.../authority/rule/AuthorityRule.java | 31 ++++++++++++++++++----
.../authority/GovernanceAuthorityContext.java | 4 +--
.../TextProtocolBackendHandlerFactoryTest.java | 20 ++++++++++++--
.../command/MySQLCommandExecutorFactoryTest.java | 16 ++++++++---
.../execute/MySQLComStmtExecuteExecutorTest.java | 20 +++++++++-----
7 files changed, 73 insertions(+), 24 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/AuthorityProvideAlgorithm.java
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/AuthorityProvideAlgorithm.java
index 4ad62bc..7fff91e 100644
---
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/AuthorityProvideAlgorithm.java
+++
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-api/src/main/java/org/apache/shardingsphere/authority/spi/AuthorityProvideAlgorithm.java
@@ -39,7 +39,7 @@ public interface AuthorityProvideAlgorithm extends
ShardingSphereAlgorithm {
* @param users users
*/
void init(Map<String, ShardingSphereMetaData> mataDataMap,
Collection<ShardingSphereUser> users);
-
+
/**
* Refresh authority.
*
diff --git
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
index a5292fb..eae958a 100644
---
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
+++
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
@@ -42,7 +42,7 @@ public final class AuthorityChecker implements
SQLChecker<AuthorityRule> {
if (null == grantee) {
return true;
}
- return
authorityRule.getAuthorityProvider().findPrivileges(grantee).map(optional ->
optional.hasPrivileges(schemaName)).orElse(false);
+ return authorityRule.findPrivileges(grantee).map(optional ->
optional.hasPrivileges(schemaName)).orElse(false);
}
@Override
@@ -50,7 +50,7 @@ public final class AuthorityChecker implements
SQLChecker<AuthorityRule> {
if (null == grantee) {
return new SQLCheckResult(true, "");
}
- Optional<ShardingSpherePrivileges> privileges =
authorityRule.getAuthorityProvider().findPrivileges(grantee);
+ Optional<ShardingSpherePrivileges> privileges =
authorityRule.findPrivileges(grantee);
// TODO add error msg
return privileges.map(optional -> new
SQLCheckResult(optional.hasPrivileges(Collections.singletonList(getPrivilege(sqlStatement))),
"")).orElseGet(() -> new SQLCheckResult(false, ""));
}
diff --git
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
index f5a59e6..b6e0901 100644
---
a/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
+++
b/shardingsphere-features/shardingsphere-authority/shardingsphere-authority-common/src/main/java/org/apache/shardingsphere/authority/rule/AuthorityRule.java
@@ -17,32 +17,53 @@
package org.apache.shardingsphere.authority.rule;
-import lombok.Getter;
import
org.apache.shardingsphere.authority.api.config.AuthorityRuleConfiguration;
+import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges;
import org.apache.shardingsphere.authority.spi.AuthorityProvideAlgorithm;
import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import java.util.Collection;
import java.util.Map;
+import java.util.Optional;
/**
* Authority rule.
*/
-@Getter
public final class AuthorityRule implements GlobalRule {
static {
ShardingSphereServiceLoader.register(AuthorityProvideAlgorithm.class);
}
- private final AuthorityProvideAlgorithm authorityProvider;
+ private final AuthorityProvideAlgorithm provider;
public AuthorityRule(final AuthorityRuleConfiguration config, final
Map<String, ShardingSphereMetaData> mataDataMap, final
Collection<ShardingSphereUser> users) {
- authorityProvider =
ShardingSphereAlgorithmFactory.createAlgorithm(config.getProvider(),
AuthorityProvideAlgorithm.class);
- authorityProvider.init(mataDataMap, users);
+ provider =
ShardingSphereAlgorithmFactory.createAlgorithm(config.getProvider(),
AuthorityProvideAlgorithm.class);
+ provider.init(mataDataMap, users);
+ }
+
+ /**
+ * Find Privileges.
+ *
+ * @param grantee grantee
+ * @return found privileges
+ */
+ public Optional<ShardingSpherePrivileges> findPrivileges(final Grantee
grantee) {
+ return provider.findPrivileges(grantee);
+ }
+
+ /**
+ * Refresh authority.
+ *
+ * @param mataDataMap mata data map
+ * @param users users
+ */
+ public void refresh(final Map<String, ShardingSphereMetaData> mataDataMap,
final Collection<ShardingSphereUser> users) {
+ provider.refresh(mataDataMap, users);
}
}
diff --git
a/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/authority/GovernanceAuthorityContext.java
b/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/authority/GovernanceAuthorityContext.java
index 93c370c..aff3de9 100644
---
a/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/authority/GovernanceAuthorityContext.java
+++
b/shardingsphere-governance/shardingsphere-governance-context/src/main/java/org/apache/shardingsphere/governance/context/authority/GovernanceAuthorityContext.java
@@ -21,7 +21,6 @@ import com.google.common.base.Preconditions;
import com.google.common.eventbus.Subscribe;
import lombok.Setter;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
-import org.apache.shardingsphere.authority.spi.AuthorityProvideAlgorithm;
import
org.apache.shardingsphere.governance.core.event.model.authority.AuthorityChangedEvent;
import
org.apache.shardingsphere.infra.context.metadata.MetaDataAwareEventSubscriber;
import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
@@ -51,7 +50,6 @@ public final class GovernanceAuthorityContext implements
MetaDataAwareEventSubsc
private void reloadAuthority(final Collection<ShardingSphereUser> users) {
Optional<AuthorityRule> authorityRule =
metaDataContexts.getGlobalRuleMetaData().getRules().stream().filter(each ->
each instanceof AuthorityRule).findAny().map(each -> (AuthorityRule) each);
Preconditions.checkState(authorityRule.isPresent());
- AuthorityProvideAlgorithm provider =
authorityRule.get().getAuthorityProvider();
- provider.refresh(metaDataContexts.getMetaDataMap(), users);
+ authorityRule.get().refresh(metaDataContexts.getMetaDataMap(), users);
}
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/TextProtocolBackendHandlerFactoryTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/TextProtocolBackendHandlerFactoryTest.java
index cf34d25..85359b4 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/TextProtocolBackendHandlerFactoryTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/TextProtocolBackendHandlerFactoryTest.java
@@ -23,6 +23,7 @@ import
org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import
org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
+import
org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
import
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
@@ -49,6 +50,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.lang.reflect.Field;
import java.sql.SQLException;
+import java.util.Collections;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
@@ -69,8 +71,10 @@ public final class TextProtocolBackendHandlerFactoryTest {
when(backendConnection.getTransactionStatus().getTransactionType()).thenReturn(TransactionType.LOCAL);
setTransactionContexts();
when(backendConnection.getSchemaName()).thenReturn("schema");
- MetaDataContexts metaDataContexts = mock(MetaDataContexts.class);
-
when(metaDataContexts.getMetaData("schema")).thenReturn(mock(ShardingSphereMetaData.class,
RETURNS_DEEP_STUBS));
+ MetaDataContexts metaDataContexts = mock(MetaDataContexts.class,
RETURNS_DEEP_STUBS);
+ mockGlobalRuleMetaData(metaDataContexts);
+ ShardingSphereMetaData shardingSphereMetaData =
mockShardingSphereMetaData();
+
when(metaDataContexts.getMetaData("schema")).thenReturn(shardingSphereMetaData);
when(metaDataContexts.getMetaData("schema").getResource()).thenReturn(mock(ShardingSphereResource.class));
when(metaDataContexts.getMetaData("schema").getResource().getDatabaseType()).thenReturn(databaseType);
TransactionContexts transactionContexts =
mock(TransactionContexts.class);
@@ -78,6 +82,18 @@ public final class TextProtocolBackendHandlerFactoryTest {
proxyContext.init(metaDataContexts, transactionContexts);
}
+ private ShardingSphereMetaData mockShardingSphereMetaData() {
+ ShardingSphereMetaData result = mock(ShardingSphereMetaData.class,
RETURNS_DEEP_STUBS);
+
when(result.getRuleMetaData().getRules()).thenReturn(Collections.emptyList());
+ return result;
+ }
+
+ private void mockGlobalRuleMetaData(final MetaDataContexts
metaDataContexts) {
+ ShardingSphereRuleMetaData globalRuleMetaData =
mock(ShardingSphereRuleMetaData.class);
+
when(globalRuleMetaData.getRules()).thenReturn(Collections.emptyList());
+
when(metaDataContexts.getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
+ }
+
@SneakyThrows(ReflectiveOperationException.class)
private void setTransactionContexts() {
Field transactionContexts =
ProxyContext.getInstance().getClass().getDeclaredField("transactionContexts");
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/MySQLCommandExecutorFactoryTest.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/MySQLCommandExecutorFactoryTest.java
index 3aa9240..c031d2f 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/MySQLCommandExecutorFactoryTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/MySQLCommandExecutorFactoryTest.java
@@ -27,6 +27,7 @@ import
org.apache.shardingsphere.db.protocol.mysql.packet.command.query.text.fie
import
org.apache.shardingsphere.db.protocol.mysql.packet.command.query.text.query.MySQLComQueryPacket;
import org.apache.shardingsphere.db.protocol.packet.CommandPacket;
import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
import
org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
@@ -74,11 +75,18 @@ public final class MySQLCommandExecutorFactoryTest {
when(backendConnection.getSchemaName()).thenReturn("logic_db");
Field field =
ProxyContext.getInstance().getClass().getDeclaredField("metaDataContexts");
field.setAccessible(true);
- ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class,
RETURNS_DEEP_STUBS);
- when(metaData.getResource().getDatabaseType()).thenReturn(new
MySQLDatabaseType());
+ ShardingSphereMetaData metaData = mockShardingSphereMetaData();
Map<String, ShardingSphereMetaData> metaDataMap =
Collections.singletonMap("logic_db", metaData);
- field.set(ProxyContext.getInstance(), new
StandardMetaDataContexts(metaDataMap,
- mock(ShardingSphereRuleMetaData.class),
mock(ExecutorEngine.class), new ShardingSphereUsers(Collections.emptyList()),
new ConfigurationProperties(new Properties())));
+ MetaDataContexts metaDataContexts = new
StandardMetaDataContexts(metaDataMap,
+ mock(ShardingSphereRuleMetaData.class),
mock(ExecutorEngine.class), new ShardingSphereUsers(Collections.emptyList()),
new ConfigurationProperties(new Properties()));
+ field.set(ProxyContext.getInstance(), metaDataContexts);
+ }
+
+ private ShardingSphereMetaData mockShardingSphereMetaData() {
+ ShardingSphereMetaData result = mock(ShardingSphereMetaData.class,
RETURNS_DEEP_STUBS);
+
when(result.getRuleMetaData().getRules()).thenReturn(Collections.emptyList());
+ when(result.getResource().getDatabaseType()).thenReturn(new
MySQLDatabaseType());
+ return result;
}
@Test
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutorTest.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutorTest.java
index b35763d..eb51024 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutorTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/command/query/binary/execute/MySQLComStmtExecuteExecutorTest.java
@@ -60,14 +60,20 @@ public final class MySQLComStmtExecuteExecutorTest {
@Before
public void setUp() throws ReflectiveOperationException {
- Field metaDataContexts =
ProxyContext.getInstance().getClass().getDeclaredField("metaDataContexts");
- metaDataContexts.setAccessible(true);
- ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class,
RETURNS_DEEP_STUBS);
- when(metaData.getResource().getDatabaseType()).thenReturn(new
MySQLDatabaseType());
+ Field field =
ProxyContext.getInstance().getClass().getDeclaredField("metaDataContexts");
+ field.setAccessible(true);
+ ShardingSphereMetaData metaData = mockShardingSphereMetaData();
Map<String, ShardingSphereMetaData> metaDataMap =
Collections.singletonMap("logic_db", metaData);
- metaDataContexts.set(ProxyContext.getInstance(),
- new StandardMetaDataContexts(metaDataMap,
- mock(ShardingSphereRuleMetaData.class),
mock(ExecutorEngine.class), new ShardingSphereUsers(Collections.emptyList()),
new ConfigurationProperties(new Properties())));
+ StandardMetaDataContexts metaDataContexts = new
StandardMetaDataContexts(metaDataMap,
+ mock(ShardingSphereRuleMetaData.class),
mock(ExecutorEngine.class), new ShardingSphereUsers(Collections.emptyList()),
new ConfigurationProperties(new Properties()));
+ field.set(ProxyContext.getInstance(), metaDataContexts);
+ }
+
+ private ShardingSphereMetaData mockShardingSphereMetaData() {
+ ShardingSphereMetaData result = mock(ShardingSphereMetaData.class,
RETURNS_DEEP_STUBS);
+
when(result.getRuleMetaData().getRules()).thenReturn(Collections.emptyList());
+ when(result.getResource().getDatabaseType()).thenReturn(new
MySQLDatabaseType());
+ return result;
}
@Test