/*
 * 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.internal.config.listener.DataSourceOrchestrationListener;
import io.shardingsphere.orchestration.internal.config.listener.RuleOrchestrationListener;
import io.shardingsphere.orchestration.internal.listener.OrchestrationListenerManager;
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 java.util.Collection;

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"), orchestrationFacade);
        setRegistry(orchestrationFacade.getClass().getDeclaredField("dataSourceService"), orchestrationFacade);
        setRegCenterForOrchestrationListenerManager();
    }
    
    private void setRegCenterForOrchestrationListenerManager() throws ReflectiveOperationException {
        Field file = orchestrationFacade.getClass().getDeclaredField("listenerManager");
        file.setAccessible(true);
        OrchestrationListenerManager listenerManager = (OrchestrationListenerManager) file.get(orchestrationFacade);
        setRegistry(listenerManager.getClass().getDeclaredField("propertiesListenerManager"), listenerManager);
        setRegistry(listenerManager.getClass().getDeclaredField("authenticationListenerManager"), listenerManager);
        setRegistry(listenerManager.getClass().getDeclaredField("configMapListenerManager"), listenerManager);
        setRegistry(listenerManager.getClass().getDeclaredField("instanceStateListenerManager"), listenerManager);
        setRegistry(listenerManager.getClass().getDeclaredField("dataSourceStateListenerManager"), listenerManager);
        setRegistryForDataSourceListenerManagers(listenerManager);
        setRegistryForRuleListenerManagers(listenerManager);
    }
    
    @SuppressWarnings("unchecked")
    private void setRegistryForDataSourceListenerManagers(final OrchestrationListenerManager listenerManager) throws ReflectiveOperationException {
        Field childField = listenerManager.getClass().getDeclaredField("dataSourceListenerManagers");
        childField.setAccessible(true);
        Collection<DataSourceOrchestrationListener> dataSourceListenerManagers = (Collection<DataSourceOrchestrationListener>) childField.get(listenerManager);
        for (DataSourceOrchestrationListener each : dataSourceListenerManagers) {
            setRegistry(each);
        }
    }
    
    @SuppressWarnings("unchecked")
    private void setRegistryForRuleListenerManagers(final OrchestrationListenerManager listenerManager) throws ReflectiveOperationException {
        Field childField = listenerManager.getClass().getDeclaredField("ruleListenerManagers");
        childField.setAccessible(true);
        Collection<RuleOrchestrationListener> ruleListenerManagers = (Collection<RuleOrchestrationListener>) childField.get(listenerManager);
        for (RuleOrchestrationListener each : ruleListenerManagers) {
            setRegistry(each);
        }
    }
    
    private void setRegistry(final Field field, final Object target) throws ReflectiveOperationException {
        field.setAccessible(true);
        setRegistry(field.get(target));
    }
    
    private void setRegistry(final Object target) throws ReflectiveOperationException {
        Field field = target.getClass().getDeclaredField("regCenter");
        field.setAccessible(true);
        field.set(target, regCenter);
    }
    
    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/schema/masterslave_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));
        verify(regCenter).watch(eq("/test/config/schema/masterslave_db/rule"), any(EventListener.class));
    }
    
    @Test
    public void testClose() {
    }
}

@@ -664,16 +664,221 @@
 ernal;%0A%0A
+import io.shardingsphere.api.config.ShardingRuleConfiguration;%0Aimport io.shardingsphere.api.config.TableRuleConfiguration;%0Aimport io.shardingsphere.api.config.strategy.InlineShardingStrategyConfiguration;%0A
 import i
@@ -5578,32 +5578,837 @@
 () %7B%0A    %7D%0A    %0A
+    private ShardingRuleConfiguration getShardingRuleConfiguration() %7B%0A        ShardingRuleConfiguration result = new ShardingRuleConfiguration();%0A        TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();%0A        tableRuleConfig.setLogicTable(%22t_order%22);%0A        tableRuleConfig.setActualDataNodes(%22ds_ms_$%7B0..1%7D.t_order_$%7B0..1%7D%22);%0A        result.getTableRuleConfigs().add(tableRuleConfig);%0A        result.getMasterSlaveRuleConfigs().addAll(getMasterSlaveRuleConfigurations());%0A        result.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration(%22user_id%22, %22ds_ms_$%7Buser_id %25 2%7D%22));%0A        result.setDefaultTableShardingStrategyConfig(new InlineShardingStrategyConfiguration(%22order_id%22, %22t_order_$%7Border_id %25 2%7D%22));%0A        return result;%0A    %7D%0A    %0A    %0A
     @Test%0A    pu

add getShardingRuleConfiguration()