use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class DatabaseAdminClientTest method instanceListDatabaseOperationsWithMetadata.
@Test
public void instanceListDatabaseOperationsWithMetadata() throws Exception {
Instance instance = spanner.getInstanceAdminClient().newInstanceBuilder(InstanceId.of(PROJECT_ID, INSTANCE_ID)).build();
String filter = "(metadata.@type:type.googleapis.com/" + "google.spanner.admin.database.v1.OptimizeRestoredDatabaseMetadata)";
mockDatabaseAdmin.addFilterMatches(filter, restoreDatabaseOperation.getMetadata().get().getOptimizeDatabaseOperationName());
Iterable<Operation> operations = instance.listDatabaseOperations(Options.filter(filter)).iterateAll();
assertThat(operations).hasSize(1);
for (Operation op : operations) {
OptimizeRestoredDatabaseMetadata metadata = op.getMetadata().unpack(OptimizeRestoredDatabaseMetadata.class);
String progress = String.format("Restored database %s is optimized %d%%", metadata.getName(), metadata.getProgress().getProgressPercent());
assertThat(progress.contains("100%"));
}
}
use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class DatabaseAdminGaxTest method updateDatabaseDdlTest.
@Test
public void updateDatabaseDdlTest() throws Exception {
Exception exception = setupException();
com.google.longrunning.Operation resultOperation = com.google.longrunning.Operation.newBuilder().setName("updateDatabaseDdlTest").setDone(true).setResponse(Any.pack(Empty.getDefaultInstance())).build();
if (exceptionAtCall == 0) {
mockDatabaseAdmin.addException(exception);
}
mockDatabaseAdmin.addResponse(resultOperation);
if (exceptionAtCall == 1) {
mockDatabaseAdmin.addException(exception);
}
mockDatabaseAdmin.addResponse(resultOperation);
for (int i = 0; i < 2; i++) {
OperationFuture<Void, UpdateDatabaseDdlMetadata> actualResponse = client.updateDatabaseDdl(INSTANCE, "DATABASE", Collections.singletonList("CREATE TABLE FOO"), "updateDatabaseDdlTest");
try {
actualResponse.get();
} catch (ExecutionException e) {
Throwables.throwIfUnchecked(e.getCause());
throw e;
}
}
List<AbstractMessage> actualRequests = mockDatabaseAdmin.getRequests();
Assert.assertEquals(2, actualRequests.size());
}
use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class MockDatabaseAdminServiceImpl method createDatabase.
@Override
public void createDatabase(CreateDatabaseRequest request, StreamObserver<Operation> responseObserver) {
requests.add(request);
try {
createDatabaseStartupExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
String id = request.getCreateStatement().replace("CREATE DATABASE ", "");
if (id.startsWith("`") && id.endsWith("`")) {
id = id.substring(1, id.length() - 1);
}
String name = String.format("%s/databases/%s", request.getParent(), id);
MockDatabase db = new MockDatabase(name, request.getExtraStatementsList(), null);
if (databases.putIfAbsent(name, db) == null) {
CreateDatabaseMetadata metadata = CreateDatabaseMetadata.newBuilder().setDatabase(name).build();
Database database = Database.newBuilder().setName(name).setState(db.state).build();
Operation operation = Operation.newBuilder().setMetadata(Any.pack(metadata)).setResponse(Any.pack(database)).setDone(false).setName(operations.generateOperationName(name)).build();
operations.addOperation(operation, new CreateDatabaseCallable(operation.getName(), name));
createDatabaseResponseExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
responseObserver.onNext(operation);
responseObserver.onCompleted();
} else {
responseObserver.onError(Status.ALREADY_EXISTS.withDescription(String.format("Database with name %s already exists", name)).asRuntimeException());
}
} catch (Throwable t) {
responseObserver.onError(t);
}
}
use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class MockDatabaseAdminServiceImplTest method listDatabases.
@Test
public void listDatabases() {
createTestDb();
ListDatabasesPagedResponse response = client.listDatabases(TEST_PARENT);
List<String> databases = new ArrayList<>();
for (Database db : response.iterateAll()) {
databases.add(db.getName());
}
assertThat(databases).containsExactly(TEST_DB_NAME);
}
use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class MockDatabaseAdminServiceImplTest method restoreDatabaseAlreadyExists.
@Test
public void restoreDatabaseAlreadyExists() throws InterruptedException, ExecutionException {
createTestDb();
createTestBackup();
RestoreDatabaseRequest request = RestoreDatabaseRequest.newBuilder().setBackup(TEST_BCK_NAME).setDatabaseId("test-db").setParent(TEST_PARENT).build();
OperationFuture<Database, RestoreDatabaseMetadata> op = client.restoreDatabaseOperationCallable().futureCall(request);
exception.expect(ApiExceptionMatcher.forCode(StatusCode.Code.ALREADY_EXISTS));
op.get();
}
Aggregations