From ab42bbd89465fc7ae114a904db5784aa69d105c4 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Thu, 31 Oct 2024 17:00:30 +0800 Subject: [PATCH] Add test cases on BroadcastDataNodeRuleAttribute (#33483) --- .../BroadcastDataNodeRuleAttribute.java | 10 +- .../BroadcastDataNodeRuleAttributeTest.java | 102 ++++++++++++++++++ 2 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/rule/attribute/BroadcastDataNodeRuleAttributeTest.java diff --git a/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/rule/attribute/BroadcastDataNodeRuleAttribute.java b/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/rule/attribute/BroadcastDataNodeRuleAttribute.java index 886b5b8dd32c5..4c256effceb95 100644 --- a/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/rule/attribute/BroadcastDataNodeRuleAttribute.java +++ b/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/rule/attribute/BroadcastDataNodeRuleAttribute.java @@ -71,12 +71,8 @@ public Optional findLogicTableByActualTable(final String actualTable) { @Override public Optional findActualTableByCatalog(final String catalog, final String logicTable) { - if (!tableDataNodes.containsKey(logicTable.toLowerCase())) { - return Optional.empty(); - } - if (tableDataNodes.get(logicTable.toLowerCase()).stream().noneMatch(each -> each.getDataSourceName().equalsIgnoreCase(catalog))) { - return Optional.empty(); - } - return Optional.of(logicTable); + return tableDataNodes.getOrDefault(logicTable.toLowerCase(), Collections.emptyList()).stream().anyMatch(each -> each.getDataSourceName().equalsIgnoreCase(catalog)) + ? Optional.of(logicTable) + : Optional.empty(); } } diff --git a/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/rule/attribute/BroadcastDataNodeRuleAttributeTest.java b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/rule/attribute/BroadcastDataNodeRuleAttributeTest.java new file mode 100644 index 0000000000000..24f06c771c730 --- /dev/null +++ b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/rule/attribute/BroadcastDataNodeRuleAttributeTest.java @@ -0,0 +1,102 @@ +/* + * 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.broadcast.rule.attribute; + +import org.apache.shardingsphere.infra.datanode.DataNode; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.Optional; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class BroadcastDataNodeRuleAttributeTest { + + @Test + void assertGetAllDataNodes() { + Map> actual = new BroadcastDataNodeRuleAttribute(Arrays.asList("foo_ds", "bar_ds"), Arrays.asList("foo_tbl", "bar_tbl")).getAllDataNodes(); + assertThat(actual.size(), is(2)); + assertThat(actual.get("foo_tbl"), is(Arrays.asList(new DataNode("foo_ds.foo_tbl"), new DataNode("bar_ds.foo_tbl")))); + assertThat(actual.get("bar_tbl"), is(Arrays.asList(new DataNode("foo_ds.bar_tbl"), new DataNode("bar_ds.bar_tbl")))); + } + + @Test + void assertGetDataNodesByTableName() { + Collection actual = new BroadcastDataNodeRuleAttribute(Arrays.asList("foo_ds", "bar_ds"), Arrays.asList("foo_tbl", "bar_tbl")).getDataNodesByTableName("foo_tbl"); + assertThat(actual, is(Arrays.asList(new DataNode("foo_ds.foo_tbl"), new DataNode("bar_ds.foo_tbl")))); + } + + @Test + void assertFindFirstActualTable() { + Optional actual = new BroadcastDataNodeRuleAttribute(Arrays.asList("foo_ds", "bar_ds"), Arrays.asList("foo_tbl", "bar_tbl")).findFirstActualTable("foo_tbl"); + assertThat(actual, is(Optional.of("foo_tbl"))); + } + + @Test + void assertNotFindFirstActualTable() { + Optional actual = new BroadcastDataNodeRuleAttribute(Arrays.asList("foo_ds", "bar_ds"), Arrays.asList("foo_tbl", "bar_tbl")).findFirstActualTable("no_tbl"); + assertFalse(actual.isPresent()); + } + + @Test + void assertIsNeedAccumulateWithEmptyTables() { + assertTrue(new BroadcastDataNodeRuleAttribute(Arrays.asList("foo_ds", "bar_ds"), Arrays.asList("foo_tbl", "bar_tbl")).isNeedAccumulate(Collections.emptyList())); + } + + @Test + void assertIsNeedAccumulate() { + assertTrue(new BroadcastDataNodeRuleAttribute(Arrays.asList("foo_ds", "bar_ds"), Arrays.asList("foo_tbl", "bar_tbl")).isNeedAccumulate(Collections.singleton("no_tbl"))); + assertFalse(new BroadcastDataNodeRuleAttribute(Arrays.asList("foo_ds", "bar_ds"), Arrays.asList("foo_tbl", "bar_tbl")).isNeedAccumulate(Arrays.asList("foo_tbl", "bar_tbl"))); + } + + @Test + void assertFindLogicTableByActualTable() { + Optional actual = new BroadcastDataNodeRuleAttribute(Arrays.asList("foo_ds", "bar_ds"), Arrays.asList("foo_tbl", "bar_tbl")).findLogicTableByActualTable("foo_tbl"); + assertThat(actual, is(Optional.of("foo_tbl"))); + } + + @Test + void assertNotFindLogicTableByActualTable() { + Optional actual = new BroadcastDataNodeRuleAttribute(Arrays.asList("foo_ds", "bar_ds"), Arrays.asList("foo_tbl", "bar_tbl")).findLogicTableByActualTable("no_tbl"); + assertFalse(actual.isPresent()); + } + + @Test + void assertFindActualTableByCatalog() { + Optional actual = new BroadcastDataNodeRuleAttribute(Arrays.asList("foo_ds", "bar_ds"), Arrays.asList("foo_tbl", "bar_tbl")).findActualTableByCatalog("foo_ds", "foo_tbl"); + assertThat(actual, is(Optional.of("foo_tbl"))); + } + + @Test + void assertNotFindActualTableByCatalogWithNotExistedCatalog() { + Optional actual = new BroadcastDataNodeRuleAttribute(Arrays.asList("foo_ds", "bar_ds"), Arrays.asList("foo_tbl", "bar_tbl")).findActualTableByCatalog("no_ds", "foo_tbl"); + assertFalse(actual.isPresent()); + } + + @Test + void assertNotFindActualTableByCatalogWithNotExistedTable() { + Optional actual = new BroadcastDataNodeRuleAttribute(Arrays.asList("foo_ds", "bar_ds"), Arrays.asList("foo_tbl", "bar_tbl")).findActualTableByCatalog("foo_ds", "no_tbl"); + assertFalse(actual.isPresent()); + } +}