/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed 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.
 * </p>
 */

package io.shardingsphere.proxy.transport.mysql.packet.command.query;

import io.shardingsphere.core.constant.ShardingConstant;
import io.shardingsphere.proxy.transport.mysql.constant.ColumnType;
import io.shardingsphere.proxy.transport.mysql.constant.ServerInfo;
import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public final class ColumnDefinition41PacketTest {
    
    @Mock
    private ResultSetMetaData resultSetMetaData;
    
    @Mock
    private MySQLPacketPayload payload;
    
    @Test
    public void assertWriteWithResultSetMetaData() throws SQLException {
        when(resultSetMetaData.getSchemaName(1)).thenReturn(ShardingConstant.LOGIC_SCHEMA_NAME);
        when(resultSetMetaData.getTableName(1)).thenReturn("tbl");
        when(resultSetMetaData.getColumnLabel(1)).thenReturn("id");
        when(resultSetMetaData.getColumnName(1)).thenReturn("id");
        when(resultSetMetaData.getColumnDisplaySize(1)).thenReturn(10);
        when(resultSetMetaData.getColumnType(1)).thenReturn(Types.INTEGER);
        new ColumnDefinition41Packet(1, resultSetMetaData, 1).write(payload);
        verify(payload).writeStringLenenc("def");
        verify(payload).writeStringLenenc(ShardingConstant.LOGIC_SCHEMA_NAME);
        verify(payload, times(2)).writeStringLenenc("tbl");
        verify(payload, times(2)).writeStringLenenc("id");
        verify(payload).writeIntLenenc(0x0c);
        verify(payload).writeInt2(ServerInfo.CHARSET);
        verify(payload).writeInt4(10);
        verify(payload).writeInt1(ColumnType.MYSQL_TYPE_LONG.getValue());
        verify(payload).writeInt2(0);
        verify(payload).writeInt1(0);
        verify(payload).writeReserved(2);
    }
    
    @Test
    public void assertWriteWithPayload() {
        when(payload.readInt1()).thenReturn(1, ColumnType.MYSQL_TYPE_LONG.getValue(), 0);
        when(payload.readStringLenenc()).thenReturn("def", ShardingConstant.LOGIC_SCHEMA_NAME, "tbl", "tbl", "id", "id");
        when(payload.readIntLenenc()).thenReturn((long) 0x0c);
        when(payload.readInt2()).thenReturn(ServerInfo.CHARSET, 0);
        when(payload.readInt4()).thenReturn(10);
        new ColumnDefinition41Packet(payload).write(payload);
        verify(payload).writeStringLenenc("def");
        verify(payload).writeStringLenenc(ShardingConstant.LOGIC_SCHEMA_NAME);
        verify(payload, times(2)).writeStringLenenc("tbl");
        verify(payload, times(2)).writeStringLenenc("id");
        verify(payload).writeIntLenenc(0x0c);
        verify(payload).writeInt2(ServerInfo.CHARSET);
        verify(payload).writeInt4(10);
        verify(payload).writeInt1(ColumnType.MYSQL_TYPE_LONG.getValue());
        verify(payload).writeInt2(0);
        verify(payload).writeInt1(0);
        verify(payload).writeReserved(2);
    }
}

@@ -2126,241 +2126,80 @@
 rify
-(payload).writeStringLenenc(%22def%22
+Write(
 );%0A    
+%7D%0A
     
-verify(payload).writeStringLenenc(ShardingConstant.LOGIC_SCHEMA_NAME);%0A        verify(payload, times(2)).writeStringLenenc(%22tbl%22);%0A        verify(payload, times(2)).writeStringLenenc(%22id%22);
+%0A    @Test%0A    public void assertWriteWithPayload() %7B
 %0A   
@@ -2199,38 +2199,36 @@
 ) %7B%0A        
-verify
+when
 (payload
 ).writeIntLe
@@ -2219,160 +2219,35 @@
 load
-).writeIntLenenc(0x0c);%0A        verify(payload).writeInt2(ServerInfo.CHARSET);%0A        verify(payload).writeInt4(10);%0A        verify(payload).writeInt1(
+.readInt1()).thenReturn(1, 
 Colu
@@ -2271,32 +2271,35 @@
 _LONG.getValue()
+, 0
 );%0A        verif
@@ -2289,38 +2289,36 @@
 0);%0A        
-verify
+when
 (payload
 ).writeInt2(
@@ -2309,58 +2309,52 @@
 load
-).writeInt2(0);%0A        verify(payload).writeInt1(
+.readInt2()).thenReturn(ServerInfo.CHARSET, 
 0);%0A
@@ -2357,38 +2357,36 @@
 0);%0A        
-verify
+when
 (payload
 ).writeReser
@@ -2377,91 +2377,36 @@
 load
-).writeReserved(2);%0A    %7D%0A    %0A    @Test%0A    public void assertWriteWithPayload() %7B
+.readInt4()).thenReturn(10);
 %0A   
@@ -2422,33 +2422,38 @@
 (payload.readInt
-1
+Lenenc
 ()).thenReturn(1
@@ -2455,51 +2455,19 @@
 urn(
-1, ColumnType.MYSQL_TYPE_LONG.getValue(), 0
+(long) 0x0c
 );%0A 
@@ -2599,241 +2599,128 @@
     
-when(payload.readIntLenenc()).thenReturn((long) 0x0c);%0A        when(payload.readInt2()).thenReturn(ServerInfo.CHARSET, 0);%0A        when(payload.readInt4()).thenReturn(10
+new ColumnDefinition41Packet(payload).write(payload);%0A        verifyWrite(
 );%0A    
+%7D%0A
     
-new ColumnDefinition41Packet(payload).write(payload);
+%0A    private void verifyWrite() %7B
 %0A   

refactor ColumnDefinition41PacketTest