Skip to content

Commit

Permalink
Add more test cases on ShadowRuleConfigurationChecker (#33514)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Nov 3, 2024
1 parent 1b4b77a commit 59bfdcf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@
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;

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;

/**
Expand All @@ -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);
Expand All @@ -58,6 +59,13 @@ private void checkShadowAlgorithms(final Map<String, AlgorithmConfiguration> sha
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));
Expand All @@ -74,13 +82,6 @@ private void checkShadowTableDataSourcesReferences(final Map<String, ShadowTable
});
}

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)));
Expand All @@ -92,14 +93,10 @@ private void checkShadowTableAlgorithmsReferences(final Map<String, ShadowTableC
@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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -46,16 +70,8 @@ private Map<String, DataSource> 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"))));
}
}

0 comments on commit 59bfdcf

Please sign in to comment.