@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(); }
@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 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 nonPaged() { 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, List<Integer>> callable = FakeCallableFactory.createUnpagedCallable( callIntList, PagedCallSettings.newBuilder(new ListIntegersPagedResponseFactory()).build(), clientContext); Truth.assertThat(ImmutableList.copyOf(callable.call(0))).containsExactly(0, 1, 2).inOrder(); Truth.assertThat(ImmutableList.copyOf(callable.call(2))).containsExactly(3, 4).inOrder(); Truth.assertThat(ImmutableList.copyOf(callable.call(4))).isEmpty(); Truth.assertThat(requestCapture.getAllValues()).containsExactly(0, 2, 4).inOrder(); }
@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(); }
@Test public void testToString() { PagedStashCallable stash = new PagedStashCallable(null); PagedCallable<Integer, List<Integer>, ListIntegersPagedResponse> pagedCallable = new PagedCallable<>(stash, new ListIntegersPagedResponseFactory()); Truth.assertThat(pagedCallable.toString()).contains("paged"); } }