use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class DatabaseAdminGaxTest method getDatabaseTest.
@Test
public void getDatabaseTest() {
Exception exception = setupException();
DatabaseName name2 = DatabaseName.of(PROJECT, INSTANCE, "DATABASE");
com.google.spanner.admin.database.v1.Database expectedResponse = com.google.spanner.admin.database.v1.Database.newBuilder().setName(name2.toString()).build();
if (exceptionAtCall == 0) {
mockDatabaseAdmin.addException(exception);
}
mockDatabaseAdmin.addResponse(expectedResponse);
if (exceptionAtCall == 1) {
mockDatabaseAdmin.addException(exception);
}
mockDatabaseAdmin.addResponse(expectedResponse);
for (int i = 0; i < 2; i++) {
Database actualResponse = client.getDatabase(INSTANCE, "DATABASE");
Assert.assertEquals(name2.toString(), actualResponse.getId().getName());
}
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 RemoteSpannerHelper method createTestDatabase.
/**
* Creates a test database defined by {@code statements} in the test instance. A {@code CREATE
* DATABASE ...} statement should not be included; an appropriate name will be chosen and the
* statement generated accordingly.
*/
public Database createTestDatabase(Dialect dialect, Iterable<String> statements) throws SpannerException {
String dbId = getUniqueDatabaseId();
Database databaseToCreate = client.getDatabaseAdminClient().newDatabaseBuilder(DatabaseId.of(instanceId.getProject(), instanceId.getInstance(), dbId)).setDialect(dialect).build();
try {
Iterable<String> ddlStatements = dialect == Dialect.POSTGRESQL ? Collections.emptyList() : statements;
OperationFuture<Database, CreateDatabaseMetadata> op = client.getDatabaseAdminClient().createDatabase(databaseToCreate, ddlStatements);
Database db = op.get();
if (dialect == Dialect.POSTGRESQL && Iterables.size(statements) > 0) {
client.getDatabaseAdminClient().updateDatabaseDdl(instanceId.getInstance(), dbId, statements, null).get();
}
logger.log(Level.FINE, "Created test database {0}", db.getId());
dbs.add(db);
return db;
} catch (Exception e) {
throw SpannerExceptionFactory.newSpannerException(e);
}
}
use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class MockDatabaseAdminServiceImpl method matchesFilter.
private boolean matchesFilter(Object obj, String filter) throws Exception {
if (!Strings.isNullOrEmpty(filter)) {
Set<String> matches = filterMatches.get(filter);
if (matches != null) {
String name = (String) obj.getClass().getMethod("getName").invoke(obj);
return matches.contains(name);
}
if (obj instanceof Operation) {
Operation operation = (Operation) obj;
Pattern pattern = Pattern.compile("(?:\\(metadata.@type:type.googleapis.com/(.*)\\)) AND (?:\\(metadata.(?:name|database):(.*)\\)|\\(name:(.*)/operations/\\))");
Matcher matcher = pattern.matcher(filter);
if (matcher.matches()) {
String type = matcher.group(1);
String objectName = matcher.group(2);
if (objectName == null) {
objectName = matcher.group(3);
}
Any anyMetadata = operation.getMetadata();
if (anyMetadata.getTypeUrl().endsWith(type)) {
if (type.equals(CreateBackupMetadata.getDescriptor().getFullName())) {
CreateBackupMetadata metadata = operation.getMetadata().unpack(CreateBackupMetadata.class);
return metadata.getName().equals(objectName);
} else if (type.equals(CreateDatabaseMetadata.getDescriptor().getFullName())) {
CreateDatabaseMetadata metadata = operation.getMetadata().unpack(CreateDatabaseMetadata.class);
return metadata.getDatabase().equals(objectName);
} else if (type.equals(RestoreDatabaseMetadata.getDescriptor().getFullName())) {
RestoreDatabaseMetadata metadata = operation.getMetadata().unpack(RestoreDatabaseMetadata.class);
return metadata.getName().equals(objectName);
}
}
}
}
return false;
}
return true;
}
use of com.google.spanner.admin.database.v1.Database in project java-spanner by googleapis.
the class MockDatabaseAdminServiceImpl method createBackup.
@Override
public void createBackup(CreateBackupRequest request, StreamObserver<Operation> responseObserver) {
requests.add(request);
try {
createBackupStartupExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
String name = String.format("%s/backups/%s", request.getParent(), request.getBackupId());
MockDatabase db = databases.get(request.getBackup().getDatabase());
if (db == null) {
responseObserver.onError(Status.NOT_FOUND.withDescription(String.format("Database with name %s not found", request.getBackup().getDatabase())).asRuntimeException());
return;
}
MockBackup bck = new MockBackup(name, request.getBackup(), db);
if (backups.putIfAbsent(name, bck) == null) {
CreateBackupMetadata metadata = CreateBackupMetadata.newBuilder().setName(name).setDatabase(bck.database).setProgress(OperationProgress.newBuilder().setStartTime(Timestamp.newBuilder().setSeconds(System.currentTimeMillis() / 1000L).build()).setProgressPercent(0)).build();
Operation operation = Operation.newBuilder().setMetadata(Any.pack(metadata)).setResponse(Any.pack(bck.toProto())).setName(operations.generateOperationName(name)).build();
operations.addOperation(operation, new CreateBackupCallable(operation.getName(), name));
createBackupResponseExecutionTime.simulateExecutionTime(exceptions, false, freezeLock);
responseObserver.onNext(operation);
responseObserver.onCompleted();
} else {
responseObserver.onError(Status.ALREADY_EXISTS.withDescription(String.format("Backup 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 restoreDatabase.
@Test
public void restoreDatabase() throws InterruptedException, ExecutionException {
createTestDb();
createTestBackup();
RestoreDatabaseRequest request = RestoreDatabaseRequest.newBuilder().setBackup(TEST_BCK_NAME).setDatabaseId("restored-db").setParent(TEST_PARENT).build();
OperationFuture<Database, RestoreDatabaseMetadata> op = client.restoreDatabaseOperationCallable().futureCall(request);
Database restoredDb = op.get();
assertThat(restoredDb.getName()).isEqualTo(String.format("%s/databases/%s", TEST_PARENT, "restored-db"));
assertThat(restoredDb.getRestoreInfo().getBackupInfo().getBackup()).isEqualTo(TEST_BCK_NAME);
assertThat(restoredDb.getRestoreInfo().getBackupInfo().getSourceDatabase()).isEqualTo(TEST_DB_NAME);
}
Aggregations