Skip to content

Commit

Permalink
Add more test cases on ShadowRuleConfigurationChecker (#33515)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Nov 3, 2024
1 parent 59bfdcf commit 0c8d337
Showing 1 changed file with 104 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,6 +44,8 @@

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 {

Expand All @@ -50,28 +58,115 @@ void setUp() {
}

@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"))));
}
}

0 comments on commit 0c8d337

Please sign in to comment.