You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2024/11/07 05:43:30 UTC

(shardingsphere) branch master updated: Add test cases on ReadwriteSplittingDataSourceRuleConfigurationChecker (#33580)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 2456495797d Add test cases on ReadwriteSplittingDataSourceRuleConfigurationChecker (#33580)
2456495797d is described below

commit 2456495797d4accfa97bb11101a04a4c895968e0
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Thu Nov 7 13:43:24 2024 +0800

    Add test cases on ReadwriteSplittingDataSourceRuleConfigurationChecker (#33580)
    
    * Add test cases on ReadwriteSplittingDataSourceRuleConfigurationChecker
    
    * Add test cases on ReadwriteSplittingDataSourceRuleConfigurationChecker
---
 ...tingDataSourceRuleConfigurationCheckerTest.java | 93 ++++++++++++++++++++++
 ...writeSplittingRuleConfigurationCheckerTest.java |  2 +-
 2 files changed, 94 insertions(+), 1 deletion(-)

diff --git a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingDataSourceRuleConfigurationCheckerTest.java b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingDataSourceRuleConfigurationCheckerTest.java
new file mode 100644
index 00000000000..6a230395874
--- /dev/null
+++ b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingDataSourceRuleConfigurationCheckerTest.java
@@ -0,0 +1,93 @@
+/*
+ * 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.checker;
+
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
+import org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;
+import org.apache.shardingsphere.readwritesplitting.config.rule.ReadwriteSplittingDataSourceGroupRuleConfiguration;
+import org.apache.shardingsphere.readwritesplitting.exception.actual.DuplicateReadwriteSplittingActualDataSourceException;
+import org.apache.shardingsphere.readwritesplitting.exception.actual.MissingRequiredReadwriteSplittingActualDataSourceException;
+import org.apache.shardingsphere.readwritesplitting.exception.actual.ReadwriteSplittingActualDataSourceNotFoundException;
+import org.apache.shardingsphere.readwritesplitting.exception.logic.MissingRequiredReadwriteSplittingDataSourceRuleNameException;
+import org.junit.jupiter.api.Test;
+
+import javax.sql.DataSource;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class ReadwriteSplittingDataSourceRuleConfigurationCheckerTest {
+    
+    @Test
+    void assertCheckWithEmptyGroupName() {
+        ReadwriteSplittingDataSourceGroupRuleConfiguration config = new ReadwriteSplittingDataSourceGroupRuleConfiguration("", "write_ds", Arrays.asList("read_ds0", "read_ds1"), "foo_algo");
+        assertThrows(MissingRequiredReadwriteSplittingDataSourceRuleNameException.class, () -> new ReadwriteSplittingDataSourceRuleConfigurationChecker(
+                "foo_db", config, Collections.emptyMap()).check(Collections.emptyList(), Collections.emptyList(), Collections.emptyList()));
+    }
+    
+    @Test
+    void assertCheckWithEmptyWriteDataSourceName() {
+        ReadwriteSplittingDataSourceGroupRuleConfiguration config = new ReadwriteSplittingDataSourceGroupRuleConfiguration("foo_group", "", Arrays.asList("read_ds0", "read_ds1"), "foo_algo");
+        assertThrows(MissingRequiredReadwriteSplittingActualDataSourceException.class, () -> new ReadwriteSplittingDataSourceRuleConfigurationChecker(
+                "foo_db", config, Collections.emptyMap()).check(Collections.emptyList(), Collections.emptyList(), Collections.emptyList()));
+    }
+    
+    @Test
+    void assertCheckWithEmptyReadDataSourceNames() {
+        ReadwriteSplittingDataSourceGroupRuleConfiguration config = new ReadwriteSplittingDataSourceGroupRuleConfiguration("foo_group", "write_ds", Collections.emptyList(), "foo_algo");
+        assertThrows(MissingRequiredReadwriteSplittingActualDataSourceException.class, () -> new ReadwriteSplittingDataSourceRuleConfigurationChecker(
+                "foo_db", config, Collections.emptyMap()).check(Collections.emptyList(), Collections.emptyList(), Collections.emptyList()));
+    }
+    
+    @Test
+    void assertCheckWithNotExistedWriteDataSourceName() {
+        ReadwriteSplittingDataSourceGroupRuleConfiguration config = new ReadwriteSplittingDataSourceGroupRuleConfiguration("foo_group", "write_ds", Arrays.asList("read_ds0", "read_ds1"), "foo_algo");
+        assertThrows(ReadwriteSplittingActualDataSourceNotFoundException.class, () -> new ReadwriteSplittingDataSourceRuleConfigurationChecker(
+                "foo_db", config, Collections.emptyMap()).check(Collections.emptyList(), Collections.emptyList(), Collections.emptyList()));
+    }
+    
+    @Test
+    void assertCheckWithDuplicatedWriteDataSourceName() {
+        ReadwriteSplittingDataSourceGroupRuleConfiguration config = new ReadwriteSplittingDataSourceGroupRuleConfiguration("foo_group", "write_ds", Arrays.asList("read_ds0", "read_ds1"), "foo_algo");
+        assertThrows(DuplicateReadwriteSplittingActualDataSourceException.class, () -> new ReadwriteSplittingDataSourceRuleConfigurationChecker("foo_db",
+                config, Collections.singletonMap("write_ds", mock(DataSource.class))).check(new HashSet<>(Collections.singleton("write_ds")), Collections.emptyList(), Collections.emptyList()));
+    }
+    
+    @Test
+    void assertCheckSuccess() {
+        ReadwriteSplittingDataSourceGroupRuleConfiguration config = new ReadwriteSplittingDataSourceGroupRuleConfiguration("foo_group", "write_ds", Arrays.asList("read_ds0", "read_ds1"), "foo_algo");
+        assertDoesNotThrow(() -> new ReadwriteSplittingDataSourceRuleConfigurationChecker("foo_db", config,
+                Collections.singletonMap("write_ds", mock(DataSource.class))).check(new HashSet<>(), new HashSet<>(), Arrays.asList(mock(ShardingSphereRule.class, RETURNS_DEEP_STUBS), mockRule())));
+    }
+    
+    private static ShardingSphereRule mockRule() {
+        ShardingSphereRule result = mock(ShardingSphereRule.class);
+        DataSourceMapperRuleAttribute dataSourceMapperRuleAttribute = mock(DataSourceMapperRuleAttribute.class, RETURNS_DEEP_STUBS);
+        when(dataSourceMapperRuleAttribute.getDataSourceMapper().containsKey("read_ds0")).thenReturn(true);
+        when(dataSourceMapperRuleAttribute.getDataSourceMapper().containsKey("read_ds1")).thenReturn(true);
+        when(result.getAttributes()).thenReturn(new RuleAttributes(dataSourceMapperRuleAttribute));
+        return result;
+    }
+}
diff --git a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationCheckerTest.java b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationCheckerTest.java
index dcd082f684a..7e8e3fc4034 100644
--- a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationCheckerTest.java
+++ b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/checker/ReadwriteSplittingRuleConfigurationCheckerTest.java
@@ -26,9 +26,9 @@ import org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMappe
 import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
 import org.apache.shardingsphere.readwritesplitting.config.ReadwriteSplittingRuleConfiguration;
 import org.apache.shardingsphere.readwritesplitting.config.rule.ReadwriteSplittingDataSourceGroupRuleConfiguration;
-import org.apache.shardingsphere.readwritesplitting.exception.actual.ReadwriteSplittingActualDataSourceNotFoundException;
 import org.apache.shardingsphere.readwritesplitting.exception.actual.DuplicateReadwriteSplittingActualDataSourceException;
 import org.apache.shardingsphere.readwritesplitting.exception.actual.MissingRequiredReadwriteSplittingActualDataSourceException;
+import org.apache.shardingsphere.readwritesplitting.exception.actual.ReadwriteSplittingActualDataSourceNotFoundException;
 import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
 import org.apache.shardingsphere.test.util.PropertiesBuilder;
 import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;