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("single", "jdbc:mysql://127.0.0.1:3306/single"); 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 tableRuleConfig0 = new TableRuleConfiguration();
        tableRuleConfig0.setLogicTable("t_order");
        tableRuleConfig0.setActualDataNodes("ms_${0..2}.t_order_${0..1}");
        TableRuleConfiguration tableRuleConfig1 = new TableRuleConfiguration();
        tableRuleConfig1.setLogicTable("t_order_item");
        tableRuleConfig1.setActualDataNodes("single.t_order_item");
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        MasterSlaveRuleConfiguration MasterSlaveConfig0 = new MasterSlaveRuleConfiguration("ms_0", "master_0", Arrays.asList("slave_0"));
        MasterSlaveRuleConfiguration MasterSlaveConfig1 = new MasterSlaveRuleConfiguration("ms_1", "master_1", Arrays.asList("slave_1"));
        MasterSlaveRuleConfiguration MasterSlaveConfig2 = new MasterSlaveRuleConfiguration("ms_2", "master_2", Arrays.asList("slave_2"));
        shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig0);
        shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig1);
        shardingRuleConfig.getMasterSlaveRuleConfigs().addAll(Lists.newArrayList(MasterSlaveConfig0, MasterSlaveConfig1, MasterSlaveConfig2));
        return new ShardingRule(shardingRuleConfig, Lists.newArrayList("single", "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 assertGetAllInstanceDataSourceNamesForMasterSlaveShardingRule() {
        assertEquals(masterSlaveShardingDataSourceMetaData.getAllInstanceDataSourceNames(), Lists.newArrayList("single", "ms_2"));
    }
    
    @Test
    public void assertGetAllInstanceDataSourceNamesForShardingRule() {
        assertEquals(shardingDataSourceMetaData.getAllInstanceDataSourceNames(), Lists.newArrayList("ds_0"));
    }
    
    @Test
    public void assertGetActualSchemaNameForMasterSlaveShardingRule() {
        assertEquals(masterSlaveShardingDataSourceMetaData.getActualSchemaName("ms_0"), "master_0");
    }
    
    @Test
    public void assertGetActualSchemaNameForShardingRule() {
        assertEquals(shardingDataSourceMetaData.getActualSchemaName("ds_0"), "db_0");
    }
}
@@ -893,37 +893,63 @@
 HashMap%3C
-String, String%3E() %7B%7B 
+%3E();%0A        masterSlaveShardingDataSourceURLs.
 put(%22sin
@@ -992,17 +992,59 @@
 ingle%22);
- 
+%0A        masterSlaveShardingDataSourceURLs.
 put(%22mas
@@ -1096,24 +1096,58 @@
 %22);%0A        
+masterSlaveShardingDataSourceURLs.
 put(%22master_
@@ -1186,25 +1186,67 @@
 /master_1%22);
- 
+%0A        masterSlaveShardingDataSourceURLs.
 put(%22master_
@@ -1289,17 +1289,59 @@
 ter_2%22);
- 
+%0A        masterSlaveShardingDataSourceURLs.
 put(%22sla
@@ -1395,16 +1395,50 @@
         
+masterSlaveShardingDataSourceURLs.
 put(%22sla
@@ -1483,17 +1483,59 @@
 ave_1%22);
- 
+%0A        masterSlaveShardingDataSourceURLs.
 put(%22sla
@@ -1580,19 +1580,16 @@
 ave_2%22);
-%7D%7D;
 %0A       
@@ -1838,12 +1838,41 @@
 g%3E()
- %7B%7B 
+;%0A        shardingDataSourceURLs.
 put(
@@ -1915,17 +1915,48 @@
 /db_0%22);
- 
+%0A        shardingDataSourceURLs.
 put(%22ds_
@@ -1999,12 +1999,8 @@
 1%22);
- %7D%7D;
 %0A   
@@ -2722,33 +2722,33 @@
 leConfiguration 
-M
+m
 asterSlaveConfig
@@ -2860,33 +2860,33 @@
 leConfiguration 
-M
+m
 asterSlaveConfig
@@ -3006,17 +3006,17 @@
 uration 
-M
+m
 asterSla
@@ -3332,17 +3332,17 @@
 rayList(
-M
+m
 asterSla
@@ -3352,17 +3352,17 @@
 onfig0, 
-M
+m
 asterSla
@@ -3372,17 +3372,17 @@
 onfig1, 
-M
+m
 asterSla

use multiple put functions