/*
 * 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 io.shardingsphere.example.jdbc.nodep;

import io.shardingsphere.example.jdbc.nodep.factory.CommonServiceFactory;
import io.shardingsphere.example.repository.api.senario.CommonServiceScenario;
import io.shardingsphere.example.repository.api.trace.ResultAssertUtils;
import io.shardingsphere.example.type.ShardingType;
import org.junit.Test;

import java.sql.SQLException;

public class JavaConfigurationExampleTest {

    @Test
    public void assertShardingDatabasePrecise() throws SQLException {
        CommonServiceScenario scenario = new CommonServiceScenario(CommonServiceFactory.newInstance(ShardingType.SHARDING_DATABASES));
        scenario.executeShardingCRUDSuccess();
        ResultAssertUtils.assertShardingDatabaseResult(scenario.getCommonService(), false);
    }

    @Test
    public void assertShardingDatabaseRange() throws SQLException {
        CommonServiceScenario scenario = new CommonServiceScenario(CommonServiceFactory.newInstance(ShardingType.SHARDING_DATABASES_RANGE));
        scenario.executeShardingCRUDSuccess();
        ResultAssertUtils.assertShardingDatabaseResult(scenario.getCommonService(), true);
    }
    
    @Test
    public void assertShardingTablesPrecise() throws SQLException {
        CommonServiceScenario scenario = new CommonServiceScenario(CommonServiceFactory.newInstance(ShardingType.SHARDING_TABLES));
        scenario.executeShardingCRUDSuccess();
        ResultAssertUtils.assertShardingTableResult(scenario.getCommonService(), false);
    }
    
    @Test
    public void assertShardingTablesRange() throws SQLException {
        CommonServiceScenario scenario = new CommonServiceScenario(CommonServiceFactory.newInstance(ShardingType.SHARDING_TABLES_RANGE));
        scenario.executeShardingCRUDSuccess();
        ResultAssertUtils.assertShardingTableResult(scenario.getCommonService(), true);
    }

    @Test
    public void assertShardingDatabaseAndTablesPrecise() throws SQLException {
        CommonServiceScenario scenario = new CommonServiceScenario(CommonServiceFactory.newInstance(ShardingType.SHARDING_DATABASES_AND_TABLES));
        scenario.executeShardingCRUDSuccess();
        ResultAssertUtils.assertShardingDatabaseAndTableResult(scenario.getCommonService(), false);
    }

    @Test
    public void assertShardingDatabaseAndTablesRange() throws SQLException {
        CommonServiceScenario scenario = new CommonServiceScenario(CommonServiceFactory.newInstance(ShardingType.SHARDING_DATABASES_AND_TABLES_RANGE));
        scenario.executeShardingCRUDSuccess();
        ResultAssertUtils.assertShardingDatabaseAndTableResult(scenario.getCommonService(), true);
    }

    @Test
    public void assertMasterSlave() throws SQLException {
        CommonServiceScenario scenario = new CommonServiceScenario(CommonServiceFactory.newInstance(ShardingType.MASTER_SLAVE));
        scenario.executeShardingCRUDSuccess();
        ResultAssertUtils.assertMasterSlaveResult(scenario.getCommonService());
    }

    @Test
    public void assertShardingMasterSlavePrecise() throws SQLException {
        CommonServiceScenario scenario = new CommonServiceScenario(CommonServiceFactory.newInstance(ShardingType.SHARDING_MASTER_SLAVE));
        scenario.executeShardingCRUDSuccess();
        ResultAssertUtils.assertMasterSlaveResult(scenario.getCommonService());
    }

    @Test
    public void assertShardingMasterSlaveRange() throws SQLException {
        CommonServiceScenario scenario = new CommonServiceScenario(CommonServiceFactory.newInstance(ShardingType.SHARDING_MASTER_SLAVE_RANGE));
        scenario.executeShardingCRUDSuccess();
        ResultAssertUtils.assertMasterSlaveResult(scenario.getCommonService());
    }
}

@@ -843,16 +843,106 @@
 nodep;%0A%0A
+import io.shardingsphere.example.jdbc.nodep.config.ShardingDatabasesConfigurationPrecise;%0A
 import i
@@ -1250,25 +1250,144 @@
 java
-.sql.SQLException
+x.sql.DataSource;%0Aimport java.sql.Connection;%0Aimport java.sql.PreparedStatement;%0Aimport java.sql.SQLException;%0Aimport java.sql.Statement
 ;%0A%0Ap
@@ -4697,10 +4697,998 @@
 ;%0A    %7D%0A
+    %0A    @Test%0A    public void assertSubStatementQuery() throws SQLException %7B%0A        DataSource dataSource = new ShardingDatabasesConfigurationPrecise().getDataSource();%0A        try (Connection connection = dataSource.getConnection()) %7B%0A            Statement statement = connection.createStatement();%0A            statement.execute(%22select * from t_order where order_id = (%22%0A                + %22select order_id from t_order_item where order_item_id=1 and user_id=1)%22);%0A        %7D%0A    %7D%0A    %0A    @Test%0A    public void assertSubPrepareStatementQuery() throws SQLException %7B%0A        DataSource dataSource = new ShardingDatabasesConfigurationPrecise().getDataSource();%0A        try (Connection connection = dataSource.getConnection()) %7B%0A            PreparedStatement preparedStatement = connection.prepareStatement(%22select * from t_order where order_id = (%22%0A                + %22select order_id from t_order_item where order_item_id=1)%22);%0A            preparedStatement.execute();%0A        %7D%0A    %7D%0A
 %7D%0A

add sub-query unit test.