This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 4f6a53e Issue#9549 (#9600)
4f6a53e is described below
commit 4f6a53e912e7500db8311e8d9d1d5cf276f2d441
Author: huanghao495430759 <[email protected]>
AuthorDate: Thu Mar 18 11:37:19 2021 +0800
Issue#9549 (#9600)
* Fixes #9549
Co-authored-by: huanghao-jk <[email protected]>
Co-authored-by: huanghao <[email protected]>
---
.../infra/datanode/DataNodeTest.java | 78 +++++++++++++
.../infra/datanode/DataNodeUtilTest.java | 56 +++++++++
.../infra/datanode/DataNodesTest.java | 129 +++++++++++++++++++++
3 files changed, 263 insertions(+)
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
new file mode 100644
index 0000000..d906799
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.datanode;
+
+import
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
+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"));
+ }
+
+ @Test(expected = ShardingSphereConfigurationException.class)
+ public void assertNewDateNodeWithoutDelimiter() {
+ DataNode dataNode = new DataNode("db");
+ }
+
+ @Test(expected = ShardingSphereConfigurationException.class)
+ public void assertNewDateNodeWithInvalidDelimiter() {
+ DataNode dataNode = new DataNode("db,table_1");
+ }
+
+ @Test(expected = ShardingSphereConfigurationException.class)
+ public void assertNewDateNodeWithGreaterThenOneValidDelimiter() {
+ DataNode dataNode = new DataNode("schema.db.table_1");
+ }
+
+ @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"));
+ }
+
+ @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));
+ }
+
+ @Test
+ public void assertDateNodeToString() {
+ DataNode dataNode1 = new DataNode("db.table_1");
+ assertThat(dataNode1.toString(), is("DataNode(dataSourceName=db,
tableName=table_1)"));
+ }
+}
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
new file mode 100644
index 0000000..fb8f922
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeUtilTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.datanode;
+
+import com.google.common.collect.Lists;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Collection;
+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;
+ }
+}
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
new file mode 100644
index 0000000..86e221d
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodesTest.java
@@ -0,0 +1,129 @@
+/*
+ * 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.datanode;
+
+import com.google.common.collect.Lists;
+import org.apache.shardingsphere.infra.rule.type.DataNodeContainedRule;
+import org.apache.shardingsphere.infra.rule.type.DataSourceContainedRule;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+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.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<>();
+
+ 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)));
+ Collection<DataNode> userDataNodes = dataNodes.getDataNodes("user");
+ assertThat(userDataNodes, is(getShardingActualDataNode().get("user")));
+ List<String> primaryDs0SingleTables =
dataSourceSingleTablesMap.get("primary_ds0");
+ for (String primaryDs0SingleTable : primaryDs0SingleTables) {
+ Collection<DataNode> primaryDs0SingleTableDataNodes =
dataNodes.getDataNodes(primaryDs0SingleTable);
+ 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;
+ }
+
+ private DataNodeContainedRule buildDataNodeContainedRule(final boolean
replicaQuery) {
+ DataNodeContainedRule dataNodeContainedRule =
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;
+ }
+
+ 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>> 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);
+ }
+ return singleTableDataNodeMap;
+ }
+
+ private Map<String, Collection<DataNode>> getReplicaShardingDataNode() {
+ return shardingActualTablesMap.entrySet().stream().collect(
+ Collectors.toMap(Map.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()
+ .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());
+ }
+}