Search in sources :

Example 1 with WireMessage

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

the class ProtocolTest method testParseMessageGsqlException.

@Test
public void testParseMessageGsqlException() throws Exception {
    byte[] messageMetadata = { 'P' };
    String statementName = "some statement\0";
    String payload = "/*GSQL*/ 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);
    List<Integer> expectedParameterDataTypes = Arrays.asList(1002);
    String expectedSQL = "/*GSQL*/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);
    Mockito.when(connectionHandler.getSpannerConnection()).thenReturn(connection);
    Mockito.when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
    Mockito.when(connectionMetadata.getInputStream()).thenReturn(inputStream);
    Mockito.when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
    WireMessage message = ControlMessage.create(connectionHandler);
    Assert.assertEquals(message.getClass(), ParseMessage.class);
    Assert.assertEquals(((ParseMessage) message).getName(), expectedMessageName);
    Assert.assertEquals(((ParseMessage) message).getStatement().getSql(), expectedSQL);
    Assert.assertThat(((ParseMessage) message).getStatement().getParameterDataTypes(), is(expectedParameterDataTypes));
    Mockito.when(connectionHandler.hasStatement(anyString())).thenReturn(false);
    message.send();
    Mockito.verify(connectionHandler, Mockito.times(1)).registerStatement(expectedMessageName, ((ParseMessage) message).getStatement());
    // ParseCompleteResponse
    DataInputStream outputResult = inputStreamFromOutputStream(result);
    Assert.assertEquals(outputResult.readByte(), '1');
    Assert.assertEquals(outputResult.readInt(), 4);
}
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 2 with WireMessage

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

the class ProtocolTest method testBindMessageOneNonTextParam.

@Test
public void testBindMessageOneNonTextParam() throws Exception {
    byte[] messageMetadata = { 'B' };
    String portalName = "some portal\0";
    String statementName = "some statement\0";
    byte[] parameterCodesCount = { 0, 2 };
    // First is text, second binary
    byte[] parameterCodes = { 0, 0, 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 = Arrays.asList((short) 0, (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 3 with WireMessage

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

the class ProtocolTest method testFlushMessageInTransaction.

@Test
public void testFlushMessageInTransaction() throws Exception {
    byte[] messageMetadata = { 'H' };
    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.getSpannerConnection()).thenReturn(connection);
    when(connection.isInTransaction()).thenReturn(true);
    when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
    when(connectionMetadata.getInputStream()).thenReturn(inputStream);
    when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
    WireMessage message = ControlMessage.create(connectionHandler);
    assertEquals(FlushMessage.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 4 with WireMessage

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

the class ProtocolTest method testSSLMessageFailsWhenCalledTwice.

@Test
public void testSSLMessageFailsWhenCalledTwice() 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());
    assertThrows(IOException.class, message::send);
}
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 5 with WireMessage

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

the class ProtocolTest method testFlushMessage.

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

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