Skip to content

Commit 843d4d1

Browse files
committed
test(showcase): add integration tests for resumable upload
1 parent f9ebd79 commit 843d4d1

2 files changed

Lines changed: 221 additions & 0 deletions

File tree

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+
21+
import com.google.api.gax.rpc.ResumableUploadCallSettings;
22+
import com.google.api.gax.rpc.ResumableUploadFuture;
23+
import com.google.showcase.v1beta1.ResumableUploadServiceClient;
24+
import com.google.showcase.v1beta1.UploadMediaRequest;
25+
import com.google.showcase.v1beta1.UploadMediaResponse;
26+
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
27+
import java.io.ByteArrayInputStream;
28+
import java.io.IOException;
29+
import java.io.InputStream;
30+
import java.nio.charset.StandardCharsets;
31+
import java.nio.file.Files;
32+
import java.nio.file.Path;
33+
import java.util.concurrent.TimeUnit;
34+
import org.junit.jupiter.api.AfterAll;
35+
import org.junit.jupiter.api.BeforeAll;
36+
import org.junit.jupiter.api.Test;
37+
import org.junit.jupiter.api.io.TempDir;
38+
39+
/**
40+
* Integration tests for generated {@link ResumableUploadServiceClient} against the Showcase server.
41+
*/
42+
class ITResumableUpload {
43+
44+
private static final int SHOWCASE_CHUNK_SIZE = 256 * 1024; // 256KB
45+
private static ResumableUploadServiceClient client;
46+
47+
@BeforeAll
48+
static void createClients() throws Exception {
49+
client = TestClientInitializer.createHttpJsonResumableUploadClient(SHOWCASE_CHUNK_SIZE);
50+
}
51+
52+
@AfterAll
53+
static void destroyClients() throws InterruptedException {
54+
if (client != null) {
55+
client.close();
56+
client.awaitTermination(TestClientInitializer.AWAIT_TERMINATION_SECONDS, TimeUnit.SECONDS);
57+
}
58+
}
59+
60+
@Test
61+
void testGeneratedClient_uploadMedia_synchronousConvenienceMethod(@TempDir Path tempDir)
62+
throws Exception {
63+
Path file =
64+
createTempFile(
65+
tempDir,
66+
"it-client-sync.txt",
67+
"Hello from generated ResumableUploadServiceClient synchronous convenience method!"
68+
.getBytes(StandardCharsets.UTF_8));
69+
UploadMediaRequest request =
70+
UploadMediaRequest.newBuilder().setName("it-client-sync.txt").build();
71+
72+
try (InputStream stream = Files.newInputStream(file)) {
73+
UploadMediaResponse response = client.uploadMedia(request, stream, null);
74+
assertThat(response.getName()).isEqualTo("it-client-sync.txt");
75+
assertThat(response.getSize()).isEqualTo(Files.size(file));
76+
}
77+
}
78+
79+
@Test
80+
void testGeneratedClient_uploadMediaCallable_asynchronousFutureCall(@TempDir Path tempDir)
81+
throws Exception {
82+
Path file =
83+
createTempFile(
84+
tempDir,
85+
"it-client-callable.txt",
86+
"Hello from generated ResumableUploadServiceClient callable futureCall!"
87+
.getBytes(StandardCharsets.UTF_8));
88+
UploadMediaRequest request =
89+
UploadMediaRequest.newBuilder().setName("it-client-callable.txt").build();
90+
91+
try (InputStream stream = Files.newInputStream(file)) {
92+
ResumableUploadFuture<UploadMediaResponse> future =
93+
client
94+
.uploadMediaCallable()
95+
.futureCall(request, stream, (ResumableUploadCallSettings) null);
96+
97+
UploadMediaResponse response = future.get(10, TimeUnit.SECONDS);
98+
assertThat(future.isDone()).isTrue();
99+
assertThat(future.isCancelled()).isFalse();
100+
assertThat(future.getUploadSessionUrl()).isNotNull();
101+
assertThat(future.getUploadSessionUrl()).contains("/resumable/upload");
102+
assertThat(response.getName()).isEqualTo("it-client-callable.txt");
103+
assertThat(response.getSize()).isEqualTo(Files.size(file));
104+
}
105+
}
106+
107+
@Test
108+
void testGeneratedClient_multiChunkUpload(@TempDir Path tempDir) throws Exception {
109+
// 600KB payload = 2 full 256KB chunks + 1 partial 88KB chunk
110+
int totalBytes = 600 * 1024;
111+
Path file = createTempFile(tempDir, "it-client-multi-chunk.txt", totalBytes);
112+
UploadMediaRequest request =
113+
UploadMediaRequest.newBuilder().setName("it-client-multi-chunk.txt").build();
114+
115+
try (InputStream stream = Files.newInputStream(file)) {
116+
UploadMediaResponse response = client.uploadMedia(request, stream, null);
117+
assertThat(response.getName()).isEqualTo("it-client-multi-chunk.txt");
118+
assertThat(response.getSize()).isEqualTo(Files.size(file));
119+
}
120+
}
121+
122+
@Test
123+
void testGeneratedClient_grpcClientDelegation_uploadMedia(@TempDir Path tempDir)
124+
throws Exception {
125+
Path file =
126+
createTempFile(
127+
tempDir,
128+
"it-grpc-delegation.txt",
129+
"Hello from generated ResumableUploadServiceClient gRPC delegation!"
130+
.getBytes(StandardCharsets.UTF_8));
131+
UploadMediaRequest request =
132+
UploadMediaRequest.newBuilder().setName("it-grpc-delegation.txt").build();
133+
134+
try (ResumableUploadServiceClient grpcClient =
135+
TestClientInitializer.createGrpcResumableUploadClient(SHOWCASE_CHUNK_SIZE)) {
136+
try (InputStream stream = Files.newInputStream(file)) {
137+
UploadMediaResponse response = grpcClient.uploadMedia(request, stream, null);
138+
assertThat(response.getName()).isEqualTo("it-grpc-delegation.txt");
139+
assertThat(response.getSize()).isEqualTo(Files.size(file));
140+
}
141+
142+
try (InputStream stream = Files.newInputStream(file)) {
143+
ResumableUploadFuture<UploadMediaResponse> future =
144+
grpcClient
145+
.uploadMediaCallable()
146+
.futureCall(request, stream, (ResumableUploadCallSettings) null);
147+
UploadMediaResponse response = future.get(10, TimeUnit.SECONDS);
148+
assertThat(future.isDone()).isTrue();
149+
assertThat(future.isCancelled()).isFalse();
150+
assertThat(future.getUploadSessionUrl()).isNotNull();
151+
assertThat(future.getUploadSessionUrl()).contains("/resumable/upload");
152+
assertThat(response.getName()).isEqualTo("it-grpc-delegation.txt");
153+
assertThat(response.getSize()).isEqualTo(Files.size(file));
154+
}
155+
}
156+
}
157+
158+
@Test
159+
void testGeneratedClient_uploadMedia_withCustomCallSettings(@TempDir Path tempDir)
160+
throws Exception {
161+
// 600KB payload with custom per-call 512KB chunk size override (default is 256KB)
162+
int totalBytes = 600 * 1024;
163+
Path file = createTempFile(tempDir, "it-client-custom-call-settings.txt", totalBytes);
164+
UploadMediaRequest request =
165+
UploadMediaRequest.newBuilder().setName("it-client-custom-call-settings.txt").build();
166+
ResumableUploadCallSettings callSettings =
167+
ResumableUploadCallSettings.newBuilder().setChunkSize(512 * 1024).build();
168+
169+
try (InputStream stream = Files.newInputStream(file)) {
170+
UploadMediaResponse response = client.uploadMedia(request, stream, callSettings);
171+
assertThat(response.getName()).isEqualTo("it-client-custom-call-settings.txt");
172+
assertThat(response.getSize()).isEqualTo(Files.size(file));
173+
}
174+
}
175+
176+
private static Path createTempFile(Path dir, String fileName, byte[] data) throws IOException {
177+
Path path = dir.resolve(fileName);
178+
Files.write(path, data);
179+
return path;
180+
}
181+
182+
private static Path createTempFile(Path dir, String fileName, int size) throws IOException {
183+
byte[] data = new byte[size];
184+
for (int i = 0; i < size; i++) {
185+
data[i] = (byte) (i % 256);
186+
}
187+
return createTempFile(dir, fileName, data);
188+
}
189+
}

java-showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/util/TestClientInitializer.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import com.google.showcase.v1beta1.EchoSettings;
3434
import com.google.showcase.v1beta1.IdentityClient;
3535
import com.google.showcase.v1beta1.IdentitySettings;
36+
import com.google.showcase.v1beta1.ResumableUploadServiceClient;
37+
import com.google.showcase.v1beta1.ResumableUploadServiceSettings;
3638
import com.google.showcase.v1beta1.SequenceServiceClient;
3739
import com.google.showcase.v1beta1.SequenceServiceSettings;
3840
import com.google.showcase.v1beta1.WaitRequest;
@@ -539,4 +541,34 @@ public static SequenceServiceClient createHttpJsonSequenceClientWithRetrySetting
539541
.build());
540542
return SequenceServiceClient.create(settingsBuilder.build());
541543
}
544+
545+
public static ResumableUploadServiceClient createHttpJsonResumableUploadClient(int chunkSize)
546+
throws Exception {
547+
ResumableUploadServiceSettings.Builder settingsBuilder =
548+
ResumableUploadServiceSettings.newHttpJsonBuilder();
549+
settingsBuilder
550+
.setCredentialsProvider(NoCredentialsProvider.create())
551+
.setTransportChannelProvider(
552+
ResumableUploadServiceSettings.defaultHttpJsonTransportProviderBuilder()
553+
.setHttpTransport(new NetHttpTransport.Builder().doNotValidateCertificate().build())
554+
.setEndpoint(DEFAULT_HTTPJSON_ENDPOINT)
555+
.build());
556+
settingsBuilder.uploadMediaSettings().setChunkSize(chunkSize);
557+
return ResumableUploadServiceClient.create(settingsBuilder.build());
558+
}
559+
560+
public static ResumableUploadServiceClient createGrpcResumableUploadClient(int chunkSize)
561+
throws Exception {
562+
ResumableUploadServiceSettings.Builder settingsBuilder =
563+
ResumableUploadServiceSettings.newBuilder()
564+
.setCredentialsProvider(NoCredentialsProvider.create())
565+
.setTransportChannelProvider(
566+
ResumableUploadServiceSettings.defaultGrpcTransportProviderBuilder()
567+
.setEndpoint(DEFAULT_GRPC_ENDPOINT)
568+
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
569+
.build())
570+
.setEndpoint(DEFAULT_HTTPJSON_ENDPOINT);
571+
settingsBuilder.uploadMediaSettings().setChunkSize(chunkSize);
572+
return ResumableUploadServiceClient.create(settingsBuilder.build());
573+
}
542574
}

0 commit comments

Comments
 (0)