This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 f3f56e7a4b5 Add more assertions for AlterSQLFederationRuleExecutorTest
(#33104)
f3f56e7a4b5 is described below
commit f3f56e7a4b591bbc248daf4fb17f51bcdf6ae159
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Oct 3 16:02:00 2024 +0800
Add more assertions for AlterSQLFederationRuleExecutorTest (#33104)
* Refactor ShowGlobalClockRuleExecutorTest
* Add more assertions for AlterGlobalClockRuleExecutorTest
* Add more assertions for ShowGlobalClockRuleExecutorTest
* Add more assertions for AlterSQLFederationRuleExecutorTest
---
...wDefaultSingleTableStorageUnitExecutorTest.java | 35 +++++---------
.../query/ShowSingleTablesExecutorTest.java | 4 +-
...tDefaultSingleTableStorageUnitExecutorTest.java | 3 +-
kernel/sql-federation/distsql/handler/pom.xml | 7 +++
.../query/ShowSQLFederationRuleExecutorTest.java | 36 ++++++--------
.../update/AlterSQLFederationRuleExecutorTest.java | 56 +++++++++++++++-------
6 files changed, 76 insertions(+), 65 deletions(-)
diff --git
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutorTest.java
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutorTest.java
index 1d580d4d273..887bf78bf88 100644
---
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutorTest.java
+++
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutorTest.java
@@ -21,18 +21,17 @@ import
org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext
import
org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.single.config.SingleRuleConfiguration;
import
org.apache.shardingsphere.single.distsql.statement.rql.ShowDefaultSingleTableStorageUnitStatement;
import org.apache.shardingsphere.single.rule.SingleRule;
-import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.sql.SQLException;
-import java.util.Collection;
+import java.util.ArrayList;
import java.util.Collections;
-import java.util.Iterator;
-import java.util.Optional;
+import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -42,30 +41,22 @@ import static org.mockito.Mockito.when;
class ShowDefaultSingleTableStorageUnitExecutorTest {
- private DistSQLQueryExecuteEngine engine;
-
- @BeforeEach
- void setUp() {
- engine = new
DistSQLQueryExecuteEngine(mock(ShowDefaultSingleTableStorageUnitStatement.class),
"foo_db", mockContextManager(), mock(DistSQLConnectionContext.class));
+ @Test
+ void assertGetRowData() throws SQLException {
+ DistSQLQueryExecuteEngine engine = new
DistSQLQueryExecuteEngine(mock(ShowDefaultSingleTableStorageUnitStatement.class),
"foo_db", mockContextManager(), mock(DistSQLConnectionContext.class));
+ engine.executeQuery();
+ List<LocalDataQueryResultRow> actual = new
ArrayList<>(engine.getRows());
+ assertThat(actual.size(), is(1));
+ assertThat((String) actual.get(0).getCell(1), is("foo_ds"));
}
private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
- ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
- when(result.getDatabase("foo_db")).thenReturn(database);
SingleRule rule = mock(SingleRule.class);
when(rule.getConfiguration()).thenReturn(new
SingleRuleConfiguration(Collections.emptyList(), "foo_ds"));
-
when(database.getRuleMetaData().findSingleRule(SingleRule.class)).thenReturn(Optional.of(rule));
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
+ when(result.getDatabase("foo_db")).thenReturn(database);
+ when(database.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
return result;
}
-
- @Test
- void assertGetRowData() throws SQLException {
- engine.executeQuery();
- Collection<LocalDataQueryResultRow> actual = engine.getRows();
- assertThat(actual.size(), is(1));
- Iterator<LocalDataQueryResultRow> rowData = actual.iterator();
- String defaultSingleTableStorageUnit = (String)
rowData.next().getCell(1);
- assertThat(defaultSingleTableStorageUnit, is("foo_ds"));
- }
}
diff --git
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTablesExecutorTest.java
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTablesExecutorTest.java
index 3dbfc06b28d..ddee5bb1c07 100644
---
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTablesExecutorTest.java
+++
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTablesExecutorTest.java
@@ -22,6 +22,7 @@ import
org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecut
import org.apache.shardingsphere.infra.datanode.DataNode;
import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import
org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute;
import org.apache.shardingsphere.mode.manager.ContextManager;
@@ -36,7 +37,6 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -73,7 +73,7 @@ class ShowSingleTablesExecutorTest {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
when(result.getDatabase("foo_db")).thenReturn(database);
SingleRule rule = mockSingleRule();
-
when(database.getRuleMetaData().findSingleRule(SingleRule.class)).thenReturn(Optional.of(rule));
+ when(database.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
return result;
}
diff --git
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutorTest.java
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutorTest.java
index da84b3fbee6..3446ca49999 100644
---
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutorTest.java
+++
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutorTest.java
@@ -86,9 +86,8 @@ class SetDefaultSingleTableStorageUnitExecutorTest {
private ContextManager mockContextManager(final SingleRule rule, final
ShardingSphereDatabase database) {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
- RuleMetaData ruleMetaData = new
RuleMetaData(Collections.singleton(rule));
when(database.getName()).thenReturn("foo_db");
- when(database.getRuleMetaData()).thenReturn(ruleMetaData);
+ when(database.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
when(result.getDatabase("foo_db")).thenReturn(database);
return result;
}
diff --git a/kernel/sql-federation/distsql/handler/pom.xml
b/kernel/sql-federation/distsql/handler/pom.xml
index 22f52123fa6..f4ff1302edf 100644
--- a/kernel/sql-federation/distsql/handler/pom.xml
+++ b/kernel/sql-federation/distsql/handler/pom.xml
@@ -47,5 +47,12 @@
<artifactId>shardingsphere-sql-federation-distsql-parser</artifactId>
<version>${project.version}</version>
</dependency>
+
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-test-util</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git
a/kernel/sql-federation/distsql/handler/src/test/java/org/apache/shardingsphere/sqlfederation/distsql/handler/query/ShowSQLFederationRuleExecutorTest.java
b/kernel/sql-federation/distsql/handler/src/test/java/org/apache/shardingsphere/sqlfederation/distsql/handler/query/ShowSQLFederationRuleExecutorTest.java
index 4315e5327f0..fe5cc2787fc 100644
---
a/kernel/sql-federation/distsql/handler/src/test/java/org/apache/shardingsphere/sqlfederation/distsql/handler/query/ShowSQLFederationRuleExecutorTest.java
+++
b/kernel/sql-federation/distsql/handler/src/test/java/org/apache/shardingsphere/sqlfederation/distsql/handler/query/ShowSQLFederationRuleExecutorTest.java
@@ -20,18 +20,18 @@ package
org.apache.shardingsphere.sqlfederation.distsql.handler.query;
import
org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext;
import
org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.sql.parser.api.CacheOption;
import
org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
import
org.apache.shardingsphere.sqlfederation.distsql.statement.queryable.ShowSQLFederationRuleStatement;
import org.apache.shardingsphere.sqlfederation.rule.SQLFederationRule;
-import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.sql.SQLException;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.Optional;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -41,30 +41,22 @@ import static org.mockito.Mockito.when;
class ShowSQLFederationRuleExecutorTest {
- private DistSQLQueryExecuteEngine engine;
-
- @BeforeEach
- void setUp() {
- engine = new DistSQLQueryExecuteEngine(new
ShowSQLFederationRuleStatement(), null, mockContextManager(),
mock(DistSQLConnectionContext.class));
+ @Test
+ void assertGetRows() throws SQLException {
+ DistSQLQueryExecuteEngine engine = new DistSQLQueryExecuteEngine(new
ShowSQLFederationRuleStatement(), null, mockContextManager(),
mock(DistSQLConnectionContext.class));
+ engine.executeQuery();
+ List<LocalDataQueryResultRow> actual = new
ArrayList<>(engine.getRows());
+ assertThat(actual.size(), is(1));
+ assertThat(actual.get(0).getCell(1), is("true"));
+ assertThat(actual.get(0).getCell(2), is("true"));
+ assertThat(actual.get(0).getCell(3), is("initialCapacity: 2000,
maximumSize: 65535"));
}
private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
SQLFederationRule rule = mock(SQLFederationRule.class);
when(rule.getConfiguration()).thenReturn(new
SQLFederationRuleConfiguration(true, true, new CacheOption(2000, 65535L)));
-
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().findSingleRule(SQLFederationRule.class)).thenReturn(Optional.of(rule));
+
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
return result;
}
-
- @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("true"));
- assertThat(row.getCell(2), is("true"));
- assertThat(row.getCell(3), is("initialCapacity: 2000, maximumSize:
65535"));
- }
}
diff --git
a/kernel/sql-federation/distsql/handler/src/test/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutorTest.java
b/kernel/sql-federation/distsql/handler/src/test/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutorTest.java
index aa7035a2df8..da35a8d0e16 100644
---
a/kernel/sql-federation/distsql/handler/src/test/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutorTest.java
+++
b/kernel/sql-federation/distsql/handler/src/test/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutorTest.java
@@ -18,52 +18,74 @@
package org.apache.shardingsphere.sqlfederation.distsql.handler.update;
import
org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExecuteEngine;
-import
org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.global.GlobalRuleDefinitionExecutor;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.mode.manager.ContextManager;
+import
org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
+import org.apache.shardingsphere.sql.parser.api.CacheOption;
+import
org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
import
org.apache.shardingsphere.sqlfederation.distsql.segment.CacheOptionSegment;
import
org.apache.shardingsphere.sqlfederation.distsql.statement.updatable.AlterSQLFederationRuleStatement;
import org.apache.shardingsphere.sqlfederation.rule.SQLFederationRule;
import
org.apache.shardingsphere.sqlfederation.rule.builder.DefaultSQLFederationRuleConfigurationBuilder;
import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentMatchers;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import java.sql.SQLException;
+import java.util.Collections;
+
+import static
org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
+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.verify;
import static org.mockito.Mockito.when;
class AlterSQLFederationRuleExecutorTest {
- private DistSQLUpdateExecuteEngine engine;
-
@Test
- void assertExecute() {
+ void assertExecuteUpdate() throws SQLException {
AlterSQLFederationRuleStatement sqlStatement = new
AlterSQLFederationRuleStatement(true, true, new CacheOptionSegment(64, 512L));
- engine = new DistSQLUpdateExecuteEngine(sqlStatement, null,
mockContextManager());
- assertDoesNotThrow(() -> engine.executeUpdate());
+ ContextManager contextManager = mockContextManager();
+ new DistSQLUpdateExecuteEngine(sqlStatement, null,
contextManager).executeUpdate();
+ MetaDataManagerPersistService metaDataManagerPersistService =
contextManager.getPersistServiceFacade().getMetaDataManagerPersistService();
+ verify(metaDataManagerPersistService).alterGlobalRuleConfiguration(
+ ArgumentMatchers.<SQLFederationRuleConfiguration>argThat(x ->
assertRuleConfiguration(x, true, true, new CacheOption(64, 512L))));
}
@Test
- void assertExecuteWithNullStatement() {
+ void assertExecuteUpdateWithNullStatement() throws SQLException {
AlterSQLFederationRuleStatement sqlStatement = new
AlterSQLFederationRuleStatement(null, null, null);
- engine = new DistSQLUpdateExecuteEngine(sqlStatement, null,
mockContextManager());
- assertDoesNotThrow(() -> engine.executeUpdate());
+ ContextManager contextManager = mockContextManager();
+ new DistSQLUpdateExecuteEngine(sqlStatement, null,
contextManager).executeUpdate();
+ MetaDataManagerPersistService metaDataManagerPersistService =
contextManager.getPersistServiceFacade().getMetaDataManagerPersistService();
+ verify(metaDataManagerPersistService).alterGlobalRuleConfiguration(
+ ArgumentMatchers.<SQLFederationRuleConfiguration>argThat(x ->
assertRuleConfiguration(x, false, false, new CacheOption(2000, 65535L))));
}
@Test
- void assertExecuteWithNullCacheOptionSegment() {
+ void assertExecuteUpdateWithNullCacheOptionSegment() throws SQLException {
AlterSQLFederationRuleStatement sqlStatement = new
AlterSQLFederationRuleStatement(null, null, new CacheOptionSegment(null, null));
- engine = new DistSQLUpdateExecuteEngine(sqlStatement, null,
mockContextManager());
- assertDoesNotThrow(() -> engine.executeUpdate());
+ ContextManager contextManager = mockContextManager();
+ new DistSQLUpdateExecuteEngine(sqlStatement, null,
contextManager).executeUpdate();
+ MetaDataManagerPersistService metaDataManagerPersistService =
contextManager.getPersistServiceFacade().getMetaDataManagerPersistService();
+ verify(metaDataManagerPersistService).alterGlobalRuleConfiguration(
+ ArgumentMatchers.<SQLFederationRuleConfiguration>argThat(x ->
assertRuleConfiguration(x, false, false, new CacheOption(2000, 65535L))));
+ }
+
+ private boolean assertRuleConfiguration(final
SQLFederationRuleConfiguration actual,
+ final boolean
expectedSQLFederationEnabled, final boolean expectedAllQueryUseSQLFederation,
final CacheOption expectedExecutionPlanCache) {
+ assertThat(actual.isSqlFederationEnabled(),
is(expectedSQLFederationEnabled));
+ assertThat(actual.isAllQueryUseSQLFederation(),
is(expectedAllQueryUseSQLFederation));
+ assertThat(actual.getExecutionPlanCache(),
deepEqual(expectedExecutionPlanCache));
+ return true;
}
- @SuppressWarnings({"rawtypes", "unchecked"})
private ContextManager mockContextManager() {
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
SQLFederationRule rule = mock(SQLFederationRule.class);
- GlobalRuleDefinitionExecutor executor =
mock(GlobalRuleDefinitionExecutor.class);
- when(executor.getRuleClass()).thenReturn(SQLFederationRule.class);
when(rule.getConfiguration()).thenReturn(new
DefaultSQLFederationRuleConfigurationBuilder().build());
-
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(executor.getRuleClass())).thenReturn(rule);
+
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
return result;
}
}