/*
 * 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.core.merge.dal;

import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult;
import org.apache.shardingsphere.core.merge.dal.desc.DescribeTableMergedResult;
import org.apache.shardingsphere.core.merge.dal.show.ShowCreateTableMergedResult;
import org.apache.shardingsphere.core.merge.dal.show.ShowDatabasesMergedResult;
import org.apache.shardingsphere.core.merge.dal.show.ShowOtherMergedResult;
import org.apache.shardingsphere.core.merge.dal.show.ShowTablesMergedResult;
import org.apache.shardingsphere.core.merge.fixture.TestQueryResult;
import org.apache.shardingsphere.core.optimize.api.segment.Tables;
import org.apache.shardingsphere.core.optimize.api.statement.OptimizedStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dal.DALStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.DescribeStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.ShowCreateTableStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.ShowDatabasesStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.ShowOtherStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.ShowTablesStatement;
import org.junit.Before;
import org.junit.Test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public final class DALMergeEngineTest {
    
    private List<QueryResult> queryResults;
    
    @Before
    public void setUp() {
        ResultSet resultSet = mock(ResultSet.class);
        queryResults = Collections.<QueryResult>singletonList(new TestQueryResult(resultSet));
    }
    
    @Test
    public void assertMergeForShowDatabasesStatement() throws SQLException {
        DALStatement dalStatement = new ShowDatabasesStatement();
        OptimizedStatement optimizedStatement = mockOptimizedStatement(dalStatement);
        DALMergeEngine dalMergeEngine = new DALMergeEngine(null, queryResults, optimizedStatement, null);
        assertThat(dalMergeEngine.merge(), instanceOf(ShowDatabasesMergedResult.class));
    }
    
    @Test
    public void assertMergeForShowShowTablesStatement() throws SQLException {
        DALStatement dalStatement = new ShowTablesStatement();
        OptimizedStatement optimizedStatement = mockOptimizedStatement(dalStatement);
        DALMergeEngine dalMergeEngine = new DALMergeEngine(null, queryResults, optimizedStatement, null);
        assertThat(dalMergeEngine.merge(), instanceOf(ShowTablesMergedResult.class));
    }
    
    @Test
    public void assertMergeForShowCreateTableStatement() throws SQLException {
        DALStatement dalStatement = new ShowCreateTableStatement();
        OptimizedStatement optimizedStatement = mockOptimizedStatement(dalStatement);
        DALMergeEngine dalMergeEngine = new DALMergeEngine(null, queryResults, optimizedStatement, null);
        assertThat(dalMergeEngine.merge(), instanceOf(ShowCreateTableMergedResult.class));
    }

    @Test
    public void assertMergeForShowOtherStatement() throws SQLException {
        DALStatement dalStatement = new ShowOtherStatement();
        OptimizedStatement optimizedStatement = mockOptimizedStatement(dalStatement);
        DALMergeEngine dalMergeEngine = new DALMergeEngine(null, queryResults, optimizedStatement, null);
        assertThat(dalMergeEngine.merge(), instanceOf(ShowOtherMergedResult.class));
    }

    @Test
    public void assertMergeForDescribeStatement() throws SQLException {
        DALStatement dalStatement = new DescribeStatement();
        OptimizedStatement optimizedStatement = mockOptimizedStatement(dalStatement);
        Tables tables = mock(Tables.class);
        when(tables.getSingleTableName()).thenReturn("user");
        when(optimizedStatement.getTables()).thenReturn(tables);
        DALMergeEngine dalMergeEngine = new DALMergeEngine(null, queryResults, optimizedStatement, null);
        assertThat(dalMergeEngine.merge(), instanceOf(DescribeTableMergedResult.class));
    }

    private OptimizedStatement mockOptimizedStatement(final DALStatement dalStatement) {
        OptimizedStatement optimizedStatement = mock(OptimizedStatement.class);
        when(optimizedStatement.getSQLStatement()).thenReturn(dalStatement);
        return optimizedStatement;
    }
}

@@ -2095,32 +2095,214 @@
 ablesStatement;%0A
+import org.apache.shardingsphere.core.rule.EncryptRule;%0Aimport org.apache.shardingsphere.core.rule.ShardingRule;%0Aimport org.apache.shardingsphere.core.strategy.encrypt.EncryptTable;%0A
 import org.junit
@@ -4938,32 +4938,153 @@
 (Tables.class);%0A
+        ShardingRule shardingRule = mock(ShardingRule.class);%0A        EncryptRule encryptRule = mock(EncryptRule.class);%0A
         when(tab
@@ -5190,24 +5190,189 @@
 rn(tables);%0A
+        when(encryptRule.getTables()).thenReturn(Collections.%3CString, EncryptTable%3EemptyMap());%0A        when(shardingRule.getEncryptRule()).thenReturn(encryptRule);%0A
         DALM
@@ -5410,36 +5410,44 @@
  DALMergeEngine(
-null
+shardingRule
 , queryResults, 

update DALMergeEngineTest.java for DescribeStatement