Skip to content

Commit 5cf1688

Browse files
committed
test(showcase): add integration tests for resumable upload
1 parent e981ac4 commit 5cf1688

1 file changed

Lines changed: 369 additions & 0 deletions

File tree

Lines changed: 369 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,369 @@
1+
/*
2+
* Copyright 2026 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+
import static org.junit.jupiter.api.Assertions.assertThrows;
21+
22+
import com.google.api.client.http.javanet.NetHttpTransport;
23+
import com.google.api.gax.core.FixedCredentialsProvider;
24+
import com.google.api.gax.core.NoCredentialsProvider;
25+
import com.google.api.gax.rpc.FailedPreconditionException;
26+
import com.google.api.gax.rpc.ResumableUploadCallSettings;
27+
import com.google.api.gax.rpc.ResumableUploadFuture;
28+
import com.google.api.gax.rpc.TransportChannelProvider;
29+
import com.google.auth.Credentials;
30+
import com.google.auth.oauth2.AccessToken;
31+
import com.google.auth.oauth2.OAuth2Credentials;
32+
import com.google.showcase.v1beta1.ResumableUploadServiceClient;
33+
import com.google.showcase.v1beta1.ResumableUploadServiceSettings;
34+
import com.google.showcase.v1beta1.UploadMediaRequest;
35+
import com.google.showcase.v1beta1.UploadMediaResponse;
36+
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
37+
import io.grpc.ManagedChannelBuilder;
38+
import java.io.ByteArrayInputStream;
39+
import java.io.IOException;
40+
import java.io.InputStream;
41+
import java.nio.charset.StandardCharsets;
42+
import java.nio.file.Files;
43+
import java.nio.file.Path;
44+
import java.util.Date;
45+
import java.util.concurrent.TimeUnit;
46+
import org.junit.jupiter.api.AfterAll;
47+
import org.junit.jupiter.api.BeforeAll;
48+
import org.junit.jupiter.api.Test;
49+
import org.junit.jupiter.api.io.TempDir;
50+
51+
/**
52+
* Integration tests for generated {@link ResumableUploadServiceClient} against the Showcase server.
53+
*/
54+
class ITResumableUpload {
55+
56+
private static final int SHOWCASE_CHUNK_SIZE = 256 * 1024; // 256KB
57+
private static final Credentials DUMMY_CREDENTIALS =
58+
OAuth2Credentials.create(new AccessToken("fake-token", new Date(Long.MAX_VALUE)));
59+
private static ResumableUploadServiceClient client;
60+
61+
@BeforeAll
62+
static void createClients() throws Exception {
63+
ResumableUploadServiceSettings.Builder settingsBuilder =
64+
ResumableUploadServiceSettings.newHttpJsonBuilder();
65+
settingsBuilder
66+
.setCredentialsProvider(NoCredentialsProvider.create())
67+
.setTransportChannelProvider(
68+
ResumableUploadServiceSettings.defaultHttpJsonTransportProviderBuilder()
69+
.setHttpTransport(new NetHttpTransport.Builder().doNotValidateCertificate().build())
70+
.setEndpoint(TestClientInitializer.DEFAULT_HTTPJSON_ENDPOINT)
71+
.build());
72+
settingsBuilder.uploadMediaSettings().setChunkSize(SHOWCASE_CHUNK_SIZE);
73+
client = ResumableUploadServiceClient.create(settingsBuilder.build());
74+
}
75+
76+
@AfterAll
77+
static void destroyClients() throws InterruptedException {
78+
if (client != null) {
79+
client.close();
80+
client.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
81+
}
82+
}
83+
84+
@Test
85+
void testGeneratedClient_uploadMedia_synchronousConvenienceMethod(@TempDir Path tempDir)
86+
throws Exception {
87+
Path file =
88+
createTempFile(
89+
tempDir,
90+
"it-client-sync.txt",
91+
"Hello from generated ResumableUploadServiceClient synchronous convenience method!"
92+
.getBytes(StandardCharsets.UTF_8));
93+
UploadMediaRequest request =
94+
UploadMediaRequest.newBuilder().setName("it-client-sync.txt").build();
95+
96+
try (InputStream stream = Files.newInputStream(file)) {
97+
UploadMediaResponse response = client.uploadMedia(request, stream);
98+
assertThat(response.getName()).isEqualTo("it-client-sync.txt");
99+
assertThat(response.getSize()).isEqualTo(Files.size(file));
100+
}
101+
}
102+
103+
@Test
104+
void testGeneratedClient_uploadMediaCallable_asynchronousFutureCall(@TempDir Path tempDir)
105+
throws Exception {
106+
Path file =
107+
createTempFile(
108+
tempDir,
109+
"it-client-callable.txt",
110+
"Hello from generated ResumableUploadServiceClient callable futureCall!"
111+
.getBytes(StandardCharsets.UTF_8));
112+
UploadMediaRequest request =
113+
UploadMediaRequest.newBuilder().setName("it-client-callable.txt").build();
114+
115+
try (InputStream stream = Files.newInputStream(file)) {
116+
ResumableUploadFuture<UploadMediaResponse> future =
117+
client.uploadMediaCallable().futureCall(request, stream, (ResumableUploadCallSettings) null);
118+
119+
UploadMediaResponse response = future.get(10, TimeUnit.SECONDS);
120+
assertThat(future.isDone()).isTrue();
121+
assertThat(future.isCancelled()).isFalse();
122+
assertThat(future.getUploadSessionUrl()).isNotNull();
123+
assertThat(future.getUploadSessionUrl()).contains("/resumable/upload");
124+
assertThat(response.getName()).isEqualTo("it-client-callable.txt");
125+
assertThat(response.getSize()).isEqualTo(Files.size(file));
126+
}
127+
}
128+
129+
@Test
130+
void testGeneratedClient_multiChunkUpload(@TempDir Path tempDir) throws Exception {
131+
// 600KB payload = 2 full 256KB chunks + 1 partial 88KB chunk
132+
int totalBytes = 600 * 1024;
133+
Path file = createTempFile(tempDir, "it-client-multi-chunk.txt", totalBytes);
134+
UploadMediaRequest request =
135+
UploadMediaRequest.newBuilder().setName("it-client-multi-chunk.txt").build();
136+
137+
try (InputStream stream = Files.newInputStream(file)) {
138+
UploadMediaResponse response = client.uploadMedia(request, stream);
139+
assertThat(response.getName()).isEqualTo("it-client-multi-chunk.txt");
140+
assertThat(response.getSize()).isEqualTo(Files.size(file));
141+
}
142+
}
143+
144+
@Test
145+
void testGeneratedClient_zeroByteUpload() throws Exception {
146+
UploadMediaRequest request =
147+
UploadMediaRequest.newBuilder().setName("it-client-zero-byte.txt").build();
148+
149+
try (InputStream stream = new ByteArrayInputStream(new byte[0])) {
150+
UploadMediaResponse response = client.uploadMedia(request, stream);
151+
assertThat(response.getName()).isEqualTo("it-client-zero-byte.txt");
152+
assertThat(response.getSize()).isEqualTo(0);
153+
}
154+
}
155+
156+
@Test
157+
void testGeneratedClient_exactChunkBoundaryUpload(@TempDir Path tempDir) throws Exception {
158+
// Exactly 2 full 256KB chunks (512KB total) -> triggers 0-byte finalize request
159+
int totalBytes = 512 * 1024;
160+
Path file = createTempFile(tempDir, "it-client-exact-chunks.txt", totalBytes);
161+
UploadMediaRequest request =
162+
UploadMediaRequest.newBuilder().setName("it-client-exact-chunks.txt").build();
163+
164+
try (InputStream stream = Files.newInputStream(file)) {
165+
UploadMediaResponse response = client.uploadMedia(request, stream);
166+
assertThat(response.getName()).isEqualTo("it-client-exact-chunks.txt");
167+
assertThat(response.getSize()).isEqualTo(Files.size(file));
168+
}
169+
}
170+
171+
@Test
172+
void testGeneratedClient_grpcClientDelegation_uploadMedia(@TempDir Path tempDir)
173+
throws Exception {
174+
// Note on test harness architecture (4A & 4B):
175+
// In production Google Front End (GFE) deployments, both gRPC and HTTP/REST traffic are served
176+
// on standard port 443. However, the local Showcase server binds gRPC to port 7469 and HTTP
177+
// to port 7470.
178+
// The custom TransportChannelProvider below routes the primary transport stub to gRPC on
179+
// port 7469 while allowing the client endpoint to be configured to port 7470
180+
// (TestClientInitializer.DEFAULT_HTTPJSON_ENDPOINT) so the internal HTTP/REST resumable upload
181+
// stub can successfully communicate with Showcase's HTTP port without altering the gRPC
182+
// endpoint.
183+
//
184+
// Additionally, DUMMY_CREDENTIALS is provided to satisfy the CL-R2.2 pre-constructed channel
185+
// guard,
186+
// which requires credentials to be present when instantiating the HTTP/REST transport stub for
187+
// resumable uploads, even in this unauthenticated local test environment.
188+
TransportChannelProvider grpcTransportChannelProvider =
189+
new TransportChannelProvider() {
190+
private final TransportChannelProvider delegate =
191+
ResumableUploadServiceSettings.defaultGrpcTransportProviderBuilder()
192+
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
193+
.setEndpoint(TestClientInitializer.DEFAULT_GRPC_ENDPOINT)
194+
.build();
195+
196+
@Override
197+
public boolean shouldAutoClose() {
198+
return delegate.shouldAutoClose();
199+
}
200+
201+
@Override
202+
public boolean needsExecutor() {
203+
return delegate.needsExecutor();
204+
}
205+
206+
@Override
207+
public TransportChannelProvider withExecutor(java.util.concurrent.Executor executor) {
208+
return delegate.withExecutor(executor);
209+
}
210+
211+
@Override
212+
public TransportChannelProvider withExecutor(
213+
java.util.concurrent.ScheduledExecutorService executor) {
214+
return delegate.withExecutor(executor);
215+
}
216+
217+
@Override
218+
public boolean needsHeaders() {
219+
return delegate.needsHeaders();
220+
}
221+
222+
@Override
223+
public TransportChannelProvider withHeaders(java.util.Map<String, String> headers) {
224+
return delegate.withHeaders(headers);
225+
}
226+
227+
@Override
228+
public boolean needsEndpoint() {
229+
return false;
230+
}
231+
232+
@Override
233+
public TransportChannelProvider withEndpoint(String endpoint) {
234+
return this;
235+
}
236+
237+
@Override
238+
public boolean acceptsPoolSize() {
239+
return delegate.acceptsPoolSize();
240+
}
241+
242+
@Override
243+
public TransportChannelProvider withPoolSize(int size) {
244+
return delegate.withPoolSize(size);
245+
}
246+
247+
@Override
248+
public boolean needsCredentials() {
249+
return delegate.needsCredentials();
250+
}
251+
252+
@Override
253+
public TransportChannelProvider withCredentials(Credentials credentials) {
254+
return delegate.withCredentials(credentials);
255+
}
256+
257+
@Override
258+
public com.google.api.gax.rpc.TransportChannel getTransportChannel() throws IOException {
259+
return delegate.getTransportChannel();
260+
}
261+
262+
@Override
263+
public String getTransportName() {
264+
return delegate.getTransportName();
265+
}
266+
267+
@Override
268+
public String getEndpoint() {
269+
return null;
270+
}
271+
};
272+
273+
ResumableUploadServiceSettings.Builder grpcSettingsBuilder =
274+
ResumableUploadServiceSettings.newBuilder()
275+
.setCredentialsProvider(FixedCredentialsProvider.create(DUMMY_CREDENTIALS))
276+
.setTransportChannelProvider(grpcTransportChannelProvider)
277+
.setEndpoint(TestClientInitializer.DEFAULT_HTTPJSON_ENDPOINT);
278+
grpcSettingsBuilder.uploadMediaSettings().setChunkSize(SHOWCASE_CHUNK_SIZE);
279+
280+
Path file =
281+
createTempFile(
282+
tempDir,
283+
"it-grpc-delegation.txt",
284+
"Hello from generated ResumableUploadServiceClient gRPC delegation!"
285+
.getBytes(StandardCharsets.UTF_8));
286+
UploadMediaRequest request =
287+
UploadMediaRequest.newBuilder().setName("it-grpc-delegation.txt").build();
288+
289+
ResumableUploadServiceClient grpcClient =
290+
ResumableUploadServiceClient.create(grpcSettingsBuilder.build());
291+
try {
292+
// 1. Synchronous convenience method delegation
293+
try (InputStream stream = Files.newInputStream(file)) {
294+
UploadMediaResponse response = grpcClient.uploadMedia(request, stream);
295+
assertThat(response.getName()).isEqualTo("it-grpc-delegation.txt");
296+
assertThat(response.getSize()).isEqualTo(Files.size(file));
297+
}
298+
299+
// 2. Asynchronous callable futureCall delegation
300+
try (InputStream stream = Files.newInputStream(file)) {
301+
ResumableUploadFuture<UploadMediaResponse> future =
302+
grpcClient.uploadMediaCallable().futureCall(request, stream, (ResumableUploadCallSettings) null);
303+
UploadMediaResponse response = future.get(10, TimeUnit.SECONDS);
304+
assertThat(future.isDone()).isTrue();
305+
assertThat(future.isCancelled()).isFalse();
306+
assertThat(future.getUploadSessionUrl()).isNotNull();
307+
assertThat(future.getUploadSessionUrl()).contains("/resumable/upload");
308+
assertThat(response.getName()).isEqualTo("it-grpc-delegation.txt");
309+
assertThat(response.getSize()).isEqualTo(Files.size(file));
310+
}
311+
} finally {
312+
grpcClient.close();
313+
assertThat(grpcClient.awaitTermination(5, TimeUnit.SECONDS)).isTrue();
314+
}
315+
}
316+
317+
@Test
318+
void testChannelGuards_grpcChannelOnly_throwsFailedPreconditionException(@TempDir Path tempDir)
319+
throws Exception {
320+
ResumableUploadServiceSettings grpcSettings =
321+
ResumableUploadServiceSettings.newBuilder()
322+
.setCredentialsProvider(NoCredentialsProvider.create())
323+
.setTransportChannelProvider(
324+
ResumableUploadServiceSettings.defaultGrpcTransportProviderBuilder()
325+
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
326+
.build())
327+
.setEndpoint(TestClientInitializer.DEFAULT_GRPC_ENDPOINT)
328+
.build();
329+
330+
Path file =
331+
createTempFile(
332+
tempDir, "guard-test.txt", "guard test data".getBytes(StandardCharsets.UTF_8));
333+
334+
try (ResumableUploadServiceClient grpcClient =
335+
ResumableUploadServiceClient.create(grpcSettings)) {
336+
UploadMediaRequest request =
337+
UploadMediaRequest.newBuilder().setName("guard-test.txt").build();
338+
339+
try (InputStream stream1 = Files.newInputStream(file)) {
340+
// 1. Verify synchronous convenience call fails fast
341+
FailedPreconditionException syncException =
342+
assertThrows(
343+
FailedPreconditionException.class, () -> grpcClient.uploadMedia(request, stream1));
344+
assertThat(syncException.getMessage())
345+
.contains("Resumable uploads execute over HTTP/REST and require credentials");
346+
}
347+
348+
// 2. Verify callable getter fails fast
349+
FailedPreconditionException callableException =
350+
assertThrows(FailedPreconditionException.class, () -> grpcClient.uploadMediaCallable());
351+
assertThat(callableException.getMessage())
352+
.contains("Resumable uploads execute over HTTP/REST and require credentials");
353+
}
354+
}
355+
356+
private static Path createTempFile(Path dir, String fileName, byte[] data) throws IOException {
357+
Path path = dir.resolve(fileName);
358+
Files.write(path, data);
359+
return path;
360+
}
361+
362+
private static Path createTempFile(Path dir, String fileName, int size) throws IOException {
363+
byte[] data = new byte[size];
364+
for (int i = 0; i < size; i++) {
365+
data[i] = (byte) (i % 256);
366+
}
367+
return createTempFile(dir, fileName, data);
368+
}
369+
}

0 commit comments

Comments
 (0)