Search in sources :

Example 1 with CopyDataMessage

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

the class ProtocolTest method testMultipleCopyDataMessages.

@Test
public void testMultipleCopyDataMessages() throws Exception {
    when(connectionHandler.getSpannerConnection()).thenReturn(connection);
    when(connectionHandler.getStatus()).thenReturn(ConnectionStatus.COPY_IN);
    byte[] messageMetadata = { 'd' };
    byte[] payload1 = "1\t'one'\n2\t".getBytes();
    byte[] payload2 = "'two'\n3\t'th".getBytes();
    byte[] payload3 = "ree'\n4\t'four'\n".getBytes();
    byte[] length1 = intToBytes(4 + payload1.length);
    byte[] length2 = intToBytes(4 + payload2.length);
    byte[] length3 = intToBytes(4 + payload3.length);
    byte[] value1 = Bytes.concat(messageMetadata, length1, payload1);
    byte[] value2 = Bytes.concat(messageMetadata, length2, payload2);
    byte[] value3 = Bytes.concat(messageMetadata, length3, payload3);
    DataInputStream inputStream1 = new DataInputStream(new ByteArrayInputStream(value1));
    DataInputStream inputStream2 = new DataInputStream(new ByteArrayInputStream(value2));
    DataInputStream inputStream3 = new DataInputStream(new ByteArrayInputStream(value3));
    ResultSet spannerType = mock(ResultSet.class);
    when(spannerType.getString("column_name")).thenReturn("key", "value");
    when(spannerType.getString("data_type")).thenReturn("bigint", "character varying");
    when(spannerType.next()).thenReturn(true, true, false);
    when(connection.executeQuery(any(Statement.class))).thenReturn(spannerType);
    CopyStatement copyStatement = new CopyStatement(connectionHandler, options, parse("COPY keyvalue FROM STDIN;"));
    copyStatement.execute();
    when(connectionHandler.getActiveStatement()).thenReturn(copyStatement);
    when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
    when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
    {
        when(connectionMetadata.getInputStream()).thenReturn(inputStream1);
        WireMessage message = ControlMessage.create(connectionHandler);
        assertEquals(CopyDataMessage.class, message.getClass());
        assertArrayEquals(payload1, ((CopyDataMessage) message).getPayload());
        CopyDataMessage copyDataMessage = (CopyDataMessage) message;
        copyDataMessage.send();
    }
    {
        when(connectionMetadata.getInputStream()).thenReturn(inputStream2);
        WireMessage message = ControlMessage.create(connectionHandler);
        assertEquals(CopyDataMessage.class, message.getClass());
        assertArrayEquals(payload2, ((CopyDataMessage) message).getPayload());
        CopyDataMessage copyDataMessage = (CopyDataMessage) message;
        copyDataMessage.send();
    }
    {
        when(connectionMetadata.getInputStream()).thenReturn(inputStream3);
        WireMessage message = ControlMessage.create(connectionHandler);
        assertEquals(CopyDataMessage.class, message.getClass());
        assertArrayEquals(payload3, ((CopyDataMessage) message).getPayload());
        CopyDataMessage copyDataMessage = (CopyDataMessage) message;
        copyDataMessage.send();
    }
}
Also used : CopyDataMessage(com.google.cloud.spanner.pgadapter.wireprotocol.CopyDataMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) CopyStatement(com.google.cloud.spanner.pgadapter.statements.CopyStatement) ParsedStatement(com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement) IntermediateStatement(com.google.cloud.spanner.pgadapter.statements.IntermediateStatement) MatcherStatement(com.google.cloud.spanner.pgadapter.statements.MatcherStatement) IntermediatePortalStatement(com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement) IntermediatePreparedStatement(com.google.cloud.spanner.pgadapter.statements.IntermediatePreparedStatement) CopyStatement(com.google.cloud.spanner.pgadapter.statements.CopyStatement) Statement(com.google.cloud.spanner.Statement) ResultSet(com.google.cloud.spanner.ResultSet) WireMessage(com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 2 with CopyDataMessage

use of com.google.cloud.spanner.pgadapter.wireprotocol.CopyDataMessage 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)

Aggregations

CopyStatement (com.google.cloud.spanner.pgadapter.statements.CopyStatement)2 CopyDataMessage (com.google.cloud.spanner.pgadapter.wireprotocol.CopyDataMessage)2 WireMessage (com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 DataInputStream (java.io.DataInputStream)2 Test (org.junit.Test)2 ResultSet (com.google.cloud.spanner.ResultSet)1 Statement (com.google.cloud.spanner.Statement)1 ParsedStatement (com.google.cloud.spanner.connection.AbstractStatementParser.ParsedStatement)1 IntermediatePortalStatement (com.google.cloud.spanner.pgadapter.statements.IntermediatePortalStatement)1 IntermediatePreparedStatement (com.google.cloud.spanner.pgadapter.statements.IntermediatePreparedStatement)1 IntermediateStatement (com.google.cloud.spanner.pgadapter.statements.IntermediateStatement)1 MatcherStatement (com.google.cloud.spanner.pgadapter.statements.MatcherStatement)1 MutationWriter (com.google.cloud.spanner.pgadapter.utils.MutationWriter)1