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 f732e40d3e3 Add more test cases on 
ReadwriteSplittingRuleStatementCheckerTest (#38255)
f732e40d3e3 is described below

commit f732e40d3e3797ae7407fea06835ee32b74b4881
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Feb 27 23:42:24 2026 +0800

    Add more test cases on ReadwriteSplittingRuleStatementCheckerTest (#38255)
---
 .../ReadwriteSplittingRuleStatementChecker.java    |   4 +-
 ...ReadwriteSplittingRuleStatementCheckerTest.java | 162 +++++++++++++++++++++
 2 files changed, 164 insertions(+), 2 deletions(-)

diff --git 
a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementChecker.java
 
b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementChecker.java
index 8e5fb42b6f7..002bb7d5910 100644
--- 
a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementChecker.java
+++ 
b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementChecker.java
@@ -61,7 +61,7 @@ public final class ReadwriteSplittingRuleStatementChecker {
      *
      * @param database database
      * @param segments segments
-     * @param currentRuleConfig current rule config
+     * @param currentRuleConfig current rule configuration
      * @param ifNotExists rule if not exists
      */
     public static void checkCreation(final ShardingSphereDatabase database, 
final Collection<ReadwriteSplittingRuleSegment> segments,
@@ -79,7 +79,7 @@ public final class ReadwriteSplittingRuleStatementChecker {
      *
      * @param database database
      * @param segments segments
-     * @param currentRuleConfig current rule config
+     * @param currentRuleConfig current rule configuration
      */
     public static void checkAlteration(final ShardingSphereDatabase database, 
final Collection<ReadwriteSplittingRuleSegment> segments, final 
ReadwriteSplittingRuleConfiguration currentRuleConfig) {
         String databaseName = database.getName();
diff --git 
a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementCheckerTest.java
 
b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementCheckerTest.java
new file mode 100644
index 00000000000..b47c3e381bd
--- /dev/null
+++ 
b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementCheckerTest.java
@@ -0,0 +1,162 @@
+/*
+ * 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.readwritesplitting.distsql.handler.checker;
+
+import org.apache.shardingsphere.distsql.segment.AlgorithmSegment;
+import 
org.apache.shardingsphere.infra.algorithm.core.exception.InvalidAlgorithmConfigurationException;
+import 
org.apache.shardingsphere.infra.exception.kernel.metadata.rule.DuplicateRuleException;
+import 
org.apache.shardingsphere.infra.exception.kernel.metadata.rule.MissingRequiredStrategyException;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import 
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
+import 
org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.infra.util.props.PropertiesBuilder;
+import org.apache.shardingsphere.infra.util.props.PropertiesBuilder.Property;
+import 
org.apache.shardingsphere.readwritesplitting.config.ReadwriteSplittingRuleConfiguration;
+import 
org.apache.shardingsphere.readwritesplitting.config.rule.ReadwriteSplittingDataSourceGroupRuleConfiguration;
+import 
org.apache.shardingsphere.readwritesplitting.distsql.segment.ReadwriteSplittingRuleSegment;
+import 
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import 
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.Answers;
+import org.mockito.Mock;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Properties;
+import java.util.stream.Stream;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.lenient;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(TypedSPILoader.class)
+class ReadwriteSplittingRuleStatementCheckerTest {
+    
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private ShardingSphereDatabase database;
+    
+    @Mock
+    private ResourceMetaData resourceMetaData;
+    
+    @BeforeEach
+    void setUp() {
+        when(database.getName()).thenReturn("foo_db");
+        
when(database.getRuleMetaData().getAttributes(DataSourceMapperRuleAttribute.class)).thenReturn(Collections.emptyList());
+        
lenient().when(resourceMetaData.getStorageUnits()).thenReturn(Collections.emptyMap());
+        
lenient().when(resourceMetaData.getNotExistedDataSources(any())).thenReturn(Collections.emptySet());
+        when(database.getResourceMetaData()).thenReturn(resourceMetaData);
+    }
+    
+    @Test
+    void assertCheckCreationWithDuplicateRuleNames() {
+        Collection<ReadwriteSplittingRuleSegment> segments = Arrays.asList(new 
ReadwriteSplittingRuleSegment("foo_rule_0", "write_ds_0", 
Arrays.asList("read_ds_0", "read_ds_1"), null, null),
+                new ReadwriteSplittingRuleSegment("foo_rule_0", "write_ds_2", 
Arrays.asList("read_ds_2", "read_ds_3"), null, null),
+                new ReadwriteSplittingRuleSegment("bar_rule_0", "write_ds_4", 
Arrays.asList("read_ds_4", "read_ds_5"), null, null));
+        assertThrows(DuplicateRuleException.class, () -> 
ReadwriteSplittingRuleStatementChecker.checkCreation(database, segments, null, 
false));
+    }
+    
+    @Test
+    void assertCheckCreationWithNullResourceMetaData() {
+        when(database.getResourceMetaData()).thenReturn(null);
+        ReadwriteSplittingRuleSegment segment = new 
ReadwriteSplittingRuleSegment("foo_rule_0", "write_ds_0", 
Arrays.asList("read_ds_0", "read_ds_1"), null, null);
+        assertThrows(NullPointerException.class, () -> 
ReadwriteSplittingRuleStatementChecker.checkCreation(database, 
Collections.singleton(segment), null, false));
+    }
+    
+    @Test
+    void assertCheckCreationWithNullStorageUnits() {
+        when(resourceMetaData.getStorageUnits()).thenReturn(null);
+        ReadwriteSplittingRuleSegment segment = new 
ReadwriteSplittingRuleSegment("foo_rule_0", "write_ds_0", 
Arrays.asList("read_ds_0", "read_ds_1"), null, null);
+        assertDoesNotThrow(() -> 
ReadwriteSplittingRuleStatementChecker.checkCreation(database, 
Collections.singleton(segment), null, false));
+    }
+    
+    @Test
+    void assertCheckCreationWithEmptyWriteDataSource() {
+        ReadwriteSplittingRuleSegment segment = new 
ReadwriteSplittingRuleSegment("foo_rule_0", "", 
Collections.singleton("read_ds_0"), null, null);
+        assertDoesNotThrow(() -> 
ReadwriteSplittingRuleStatementChecker.checkCreation(database, 
Collections.singleton(segment), null, false));
+    }
+    
+    @Test
+    void assertCheckCreationWithInvalidTransactionalReadQueryStrategy() {
+        ReadwriteSplittingRuleSegment segment = new 
ReadwriteSplittingRuleSegment("foo_rule_0", "write_ds_0", 
Arrays.asList("read_ds_0", "read_ds_1"), "invalid", null);
+        assertThrows(MissingRequiredStrategyException.class, () -> 
ReadwriteSplittingRuleStatementChecker.checkCreation(database, 
Collections.singleton(segment), null, false));
+    }
+    
+    @Test
+    void assertCheckCreationWithValidTransactionalReadQueryStrategy() {
+        ReadwriteSplittingRuleSegment segment = new 
ReadwriteSplittingRuleSegment("foo_rule_0", "write_ds_0", 
Arrays.asList("read_ds_0", "read_ds_1"), "primary", null);
+        assertDoesNotThrow(() -> 
ReadwriteSplittingRuleStatementChecker.checkCreation(database, 
Collections.singleton(segment), null, false));
+    }
+    
+    @ParameterizedTest(name = "{0}")
+    @MethodSource("invalidWeightConfigurationArguments")
+    void assertCheckCreationWithInvalidWeightConfiguration(final String name, 
final ReadwriteSplittingRuleSegment segment) {
+        assertThrows(InvalidAlgorithmConfigurationException.class, () -> 
ReadwriteSplittingRuleStatementChecker.checkCreation(database, 
Collections.singleton(segment), null, false), name);
+    }
+    
+    @Test
+    void assertCheckCreationWithValidWeightConfiguration() {
+        AlgorithmSegment loadBalancer = new AlgorithmSegment("weight", 
PropertiesBuilder.build(new Property("read_ds_0", "1"), new 
Property("read_ds_1", "1")));
+        ReadwriteSplittingRuleSegment segment = new 
ReadwriteSplittingRuleSegment("foo_rule_0", "write_ds_0", 
Arrays.asList("read_ds_0", "read_ds_1"), null, loadBalancer);
+        assertDoesNotThrow(() -> 
ReadwriteSplittingRuleStatementChecker.checkCreation(database, 
Collections.singleton(segment), null, false));
+    }
+    
+    @Test
+    void assertCheckCreationWithNullReadDataSourcesForDuplicateCheck() {
+        ReadwriteSplittingRuleSegment segment = 
mock(ReadwriteSplittingRuleSegment.class);
+        when(segment.getName()).thenReturn("foo_rule_0");
+        when(segment.getWriteDataSource()).thenReturn("write_ds_0");
+        
when(segment.getReadDataSources()).thenReturn(Collections.singleton("read_ds_0")).thenReturn(null);
+        when(segment.getTransactionalReadQueryStrategy()).thenReturn(null);
+        when(segment.getLoadBalancer()).thenReturn(null);
+        assertDoesNotThrow(() -> 
ReadwriteSplittingRuleStatementChecker.checkCreation(database, 
Collections.singleton(segment), null, false));
+    }
+    
+    @Test
+    void assertCheckAlteration() {
+        ReadwriteSplittingRuleSegment segment = new 
ReadwriteSplittingRuleSegment("foo_rule_0", "write_ds_0", 
Arrays.asList("read_ds_0", "read_ds_1"), null, null);
+        Collection<ReadwriteSplittingDataSourceGroupRuleConfiguration> 
dataSourceGroups = Arrays.asList(
+                new 
ReadwriteSplittingDataSourceGroupRuleConfiguration("foo_rule_0", "write_ds_9", 
Arrays.asList("read_ds_9", "read_ds_10"), "RANDOM"),
+                new 
ReadwriteSplittingDataSourceGroupRuleConfiguration("bar_rule_0", "write_ds_2", 
Arrays.asList("read_ds_2", "read_ds_3"), "RANDOM"));
+        ReadwriteSplittingRuleConfiguration currentRuleConfig = new 
ReadwriteSplittingRuleConfiguration(dataSourceGroups, Collections.emptyMap());
+        assertDoesNotThrow(() -> 
ReadwriteSplittingRuleStatementChecker.checkAlteration(database, 
Collections.singleton(segment), currentRuleConfig));
+    }
+    
+    private static Stream<Arguments> invalidWeightConfigurationArguments() {
+        return Stream.of(
+                Arguments.of("empty weight properties",
+                        new ReadwriteSplittingRuleSegment("foo_rule_0", 
"write_ds_0", Arrays.asList("read_ds_0", "read_ds_1"), null,
+                                new AlgorithmSegment("weight", new 
Properties()))),
+                Arguments.of("weight property has unknown read storage unit",
+                        new ReadwriteSplittingRuleSegment("foo_rule_0", 
"write_ds_0", Arrays.asList("read_ds_0", "read_ds_1"), null,
+                                new AlgorithmSegment("weight", 
PropertiesBuilder.build(new Property("read_ds_0", "1"), new 
Property("read_ds_2", "1"))))),
+                Arguments.of("weight property misses required read storage 
unit",
+                        new ReadwriteSplittingRuleSegment("foo_rule_0", 
"write_ds_0", Arrays.asList("read_ds_0", "read_ds_1"), null,
+                                new AlgorithmSegment("weight", 
PropertiesBuilder.build(new Property("read_ds_0", "1"))))));
+    }
+}

Reply via email to