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 59bfdcf6449 Add more test cases on ShadowRuleConfigurationChecker 
(#33514)
59bfdcf6449 is described below

commit 59bfdcf64490d4e55410118778bd7f64178f80fc
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Nov 3 20:30:40 2024 +0800

    Add more test cases on ShadowRuleConfigurationChecker (#33514)
---
 .../checker/ShadowRuleConfigurationChecker.java    | 31 +++++++--------
 .../ShadowRuleConfigurationCheckerTest.java        | 44 +++++++++++++++-------
 2 files changed, 44 insertions(+), 31 deletions(-)

diff --git 
a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java
 
b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java
index a001e794d93..8bb90cb1b14 100644
--- 
a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java
+++ 
b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java
@@ -28,9 +28,9 @@ 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.constant.ShadowOrder;
-import 
org.apache.shardingsphere.shadow.exception.metadata.NotImplementHintShadowAlgorithmException;
 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.shadow.exception.metadata.ShadowDataSourceMappingNotFoundException;
 import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
 
@@ -38,6 +38,7 @@ import javax.sql.DataSource;
 import java.util.Collection;
 import java.util.LinkedHashSet;
 import java.util.Map;
+import java.util.Optional;
 import java.util.stream.Collectors;
 
 /**
@@ -48,7 +49,7 @@ public final class ShadowRuleConfigurationChecker implements 
RuleConfigurationCh
     @Override
     public void check(final String databaseName, final ShadowRuleConfiguration 
ruleConfig, final Map<String, DataSource> dataSourceMap, final 
Collection<ShardingSphereRule> builtRules) {
         checkShadowAlgorithms(ruleConfig.getShadowAlgorithms());
-        
checkDefaultShadowAlgorithmConfiguration(ruleConfig.getDefaultShadowAlgorithmName(),
 ruleConfig.getShadowAlgorithms());
+        
checkDefaultShadowAlgorithm(ruleConfig.getDefaultShadowAlgorithmName(), 
ruleConfig.getShadowAlgorithms());
         checkDataSources(ruleConfig.getDataSources(), dataSourceMap, 
databaseName);
         checkShadowTableDataSourcesReferences(ruleConfig.getTables(), 
ruleConfig.getDataSources());
         checkShadowTableAlgorithmsReferences(ruleConfig.getTables(), 
ruleConfig.getShadowAlgorithms(), databaseName);
@@ -58,6 +59,13 @@ public final class ShadowRuleConfigurationChecker implements 
RuleConfigurationCh
         shadowAlgorithmConfigs.values().forEach(each -> 
TypedSPILoader.checkService(ShadowAlgorithm.class, each.getType(), 
each.getProps()));
     }
     
+    private void checkDefaultShadowAlgorithm(final String 
defaultShadowAlgorithmName, final Map<String, AlgorithmConfiguration> 
shadowAlgorithmConfigs) {
+        if (null != defaultShadowAlgorithmName) {
+            AlgorithmConfiguration algorithmConfig = 
shadowAlgorithmConfigs.get(defaultShadowAlgorithmName);
+            ShardingSpherePreconditions.checkState(null != algorithmConfig && 
"SQL_HINT".equals(algorithmConfig.getType()), 
NotImplementHintShadowAlgorithmException::new);
+        }
+    }
+    
     private void checkDataSources(final 
Collection<ShadowDataSourceConfiguration> shadowDataSources, final Map<String, 
DataSource> dataSourceMap, final String databaseName) {
         for (ShadowDataSourceConfiguration each : shadowDataSources) {
             ShardingSpherePreconditions.checkContainsKey(dataSourceMap, 
each.getProductionDataSourceName(), () -> new 
MissingRequiredProductionDataSourceException(databaseName));
@@ -74,13 +82,6 @@ public final class ShadowRuleConfigurationChecker implements 
RuleConfigurationCh
         });
     }
     
-    private void checkDefaultShadowAlgorithmConfiguration(final String 
defaultShadowAlgorithmName, final Map<String, AlgorithmConfiguration> 
shadowAlgorithmConfigs) {
-        if (null != defaultShadowAlgorithmName) {
-            AlgorithmConfiguration algorithmConfig = 
shadowAlgorithmConfigs.get(defaultShadowAlgorithmName);
-            ShardingSpherePreconditions.checkState(null != algorithmConfig && 
"SQL_HINT".equals(algorithmConfig.getType()), 
NotImplementHintShadowAlgorithmException::new);
-        }
-    }
-    
     private void checkShadowTableAlgorithmsReferences(final Map<String, 
ShadowTableConfiguration> shadowTables, final Map<String, 
AlgorithmConfiguration> shadowAlgorithms, final String databaseName) {
         for (ShadowTableConfiguration each : shadowTables.values()) {
             
ShardingSpherePreconditions.checkNotEmpty(each.getShadowAlgorithmNames(), () -> 
new MissingRequiredAlgorithmException("Shadow", new 
SQLExceptionIdentifier(databaseName)));
@@ -92,14 +93,10 @@ public final class ShadowRuleConfigurationChecker 
implements RuleConfigurationCh
     @Override
     public Collection<String> getRequiredDataSourceNames(final 
ShadowRuleConfiguration ruleConfig) {
         Collection<String> result = new LinkedHashSet<>();
-        ruleConfig.getDataSources().forEach(each -> {
-            if (null != each.getShadowDataSourceName()) {
-                result.add(each.getShadowDataSourceName());
-            }
-            if (null != each.getProductionDataSourceName()) {
-                result.add(each.getProductionDataSourceName());
-            }
-        });
+        for (ShadowDataSourceConfiguration each : ruleConfig.getDataSources()) 
{
+            
Optional.ofNullable(each.getShadowDataSourceName()).ifPresent(result::add);
+            
Optional.ofNullable(each.getProductionDataSourceName()).ifPresent(result::add);
+        }
         return result;
     }
     
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 f34a568a39f..389cdadf552 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,25 +18,49 @@
 package org.apache.shardingsphere.shadow.checker;
 
 import 
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+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.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;
 
 import javax.sql.DataSource;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
 
 class ShadowRuleConfigurationCheckerTest {
     
+    private ShadowRuleConfigurationChecker ruleConfigChecker;
+    
+    @BeforeEach
+    void setUp() {
+        ruleConfigChecker = (ShadowRuleConfigurationChecker) 
OrderedSPILoader.getServicesByClass(
+                RuleConfigurationChecker.class, 
Collections.singleton(ShadowRuleConfiguration.class)).get(ShadowRuleConfiguration.class);
+    }
+    
     @Test
     void assertCheck() {
-        new ShadowRuleConfigurationChecker().check("", 
createShadowRuleConfiguration(), createDataSourceMap(), 
Collections.emptyList());
+        ruleConfigChecker.check("", 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")))));
+        return result;
     }
     
     private Map<String, DataSource> createDataSourceMap() {
@@ -46,16 +70,8 @@ class ShadowRuleConfigurationCheckerTest {
         return result;
     }
     
-    private ShadowRuleConfiguration createShadowRuleConfiguration() {
-        ShadowRuleConfiguration result = new ShadowRuleConfiguration();
-        
result.setShadowAlgorithms(Collections.singletonMap("user-id-insert-match-algorithm",
 createAlgorithmConfiguration()));
-        result.setDataSources(Collections.singleton(new 
ShadowDataSourceConfiguration("shadow-data-source", "ds", "ds_shadow")));
-        result.setTables(Collections.singletonMap("t_order", new 
ShadowTableConfiguration(new LinkedList<>(), new 
LinkedList<>(Collections.singleton("user-id-insert-match-algorithm")))));
-        return result;
-    }
-    
-    private AlgorithmConfiguration createAlgorithmConfiguration() {
-        return new AlgorithmConfiguration("REGEX_MATCH",
-                PropertiesBuilder.build(new Property("column", "shadow"), new 
Property("operation", "insert"), new Property("regex", "[1]")));
+    @Test
+    void assertGetRequiredDataSourceNames() {
+        
assertThat(ruleConfigChecker.getRequiredDataSourceNames(createRuleConfiguration()),
 is(new LinkedHashSet<>(Arrays.asList("ds_shadow", "ds"))));
     }
 }

Reply via email to