Search in sources :

Example 21 with WireMessage

use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.

the class ProtocolTest method testBindMessageAllNonTextParam.

@Test
public void testBindMessageAllNonTextParam() throws Exception {
    byte[] messageMetadata = { 'B' };
    String portalName = "some portal\0";
    String statementName = "some statement\0";
    byte[] parameterCodesCount = { 0, 1 };
    // binary
    byte[] parameterCodes = { 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 = Collections.singletonList((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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) WireMessage(com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 22 with WireMessage

use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.

the class ProtocolTest method testStartUpMessage.

@Test
public void testStartUpMessage() throws Exception {
    byte[] protocol = intToBytes(196608);
    byte[] payload = ("database\0" + "databasename\0" + "application_name\0" + "psql\0" + "client_encoding\0" + "UTF8\0" + "server_version\0" + "13.4\0" + "user\0" + "me\0").getBytes();
    byte[] length = intToBytes(8 + payload.length);
    byte[] value = Bytes.concat(length, protocol, payload);
    Map<String, String> expectedParameters = new HashMap<>();
    expectedParameters.put("database", "databasename");
    expectedParameters.put("application_name", "psql");
    expectedParameters.put("client_encoding", "UTF8");
    expectedParameters.put("server_version", "13.4");
    expectedParameters.put("user", "me");
    DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    DataOutputStream outputStream = new DataOutputStream(result);
    when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
    when(connectionHandler.getServer()).thenReturn(server);
    when(connectionHandler.getConnectionId()).thenReturn(1);
    when(server.getOptions()).thenReturn(options);
    when(options.getServerVersion()).thenReturn("13.4");
    when(options.shouldAuthenticate()).thenReturn(false);
    when(connectionMetadata.getInputStream()).thenReturn(inputStream);
    when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
    WireMessage message = BootstrapMessage.create(connectionHandler);
    assertEquals(StartupMessage.class, message.getClass());
    assertEquals(expectedParameters, ((StartupMessage) message).getParameters());
    message.send();
    DataInputStream outputResult = inputStreamFromOutputStream(result);
    verify(connectionHandler).connectToSpanner("databasename");
    // AuthenticationOkResponse
    assertEquals('R', outputResult.readByte());
    assertEquals(8, outputResult.readInt());
    assertEquals(0, outputResult.readInt());
    // KeyDataResponse
    assertEquals('K', outputResult.readByte());
    assertEquals(12, outputResult.readInt());
    assertEquals(1, outputResult.readInt());
    assertEquals(0, outputResult.readInt());
    // ParameterStatusResponse (x11)
    assertEquals('S', outputResult.readByte());
    assertEquals(24, outputResult.readInt());
    assertEquals("server_version\0", readUntil(outputResult, "server_version\0".length()));
    assertEquals("13.4\0", readUntil(outputResult, "13.4\0".length()));
    assertEquals('S', outputResult.readByte());
    assertEquals(31, outputResult.readInt());
    assertEquals("application_name\0", readUntil(outputResult, "application_name\0".length()));
    assertEquals("PGAdapter\0", readUntil(outputResult, "PGAdapter\0".length()));
    assertEquals('S', outputResult.readByte());
    assertEquals(23, outputResult.readInt());
    assertEquals("is_superuser\0", readUntil(outputResult, "is_superuser\0".length()));
    assertEquals("false\0", readUntil(outputResult, "false\0".length()));
    assertEquals('S', outputResult.readByte());
    assertEquals(36, outputResult.readInt());
    assertEquals("session_authorization\0", readUntil(outputResult, "session_authorization\0".length()));
    assertEquals("PGAdapter\0", readUntil(outputResult, "PGAdapter\0".length()));
    assertEquals('S', outputResult.readByte());
    assertEquals(25, outputResult.readInt());
    assertEquals("integer_datetimes\0", readUntil(outputResult, "integer_datetimes\0".length()));
    assertEquals("on\0", readUntil(outputResult, "on\0".length()));
    assertEquals('S', outputResult.readByte());
    assertEquals(25, outputResult.readInt());
    assertEquals("server_encoding\0", readUntil(outputResult, "server_encoding\0".length()));
    assertEquals("UTF8\0", readUntil(outputResult, "UTF8\0".length()));
    assertEquals('S', outputResult.readByte());
    assertEquals(25, outputResult.readInt());
    assertEquals("client_encoding\0", readUntil(outputResult, "client_encoding\0".length()));
    assertEquals("UTF8\0", readUntil(outputResult, "UTF8\0".length()));
    assertEquals('S', outputResult.readByte());
    assertEquals(22, outputResult.readInt());
    assertEquals("DateStyle\0", readUntil(outputResult, "DateStyle\0".length()));
    assertEquals("ISO,YMD\0", readUntil(outputResult, "ISO,YMD\0".length()));
    assertEquals('S', outputResult.readByte());
    assertEquals(27, outputResult.readInt());
    assertEquals("IntervalStyle\0", readUntil(outputResult, "IntervalStyle\0".length()));
    assertEquals("iso_8601\0", readUntil(outputResult, "iso_8601\0".length()));
    assertEquals('S', outputResult.readByte());
    assertEquals(35, outputResult.readInt());
    assertEquals("standard_conforming_strings\0", readUntil(outputResult, "standard_conforming_strings\0".length()));
    assertEquals("on\0", readUntil(outputResult, "on\0".length()));
    assertEquals('S', outputResult.readByte());
    assertEquals(17, outputResult.readInt());
    assertEquals("TimeZone\0", readUntil(outputResult, "TimeZone\0".length()));
    // Timezone will vary depending on the default location of the JVM that is running.
    readUntilNullTerminator(outputResult);
    // ReadyResponse
    assertEquals('Z', outputResult.readByte());
    assertEquals(5, outputResult.readInt());
    assertEquals('I', outputResult.readByte());
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) WireMessage(com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 23 with WireMessage

use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.

the class ProtocolTest method testQueryUsesPSQLStatementWhenPSQLModeSelectedMessage.

@Test
public void testQueryUsesPSQLStatementWhenPSQLModeSelectedMessage() throws Exception {
    JSONParser parser = new JSONParser();
    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(true);
    when(options.getCommandMetadataJSON()).thenReturn((JSONObject) parser.parse("{\"commands\": []}"));
    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(MatcherStatement.class, ((QueryMessage) message).getStatement().getClass());
    assertEquals(expectedSQL, ((QueryMessage) message).getStatement().getSql());
    QueryMessage messageSpy = (QueryMessage) spy(message);
    doNothing().when(messageSpy).handleQuery();
    messageSpy.send();
    verify(connection).execute(Statement.of(expectedSQL));
}
Also used : QueryMessage(com.google.cloud.spanner.pgadapter.wireprotocol.QueryMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) WireMessage(com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage) JSONParser(org.json.simple.parser.JSONParser) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 24 with WireMessage

use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.

the class ProtocolTest method testQueryMessageInTransaction.

@Test
public void testQueryMessageInTransaction() throws Exception {
    byte[] messageMetadata = { 'Q', 0, 0, 0, 45 };
    String payload = "INSERT INTO users (name) VALUES ('test')\0";
    byte[] value = Bytes.concat(messageMetadata, payload.getBytes());
    DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    DataOutputStream outputStream = new DataOutputStream(result);
    String expectedSQL = "INSERT INTO users (name) VALUES ('test')";
    when(connectionHandler.getSpannerConnection()).thenReturn(connection);
    when(connectionHandler.getStatus()).thenReturn(ConnectionStatus.TRANSACTION);
    when(statementResult.getResultType()).thenReturn(ResultType.UPDATE_COUNT);
    when(statementResult.getUpdateCount()).thenReturn(1L);
    when(connection.execute(Statement.of(expectedSQL))).thenReturn(statementResult);
    when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
    when(connectionHandler.getServer()).thenReturn(server);
    OptionsMetadata options = mock(OptionsMetadata.class);
    when(server.getOptions()).thenReturn(options);
    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());
    message.send();
    // NoData response (query does not return any results).
    DataInputStream outputResult = inputStreamFromOutputStream(result);
    // CommandComplete
    assertEquals('C', outputResult.readByte());
    assertEquals('\0', outputResult.readByte());
    assertEquals('\0', outputResult.readByte());
    assertEquals('\0', outputResult.readByte());
    // 15 = 4 + "INSERT".length() + " 0 1".length() + 1 (header + command length + null terminator)
    assertEquals(15, outputResult.readByte());
    byte[] command = new byte[10];
    assertEquals(10, outputResult.read(command, 0, 10));
    assertEquals("INSERT 0 1", new String(command));
    assertEquals('\0', outputResult.readByte());
    // ReadyResponse in transaction ('T')
    assertEquals('Z', outputResult.readByte());
    assertEquals(5, outputResult.readInt());
    assertEquals('T', outputResult.readByte());
}
Also used : QueryMessage(com.google.cloud.spanner.pgadapter.wireprotocol.QueryMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) WireMessage(com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 25 with WireMessage

use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.

the class ProtocolTest method testFunctionCallMessageThrowsException.

@Test
public void testFunctionCallMessageThrowsException() throws Exception {
    byte[] messageMetadata = { 'F' };
    byte[] functionId = intToBytes(1);
    byte[] argumentCodesCount = { 0, 2 };
    // First is text, second binary
    byte[] argumentCodes = { 0, 0, 0, 1 };
    byte[] parameterCount = { 0, 2 };
    byte[] firstParameter = "first parameter\0".getBytes();
    byte[] secondParameter = intToBytes(10);
    byte[] firstParameterLength = intToBytes(firstParameter.length);
    byte[] secondParameterLength = intToBytes(secondParameter.length);
    byte[] resultCode = { 0, 0 };
    byte[] length = intToBytes(4 + functionId.length + argumentCodesCount.length + argumentCodes.length + parameterCount.length + firstParameterLength.length + firstParameter.length + secondParameterLength.length + secondParameter.length + resultCode.length);
    byte[] value = Bytes.concat(messageMetadata, length, functionId, argumentCodesCount, argumentCodes, parameterCount, firstParameterLength, firstParameter, secondParameterLength, secondParameter, resultCode);
    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(FunctionCallMessage.class, message.getClass());
    assertThrows(IllegalStateException.class, message::send);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) WireMessage(com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Aggregations

WireMessage (com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage)34 ByteArrayInputStream (java.io.ByteArrayInputStream)34 DataInputStream (java.io.DataInputStream)34 Test (org.junit.Test)34 ByteArrayOutputStream (java.io.ByteArrayOutputStream)24 DataOutputStream (java.io.DataOutputStream)24 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)21 ParseMessage (com.google.cloud.spanner.pgadapter.wireprotocol.ParseMessage)5 CopyStatement (com.google.cloud.spanner.pgadapter.statements.CopyStatement)4 QueryMessage (com.google.cloud.spanner.pgadapter.wireprotocol.QueryMessage)4 IntermediateStatement (com.google.cloud.spanner.pgadapter.statements.IntermediateStatement)3 MutationWriter (com.google.cloud.spanner.pgadapter.utils.MutationWriter)3 QueryMode (com.google.cloud.spanner.pgadapter.ConnectionHandler.QueryMode)2 IntermediatePortalStatement (com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement)2 PreparedType (com.google.cloud.spanner.pgadapter.wireprotocol.ControlMessage.PreparedType)2 CopyDataMessage (com.google.cloud.spanner.pgadapter.wireprotocol.CopyDataMessage)2 DescribeMessage (com.google.cloud.spanner.pgadapter.wireprotocol.DescribeMessage)2 ResultSet (com.google.cloud.spanner.ResultSet)1 Statement (com.google.cloud.spanner.Statement)1 ParsedStatement (com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)1