Search in sources :

Example 1 with InstanceId

use of com.google.cloud.spanner.InstanceId in project java-spanner by googleapis.

the class ITDatabaseTest method instanceNotFound.

@Test
public void instanceNotFound() {
    InstanceId testId = env.getTestHelper().getInstanceId();
    InstanceId nonExistingInstanceId = InstanceId.of(testId.getProject(), testId.getInstance() + "-na");
    DatabaseClient client = env.getTestHelper().getClient().getDatabaseClient(DatabaseId.of(nonExistingInstanceId, "some-db"));
    try (ResultSet rs = client.singleUse().executeQuery(Statement.of("SELECT 1"))) {
        rs.next();
        fail("missing expected exception");
    } catch (InstanceNotFoundException e) {
        assertThat(e.getResourceName()).isEqualTo(nonExistingInstanceId.getName());
    }
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) InstanceId(com.google.cloud.spanner.InstanceId) InstanceNotFoundException(com.google.cloud.spanner.InstanceNotFoundException) ResultSet(com.google.cloud.spanner.ResultSet) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Test(org.junit.Test)

Example 2 with InstanceId

use of com.google.cloud.spanner.InstanceId in project testcontainers-java by testcontainers.

the class SpannerEmulatorContainerTest method createInstance.

// }
// createInstance {
private InstanceId createInstance(Spanner spanner) throws InterruptedException, ExecutionException {
    InstanceConfigId instanceConfig = InstanceConfigId.of(PROJECT_NAME, "emulator-config");
    InstanceId instanceId = InstanceId.of(PROJECT_NAME, INSTANCE_NAME);
    InstanceAdminClient insAdminClient = spanner.getInstanceAdminClient();
    Instance instance = insAdminClient.createInstance(InstanceInfo.newBuilder(instanceId).setNodeCount(1).setDisplayName("Test instance").setInstanceConfigId(instanceConfig).build()).get();
    return instanceId;
}
Also used : InstanceConfigId(com.google.cloud.spanner.InstanceConfigId) InstanceId(com.google.cloud.spanner.InstanceId) Instance(com.google.cloud.spanner.Instance) InstanceAdminClient(com.google.cloud.spanner.InstanceAdminClient)

Example 3 with InstanceId

use of com.google.cloud.spanner.InstanceId in project testcontainers-java by testcontainers.

the class SpannerEmulatorContainerTest method testSimple.

// testWithEmulatorContainer {
@Test
public void testSimple() throws ExecutionException, InterruptedException {
    SpannerOptions options = SpannerOptions.newBuilder().setEmulatorHost(emulator.getEmulatorGrpcEndpoint()).setCredentials(NoCredentials.getInstance()).setProjectId(PROJECT_NAME).build();
    Spanner spanner = options.getService();
    InstanceId instanceId = createInstance(spanner);
    createDatabase(spanner);
    DatabaseId databaseId = DatabaseId.of(instanceId, DATABASE_NAME);
    DatabaseClient dbClient = spanner.getDatabaseClient(databaseId);
    dbClient.readWriteTransaction().run(tx -> {
        String sql1 = "Delete from TestTable where 1=1";
        tx.executeUpdate(Statement.of(sql1));
        String sql = "INSERT INTO TestTable (Key, Value) VALUES (1, 'Java'), (2, 'Go')";
        tx.executeUpdate(Statement.of(sql));
        return null;
    });
    ResultSet resultSet = dbClient.readOnlyTransaction().executeQuery(Statement.of("select * from TestTable order by Key"));
    resultSet.next();
    assertThat(resultSet.getLong(0)).isEqualTo(1);
    assertThat(resultSet.getString(1)).isEqualTo("Java");
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) InstanceId(com.google.cloud.spanner.InstanceId) ResultSet(com.google.cloud.spanner.ResultSet) DatabaseId(com.google.cloud.spanner.DatabaseId) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Spanner(com.google.cloud.spanner.Spanner) Test(org.junit.Test)

Example 4 with InstanceId

use of com.google.cloud.spanner.InstanceId in project java-spanner by googleapis.

the class ITBackupTest method test05_backupCreationWithVersionTimeInTheFutureFails.

@Test(expected = SpannerException.class)
public void test05_backupCreationWithVersionTimeInTheFutureFails() throws Exception {
    final Database testDatabase = testHelper.createTestDatabase();
    final DatabaseId databaseId = testDatabase.getId();
    final InstanceId instanceId = databaseId.getInstanceId();
    final String backupId = testHelper.getUniqueBackupId();
    final Timestamp expireTime = afterDays(7);
    final Timestamp versionTime = afterDays(1);
    final Backup backupToCreate = dbAdminClient.newBackupBuilder(BackupId.of(instanceId, backupId)).setDatabase(databaseId).setExpireTime(expireTime).setVersionTime(versionTime).build();
    getOrThrow(dbAdminClient.createBackup(backupToCreate));
}
Also used : InstanceId(com.google.cloud.spanner.InstanceId) Backup(com.google.cloud.spanner.Backup) Database(com.google.cloud.spanner.Database) DatabaseId(com.google.cloud.spanner.DatabaseId) Timestamp(com.google.cloud.Timestamp) Test(org.junit.Test) SlowTest(com.google.cloud.spanner.SlowTest)

Example 5 with InstanceId

use of com.google.cloud.spanner.InstanceId in project java-spanner by googleapis.

the class ITBackupTest method test04_backupCreationWithVersionTimeTooFarInThePastFails.

@Test(expected = SpannerException.class)
public void test04_backupCreationWithVersionTimeTooFarInThePastFails() throws Exception {
    final Database testDatabase = testHelper.createTestDatabase();
    final DatabaseId databaseId = testDatabase.getId();
    final InstanceId instanceId = databaseId.getInstanceId();
    final String backupId = testHelper.getUniqueBackupId();
    final Timestamp expireTime = afterDays(7);
    final Timestamp versionTime = daysAgo(30);
    final Backup backupToCreate = dbAdminClient.newBackupBuilder(BackupId.of(instanceId, backupId)).setDatabase(databaseId).setExpireTime(expireTime).setVersionTime(versionTime).build();
    getOrThrow(dbAdminClient.createBackup(backupToCreate));
}
Also used : InstanceId(com.google.cloud.spanner.InstanceId) Backup(com.google.cloud.spanner.Backup) Database(com.google.cloud.spanner.Database) DatabaseId(com.google.cloud.spanner.DatabaseId) Timestamp(com.google.cloud.Timestamp) Test(org.junit.Test) SlowTest(com.google.cloud.spanner.SlowTest)

Aggregations

InstanceId (com.google.cloud.spanner.InstanceId)5 Test (org.junit.Test)4 DatabaseId (com.google.cloud.spanner.DatabaseId)3 Timestamp (com.google.cloud.Timestamp)2 Backup (com.google.cloud.spanner.Backup)2 Database (com.google.cloud.spanner.Database)2 DatabaseClient (com.google.cloud.spanner.DatabaseClient)2 ResultSet (com.google.cloud.spanner.ResultSet)2 SlowTest (com.google.cloud.spanner.SlowTest)2 Instance (com.google.cloud.spanner.Instance)1 InstanceAdminClient (com.google.cloud.spanner.InstanceAdminClient)1 InstanceConfigId (com.google.cloud.spanner.InstanceConfigId)1 InstanceNotFoundException (com.google.cloud.spanner.InstanceNotFoundException)1 ParallelIntegrationTest (com.google.cloud.spanner.ParallelIntegrationTest)1 Spanner (com.google.cloud.spanner.Spanner)1 SpannerOptions (com.google.cloud.spanner.SpannerOptions)1