Skip to content

Commit

Permalink
Added unit tests for RuleMetaData.getInUsedStorageUnitNameAndRulesMap…
Browse files Browse the repository at this point in the history
…() (#28724)

* added some tests for RuleMetaData.getInUsedStorageUnitNameAndRulesMap()

* added test for 0 rules

* remove unused import

* checkstyle issues fixed

* address feedbacks

* address feedbacks

* removed duplicate lines

* replaced MockDataSourceContainedRule with mock object

* spotless issues fixed
  • Loading branch information
Sakshi-75 authored Mar 28, 2024
1 parent 355c8fa commit 202d87f
Showing 1 changed file with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -44,4 +57,83 @@ void assertFindSingleRule() {
void assertGetSingleRule() {
assertThat(ruleMetaData.getSingleRule(ShardingSphereRuleFixture.class), 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 = 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<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(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<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();
}
}
}

0 comments on commit 202d87f

Please sign in to comment.