use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testParseMessageGsqlException.
@Test
public void testParseMessageGsqlException() throws Exception {
byte[] messageMetadata = { 'P' };
String statementName = "some statement\0";
String payload = "/*GSQL*/ SELECT * FROM users WHERE name = $1\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);
List<Integer> expectedParameterDataTypes = Arrays.asList(1002);
String expectedSQL = "/*GSQL*/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);
Mockito.when(connectionHandler.getSpannerConnection()).thenReturn(connection);
Mockito.when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
Mockito.when(connectionMetadata.getInputStream()).thenReturn(inputStream);
Mockito.when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
WireMessage message = ControlMessage.create(connectionHandler);
Assert.assertEquals(message.getClass(), ParseMessage.class);
Assert.assertEquals(((ParseMessage) message).getName(), expectedMessageName);
Assert.assertEquals(((ParseMessage) message).getStatement().getSql(), expectedSQL);
Assert.assertThat(((ParseMessage) message).getStatement().getParameterDataTypes(), is(expectedParameterDataTypes));
Mockito.when(connectionHandler.hasStatement(anyString())).thenReturn(false);
message.send();
Mockito.verify(connectionHandler, Mockito.times(1)).registerStatement(expectedMessageName, ((ParseMessage) message).getStatement());
// ParseCompleteResponse
DataInputStream outputResult = inputStreamFromOutputStream(result);
Assert.assertEquals(outputResult.readByte(), '1');
Assert.assertEquals(outputResult.readInt(), 4);
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testBindMessageOneNonTextParam.
@Test
public void testBindMessageOneNonTextParam() throws Exception {
byte[] messageMetadata = { 'B' };
String portalName = "some portal\0";
String statementName = "some statement\0";
byte[] parameterCodesCount = { 0, 2 };
// First is text, second binary
byte[] parameterCodes = { 0, 0, 0, 1 };
byte[] parameterCount = { 0, 2 };
byte[] firstParameter = "someUser\0".getBytes();
byte[] firstParameterLength = intToBytes(firstParameter.length);
byte[] secondParameter = { 0, 1, 0, 1 };
byte[] secondParameterLength = intToBytes(secondParameter.length);
byte[] resultCodesCount = { 0, 1 };
// binary
byte[] resultCodes = { 0, 1 };
byte[] length = intToBytes(4 + portalName.length() + statementName.length() + parameterCodesCount.length + parameterCodes.length + parameterCount.length + firstParameterLength.length + firstParameter.length + secondParameterLength.length + secondParameter.length + resultCodesCount.length + resultCodes.length);
byte[] value = Bytes.concat(messageMetadata, length, portalName.getBytes(), statementName.getBytes(), parameterCodesCount, parameterCodes, parameterCount, firstParameterLength, firstParameter, secondParameterLength, secondParameter, resultCodesCount, resultCodes);
byte[][] expectedParameters = { firstParameter, secondParameter };
List<Short> expectedFormatCodes = Arrays.asList((short) 0, (short) 1);
List<Short> expectedResultFormatCodes = Collections.singletonList((short) 1);
String expectedPortalName = "some portal";
String expectedStatementName = "some statement";
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
when(connectionMetadata.getInputStream()).thenReturn(inputStream);
when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
WireMessage message = ControlMessage.create(connectionHandler);
assertEquals(BindMessage.class, message.getClass());
assertEquals(expectedPortalName, ((BindMessage) message).getPortalName());
assertEquals(expectedStatementName, ((BindMessage) message).getStatementName());
assertArrayEquals(expectedParameters, ((BindMessage) message).getParameters());
assertEquals(expectedFormatCodes, ((BindMessage) message).getFormatCodes());
assertEquals(expectedResultFormatCodes, ((BindMessage) message).getResultFormatCodes());
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testFlushMessageInTransaction.
@Test
public void testFlushMessageInTransaction() throws Exception {
byte[] messageMetadata = { 'H' };
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.getSpannerConnection()).thenReturn(connection);
when(connection.isInTransaction()).thenReturn(true);
when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
when(connectionMetadata.getInputStream()).thenReturn(inputStream);
when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
WireMessage message = ControlMessage.create(connectionHandler);
assertEquals(FlushMessage.class, message.getClass());
message.send();
// ReadyResponse
DataInputStream outputResult = inputStreamFromOutputStream(result);
assertEquals('Z', outputResult.readByte());
assertEquals(5, outputResult.readInt());
assertEquals('T', outputResult.readByte());
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testSSLMessageFailsWhenCalledTwice.
@Test
public void testSSLMessageFailsWhenCalledTwice() throws Exception {
byte[] length = intToBytes(8);
byte[] protocol = intToBytes(80877103);
byte[] value = Bytes.concat(length, protocol);
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(SSLMessage.class, message.getClass());
message.send();
DataInputStream outputResult = inputStreamFromOutputStream(result);
// DeclineSSLResponse
assertEquals('N', outputResult.readByte());
assertThrows(IOException.class, message::send);
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testFlushMessage.
@Test
public void testFlushMessage() throws Exception {
byte[] messageMetadata = { 'H' };
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.getSpannerConnection()).thenReturn(connection);
when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
when(connectionMetadata.getInputStream()).thenReturn(inputStream);
when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
WireMessage message = ControlMessage.create(connectionHandler);
assertEquals(message.getClass(), FlushMessage.class);
message.send();
// ReadyResponse
DataInputStream outputResult = inputStreamFromOutputStream(result);
assertEquals('Z', outputResult.readByte());
assertEquals(5, outputResult.readInt());
assertEquals('I', outputResult.readByte());
}
Aggregations