/*
 * 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;

import io.shardingsphere.orchestration.config.OrchestrationConfiguration;
import io.shardingsphere.orchestration.reg.api.RegistryCenter;
import io.shardingsphere.orchestration.reg.api.RegistryCenterConfiguration;
import io.shardingsphere.orchestration.reg.listener.EventListener;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.lang.reflect.Field;
import java.util.Arrays;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public final class OrchestrationFacadeTest {
    
    private OrchestrationFacade orchestrationFacade;
    
    @Mock
    private RegistryCenter regCenter;
    
    @Before
    public void setUp() throws ReflectiveOperationException {
        orchestrationFacade = new OrchestrationFacade(getOrchestrationConfiguration(), Arrays.asList("sharding_db", "masterslave_db"));
        setRegistry(orchestrationFacade);
        setRegistry(orchestrationFacade.getConfigService());
        setRegistry(orchestrationFacade.getClass().getDeclaredField("instanceStateService"));
        setRegistry(orchestrationFacade.getClass().getDeclaredField("dataSourceService"));
    }
    
    private void setRegistry(final Object target) throws ReflectiveOperationException {
        Field field = target.getClass().getDeclaredField("regCenter");
        field.setAccessible(true);
        field.set(target, regCenter);
    }
    
    private void setRegistry(final Field field) throws ReflectiveOperationException {
        field.setAccessible(true);
        setRegistry(field.get(orchestrationFacade));
    }
    
    private OrchestrationConfiguration getOrchestrationConfiguration() {
        return new OrchestrationConfiguration("test", new RegistryCenterConfiguration(), true);
    }
    
    @Test
    public void assertInitWithParameters() {
    }
    
    @Test
    public void assertInitWithoutParameters() {
        orchestrationFacade.init();
        verify(regCenter).persistEphemeral(anyString(), anyString());
        verify(regCenter).persist("/test/state/datasources", "");
        verify(regCenter).watch(eq("/test/config/authentication"), any(EventListener.class));
        verify(regCenter).watch(eq("/test/config/configmap"), any(EventListener.class));
        verify(regCenter).watch(eq("/test/config/schema/sharding_db/datasource"), any(EventListener.class));
        verify(regCenter).watch(eq("/test/config/props"), any(EventListener.class));
        verify(regCenter).watch(eq("/test/config/schema/sharding_db/rule"), any(EventListener.class));
    }
    
    @Test
    public void testClose() {
    }
}

@@ -730,32 +730,119 @@
 nConfiguration;%0A
+import io.shardingsphere.orchestration.internal.listener.OrchestrationListenerManager;%0A
 import io.shardi
@@ -2130,32 +2130,402 @@
 %22));%0A    %7D%0A    %0A
+    private void setRegCenterForOrchestrationListenerManager() throws ReflectiveOperationException %7B%0A        Field file = orchestrationFacade.getClass().getDeclaredField(%22listenerManager%22);%0A        file.setAccessible(true);%0A        OrchestrationListenerManager listenerManager = (OrchestrationListenerManager) file.get(orchestrationFacade);%0A        %0A        %0A    %7D%0A    %0A
     private void
@@ -3757,108 +3757,326 @@
 fig/
-props%22), any(EventListener.class));%0A        verify(regCenter).watch(eq(%22/test/config/schema/sharding
+schema/masterslave_db/datasource%22), any(EventListener.class));%0A        verify(regCenter).watch(eq(%22/test/config/props%22), any(EventListener.class));%0A        verify(regCenter).watch(eq(%22/test/config/schema/sharding_db/rule%22), any(EventListener.class));%0A        verify(regCenter).watch(eq(%22/test/config/schema/masterslave
 _db/

add setRegCenterForOrchestrationListenerManager()