/*
 * 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.Time;
import java.sql.Timestamp;
import java.util.Calendar;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
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);
    }
    
    @Test
    public void assertWriteWithZeroByte() {
        MySQLTimeBinaryProtocolValue actual = new MySQLTimeBinaryProtocolValue();
        Time time = Time.valueOf("00:00:00");
        actual.write(payload, time);
        verify(payload).writeInt1(0);
    }
    
    @Test
    public void assertWriteWithEightBytes() {
        MySQLTimeBinaryProtocolValue actual = new MySQLTimeBinaryProtocolValue();
        Time time = Time.valueOf("01:30:10");
        actual.write(payload, time);
        verify(payload).writeInt1(8);
        verify(payload).writeInt1(0);
        verify(payload).writeInt4(0);
        payload.writeInt1(0);
        payload.writeInt4(0);
        verify(payload).writeInt1(1);
        verify(payload).writeInt1(30);
        verify(payload).writeInt1(10);
    }
    
    @Test
    public void assertWriteWithTwelveBytes() {
        MySQLTimeBinaryProtocolValue actual = new MySQLTimeBinaryProtocolValue();
        Time time = new Time(1L);
        actual.write(payload, time);
        verify(payload).writeInt1(12);
        verify(payload, times(5)).writeInt1(anyInt());
        verify(payload).writeInt4(0);
        verify(payload).writeInt4(1000000);
    }
}

@@ -3106,35 +3106,45 @@
 ();%0A        
-Time time =
+actual.write(payload,
  Time.valueO
@@ -3160,44 +3160,8 @@
 00%22)
-;%0A        actual.write(payload, time
 );%0A 
@@ -3354,27 +3354,37 @@
         
-Time time =
+actual.write(payload,
  Time.va
@@ -3404,44 +3404,8 @@
 10%22)
-;%0A        actual.write(payload, time
 );%0A 
@@ -3847,42 +3847,8 @@
 ();%0A
-        Time time = new Time(1L);%0A
     
@@ -3873,20 +3873,28 @@
 ayload, 
-time
+new Time(1L)
 );%0A     

refactor MySQLTimeBinaryProtocolValueTest