Skip to content

Commit

Permalink
Add more test cases on MySQLAuthSwitchResponsePacket (#33271)
Browse files Browse the repository at this point in the history
* Add more test cases on MySQLAuthSwitchResponsePacket

* Add more test cases on MySQLAuthSwitchResponsePacket
  • Loading branch information
terrymanu authored Oct 16, 2024
1 parent 7e9c880 commit d4c7e85
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,28 @@
import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload;
import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class MySQLAuthMoreDataPacketTest {

@Mock
private MySQLPacketPayload payload;

@Test
void assertNewWithInvalidHeader() {
MySQLPacketPayload payload = mock(MySQLPacketPayload.class);
assertThrows(IllegalArgumentException.class, () -> new MySQLAuthMoreDataPacket(payload));
}

@Test
void assertNewWithValidHeader() {
MySQLPacketPayload payload = mock(MySQLPacketPayload.class);
when(payload.readInt1()).thenReturn(MySQLAuthMoreDataPacket.HEADER);
when(payload.readStringEOFByBytes()).thenReturn(new byte[0]);
MySQLAuthMoreDataPacket packet = new MySQLAuthMoreDataPacket(payload);
Expand All @@ -46,6 +50,6 @@ void assertNewWithValidHeader() {

@Test
void assertWrite() {
assertThrows(UnsupportedSQLOperationException.class, () -> new MySQLAuthMoreDataPacket(new byte[0]).write(mock(MySQLPacketPayload.class)));
assertThrows(UnsupportedSQLOperationException.class, () -> new MySQLAuthMoreDataPacket(new byte[0]).write(payload));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
Expand All @@ -36,9 +35,9 @@ class MySQLAuthSwitchResponsePacketTest {
private final byte[] authPluginResponse = {0x22, 0x33};

@Test
void assertMySQLAuthSwitchResponsePacket() {
void assertWrite() {
when(payload.readStringEOFByBytes()).thenReturn(authPluginResponse);
MySQLAuthSwitchResponsePacket authSwitchResponsePacket = new MySQLAuthSwitchResponsePacket(payload);
assertThat(authSwitchResponsePacket.getAuthPluginResponse(), is(authPluginResponse));
new MySQLAuthSwitchResponsePacket(payload).write(payload);
verify(payload).writeBytes(authPluginResponse);
}
}

0 comments on commit d4c7e85

Please sign in to comment.