/*
 * 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.route.type.broadcast;

import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.core.optimize.result.OptimizeResult;
import org.apache.shardingsphere.core.parse.sql.statement.ddl.DDLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.route.type.RoutingResult;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;

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

public final class TableBroadcastRoutingEngineTest {
    
    private ShardingRule shardingRule;
    
    @Before
    public void setEngineContext() {
        TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration("t_order", "ds${0..1}.t_order_${0..2}");
        tableRuleConfig.setLogicIndex("t_order_index");
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
        shardingRule = new ShardingRule(shardingRuleConfig, Arrays.asList("ds0", "ds1"));
    }
    
    @Test
    public void assertRoutingResultForDQLStatement() {
        assertThat(createDQLStatementRoutingResult(), instanceOf(RoutingResult.class));
    }
    
    @Test
    public void assertIsSingleRoutingForDQLStatement() {
        assertFalse(createDQLStatementRoutingResult().isSingleRouting());
    }
    
    @Test
    public void assertTableUnitsForDQLStatement() {
        RoutingResult routingResult = createDQLStatementRoutingResult();
        assertThat(routingResult.getRoutingUnits().size(), is(0));
    }
    
    @Test
    public void assertRoutingResultForDDLStatement() {
        assertThat(createDDLStatementRoutingResult(), instanceOf(RoutingResult.class));
    }
    
    @Test
    public void assertIsSingleRoutingForDDLStatement() {
        assertFalse(createDDLStatementRoutingResult().isSingleRouting());
    }
    
    @Test
    public void assertTableUnitsForDDLStatement() {
        RoutingResult routingResult = createDDLStatementRoutingResult();
        assertThat(routingResult.getRoutingUnits().size(), is(6));
    }
    
    private RoutingResult createDQLStatementRoutingResult() {
        return new TableBroadcastRoutingEngine(shardingRule, new SelectStatement(), mock(OptimizeResult.class)).route();
    }
    
    private RoutingResult createDDLStatementRoutingResult() {
        DDLStatement ddlStatement = new DDLStatement();
        ddlStatement.setLogicSQL("CREATE INDEX t_order_index on t_order");
        ddlStatement.setIndexName("t_order_index");
        return new TableBroadcastRoutingEngine(shardingRule, ddlStatement, mock(OptimizeResult.class)).route();
    }
}

@@ -1077,32 +1077,184 @@
 OptimizeResult;%0A
+import org.apache.shardingsphere.core.optimize.result.insert.InsertOptimizeResult;%0Aimport org.apache.shardingsphere.core.parse.sql.context.table.Table;%0A
 import org.apach
@@ -1305,32 +1305,32 @@
 l.DDLStatement;%0A
-
 import org.apach
@@ -1586,16 +1586,46 @@
 .Arrays;
+%0Aimport java.util.Collections;
 %0A%0Aimport
@@ -1758,16 +1758,16 @@
 tFalse;%0A
+
 import s
@@ -1804,48 +1804,8 @@
 hat;
-%0Aimport static org.mockito.Mockito.mock;
 %0A%0Apu
@@ -3594,33 +3594,84 @@
 (), 
-mock(OptimizeResult.class
+new OptimizeResult(new InsertOptimizeResult(Collections.%3CString%3EemptyList())
 )).r
@@ -3803,24 +3803,90 @@
 tatement();%0A
+        ddlStatement.getTables().add(new Table(%22t_order%22, null));%0A
         ddlS
@@ -4079,33 +4079,84 @@
 nt, 
-mock(OptimizeResult.class
+new OptimizeResult(new InsertOptimizeResult(Collections.%3CString%3EemptyList())
 )).r

modify createDDLStatementRoutingResult()