From 616c24775d2d6bcf45a2ce42ed9246e48c055f63 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Tue, 27 Feb 2024 23:33:15 +0800 Subject: [PATCH] Add TableMapperRuleTest (#30323) * Add EncryptTableMapperRuleTest * Add ShardingTableMapperRuleTest * Add BroadcastTableMapperRuleTest * Add MaskTableMapperRuleTest * Add MaskTableMapperRuleTest * Add SingleTableMapperRuleTest --- .../rule/BroadcastTableMapperRuleTest.java | 46 ++++++++++++++ .../encrypt/rule/EncryptRuleTest.java | 27 +------- .../rule/EncryptTableMapperRuleTest.java | 47 ++++++++++++++ .../mask/rule/MaskTableMapperRuleTest.java | 47 ++++++++++++++ .../sharding/rule/ShardingRuleTest.java | 16 ++--- .../rule/ShardingTableMapperRuleTest.java | 63 +++++++++++++++++++ .../rule/SingleTableMapperRuleTest.java | 47 ++++++++++++++ 7 files changed, 257 insertions(+), 36 deletions(-) create mode 100644 features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/rule/BroadcastTableMapperRuleTest.java create mode 100644 features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptTableMapperRuleTest.java create mode 100644 features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/MaskTableMapperRuleTest.java create mode 100644 features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingTableMapperRuleTest.java create mode 100644 kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/SingleTableMapperRuleTest.java diff --git a/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/rule/BroadcastTableMapperRuleTest.java b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/rule/BroadcastTableMapperRuleTest.java new file mode 100644 index 0000000000000..cf5931b49b270 --- /dev/null +++ b/features/broadcast/core/src/test/java/org/apache/shardingsphere/broadcast/rule/BroadcastTableMapperRuleTest.java @@ -0,0 +1,46 @@ +/* + * 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; + +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.LinkedList; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +class BroadcastTableMapperRuleTest { + + private final BroadcastTableMapperRule tableMapperRule = new BroadcastTableMapperRule(Collections.singleton("foo_tbl")); + + @Test + void assertGetLogicTableMapper() { + assertThat(new LinkedList<>(tableMapperRule.getLogicTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl"))); + } + + @Test + void assertGetDistributedTableMapper() { + assertThat(new LinkedList<>(tableMapperRule.getDistributedTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl"))); + } + + @Test + void assertGetEnhancedTableMapper() { + assertThat(new LinkedList<>(tableMapperRule.getEnhancedTableMapper().getTableNames()), is(Collections.emptyList())); + } +} diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java index d0512c3cd7261..7ef4b76281649 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java @@ -34,7 +34,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.HashMap; -import java.util.LinkedList; import java.util.Map; import java.util.Properties; import java.util.stream.Stream; @@ -61,18 +60,6 @@ void assertGetNotExistedEncryptTable() { assertThrows(EncryptTableNotFoundException.class, () -> new EncryptRule("foo_db", createEncryptRuleConfiguration()).getEncryptTable("not_existed_tbl")); } - @Test - void assertGetTables() { - assertThat(new LinkedList<>(new EncryptRule("foo_db", createEncryptRuleConfiguration()).getTableMapperRule().getLogicTableMapper().getTableNames()), - is(Collections.singletonList("t_encrypt"))); - } - - @Test - void assertGetTableWithLowercase() { - assertThat(new LinkedList<>(new EncryptRule("foo_db", createEncryptRuleConfigurationWithUpperCaseLogicTable()).getTableMapperRule().getLogicTableMapper().getTableNames()), - is(Collections.singletonList("T_ENCRYPT"))); - } - private EncryptRuleConfiguration createEncryptRuleConfiguration() { EncryptColumnRuleConfiguration pwdColumnConfig = createEncryptColumnRuleConfiguration("standard_encryptor", "assisted_encryptor", "like_encryptor"); EncryptColumnRuleConfiguration creditCardColumnConfig = new EncryptColumnRuleConfiguration("credit_card", new EncryptColumnItemRuleConfiguration("credit_card_cipher", "standard_encryptor")); @@ -99,19 +86,9 @@ void assertLikeQueryEncryptorNameSpecified() { assertThat(pwdColumnConfig.getLikeQuery().get().getEncryptorName(), is("like_query_test_encryptor")); } - private EncryptRuleConfiguration createEncryptRuleConfigurationWithUpperCaseLogicTable() { - AlgorithmConfiguration standardEncryptConfig = new AlgorithmConfiguration("CORE.FIXTURE", new Properties()); - AlgorithmConfiguration queryAssistedEncryptConfig = new AlgorithmConfiguration("CORE.QUERY_ASSISTED.FIXTURE", new Properties()); - AlgorithmConfiguration queryLikeEncryptConfig = new AlgorithmConfiguration("CORE.QUERY_LIKE.FIXTURE", new Properties()); - EncryptColumnRuleConfiguration pwdColumnConfig = new EncryptColumnRuleConfiguration("pwd", new EncryptColumnItemRuleConfiguration("pwd_cipher", "standard_encryptor")); - EncryptColumnRuleConfiguration creditCardColumnConfig = new EncryptColumnRuleConfiguration("credit_card", new EncryptColumnItemRuleConfiguration("credit_card_cipher", "standard_encryptor")); - EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration("T_ENCRYPT", Arrays.asList(pwdColumnConfig, creditCardColumnConfig)); - return new EncryptRuleConfiguration(Collections.singleton(tableConfig), getEncryptors(standardEncryptConfig, queryAssistedEncryptConfig, queryLikeEncryptConfig)); - } - private Map getEncryptors(final AlgorithmConfiguration standardEncryptConfig, final AlgorithmConfiguration queryAssistedEncryptConfig, final AlgorithmConfiguration queryLikeEncryptConfig) { - Map result = new HashMap<>(2, 1F); + Map result = new HashMap<>(3, 1F); result.put("standard_encryptor", standardEncryptConfig); result.put("assisted_encryptor", queryAssistedEncryptConfig); result.put("like_encryptor", queryLikeEncryptConfig); @@ -130,7 +107,7 @@ void assertNewEncryptRuleWhenConfigureWrongEncryptorType(final String name, fina assertThrows(MismatchedEncryptAlgorithmTypeException.class, () -> new EncryptRule("foo_db", ruleConfig)); } - private static EncryptColumnRuleConfiguration createEncryptColumnRuleConfiguration(final String encryptorName, final String assistedQueryEncryptorName, final String likeEncryptorName) { + private EncryptColumnRuleConfiguration createEncryptColumnRuleConfiguration(final String encryptorName, final String assistedQueryEncryptorName, final String likeEncryptorName) { EncryptColumnRuleConfiguration result = new EncryptColumnRuleConfiguration("pwd", new EncryptColumnItemRuleConfiguration("pwd_cipher", encryptorName)); result.setAssistedQuery(new EncryptColumnItemRuleConfiguration("pwd_assist", assistedQueryEncryptorName)); result.setLikeQuery(new EncryptColumnItemRuleConfiguration("pwd_like", likeEncryptorName)); diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptTableMapperRuleTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptTableMapperRuleTest.java new file mode 100644 index 0000000000000..d63310d4496a1 --- /dev/null +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptTableMapperRuleTest.java @@ -0,0 +1,47 @@ +/* + * 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.encrypt.rule; + +import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.LinkedList; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +class EncryptTableMapperRuleTest { + + private final EncryptTableMapperRule tableMapperRule = new EncryptTableMapperRule(Collections.singleton(new EncryptTableRuleConfiguration("foo_tbl", Collections.emptyList()))); + + @Test + void assertGetLogicTableMapper() { + assertThat(new LinkedList<>(tableMapperRule.getLogicTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl"))); + } + + @Test + void assertGetDistributedTableMapper() { + assertThat(new LinkedList<>(tableMapperRule.getDistributedTableMapper().getTableNames()), is(Collections.emptyList())); + } + + @Test + void assertGetEnhancedTableMapper() { + assertThat(new LinkedList<>(tableMapperRule.getEnhancedTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl"))); + } +} diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/MaskTableMapperRuleTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/MaskTableMapperRuleTest.java new file mode 100644 index 0000000000000..e3a56b6f0207b --- /dev/null +++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/MaskTableMapperRuleTest.java @@ -0,0 +1,47 @@ +/* + * 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.mask.rule; + +import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.LinkedList; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +class MaskTableMapperRuleTest { + + private final MaskTableMapperRule tableMapperRule = new MaskTableMapperRule(Collections.singleton(new MaskTableRuleConfiguration("foo_tbl", Collections.emptyList()))); + + @Test + void assertGetLogicTableMapper() { + assertThat(new LinkedList<>(tableMapperRule.getLogicTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl"))); + } + + @Test + void assertGetDistributedTableMapper() { + assertThat(new LinkedList<>(tableMapperRule.getDistributedTableMapper().getTableNames()), is(Collections.emptyList())); + } + + @Test + void assertGetEnhancedTableMapper() { + assertThat(new LinkedList<>(tableMapperRule.getEnhancedTableMapper().getTableNames()), is(Collections.emptyList())); + } +} diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java index 8376026a7d596..79723adcc3ac8 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java @@ -18,11 +18,15 @@ package org.apache.shardingsphere.sharding.rule; import org.apache.groovy.util.Maps; +import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext; +import org.apache.shardingsphere.infra.algorithm.keygen.core.exception.GenerateKeyStrategyNotFoundException; +import org.apache.shardingsphere.infra.algorithm.keygen.snowflake.SnowflakeKeyGenerateAlgorithm; +import org.apache.shardingsphere.infra.algorithm.keygen.uuid.UUIDKeyGenerateAlgorithm; import org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext; import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.dml.UpdateStatementContext; -import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.datanode.DataNode; @@ -30,10 +34,6 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; -import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext; -import org.apache.shardingsphere.infra.algorithm.keygen.core.exception.GenerateKeyStrategyNotFoundException; -import org.apache.shardingsphere.infra.algorithm.keygen.snowflake.SnowflakeKeyGenerateAlgorithm; -import org.apache.shardingsphere.infra.algorithm.keygen.uuid.UUIDKeyGenerateAlgorithm; import org.apache.shardingsphere.sharding.algorithm.audit.DMLShardingConditionsShardingAuditAlgorithm; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration; @@ -72,7 +72,6 @@ import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashSet; -import java.util.LinkedList; import java.util.Map; import java.util.Optional; import java.util.Properties; @@ -456,11 +455,6 @@ void assertNotContainsShardingTable() { assertFalse(createMinimumShardingRule().containsShardingTable(Collections.singleton("table_0"))); } - @Test - void assertGetTables() { - assertThat(new LinkedList<>(createMaximumShardingRule().getTableMapperRule().getLogicTableMapper().getTableNames()), is(Arrays.asList("LOGIC_TABLE", "SUB_LOGIC_TABLE"))); - } - @Test void assertGetDataSourceNamesWithShardingAutoTables() { ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingTableMapperRuleTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingTableMapperRuleTest.java new file mode 100644 index 0000000000000..3aaf346e0f5ef --- /dev/null +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingTableMapperRuleTest.java @@ -0,0 +1,63 @@ +/* + * 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.sharding.rule; + +import org.apache.shardingsphere.infra.datanode.DataNode; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.LinkedList; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class ShardingTableMapperRuleTest { + + private ShardingTableMapperRule tableMapperRule; + + @BeforeEach + void setUp() { + ShardingTable shardingTable = mock(ShardingTable.class); + when(shardingTable.getLogicTable()).thenReturn("foo_tbl"); + when(shardingTable.getActualDataNodes()).thenReturn(Collections.singletonList(new DataNode("foo_ds.foo_tbl_0"))); + tableMapperRule = new ShardingTableMapperRule(Collections.singletonMap("foo_tbl", shardingTable)); + } + + @Test + void assertGetLogicTableMapper() { + assertThat(new LinkedList<>(tableMapperRule.getLogicTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl"))); + } + + @Test + void assertGetActualTableMapper() { + assertThat(new LinkedList<>(tableMapperRule.getActualTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl_0"))); + } + + @Test + void assertGetDistributedTableMapper() { + assertThat(new LinkedList<>(tableMapperRule.getDistributedTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl"))); + } + + @Test + void assertGetEnhancedTableMapper() { + assertThat(new LinkedList<>(tableMapperRule.getEnhancedTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl"))); + } +} diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/SingleTableMapperRuleTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/SingleTableMapperRuleTest.java new file mode 100644 index 0000000000000..cbd5fe2d64272 --- /dev/null +++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/SingleTableMapperRuleTest.java @@ -0,0 +1,47 @@ +/* + * 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.single.rule; + +import org.apache.shardingsphere.infra.datanode.DataNode; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.LinkedList; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +class SingleTableMapperRuleTest { + + private final SingleTableMapperRule tableMapperRule = new SingleTableMapperRule(Collections.singletonMap("foo_tbl", Collections.singleton(new DataNode("foo_ds.foo_tbl")))); + + @Test + void assertGetLogicTableMapper() { + assertThat(new LinkedList<>(tableMapperRule.getLogicTableMapper().getTableNames()), is(Collections.singletonList("foo_tbl"))); + } + + @Test + void assertGetDistributedTableMapper() { + assertThat(new LinkedList<>(tableMapperRule.getDistributedTableMapper().getTableNames()), is(Collections.emptyList())); + } + + @Test + void assertGetEnhancedTableMapper() { + assertThat(new LinkedList<>(tableMapperRule.getEnhancedTableMapper().getTableNames()), is(Collections.emptyList())); + } +}