This is an automated email from the ASF dual-hosted git repository.
menghaoran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 16cec09 Merge DataNode test cases (#9715)
16cec09 is described below
commit 16cec09affcf246521f483ea30c73d61e17b48e9
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Mar 18 12:27:44 2021 +0800
Merge DataNode test cases (#9715)
---
.../infra/datanode/DataNodeTest.java | 66 ++++++-------
.../infra/datanode/DataNodeUtilTest.java | 35 +++----
.../infra/datanode/DataNodesTest.java | 104 ++++++++++++++-----
.../shardingsphere/infra/rule/DataNodeTest.java | 73 --------------
.../infra/rule/DataNodeUtilTest.java | 45 ---------
.../shardingsphere/infra/rule/DataNodesTest.java | 110 ---------------------
6 files changed, 118 insertions(+), 315 deletions(-)
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeTest.java
index d906799..7cd7f6c 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeTest.java
@@ -21,58 +21,52 @@ import
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurat
import org.junit.Test;
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;
-import static org.junit.Assert.assertTrue;
public final class DataNodeTest {
-
+
@Test
- public void assertNewDateNodeWithPointDelimiter() {
- DataNode dataNode = new DataNode("db.table_1");
- assertThat(dataNode.getDataSourceName(), is("db"));
- assertThat(dataNode.getTableName(), is("table_1"));
+ public void assertNewValidDataNode() {
+ DataNode dataNode = new DataNode("ds_0.tbl_0");
+ assertThat(dataNode.getDataSourceName(), is("ds_0"));
+ assertThat(dataNode.getTableName(), is("tbl_0"));
}
-
+
@Test(expected = ShardingSphereConfigurationException.class)
- public void assertNewDateNodeWithoutDelimiter() {
- DataNode dataNode = new DataNode("db");
+ public void assertNewInValidDataNodeWithoutDelimiter() {
+ new DataNode("ds_0tbl_0");
}
-
+
@Test(expected = ShardingSphereConfigurationException.class)
- public void assertNewDateNodeWithInvalidDelimiter() {
- DataNode dataNode = new DataNode("db,table_1");
+ public void assertNewInValidDataNodeWithTwoDelimiters() {
+ new DataNode("ds_0.tbl_0.tbl_1");
}
-
+
@Test(expected = ShardingSphereConfigurationException.class)
- public void assertNewDateNodeWithGreaterThenOneValidDelimiter() {
- DataNode dataNode = new DataNode("schema.db.table_1");
+ public void assertNewValidDataNodeWithInvalidDelimiter() {
+ new DataNode("ds_0,tbl_0");
}
-
+
@Test
- public void assertNewDateNodeWithNoTableNameOrDbName() {
- DataNode dataNodeWithNoTableName = new DataNode("db.");
- assertThat(dataNodeWithNoTableName.getDataSourceName(), is("db"));
- assertThat(dataNodeWithNoTableName.getTableName(), is(""));
- DataNode dataNodeWithNoDbName = new DataNode(".table");
- assertThat(dataNodeWithNoDbName.getDataSourceName(), is(""));
- assertThat(dataNodeWithNoDbName.getTableName(), is("table"));
+ public void assertEquals() {
+ DataNode dataNode = new DataNode("ds_0.tbl_0");
+ 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);
}
-
+
@Test
- public void assertDateNodeEqualsAndHashCode() {
- DataNode dataNode1 = new DataNode("db.table_1");
- DataNode dataNode2 = new DataNode("db.table_1");
- assertThat(dataNode1, is(dataNode2));
- assertTrue(dataNode1.equals(dataNode2));
- assertTrue(dataNode1.hashCode() == dataNode2.hashCode());
- DataNode dataNode3 = new DataNode("db.table_3");
- assertFalse(dataNode1.equals(dataNode3));
+ public void assertHashCode() {
+ assertThat(new DataNode("ds_0.tbl_0").hashCode(), is(new
DataNode("ds_0.tbl_0").hashCode()));
}
-
+
@Test
- public void assertDateNodeToString() {
- DataNode dataNode1 = new DataNode("db.table_1");
- assertThat(dataNode1.toString(), is("DataNode(dataSourceName=db,
tableName=table_1)"));
+ public void assertToString() {
+ assertThat(new DataNode("ds_0.tbl_0").toString(),
is("DataNode(dataSourceName=ds_0, tableName=tbl_0)"));
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeUtilTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeUtilTest.java
index fb8f922..a59716e 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeUtilTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeUtilTest.java
@@ -17,40 +17,27 @@
package org.apache.shardingsphere.infra.datanode;
-import com.google.common.collect.Lists;
import org.junit.Test;
-import java.util.ArrayList;
-import java.util.Collection;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
import java.util.List;
import java.util.Map;
-import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
public final class DataNodeUtilTest {
-
+
@Test
public void assertGetDataNodeGroups() {
- Map<String, List<DataNode>> dataNodeGroups =
DataNodeUtil.getDataNodeGroups(getDataNodes());
- assertThat(dataNodeGroups.size(), is(3));
- assertTrue(dataNodeGroups.containsKey("db0"));
- dataNodeGroups.keySet().containsAll(Lists.newArrayList("db0", "db1",
"db2"));
- assertThat(dataNodeGroups.get("db0"), hasItems(new
DataNode("db0.table_1"), new DataNode("db0.table_2")));
- assertThat(dataNodeGroups.get("db0").get(0).getDataSourceName(),
is("db0"));
- assertThat(dataNodeGroups.get("db0").get(1).getDataSourceName(),
is("db0"));
- assertThat(dataNodeGroups.get("db1"), hasItems(new
DataNode("db1.table_3")));
- assertThat(dataNodeGroups.get("db2"), hasItems(new
DataNode("db2.table_4")));
- }
-
- private Collection<DataNode> getDataNodes() {
- List<DataNode> dataNodes = new ArrayList<>(3);
- dataNodes.add(new DataNode("db0.table_1"));
- dataNodes.add(new DataNode("db0.table_2"));
- dataNodes.add(new DataNode("db1.table_3"));
- dataNodes.add(new DataNode("db2.table_4"));
- return dataNodes;
+ Map<String, List<DataNode>> expected = new LinkedHashMap<>(2, 1);
+ expected.put("ds_0", Arrays.asList(new DataNode("ds_0.tbl_0"), new
DataNode("ds_0.tbl_1")));
+ expected.put("ds_1", Arrays.asList(new DataNode("ds_1.tbl_0"), new
DataNode("ds_1.tbl_1")));
+ List<DataNode> dataNodes = new LinkedList<>();
+ expected.values().forEach(dataNodes::addAll);
+ Map<String, List<DataNode>> actual =
DataNodeUtil.getDataNodeGroups(dataNodes);
+ assertThat(actual, is(expected));
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodesTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodesTest.java
index 86e221d..ee0e321 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodesTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodesTest.java
@@ -22,11 +22,14 @@ import
org.apache.shardingsphere.infra.rule.type.DataNodeContainedRule;
import org.apache.shardingsphere.infra.rule.type.DataSourceContainedRule;
import org.junit.Test;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
+import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.is;
@@ -35,19 +38,31 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public final class DataNodesTest {
-
+
private static Map<String, Collection<String>> replicaDataSourcesMap = new
HashMap<>();
-
+
private static Map<String, List<String>> shardingActualTablesMap = new
HashMap<>();
-
+
private static Map<String, List<String>> dataSourceSingleTablesMap = new
HashMap<>();
-
+
+ private final String logicTableName1 = "user";
+
+ private final String logicTableName2 = "dept";
+
+ private final Collection<String> dataSourceNames1 =
Arrays.asList("primary_db_1", "primary_db_2", "replica_db_1", "replica_db_2");
+
+ private final Collection<String> dataSourceNames2 =
Arrays.asList("primary_db_3", "replica_db_3");
+
+ private final String logicDataSourceName = "primary_db_1";
+
+ private final Collection<String> replicaDataSourceNames =
Arrays.asList("route_db_1", "route_db_2");
+
static {
replicaDataSourcesMap.putIfAbsent("node_0",
Lists.newArrayList("primary_ds0", "replica_ds0_0", "replica_ds0_1"));
shardingActualTablesMap.put("user", Lists.newArrayList("user_0",
"user_1"));
dataSourceSingleTablesMap.put("primary_ds0",
Lists.newArrayList("primary_ds0_table_0", "primary_ds0_table_1"));
}
-
+
@Test
public void assertGetDataNodesWithTablePresent() {
DataNodes dataNodes = new
DataNodes(Lists.newArrayList(buildDataSourceContainedRule(),
buildDataNodeContainedRule(true)));
@@ -59,71 +74,106 @@ public final class DataNodesTest {
assertThat(primaryDs0SingleTableDataNodes,
is(getSingleTableDataNode().get(primaryDs0SingleTable)));
}
}
-
+
@Test(expected = NullPointerException.class)
public void assertGetDataNodesWithTableAbsent() {
DataNodes dataNodes = new
DataNodes(Lists.newArrayList(buildDataSourceContainedRule(),
buildDataNodeContainedRule(true)));
Collection<DataNode> userDataNodes = dataNodes.getDataNodes("order");
assertThat(userDataNodes, is(Collections.emptyMap()));
}
-
+
@Test
public void assertGetDataNodesWithDataNodeContainedRuleAbsent() {
DataNodes dataNodes = new
DataNodes(Lists.newArrayList(buildDataSourceContainedRule()));
Collection<DataNode> userDataNodes = dataNodes.getDataNodes("user");
assertThat(userDataNodes, is(Collections.emptyList()));
}
-
+
@Test
public void assertGetDataNodesWithDataSourceContainedRuleAbsent() {
DataNodes dataNodes = new
DataNodes(Lists.newArrayList(buildDataNodeContainedRule(false)));
Collection<DataNode> userDataNodes = dataNodes.getDataNodes("user");
assertThat(userDataNodes, is(getShardingActualDataNode().get("user")));
}
-
+
private DataSourceContainedRule buildDataSourceContainedRule() {
- DataSourceContainedRule dataSourceContainedRule =
mock(DataSourceContainedRule.class);
-
when(dataSourceContainedRule.getDataSourceMapper()).thenReturn(replicaDataSourcesMap);
- return dataSourceContainedRule;
+ DataSourceContainedRule result = mock(DataSourceContainedRule.class);
+ when(result.getDataSourceMapper()).thenReturn(replicaDataSourcesMap);
+ return result;
}
-
+
private DataNodeContainedRule buildDataNodeContainedRule(final boolean
replicaQuery) {
- DataNodeContainedRule dataNodeContainedRule =
mock(DataNodeContainedRule.class);
+ DataNodeContainedRule result = mock(DataNodeContainedRule.class);
Map<String, Collection<DataNode>> dataNodes = new HashMap<>();
dataNodes.putAll(getSingleTableDataNode());
dataNodes.putAll(replicaQuery ? getReplicaShardingDataNode() :
getShardingActualDataNode());
- when(dataNodeContainedRule.getAllDataNodes()).thenReturn(dataNodes);
- return dataNodeContainedRule;
+ when(result.getAllDataNodes()).thenReturn(dataNodes);
+ return result;
}
-
+
private Map<String, Collection<DataNode>> getSingleTableDataNode() {
- Map<String, Collection<DataNode>> singleTableDataNodeMap = new
HashMap<>();
- for (Map.Entry<String, List<String>> entry
:dataSourceSingleTablesMap.entrySet()) {
+ Map<String, Collection<DataNode>> result = new HashMap<>();
+ for (Entry<String, List<String>> entry
:dataSourceSingleTablesMap.entrySet()) {
Map<String, Collection<DataNode>> map =
entry.getValue().stream().collect(Collectors.toMap(singleTable -> singleTable,
singleTable -> Lists.newArrayList(new DataNode(entry.getKey(),
singleTable)), (Collection<DataNode> oldList, Collection<DataNode> newList) -> {
oldList.addAll(newList);
return oldList;
})
);
- singleTableDataNodeMap.putAll(map);
+ result.putAll(map);
}
- return singleTableDataNodeMap;
+ return result;
}
-
+
private Map<String, Collection<DataNode>> getReplicaShardingDataNode() {
return shardingActualTablesMap.entrySet().stream().collect(
- Collectors.toMap(Map.Entry::getKey,
+ Collectors.toMap(Entry::getKey,
entry -> entry.getValue().stream().map(shardingTable ->
getActualDataNode(replicaDataSourcesMap.keySet(), shardingTable))
.flatMap(Collection::stream).collect(Collectors.toList())));
}
-
+
private Map<String, Collection<DataNode>> getShardingActualDataNode() {
List<String> allDataSources =
replicaDataSourcesMap.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
- return
shardingActualTablesMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
entry -> entry.getValue().stream()
+ return
shardingActualTablesMap.entrySet().stream().collect(Collectors.toMap(Entry::getKey,
entry -> entry.getValue().stream()
.map(shardingTable -> getActualDataNode(allDataSources,
shardingTable)).flatMap(Collection::stream).collect(Collectors.toList())));
}
-
+
private List<DataNode> getActualDataNode(final Collection<String>
dataSources, final String tableName) {
- return dataSources.stream().map(dataSource -> new DataNode(dataSource,
tableName)).collect(Collectors.toList());
+ return dataSources.stream().map(each -> new DataNode(each,
tableName)).collect(Collectors.toList());
+ }
+
+ @Test
+ public void assertGetDataNodeGroups() {
+ DataNodes dataNodes = getRoutedRuleDataNodes();
+ assertThat(dataNodes.getDataNodeGroups(logicTableName1),
is(getExpectedDataNodeGroups(dataSourceNames1, logicTableName1)));
+ assertThat(dataNodes.getDataNodeGroups(logicTableName2),
is(getExpectedDataNodeGroups(dataSourceNames2, logicTableName2)));
+ }
+
+ private DataNodes getRoutedRuleDataNodes() {
+ Map<String, Collection<DataNode>> nodeMap = new HashMap<>();
+ nodeMap.put(logicTableName1, getExpectedDataNodes(dataSourceNames1,
logicTableName1));
+ nodeMap.put(logicTableName2, getExpectedDataNodes(dataSourceNames2,
logicTableName2));
+ DataNodeContainedRule rule1 = mock(DataNodeContainedRule.class);
+ when(rule1.getAllDataNodes()).thenReturn(nodeMap);
+ Map<String, Collection<String>> dataSourceMapper =
Collections.singletonMap(logicDataSourceName, replicaDataSourceNames);
+ DataSourceContainedRule rule2 = mock(DataSourceContainedRule.class);
+ when(rule2.getDataSourceMapper()).thenReturn(dataSourceMapper);
+ return new DataNodes(Arrays.asList(rule1, rule2));
+ }
+
+ private Collection<DataNode> getExpectedDataNodes(final Collection<String>
dataSourceNames, final String logicTableName) {
+ Collection<DataNode> result = new LinkedList<>();
+ for (String each : dataSourceNames) {
+ if (logicDataSourceName.equals(each)) {
+ replicaDataSourceNames.forEach(dataSourceName ->
result.add(new DataNode(dataSourceName, logicTableName)));
+ } else {
+ result.add(new DataNode(each, logicTableName));
+ }
+ }
+ 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));
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodeTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodeTest.java
deleted file mode 100644
index 28e49bc..0000000
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodeTest.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * 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;
-
-import
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
-import org.apache.shardingsphere.infra.datanode.DataNode;
-import org.junit.Test;
-
-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;
-
-public final class DataNodeTest {
-
- @Test
- public void assertNewValidDataNode() {
- DataNode dataNode = new DataNode("ds_0.tbl_0");
- assertThat(dataNode.getDataSourceName(), is("ds_0"));
- assertThat(dataNode.getTableName(), is("tbl_0"));
- }
-
- @Test(expected = ShardingSphereConfigurationException.class)
- public void assertNewInValidDataNodeWithoutDelimiter() {
- new DataNode("ds_0tbl_0");
- }
-
- @Test(expected = ShardingSphereConfigurationException.class)
- public void assertNewInValidDataNodeWithTwoDelimiters() {
- new DataNode("ds_0.tbl_0.tbl_1");
- }
-
- @Test(expected = ShardingSphereConfigurationException.class)
- public void assertNewValidDataNodeWithInvalidDelimiter() {
- new DataNode("ds_0,tbl_0");
- }
-
- @Test
- public void assertEquals() {
- DataNode dataNode = new DataNode("ds_0.tbl_0");
- 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);
- }
-
- @Test
- public void assertHashCode() {
- assertThat(new DataNode("ds_0.tbl_0").hashCode(), is(new
DataNode("ds_0.tbl_0").hashCode()));
- }
-
- @Test
- public void assertToString() {
- assertThat(new DataNode("ds_0.tbl_0").toString(),
is("DataNode(dataSourceName=ds_0, tableName=tbl_0)"));
- }
-}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodeUtilTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodeUtilTest.java
deleted file mode 100644
index 5506042..0000000
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodeUtilTest.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * 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;
-
-import org.apache.shardingsphere.infra.datanode.DataNode;
-import org.apache.shardingsphere.infra.datanode.DataNodeUtil;
-import org.junit.Test;
-
-import java.util.Arrays;
-import java.util.LinkedHashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-public final class DataNodeUtilTest {
-
- @Test
- public void assertGetDataNodeGroups() {
- Map<String, List<DataNode>> expected = new LinkedHashMap<>(2, 1);
- expected.put("ds_0", Arrays.asList(new DataNode("ds_0.tbl_0"), new
DataNode("ds_0.tbl_1")));
- expected.put("ds_1", Arrays.asList(new DataNode("ds_1.tbl_0"), new
DataNode("ds_1.tbl_1")));
- List<DataNode> dataNodes = new LinkedList<>();
- expected.values().forEach(dataNodes::addAll);
- Map<String, List<DataNode>> actual =
DataNodeUtil.getDataNodeGroups(dataNodes);
- assertThat(actual, is(expected));
- }
-}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodesTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodesTest.java
deleted file mode 100644
index 8e3d126..0000000
---
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/DataNodesTest.java
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- * 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;
-
-import org.apache.shardingsphere.infra.datanode.DataNode;
-import org.apache.shardingsphere.infra.datanode.DataNodes;
-import org.apache.shardingsphere.infra.rule.type.DataNodeContainedRule;
-import org.apache.shardingsphere.infra.rule.type.DataSourceContainedRule;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-import org.junit.Test;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
-
-public final class DataNodesTest {
-
- private final String logicTableName1 = "user";
-
- private final String logicTableName2 = "dept";
-
- private final Collection<String> dataSourceNames1 =
Arrays.asList("primary_db_1", "primary_db_2", "replica_db_1", "replica_db_2");
-
- private final Collection<String> dataSourceNames2 =
Arrays.asList("primary_db_3", "replica_db_3");
-
- private final String logicDataSourceName = "primary_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();
- dataNodes.getDataNodes("wrongTableName");
- }
-
- @Test
- public void assertGetEmpty() {
- assertThat(getNonRoutedRuleDataNodes().getDataNodes("tableName"),
is(Collections.emptyList()));
- }
-
- @Test
- public void assertGetDataNodes() {
- DataNodes dataNodes = getRoutedRuleDataNodes();
-
assertTrue(dataNodes.getDataNodes(logicTableName1).containsAll(getExpectedDataNodes(dataSourceNames1,
logicTableName1)));
-
assertTrue(dataNodes.getDataNodes(logicTableName2).containsAll(getExpectedDataNodes(dataSourceNames2,
logicTableName2)));
- }
-
- @Test
- public void assertGetDataNodeGroups() {
- DataNodes dataNodes = getRoutedRuleDataNodes();
- assertThat(dataNodes.getDataNodeGroups(logicTableName1),
is(getExpectedDataNodeGroups(dataSourceNames1, logicTableName1)));
- assertThat(dataNodes.getDataNodeGroups(logicTableName2),
is(getExpectedDataNodeGroups(dataSourceNames2, logicTableName2)));
- }
-
- private DataNodes getRoutedRuleDataNodes() {
- Map<String, Collection<DataNode>> nodeMap = new HashMap<>();
- nodeMap.put(logicTableName1, getExpectedDataNodes(dataSourceNames1,
logicTableName1));
- nodeMap.put(logicTableName2, getExpectedDataNodes(dataSourceNames2,
logicTableName2));
- DataNodeContainedRule rule1 = mock(DataNodeContainedRule.class);
- when(rule1.getAllDataNodes()).thenReturn(nodeMap);
- Map<String, Collection<String>> dataSourceMapper =
Collections.singletonMap(logicDataSourceName, replicaDataSourceNames);
- DataSourceContainedRule rule2 = mock(DataSourceContainedRule.class);
- when(rule2.getDataSourceMapper()).thenReturn(dataSourceMapper);
- return new DataNodes(Arrays.asList(rule1, rule2));
- }
-
- private DataNodes getNonRoutedRuleDataNodes() {
- return new
DataNodes(Collections.singleton(mock(ShardingSphereRule.class)));
- }
-
- private Collection<DataNode> getExpectedDataNodes(final Collection<String>
dataSourceNames, final String logicTableName) {
- Collection<DataNode> result = new LinkedList<>();
- for (String each : dataSourceNames) {
- if (logicDataSourceName.equals(each)) {
- replicaDataSourceNames.forEach(dataSourceName ->
result.add(new DataNode(dataSourceName, logicTableName)));
- } else {
- result.add(new DataNode(each, logicTableName));
- }
- }
- 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));
- }
-}