Skip to content

Commit

Permalink
Add TableMapperRuleTest (#30323)
Browse files Browse the repository at this point in the history
* Add EncryptTableMapperRuleTest

* Add ShardingTableMapperRuleTest

* Add BroadcastTableMapperRuleTest

* Add MaskTableMapperRuleTest

* Add MaskTableMapperRuleTest

* Add SingleTableMapperRuleTest
  • Loading branch information
terrymanu authored Feb 27, 2024
1 parent 99a65f3 commit 616c247
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"));
Expand All @@ -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<String, AlgorithmConfiguration> getEncryptors(final AlgorithmConfiguration standardEncryptConfig, final AlgorithmConfiguration queryAssistedEncryptConfig,
final AlgorithmConfiguration queryLikeEncryptConfig) {
Map<String, AlgorithmConfiguration> result = new HashMap<>(2, 1F);
Map<String, AlgorithmConfiguration> result = new HashMap<>(3, 1F);
result.put("standard_encryptor", standardEncryptConfig);
result.put("assisted_encryptor", queryAssistedEncryptConfig);
result.put("like_encryptor", queryLikeEncryptConfig);
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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")));
}
}
Original file line number Diff line number Diff line change
@@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
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;
import org.apache.shardingsphere.infra.instance.InstanceContext;
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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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")));
}
}
Original file line number Diff line number Diff line change
@@ -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()));
}
}

0 comments on commit 616c247

Please sign in to comment.