/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

package org.apache.shardingsphere.shardingproxy.context;

import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant;
import org.apache.shardingsphere.core.rule.Authentication;
import org.apache.shardingsphere.core.rule.User;
import org.apache.shardingsphere.orchestration.internal.eventbus.ShardingOrchestrationEventBus;
import org.apache.shardingsphere.orchestration.internal.registry.config.event.AuthenticationChangedEvent;
import org.apache.shardingsphere.orchestration.internal.registry.config.event.PropertiesChangedEvent;
import org.apache.shardingsphere.orchestration.internal.registry.state.event.CircuitStateChangedEvent;
import org.junit.Test;

import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public final class ShardingProxyContextTest {
    
    @Test
    public void assertInit() {
        User user = new User("root", "db1");
        Authentication authentication = new Authentication();
        authentication.getUsers().put("root", user);
        Properties props = new Properties();
        props.setProperty(ShardingPropertiesConstant.SQL_SHOW.getKey(), Boolean.TRUE.toString());
        props.setProperty(ShardingPropertiesConstant.PROXY_TRANSACTION_TYPE.getKey(), "BASE");
        ShardingProxyContext.getInstance().init(authentication, props);
        assertThat(ShardingProxyContext.getInstance().getAuthentication(), is(authentication));
        assertTrue(ShardingProxyContext.getInstance().getShardingProperties().<Boolean>getValue(ShardingPropertiesConstant.SQL_SHOW));
        assertThat(ShardingProxyContext.getInstance().getShardingProperties().<String>getValue(ShardingPropertiesConstant.PROXY_TRANSACTION_TYPE), is("BASE"));
    }
    
    @Test
    public void assertRenewShardingProperties() {
        User user = new User("root", "db1");
        Authentication authentication = new Authentication();
        authentication.getUsers().put("root", user);
        ShardingProxyContext.getInstance().init(authentication, new Properties());
        assertTrue(ShardingProxyContext.getInstance().getShardingProperties().getProps().isEmpty());
        Properties props = new Properties();
        props.setProperty(ShardingPropertiesConstant.SQL_SHOW.getKey(), Boolean.TRUE.toString());
        ShardingOrchestrationEventBus.getInstance().post(new PropertiesChangedEvent(props));
        assertFalse(ShardingProxyContext.getInstance().getShardingProperties().getProps().isEmpty());
    }
    
    @Test
    public void assertRenewAuthentication() {
        User user = new User("root", "db1");
        Authentication authentication = new Authentication();
        authentication.getUsers().put("root", user);
        ShardingProxyContext.getInstance().init(authentication, new Properties());
        ShardingOrchestrationEventBus.getInstance().post(new AuthenticationChangedEvent(authentication));
        assertThat(ShardingProxyContext.getInstance().getAuthentication().getUsers().keySet().iterator().next(), is("root"));
        assertThat(ShardingProxyContext.getInstance().getAuthentication().getUsers().get("root").getPassword(), is("root"));
        assertThat(ShardingProxyContext.getInstance().getAuthentication().getUsers().get("root").getAuthorizedSchemas(), is("db1"));
    }
    
    @Test
    public void assertRenewCircuitState() {
        assertFalse(ShardingProxyContext.getInstance().isCircuitBreak());
        ShardingOrchestrationEventBus.getInstance().post(new CircuitStateChangedEvent(true));
        assertTrue(ShardingProxyContext.getInstance().isCircuitBreak());
        ShardingOrchestrationEventBus.getInstance().post(new CircuitStateChangedEvent(false));
    }
}

@@ -1475,24 +1475,54 @@
 unit.Test;%0A%0A
+import java.util.Collections;%0A
 import java.
@@ -1538,16 +1538,16 @@
 erties;%0A
-
 %0Aimport 
@@ -1835,38 +1835,61 @@
 ew User(%22root%22, 
+Collections.singleton(
 %22db1%22)
+)
 ;%0A        Authen
@@ -2790,38 +2790,61 @@
 ew User(%22root%22, 
+Collections.singleton(
 %22db1%22)
+)
 ;%0A        Authen
@@ -3570,22 +3570,45 @@
 %22root%22, 
+Collections.singleton(
 %22db1%22)
+)
 ;%0A      
@@ -4148,32 +4148,32 @@
 ), is(%22root%22));%0A
-
         assertTh
@@ -4275,16 +4275,34 @@
 chemas()
+.iterator().next()
 , is(%22db

modify unit cases