use of com.google.cloud.firestore.v1.FirestoreClient.ListCollectionIdsPage in project beam by apache.
the class FirestoreV1FnListCollectionIdsTest method resumeFromLastReadValue.
@Override
public void resumeFromLastReadValue() throws Exception {
when(ff.getFirestoreStub(any())).thenReturn(stub);
when(ff.getRpcQos(any())).thenReturn(rpcQos);
when(rpcQos.newReadAttempt(any())).thenReturn(attempt);
when(attempt.awaitSafeToProceed(any())).thenReturn(true);
// First page of the response
ListCollectionIdsRequest request1 = ListCollectionIdsRequest.newBuilder().setParent(String.format("projects/%s/databases/(default)/document", projectId)).build();
ListCollectionIdsResponse response1 = ListCollectionIdsResponse.newBuilder().addCollectionIds("col_1-1").addCollectionIds("col_1-2").addCollectionIds("col_1-3").setNextPageToken("page2").build();
when(page1.getNextPageToken()).thenReturn(response1.getNextPageToken());
when(page1.getResponse()).thenReturn(response1);
when(page1.hasNextPage()).thenReturn(true);
when(callable.call(request1)).thenReturn(pagedResponse1);
doNothing().when(attempt).checkCanRetry(any(), eq(RETRYABLE_ERROR));
when(pagedResponse1.iteratePages()).thenAnswer(invocation -> new Iterable<ListCollectionIdsPage>() {
@Override
public Iterator<ListCollectionIdsPage> iterator() {
return new AbstractIterator<ListCollectionIdsPage>() {
private boolean first = true;
@Override
protected ListCollectionIdsPage computeNext() {
if (first) {
first = false;
return page1;
} else {
throw RETRYABLE_ERROR;
}
}
};
}
});
// Second page of the response
ListCollectionIdsRequest request2 = ListCollectionIdsRequest.newBuilder().setParent(String.format("projects/%s/databases/(default)/document", projectId)).setPageToken("page2").build();
ListCollectionIdsResponse response2 = ListCollectionIdsResponse.newBuilder().addCollectionIds("col_2-1").build();
when(page2.getResponse()).thenReturn(response2);
when(page2.hasNextPage()).thenReturn(false);
when(callable.call(request2)).thenReturn(pagedResponse2);
when(pagedResponse2.iteratePages()).thenReturn(ImmutableList.of(page2));
when(stub.listCollectionIdsPagedCallable()).thenReturn(callable);
when(ff.getFirestoreStub(any())).thenReturn(stub);
ArgumentCaptor<ListCollectionIdsResponse> responses = ArgumentCaptor.forClass(ListCollectionIdsResponse.class);
doNothing().when(processContext).output(responses.capture());
when(processContext.element()).thenReturn(request1);
ListCollectionIdsFn fn = new ListCollectionIdsFn(clock, ff, rpcQosOptions);
runFunction(fn);
List<ListCollectionIdsResponse> expected = newArrayList(response1, response2);
List<ListCollectionIdsResponse> allValues = responses.getAllValues();
assertEquals(expected, allValues);
}
Aggregations