未验证 提交 4d7a8bd8 编写于 作者: Y Yanjie Zhou 提交者: GitHub

Add rules assertions for SpringBootStarterTest (#7023)

* Add sharding rule assertion

* Add encrypt rule assertion

* Add master-slave rule assertion
Add shadow rule assertion
上级 c4fc6d75
......@@ -17,13 +17,23 @@
package org.apache.shardingsphere.spring.boot;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.datanode.DataNodeUtil;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.masterslave.algorithm.RandomMasterSlaveLoadBalanceAlgorithm;
import org.apache.shardingsphere.masterslave.rule.MasterSlaveDataSourceRule;
import org.apache.shardingsphere.masterslave.rule.MasterSlaveRule;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.TableRule;
import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import org.apache.shardingsphere.sharding.strategy.ShardingStrategy;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.SpringBootApplication;
......@@ -32,9 +42,16 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
......@@ -57,7 +74,7 @@ public class SpringBootStarterTest {
@Test
public void assertRules() {
Collection<ShardingSphereRule> rules = dataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getRules();
assertThat(rules.size(), is(2));
assertThat(rules.size(), is(4));
for (ShardingSphereRule each : rules) {
if (each instanceof ShardingRule) {
assertShardingRule((ShardingRule) each);
......@@ -72,19 +89,59 @@ public class SpringBootStarterTest {
}
private void assertShardingRule(final ShardingRule rule) {
// TODO
assertThat(rule.getDataSourceNames(), is(Sets.newHashSet("ds_0", "ds_1")));
Map<String, ShardingAlgorithm> shardingAlgorithmMap = rule.getShardingAlgorithms();
assertNotNull(shardingAlgorithmMap);
InlineShardingAlgorithm databaseShardingAlgorithm = InlineShardingAlgorithm.class.cast(shardingAlgorithmMap.get("databaseShardingAlgorithm"));
assertThat(databaseShardingAlgorithm.getProps().getProperty("algorithm.expression"), is("ds_$->{user_id % 2}"));
InlineShardingAlgorithm orderTableShardingAlgorithm = InlineShardingAlgorithm.class.cast(shardingAlgorithmMap.get("orderTableShardingAlgorithm"));
assertThat(orderTableShardingAlgorithm.getProps().getProperty("algorithm.expression"), is("t_order_$->{order_id % 2}"));
Collection<TableRule> tableRules = rule.getTableRules();
assertNotNull(tableRules);
assertThat(tableRules.size(), equalTo(1));
TableRule tableRule = tableRules.iterator().next();
assertThat(tableRule.getLogicTable(), is("t_order"));
List<DataNode> dataNodes = Arrays.asList(new DataNode("ds_0.t_order_0"),
new DataNode("ds_0.t_order_1"), new DataNode("ds_1.t_order_0"), new DataNode("ds_1.t_order_1"));
assertThat(tableRule.getActualDataNodes(), is(dataNodes));
assertThat(tableRule.getActualDatasourceNames(), is(Sets.newHashSet("ds_0", "ds_1")));
assertThat(tableRule.getDataNodeGroups(), is(DataNodeUtil.getDataNodeGroups(dataNodes)));
assertThat(tableRule.getDatasourceToTablesMap(), is(ImmutableMap.of("ds_1",
Sets.newHashSet("t_order_0", "t_order_1"), "ds_0", Sets.newHashSet("t_order_0", "t_order_1"))));
ShardingStrategy databaseShardingStrategy = tableRule.getDatabaseShardingStrategy();
assertNotNull(databaseShardingStrategy);
assertThat(databaseShardingStrategy.getShardingColumns(), is(Sets.newTreeSet(Collections.singleton("user_id"))));
assertThat(databaseShardingStrategy.getShardingAlgorithm().getProps().getProperty("algorithm.expression"), is("ds_$->{user_id % 2}"));
ShardingStrategy tableShardingStrategy = tableRule.getTableShardingStrategy();
assertNotNull(tableShardingStrategy);
assertThat(tableShardingStrategy.getShardingColumns(), is(Sets.newTreeSet(Collections.singleton("order_id"))));
assertThat(tableShardingStrategy.getShardingAlgorithm().getProps().getProperty("algorithm.expression"), is("t_order_$->{order_id % 2}"));
}
private void assertMasterSlaveRule(final MasterSlaveRule rule) {
// TODO
assertThat(rule.getDataSourceMapper(), is(Collections.singletonMap("ds_ms", Arrays.asList("ds_master", "ds_slave_0", "ds_slave_1"))));
MasterSlaveDataSourceRule masterSlaveDataSourceRule = rule.getSingleDataSourceRule();
assertNotNull(masterSlaveDataSourceRule);
assertThat(masterSlaveDataSourceRule.getName(), is("ds_ms"));
assertThat(masterSlaveDataSourceRule.getMasterDataSourceName(), is("ds_master"));
assertThat(masterSlaveDataSourceRule.getSlaveDataSourceNames(), is(Arrays.asList("ds_slave_0", "ds_slave_1")));
assertThat(masterSlaveDataSourceRule.getLoadBalancer(), instanceOf(RandomMasterSlaveLoadBalanceAlgorithm.class));
assertThat(masterSlaveDataSourceRule.getDataSourceMapper(), is(Collections.singletonMap("ds_ms", Arrays.asList("ds_master", "ds_slave_0", "ds_slave_1"))));
}
private void assertEncryptRule(final EncryptRule rule) {
// TODO
assertThat(rule.getEncryptTableNames(), is(Sets.newLinkedHashSet(Arrays.asList("t_order"))));
assertThat(rule.getCipherColumn("t_order", "pwd"), is("pwd_cipher"));
assertThat(rule.getAssistedQueryColumns("t_order"), is(Collections.singletonList("pwd_assisted_query_cipher")));
assertThat(rule.getLogicAndCipherColumns("t_order"), is(Collections.singletonMap("pwd", "pwd_cipher")));
assertThat(rule.getLogicColumnOfCipher("t_order", "pwd_cipher"), is("pwd"));
assertThat(rule.getEncryptValues("t_order", "pwd", Collections.singletonList("pwd_plain")), is(Collections.singletonList("V/RkV1+dVv80Y3csT3cR4g==")));
assertThat(rule.getAssistedQueryAndPlainColumns("t_order"), is(Arrays.asList("pwd_assisted_query_cipher", "pwd_plain")));
}
private void assertShadowRule(final ShadowRule rule) {
// TODO
assertThat(rule.getColumn(), is("shadow"));
assertThat(rule.getShadowMappings(), is(Collections.singletonMap("ds", "shadow_ds")));
}
@Test
......
......@@ -20,7 +20,12 @@ spring.shardingsphere.datasource.names=ds_${0..1}
spring.shardingsphere.datasource.ds_0.type=org.apache.shardingsphere.test.MockedDataSource
spring.shardingsphere.datasource.ds_1.type=org.apache.shardingsphere.test.MockedDataSource
# TODO add master-slave config
spring.shardingsphere.rules.master-slave.load-balancers.random.type=RANDOM
spring.shardingsphere.rules.master-slave.data-sources.ds_ms.master-data-source-name=ds_master
spring.shardingsphere.rules.master-slave.data-sources.ds_ms.slave-data-source-names=ds_slave_0,ds_slave_1
spring.shardingsphere.rules.master-slave.data-sources.ds_ms.load-balancer-name=random
spring.shardingsphere.rules.sharding.sharding-algorithms.databaseShardingAlgorithm.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.databaseShardingAlgorithm.props.algorithm.expression=ds_$->{user_id % 2}
......@@ -39,10 +44,12 @@ spring.shardingsphere.rules.encrypt.encryptors.aesEncryptor.type=AES
spring.shardingsphere.rules.encrypt.encryptors.aesEncryptor.props.aes.key.value=123456
spring.shardingsphere.rules.encrypt.tables.t_order.columns.pwd.cipher-column=pwd_cipher
spring.shardingsphere.rules.encrypt.tables.t_order.columns.pwd.assisted-query-column=pwd_assisted_query_cipher
spring.shardingsphere.rules.encrypt.tables.t_order.columns.pwd.plain-column=pwd_plain
spring.shardingsphere.rules.encrypt.tables.t_order.columns.pwd.encryptor-name=aesEncryptor
# TODO add shadow config
spring.shardingsphere.rules.shadow.column=shadow
spring.shardingsphere.rules.shadow.shadowMappings.ds=shadow_ds
spring.shardingsphere.props.sql.show=true
spring.shardingsphere.props.executor.size=10
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册