Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more test cases on ShardingRuleConfigurationChecker #33710

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private void checkShardingStrategy(final String databaseName, final ShardingStra
}
ShardingSpherePreconditions.checkNotNull(shardingStrategy.getShardingAlgorithmName(), () -> new MissingRequiredShardingConfigurationException("Sharding algorithm name", databaseName));
ShardingSpherePreconditions.checkContains(shardingAlgorithms, shardingStrategy.getShardingAlgorithmName(),
() -> new UnregisteredAlgorithmException("Key generate", shardingStrategy.getShardingAlgorithmName(), new SQLExceptionIdentifier(databaseName)));
() -> new UnregisteredAlgorithmException("sharding", shardingStrategy.getShardingAlgorithmName(), new SQLExceptionIdentifier(databaseName)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,126 @@
package org.apache.shardingsphere.sharding.checker;

import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.exception.UnregisteredAlgorithmException;
import org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker;
import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ComplexShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.exception.metadata.MissingRequiredShardingConfigurationException;
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 java.util.Arrays;
import java.util.Collections;
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.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class ShardingRuleConfigurationCheckerTest {

@SuppressWarnings("unchecked")
private ShardingRuleConfigurationChecker checker;

@BeforeEach
void setUp() {
checker = (ShardingRuleConfigurationChecker) OrderedSPILoader.getServicesByClass(
RuleConfigurationChecker.class, Collections.singleton(ShardingRuleConfiguration.class)).get(ShardingRuleConfiguration.class);
}

@Test
void assertCheckSuccess() {
ShardingRuleConfiguration ruleConfig = createRuleConfiguration();
ShardingAuditStrategyConfiguration shardingAuditStrategyConfig = new ShardingAuditStrategyConfiguration(Collections.singletonList("foo_audit"), false);
ShardingAuditStrategyConfiguration shardingAuditStrategyConfig = new ShardingAuditStrategyConfiguration(Collections.singleton("foo_audit"), false);
ShardingStrategyConfiguration shardingStrategyConfig = createShardingStrategyConfiguration();
ruleConfig.setTables(Collections.singleton(createShardingTableRuleConfiguration(shardingStrategyConfig, shardingAuditStrategyConfig, ruleConfig.getDefaultKeyGenerateStrategy())));
ruleConfig.setAutoTables(Collections.singleton(createShardingAutoTableRuleConfiguration(shardingStrategyConfig, shardingAuditStrategyConfig, ruleConfig.getDefaultKeyGenerateStrategy())));
RuleConfigurationChecker<ShardingRuleConfiguration> checker = OrderedSPILoader.getServicesByClass(
RuleConfigurationChecker.class, Collections.singleton(ruleConfig.getClass())).get(ruleConfig.getClass());
checker.check("foo_db", ruleConfig, Collections.emptyMap(), Collections.emptyList());
}

@SuppressWarnings("unchecked")
@Test
void assertCheckTableConfigurationFailed() {
ShardingRuleConfiguration ruleConfig = createRuleConfiguration();
ruleConfig.setTables(Collections.singletonList(createShardingTableRuleConfiguration(null, null, null)));
ruleConfig.setTables(Collections.singleton(createShardingTableRuleConfiguration(null, null, null)));
ruleConfig.setAutoTables(Collections.singleton(createShardingAutoTableRuleConfiguration(null, null, null)));
RuleConfigurationChecker<ShardingRuleConfiguration> checker = OrderedSPILoader.getServicesByClass(
RuleConfigurationChecker.class, Collections.singleton(ruleConfig.getClass())).get(ruleConfig.getClass());
assertThrows(MissingRequiredShardingConfigurationException.class, () -> checker.check("foo_db", ruleConfig, Collections.emptyMap(), Collections.emptyList()));
}

@Test
void assertCheckKeyGenerateStrategyFailed() {
ShardingRuleConfiguration ruleConfig = createRuleConfiguration();
ShardingAuditStrategyConfiguration shardingAuditStrategyConfig = new ShardingAuditStrategyConfiguration(Collections.singleton("foo_audit"), false);
ShardingStrategyConfiguration shardingStrategyConfig = createShardingStrategyConfiguration();
ruleConfig.setTables(Collections.singleton(createShardingTableRuleConfiguration(shardingStrategyConfig, shardingAuditStrategyConfig, ruleConfig.getDefaultKeyGenerateStrategy())));
ruleConfig.setAutoTables(Collections.singleton(createShardingAutoTableRuleConfiguration(null, null,
new KeyGenerateStrategyConfiguration("foo_col", "bar_keygen"))));
assertThrows(UnregisteredAlgorithmException.class, () -> checker.check("foo_db", ruleConfig, Collections.emptyMap(), Collections.emptyList()));
}

@Test
void assertCheckAuditStrategyFailed() {
ShardingRuleConfiguration ruleConfig = createRuleConfiguration();
ShardingStrategyConfiguration shardingStrategyConfig = createShardingStrategyConfiguration();
ruleConfig.setTables(Collections.singleton(createShardingTableRuleConfiguration(shardingStrategyConfig, null, ruleConfig.getDefaultKeyGenerateStrategy())));
ruleConfig.setAutoTables(Collections.singleton(createShardingAutoTableRuleConfiguration(shardingStrategyConfig,
new ShardingAuditStrategyConfiguration(Collections.singleton("bar_audit"), false), ruleConfig.getDefaultKeyGenerateStrategy())));
assertThrows(UnregisteredAlgorithmException.class, () -> checker.check("foo_db", ruleConfig, Collections.emptyMap(), Collections.emptyList()));
}

@Test
void assertCheckShardingStrategyFailedWithComplexShardingStrategy() {
ShardingRuleConfiguration ruleConfig = createRuleConfiguration();
ShardingAuditStrategyConfiguration shardingAuditStrategyConfig = new ShardingAuditStrategyConfiguration(Collections.singleton("foo_audit"), false);
ruleConfig.setTables(Collections.singleton(
createShardingTableRuleConfiguration(new NoneShardingStrategyConfiguration(), shardingAuditStrategyConfig, ruleConfig.getDefaultKeyGenerateStrategy())));
ruleConfig.setAutoTables(Collections.singleton(createShardingAutoTableRuleConfiguration(
new ComplexShardingStrategyConfiguration("", "foo_algorithm"), shardingAuditStrategyConfig, ruleConfig.getDefaultKeyGenerateStrategy())));
assertThrows(MissingRequiredShardingConfigurationException.class, () -> checker.check("foo_db", ruleConfig, Collections.emptyMap(), Collections.emptyList()));
}

@Test
void assertCheckShardingStrategyFailedWithUnregisteredAlgorithm() {
ShardingRuleConfiguration ruleConfig = createRuleConfiguration();
ShardingAuditStrategyConfiguration shardingAuditStrategyConfig = new ShardingAuditStrategyConfiguration(Collections.singleton("foo_audit"), false);
ruleConfig.setTables(Collections.singleton(
createShardingTableRuleConfiguration(new NoneShardingStrategyConfiguration(), shardingAuditStrategyConfig, ruleConfig.getDefaultKeyGenerateStrategy())));
ruleConfig.setAutoTables(Collections.singleton(createShardingAutoTableRuleConfiguration(
new StandardShardingStrategyConfiguration("foo_col", "bar_algorithm"), shardingAuditStrategyConfig, ruleConfig.getDefaultKeyGenerateStrategy())));
assertThrows(UnregisteredAlgorithmException.class, () -> checker.check("foo_db", ruleConfig, Collections.emptyMap(), Collections.emptyList()));
}

@Test
void assertGetRequiredDataSourceNames() {
ShardingRuleConfiguration ruleConfig = createRuleConfiguration();
ShardingAuditStrategyConfiguration shardingAuditStrategyConfig = new ShardingAuditStrategyConfiguration(Collections.singleton("foo_audit"), false);
ShardingStrategyConfiguration shardingStrategyConfig = createShardingStrategyConfiguration();
ruleConfig.setTables(Collections.singleton(createShardingTableRuleConfiguration(shardingStrategyConfig, shardingAuditStrategyConfig, ruleConfig.getDefaultKeyGenerateStrategy())));
ruleConfig.setAutoTables(Collections.singleton(createShardingAutoTableRuleConfiguration(shardingStrategyConfig, shardingAuditStrategyConfig, ruleConfig.getDefaultKeyGenerateStrategy())));
assertTrue(checker.getRequiredDataSourceNames(ruleConfig).isEmpty());
}

@Test
void assertGetTableNames() {
ShardingRuleConfiguration ruleConfig = createRuleConfiguration();
ShardingAuditStrategyConfiguration shardingAuditStrategyConfig = new ShardingAuditStrategyConfiguration(Collections.singleton("foo_audit"), false);
ShardingStrategyConfiguration shardingStrategyConfig = createShardingStrategyConfiguration();
ruleConfig.setTables(Collections.singleton(createShardingTableRuleConfiguration(shardingStrategyConfig, shardingAuditStrategyConfig, ruleConfig.getDefaultKeyGenerateStrategy())));
ruleConfig.setAutoTables(Collections.singleton(createShardingAutoTableRuleConfiguration(shardingStrategyConfig, shardingAuditStrategyConfig, ruleConfig.getDefaultKeyGenerateStrategy())));
assertThat(checker.getTableNames(ruleConfig), is(Arrays.asList("foo_tbl", "bar_tbl")));
}

private ShardingRuleConfiguration createRuleConfiguration() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
result.getShardingAlgorithms().put("foo_algorithm", new AlgorithmConfiguration("MOD", PropertiesBuilder.build(new Property("sharding-count", "1"))));
Expand All @@ -85,8 +159,8 @@ private ShardingTableRuleConfiguration createShardingTableRuleConfiguration(fina
ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("foo_tbl", null);
result.setDatabaseShardingStrategy(null == shardingStrategyConfig ? mock(ShardingStrategyConfiguration.class) : shardingStrategyConfig);
result.setTableShardingStrategy(null == shardingStrategyConfig ? mock(ShardingStrategyConfiguration.class) : shardingStrategyConfig);
result.setAuditStrategy(null == shardingAuditStrategyConfig ? mock(ShardingAuditStrategyConfiguration.class) : shardingAuditStrategyConfig);
result.setKeyGenerateStrategy(null == keyGenerateStrategyConfig ? mock(KeyGenerateStrategyConfiguration.class) : keyGenerateStrategyConfig);
result.setAuditStrategy(shardingAuditStrategyConfig);
result.setKeyGenerateStrategy(keyGenerateStrategyConfig);
return result;
}

Expand All @@ -96,8 +170,8 @@ private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfigurat
ShardingAutoTableRuleConfiguration result = mock(ShardingAutoTableRuleConfiguration.class);
when(result.getLogicTable()).thenReturn("bar_tbl");
when(result.getShardingStrategy()).thenReturn(null == shardingStrategyConfig ? mock(ShardingStrategyConfiguration.class) : shardingStrategyConfig);
when(result.getAuditStrategy()).thenReturn(null == shardingAuditStrategyConfig ? mock(ShardingAuditStrategyConfiguration.class) : shardingAuditStrategyConfig);
when(result.getKeyGenerateStrategy()).thenReturn(null == keyGenerateStrategyConfig ? mock(KeyGenerateStrategyConfiguration.class) : keyGenerateStrategyConfig);
when(result.getAuditStrategy()).thenReturn(shardingAuditStrategyConfig);
when(result.getKeyGenerateStrategy()).thenReturn(keyGenerateStrategyConfig);
return result;
}
}