use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testClosePortalMessage.
@Test
public void testClosePortalMessage() throws Exception {
byte[] messageMetadata = { 'C' };
byte[] statementType = { 'P' };
String statementName = "some portal\0";
byte[] length = intToBytes(4 + statementType.length + statementName.length());
byte[] value = Bytes.concat(messageMetadata, length, statementType, statementName.getBytes());
String expectedStatementName = "some portal";
PreparedType expectedType = PreparedType.Portal;
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(CloseMessage.class, message.getClass());
assertEquals(expectedStatementName, ((CloseMessage) message).getName());
assertEquals(expectedType, ((CloseMessage) message).getType());
verify(connectionHandler).getPortal("some portal");
message.send();
verify(intermediatePortalStatement).close();
verify(connectionHandler).closePortal(expectedStatementName);
// CloseResponse
DataInputStream outputResult = inputStreamFromOutputStream(result);
assertEquals('3', outputResult.readByte());
assertEquals(4, outputResult.readInt());
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testDescribeStatementMessage.
@Test
public void testDescribeStatementMessage() throws Exception {
byte[] messageMetadata = { 'D' };
byte[] statementType = { 'S' };
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.getStatement(anyString())).thenReturn(intermediatePreparedStatement);
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).getStatement("some statement");
DescribeMessage messageSpy = (DescribeMessage) spy(message);
doNothing().when(messageSpy).handleDescribeStatement();
messageSpy.send();
verify(messageSpy).handleDescribeStatement();
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testParseMessage.
@Test
public void testParseMessage() 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);
int[] expectedParameterDataTypes = new int[] { 1002 };
String expectedSQL = "SELECT * FROM users WHERE name = $1";
String expectedMessageName = "some statement";
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
ByteArrayOutputStream result = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(result);
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);
assertEquals(ParseMessage.class, message.getClass());
assertEquals(expectedMessageName, ((ParseMessage) message).getName());
assertEquals(expectedSQL, ((ParseMessage) message).getStatement().getSql());
assertArrayEquals(expectedParameterDataTypes, ((ParseMessage) message).getStatement().getParameterDataTypes());
when(connectionHandler.hasStatement(anyString())).thenReturn(false);
message.send();
verify(connectionHandler).registerStatement(expectedMessageName, ((ParseMessage) message).getStatement());
// ParseCompleteResponse
DataInputStream outputResult = inputStreamFromOutputStream(result);
assertEquals('1', outputResult.readByte());
assertEquals(4, outputResult.readInt());
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testCancelMessage.
@Test
public void testCancelMessage() throws Exception {
byte[] length = intToBytes(16);
byte[] protocol = intToBytes(80877102);
byte[] connectionId = intToBytes(1);
byte[] secret = intToBytes(1);
byte[] value = Bytes.concat(length, protocol, connectionId, secret);
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 = BootstrapMessage.create(connectionHandler);
assertEquals(CancelMessage.class, message.getClass());
assertEquals(1, ((CancelMessage) message).getConnectionId());
assertEquals(1, ((CancelMessage) message).getSecret());
message.send();
verify(connectionHandler).cancelActiveStatement(1, 1);
verify(connectionHandler).handleTerminate();
}
Aggregations