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