Skip to content

Commit

Permalink
Add test case for DataNodes contains DataSourceRoutedRule branch (apa…
Browse files Browse the repository at this point in the history
…che#6756)

* Add test case for DataNodeTest

* Add test case for DataNodes
  • Loading branch information
gzdzss authored Aug 10, 2020
1 parent 4b84b8d commit 73d8666
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

Expand Down Expand Up @@ -56,6 +57,7 @@ public void assertEquals() {
assertThat(dataNode, is(new DataNode("ds_0.tbl_0")));
assertThat(dataNode, is(dataNode));
assertThat(dataNode, not(new DataNode("ds_0.tbl_1")));
assertFalse(dataNode.equals(null));
assertNotNull(dataNode);
}

Expand All @@ -68,4 +70,9 @@ public void assertHashCode() {
public void assertToString() {
assertThat(new DataNode("ds_0.tbl_0").toString(), is("DataNode(dataSourceName=ds_0, tableName=tbl_0)"));
}

@Test
public void assertRequiredArgsConstructor() {
assertNotNull(new DataNode("ds_0", "tbl_0"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.datanode.DataNodes;
import org.apache.shardingsphere.infra.rule.fixture.TestDataSourceRoutedRule;
import org.apache.shardingsphere.infra.rule.fixture.TestShardingRule;
import org.apache.shardingsphere.infra.rule.fixture.TestShardingSphereRule;
import org.apache.shardingsphere.infra.rule.fixture.TestTableRule;
Expand All @@ -27,13 +28,14 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public final class DataNodesTest {

Expand All @@ -45,6 +47,10 @@ public final class DataNodesTest {

private final Collection<String> dataSourceNames2 = Arrays.asList("master_db_3", "slave_db_3");

private final String logicDataSourceName = "master_db_1";

private final Collection<String> replicaDataSourceNames = Arrays.asList("route_db_1", "route_db_2");

@Test(expected = NullPointerException.class)
public void assertWrongTable() {
DataNodes dataNodes = getRoutedRuleDataNodes();
Expand All @@ -59,8 +65,8 @@ public void assertGetEmpty() {
@Test
public void assertGetDataNodes() {
DataNodes dataNodes = getRoutedRuleDataNodes();
assertThat(dataNodes.getDataNodes(logicTableName1), is(getExpectedDataNodes(dataSourceNames1, logicTableName1)));
assertThat(dataNodes.getDataNodes(logicTableName2), is(getExpectedDataNodes(dataSourceNames2, logicTableName2)));
assertTrue(dataNodes.getDataNodes(logicTableName1).containsAll(getExpectedDataNodes(dataSourceNames1, logicTableName1)));
assertTrue(dataNodes.getDataNodes(logicTableName2).containsAll(getExpectedDataNodes(dataSourceNames2, logicTableName2)));
}

@Test
Expand All @@ -74,8 +80,10 @@ private DataNodes getRoutedRuleDataNodes() {
TestTableRule tableRule1 = new TestTableRule(dataSourceNames1, logicTableName1);
TestTableRule tableRule2 = new TestTableRule(dataSourceNames2, logicTableName2);
List<TestTableRule> tableRules = Arrays.asList(tableRule1, tableRule2);
ShardingSphereRule rule = new TestShardingRule(tableRules);
return new DataNodes(Collections.singleton(rule));
ShardingSphereRule rule1 = new TestShardingRule(tableRules);
Map<String, Collection<String>> dataSourceMapper = Collections.singletonMap(logicDataSourceName, replicaDataSourceNames);
TestDataSourceRoutedRule rule2 = new TestDataSourceRoutedRule(dataSourceMapper);
return new DataNodes(Arrays.asList(rule1, rule2));
}

private DataNodes getNonRoutedRuleDataNodes() {
Expand All @@ -85,18 +93,16 @@ private DataNodes getNonRoutedRuleDataNodes() {
private Collection<DataNode> getExpectedDataNodes(final Collection<String> dataSourceNames, final String logicTableName) {
Collection<DataNode> result = new LinkedList<>();
for (String each : dataSourceNames) {
result.add(new DataNode(each, logicTableName));
if (logicDataSourceName.equals(each)) {
replicaDataSourceNames.stream().forEach(a -> result.add(new DataNode(a, logicTableName)));
} else {
result.add(new DataNode(each, logicTableName));
}
}
return result;
}

private Map<String, Collection<DataNode>> getExpectedDataNodeGroups(final Collection<String> dataSourceNames, final String logicTableName) {
Map<String, Collection<DataNode>> result = new LinkedHashMap<>(dataSourceNames.size(), 1);
for (String each : dataSourceNames) {
Collection<DataNode> self = new LinkedList<>();
self.add(new DataNode(each, logicTableName));
result.put(each, self);
}
return result;
private Map<String, List<DataNode>> getExpectedDataNodeGroups(final Collection<String> dataSourceNames, final String logicTableName) {
return getExpectedDataNodes(dataSourceNames, logicTableName).stream().collect(Collectors.groupingBy(DataNode::getDataSourceName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.infra.rule.fixture;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.rule.DataSourceRoutedRule;

import java.util.Collection;
import java.util.Map;

@RequiredArgsConstructor
public final class TestDataSourceRoutedRule implements DataSourceRoutedRule {

private final Map<String, Collection<String>> dataSourceMapper;

@Override
public Map<String, Collection<String>> getDataSourceMapper() {
return dataSourceMapper;
}
}

0 comments on commit 73d8666

Please sign in to comment.