Skip to content

Commit

Permalink
Add test cases on BroadcastDataNodeRuleAttribute (#33483)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Oct 31, 2024
1 parent 0bf90dc commit ab42bbd
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,8 @@ public Optional<String> findLogicTableByActualTable(final String actualTable) {

@Override
public Optional<String> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Collection<DataNode>> 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<DataNode> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> 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<String> actual = new BroadcastDataNodeRuleAttribute(Arrays.asList("foo_ds", "bar_ds"), Arrays.asList("foo_tbl", "bar_tbl")).findActualTableByCatalog("foo_ds", "no_tbl");
assertFalse(actual.isPresent());
}
}

0 comments on commit ab42bbd

Please sign in to comment.