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 2022/06/28 13:31:06 UTC

[shardingsphere] branch master updated: test: assertion for CreateTrafficRuleHandlerTest and AlterTrafficRuleHandlerTest (#18658)

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 30a933d415c test: assertion for CreateTrafficRuleHandlerTest and AlterTrafficRuleHandlerTest (#18658)
30a933d415c is described below

commit 30a933d415c2b064aa5ea93726a276bc6850432f
Author: SongTao Zhuang <51...@users.noreply.github.com>
AuthorDate: Tue Jun 28 21:30:59 2022 +0800

    test: assertion for CreateTrafficRuleHandlerTest and AlterTrafficRuleHandlerTest (#18658)
---
 .../common/updatable/AlterTrafficRuleHandlerTest.java | 16 ++++++++++++++--
 .../updatable/CreateTrafficRuleHandlerTest.java       | 19 +++++++++++++++++--
 2 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTrafficRuleHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTrafficRuleHandlerTest.java
index e7874a4826e..a30e46cb6f1 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTrafficRuleHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/AlterTrafficRuleHandlerTest.java
@@ -34,11 +34,15 @@ import org.apache.shardingsphere.traffic.rule.TrafficStrategyRule;
 import org.junit.Test;
 
 import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.Properties;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -67,7 +71,7 @@ public final class AlterTrafficRuleHandlerTest extends ProxyContextRestorer {
     
     @Test
     public void assertExecute() throws SQLException {
-        mockContextManager();
+        ContextManager contextManager = mockContextManager();
         TrafficRuleSegment trafficRuleSegment1 = new TrafficRuleSegment(
                 "rule_name_1", Arrays.asList("olap", "order_by"), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
         TrafficRuleSegment trafficRuleSegment2 = new TrafficRuleSegment(
@@ -75,15 +79,23 @@ public final class AlterTrafficRuleHandlerTest extends ProxyContextRestorer {
         AlterTrafficRuleHandler handler = new AlterTrafficRuleHandler();
         handler.init(new AlterTrafficRuleStatement(Arrays.asList(trafficRuleSegment1, trafficRuleSegment2)), null);
         handler.execute();
+        TrafficRuleConfiguration alterConfig = contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class).getConfiguration();
+        assertThat(alterConfig.getTrafficStrategies().size(), is(2));
+        assertThat(alterConfig.getLoadBalancers().size(), is(2));
+        assertThat(alterConfig.getTrafficAlgorithms().size(), is(2));
+        assertThat(new ArrayList<>(alterConfig.getTrafficStrategies()).get(0).getName(), is("rule_name_1"));
+        assertNotNull(alterConfig.getTrafficAlgorithms().get("rule_name_1_distsql.fixture"));
+        assertNotNull(alterConfig.getLoadBalancers().get("rule_name_2_distsql.fixture"));
     }
     
-    private void mockContextManager() {
+    private ContextManager mockContextManager() {
         ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
         TrafficRule rule = mockTrafficRule();
         when(rule.getConfiguration()).thenReturn(createTrafficRuleConfiguration());
         ShardingSphereRuleMetaData globalRuleMetaData = new ShardingSphereRuleMetaData(new LinkedList<>(Collections.singleton(rule)));
         when(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
         ProxyContext.init(contextManager);
+        return contextManager;
     }
     
     private TrafficRule mockTrafficRule() {
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/CreateTrafficRuleHandlerTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/CreateTrafficRuleHandlerTest.java
index 29f7be3e54c..957126d152a 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/CreateTrafficRuleHandlerTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/common/updatable/CreateTrafficRuleHandlerTest.java
@@ -34,11 +34,15 @@ import org.apache.shardingsphere.traffic.rule.TrafficStrategyRule;
 import org.junit.Test;
 
 import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.Properties;
 
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -67,7 +71,7 @@ public final class CreateTrafficRuleHandlerTest extends ProxyContextRestorer {
     
     @Test
     public void assertExecute() throws SQLException {
-        mockContextManager();
+        ContextManager contextManager = mockContextManager();
         TrafficRuleSegment trafficRuleSegment1 = new TrafficRuleSegment(
                 "rule_name_3", Arrays.asList("olap", "order_by"), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()), new AlgorithmSegment("DISTSQL.FIXTURE", new Properties()));
         TrafficRuleSegment trafficRuleSegment2 = new TrafficRuleSegment(
@@ -75,15 +79,26 @@ public final class CreateTrafficRuleHandlerTest extends ProxyContextRestorer {
         CreateTrafficRuleHandler handler = new CreateTrafficRuleHandler();
         handler.init(new CreateTrafficRuleStatement(Arrays.asList(trafficRuleSegment1, trafficRuleSegment2)), null);
         handler.execute();
+        TrafficRuleConfiguration addConfig = contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(TrafficRule.class).getConfiguration();
+        assertThat(addConfig.getTrafficStrategies().size(), is(4));
+        assertThat(addConfig.getLoadBalancers().size(), is(4));
+        assertThat(addConfig.getTrafficAlgorithms().size(), is(4));
+        assertThat(new ArrayList<>(addConfig.getTrafficStrategies()).get(2).getName(), is("rule_name_3"));
+        assertThat(new ArrayList<>(addConfig.getTrafficStrategies()).get(3).getName(), is("rule_name_4"));
+        assertNotNull(addConfig.getTrafficAlgorithms().get("algorithm_2"));
+        assertNotNull(addConfig.getLoadBalancers().get("load_balancer_2"));
+        assertNotNull(addConfig.getTrafficAlgorithms().get("rule_name_3_distsql.fixture"));
+        assertNotNull(addConfig.getLoadBalancers().get("rule_name_4_distsql.fixture"));
     }
     
-    private void mockContextManager() {
+    private ContextManager mockContextManager() {
         ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
         TrafficRule rule = mockTrafficRule();
         when(rule.getConfiguration()).thenReturn(createTrafficRuleConfiguration());
         ShardingSphereRuleMetaData globalRuleMetaData = new ShardingSphereRuleMetaData(new LinkedList<>(Collections.singleton(rule)));
         when(contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
         ProxyContext.init(contextManager);
+        return contextManager;
     }
     
     private TrafficRule mockTrafficRule() {