Search in sources :

Example 1 with OptionsMetadata

use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.

the class ITParameterizedQueryTest method setUp.

@Before
public void setUp() throws Exception {
    final String ddl = "CREATE TABLE users (\n" + "  id   bigint PRIMARY KEY,\n" + "  age  bigint,\n" + "  name   text\n" + ");";
    List<String> values = new ArrayList<>(Arrays.asList("(1, 1, '1')", "(2, 20, 'Joe')", "(3, 23, 'Jack')"));
    String dml = "INSERT INTO users (id, age, name) VALUES " + String.join(", ", values);
    // TODO: Refactor the integration tests to use a common subclass, as this is repeated in each
    // class.
    testEnv.setUp();
    Database db = testEnv.createDatabase();
    testEnv.updateDdl(db.getId().getDatabase(), Arrays.asList(ddl));
    testEnv.updateTables(db.getId().getDatabase(), Arrays.asList(dml));
    String credentials = testEnv.getCredentials();
    ImmutableList.Builder<String> argsListBuilder = ImmutableList.<String>builder().add("-p", testEnv.getProjectId(), "-i", testEnv.getInstanceId(), "-d", db.getId().getDatabase(), "-s", String.valueOf(testEnv.getPort()), "-e", testEnv.getUrl().getHost());
    if (credentials != null) {
        argsListBuilder.add("-c", testEnv.getCredentials());
    }
    args = argsListBuilder.build().toArray(new String[0]);
    server = new ProxyServer(new OptionsMetadata(args));
    server.startServer();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) Database(com.google.cloud.spanner.Database) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) Before(org.junit.Before)

Example 2 with OptionsMetadata

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

Example 3 with OptionsMetadata

use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.

the class ITPgxTest method data.

@Parameters(name = "preferQueryMode = {0}, useDomainSocket = {1}")
public static List<Object[]> data() {
    OptionsMetadata options = new OptionsMetadata(new String[] { "-p p", "-i i" });
    boolean[] useDomainSockets;
    if (options.isDomainSocketEnabled()) {
        useDomainSockets = new boolean[] { true, false };
    } else {
        useDomainSockets = new boolean[] { false };
    }
    String[] queryModes = { "extended", "simple" };
    List<Object[]> parameters = new ArrayList<>();
    for (String queryMode : queryModes) {
        for (boolean useDomainSocket : useDomainSockets) {
            parameters.add(new Object[] { queryMode, useDomainSocket });
        }
    }
    return parameters;
}
Also used : ArrayList(java.util.ArrayList) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) Parameters(org.junit.runners.Parameterized.Parameters)

Example 4 with OptionsMetadata

use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.

the class RowDescriptionTest method SendPayloadStatementTest.

@Test
public void SendPayloadStatementTest() throws Exception {
    int COLUMN_COUNT = 2;
    String COLUMN_NAME = "default-column-name";
    Type rowType = Type.struct(StructField.of(COLUMN_NAME, Type.string()), StructField.of(COLUMN_NAME, Type.string()));
    when(metadata.getColumnCount()).thenReturn(COLUMN_COUNT);
    when(metadata.getType()).thenReturn(rowType);
    when(metadata.getColumnType(Mockito.anyInt())).thenReturn(Type.int64());
    when(statement.getResultFormatCode(Mockito.anyInt())).thenReturn((short) 0).thenReturn((short) 0).thenReturn((short) 1);
    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);
    QueryMode mode = QueryMode.EXTENDED;
    RowDescriptionResponse response = new RowDescriptionResponse(output, statement, metadata, options, mode);
    response.sendPayload();
    DataInputStream outputReader = new DataInputStream(new ByteArrayInputStream(buffer.toByteArray()));
    // column count
    assertEquals(COLUMN_COUNT, outputReader.readShort());
    for (int i = 0; i < COLUMN_COUNT; i++) {
        // column name
        int numOfBytes = COLUMN_NAME.getBytes(UTF8).length;
        byte[] bytes = new byte[numOfBytes];
        assertEquals(numOfBytes, outputReader.read(bytes, 0, numOfBytes));
        assertEquals(new String(bytes, UTF8), COLUMN_NAME);
        // null terminator
        assertEquals(DEFAULT_FLAG, outputReader.readByte());
        // table oid
        assertEquals(DEFAULT_FLAG, outputReader.readInt());
        // column index
        assertEquals(DEFAULT_FLAG, outputReader.readShort());
        // type oid
        assertEquals(Oid.INT8, outputReader.readInt());
        // type size
        assertEquals(8, outputReader.readShort());
        // type modifier
        assertEquals(DEFAULT_FLAG, outputReader.readInt());
        // format code
        assertEquals(i, outputReader.readShort());
    }
}
Also used : Type(com.google.cloud.spanner.Type) JSONObject(org.json.simple.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) QueryMode(com.google.cloud.spanner.pgadapter.ConnectionHandler.QueryMode) JSONParser(org.json.simple.parser.JSONParser) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 5 with OptionsMetadata

use of com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata in project pgadapter by GoogleCloudPlatform.

the class ITJdbcTest method data.

@Parameters(name = "preferQueryMode = {0}, useDomainSocket = {1}")
public static List<Object[]> data() {
    OptionsMetadata options = new OptionsMetadata(new String[] { "-p p", "-i i" });
    boolean[] useDomainSockets;
    if (options.isDomainSocketEnabled()) {
        useDomainSockets = new boolean[] { true, false };
    } else {
        useDomainSockets = new boolean[] { false };
    }
    String[] queryModes = { "extended", "simple" };
    List<Object[]> parameters = new ArrayList<>();
    for (String queryMode : queryModes) {
        for (boolean useDomainSocket : useDomainSockets) {
            parameters.add(new Object[] { queryMode, useDomainSocket });
        }
    }
    return parameters;
}
Also used : ArrayList(java.util.ArrayList) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) Parameters(org.junit.runners.Parameterized.Parameters)

Aggregations

OptionsMetadata (com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata)13 Test (org.junit.Test)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 DataInputStream (java.io.DataInputStream)5 JSONObject (org.json.simple.JSONObject)5 JSONParser (org.json.simple.parser.JSONParser)5 QueryMode (com.google.cloud.spanner.pgadapter.ConnectionHandler.QueryMode)4 Type (com.google.cloud.spanner.Type)3 ImmutableList (com.google.common.collect.ImmutableList)3 ArrayList (java.util.ArrayList)3 MockSpannerServiceImpl (com.google.cloud.spanner.MockSpannerServiceImpl)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 InetSocketAddress (java.net.InetSocketAddress)2 Parameters (org.junit.runners.Parameterized.Parameters)2 InternalApi (com.google.api.core.InternalApi)1 Database (com.google.cloud.spanner.Database)1 MockDatabaseAdminServiceImpl (com.google.cloud.spanner.MockDatabaseAdminServiceImpl)1 MockOperationsServiceImpl (com.google.cloud.spanner.MockOperationsServiceImpl)1 SpannerException (com.google.cloud.spanner.SpannerException)1