|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | + |
| 21 | +import com.google.common.collect.ImmutableList; |
| 22 | +import com.google.common.util.concurrent.Futures; |
| 23 | + |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import java.util.concurrent.ExecutionException; |
| 27 | +import java.util.concurrent.Future; |
| 28 | + |
| 29 | +public class AsyncPageImplTest { |
| 30 | + |
| 31 | + private static final ImmutableList<String> VALUES1 = ImmutableList.of("1", "2"); |
| 32 | + private static final ImmutableList<String> VALUES2 = ImmutableList.of("3", "4"); |
| 33 | + private static final ImmutableList<String> VALUES3 = ImmutableList.of("5", "6"); |
| 34 | + private static final ImmutableList<String> ALL_VALUES = ImmutableList.<String>builder() |
| 35 | + .addAll(VALUES1) |
| 36 | + .addAll(VALUES2) |
| 37 | + .addAll(VALUES3) |
| 38 | + .build(); |
| 39 | + private static final ImmutableList<String> SOME_VALUES = ImmutableList.<String>builder() |
| 40 | + .addAll(VALUES2) |
| 41 | + .addAll(VALUES3) |
| 42 | + .build(); |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testPage() { |
| 46 | + final AsyncPageImpl<String> nextResult = new AsyncPageImpl<>(null, "c", VALUES2); |
| 47 | + AsyncPageImpl.NextPageFetcher<String> fetcher = new AsyncPageImpl.NextPageFetcher<String>() { |
| 48 | + private static final long serialVersionUID = 4703765400378593176L; |
| 49 | + |
| 50 | + @Override |
| 51 | + public Future<AsyncPage<String>> nextPage() { |
| 52 | + return Futures.<AsyncPage<String>>immediateFuture(nextResult); |
| 53 | + } |
| 54 | + }; |
| 55 | + AsyncPageImpl<String> result = new AsyncPageImpl<>(fetcher, "c", VALUES1); |
| 56 | + assertEquals(nextResult, result.nextPage()); |
| 57 | + assertEquals("c", result.nextPageCursor()); |
| 58 | + assertEquals(VALUES1, result.values()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testPageAsync() throws ExecutionException, InterruptedException { |
| 63 | + final AsyncPageImpl<String> nextResult = new AsyncPageImpl<>(null, "c", VALUES2); |
| 64 | + AsyncPageImpl.NextPageFetcher<String> fetcher = new AsyncPageImpl.NextPageFetcher<String>() { |
| 65 | + private static final long serialVersionUID = 4703765400378593176L; |
| 66 | + |
| 67 | + @Override |
| 68 | + public Future<AsyncPage<String>> nextPage() { |
| 69 | + return Futures.<AsyncPage<String>>immediateFuture(nextResult); |
| 70 | + } |
| 71 | + }; |
| 72 | + AsyncPageImpl<String> result = new AsyncPageImpl<>(fetcher, "c", VALUES1); |
| 73 | + assertEquals(nextResult, result.nextPageAsync().get()); |
| 74 | + assertEquals("c", result.nextPageCursor()); |
| 75 | + assertEquals(VALUES1, result.values()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testIterateAll() { |
| 80 | + final AsyncPageImpl<String> nextResult2 = new AsyncPageImpl<>(null, "c3", VALUES3); |
| 81 | + AsyncPageImpl.NextPageFetcher<String> fetcher2 = new AsyncPageImpl.NextPageFetcher<String>() { |
| 82 | + private static final long serialVersionUID = -9203621430631884026L; |
| 83 | + |
| 84 | + @Override |
| 85 | + public Future<AsyncPage<String>> nextPage() { |
| 86 | + return Futures.<AsyncPage<String>>immediateFuture(nextResult2); |
| 87 | + } |
| 88 | + }; |
| 89 | + final AsyncPageImpl<String> nextResult1 = new AsyncPageImpl<>(fetcher2, "c2", VALUES2); |
| 90 | + AsyncPageImpl.NextPageFetcher<String> fetcher1 = new AsyncPageImpl.NextPageFetcher<String>() { |
| 91 | + private static final long serialVersionUID = -9203621430631884026L; |
| 92 | + |
| 93 | + @Override |
| 94 | + public Future<AsyncPage<String>> nextPage() { |
| 95 | + return Futures.<AsyncPage<String>>immediateFuture(nextResult1); |
| 96 | + } |
| 97 | + }; |
| 98 | + AsyncPageImpl<String> result = new AsyncPageImpl<>(fetcher1, "c1", VALUES1); |
| 99 | + assertEquals(ALL_VALUES, ImmutableList.copyOf(result.iterateAll())); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testAsyncPageAndIterateAll() throws ExecutionException, InterruptedException { |
| 104 | + final AsyncPageImpl<String> nextResult2 = new AsyncPageImpl<>(null, "c3", VALUES3); |
| 105 | + AsyncPageImpl.NextPageFetcher<String> fetcher2 = new AsyncPageImpl.NextPageFetcher<String>() { |
| 106 | + private static final long serialVersionUID = -9203621430631884026L; |
| 107 | + |
| 108 | + @Override |
| 109 | + public Future<AsyncPage<String>> nextPage() { |
| 110 | + return Futures.<AsyncPage<String>>immediateFuture(nextResult2); |
| 111 | + } |
| 112 | + }; |
| 113 | + final AsyncPageImpl<String> nextResult1 = new AsyncPageImpl<>(fetcher2, "c2", VALUES2); |
| 114 | + AsyncPageImpl.NextPageFetcher<String> fetcher1 = new AsyncPageImpl.NextPageFetcher<String>() { |
| 115 | + private static final long serialVersionUID = -9203621430631884026L; |
| 116 | + |
| 117 | + @Override |
| 118 | + public Future<AsyncPage<String>> nextPage() { |
| 119 | + return Futures.<AsyncPage<String>>immediateFuture(nextResult1); |
| 120 | + } |
| 121 | + }; |
| 122 | + AsyncPageImpl<String> result = new AsyncPageImpl<>(fetcher1, "c1", VALUES1); |
| 123 | + assertEquals(nextResult1, result.nextPageAsync().get()); |
| 124 | + assertEquals("c1", result.nextPageCursor()); |
| 125 | + assertEquals(VALUES1, result.values()); |
| 126 | + assertEquals(SOME_VALUES, ImmutableList.copyOf(result.nextPageAsync().get().iterateAll())); |
| 127 | + } |
| 128 | +} |
0 commit comments