|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 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 | + * https://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.showcase.v1beta1.it; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import com.google.showcase.v1beta1.EchoClient; |
| 22 | +import com.google.showcase.v1beta1.EchoResponse; |
| 23 | +import com.google.showcase.v1beta1.PagedExpandRequest; |
| 24 | +import com.google.showcase.v1beta1.it.util.TestClientInitializer; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | +import org.junit.AfterClass; |
| 27 | +import org.junit.BeforeClass; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +public class ITPagination { |
| 31 | + |
| 32 | + private static EchoClient grpcClient; |
| 33 | + private static EchoClient httpjsonClient; |
| 34 | + |
| 35 | + @BeforeClass |
| 36 | + public static void createClients() throws Exception { |
| 37 | + // Create gRPC Echo Client |
| 38 | + grpcClient = TestClientInitializer.createGrpcEchoClient(); |
| 39 | + // Create Http JSON Echo Client |
| 40 | + httpjsonClient = TestClientInitializer.createHttpJsonEchoClient(); |
| 41 | + } |
| 42 | + |
| 43 | + @AfterClass |
| 44 | + public static void destroyClients() throws InterruptedException { |
| 45 | + grpcClient.close(); |
| 46 | + httpjsonClient.close(); |
| 47 | + |
| 48 | + grpcClient.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); |
| 49 | + httpjsonClient.awaitTermination( |
| 50 | + TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS); |
| 51 | + } |
| 52 | + |
| 53 | + // This tests that pagination returns the correct number of pages + responses and that |
| 54 | + // the content is correct. |
| 55 | + // |
| 56 | + // The pageToken is where the streaming responses come back from and the page size denotes |
| 57 | + // how many of the responses come back together (in a page). i.e for PageSize = 2 and |
| 58 | + // PageToken = 3, see below: |
| 59 | + // | A | Series | Of | Words | That | Will | Be | Sent | Back | One | By | One |
| 60 | + // Page # | - | - | - | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 | 5 |
| 61 | + // Token # | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 62 | + @Test |
| 63 | + public void testPagedExpandWithTokenGrpc() { |
| 64 | + int pageSize = 2; |
| 65 | + int pageToken = 3; |
| 66 | + String content = "A series of words that will be sent back one by one"; |
| 67 | + int contentLength = content.split(" ").length; |
| 68 | + |
| 69 | + EchoClient.PagedExpandPagedResponse pagedExpandPagedResponse = |
| 70 | + grpcClient.pagedExpand( |
| 71 | + PagedExpandRequest.newBuilder() |
| 72 | + .setContent(content) |
| 73 | + .setPageSize(pageSize) |
| 74 | + .setPageToken(String.valueOf(pageToken)) |
| 75 | + .build()); |
| 76 | + |
| 77 | + String[] expected = content.split(" "); |
| 78 | + int numExpectedPages = ((contentLength - pageToken) / pageSize); |
| 79 | + // If the responses can't be evenly split into pages, then the extra responses |
| 80 | + // will go to an additional page |
| 81 | + if ((contentLength - pageToken) % pageSize != 0) { |
| 82 | + numExpectedPages++; |
| 83 | + } |
| 84 | + int numExpectedResponses = contentLength - pageToken; |
| 85 | + |
| 86 | + validatePagedResponses( |
| 87 | + pagedExpandPagedResponse, expected, pageToken, numExpectedResponses, numExpectedPages); |
| 88 | + } |
| 89 | + |
| 90 | + // This tests that pagination returns the correct number of pages + responses and that |
| 91 | + // the content is correct. |
| 92 | + // |
| 93 | + // The pageToken is where the streaming responses come back from and the page size denotes |
| 94 | + // how many of the responses come back together (in a page). i.e for PageSize = 2 and |
| 95 | + // PageToken = 3, see below: |
| 96 | + // | A | Series | Of | Words | That | Will | Be | Sent | Back | One | By | One |
| 97 | + // Page # | - | - | - | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 | 5 |
| 98 | + // Token # | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 99 | + @Test |
| 100 | + public void testPagedExpandWithTokenHttpJson() { |
| 101 | + int pageSize = 2; |
| 102 | + int pageToken = 3; |
| 103 | + String content = "A series of words that will be sent back one by one"; |
| 104 | + int contentLength = content.split(" ").length; |
| 105 | + |
| 106 | + EchoClient.PagedExpandPagedResponse pagedExpandPagedResponse = |
| 107 | + httpjsonClient.pagedExpand( |
| 108 | + PagedExpandRequest.newBuilder() |
| 109 | + .setContent(content) |
| 110 | + .setPageSize(pageSize) |
| 111 | + .setPageToken(String.valueOf(pageToken)) |
| 112 | + .build()); |
| 113 | + |
| 114 | + String[] expected = content.split(" "); |
| 115 | + int numExpectedPages = ((contentLength - pageToken) / pageSize); |
| 116 | + // If the responses can't be evenly split into pages, then the extra responses |
| 117 | + // will go to an additional page |
| 118 | + if ((contentLength - pageToken) % pageSize != 0) { |
| 119 | + numExpectedPages++; |
| 120 | + } |
| 121 | + int numExpectedResponses = contentLength - pageToken; |
| 122 | + |
| 123 | + validatePagedResponses( |
| 124 | + pagedExpandPagedResponse, expected, pageToken, numExpectedResponses, numExpectedPages); |
| 125 | + } |
| 126 | + |
| 127 | + private void validatePagedResponses( |
| 128 | + EchoClient.PagedExpandPagedResponse pagedExpandPagedResponse, |
| 129 | + String[] expected, |
| 130 | + int pageToken, |
| 131 | + int numExpectedResponses, |
| 132 | + int numExpectedPages) { |
| 133 | + int numResponses = 0; |
| 134 | + int numPages = 0; |
| 135 | + for (EchoClient.PagedExpandPage page : pagedExpandPagedResponse.iteratePages()) { |
| 136 | + for (EchoResponse echoResponse : page.getValues()) { |
| 137 | + // Add pageToken as offset to the expected array to start indexing at the pageToken |
| 138 | + assertThat(echoResponse.getContent()).isEqualTo(expected[numResponses + pageToken]); |
| 139 | + numResponses++; |
| 140 | + } |
| 141 | + numPages++; |
| 142 | + } |
| 143 | + |
| 144 | + assertThat(numPages).isEqualTo(numExpectedPages); |
| 145 | + assertThat(numResponses).isEqualTo(numExpectedResponses); |
| 146 | + } |
| 147 | +} |
0 commit comments