Search in sources :

Example 1 with DataNode

use of com.dangdang.ddframe.rdb.sharding.api.rule.DataNode in project sharding-jdbc by dangdangdotcom.

the class ShardingRuleBuilderTest method assertBuildSuccess.

@Test
public void assertBuildSuccess() {
    ShardingRuleConfig shardingRuleConfig = new ShardingRuleConfig();
    shardingRuleConfig.setDataSource(createDataSourceMap());
    shardingRuleConfig.setDefaultDataSourceName("ds_0");
    shardingRuleConfig.setTables(createTableRuleConfigMap());
    shardingRuleConfig.setIdGeneratorClass("com.dangdang.ddframe.rdb.sharding.config.common.fixture.IncrementIdGenerator");
    shardingRuleConfig.setBindingTables(Collections.singletonList(createBindingTableRule("t_order", "t_order_item")));
    shardingRuleConfig.setDefaultDatabaseStrategy(getDatabaseStrategyConfig(SingleAlgorithm.class.getName()));
    shardingRuleConfig.setDefaultTableStrategy(getTableStrategyConfigForAlgorithmClass());
    ShardingRule actual = new ShardingRuleBuilder(shardingRuleConfig).build();
    assertThat(actual.getDataSourceRule().getDataSourceNames().size(), is(2));
    assertThat(actual.getDataSourceRule().getDataSourceNames(), hasItem("ds_0"));
    assertThat(actual.getDataSourceRule().getDataSourceNames(), hasItem("ds_1"));
    assertThat(actual.getTableRules().size(), is(3));
    for (TableRule each : actual.getTableRules()) {
        String logicTable = each.getLogicTable();
        switch(logicTable) {
            case "t_order":
                assertFalse(each.isDynamic());
                assertThat(each.getActualTables().size(), is(4));
                assertThat(each.getActualTables(), hasItem(new DataNode("ds_0", "t_order_0")));
                assertThat(each.getActualTables(), hasItem(new DataNode("ds_0", "t_order_0")));
                assertThat(each.getActualTables(), hasItem(new DataNode("ds_1", "t_order_0")));
                assertThat(each.getActualTables(), hasItem(new DataNode("ds_1", "t_order_1")));
                break;
            case "t_order_item":
                assertFalse(each.isDynamic());
                assertThat(each.getActualTables().size(), is(4));
                assertThat(each.getActualTables(), hasItem(new DataNode("ds_0", "t_order_item_0")));
                assertThat(each.getActualTables(), hasItem(new DataNode("ds_0", "t_order_item_0")));
                assertThat(each.getActualTables(), hasItem(new DataNode("ds_1", "t_order_item_0")));
                assertThat(each.getActualTables(), hasItem(new DataNode("ds_1", "t_order_item_1")));
                break;
            case "t_log":
                assertTrue(each.isDynamic());
                assertThat(each.getActualTables().size(), is(2));
                assertThat(each.getActualTables(), hasItem(new DataNode("ds_0", "SHARDING_JDBC DYNAMIC_TABLE_PLACEHOLDER")));
                assertThat(each.getActualTables(), hasItem(new DataNode("ds_1", "SHARDING_JDBC DYNAMIC_TABLE_PLACEHOLDER")));
                break;
            default:
                fail();
        }
    }
    assertThat(actual.getBindingTableRules().size(), is(1));
    BindingTableRule bindingTableRule = actual.getBindingTableRules().iterator().next();
    assertTrue(bindingTableRule.hasLogicTable("t_order"));
    assertTrue(bindingTableRule.hasLogicTable("t_order_item"));
    assertFalse(bindingTableRule.hasLogicTable("t_log"));
    assertThat(actual.getDatabaseShardingStrategy().getShardingColumns().size(), is(1));
    assertThat(actual.getDatabaseShardingStrategy().getShardingColumns().iterator().next(), is("uid"));
    assertThat(actual.getTableShardingStrategy().getShardingColumns().size(), is(1));
    assertThat(actual.getTableShardingStrategy().getShardingColumns().iterator().next(), is("oid"));
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) ShardingRuleConfig(com.dangdang.ddframe.rdb.sharding.config.common.api.config.ShardingRuleConfig) DataNode(com.dangdang.ddframe.rdb.sharding.api.rule.DataNode) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule) Test(org.junit.Test)

Aggregations

BindingTableRule (com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule)1 DataNode (com.dangdang.ddframe.rdb.sharding.api.rule.DataNode)1 ShardingRule (com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule)1 TableRule (com.dangdang.ddframe.rdb.sharding.api.rule.TableRule)1 ShardingRuleConfig (com.dangdang.ddframe.rdb.sharding.config.common.api.config.ShardingRuleConfig)1 Test (org.junit.Test)1