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

Added unit tests for RuleMetaData.getInUsedStorageUnitNameAndRulesMap() #28724

Merged
merged 13 commits into from
Mar 28, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,26 @@

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;

class RuleMetaDataTest {

Expand All @@ -42,6 +54,100 @@ void assertFindSingleRule() {

@Test
void assertGetSingleRule() {
assertThat(ruleMetaData.getSingleRule(ShardingSphereRuleFixture.class), instanceOf(ShardingSphereRuleFixture.class));
assertThat(ruleMetaData.getSingleRule(ShardingSphereRuleFixture.class),
Sakshi-75 marked this conversation as resolved.
Show resolved Hide resolved
instanceOf(ShardingSphereRuleFixture.class));
}

@Test
void assertGetInUsedStorageUnitNameAndRulesMapWhenRulesAreEmpty() {
Collection<ShardingSphereRule> rules = new ArrayList<>();
RuleMetaData ruleMetaData = new RuleMetaData(rules);
Map<String, Collection<Class<? extends ShardingSphereRule>>> actual = ruleMetaData.getInUsedStorageUnitNameAndRulesMap();
assertThat(actual.size(), is(0));
}

@Test
void assertGetInUsedStorageUnitNameAndRulesMapWhenRulesContainDataNodeContainedRule() {
Collection<ShardingSphereRule> rules = new ArrayList<>();
DataNodeContainedRule rule = new MockDataNodeContainedRule();
rules.add(rule);
RuleMetaData ruleMetaData = new RuleMetaData(rules);
Map<String, Collection<Class<? extends ShardingSphereRule>>> 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<ShardingSphereRule> rules = new ArrayList<>();
DataSourceContainedRule dataSourceContainedRule = new MockDataSourceContainedRule();
DataNodeContainedRule dataNodeContainedRule = new MockDataNodeContainedRule();
rules.add(dataSourceContainedRule);
rules.add(dataNodeContainedRule);
RuleMetaData ruleMetaData = new RuleMetaData(rules);
Map<String, Collection<Class<? extends ShardingSphereRule>>> actual = ruleMetaData.getInUsedStorageUnitNameAndRulesMap();
assertThat(actual.size(), is(2));
assertTrue(actual.containsKey("testDataSourceName"));
assertTrue(actual.containsKey("testDataNodeSourceName"));
assertTrue(actual.get("testDataSourceName").contains(MockDataSourceContainedRule.class));
assertTrue(actual.get("testDataNodeSourceName").contains(MockDataNodeContainedRule.class));
}

private static class MockDataSourceContainedRule implements DataSourceContainedRule {
Sakshi-75 marked this conversation as resolved.
Show resolved Hide resolved

@Override
public RuleConfiguration getConfiguration() {
return mock(RuleConfiguration.class);
}

@Override
public Map<String, Collection<String>> getDataSourceMapper() {
Map<String, Collection<String>> result = new LinkedHashMap<>();
result.put("test", Arrays.asList("testDataSourceName"));
return result;
}
}

private static class MockDataNodeContainedRule implements DataNodeContainedRule {

@Override
public RuleConfiguration getConfiguration() {
return mock(RuleConfiguration.class);
}

@Override
public Map<String, Collection<DataNode>> getAllDataNodes() {
Map<String, Collection<DataNode>> result = new LinkedHashMap<>();
result.put("test", Arrays.asList(new DataNode("testDataNodeSourceName", "testTableName")));
return result;
}

@Override
public boolean isNeedAccumulate(final Collection<String> tableNames) {
return false;
}

@Override
public Optional<String> findLogicTableByActualTable(final String actualTable) {
return Optional.empty();
}

@Override
public Optional<String> findFirstActualTable(final String logicTable) {
return Optional.empty();
}

@Override
public Collection<DataNode> getDataNodesByTableName(final String tableName) {
return null;
}

@Override
public Optional<String> findActualTableByCatalog(final String catalog, final String logicTable) {
return Optional.empty();
}
}
}