/*
 * 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.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;

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() throws Exception {
        AuthPluginData authPluginData = new AuthPluginData(part1, part2);
        handshakePacket = new HandshakePacket(1, authPluginData);
    }
    
    @Test
    public void assertWrite() {
    }
    
    @Test
    public void assertGetProtocolVersion() {
        assertEquals(handshakePacket.getProtocolVersion(), ServerInfo.PROTOCOL_VERSION);
    }
    
    @Test
    public void assertGetServerVersion() {
        assertEquals(handshakePacket.getServerVersion(), ServerInfo.SERVER_VERSION);
    }
    
    @Test
    public void assertGetCapabilityFlagsLower() {
        assertEquals(handshakePacket.getCapabilityFlagsLower(), CapabilityFlag.calculateHandshakeCapabilityFlagsLower());
    }
    
    @Test
    public void assertGetCapabilityFlagsUpper() {
        assertEquals(handshakePacket.getCapabilityFlagsUpper(), CapabilityFlag.calculateHandshakeCapabilityFlagsUpper());
    }
    
    @Test
    public void assertGetCharacterSet() {
        assertEquals(handshakePacket.getCharacterSet(), ServerInfo.CHARSET);
    }
    
    @Test
    public void assertGetStatusFlag() {
        assertEquals(handshakePacket.getStatusFlag(), StatusFlag.SERVER_STATUS_AUTOCOMMIT);
    }
    
    @Test
    public void assertGetSequenceId() {
        assertEquals(handshakePacket.getSequenceId(), 0);
    }
    
    @Test
    public void assertGetConnectionId() {
        assertEquals(handshakePacket.getConnectionId(), 1);
    }
    
    @Test
    public void assertGetAuthPluginData() {
        byte[] actual = Bytes.concat(part1, part2);
        assertTrue(Arrays.equals(handshakePacket.getAuthPluginData().getAuthPluginData(), actual));
    }
}

@@ -1006,16 +1006,60 @@
 rrays;%0A%0A
+import static org.hamcrest.CoreMatchers.is;%0A
 import s
@@ -1088,19 +1088,19 @@
 .assertT
-rue
+hat
 ;%0Aimport
@@ -1130,22 +1130,20 @@
 t.assert
-Equals
+True
 ;%0A%0Apubli
@@ -1437,25 +1437,8 @@
 Up()
- throws Exception
  %7B%0A 
@@ -1625,24 +1625,29 @@
 rtWrite() %7B%0A
+    %0A
     %7D%0A    %0A 
@@ -1706,38 +1706,36 @@
 %7B%0A        assert
-Equals
+That
 (handshakePacket
@@ -1749,32 +1749,35 @@
 tocolVersion(), 
+is(
 ServerInfo.PROTO
@@ -1780,32 +1780,33 @@
 ROTOCOL_VERSION)
+)
 ;%0A    %7D%0A    %0A   
@@ -1861,38 +1861,36 @@
 %7B%0A        assert
-Equals
+That
 (handshakePacket
@@ -1906,24 +1906,27 @@
 rVersion(), 
+is(
 ServerInfo.S
@@ -1939,16 +1939,17 @@
 VERSION)
+)
 ;%0A    %7D%0A
@@ -2019,38 +2019,36 @@
 %7B%0A        assert
-Equals
+That
 (handshakePacket
@@ -2071,24 +2071,27 @@
 agsLower(), 
+is(
 CapabilityFl
@@ -2130,24 +2130,25 @@
 lagsLower())
+)
 ;%0A    %7D%0A    
@@ -2214,38 +2214,36 @@
 %7B%0A        assert
-Equals
+That
 (handshakePacket
@@ -2270,16 +2270,19 @@
 pper(), 
+is(
 Capabili
@@ -2329,16 +2329,17 @@
 Upper())
+)
 ;%0A    %7D%0A
@@ -2401,38 +2401,36 @@
 %7B%0A        assert
-Equals
+That
 (handshakePacket
@@ -2449,16 +2449,19 @@
 rSet(), 
+is(
 ServerIn
@@ -2471,16 +2471,17 @@
 CHARSET)
+)
 ;%0A    %7D%0A
@@ -2541,38 +2541,36 @@
 %7B%0A        assert
-Equals
+That
 (handshakePacket
@@ -2587,16 +2587,19 @@
 Flag(), 
+is(
 StatusFl
@@ -2626,16 +2626,17 @@
 OCOMMIT)
+)
 ;%0A    %7D%0A
@@ -2696,38 +2696,36 @@
 %7B%0A        assert
-Equals
+That
 (handshakePacket
@@ -2742,17 +2742,21 @@
 ceId(), 
-0
+is(0)
 );%0A    %7D
@@ -2831,14 +2831,12 @@
 sert
-Equals
+That
 (han
@@ -2867,17 +2867,21 @@
 onId(), 
-1
+is(1)
 );%0A    %7D

use assertThat