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

import com.google.common.primitives.Bytes;
import io.shardingsphere.proxy.transport.mysql.constant.CapabilityFlag;
import io.shardingsphere.proxy.transport.mysql.constant.ServerInfo;
import io.shardingsphere.proxy.transport.mysql.constant.StatusFlag;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class HandshakePacketTest {
    
    private HandshakePacket handshakePacket;
    
    private final byte[] part1 = {106, 105, 55, 122, 117, 98, 115, 109};
    
    private final byte[] part2 = {68, 102, 53, 122, 65, 49, 84, 79, 85, 115, 116, 113};
    
    @Before
    public void setUp() {
        AuthPluginData authPluginData = new AuthPluginData(part1, part2);
        handshakePacket = new HandshakePacket(1, authPluginData);
    }
    
    @Test
    public void assertWrite() {
    
    }
    
    @Test
    public void assertGetProtocolVersion() {
        assertThat(handshakePacket.getProtocolVersion(), is(ServerInfo.PROTOCOL_VERSION));
    }
    
    @Test
    public void assertGetServerVersion() {
        assertThat(handshakePacket.getServerVersion(), is(ServerInfo.SERVER_VERSION));
    }
    
    @Test
    public void assertGetCapabilityFlagsLower() {
        assertThat(handshakePacket.getCapabilityFlagsLower(), is(CapabilityFlag.calculateHandshakeCapabilityFlagsLower()));
    }
    
    @Test
    public void assertGetCapabilityFlagsUpper() {
        assertThat(handshakePacket.getCapabilityFlagsUpper(), is(CapabilityFlag.calculateHandshakeCapabilityFlagsUpper()));
    }
    
    @Test
    public void assertGetCharacterSet() {
        assertThat(handshakePacket.getCharacterSet(), is(ServerInfo.CHARSET));
    }
    
    @Test
    public void assertGetStatusFlag() {
        assertThat(handshakePacket.getStatusFlag(), is(StatusFlag.SERVER_STATUS_AUTOCOMMIT));
    }
    
    @Test
    public void assertGetSequenceId() {
        assertThat(handshakePacket.getSequenceId(), is(0));
    }
    
    @Test
    public void assertGetConnectionId() {
        assertThat(handshakePacket.getConnectionId(), is(1));
    }
    
    @Test
    public void assertGetAuthPluginData() {
        byte[] actual = Bytes.concat(part1, part2);
        assertTrue(Arrays.equals(handshakePacket.getAuthPluginData().getAuthPluginData(), actual));
    }
}

@@ -723,16 +723,48 @@
 .Bytes;%0A
+import io.netty.buffer.ByteBuf;%0A
 import i
@@ -823,24 +823,24 @@
 bilityFlag;%0A
-
 import io.sh
@@ -959,24 +959,98 @@
 StatusFlag;%0A
+import io.shardingsphere.proxy.transport.mysql.packet.MySQLPacketPayload;%0A
 import org.j
@@ -1241,16 +1241,147 @@
 ertTrue;
+%0Aimport static org.mockito.ArgumentMatchers.anyInt;%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.when;
 %0A%0Apublic
@@ -1866,20 +1866,358 @@
 ite() %7B%0A
-
     
+    ByteBuf byteBuf = mock(ByteBuf.class);%0A        MySQLPacketPayload payload = new MySQLPacketPayload(byteBuf);%0A        when(byteBuf.writeByte(anyInt())).thenReturn(byteBuf);%0A        when(byteBuf.writeBytes(byteBuf)).thenReturn(byteBuf);%0A        handshakePacket.write(payload);%0A        assertThat(handshakePacket.getSequenceId(), is(0));
 %0A    %7D%0A 

add test case