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

import org.apache.shardingsphere.core.optimize.statement.InsertOptimizedStatement;
import org.apache.shardingsphere.core.optimize.statement.sharding.dml.insert.InsertOptimizeResultUnit;
import org.apache.shardingsphere.core.rule.DataNode;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;

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

public final class InsertParameterBuilderTest {
    
    private InsertParameterBuilder insertParameterBuilder;
    
    @Before
    public void setUp() {
        insertParameterBuilder = new InsertParameterBuilder(Arrays.<Object>asList(1, 2), createInsertOptimizedStatement());
    }
    
    private InsertOptimizedStatement createInsertOptimizedStatement() {
        InsertOptimizeResultUnit unit1 = mock(InsertOptimizeResultUnit.class);
        when(unit1.getParameters()).thenReturn(new Object[] {3, 4});
        when(unit1.getDataNodes()).thenReturn(Collections.singletonList(new DataNode("db1.tb1")));
        InsertOptimizeResultUnit unit2 = mock(InsertOptimizeResultUnit.class);
        when(unit2.getParameters()).thenReturn(new Object[] {5, 6});
        when(unit2.getDataNodes()).thenReturn(Collections.singletonList(new DataNode("db2.tb2")));
        InsertOptimizedStatement result = mock(InsertOptimizedStatement.class);
        when(result.getUnits()).thenReturn(Arrays.asList(unit1, unit2));
        return result;
    }
    
    @Test
    public void assertGetParameters() {
        assertThat(insertParameterBuilder.getParameters(), is(Arrays.<Object>asList(3, 4, 5, 6)));
    }
    
    @Test
    public void assertGetParameters1() {
    }
    
    @Test
    public void assertGetOriginalParameters() {
        assertThat(insertParameterBuilder.getOriginalParameters(), is(Arrays.<Object>asList(1, 2)));
    }
    
    @Test
    public void assertGetInsertParameterUnits() {
        assertThat(insertParameterBuilder.getInsertParameterUnits().size(), is(2));
    }
}

@@ -1039,16 +1039,138 @@
 ltUnit;%0A
+import org.apache.shardingsphere.core.route.type.RoutingUnit;%0Aimport org.apache.shardingsphere.core.route.type.TableUnit;%0A
 import o
@@ -2739,13 +2739,259 @@
 ters
-1() %7B
+WithRoutingUnit() %7B%0A        RoutingUnit routingUnit = new RoutingUnit(%22db1%22);%0A        routingUnit.getTableUnits().add(new TableUnit(%22tb1%22, %22tb1%22));%0A        assertThat(insertParameterBuilder.getParameters(routingUnit), is(Arrays.%3CObject%3EasList(3, 4)));
 %0A   

add assertGetParametersWithRoutingUnit()