Search in sources :

Example 1 with ControlMessage

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));
}
Also used : JSONObject(org.json.simple.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) JSONParser(org.json.simple.parser.JSONParser) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) ControlMessage(com.google.cloud.spanner.pgadapter.wireprotocol.ControlMessage) Test(org.junit.Test)

Aggregations

OptionsMetadata (com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata)1 ControlMessage (com.google.cloud.spanner.pgadapter.wireprotocol.ControlMessage)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 JSONObject (org.json.simple.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1 Test (org.junit.Test)1