package io.shardingsphere.core.metadata.datasource;

import com.google.common.collect.Lists;
import io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration;
import io.shardingsphere.core.api.config.ShardingRuleConfiguration;
import io.shardingsphere.core.api.config.TableRuleConfiguration;
import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.rule.ShardingRule;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class ShardingDataSourceMetaDataTest {
    
    private ShardingDataSourceMetaData masterSlaveShardingDataSourceMetaData;
    
    private ShardingDataSourceMetaData shardingDataSourceMetaData;
    
    @Before
    public void setUp() {
        Map<String, String> masterSlaveShardingDataSourceURLs = new LinkedHashMap<String, String>() {{ put("master_0", "jdbc:mysql://127.0.0.1:3306/master_0");
        put("master_1", "jdbc:mysql://127.0.0.1:3306/master_1"); put("master_2", "jdbc:mysql://127.0.0.1:3307/master_2"); put("slave_0", "jdbc:mysql://127.0.0.2:3306/slave_0");
            put("slave_1", "jdbc:mysql://127.0.0.2:3306/slave_1"); put("slave_2", "jdbc:mysql://127.0.0.2:3307/slave_2");}};
        masterSlaveShardingDataSourceMetaData = new ShardingDataSourceMetaData(masterSlaveShardingDataSourceURLs, getMasterSlaveShardingRule(), DatabaseType.MySQL);
        Map<String, String> shardingDataSourceURLs = new LinkedHashMap<String, String>() {{ put("ds_0", "jdbc:mysql://127.0.0.1:3306/db_0"); put("ds_1", "jdbc:mysql://127.0.0.1:3306/db_1"); }};
        shardingDataSourceMetaData = new ShardingDataSourceMetaData(shardingDataSourceURLs, getShardingRule(), DatabaseType.MySQL);
    }
    
    private ShardingRule getMasterSlaveShardingRule() {
        TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
        tableRuleConfig.setLogicTable("t_order");
        tableRuleConfig.setActualDataNodes("ms_${0..2}.t_order_${0..1}");
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        MasterSlaveRuleConfiguration msConfig_0 = new MasterSlaveRuleConfiguration("ms_0", "master_0", Arrays.asList("slave_0"));
        MasterSlaveRuleConfiguration msConfig_1 = new MasterSlaveRuleConfiguration("ms_1", "master_1", Arrays.asList("slave_1"));
        MasterSlaveRuleConfiguration msConfig_2 = new MasterSlaveRuleConfiguration("ms_2", "master_2", Arrays.asList("slave_2"));
        shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
        shardingRuleConfig.getMasterSlaveRuleConfigs().addAll(Lists.newArrayList(msConfig_0, msConfig_1, msConfig_2));
        return new ShardingRule(shardingRuleConfig, Lists.newArrayList("master_0", "master_1", "master_2", "slave_0", "slave_1", "slave_2"));
    }
    
    private ShardingRule getShardingRule() {
        TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
        tableRuleConfig.setLogicTable("t_order");
        tableRuleConfig.setActualDataNodes("ds_${0..1}.t_order_${0..1}");
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
        return new ShardingRule(shardingRuleConfig, Lists.newArrayList("ds_0", "ds_1"));
    }
    
    @Test
    public void testGetAllInstanceDataSourceNamesForMasterSlaveShardingRule() {
        assertEquals(masterSlaveShardingDataSourceMetaData.getAllInstanceDataSourceNames(), Lists.newArrayList("ms_0", "ms_2"));
    }
    
    @Test
    public void testGetAllInstanceDataSourceNamesForShardingRule() {
        assertEquals(shardingDataSourceMetaData.getAllInstanceDataSourceNames(), Lists.newArrayList("ds_0"));
    }
    
    @Test
    public void testGetActualSchemaNameForMasterSlaveShardingRule() {
        assertEquals(masterSlaveShardingDataSourceMetaData.getActualSchemaName("ms_0"), "master_0");
    }
    
    @Test
    public void testGetActualSchemaNameForShardingRule() {
        assertEquals(shardingDataSourceMetaData.getActualSchemaName("ds_0"), "db_0");
    }
}
@@ -909,24 +909,77 @@
 String%3E() %7B%7B
+ put(%22single%22, %22jdbc:mysql://127.0.0.1:3306/single%22);
  put(%22master
@@ -1922,32 +1922,34 @@
  tableRuleConfig
+_0
  = new TableRule
@@ -1980,32 +1980,34 @@
  tableRuleConfig
+_0
 .setLogicTable(%22
@@ -2032,32 +2032,34 @@
  tableRuleConfig
+_0
 .setActualDataNo
@@ -2085,32 +2085,239 @@
 rder_$%7B0..1%7D%22);%0A
+        TableRuleConfiguration tableRuleConfig_1 = new TableRuleConfiguration();%0A        tableRuleConfig_1.setLogicTable(%22t_order_item%22);%0A        tableRuleConfig_1.setActualDataNodes(%22single.t_order_item%22);%0A
         Sharding
@@ -2421,26 +2421,35 @@
 nfiguration 
-ms
+MasterSlave
 Config_0 = n
@@ -2560,26 +2560,35 @@
 nfiguration 
-ms
+MasterSlave
 Config_1 = n
@@ -2703,18 +2703,27 @@
 uration 
-ms
+MasterSlave
 Config_2
@@ -2865,32 +2865,107 @@
 (tableRuleConfig
+_0);%0A        shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig_1
 );%0A        shard
@@ -3036,34 +3036,61 @@
 ist(
-msConfig_0, msConfig_1, ms
+MasterSlaveConfig_0, MasterSlaveConfig_1, MasterSlave
 Conf
@@ -3168,16 +3168,26 @@
 rayList(
+%22single%22, 
 %22master_
@@ -3969,20 +3969,22 @@
 ayList(%22
-ms_0
+single
 %22, %22ms_2

add single data source