Search in sources :

Example 31 with WireMessage

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) PreparedType(com.google.cloud.spanner.pgadapter.wireprotocol.ControlMessage.PreparedType) 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 32 with WireMessage

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();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) DescribeMessage(com.google.cloud.spanner.pgadapter.wireprotocol.DescribeMessage) 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 33 with WireMessage

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());
}
Also used : ParseMessage(com.google.cloud.spanner.pgadapter.wireprotocol.ParseMessage) 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 34 with WireMessage

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();
}
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