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());
}
}
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;
}
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");
}
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));
}
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));
}
Aggregations