Search in sources :

Example 6 with WireMessage

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

the class ProtocolTest method testDescribePortalMessage.

@Test
public void testDescribePortalMessage() throws Exception {
    byte[] messageMetadata = { 'D' };
    byte[] statementType = { 'P' };
    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.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(DescribeMessage.class, message.getClass());
    assertEquals(expectedStatementName, ((DescribeMessage) message).getName());
    verify(connectionHandler).getPortal("some statement");
    DescribeMessage messageSpy = (DescribeMessage) spy(message);
    doNothing().when(messageSpy).handleDescribePortal();
    messageSpy.send();
    verify(messageSpy).handleDescribePortal();
}
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 7 with WireMessage

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

the class ProtocolTest method testParseMessageExceptsIfNameIsNull.

@Test
public void testParseMessageExceptsIfNameIsNull() 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);
    when(connectionHandler.hasStatement(anyString())).thenReturn(true);
    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);
}
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 8 with WireMessage

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

the class ProtocolTest method testSyncMessage.

@Test
public void testSyncMessage() 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.IDLE);
    when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
    when(connectionMetadata.getInputStream()).thenReturn(inputStream);
    when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
    WireMessage message = ControlMessage.create(connectionHandler);
    assertEquals(message.getClass(), SyncMessage.class);
    message.send();
    // ReadyResponse
    DataInputStream outputResult = inputStreamFromOutputStream(result);
    assertEquals('Z', outputResult.readByte());
    assertEquals(5, outputResult.readInt());
    assertEquals('I', 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 9 with WireMessage

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

the class ProtocolTest method testCopyFailMessage.

@Test
public void testCopyFailMessage() throws Exception {
    byte[] messageMetadata = { 'f' };
    byte[] errorMessage = "Error Message\0".getBytes();
    byte[] length = intToBytes(4 + errorMessage.length);
    byte[] value = Bytes.concat(messageMetadata, length, errorMessage);
    DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    DataOutputStream outputStream = new DataOutputStream(result);
    String expectedErrorMessage = "Error Message";
    CopyStatement copyStatement = mock(CopyStatement.class);
    MutationWriter mutationWriter = mock(MutationWriter.class);
    when(copyStatement.getMutationWriter()).thenReturn(mutationWriter);
    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);
    WireMessage message = ControlMessage.create(connectionHandler);
    assertEquals(CopyFailMessage.class, message.getClass());
    assertEquals(expectedErrorMessage, ((CopyFailMessage) message).getErrorMessage());
    message.send();
    verify(mutationWriter).rollback();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CopyStatement(com.google.cloud.spanner.pgadapter.statements.CopyStatement) DataOutputStream(java.io.DataOutputStream) MutationWriter(com.google.cloud.spanner.pgadapter.utils.MutationWriter) WireMessage(com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 10 with WireMessage

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

the class ProtocolTest method testTerminateMessage.

@Test
public void testTerminateMessage() throws Exception {
    byte[] messageMetadata = { 'X' };
    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.getConnectionMetadata()).thenReturn(connectionMetadata);
    when(connectionMetadata.getInputStream()).thenReturn(inputStream);
    when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
    WireMessage message = ControlMessage.create(connectionHandler);
    assertEquals(TerminateMessage.class, message.getClass());
}
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