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

import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration;
import org.apache.shardingsphere.api.config.encryptor.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.api.config.encryptor.EncryptorConfiguration;
import org.apache.shardingsphere.core.constant.DatabaseType;
import org.apache.shardingsphere.core.metadata.table.ColumnMetaData;
import org.apache.shardingsphere.core.metadata.table.ShardingTableMetaData;
import org.apache.shardingsphere.core.metadata.table.TableMetaData;
import org.apache.shardingsphere.core.parse.EncryptSQLParsingEngine;
import org.apache.shardingsphere.core.rule.EncryptRule;
import org.junit.Before;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

public final class EncryptSQLRewriteEngineTest {
    
    private EncryptSQLParsingEngine sqlParsingEngine;
    
    @Before
    public void setUp() {
        sqlParsingEngine = new EncryptSQLParsingEngine(DatabaseType.MySQL, new EncryptRule(createEncryptRuleConfiguration()), createShardingTableMetaData());
        
    }
    
    private EncryptRuleConfiguration createEncryptRuleConfiguration() {
        EncryptorConfiguration encryptorConfig = new EncryptorConfiguration("test", "col1, col2", new Properties());
        EncryptTableRuleConfiguration encryptTableRuleConfig = new EncryptTableRuleConfiguration();
        encryptTableRuleConfig.setTable("t_encrypt");
        encryptTableRuleConfig.setEncryptorConfig(encryptorConfig);
        EncryptorConfiguration encryptorQueryConfig = new EncryptorConfiguration("assistedTest", "col1, col2", "query1, query2", new Properties());
        EncryptTableRuleConfiguration encryptQueryTableRuleConfig = new EncryptTableRuleConfiguration();
        encryptQueryTableRuleConfig.setTable("t_query_encrypt");
        encryptQueryTableRuleConfig.setEncryptorConfig(encryptorQueryConfig);
        EncryptRuleConfiguration result = new EncryptRuleConfiguration();
        result.getTableRuleConfigs().add(encryptTableRuleConfig);
        result.getTableRuleConfigs().add(encryptQueryTableRuleConfig);
        return result;
    }
    
    private ShardingTableMetaData createShardingTableMetaData() {
        ColumnMetaData columnMetaData1 = new ColumnMetaData("col1", "VARCHAR(10)", false);
        ColumnMetaData columnMetaData2 = new ColumnMetaData("col2", "VARCHAR(10)", false);
        ColumnMetaData queryColumnMetaData1 = new ColumnMetaData("query1", "VARCHAR(10)", false);
        ColumnMetaData queryColumnMetaData2 = new ColumnMetaData("query2", "VARCHAR(10)", false);
        TableMetaData encryptTableMetaData = new TableMetaData(Arrays.asList(columnMetaData1, columnMetaData2));
        TableMetaData queryTableMetaData = new TableMetaData(Arrays.asList(columnMetaData1, columnMetaData2, queryColumnMetaData1, queryColumnMetaData2));
        Map<String, TableMetaData> tables = new LinkedHashMap<>();
        tables.put("t_encrypt", encryptTableMetaData);
        tables.put("t_query_encrypt", queryTableMetaData);
        return new ShardingTableMetaData(tables);
    }
}

@@ -1404,37 +1404,246 @@
 ore.
-parse.EncryptSQLParsingEngine
+optimize.OptimizeEngineFactory;%0Aimport org.apache.shardingsphere.core.optimize.result.OptimizeResult;%0Aimport org.apache.shardingsphere.core.parse.EncryptSQLParsingEngine;%0Aimport org.apache.shardingsphere.core.parse.parser.sql.SQLStatement
 ;%0Aim
@@ -1775,24 +1775,76 @@
 kedHashMap;%0A
+import java.util.LinkedList;%0Aimport java.util.List;%0A
 import java.
@@ -1933,24 +1933,66 @@
 Test %7B%0A    %0A
+    private EncryptRule encryptRule;%0A    %0A
     private 
@@ -2046,41 +2046,202 @@
     
-@Before%0A    public void setUp() %7B
+private final List%3CObject%3E parameters = Arrays.asList((Object) 1, (Object) 2);%0A    %0A    @Before%0A    public void setUp() %7B%0A        encryptRule = new EncryptRule(createEncryptRuleConfiguration());
 %0A   
@@ -2312,21 +2312,17 @@
 .MySQL, 
-new E
+e
 ncryptRu
@@ -2327,42 +2327,8 @@
 Rule
-(createEncryptRuleConfiguration())
 , cr
@@ -2361,17 +2361,8 @@
 ));%0A
-        %0A
     
@@ -4369,10 +4369,352 @@
 ;%0A    %7D%0A
+    %0A    public void assertSelectWithoutPlaceholderWithEncrypt() %7B%0A        String sql = %22SELECT * FROM t_encrypt WHERE col1 = 1 or col2 = 2%22;%0A        SQLStatement sqlStatement = sqlParsingEngine.parse(false, sql);%0A        OptimizeResult optimizeResult = OptimizeEngineFactory.newInstance(, sqlStatement, new LinkedList%3C%3E()).optimize();%0A    %7D%0A
 %7D%0A

use encryptRule