diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/rule/RuleMetaDataTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/rule/RuleMetaDataTest.java index 4dfba648d5593..e669e27810ba4 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/rule/RuleMetaDataTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/rule/RuleMetaDataTest.java @@ -17,14 +17,27 @@ package org.apache.shardingsphere.infra.metadata.database.rule; +import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; +import org.apache.shardingsphere.infra.datanode.DataNode; +import org.apache.shardingsphere.infra.rule.ShardingSphereRule; +import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule; +import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule; import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Optional; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class RuleMetaDataTest { @@ -44,4 +57,83 @@ void assertFindSingleRule() { void assertGetSingleRule() { assertThat(ruleMetaData.getSingleRule(ShardingSphereRuleFixture.class), instanceOf(ShardingSphereRuleFixture.class)); } + + @Test + void assertGetInUsedStorageUnitNameAndRulesMapWhenRulesAreEmpty() { + Collection rules = new ArrayList<>(); + RuleMetaData ruleMetaData = new RuleMetaData(rules); + Map>> actual = ruleMetaData.getInUsedStorageUnitNameAndRulesMap(); + assertThat(actual.size(), is(0)); + } + + @Test + void assertGetInUsedStorageUnitNameAndRulesMapWhenRulesContainDataNodeContainedRule() { + Collection rules = new ArrayList<>(); + DataNodeContainedRule rule = new MockDataNodeContainedRule(); + rules.add(rule); + RuleMetaData ruleMetaData = new RuleMetaData(rules); + Map>> actual = ruleMetaData.getInUsedStorageUnitNameAndRulesMap(); + assertThat(actual.size(), is(1)); + assertTrue(actual.containsKey("testDataNodeSourceName")); + assertThat(actual.get("testDataNodeSourceName").size(), is(1)); + assertThat(actual.get("testDataNodeSourceName").size(), is(1)); + assertTrue(actual.get("testDataNodeSourceName").contains(MockDataNodeContainedRule.class)); + } + + @Test + void assertGetInUsedStorageUnitNameAndRulesMapWhenRulesContainBothDataSourceContainedRuleAndDataNodeContainedRule() { + Collection rules = new ArrayList<>(); + DataSourceContainedRule dataSourceContainedRule = mock(DataSourceContainedRule.class); + when(dataSourceContainedRule.getDataSourceMapper()).thenReturn(Collections.singletonMap("test", Arrays.asList("testDataSourceName"))); + DataNodeContainedRule dataNodeContainedRule = new MockDataNodeContainedRule(); + rules.add(dataSourceContainedRule); + rules.add(dataNodeContainedRule); + RuleMetaData ruleMetaData = new RuleMetaData(rules); + Map>> actual = ruleMetaData.getInUsedStorageUnitNameAndRulesMap(); + assertThat(actual.size(), is(2)); + assertTrue(actual.containsKey("testDataSourceName")); + assertTrue(actual.containsKey("testDataNodeSourceName")); + assertTrue(actual.get("testDataSourceName").contains(dataSourceContainedRule.getClass())); + assertTrue(actual.get("testDataNodeSourceName").contains(MockDataNodeContainedRule.class)); + } + + private static class MockDataNodeContainedRule implements DataNodeContainedRule { + + @Override + public RuleConfiguration getConfiguration() { + return mock(RuleConfiguration.class); + } + + @Override + public Map> getAllDataNodes() { + Map> result = new LinkedHashMap<>(); + result.put("test", Arrays.asList(new DataNode("testDataNodeSourceName", "testTableName"))); + return result; + } + + @Override + public boolean isNeedAccumulate(final Collection tableNames) { + return false; + } + + @Override + public Optional findLogicTableByActualTable(final String actualTable) { + return Optional.empty(); + } + + @Override + public Optional findFirstActualTable(final String logicTable) { + return Optional.empty(); + } + + @Override + public Collection getDataNodesByTableName(final String tableName) { + return null; + } + + @Override + public Optional findActualTableByCatalog(final String catalog, final String logicTable) { + return Optional.empty(); + } + } }