/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed 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.
 * </p>
 */

package io.shardingsphere.orchestration.yaml;

import io.shardingsphere.api.config.ShardingRuleConfiguration;
import io.shardingsphere.core.config.DataSourceConfiguration;
import org.junit.Test;

import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertTrue;

public class ConfigurationYamlConverterTest {
    
    private static final String DATA_SOURCE_YAML = "master_ds: !!io.shardingsphere.orchestration.yaml.YamlDataSourceConfiguration\n"
            + "  dataSourceClassName: com.zaxxer.hikari.HikariDataSource\n" + "  properties:\n" + "    DATA_SOURCE_POOL_CLASS_NAME: com.zaxxer.hikari.HikariDataSource\n"
            + "    proxyDatasourceType: !!io.shardingsphere.core.constant.transaction.ProxyPoolType 'VENDOR'\n"
            + "    url: jdbc:mysql://localhost:3306/demo_ds_master\n" + "    username: root\n" + "    password: null\n";
    
    private static final String SHARDING_RULE_YAML = "  tables:\n" + "    t_order:\n" + "      actualDataNodes: ds_${0..1}.t_order_${0..1}\n" + "      tableStrategy:\n"
            + "        inline:\n" + "          shardingColumn: order_id\n" + "          algorithmExpression: t_order_${order_id % 2}\n"
            + "      keyGeneratorColumnName: order_id\n" + "    t_order_item:\n" + "      actualDataNodes: ds_${0..1}.t_order_item_${0..1}\n" + "      tableStrategy:\n" + "        inline:\n"
            + "          shardingColumn: order_id\n" + "          algorithmExpression: t_order_item_${order_id % 2}\n" + "      keyGeneratorColumnName: order_item_id\n" + "  bindingTables:\n"
            + "    - t_order,t_order_item\n" + "  defaultDataSourceName: ds_1\n" + "  defaultDatabaseStrategy:\n" + "    inline:\n" + "      shardingColumn: user_id\n"
            + "      algorithmExpression: ds_${user_id % 2}";
    
    private static final String MASTER_SLAVE_RULE_YAML = "masterDataSourceName: master_ds\n" + "name: ms_ds\n" + "slaveDataSourceNames:\n" + "- slave_ds_0\n" + "- slave_ds_1\n";
    
    private static final String AUTHENTICATION_YAML = "password: root\nusername: root\n";
    
    private static final String CONFIG_MAP_YAML = "sharding-key1: sharding-value1";
    
    private static final String PROPERTIES_YAML = "executor.size: 16\nsql.show: true";
    
    @Test
    public void assertLoadDataSourceConfigurations() {
        Map<String, DataSourceConfiguration> actual = ConfigurationYamlConverter.loadDataSourceConfigurations(DATA_SOURCE_YAML);
        assertThat(actual.size(), is(1));
        assertTrue(actual.keySet().contains("master_ds"));
    }
    
    @Test
    public void assertLoadShardingRuleConfiguration() {
        ShardingRuleConfiguration actual = ConfigurationYamlConverter.loadShardingRuleConfiguration(SHARDING_RULE_YAML);
        assertThat(actual.getTableRuleConfigs().size(), is(2));
        assertThat(actual.getBindingTableGroups().size(), is(1));
        assertThat(actual.getDefaultDataSourceName(), is("ds_1"));
    }
    
    @Test
    public void assertLoadMasterSlaveRuleConfiguration() {
    }
    
    @Test
    public void assertLoadAuthentication() {
    }
    
    @Test
    public void assertLoadConfigMap() {
    }
    
    @Test
    public void assertLoadProperties() {
    }
    
    @Test
    public void assertDumpDataSourceConfigurations() {
    }
    
    @Test
    public void assertDumpShardingRuleConfiguration() {
    }
    
    @Test
    public void assertDumpMasterSlaveRuleConfiguration() {
    }
    
    @Test
    public void assertDumpAuthentication() {
    }
    
    @Test
    public void assertDumpConfigMap() {
    }
    
    @Test
    public void assertDumpProperties() {
    }
}

@@ -660,16 +660,82 @@
 .yaml;%0A%0A
+import io.shardingsphere.api.config.MasterSlaveRuleConfiguration;%0A
 import i
@@ -847,24 +847,75 @@
 figuration;%0A
+import io.shardingsphere.core.rule.Authentication;%0A
 import org.j
@@ -947,16 +947,45 @@
 til.Map;
+%0Aimport java.util.Properties;
 %0A%0Aimport
@@ -3866,192 +3866,1101 @@
     
-%7D%0A    %0A    @Test%0A    public void assertLoadAuthentication() %7B%0A    %7D%0A    %0A    @Test%0A    public void assertLoadConfigMap() %7B%0A    %7D%0A    %0A    @Test%0A    public void assertLoadProperties() %7B
+    MasterSlaveRuleConfiguration actual = ConfigurationYamlConverter.loadMasterSlaveRuleConfiguration(MASTER_SLAVE_RULE_YAML);%0A        assertThat(actual.getMasterDataSourceName(), is(%22master_ds%22));%0A        assertThat(actual.getSlaveDataSourceNames().size(), is(2));%0A    %7D%0A    %0A    @Test%0A    public void assertLoadAuthentication() %7B%0A        Authentication actual = ConfigurationYamlConverter.loadAuthentication(AUTHENTICATION_YAML);%0A        assertThat(actual.getUsername(), is(%22root%22));%0A        assertThat(actual.getPassword(), is(%22root%22));%0A    %7D%0A    %0A    @Test%0A    public void assertLoadConfigMap() %7B%0A        Map%3CString, Object%3E actual = ConfigurationYamlConverter.loadConfigMap(CONFIG_MAP_YAML);%0A        assertTrue(actual.containsKey(%22sharding-key1%22));%0A        assertTrue(actual.containsValue(%22sharding-value1%22));%0A    %7D%0A    %0A    @Test%0A    public void assertLoadProperties() %7B%0A        Properties actual = ConfigurationYamlConverter.loadProperties(PROPERTIES_YAML);%0A        assertThat(actual.get(%22executor.size%22), is((Object) 16));%0A        assertThat(actual.get(%22sql.show%22), is((Object) true));
 %0A   

add load functions