This is an automated email from the ASF dual-hosted git repository.
panjuan 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 fc2c7bf9fb5 Remove SQLCheckerFixture for proxy backend module (#19739)
fc2c7bf9fb5 is described below
commit fc2c7bf9fb57b160bf516718830e06204fe15382
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Aug 1 14:51:11 2022 +0800
Remove SQLCheckerFixture for proxy backend module (#19739)
---
.../authority/checker/AuthorityChecker.java | 16 ++--
.../rulealtered/RuleAlteredJobPersistService.java | 1 -
.../mysql/executor/fixture/SQLCheckerFixture.java | 66 ---------------
... => SelectInformationSchemataExecutorTest.java} | 99 ++++++++++++----------
....shardingsphere.infra.executor.check.SQLChecker | 18 ----
5 files changed, 60 insertions(+), 140 deletions(-)
diff --git
a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
index 11471513a9a..d0227513fd9 100644
---
a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
+++
b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/checker/AuthorityChecker.java
@@ -56,20 +56,20 @@ import java.util.function.BiPredicate;
public final class AuthorityChecker implements SQLChecker<AuthorityRule> {
@Override
- public boolean check(final String databaseName, final Grantee grantee,
final AuthorityRule authorityRule) {
+ public boolean check(final String databaseName, final Grantee grantee,
final AuthorityRule rule) {
if (null == grantee) {
return true;
}
- return authorityRule.findPrivileges(grantee).map(optional ->
optional.hasPrivileges(databaseName)).orElse(false);
+ return rule.findPrivileges(grantee).map(optional ->
optional.hasPrivileges(databaseName)).orElse(false);
}
@Override
public SQLCheckResult check(final SQLStatementContext<?>
sqlStatementContext, final List<Object> parameters, final Grantee grantee,
- final String currentDatabase, final
Map<String, ShardingSphereDatabase> databases, final AuthorityRule
authorityRule) {
+ final String currentDatabase, final
Map<String, ShardingSphereDatabase> databases, final AuthorityRule rule) {
if (null == grantee) {
return new SQLCheckResult(true, "");
}
- Optional<ShardingSpherePrivileges> privileges =
authorityRule.findPrivileges(grantee);
+ Optional<ShardingSpherePrivileges> privileges =
rule.findPrivileges(grantee);
if (!privileges.isPresent()) {
return new SQLCheckResult(false, String.format("Access denied for
user '%s'@'%s'", grantee.getUsername(), grantee.getHostname()));
}
@@ -85,13 +85,13 @@ public final class AuthorityChecker implements
SQLChecker<AuthorityRule> {
}
@Override
- public boolean check(final Grantee grantee, final AuthorityRule
authorityRule) {
- return authorityRule.findUser(grantee).isPresent();
+ public boolean check(final Grantee grantee, final AuthorityRule rule) {
+ return rule.findUser(grantee).isPresent();
}
@Override
- public boolean check(final Grantee grantee, final BiPredicate<Object,
Object> validator, final Object cipher, final AuthorityRule authorityRule) {
- Optional<ShardingSphereUser> user = authorityRule.findUser(grantee);
+ public boolean check(final Grantee grantee, final BiPredicate<Object,
Object> validator, final Object cipher, final AuthorityRule rule) {
+ Optional<ShardingSphereUser> user = rule.findUser(grantee);
return user.filter(each -> validator.test(each, cipher)).isPresent();
}
diff --git
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/rulealtered/RuleAlteredJobPersistService.java
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/rulealtered/RuleAlteredJobPersistService.java
index dcda0743b23..3d5245bfef1 100644
---
a/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/rulealtered/RuleAlteredJobPersistService.java
+++
b/shardingsphere-kernel/shardingsphere-data-pipeline/shardingsphere-data-pipeline-core/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/rulealtered/RuleAlteredJobPersistService.java
@@ -32,7 +32,6 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
-
/**
* Rule altered job persist service.
*/
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/fixture/SQLCheckerFixture.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/fixture/SQLCheckerFixture.java
deleted file mode 100644
index f1cc51982c9..00000000000
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/fixture/SQLCheckerFixture.java
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package
org.apache.shardingsphere.proxy.backend.handler.admin.mysql.executor.fixture;
-
-import org.apache.shardingsphere.authority.rule.AuthorityRule;
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.check.SQLCheckResult;
-import org.apache.shardingsphere.infra.executor.check.SQLChecker;
-import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.metadata.user.Grantee;
-
-import java.util.List;
-import java.util.Map;
-import java.util.function.BiPredicate;
-
-/**
- * SQL checker fixture.
- */
-public final class SQLCheckerFixture implements SQLChecker<AuthorityRule> {
-
- @Override
- public boolean check(final String databaseName, final Grantee grantee,
final AuthorityRule authorityRule) {
- return "sharding_db".equals(databaseName) ||
"empty_db".equals(databaseName);
- }
-
- @Override
- public SQLCheckResult check(final SQLStatementContext<?>
sqlStatementContext, final List<Object> parameters, final Grantee grantee,
- final String currentDatabase, final
Map<String, ShardingSphereDatabase> databases, final AuthorityRule
authorityRule) {
- return new SQLCheckResult(true, "");
- }
-
- @Override
- public boolean check(final Grantee grantee, final AuthorityRule
authorityRule) {
- return true;
- }
-
- @Override
- public boolean check(final Grantee grantee, final BiPredicate<Object,
Object> validator, final Object cipher, final AuthorityRule authorityRule) {
- return true;
- }
-
- @Override
- public int getOrder() {
- return 501;
- }
-
- @Override
- public Class<AuthorityRule> getTypeClass() {
- return AuthorityRule.class;
- }
-}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/information/SelectInformationExecutorTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/information/SelectInformationSchemataExecutorTest.java
similarity index 68%
rename from
shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/information/SelectInformationExecutorTest.java
rename to
shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/information/SelectInformationSchemataExecutorTest.java
index b42cafc2336..d7834283d48 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/information/SelectInformationExecutorTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/mysql/executor/information/SelectInformationSchemataExecutorTest.java
@@ -17,6 +17,7 @@
package
org.apache.shardingsphere.proxy.backend.handler.admin.mysql.executor.information;
+import
org.apache.shardingsphere.authority.provider.database.model.privilege.DatabasePermittedPrivileges;
import org.apache.shardingsphere.authority.rule.AuthorityRule;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
@@ -32,8 +33,8 @@ import
org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
import
org.apache.shardingsphere.parser.rule.builder.DefaultSQLParserRuleConfigurationBuilder;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
-import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import
org.apache.shardingsphere.proxy.backend.handler.admin.executor.AbstractDatabaseMetadataExecutor.DefaultDatabaseMetadataExecutor;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
import org.apache.shardingsphere.proxy.backend.util.ProxyContextRestorer;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import org.apache.shardingsphere.test.mock.MockedDataSource;
@@ -47,82 +48,85 @@ import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
-public final class SelectInformationExecutorTest extends ProxyContextRestorer {
+public final class SelectInformationSchemataExecutorTest extends
ProxyContextRestorer {
private final SQLParserRule sqlParserRule = new SQLParserRule(new
DefaultSQLParserRuleConfigurationBuilder().build());
+ private final Grantee grantee = new Grantee("root", "127.0.0.1");
+
@Mock
private ConnectionSession connectionSession;
@Before
public void setUp() {
- ContextManager contextManager = mock(ContextManager.class,
RETURNS_DEEP_STUBS);
+ ProxyContext.init(mockContextManager());
+ when(connectionSession.getGrantee()).thenReturn(grantee);
+ }
+
+ private ContextManager mockContextManager() {
+ AuthorityRule authorityRule = mock(AuthorityRule.class);
+ when(authorityRule.findPrivileges(grantee)).thenReturn(Optional.of(new
DatabasePermittedPrivileges(new HashSet<>(Arrays.asList("auth_db",
"empty_db")))));
+ ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
MetaDataContexts metaDataContexts = new
MetaDataContexts(mock(MetaDataPersistService.class),
- new ShardingSphereMetaData(new HashMap<>(),
mock(ShardingSphereRuleMetaData.class), new ConfigurationProperties(new
Properties())), mock(OptimizerContext.class));
-
when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
- ProxyContext.init(contextManager);
- when(connectionSession.getGrantee()).thenReturn(new Grantee("root",
"127.0.0.1"));
+ new ShardingSphereMetaData(new HashMap<>(), new
ShardingSphereRuleMetaData(Collections.singleton(authorityRule)), new
ConfigurationProperties(new Properties())),
+ mock(OptimizerContext.class));
+ when(result.getMetaDataContexts()).thenReturn(metaDataContexts);
+ return result;
}
@Test
- public void assertSelectSchemataExecute() throws SQLException {
- Map<String, String> expectedResultSetMap = new HashMap<>();
+ public void assertExecuteWithResource() throws SQLException {
+ Map<String, String> expectedResultSetMap = new HashMap<>(2, 1);
expectedResultSetMap.put("SCHEMA_NAME", "foo_ds");
- expectedResultSetMap.put("DEFAULT_CHARACTER_SET_NAME",
"utf8mb4_0900_ai_ci");
expectedResultSetMap.put("DEFAULT_COLLATION_NAME", "utf8mb4");
- Map<String, ShardingSphereDatabase> databases =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases();
- databases.put("sharding_db", createDatabase(expectedResultSetMap));
- databases.put("database_without_authority",
createEmptyDatabase("database_without_authority"));
- String sql = "SELECT SCHEMA_NAME, DEFAULT_CHARACTER_SET_NAME,
DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA";
+
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases().put("auth_db",
createDatabase(expectedResultSetMap));
+
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases().put("no_auth_db",
createDatabase("no_auth_db"));
+ String sql = "SELECT SCHEMA_NAME, DEFAULT_COLLATION_NAME,
DEFAULT_ENCRYPTION FROM information_schema.SCHEMATA";
SelectInformationSchemataExecutor executor = new
SelectInformationSchemataExecutor((SelectStatement)
sqlParserRule.getSQLParserEngine("MySQL").parse(sql, false), sql);
executor.execute(connectionSession);
- assertThat(executor.getQueryResultMetaData().getColumnCount(),
is(expectedResultSetMap.size()));
- int count = 0;
- while (executor.getMergedResult().next()) {
- count++;
- if ("sharding_db".equals(executor.getMergedResult().getValue(1,
String.class))) {
- assertThat(executor.getMergedResult().getValue(2,
String.class), is("utf8mb4"));
- assertThat(executor.getMergedResult().getValue(3,
String.class), is("utf8mb4_0900_ai_ci"));
- } else {
- fail("expected : `sharding_db`");
- }
- }
- assertThat(count, is(1));
+ assertThat(executor.getQueryResultMetaData().getColumnCount(), is(2));
+ assertTrue(executor.getMergedResult().next());
+ assertThat(executor.getMergedResult().getValue(1, String.class),
is("auth_db"));
+ assertThat(executor.getMergedResult().getValue(2, String.class),
is("utf8mb4"));
+ assertFalse(executor.getMergedResult().next());
}
@Test
- public void assertSelectSchemataInSchemaWithoutDataSourceExecute() throws
SQLException {
- Map<String, ShardingSphereDatabase> databases =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases();
- databases.put("empty_db", createEmptyDatabase("empty_db"));
+ public void assertExecuteWithoutResource() throws SQLException {
+
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases().put("empty_db",
createDatabase("empty_db"));
String sql = "SELECT SCHEMA_NAME, DEFAULT_CHARACTER_SET_NAME,
DEFAULT_COLLATION_NAME, DEFAULT_ENCRYPTION FROM information_schema.SCHEMATA";
SelectInformationSchemataExecutor executor = new
SelectInformationSchemataExecutor((SelectStatement)
sqlParserRule.getSQLParserEngine("MySQL").parse(sql, false), sql);
executor.execute(connectionSession);
assertThat(executor.getQueryResultMetaData().getColumnCount(), is(4));
- while (executor.getMergedResult().next()) {
- assertThat(executor.getMergedResult().getValue(1, String.class),
is("empty_db"));
- assertThat(executor.getMergedResult().getValue(2, String.class),
is(""));
- assertThat(executor.getMergedResult().getValue(3, String.class),
is(""));
- assertThat(executor.getMergedResult().getValue(4, String.class),
is(""));
- }
+ assertTrue(executor.getMergedResult().next());
+ assertThat(executor.getMergedResult().getValue(1, String.class),
is("empty_db"));
+ assertThat(executor.getMergedResult().getValue(2, String.class),
is(""));
+ assertThat(executor.getMergedResult().getValue(3, String.class),
is(""));
+ assertThat(executor.getMergedResult().getValue(4, String.class),
is(""));
+ assertFalse(executor.getMergedResult().next());
}
@Test
- public void assertSelectSchemataInNoSchemaExecute() throws SQLException {
+ public void assertExecuteWithoutDatabases() throws SQLException {
String sql = "SELECT SCHEMA_NAME, DEFAULT_CHARACTER_SET_NAME,
DEFAULT_COLLATION_NAME, DEFAULT_ENCRYPTION FROM information_schema.SCHEMATA";
SelectInformationSchemataExecutor executor = new
SelectInformationSchemataExecutor((SelectStatement)
sqlParserRule.getSQLParserEngine("MySQL").parse(sql, false), sql);
executor.execute(connectionSession);
@@ -135,8 +139,8 @@ public final class SelectInformationExecutorTest extends
ProxyContextRestorer {
expectedResultSetMap.put("sn", "foo_ds");
expectedResultSetMap.put("DEFAULT_CHARACTER_SET_NAME", "utf8mb4");
Map<String, ShardingSphereDatabase> databases =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases();
- databases.put("sharding_db", createDatabase(expectedResultSetMap));
- databases.put("empty_db", createEmptyDatabase("empty_db"));
+ databases.put("auth_db", createDatabase(expectedResultSetMap));
+ databases.put("empty_db", createDatabase("empty_db"));
String sql = "SELECT SCHEMA_NAME AS sn, DEFAULT_CHARACTER_SET_NAME
FROM information_schema.SCHEMATA";
DefaultDatabaseMetadataExecutor executor = new
DefaultDatabaseMetadataExecutor(sql);
executor.execute(connectionSession);
@@ -147,7 +151,7 @@ public final class SelectInformationExecutorTest extends
ProxyContextRestorer {
@Test
public void assertDefaultExecute() throws SQLException {
Map<String, ShardingSphereDatabase> databases =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabases();
- databases.put("sharding_db",
createDatabase(Collections.singletonMap("support_ndb", "0")));
+ databases.put("auth_db",
createDatabase(Collections.singletonMap("support_ndb", "0")));
String sql = "SELECT COUNT(*) AS support_ndb FROM
information_schema.ENGINES WHERE Engine = 'ndbcluster'";
DefaultDatabaseMetadataExecutor executor = new
DefaultDatabaseMetadataExecutor(sql);
executor.execute(connectionSession);
@@ -157,15 +161,16 @@ public final class SelectInformationExecutorTest extends
ProxyContextRestorer {
}
}
- private ShardingSphereDatabase createDatabase(final Map<String, String>
expectedResultSetMap) throws SQLException {
- return new ShardingSphereDatabase("sharding_db", new
MySQLDatabaseType(), new ShardingSphereResource(
- Collections.singletonMap("foo_ds", new
MockedDataSource(mockConnection(expectedResultSetMap)))),
mock(ShardingSphereRuleMetaData.class), Collections.emptyMap());
+ private ShardingSphereDatabase createDatabase(final String databaseName,
final ShardingSphereResource resource) {
+ return new ShardingSphereDatabase(databaseName, new
MySQLDatabaseType(), resource, mock(ShardingSphereRuleMetaData.class),
Collections.emptyMap());
+ }
+
+ private ShardingSphereDatabase createDatabase(final String databaseName) {
+ return createDatabase(databaseName, new
ShardingSphereResource(Collections.emptyMap()));
}
- private ShardingSphereDatabase createEmptyDatabase(final String
databaseName) {
- ShardingSphereRuleMetaData ruleMetaData =
mock(ShardingSphereRuleMetaData.class);
-
when(ruleMetaData.getRules()).thenReturn(Collections.singleton(mock(AuthorityRule.class,
RETURNS_DEEP_STUBS)));
- return new ShardingSphereDatabase(databaseName, new
MySQLDatabaseType(), new ShardingSphereResource(Collections.emptyMap()),
ruleMetaData, Collections.emptyMap());
+ private ShardingSphereDatabase createDatabase(final Map<String, String>
expectedResultSetMap) throws SQLException {
+ return createDatabase("auth_db", new
ShardingSphereResource(Collections.singletonMap("foo_ds", new
MockedDataSource(mockConnection(expectedResultSetMap)))));
}
private Connection mockConnection(final Map<String, String>
expectedResultSetMap) throws SQLException {
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.executor.check.SQLChecker
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.executor.check.SQLChecker
deleted file mode 100644
index 07764e3e3ab..00000000000
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.executor.check.SQLChecker
+++ /dev/null
@@ -1,18 +0,0 @@
-#
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements. See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.
-# The ASF licenses this file to You under the Apache License, Version 2.0
-# (the "License"); you may not use this file except in compliance with
-# the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-org.apache.shardingsphere.proxy.backend.handler.admin.mysql.executor.fixture.SQLCheckerFixture