Search in sources :

Example 41 with Backup

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);
    }
}
Also used : CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) Operation(com.google.longrunning.Operation)

Example 42 with Backup

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);
    }
}
Also used : CreateBackupRequest(com.google.spanner.admin.database.v1.CreateBackupRequest) CancelledException(com.google.api.gax.rpc.CancelledException) Backup(com.google.spanner.admin.database.v1.Backup) CreateBackupMetadata(com.google.spanner.admin.database.v1.CreateBackupMetadata) ExecutionException(java.util.concurrent.ExecutionException)

Example 43 with Backup

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);
}
Also used : Backup(com.google.spanner.admin.database.v1.Backup) ArrayList(java.util.ArrayList) ListBackupsPagedResponse(com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient.ListBackupsPagedResponse) Test(org.junit.Test)

Example 44 with Backup

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);
}
Also used : Backup(com.google.spanner.admin.database.v1.Backup) Test(org.junit.Test)

Example 45 with Backup

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()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) RestoreDatabaseRequest(com.google.spanner.admin.database.v1.RestoreDatabaseRequest) Database(com.google.spanner.admin.database.v1.Database) ArrayList(java.util.ArrayList) Operation(com.google.longrunning.Operation) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)43 Backup (com.google.spanner.admin.database.v1.Backup)21 AbstractMessage (com.google.protobuf.AbstractMessage)19 ByteString (com.google.protobuf.ByteString)19 CreateBackupMetadata (com.google.spanner.admin.database.v1.CreateBackupMetadata)18 ExecutionException (java.util.concurrent.ExecutionException)17 Timestamp (com.google.cloud.Timestamp)16 Operation (com.google.longrunning.Operation)14 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)11 StatusRuntimeException (io.grpc.StatusRuntimeException)11 ArrayList (java.util.ArrayList)11 Backup (com.google.bigtable.admin.v2.Backup)10 Backup (com.google.cloud.spanner.Backup)8 BackupName (com.google.spanner.admin.database.v1.BackupName)8 InstanceName (com.google.spanner.admin.database.v1.InstanceName)7 Database (com.google.cloud.spanner.Database)5 FieldMask (com.google.protobuf.FieldMask)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)4 CreateBackupRequest (com.google.spanner.admin.database.v1.CreateBackupRequest)4 Database (com.google.spanner.admin.database.v1.Database)4