/*
 * 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.internal.registry.config.listener;

import io.shardingsphere.api.config.MasterSlaveRuleConfiguration;
import io.shardingsphere.api.config.ShardingRuleConfiguration;
import io.shardingsphere.orchestration.internal.registry.config.event.MasterSlaveRuleChangedEvent;
import io.shardingsphere.orchestration.internal.registry.config.event.ShardingRuleChangedEvent;
import io.shardingsphere.orchestration.internal.registry.config.service.ConfigurationService;
import io.shardingsphere.orchestration.reg.api.RegistryCenter;
import io.shardingsphere.orchestration.reg.listener.DataChangedEvent;
import io.shardingsphere.orchestration.util.FieldUtil;
import lombok.SneakyThrows;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public final class RuleChangedListenerTest {
    
    private RuleChangedListener ruleChangedListener;
    
    @Mock
    private RegistryCenter regCenter;
    
    @Mock
    private ConfigurationService configService;
    
    @Before
    @SneakyThrows
    public void setUp() {
        ruleChangedListener = new RuleChangedListener("test", regCenter, "sharding_schema");
        FieldUtil.setField(ruleChangedListener, "configService", configService);
    }
    
    @Test
    public void assertCreateShardingOrchestrationEventForSharding() {
        when(configService.isShardingRule("sharding_schema")).thenReturn(true);
        ShardingRuleConfiguration expected = mock(ShardingRuleConfiguration.class);
        when(configService.loadShardingRuleConfiguration("sharding_schema")).thenReturn(expected);
        ShardingRuleChangedEvent actual = (ShardingRuleChangedEvent) ruleChangedListener.createShardingOrchestrationEvent(mock(DataChangedEvent.class));
        assertThat(actual.getShardingSchemaName(), is("sharding_schema"));
        assertThat(actual.getShardingRuleConfiguration(), is(expected));
    }
    
    @Test
    public void assertCreateShardingOrchestrationEventForMasterSlave() {
        MasterSlaveRuleConfiguration expected = mock(MasterSlaveRuleConfiguration.class);
        when(configService.loadMasterSlaveRuleConfiguration("sharding_schema")).thenReturn(expected);
        MasterSlaveRuleChangedEvent actual = (MasterSlaveRuleChangedEvent) ruleChangedListener.createShardingOrchestrationEvent(mock(DataChangedEvent.class));
        assertThat(actual.getShardingSchemaName(), is("sharding_schema"));
        assertThat(actual.getMasterSlaveRuleConfiguration(), is(expected));
    }
}

@@ -693,137 +693,8 @@
 r;%0A%0A
-import io.shardingsphere.api.config.MasterSlaveRuleConfiguration;%0Aimport io.shardingsphere.api.config.ShardingRuleConfiguration;%0A
 impo
@@ -888,102 +888,8 @@
 nt;%0A
-import io.shardingsphere.orchestration.internal.registry.config.service.ConfigurationService;%0A
 impo
@@ -1060,22 +1060,49 @@
 ion.
-util.FieldUtil
+reg.listener.DataChangedEvent.ChangedType
 ;%0Aim
@@ -1369,88 +1369,8 @@
 hat;
-%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.when;
 %0A%0A@R
@@ -1448,24 +1448,587 @@
 Test %7B%0A    %0A
+    private static final String SHARDING_RULE_YAML = %22tables:%5Cn%22 + %22  t_order:%5Cn%22 + %22    actualDataNodes: ds_$%7B0..1%7D.t_order_$%7B0..1%7D%5Cn%22%0A            + %22    keyGeneratorColumnName: order_id%5Cn%22 + %22    logicTable: t_order%5Cn%22 + %22    tableStrategy:%5Cn%22 + %22      inline:%5Cn%22%0A            + %22        algorithmExpression: t_order_$%7Border_id %25 2%7D%5Cn%22 + %22        shardingColumn: order_id%22;%0A    %0A    private static final String MASTER_SLAVE_RULE_YAML = %22masterDataSourceName: master_ds%5Cn%22 + %22name: ms_ds%5Cn%22 + %22slaveDataSourceNames:%5Cn%22 + %22- slave_ds_0%5Cn%22 + %22- slave_ds_1%5Cn%22;%0A    %0A
     private 
@@ -2130,71 +2130,8 @@
    %0A
-    @Mock%0A    private ConfigurationService configService;%0A    %0A
     
@@ -2279,89 +2279,8 @@
 %22);%0A
-        FieldUtil.setField(ruleChangedListener, %22configService%22, configService);%0A
     
@@ -2378,260 +2378,112 @@
     
-when(configService.isShardingRule(%22sharding_schema%22)).thenReturn(true);%0A        ShardingRuleConfiguration expected = mock(ShardingRuleConfiguration.class);%0A        when(configService.loadShardingRuleConfiguration(%22sharding_schema%22)).thenReturn(expected
+DataChangedEvent dataChangedEvent = new DataChangedEvent(%22test%22, SHARDING_RULE_YAML, ChangedType.UPDATED
 );%0A 
@@ -2595,38 +2595,33 @@
 hestrationEvent(
-mock(D
+d
 ataChangedEvent.
@@ -2611,39 +2611,32 @@
 dataChangedEvent
-.class)
 );%0A        asser
@@ -2749,37 +2749,59 @@
 figuration()
-, is(expected
+.getTableRuleConfigs().size(), is(1
 ));%0A    %7D%0A  
@@ -2898,189 +2898,116 @@
     
-MasterSlaveRuleConfiguration expected = mock(MasterSlaveRuleConfiguration.class);%0A        when(configService.loadMasterSlaveRuleConfiguration(%22sharding_schema%22)).thenReturn(expected
+DataChangedEvent dataChangedEvent = new DataChangedEvent(%22test%22, MASTER_SLAVE_RULE_YAML, ChangedType.UPDATED
 );%0A 
@@ -3137,14 +3137,9 @@
 ent(
-mock(D
+d
 ataC
@@ -3149,23 +3149,16 @@
 gedEvent
-.class)
 );%0A     
@@ -3290,21 +3290,50 @@
 on()
-, is(expected
+.getMasterDataSourceName(), is(%22master_ds%22
 ));%0A

modify assertCreateShardingOrchestrationEventForSharding()