use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class DatabaseAdminClientTest method getDatabaseTest2.
@Test
public void getDatabaseTest2() throws Exception {
Database expectedResponse = Database.newBuilder().setName(DatabaseName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]").toString()).setCreateTime(Timestamp.newBuilder().build()).setRestoreInfo(RestoreInfo.newBuilder().build()).setEncryptionConfig(EncryptionConfig.newBuilder().build()).addAllEncryptionInfo(new ArrayList<EncryptionInfo>()).setVersionRetentionPeriod("versionRetentionPeriod-629783929").setEarliestVersionTime(Timestamp.newBuilder().build()).setDefaultLeader("defaultLeader759009962").setDatabaseDialect(DatabaseDialect.forNumber(0)).build();
mockDatabaseAdmin.addResponse(expectedResponse);
String name = "name3373707";
Database actualResponse = client.getDatabase(name);
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockDatabaseAdmin.getRequests();
Assert.assertEquals(1, actualRequests.size());
GetDatabaseRequest actualRequest = ((GetDatabaseRequest) actualRequests.get(0));
Assert.assertEquals(name, actualRequest.getName());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class EmulatorUtilTest method testCreateInstanceAndDatabase_bothSucceed.
@Test
public void testCreateInstanceAndDatabase_bothSucceed() 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()).thenReturn(mock(Database.class));
EmulatorUtil.maybeCreateInstanceAndDatabase(spanner, DatabaseId.of("test-project", "test-instance", "test-database"));
// Verify that both the instance and the database was created.
verify(instanceClient).createInstance(InstanceInfo.newBuilder(InstanceId.of("test-project", "test-instance")).setDisplayName("Automatically Generated Test Instance").setInstanceConfigId(InstanceConfigId.of("test-project", "emulator-config")).setNodeCount(1).build());
verify(databaseClient).createDatabase("test-instance", "test-database", ImmutableList.of());
}
use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class EmulatorUtilTest method testCreateInstanceAndDatabase_bothFailWithAlreadyExists.
@Test
public void testCreateInstanceAndDatabase_bothFailWithAlreadyExists() 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()).thenThrow(new ExecutionException(SpannerExceptionFactory.newSpannerException(ErrorCode.ALREADY_EXISTS, "Instance already exists")));
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.ALREADY_EXISTS, "Database already exists")));
EmulatorUtil.maybeCreateInstanceAndDatabase(spanner, DatabaseId.of("test-project", "test-instance", "test-database"));
// Verify that both the instance and the database was created.
verify(instanceClient).createInstance(InstanceInfo.newBuilder(InstanceId.of("test-project", "test-instance")).setDisplayName("Automatically Generated Test Instance").setInstanceConfigId(InstanceConfigId.of("test-project", "emulator-config")).setNodeCount(1).build());
verify(databaseClient).createDatabase("test-instance", "test-database", ImmutableList.of());
}
use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class ITDatabaseAdminTest method updateDdlRetry.
@Test
public void updateDdlRetry() 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);
String statement2 = "CREATE TABLE T2 (\n" + " K2 STRING(MAX),\n" + ") PRIMARY KEY(K2)";
OperationFuture<Void, UpdateDatabaseDdlMetadata> op1 = dbAdminClient.updateDatabaseDdl(instanceId, dbId, ImmutableList.of(statement2), "myop");
OperationFuture<Void, UpdateDatabaseDdlMetadata> op2 = dbAdminClient.updateDatabaseDdl(instanceId, dbId, ImmutableList.of(statement2), "myop");
op1.get(TIMEOUT_MINUTES, TimeUnit.MINUTES);
op2.get(TIMEOUT_MINUTES, TimeUnit.MINUTES);
// Remove the progress list from the metadata before comparing, as there could be small
// differences between the two in the reported progress depending on exactly when each
// operation was fetched from the backend.
UpdateDatabaseDdlMetadata metadata1 = op1.getMetadata().get().toBuilder().clearProgress().build();
UpdateDatabaseDdlMetadata metadata2 = op2.getMetadata().get().toBuilder().clearProgress().build();
assertThat(metadata1).isEqualTo(metadata2);
}
use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class ITDatabaseTest method databaseDeletedTest.
@Test
public void databaseDeletedTest() throws Exception {
// Create a test db, do a query, then delete it and verify that it returns
// DatabaseNotFoundExceptions.
Database db = env.getTestHelper().createTestDatabase();
DatabaseClient client = env.getTestHelper().getClient().getDatabaseClient(db.getId());
try (ResultSet rs = client.singleUse().executeQuery(Statement.of("SELECT 1"))) {
assertThat(rs.next()).isTrue();
assertThat(rs.getLong(0)).isEqualTo(1L);
assertThat(rs.next()).isFalse();
}
// Delete the database.
db.drop();
// We need to wait a little before Spanner actually starts sending DatabaseNotFound errors.
ExponentialBackOff backoff = new ExponentialBackOff.Builder().setInitialIntervalMillis(1000).setMaxElapsedTimeMillis(65000).setMaxIntervalMillis(5000).build();
DatabaseNotFoundException notFoundException = null;
long millis = 0L;
while ((millis = backoff.nextBackOffMillis()) != ExponentialBackOff.STOP) {
Thread.sleep(millis);
// Queries to this database should eventually return DatabaseNotFoundExceptions.
try (ResultSet rs = client.singleUse().executeQuery(Statement.of("SELECT 1"))) {
rs.next();
} catch (DatabaseNotFoundException e) {
// This is what we expect.
notFoundException = e;
break;
}
}
assertThat(notFoundException).isNotNull();
// Now re-create a database with the same name.
OperationFuture<Database, CreateDatabaseMetadata> op = env.getTestHelper().getClient().getDatabaseAdminClient().createDatabase(db.getId().getInstanceId().getInstance(), db.getId().getDatabase(), Collections.emptyList());
Database newDb = op.get();
// Queries using the same DatabaseClient should still return DatabaseNotFoundExceptions.
try (ResultSet rs = client.singleUse().executeQuery(Statement.of("SELECT 1"))) {
rs.next();
fail("Missing expected DatabaseNotFoundException");
} catch (DatabaseNotFoundException e) {
// This is what we expect.
}
// Now get a new DatabaseClient for the database. This should now result in a valid
// DatabaseClient.
DatabaseClient newClient = env.getTestHelper().getClient().getDatabaseClient(newDb.getId());
try (ResultSet rs = newClient.singleUse().executeQuery(Statement.of("SELECT 1"))) {
assertThat(rs.next()).isTrue();
assertThat(rs.getLong(0)).isEqualTo(1L);
assertThat(rs.next()).isFalse();
}
}
Aggregations