@Test public void pagedByFixedSizeCollection() { ArgumentCaptor<Integer> requestCapture = ArgumentCaptor.forClass(Integer.class); Mockito.when(callIntList.futureCall(requestCapture.capture(), (ApiCallContext) Mockito.any())) .thenReturn(ApiFutures.immediateFuture(Arrays.asList(0, 1, 2))) .thenReturn(ApiFutures.immediateFuture(Arrays.asList(3, 4))) .thenReturn(ApiFutures.immediateFuture(Arrays.asList(5, 6, 7))) .thenReturn(ApiFutures.immediateFuture(Collections.<Integer>emptyList())); FixedSizeCollection<Integer> fixedSizeCollection = FakeCallableFactory.createPagedCallable( callIntList, PagedCallSettings.newBuilder(new ListIntegersPagedResponseFactory()).build(), clientContext) .call(0) .expandToFixedSizeCollection(5); Truth.assertThat(fixedSizeCollection.getValues()).containsExactly(0, 1, 2, 3, 4).inOrder(); Truth.assertThat(fixedSizeCollection.getNextCollection().getValues()) .containsExactly(5, 6, 7) .inOrder(); Truth.assertThat(requestCapture.getAllValues()).containsExactly(0, 2, 4, 7).inOrder(); }
@Test public void pagedByPage() { ArgumentCaptor<Integer> requestCapture = ArgumentCaptor.forClass(Integer.class); Mockito.when(callIntList.futureCall(requestCapture.capture(), (ApiCallContext) Mockito.any())) .thenReturn(ApiFutures.immediateFuture(Arrays.asList(0, 1, 2))) .thenReturn(ApiFutures.immediateFuture(Arrays.asList(3, 4))) .thenReturn(ApiFutures.immediateFuture(Collections.<Integer>emptyList())); Page<Integer> page = FakeCallableFactory.createPagedCallable( callIntList, PagedCallSettings.newBuilder(new ListIntegersPagedResponseFactory()).build(), clientContext) .call(0) .getPage(); Truth.assertThat(page.getValues()).containsExactly(0, 1, 2).inOrder(); Truth.assertThat(page.hasNextPage()).isTrue(); page = page.getNextPage(); Truth.assertThat(page.getValues()).containsExactly(3, 4).inOrder(); Truth.assertThat(page.hasNextPage()).isTrue(); page = page.getNextPage(); Truth.assertThat(page.getValues()).isEmpty(); Truth.assertThat(page.hasNextPage()).isFalse(); Truth.assertThat(page.getNextPage()).isNull(); Truth.assertThat(requestCapture.getAllValues()).containsExactly(0, 2, 4).inOrder(); }
@Test public void paged() { ArgumentCaptor<Integer> requestCapture = ArgumentCaptor.forClass(Integer.class); Mockito.when(callIntList.futureCall(requestCapture.capture(), (ApiCallContext) Mockito.any())) .thenReturn(ApiFutures.immediateFuture(Arrays.asList(0, 1, 2))) .thenReturn(ApiFutures.immediateFuture(Arrays.asList(3, 4))) .thenReturn(ApiFutures.immediateFuture(Collections.<Integer>emptyList())); UnaryCallable<Integer, ListIntegersPagedResponse> callable = FakeCallableFactory.createPagedCallable( callIntList, PagedCallSettings.newBuilder(new ListIntegersPagedResponseFactory()).build(), clientContext); Truth.assertThat(ImmutableList.copyOf(callable.call(0).iterateAll())) .containsExactly(0, 1, 2, 3, 4) .inOrder(); Truth.assertThat(requestCapture.getAllValues()).containsExactly(0, 2, 4).inOrder(); }
public static ApiFuture<ListIntegersPagedResponse> create( PageContext<Integer, List<Integer>, Integer> context, ApiFuture<List<Integer>> futureResponse) { ApiFuture<ListIntegersPage> futurePage = new ListIntegersPage(null, null).createPageAsync(context, futureResponse); return ApiFutures.transform( futurePage, new ApiFunction<ListIntegersPage, ListIntegersPagedResponse>() { @Override public ListIntegersPagedResponse apply(ListIntegersPage input) { return new ListIntegersPagedResponse(input); } }, directExecutor()); } }
@Test(expected = ValidationException.class) public void pagedFixedSizeCollectionTooSmallCollectionSize() { Mockito.when(callIntList.futureCall((Integer) Mockito.any(), (ApiCallContext) Mockito.any())) .thenReturn(ApiFutures.immediateFuture(Arrays.asList(0, 1))) .thenReturn(ApiFutures.immediateFuture(Collections.<Integer>emptyList())); FakeCallableFactory.createPagedCallable( callIntList, PagedCallSettings.newBuilder(new ListIntegersPagedResponseFactory()).build(), clientContext) .call(0) .expandToFixedSizeCollection(2); } }
@Test(expected = ValidationException.class) public void pagedFixedSizeCollectionTooManyElements() { Mockito.when(callIntList.futureCall((Integer) Mockito.any(), (ApiCallContext) Mockito.any())) .thenReturn(ApiFutures.immediateFuture(Arrays.asList(0, 1, 2))) .thenReturn(ApiFutures.immediateFuture(Arrays.asList(3, 4))) .thenReturn(ApiFutures.immediateFuture(Collections.<Integer>emptyList())); FakeCallableFactory.createPagedCallable( callIntList, PagedCallSettings.newBuilder(new ListIntegersPagedResponseFactory()).build(), clientContext) .call(0) .expandToFixedSizeCollection(4); }
@Test public void futureCall() { List<List<Integer>> results = Arrays.asList( Arrays.asList(0, 1, 2), Arrays.asList(3, 4), Collections.<Integer>emptyList()); PagedStashCallable callable = new PagedStashCallable(results); PagedCallable<Integer, List<Integer>, ListIntegersPagedResponse> pagedCallable = new PagedCallable<>(callable, new ListIntegersPagedResponseFactory()); Truth.assertThat( ImmutableList.copyOf( pagedCallable.call(0, FakeCallContext.createDefault()).iterateAll())) .containsExactly(0, 1, 2, 3, 4) .inOrder(); }
@Override public ListIntegersPagedResponse apply(ListIntegersPage input) { return new ListIntegersPagedResponse(input); } },
public static ListIntegersPagedResponse create( PageContext<Integer, List<Integer>, Integer> context, List<Integer> response) { ListIntegersPage page = new ListIntegersPage(context, response); return new ListIntegersPagedResponse(page); }
@Override public ApiFuture<ListIntegersPagedResponse> getFuturePagedResponse( UnaryCallable<Integer, List<Integer>> callable, Integer request, ApiCallContext context, ApiFuture<List<Integer>> futureResponse) { PageContext<Integer, List<Integer>, Integer> pageContext = PageContext.create(callable, streamingDescriptor, request, context); return ListIntegersPagedResponse.create(pageContext, futureResponse); } }