use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testBindMessage.
@Test
public void testBindMessage() throws Exception {
byte[] messageMetadata = { 'B' };
String portalName = "some portal\0";
String statementName = "some statement\0";
// Denotes no codes
byte[] parameterCodesCount = { 0, 0 };
byte[] parameterCount = { 0, 1 };
byte[] parameter = "someUser\0".getBytes();
byte[] parameterLength = intToBytes(parameter.length);
byte[] resultCodesCount = { 0, 0 };
byte[] length = intToBytes(4 + portalName.length() + statementName.length() + parameterCodesCount.length + parameterCount.length + parameterLength.length + parameter.length + resultCodesCount.length);
byte[] value = Bytes.concat(messageMetadata, length, portalName.getBytes(), statementName.getBytes(), parameterCodesCount, parameterCount, parameterLength, parameter, resultCodesCount);
when(connectionHandler.getStatement(anyString())).thenReturn(intermediatePreparedStatement);
byte[][] expectedParameters = { parameter };
List<Short> expectedFormatCodes = new ArrayList<>();
String expectedPortalName = "some portal";
String expectedStatementName = "some statement";
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(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(expectedFormatCodes, ((BindMessage) message).getResultFormatCodes());
when(intermediatePreparedStatement.bind(ArgumentMatchers.anyString(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(intermediatePortalStatement);
message.send();
verify(connectionHandler).registerPortal(expectedPortalName, intermediatePortalStatement);
// BindCompleteResponse
DataInputStream outputResult = inputStreamFromOutputStream(result);
assertEquals('2', outputResult.readByte());
assertEquals(4, outputResult.readInt());
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class StatementTest method testBatchStatementsWithComments.
@Test
public void testBatchStatementsWithComments() throws Exception {
byte[] messageMetadata = { 'Q', 0, 0, 0, (byte) 132 };
String payload = "INSERT INTO users (name) VALUES (';;test;;'); /* Comment;; Comment; */INSERT INTO users (name1, name2) VALUES ('''''', ';'';');\0";
byte[] value = Bytes.concat(messageMetadata, payload.getBytes());
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
when(connectionHandler.getSpannerConnection()).thenReturn(connection);
when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
when(connectionHandler.getServer()).thenReturn(server);
when(server.getOptions()).thenReturn(options);
when(options.requiresMatcher()).thenReturn(false);
when(connectionMetadata.getInputStream()).thenReturn(inputStream);
when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
WireMessage message = ControlMessage.create(connectionHandler);
assertEquals(QueryMessage.class, message.getClass());
IntermediateStatement intermediateStatement = ((QueryMessage) message).getStatement();
assertTrue(intermediateStatement.isBatchedQuery());
assertEquals(2, intermediateStatement.getStatements().size());
assertEquals("INSERT INTO users (name) VALUES (';;test;;')", intermediateStatement.getStatement(0));
assertEquals("INSERT INTO users (name1, name2) VALUES ('''''', ';'';')", intermediateStatement.getStatement(1));
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testSyncMessageInTransaction.
@Test
public void testSyncMessageInTransaction() 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.TRANSACTION);
when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
when(connectionMetadata.getInputStream()).thenReturn(inputStream);
when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
WireMessage message = ControlMessage.create(connectionHandler);
assertEquals(SyncMessage.class, message.getClass());
message.send();
// ReadyResponse
DataInputStream outputResult = inputStreamFromOutputStream(result);
assertEquals('Z', outputResult.readByte());
assertEquals(5, outputResult.readInt());
assertEquals('T', outputResult.readByte());
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage 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);
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.WireMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testSSLMessage.
@Test
public void testSSLMessage() 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());
}
Aggregations