use of com.google.cloud.spanner.pgadapter.wireprotocol.ControlMessage in project pgadapter by GoogleCloudPlatform.
the class ControlMessageTest method testInsertResult.
@Test
public void testInsertResult() throws Exception {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(buffer);
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(new byte[] { (byte) QUERY_IDENTIFIER, 0, 0, 0, 5, 0 }));
when(connectionMetadata.getInputStream()).thenReturn(inputStream);
when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
when(intermediateStatement.getStatementType(0)).thenReturn(StatementType.UPDATE);
when(intermediateStatement.getCommandTag(0)).thenReturn("INSERT");
when(intermediateStatement.getUpdateCount(0)).thenReturn(1L);
when(connectionHandler.getSpannerConnection()).thenReturn(connection);
JSONParser parser = new JSONParser();
JSONObject commandMetadata = (JSONObject) parser.parse(EMPTY_COMMAND_JSON);
OptionsMetadata options = new OptionsMetadata("jdbc:cloudspanner:/projects/test-project/instances/test-instance/databases/test-database", 8888, TextFormat.POSTGRESQL, false, false, false, false, commandMetadata);
ProxyServer server = new ProxyServer(options);
when(connectionHandler.getServer()).thenReturn(server);
ControlMessage controlMessage = ControlMessage.create(connectionHandler);
controlMessage.sendSpannerResult(0, intermediateStatement, QueryMode.SIMPLE, 0L);
DataInputStream outputReader = new DataInputStream(new ByteArrayInputStream(buffer.toByteArray()));
// identifier
outputReader.readByte();
// length
outputReader.readInt();
final String resultMessage = "INSERT 0 1";
int numOfBytes = resultMessage.getBytes(UTF8).length;
byte[] bytes = new byte[numOfBytes];
assertEquals(numOfBytes, outputReader.read(bytes, 0, numOfBytes));
assertEquals(resultMessage, new String(bytes, UTF8));
}
Aggregations