Search in sources :

Example 16 with WireMessage

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

the class ProtocolTest method testBindMessage.

@Test
public void testBindMessage() throws Exception {
    byte[] messageMetadata = { 'B' };
    String portalName = "some portal\0";
    String statementName = "some statement\0";
    // Denotes no codes
    byte[] parameterCodesCount = { 0, 0 };
    byte[] parameterCount = { 0, 1 };
    byte[] parameter = "someUser\0".getBytes();
    byte[] parameterLength = intToBytes(parameter.length);
    byte[] resultCodesCount = { 0, 0 };
    byte[] length = intToBytes(4 + portalName.length() + statementName.length() + parameterCodesCount.length + parameterCount.length + parameterLength.length + parameter.length + resultCodesCount.length);
    byte[] value = Bytes.concat(messageMetadata, length, portalName.getBytes(), statementName.getBytes(), parameterCodesCount, parameterCount, parameterLength, parameter, resultCodesCount);
    when(connectionHandler.getStatement(anyString())).thenReturn(intermediatePreparedStatement);
    byte[][] expectedParameters = { parameter };
    List<Short> expectedFormatCodes = new ArrayList<>();
    String expectedPortalName = "some portal";
    String expectedStatementName = "some statement";
    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(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(expectedFormatCodes, ((BindMessage) message).getResultFormatCodes());
    when(intermediatePreparedStatement.bind(ArgumentMatchers.anyString(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(intermediatePortalStatement);
    message.send();
    verify(connectionHandler).registerPortal(expectedPortalName, intermediatePortalStatement);
    // BindCompleteResponse
    DataInputStream outputResult = inputStreamFromOutputStream(result);
    assertEquals('2', outputResult.readByte());
    assertEquals(4, outputResult.readInt());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) 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 17 with WireMessage

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

the class StatementTest method testBatchStatementsWithComments.

@Test
public void testBatchStatementsWithComments() throws Exception {
    byte[] messageMetadata = { 'Q', 0, 0, 0, (byte) 132 };
    String payload = "INSERT INTO users (name) VALUES (';;test;;'); /* Comment;; Comment; */INSERT INTO users (name1, name2) VALUES ('''''', ';'';');\0";
    byte[] value = Bytes.concat(messageMetadata, payload.getBytes());
    DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
    when(connectionHandler.getSpannerConnection()).thenReturn(connection);
    when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
    when(connectionHandler.getServer()).thenReturn(server);
    when(server.getOptions()).thenReturn(options);
    when(options.requiresMatcher()).thenReturn(false);
    when(connectionMetadata.getInputStream()).thenReturn(inputStream);
    when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
    WireMessage message = ControlMessage.create(connectionHandler);
    assertEquals(QueryMessage.class, message.getClass());
    IntermediateStatement intermediateStatement = ((QueryMessage) message).getStatement();
    assertTrue(intermediateStatement.isBatchedQuery());
    assertEquals(2, intermediateStatement.getStatements().size());
    assertEquals("INSERT INTO users (name) VALUES (';;test;;')", intermediateStatement.getStatement(0));
    assertEquals("INSERT INTO users (name1, name2) VALUES ('''''', ';'';')", intermediateStatement.getStatement(1));
}
Also used : QueryMessage(com.google.cloud.spanner.pgadapter.wireprotocol.QueryMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) WireMessage(com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage) IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 18 with WireMessage

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

the class ProtocolTest method testSyncMessageInTransaction.

@Test
public void testSyncMessageInTransaction() 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.TRANSACTION);
    when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
    when(connectionMetadata.getInputStream()).thenReturn(inputStream);
    when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
    WireMessage message = ControlMessage.create(connectionHandler);
    assertEquals(SyncMessage.class, message.getClass());
    message.send();
    // ReadyResponse
    DataInputStream outputResult = inputStreamFromOutputStream(result);
    assertEquals('Z', outputResult.readByte());
    assertEquals(5, outputResult.readInt());
    assertEquals('T', outputResult.readByte());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) WireMessage(com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 19 with WireMessage

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

the class ProtocolTest method testCopyDataMessage.

@Test
public void testCopyDataMessage() throws Exception {
    byte[] messageMetadata = { 'd' };
    byte[] payload = "This is the payload".getBytes();
    byte[] length = intToBytes(4 + payload.length);
    byte[] value = Bytes.concat(messageMetadata, length, payload);
    DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
    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 mw = mock(MutationWriter.class);
    when(copyStatement.getMutationWriter()).thenReturn(mw);
    WireMessage message = ControlMessage.create(connectionHandler);
    assertEquals(CopyDataMessage.class, message.getClass());
    assertArrayEquals(payload, ((CopyDataMessage) message).getPayload());
    CopyDataMessage messageSpy = (CopyDataMessage) spy(message);
    messageSpy.send();
    verify(mw).addCopyData(payload);
}
Also used : CopyDataMessage(com.google.cloud.spanner.pgadapter.wireprotocol.CopyDataMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) CopyStatement(com.google.cloud.spanner.pgadapter.statements.CopyStatement) MutationWriter(com.google.cloud.spanner.pgadapter.utils.MutationWriter) WireMessage(com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 20 with WireMessage

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

the class ProtocolTest method testSSLMessage.

@Test
public void testSSLMessage() 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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) WireMessage(com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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