use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testDescribePortalMessage.
@Test
public void testDescribePortalMessage() throws Exception {
byte[] messageMetadata = { 'D' };
byte[] statementType = { 'P' };
String statementName = "some statement\0";
byte[] length = intToBytes(4 + 1 + statementName.length());
byte[] value = Bytes.concat(messageMetadata, length, statementType, statementName.getBytes());
String expectedStatementName = "some statement";
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
ByteArrayOutputStream result = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(result);
when(connectionHandler.getPortal(anyString())).thenReturn(intermediatePortalStatement);
when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
when(connectionMetadata.getInputStream()).thenReturn(inputStream);
when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
WireMessage message = ControlMessage.create(connectionHandler);
assertEquals(DescribeMessage.class, message.getClass());
assertEquals(expectedStatementName, ((DescribeMessage) message).getName());
verify(connectionHandler).getPortal("some statement");
DescribeMessage messageSpy = (DescribeMessage) spy(message);
doNothing().when(messageSpy).handleDescribePortal();
messageSpy.send();
verify(messageSpy).handleDescribePortal();
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testParseMessageExceptsIfNameIsNull.
@Test
public void testParseMessageExceptsIfNameIsNull() throws Exception {
byte[] messageMetadata = { 'P' };
String statementName = "some statement\0";
String payload = "SELECT * FROM users WHERE name = $1 /*This is a comment*/ --this is another comment\0";
byte[] parameterCount = { 0, 1 };
byte[] parameters = intToBytes(1002);
byte[] length = intToBytes(4 + statementName.length() + payload.length() + parameterCount.length + parameters.length);
byte[] value = Bytes.concat(messageMetadata, length, statementName.getBytes(), payload.getBytes(), parameterCount, parameters);
when(connectionHandler.hasStatement(anyString())).thenReturn(true);
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
when(connectionHandler.getServer()).thenReturn(server);
when(server.getOptions()).thenReturn(options);
when(connectionHandler.getSpannerConnection()).thenReturn(connection);
when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
when(connectionMetadata.getInputStream()).thenReturn(inputStream);
when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
WireMessage message = ControlMessage.create(connectionHandler);
when(connectionHandler.hasStatement(anyString())).thenReturn(true);
assertThrows(IllegalStateException.class, message::send);
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testSyncMessage.
@Test
public void testSyncMessage() throws Exception {
byte[] messageMetadata = { 'S' };
byte[] length = intToBytes(4);
byte[] value = Bytes.concat(messageMetadata, length);
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
ByteArrayOutputStream result = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(result);
when(connectionHandler.getStatus()).thenReturn(ConnectionStatus.IDLE);
when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
when(connectionMetadata.getInputStream()).thenReturn(inputStream);
when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
WireMessage message = ControlMessage.create(connectionHandler);
assertEquals(message.getClass(), SyncMessage.class);
message.send();
// ReadyResponse
DataInputStream outputResult = inputStreamFromOutputStream(result);
assertEquals('Z', outputResult.readByte());
assertEquals(5, outputResult.readInt());
assertEquals('I', outputResult.readByte());
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testCopyFailMessage.
@Test
public void testCopyFailMessage() throws Exception {
byte[] messageMetadata = { 'f' };
byte[] errorMessage = "Error Message\0".getBytes();
byte[] length = intToBytes(4 + errorMessage.length);
byte[] value = Bytes.concat(messageMetadata, length, errorMessage);
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
ByteArrayOutputStream result = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(result);
String expectedErrorMessage = "Error Message";
CopyStatement copyStatement = mock(CopyStatement.class);
MutationWriter mutationWriter = mock(MutationWriter.class);
when(copyStatement.getMutationWriter()).thenReturn(mutationWriter);
when(connectionHandler.getActiveStatement()).thenReturn(copyStatement);
when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
when(connectionHandler.getStatus()).thenReturn(ConnectionStatus.COPY_IN);
when(connectionMetadata.getInputStream()).thenReturn(inputStream);
when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
WireMessage message = ControlMessage.create(connectionHandler);
assertEquals(CopyFailMessage.class, message.getClass());
assertEquals(expectedErrorMessage, ((CopyFailMessage) message).getErrorMessage());
message.send();
verify(mutationWriter).rollback();
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testTerminateMessage.
@Test
public void testTerminateMessage() throws Exception {
byte[] messageMetadata = { 'X' };
byte[] length = intToBytes(4);
byte[] value = Bytes.concat(messageMetadata, length);
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
ByteArrayOutputStream result = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(result);
when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
when(connectionMetadata.getInputStream()).thenReturn(inputStream);
when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
WireMessage message = ControlMessage.create(connectionHandler);
assertEquals(TerminateMessage.class, message.getClass());
}
Aggregations