/*
 * 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.shardingproxy.transport.mysql.packet.command.query.binary.execute.protocol;

import org.apache.shardingsphere.shardingproxy.transport.mysql.payload.MySQLPacketPayload;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.sql.Timestamp;
import java.util.Calendar;

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

@RunWith(MockitoJUnitRunner.class)
public final class MySQLTimeBinaryProtocolValueTest {
    
    @Mock
    private MySQLPacketPayload payload;
    
    @Test
    public void assertReadWithZeroByte() {
        assertThat(new MySQLTimeBinaryProtocolValue().read(payload), CoreMatchers.<Object>is(new Timestamp(0)));
    }
    
    @Test
    public void assertReadWithEightBytes() {
        when(payload.readInt1()).thenReturn(8, 0, 10, 59, 0);
        Calendar actual = Calendar.getInstance();
        actual.setTimeInMillis(((Timestamp) new MySQLTimeBinaryProtocolValue().read(payload)).getTime());
        assertThat(actual.get(Calendar.HOUR_OF_DAY), is(10));
        assertThat(actual.get(Calendar.MINUTE), is(59));
        assertThat(actual.get(Calendar.SECOND), is(0));
    }
    
    @Test
    public void assertReadWithTwelveBytes() {
        when(payload.readInt1()).thenReturn(12, 0, 10, 59, 0);
        Calendar actual = Calendar.getInstance();
        actual.setTimeInMillis(((Timestamp) new MySQLTimeBinaryProtocolValue().read(payload)).getTime());
        assertThat(actual.get(Calendar.HOUR_OF_DAY), is(10));
        assertThat(actual.get(Calendar.MINUTE), is(59));
        assertThat(actual.get(Calendar.SECOND), is(0));
    }
    
    @Test(expected = IllegalArgumentException.class)
    public void assertReadWithIllegalArgument() {
        when(payload.readInt1()).thenReturn(100);
        new MySQLTimeBinaryProtocolValue().read(payload);
    }
}

@@ -1155,24 +1155,46 @@
 nitRunner;%0A%0A
+import java.sql.Time;%0A
 import java.
@@ -1323,16 +1323,150 @@
 rtThat;%0A
+import static org.mockito.ArgumentMatchers.anyInt;%0Aimport static org.mockito.Mockito.times;%0Aimport static org.mockito.Mockito.verify;%0A
 import s
@@ -2961,10 +2961,1197 @@
 ;%0A    %7D%0A
+    %0A    @Test%0A    public void assertWriteWithZeroByte() %7B%0A        MySQLTimeBinaryProtocolValue actual = new MySQLTimeBinaryProtocolValue();%0A        Time time = Time.valueOf(%2200:00:00%22);%0A        actual.write(payload, time);%0A        verify(payload).writeInt1(0);%0A    %7D%0A    %0A    @Test%0A    public void assertWriteWithEightBytes() %7B%0A        MySQLTimeBinaryProtocolValue actual = new MySQLTimeBinaryProtocolValue();%0A        Time time = Time.valueOf(%2201:30:10%22);%0A        actual.write(payload, time);%0A        verify(payload).writeInt1(8);%0A        verify(payload).writeInt1(0);%0A        verify(payload).writeInt4(0);%0A        payload.writeInt1(0);%0A        payload.writeInt4(0);%0A        verify(payload).writeInt1(1);%0A        verify(payload).writeInt1(30);%0A        verify(payload).writeInt1(10);%0A    %7D%0A    %0A    @Test%0A    public void assertWriteWithTwelveBytes() %7B%0A        MySQLTimeBinaryProtocolValue actual = new MySQLTimeBinaryProtocolValue();%0A        Time time = new Time(1L);%0A        actual.write(payload, time);%0A        verify(payload).writeInt1(12);%0A        verify(payload, times(5)).writeInt1(anyInt());%0A        verify(payload).writeInt4(0);%0A        verify(payload).writeInt4(1000000);%0A    %7D%0A
 %7D%0A

add test case for MySQLTimeBinaryProtocolValue