/*
 * 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.backend.text.admin;

import org.apache.shardingsphere.shardingproxy.backend.MockLogicSchemasUtil;
import org.apache.shardingsphere.shardingproxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.shardingproxy.backend.response.query.QueryData;
import org.apache.shardingsphere.shardingproxy.backend.response.query.QueryResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.sql.SQLException;
import java.sql.Types;

import static org.hamcrest.CoreMatchers.instanceOf;
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 ShowDatabasesBackendHandlerTest {
    
    private ShowDatabasesBackendHandler showDatabasesBackendHandler;
    
    @Before
    public void setUp() {
        MockLogicSchemasUtil.setLogicSchemas("schema", 5);
        BackendConnection backendConnection = mock(BackendConnection.class);
        when(backendConnection.getUserName()).thenReturn("root");
        showDatabasesBackendHandler = new ShowDatabasesBackendHandler(backendConnection);
    }
    
    @Test
    public void assertExecuteShowDatabaseBackendHandler() {
        QueryResponse actual = (QueryResponse) showDatabasesBackendHandler.execute();
        assertThat(actual, instanceOf(QueryResponse.class));
        assertThat(actual.getQueryHeaders().size(), is(1));
    }
    
    @Test
    public void assertShowDatabaseUsingStream() throws SQLException {
        showDatabasesBackendHandler.execute();
        while (showDatabasesBackendHandler.next()) {
            QueryData queryData = showDatabasesBackendHandler.getQueryData();
            assertThat(queryData.getColumnTypes().size(), is(1));
            assertThat(queryData.getColumnTypes().iterator().next(), is(Types.VARCHAR));
            assertThat(queryData.getData().size(), is(1));
        }
    }
}

@@ -865,16 +865,124 @@
 admin;%0A%0A
+import org.apache.shardingsphere.core.rule.Authentication;%0Aimport org.apache.shardingsphere.core.rule.User;%0A
 import o
@@ -1050,16 +1050,16 @@
 asUtil;%0A
-
 import o
@@ -1320,16 +1320,93 @@
 sponse;%0A
+import org.apache.shardingsphere.shardingproxy.context.ShardingProxyContext;%0A
 import o
@@ -1482,33 +1482,8 @@
 th;%0A
-import org.mockito.Mock;%0A
 impo
@@ -1576,16 +1576,70 @@
 l.Types;
+%0Aimport java.util.Arrays;%0Aimport java.util.Properties;
 %0A%0Aimport
@@ -2271,88 +2271,435 @@
     
-showDatabasesBackendHandler = new ShowDatabasesBackendHandler(backendConnection)
+ShardingProxyContext.getInstance().init(getAuthentication(), new Properties());%0A        showDatabasesBackendHandler = new ShowDatabasesBackendHandler(backendConnection);%0A    %7D%0A    %0A    private Authentication getAuthentication() %7B%0A        User user = new User(%22root%22, Arrays.asList(%22schema_0%22, %22schema_1%22));%0A        Authentication result = new Authentication();%0A        result.getUsers().put(%22root%22, user);%0A        return result
 ;%0A  

modify setUp()