use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class EmulatorUtilTest method testCreateInstanceAndDatabase_propagatesInterruptsOnDatabaseCreation.
@Test
public void testCreateInstanceAndDatabase_propagatesInterruptsOnDatabaseCreation() throws InterruptedException, ExecutionException {
Spanner spanner = mock(Spanner.class);
SpannerOptions options = mock(SpannerOptions.class);
when(spanner.getOptions()).thenReturn(options);
when(options.getCredentials()).thenReturn(NoCredentials.getInstance());
InstanceAdminClient instanceClient = mock(InstanceAdminClient.class);
@SuppressWarnings("unchecked") OperationFuture<Instance, CreateInstanceMetadata> instanceOperationFuture = mock(OperationFuture.class);
when(spanner.getInstanceAdminClient()).thenReturn(instanceClient);
when(instanceClient.createInstance(any(InstanceInfo.class))).thenReturn(instanceOperationFuture);
when(instanceOperationFuture.get()).thenReturn(mock(Instance.class));
DatabaseAdminClient databaseClient = mock(DatabaseAdminClient.class);
@SuppressWarnings("unchecked") OperationFuture<Database, CreateDatabaseMetadata> databaseOperationFuture = mock(OperationFuture.class);
when(spanner.getDatabaseAdminClient()).thenReturn(databaseClient);
when(databaseClient.createDatabase(eq("test-instance"), eq("test-database"), eq(ImmutableList.of()))).thenReturn(databaseOperationFuture);
when(databaseOperationFuture.get()).thenThrow(new InterruptedException());
try {
EmulatorUtil.maybeCreateInstanceAndDatabase(spanner, DatabaseId.of("test-project", "test-instance", "test-database"));
fail("missing expected exception");
} catch (SpannerException e) {
assertEquals(ErrorCode.CANCELLED, e.getErrorCode());
}
}
use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class EmulatorUtilTest method testCreateInstanceAndDatabase_propagatesOtherErrorsOnDatabaseCreation.
@Test
public void testCreateInstanceAndDatabase_propagatesOtherErrorsOnDatabaseCreation() throws InterruptedException, ExecutionException {
Spanner spanner = mock(Spanner.class);
SpannerOptions options = mock(SpannerOptions.class);
when(spanner.getOptions()).thenReturn(options);
when(options.getCredentials()).thenReturn(NoCredentials.getInstance());
InstanceAdminClient instanceClient = mock(InstanceAdminClient.class);
@SuppressWarnings("unchecked") OperationFuture<Instance, CreateInstanceMetadata> instanceOperationFuture = mock(OperationFuture.class);
when(spanner.getInstanceAdminClient()).thenReturn(instanceClient);
when(instanceClient.createInstance(any(InstanceInfo.class))).thenReturn(instanceOperationFuture);
when(instanceOperationFuture.get()).thenReturn(mock(Instance.class));
DatabaseAdminClient databaseClient = mock(DatabaseAdminClient.class);
@SuppressWarnings("unchecked") OperationFuture<Database, CreateDatabaseMetadata> databaseOperationFuture = mock(OperationFuture.class);
when(spanner.getDatabaseAdminClient()).thenReturn(databaseClient);
when(databaseClient.createDatabase(eq("test-instance"), eq("test-database"), eq(ImmutableList.of()))).thenReturn(databaseOperationFuture);
when(databaseOperationFuture.get()).thenThrow(new ExecutionException(SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Invalid database options")));
try {
EmulatorUtil.maybeCreateInstanceAndDatabase(spanner, DatabaseId.of("test-project", "test-instance", "test-database"));
fail("missing expected exception");
} catch (SpannerException e) {
assertEquals(ErrorCode.INVALID_ARGUMENT, e.getErrorCode());
}
}
use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class ITDatabaseAdminTest method databaseOperationsViaEntity.
@Test
public void databaseOperationsViaEntity() throws Exception {
String dbId = testHelper.getUniqueDatabaseId();
String instanceId = testHelper.getInstanceId().getInstance();
String statement1 = "CREATE TABLE T (\n" + " K STRING(MAX),\n" + ") PRIMARY KEY(K)";
OperationFuture<Database, CreateDatabaseMetadata> op = dbAdminClient.createDatabase(instanceId, dbId, ImmutableList.of(statement1));
Database db = op.get(TIMEOUT_MINUTES, TimeUnit.MINUTES);
dbs.add(db);
assertThat(db.getId().getDatabase()).isEqualTo(dbId);
db = db.reload();
assertThat(db.getId().getDatabase()).isEqualTo(dbId);
String statement2 = "CREATE TABLE T2 (\n" + " K2 STRING(MAX),\n" + ") PRIMARY KEY(K2)";
OperationFuture<?, ?> op2 = db.updateDdl(ImmutableList.of(statement2), null);
op2.get(TIMEOUT_MINUTES, TimeUnit.MINUTES);
Iterable<String> statementsInDb = db.getDdl();
assertThat(statementsInDb).containsExactly(statement1, statement2);
db.drop();
dbs.clear();
try {
db.reload();
fail("Expected exception");
} catch (SpannerException ex) {
assertThat(ex.getErrorCode()).isEqualTo(ErrorCode.NOT_FOUND);
}
}
use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class ITDatabaseTest method testNumericPrimaryKey.
@Test
public void testNumericPrimaryKey() throws InterruptedException, ExecutionException, TimeoutException {
assumeFalse("Emulator does not support numeric primary keys", isUsingEmulator());
final String projectId = env.getTestHelper().getInstanceId().getProject();
final String instanceId = env.getTestHelper().getInstanceId().getInstance();
final String databaseId = env.getTestHelper().getUniqueDatabaseId();
final String table = "NumericTable";
final DatabaseId id = DatabaseId.of(projectId, instanceId, databaseId);
final DatabaseAdminClient databaseAdminClient = env.getTestHelper().getClient().getDatabaseAdminClient();
try {
// Creates table with numeric primary key
final OperationFuture<Database, CreateDatabaseMetadata> operation = databaseAdminClient.createDatabase(instanceId, databaseId, Collections.singletonList("CREATE TABLE " + table + " (" + "Id NUMERIC NOT NULL" + ") PRIMARY KEY (Id)"));
final Database database = operation.get(10, TimeUnit.MINUTES);
assertNotNull(database);
// Writes data into the table
final DatabaseClient databaseClient = env.getTestHelper().getClient().getDatabaseClient(id);
final ArrayList<Mutation> mutations = new ArrayList<>();
for (int i = 0; i < 5; i++) {
mutations.add(Mutation.newInsertBuilder(table).set("Id").to(new BigDecimal(i + "")).build());
}
databaseClient.write(mutations);
// Reads the data to verify the writes
try (final ResultSet resultSet = databaseClient.singleUse().read(table, KeySet.all(), Collections.singletonList("Id"))) {
for (int i = 0; resultSet.next(); i++) {
assertEquals(new BigDecimal(i + ""), resultSet.getBigDecimal("Id"));
}
}
// Deletes data from the table, leaving only the Id = 0 row
databaseClient.readWriteTransaction().run(new TransactionCallable<Object>() {
@Nullable
@Override
public Object run(TransactionContext transaction) throws Exception {
transaction.executeUpdate(Statement.of("DELETE FROM " + table + " WHERE Id > 0"));
return null;
}
});
// Reads the data to verify the deletes only left a single row left
try (final ResultSet resultSet = databaseClient.singleUse().executeQuery(Statement.of("SELECT COUNT(1) as cnt FROM " + table))) {
resultSet.next();
assertEquals(1L, resultSet.getLong("cnt"));
}
} finally {
databaseAdminClient.dropDatabase(instanceId, databaseId);
}
}
use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class ITBackupTest method testMetadata.
private void testMetadata(OperationFuture<Backup, CreateBackupMetadata> operation, String backupId, Database database) throws InterruptedException, ExecutionException {
logger.info("Getting operation metadata");
CreateBackupMetadata metadata1 = operation.getMetadata().get();
String expectedOperationName1 = String.format(EXPECTED_OP_NAME_FORMAT, testHelper.getInstanceId().getName(), backupId);
assertTrue(operation.getName().startsWith(expectedOperationName1));
assertEquals(database.getId().getName(), metadata1.getDatabase());
assertEquals(BackupId.of(testHelper.getInstanceId(), backupId).getName(), metadata1.getName());
logger.info("Finished metadata tests");
}
Aggregations