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 0c8d3374049 Add more test cases on ShadowRuleConfigurationChecker 
(#33515)
0c8d3374049 is described below

commit 0c8d33740496104d6018ca035328199ab8684cd5
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Nov 3 21:32:16 2024 +0800

    Add more test cases on ShadowRuleConfigurationChecker (#33515)
---
 .../ShadowRuleConfigurationCheckerTest.java        | 113 +++++++++++++++++++--
 1 file changed, 104 insertions(+), 9 deletions(-)

diff --git 
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationCheckerTest.java
 
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationCheckerTest.java
index 389cdadf552..cbe581d5484 100644
--- 
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationCheckerTest.java
+++ 
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationCheckerTest.java
@@ -18,12 +18,18 @@
 package org.apache.shardingsphere.shadow.checker;
 
 import 
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import 
org.apache.shardingsphere.infra.algorithm.core.exception.MissingRequiredAlgorithmException;
 import 
org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker;
 import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
 import org.apache.shardingsphere.shadow.config.ShadowRuleConfiguration;
 import 
org.apache.shardingsphere.shadow.config.datasource.ShadowDataSourceConfiguration;
 import org.apache.shardingsphere.shadow.config.table.ShadowTableConfiguration;
+import 
org.apache.shardingsphere.shadow.exception.metadata.MissingRequiredProductionDataSourceException;
+import 
org.apache.shardingsphere.shadow.exception.metadata.MissingRequiredShadowDataSourceException;
+import 
org.apache.shardingsphere.shadow.exception.metadata.NotImplementHintShadowAlgorithmException;
 import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
+import org.apache.shardingsphere.test.util.PropertiesBuilder;
+import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
@@ -38,6 +44,8 @@ import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 
 class ShadowRuleConfigurationCheckerTest {
     
@@ -50,28 +58,115 @@ class ShadowRuleConfigurationCheckerTest {
     }
     
     @Test
-    void assertCheck() {
-        ruleConfigChecker.check("", createRuleConfiguration(), 
createDataSourceMap(), Collections.emptyList());
+    void assertCheckWithNotExistedDefaultShadowAlgorithm() {
+        assertThrows(NotImplementHintShadowAlgorithmException.class,
+                () -> ruleConfigChecker.check("foo_db", 
createRuleConfigurationWithNotExistedDefaultShadowAlgorithm(), 
createDataSourceMap(), Collections.emptyList()));
+    }
+    
+    private ShadowRuleConfiguration 
createRuleConfigurationWithNotExistedDefaultShadowAlgorithm() {
+        ShadowRuleConfiguration result = new ShadowRuleConfiguration();
+        result.setShadowAlgorithms(Collections.singletonMap("foo-algo", new 
AlgorithmConfiguration("SQL_HINT", new Properties())));
+        result.setDefaultShadowAlgorithmName("bar-algo");
+        result.setDataSources(Collections.singleton(new 
ShadowDataSourceConfiguration("foo_ds", "prod_ds", "shadow_ds")));
+        result.setTables(Collections.singletonMap("foo_tbl", new 
ShadowTableConfiguration(Collections.singletonList("foo_ds"), new 
LinkedList<>(Collections.singleton("foo-algo")))));
+        return result;
+    }
+    
+    @Test
+    void assertCheckWithInvalidDefaultShadowAlgorithm() {
+        assertThrows(NotImplementHintShadowAlgorithmException.class,
+                () -> ruleConfigChecker.check("foo_db", 
createRuleConfigurationWithInvalidDefaultShadowAlgorithm(), 
createDataSourceMap(), Collections.emptyList()));
+    }
+    
+    private ShadowRuleConfiguration 
createRuleConfigurationWithInvalidDefaultShadowAlgorithm() {
+        ShadowRuleConfiguration result = new ShadowRuleConfiguration();
+        result.setShadowAlgorithms(Collections.singletonMap("foo-algo", new 
AlgorithmConfiguration("REGEX_MATCH",
+                PropertiesBuilder.build(new Property("column", "foo_id"), new 
Property("operation", "insert"), new Property("regex", "[1]")))));
+        result.setDefaultShadowAlgorithmName("foo-algo");
+        result.setDataSources(Collections.singleton(new 
ShadowDataSourceConfiguration("foo_ds", "prod_ds", "shadow_ds")));
+        result.setTables(Collections.singletonMap("foo_tbl", new 
ShadowTableConfiguration(Collections.singletonList("foo_ds"), new 
LinkedList<>(Collections.singleton("foo-algo")))));
+        return result;
+    }
+    
+    @Test
+    void assertCheckWithInvalidShadowTableAlgorithmsReferences() {
+        assertThrows(MissingRequiredAlgorithmException.class,
+                () -> ruleConfigChecker.check("foo_db", 
createRuleConfigurationWithInvalidShadowTableAlgorithmsReferences(), 
createDataSourceMap(), Collections.emptyList()));
+    }
+    
+    private ShadowRuleConfiguration 
createRuleConfigurationWithInvalidShadowTableAlgorithmsReferences() {
+        ShadowRuleConfiguration result = new ShadowRuleConfiguration();
+        result.setShadowAlgorithms(Collections.singletonMap("foo-algo", new 
AlgorithmConfiguration("SQL_HINT", new Properties())));
+        result.setDefaultShadowAlgorithmName("foo-algo");
+        result.setDataSources(Collections.singleton(new 
ShadowDataSourceConfiguration("foo_ds", "prod_ds", "shadow_ds")));
+        result.setTables(Collections.singletonMap("foo_tbl", new 
ShadowTableConfiguration(Collections.singletonList("foo_ds"), new 
LinkedList<>(Collections.singleton("bar-algo")))));
+        return result;
+    }
+    
+    @Test
+    void assertCheckWithoutProductionDataSourceName() {
+        assertThrows(MissingRequiredProductionDataSourceException.class,
+                () -> ruleConfigChecker.check("foo_db", 
createRuleConfigurationWithoutProductionDataSourceName(), 
createDataSourceMap(), Collections.emptyList()));
+    }
+    
+    private ShadowRuleConfiguration 
createRuleConfigurationWithoutProductionDataSourceName() {
+        ShadowRuleConfiguration result = new ShadowRuleConfiguration();
+        result.setShadowAlgorithms(Collections.singletonMap("foo-algo", new 
AlgorithmConfiguration("SQL_HINT", new Properties())));
+        result.setDataSources(Collections.singleton(new 
ShadowDataSourceConfiguration("foo_ds", "no_prod_ds", "shadow_ds")));
+        result.setTables(Collections.singletonMap("foo_tbl", new 
ShadowTableConfiguration(Collections.singletonList("foo_ds"), new 
LinkedList<>(Collections.singleton("foo-algo")))));
+        return result;
+    }
+    
+    @Test
+    void assertCheckWithoutShadowDataSourceName() {
+        assertThrows(MissingRequiredShadowDataSourceException.class,
+                () -> ruleConfigChecker.check("foo_db", 
createRuleConfigurationWithoutShadowDataSourceName(), createDataSourceMap(), 
Collections.emptyList()));
+    }
+    
+    private ShadowRuleConfiguration 
createRuleConfigurationWithoutShadowDataSourceName() {
+        ShadowRuleConfiguration result = new ShadowRuleConfiguration();
+        result.setShadowAlgorithms(Collections.singletonMap("foo-algo", new 
AlgorithmConfiguration("SQL_HINT", new Properties())));
+        result.setDataSources(Collections.singleton(new 
ShadowDataSourceConfiguration("foo_ds", "prod_ds", "no_shadow_ds")));
+        result.setTables(Collections.singletonMap("foo_tbl", new 
ShadowTableConfiguration(Collections.singletonList("foo_ds"), new 
LinkedList<>(Collections.singleton("foo-algo")))));
+        return result;
+    }
+    
+    @Test
+    void assertCheckWithoutDefaultShadowAlgorithm() {
+        assertDoesNotThrow(() -> ruleConfigChecker.check("foo_db", 
createRuleConfigurationWithoutDefaultShadowAlgorithm(), createDataSourceMap(), 
Collections.emptyList()));
+    }
+    
+    private ShadowRuleConfiguration 
createRuleConfigurationWithoutDefaultShadowAlgorithm() {
+        ShadowRuleConfiguration result = new ShadowRuleConfiguration();
+        result.setShadowAlgorithms(Collections.singletonMap("foo-algo", new 
AlgorithmConfiguration("SQL_HINT", new Properties())));
+        result.setDataSources(Collections.singleton(new 
ShadowDataSourceConfiguration("foo_ds", "prod_ds", "shadow_ds")));
+        result.setTables(Collections.singletonMap("foo_tbl", new 
ShadowTableConfiguration(Collections.singletonList("foo_ds"), new 
LinkedList<>(Collections.singleton("foo-algo")))));
+        return result;
+    }
+    
+    @Test
+    void assertCheckWithDefaultShadowAlgorithm() {
+        assertDoesNotThrow(() -> ruleConfigChecker.check("foo_db", 
createRuleConfiguration(), createDataSourceMap(), Collections.emptyList()));
     }
     
     private ShadowRuleConfiguration createRuleConfiguration() {
         ShadowRuleConfiguration result = new ShadowRuleConfiguration();
-        result.setShadowAlgorithms(Collections.singletonMap("hint-algorithm", 
new AlgorithmConfiguration("SQL_HINT", new Properties())));
-        result.setDefaultShadowAlgorithmName("hint-algorithm");
-        result.setDataSources(Collections.singleton(new 
ShadowDataSourceConfiguration("foo_ds", "ds", "ds_shadow")));
-        result.setTables(Collections.singletonMap("foo_tbl", new 
ShadowTableConfiguration(Collections.singletonList("foo_ds"), new 
LinkedList<>(Collections.singleton("hint-algorithm")))));
+        result.setShadowAlgorithms(Collections.singletonMap("foo-algo", new 
AlgorithmConfiguration("SQL_HINT", new Properties())));
+        result.setDefaultShadowAlgorithmName("foo-algo");
+        result.setDataSources(Collections.singleton(new 
ShadowDataSourceConfiguration("foo_ds", "prod_ds", "shadow_ds")));
+        result.setTables(Collections.singletonMap("foo_tbl", new 
ShadowTableConfiguration(Collections.singletonList("foo_ds"), new 
LinkedList<>(Collections.singleton("foo-algo")))));
         return result;
     }
     
     private Map<String, DataSource> createDataSourceMap() {
         Map<String, DataSource> result = new LinkedHashMap<>(2, 1F);
-        result.put("ds", new MockedDataSource());
-        result.put("ds_shadow", new MockedDataSource());
+        result.put("prod_ds", new MockedDataSource());
+        result.put("shadow_ds", new MockedDataSource());
         return result;
     }
     
     @Test
     void assertGetRequiredDataSourceNames() {
-        
assertThat(ruleConfigChecker.getRequiredDataSourceNames(createRuleConfiguration()),
 is(new LinkedHashSet<>(Arrays.asList("ds_shadow", "ds"))));
+        
assertThat(ruleConfigChecker.getRequiredDataSourceNames(createRuleConfiguration()),
 is(new LinkedHashSet<>(Arrays.asList("shadow_ds", "prod_ds"))));
     }
 }

Reply via email to