/*
 * 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 org.apache.shardingsphere.shardingjdbc.orchestration.internal.datasource;

import lombok.SneakyThrows;
import org.apache.shardingsphere.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithmType;
import org.apache.shardingsphere.api.config.rule.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.core.constant.ShardingConstant;
import org.apache.shardingsphere.orchestration.config.OrchestrationConfiguration;
import org.apache.shardingsphere.orchestration.internal.registry.config.event.MasterSlaveRuleChangedEvent;
import org.apache.shardingsphere.orchestration.reg.api.RegistryCenterConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlMasterSlaveDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.MasterSlaveDataSource;
import org.apache.shardingsphere.shardingjdbc.orchestration.user.YamlUserTest;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.Collections;

public class OrchestrationMasterSlaveDataSourceTest {
    
    private OrchestrationMasterSlaveDataSource masterSlaveDataSource;
    
    @Before
    @SneakyThrows
    public void setUp() {
        masterSlaveDataSource = new OrchestrationMasterSlaveDataSource(getMasterSlaveDataSource(), getOrchestrationConfiguration());
        
    }
    
    @SneakyThrows
    private MasterSlaveDataSource getMasterSlaveDataSource() {
        File yamlFile = new File(YamlUserTest.class.getResource("/yaml/unit/masterSlave.yaml").toURI());
        return (MasterSlaveDataSource) YamlMasterSlaveDataSourceFactory.createDataSource(yamlFile);
    }
    
    private OrchestrationConfiguration getOrchestrationConfiguration() {
        return new OrchestrationConfiguration("test", new RegistryCenterConfiguration(), true);
    }
    
    @Test
    public void assertRenewRule() {
    }
    
    private MasterSlaveRuleChangedEvent getMasterSlaveRuleChangedEvent() {
        MasterSlaveRuleConfiguration masterSlaveRuleConfiguration = new MasterSlaveRuleConfiguration("ms", "ds_m", Collections.singletonList("ds_s"), MasterSlaveLoadBalanceAlgorithmType.ROUND_ROBIN.getAlgorithm());
        return new MasterSlaveRuleChangedEvent(ShardingConstant.LOGIC_SCHEMA_NAME, masterSlaveRuleConfiguration);
    }
    
    @Test
    public void assertRenewDataSource() {
    }
    
    @Test
    public void assertRenewProperties() {
    }
    
    @Test
    public void assertRenewConfigMap() {
    }
    
    @Test
    public void assertRenewDisabledState() {
    }
    
    @Test
    public void assertRenewCircuitState() {
    }
    
    @Test
    public void assertGetDataSource() {
    }
}

@@ -1038,32 +1038,134 @@
 nConfiguration;%0A
+import org.apache.shardingsphere.orchestration.internal.registry.config.event.DataSourceChangedEvent;%0A
 import org.apach
@@ -1698,16 +1698,104 @@
 tions;%0A%0A
+import static org.hamcrest.CoreMatchers.is;%0Aimport static org.junit.Assert.assertThat;%0A%0A
 public c
@@ -2495,24 +2495,256 @@
 uration() %7B%0A
+        RegistryCenterConfiguration registryCenterConfiguration = new RegistryCenterConfiguration();%0A        registryCenterConfiguration.setNamespace(%22test_ms%22);%0A        registryCenterConfiguration.setServerLists(%22localhost:2181%22);%0A
         retu
@@ -2785,21 +2785,17 @@
 %22test%22, 
-new R
+r
 egistryC
@@ -2812,18 +2812,16 @@
 guration
-()
 , true);
@@ -2874,24 +2874,199 @@
 newRule() %7B%0A
+        masterSlaveDataSource.renew(getMasterSlaveRuleChangedEvent());%0A        assertThat(masterSlaveDataSource.getDataSource().getMasterSlaveRule().getName(), is(%22new_ms%22));%0A
     %7D%0A    %0A 
@@ -3241,16 +3241,20 @@
 ration(%22
+new_
 ms%22, %22ds
@@ -3531,24 +3531,118 @@
 aSource() %7B%0A
+        %0A    %7D%0A    %0A    private DataSourceChangedEvent getDataSourceChangedEvent() %7B%0A        %0A
     %7D%0A    %0A 

add getMasterSlaveRuleChangedEvent()