use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testParseMessageException.
@Test
public void testParseMessageException() throws Exception {
byte[] messageMetadata = { 'P' };
String statementName = "some statement\0";
String payload = "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);
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 testParseMessageWithNonMatchingParameterTypeCount.
@Test
public void testParseMessageWithNonMatchingParameterTypeCount() 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[] length = intToBytes(4 + statementName.length() + payload.length() + 1);
byte[] value = Bytes.concat(messageMetadata, length, statementName.getBytes(), payload.getBytes(), intToBytes(0));
int[] expectedParameterDataTypes = new int[0];
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 testCopyDoneMessage.
@Test
public void testCopyDoneMessage() throws Exception {
byte[] messageMetadata = { 'c' };
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);
CopyStatement copyStatement = mock(CopyStatement.class);
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);
MutationWriter mb = mock(MutationWriter.class);
when(copyStatement.getMutationWriter()).thenReturn(mb);
WireMessage message = ControlMessage.create(connectionHandler);
assertEquals(CopyDoneMessage.class, message.getClass());
CopyDoneMessage messageSpy = (CopyDoneMessage) spy(message);
doReturn(false).when(messageSpy).sendSpannerResult(anyInt(), any(IntermediateStatement.class), any(QueryMode.class), anyLong());
messageSpy.send();
verify(messageSpy).sendSpannerResult(0, copyStatement, QueryMode.SIMPLE, 0L);
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testParseMessageExceptsIfNameIsInUse.
@Test
public void testParseMessageExceptsIfNameIsInUse() 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);
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 testQueryMessage.
@Test
public void testQueryMessage() throws Exception {
byte[] messageMetadata = { 'Q', 0, 0, 0, 24 };
String payload = "SELECT * FROM users\0";
byte[] value = Bytes.concat(messageMetadata, payload.getBytes());
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
String expectedSQL = "SELECT * FROM users";
when(connection.execute(Statement.of(expectedSQL))).thenReturn(statementResult);
when(statementResult.getResultType()).thenReturn(ResultType.RESULT_SET);
when(statementResult.getResultSet()).thenReturn(resultSet);
when(connectionHandler.getServer()).thenReturn(server);
when(server.getOptions()).thenReturn(options);
when(options.requiresMatcher()).thenReturn(false);
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(QueryMessage.class, message.getClass());
assertEquals(expectedSQL, ((QueryMessage) message).getStatement().getSql());
QueryMessage messageSpy = (QueryMessage) spy(message);
doNothing().when(messageSpy).handleQuery();
messageSpy.send();
// Execute
verify(connection).execute(Statement.of(expectedSQL));
}
Aggregations