From 59bfdcf64490d4e55410118778bd7f64178f80fc Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Sun, 3 Nov 2024 20:30:40 +0800 Subject: [PATCH] Add more test cases on ShadowRuleConfigurationChecker (#33514) --- .../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 a001e794d9348..8bb90cb1b1482 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.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 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 dataSourceMap, final Collection 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 @@ private void checkShadowAlgorithms(final Map sha shadowAlgorithmConfigs.values().forEach(each -> TypedSPILoader.checkService(ShadowAlgorithm.class, each.getType(), each.getProps())); } + private void checkDefaultShadowAlgorithm(final String defaultShadowAlgorithmName, final Map 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 shadowDataSources, final Map dataSourceMap, final String databaseName) { for (ShadowDataSourceConfiguration each : shadowDataSources) { ShardingSpherePreconditions.checkContainsKey(dataSourceMap, each.getProductionDataSourceName(), () -> new MissingRequiredProductionDataSourceException(databaseName)); @@ -74,13 +82,6 @@ private void checkShadowTableDataSourcesReferences(final Map 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 shadowTables, final Map shadowAlgorithms, final String databaseName) { for (ShadowTableConfiguration each : shadowTables.values()) { ShardingSpherePreconditions.checkNotEmpty(each.getShadowAlgorithmNames(), () -> new MissingRequiredAlgorithmException("Shadow", new SQLExceptionIdentifier(databaseName))); @@ -92,14 +93,10 @@ private void checkShadowTableAlgorithmsReferences(final Map getRequiredDataSourceNames(final ShadowRuleConfiguration ruleConfig) { Collection 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 f34a568a39f07..389cdadf5528a 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 createDataSourceMap() { @@ -46,16 +70,8 @@ private Map createDataSourceMap() { 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")))); } }