/*
 * 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.orchestration.internal.registry.config.event.IgnoredShardingOrchestrationEvent;
import io.shardingsphere.orchestration.reg.api.RegistryCenter;
import io.shardingsphere.orchestration.reg.listener.DataChangedEvent;
import io.shardingsphere.orchestration.reg.listener.DataChangedEvent.ChangedType;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

import java.util.Arrays;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;

public class SchemaChangedListenerTest {
    
    private SchemaChangedListener schemaChangedListener;
    
    @Mock
    private RegistryCenter regCenter;
    
    @Before
    public void setUp() {
        schemaChangedListener = new SchemaChangedListener("test", regCenter, Arrays.asList("sharding_db", "masterslave_db"));
    }
    
    @Test
    public void assertCreateIgnoredEvent() {
        assertThat(schemaChangedListener.createShardingOrchestrationEvent(new DataChangedEvent("/test/config/schema/logic_db", "test", ChangedType.UPDATED)), 
                instanceOf(IgnoredShardingOrchestrationEvent.class));
        assertThat(schemaChangedListener.createShardingOrchestrationEvent(new DataChangedEvent("/test/config/schema/logic_db/rule", "test", ChangedType.IGNORED)), 
                instanceOf(IgnoredShardingOrchestrationEvent.class));
    }
}

@@ -759,23 +759,211 @@
 g.event.
-Ignored
+DataSourceChangedEvent;%0Aimport io.shardingsphere.orchestration.internal.registry.config.event.IgnoredShardingOrchestrationEvent;%0Aimport io.shardingsphere.orchestration.internal.registry.listener.
 Sharding
@@ -1260,20 +1260,98 @@
 org.
-mockito.Mock
+junit.runner.RunWith;%0Aimport org.mockito.Mock;%0Aimport org.mockito.junit.MockitoJUnitRunner
 ;%0A%0Ai
@@ -1427,16 +1427,55 @@
 anceOf;%0A
+import static org.hamcrest.core.Is.is;%0A
 import s
@@ -1513,49 +1513,479 @@
 at;%0A
-%0Apublic class SchemaChangedListenerTest %7B
+import static org.mockito.Mockito.when;%0A%0A@RunWith(MockitoJUnitRunner.class)%0Apublic class SchemaChangedListenerTest %7B%0A    %0A    private static final String DATA_SOURCE_YAML = %22master_ds: !!io.shardingsphere.orchestration.yaml.YamlDataSourceConfiguration%5Cn%22%0A            + %22  dataSourceClassName: com.zaxxer.hikari.HikariDataSource%5Cn%22 + %22  properties:%5Cn%22%0A            + %22    url: jdbc:mysql://localhost:3306/demo_ds_master%5Cn%22 + %22    username: root%5Cn%22 + %22    password: null%5Cn%22;
 %0A   
@@ -2800,10 +2800,718 @@
 ;%0A    %7D%0A
+    %0A    @Test%0A    public void assertCreateDataSourceChangedEventForExistedSchema() %7B%0A        when(regCenter.get(%22/test/config/schema/logic_db/rule%22)).thenReturn(%22rule%22);%0A        when(regCenter.get(%22/test/config/schema/logic_db/datasource%22)).thenReturn(%22datasource%22);%0A        DataChangedEvent dataChangedEvent = new DataChangedEvent(%22/test/config/schema/logic_db/datasource%22, DATA_SOURCE_YAML, ChangedType.UPDATED);%0A        ShardingOrchestrationEvent actual = schemaChangedListener.createShardingOrchestrationEvent(dataChangedEvent);%0A        assertThat(actual, instanceOf(DataSourceChangedEvent.class));%0A        assertThat(((DataSourceChangedEvent) actual).getShardingSchemaName(), is(%22sharding_db%22));%0A    %7D%0A
 %7D%0A

use @RunWith(MockitoJUnitRunner.class)