use of com.google.spanner.admin.database.v1.ListBackupsRequest in project java-spanner by googleapis.
the class DatabaseAdminClientTest method listBackupsTest2.
@Test
public void listBackupsTest2() throws Exception {
Backup responsesElement = Backup.newBuilder().build();
ListBackupsResponse expectedResponse = ListBackupsResponse.newBuilder().setNextPageToken("").addAllBackups(Arrays.asList(responsesElement)).build();
mockDatabaseAdmin.addResponse(expectedResponse);
String parent = "parent-995424086";
ListBackupsPagedResponse pagedListResponse = client.listBackups(parent);
List<Backup> resources = Lists.newArrayList(pagedListResponse.iterateAll());
Assert.assertEquals(1, resources.size());
Assert.assertEquals(expectedResponse.getBackupsList().get(0), resources.get(0));
List<AbstractMessage> actualRequests = mockDatabaseAdmin.getRequests();
Assert.assertEquals(1, actualRequests.size());
ListBackupsRequest actualRequest = ((ListBackupsRequest) actualRequests.get(0));
Assert.assertEquals(parent, actualRequest.getParent());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.spanner.admin.database.v1.ListBackupsRequest in project java-bigtable by googleapis.
the class BigtableTableAdminClientTest method testListBackups.
@Test
public void testListBackups() {
// Setup
Mockito.when(mockStub.listBackupsPagedCallable()).thenReturn(mockListBackupCallable);
com.google.bigtable.admin.v2.ListBackupsRequest testRequest = com.google.bigtable.admin.v2.ListBackupsRequest.newBuilder().setParent(NameUtil.formatClusterName(PROJECT_ID, INSTANCE_ID, CLUSTER_ID)).build();
// 3 Backups spread across 2 pages
List<com.google.bigtable.admin.v2.Backup> expectedProtos = Lists.newArrayList();
for (int i = 0; i < 3; i++) {
expectedProtos.add(com.google.bigtable.admin.v2.Backup.newBuilder().setName(NameUtil.formatBackupName(PROJECT_ID, INSTANCE_ID, CLUSTER_ID, BACKUP_ID + i)).build());
}
// 2 on the first page
ListBackupsPage page0 = Mockito.mock(ListBackupsPage.class);
Mockito.when(page0.getValues()).thenReturn(expectedProtos.subList(0, 2));
Mockito.when(page0.hasNextPage()).thenReturn(true);
// 1 on the last page
ListBackupsPage page1 = Mockito.mock(ListBackupsPage.class);
Mockito.when(page1.getValues()).thenReturn(expectedProtos.subList(2, 3));
// Link page0 to page1
Mockito.when(page0.getNextPageAsync()).thenReturn(ApiFutures.immediateFuture(page1));
// Link page to the response
ListBackupsPagedResponse response0 = Mockito.mock(ListBackupsPagedResponse.class);
Mockito.when(response0.getPage()).thenReturn(page0);
Mockito.when(mockListBackupCallable.futureCall(testRequest)).thenReturn(ApiFutures.immediateFuture(response0));
// Execute
List<String> actualResults = adminClient.listBackups(CLUSTER_ID);
// Verify
List<String> expectedResults = Lists.newArrayList();
for (com.google.bigtable.admin.v2.Backup expectedProto : expectedProtos) {
expectedResults.add(NameUtil.extractBackupIdFromBackupName(expectedProto.getName()));
}
assertThat(actualResults).containsExactlyElementsIn(expectedResults);
}
use of com.google.spanner.admin.database.v1.ListBackupsRequest in project java-bigtable by googleapis.
the class BigtableTableAdminClient method listBackupsAsync.
/**
* Lists backups in the specified cluster asynchronously.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<List<String>> listFuture = client.listBackupsAsync(clusterId);
*
* ApiFutures.addCallback(
* listFuture,
* new ApiFutureCallback<List<String>>() {
* public void onSuccess(List<String> backupIds) {
* System.out.println("Got list of backups:");
* for (String backupId : backupIds) {
* System.out.println(backupId);
* }
* }
*
* public void onFailure(Throwable t) {
* t.printStackTrace();
* }
* },
* MoreExecutors.directExecutor()
* );
* }</pre>
*/
public ApiFuture<List<String>> listBackupsAsync(String clusterId) {
ListBackupsRequest request = ListBackupsRequest.newBuilder().setParent(NameUtil.formatClusterName(projectId, instanceId, clusterId)).build();
// TODO(igorbernstein2): try to upstream pagination spooling or figure out a way to expose the
// paginated responses while maintaining the wrapper facade.
// Fetches the first page.
ApiFuture<ListBackupsPage> firstPageFuture = ApiFutures.transform(stub.listBackupsPagedCallable().futureCall(request), new ApiFunction<ListBackupsPagedResponse, ListBackupsPage>() {
@Override
public ListBackupsPage apply(ListBackupsPagedResponse response) {
return response.getPage();
}
}, MoreExecutors.directExecutor());
// Fetches the rest of the pages by chaining the futures.
ApiFuture<List<com.google.bigtable.admin.v2.Backup>> allProtos = ApiFutures.transformAsync(firstPageFuture, new ApiAsyncFunction<ListBackupsPage, List<com.google.bigtable.admin.v2.Backup>>() {
List<com.google.bigtable.admin.v2.Backup> responseAccumulator = Lists.newArrayList();
@Override
public ApiFuture<List<com.google.bigtable.admin.v2.Backup>> apply(ListBackupsPage page) {
// Add all entries from the page
responseAccumulator.addAll(Lists.newArrayList(page.getValues()));
// If this is the last page, just return the accumulated responses.
if (!page.hasNextPage()) {
return ApiFutures.immediateFuture(responseAccumulator);
}
// Otherwise fetch the next page.
return ApiFutures.transformAsync(page.getNextPageAsync(), this, MoreExecutors.directExecutor());
}
}, MoreExecutors.directExecutor());
// Wraps all of the accumulated protos.
return ApiFutures.transform(allProtos, new ApiFunction<List<com.google.bigtable.admin.v2.Backup>, List<String>>() {
@Override
public List<String> apply(List<com.google.bigtable.admin.v2.Backup> protos) {
List<String> results = Lists.newArrayListWithCapacity(protos.size());
for (com.google.bigtable.admin.v2.Backup proto : protos) {
results.add(NameUtil.extractBackupIdFromBackupName(proto.getName()));
}
return results;
}
}, MoreExecutors.directExecutor());
}
use of com.google.spanner.admin.database.v1.ListBackupsRequest in project java-bigtable by googleapis.
the class BigtableTableAdminClientTests method testListBackups.
@Test
public void testListBackups() {
// Setup
Mockito.when(mockStub.listBackupsPagedCallable()).thenReturn(mockListBackupCallable);
com.google.bigtable.admin.v2.ListBackupsRequest testRequest = com.google.bigtable.admin.v2.ListBackupsRequest.newBuilder().setParent(NameUtil.formatClusterName(PROJECT_ID, INSTANCE_ID, CLUSTER_ID)).build();
// 3 Backups spread across 2 pages
List<com.google.bigtable.admin.v2.Backup> expectedProtos = Lists.newArrayList();
for (int i = 0; i < 3; i++) {
expectedProtos.add(com.google.bigtable.admin.v2.Backup.newBuilder().setName(NameUtil.formatBackupName(PROJECT_ID, INSTANCE_ID, CLUSTER_ID, BACKUP_ID + i)).build());
}
// 2 on the first page
ListBackupsPage page0 = Mockito.mock(ListBackupsPage.class);
Mockito.when(page0.getValues()).thenReturn(expectedProtos.subList(0, 2));
Mockito.when(page0.hasNextPage()).thenReturn(true);
// 1 on the last page
ListBackupsPage page1 = Mockito.mock(ListBackupsPage.class);
Mockito.when(page1.getValues()).thenReturn(expectedProtos.subList(2, 3));
// Link page0 to page1
Mockito.when(page0.getNextPageAsync()).thenReturn(ApiFutures.immediateFuture(page1));
// Link page to the response
ListBackupsPagedResponse response0 = Mockito.mock(ListBackupsPagedResponse.class);
Mockito.when(response0.getPage()).thenReturn(page0);
Mockito.when(mockListBackupCallable.futureCall(testRequest)).thenReturn(ApiFutures.immediateFuture(response0));
// Execute
List<String> actualResults = adminClient.listBackups(CLUSTER_ID);
// Verify
List<String> expectedResults = Lists.newArrayList();
for (com.google.bigtable.admin.v2.Backup expectedProto : expectedProtos) {
expectedResults.add(NameUtil.extractBackupIdFromBackupName(expectedProto.getName()));
}
assertThat(actualResults).containsExactlyElementsIn(expectedResults);
}
use of com.google.spanner.admin.database.v1.ListBackupsRequest in project java-bigtable by googleapis.
the class BaseBigtableTableAdminClientTest method listBackupsTest2.
@Test
public void listBackupsTest2() throws Exception {
Backup responsesElement = Backup.newBuilder().build();
ListBackupsResponse expectedResponse = ListBackupsResponse.newBuilder().setNextPageToken("").addAllBackups(Arrays.asList(responsesElement)).build();
mockBigtableTableAdmin.addResponse(expectedResponse);
String parent = "parent-995424086";
ListBackupsPagedResponse pagedListResponse = client.listBackups(parent);
List<Backup> resources = Lists.newArrayList(pagedListResponse.iterateAll());
Assert.assertEquals(1, resources.size());
Assert.assertEquals(expectedResponse.getBackupsList().get(0), resources.get(0));
List<AbstractMessage> actualRequests = mockBigtableTableAdmin.getRequests();
Assert.assertEquals(1, actualRequests.size());
ListBackupsRequest actualRequest = ((ListBackupsRequest) actualRequests.get(0));
Assert.assertEquals(parent, actualRequest.getParent());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Aggregations