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/10/04 05:48:07 UTC

(shardingsphere) branch master updated: Refactor ShowStatusFromReadwriteSplittingRulesExecutorTest (#33117)

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 07b4e4a7d3d Refactor ShowStatusFromReadwriteSplittingRulesExecutorTest (#33117)
07b4e4a7d3d is described below

commit 07b4e4a7d3d7476b084e7fabd80b65ef4095574e
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri Oct 4 13:48:01 2024 +0800

    Refactor ShowStatusFromReadwriteSplittingRulesExecutorTest (#33117)
---
 ...tusFromReadwriteSplittingRulesExecutorTest.java | 53 ++++++++++++++++------
 1 file changed, 38 insertions(+), 15 deletions(-)

diff --git a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowStatusFromReadwriteSplittingRulesExecutorTest.java b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowStatusFromReadwriteSplittingRulesExecutorTest.java
index 48fdba57a9d..afa38bc28f5 100644
--- a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowStatusFromReadwriteSplittingRulesExecutorTest.java
+++ b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowStatusFromReadwriteSplittingRulesExecutorTest.java
@@ -17,41 +17,54 @@
 
 package org.apache.shardingsphere.readwritesplitting.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.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
 import org.apache.shardingsphere.mode.manager.ContextManager;
 import org.apache.shardingsphere.readwritesplitting.distsql.statement.ShowStatusFromReadwriteSplittingRulesStatement;
 import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingDataSourceGroupRule;
 import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule;
 import org.junit.jupiter.api.Test;
 
+import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collection;
 import java.util.Collections;
-import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 class ShowStatusFromReadwriteSplittingRulesExecutorTest {
     
-    // TODO Replace With DistSQLQueryExecuteEngine
     @Test
-    void assertGetRowData() {
-        ShowStatusFromReadwriteSplittingRulesExecutor executor = new ShowStatusFromReadwriteSplittingRulesExecutor();
-        executor.setRule(mockRule());
-        Collection<LocalDataQueryResultRow> actual = executor.getRows(mock(ShowStatusFromReadwriteSplittingRulesStatement.class), mock(ContextManager.class, RETURNS_DEEP_STUBS));
-        assertThat(actual.size(), is(2));
-        Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
-        LocalDataQueryResultRow row = iterator.next();
-        assertThat(row.getCell(1), is("read_ds_0"));
-        assertThat(row.getCell(2), is("ENABLED"));
-        row = iterator.next();
-        assertThat(row.getCell(1), is("read_ds_1"));
-        assertThat(row.getCell(2), is("DISABLED"));
+    void assertExecuteQueryWithoutRuleName() throws SQLException {
+        ContextManager contextManager = mockContextManager(mockRule());
+        DistSQLQueryExecuteEngine queryExecuteEngine = new DistSQLQueryExecuteEngine(
+                new ShowStatusFromReadwriteSplittingRulesStatement(null, null), "foo_db", contextManager, mock(DistSQLConnectionContext.class));
+        queryExecuteEngine.executeQuery();
+        List<LocalDataQueryResultRow> rows = new ArrayList<>(queryExecuteEngine.getRows());
+        assertThat(rows.size(), is(2));
+        assertThat(rows.get(0).getCell(1), is("read_ds_0"));
+        assertThat(rows.get(0).getCell(2), is("ENABLED"));
+        assertThat(rows.get(1).getCell(1), is("read_ds_1"));
+        assertThat(rows.get(1).getCell(2), is("DISABLED"));
+    }
+    
+    @Test
+    void assertExecuteQueryWithRuleName() throws SQLException {
+        ContextManager contextManager = mockContextManager(mockRule());
+        DistSQLQueryExecuteEngine queryExecuteEngine = new DistSQLQueryExecuteEngine(
+                new ShowStatusFromReadwriteSplittingRulesStatement(null, "bar_rule"), "foo_db", contextManager, mock(DistSQLConnectionContext.class));
+        queryExecuteEngine.executeQuery();
+        assertTrue(queryExecuteEngine.getRows().isEmpty());
     }
     
     private ReadwriteSplittingRule mockRule() {
@@ -63,8 +76,18 @@ class ShowStatusFromReadwriteSplittingRulesExecutorTest {
     
     private ReadwriteSplittingDataSourceGroupRule mockDataSourceGroupRule() {
         ReadwriteSplittingDataSourceGroupRule result = mock(ReadwriteSplittingDataSourceGroupRule.class, RETURNS_DEEP_STUBS);
+        when(result.getName()).thenReturn("foo_rule");
         when(result.getReadwriteSplittingGroup().getReadDataSources()).thenReturn(Arrays.asList("read_ds_0", "read_ds_1"));
         when(result.getDisabledDataSourceNames()).thenReturn(Collections.singleton("read_ds_1"));
         return result;
     }
+    
+    private ContextManager mockContextManager(final ReadwriteSplittingRule rule) {
+        ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
+        when(database.getName()).thenReturn("foo_db");
+        when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule)));
+        ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
+        when(result.getDatabase("foo_db")).thenReturn(database);
+        return result;
+    }
 }