use of com.google.spanner.admin.database.v1.Backup 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.Backup in project java-spanner by googleapis.
the class MockDatabaseAdminServiceImplTest method createTestBackup.
private Backup createTestBackup() {
CreateBackupRequest request = CreateBackupRequest.newBuilder().setBackupId("test-bck").setBackup(Backup.newBuilder().setDatabase(TEST_DB_NAME).setExpireTime(Timestamp.newBuilder().setSeconds(System.currentTimeMillis() * 1000L + TimeUnit.MILLISECONDS.convert(7, TimeUnit.DAYS))).build()).setParent(TEST_PARENT).build();
OperationFuture<Backup, CreateBackupMetadata> op = client.createBackupOperationCallable().futureCall(request);
try {
return op.get();
} catch (ExecutionException e) {
if (e.getCause() != null && e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
}
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new CancelledException(e, FakeStatusCode.of(Code.CANCELLED), false);
}
}
use of com.google.spanner.admin.database.v1.Backup in project java-spanner by googleapis.
the class MockDatabaseAdminServiceImplTest method listBackups.
@Test
public void listBackups() {
createTestDb();
createTestBackup();
ListBackupsPagedResponse response = client.listBackups(TEST_PARENT);
List<String> backups = new ArrayList<>();
for (Backup bck : response.iterateAll()) {
backups.add(bck.getName());
}
assertThat(backups).containsExactly(TEST_BCK_NAME);
}
use of com.google.spanner.admin.database.v1.Backup in project java-spanner by googleapis.
the class MockDatabaseAdminServiceImplTest method getBackup.
@Test
public void getBackup() {
createTestDb();
createTestBackup();
Backup bck = client.getBackup(TEST_BCK_NAME);
assertThat(bck.getName()).isEqualTo(TEST_BCK_NAME);
}
use of com.google.spanner.admin.database.v1.Backup in project java-spanner by googleapis.
the class DatabaseAdminClientTest method restoreDatabaseTest4.
@Test
public void restoreDatabaseTest4() 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();
Operation resultOperation = Operation.newBuilder().setName("restoreDatabaseTest").setDone(true).setResponse(Any.pack(expectedResponse)).build();
mockDatabaseAdmin.addResponse(resultOperation);
String parent = "parent-995424086";
String databaseId = "databaseId1688905718";
String backup = "backup-1396673086";
Database actualResponse = client.restoreDatabaseAsync(parent, databaseId, backup).get();
Assert.assertEquals(expectedResponse, actualResponse);
List<AbstractMessage> actualRequests = mockDatabaseAdmin.getRequests();
Assert.assertEquals(1, actualRequests.size());
RestoreDatabaseRequest actualRequest = ((RestoreDatabaseRequest) actualRequests.get(0));
Assert.assertEquals(parent, actualRequest.getParent());
Assert.assertEquals(databaseId, actualRequest.getDatabaseId());
Assert.assertEquals(backup, actualRequest.getBackup());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Aggregations