Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.showcase.v1beta1.UploadMediaRequest;
import com.google.showcase.v1beta1.UploadMediaResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -98,15 +99,29 @@ class HttpJsonResumableUploadServiceResumableUploadStub implements BackgroundRes

public static HttpJsonResumableUploadServiceResumableUploadStub create(
ResumableUploadServiceStubSettings settings) throws IOException {
if (!HttpJsonCallableFactory.isResumableUploadSupported(settings)) {
return new HttpJsonResumableUploadServiceResumableUploadStub(
HttpJsonCallableFactory.createFailingResumableUploadCallable(),
new BackgroundResourceAggregation(Collections.emptyList()));
}
ClientContext clientContext;
if (settings
.getTransportChannelProvider()
.getTransportName()
.equals(HttpJsonTransportChannel.getHttpJsonTransportName())) {
return new HttpJsonResumableUploadServiceResumableUploadStub(
ClientContext.create(settings), settings);
clientContext = ClientContext.create(settings);
} else {
clientContext = deriveHttpJsonClientContext(settings);
}
HttpJsonCallSettings<UploadMediaRequest, UploadMediaResponse> uploadMediaTransportSettings =
HttpJsonCallSettings.<UploadMediaRequest, UploadMediaResponse>newBuilder()
.setMethodDescriptor(uploadMediaMethodDescriptor)
.setTypeRegistry(typeRegistry)
.build();
return new HttpJsonResumableUploadServiceResumableUploadStub(
deriveHttpJsonClientContext(settings), settings);
HttpJsonCallableFactory.createResumableUploadCallable(
uploadMediaTransportSettings, settings.uploadMediaSettings(), clientContext),
new BackgroundResourceAggregation(clientContext.getBackgroundResources()));
}

private static ClientContext deriveHttpJsonClientContext(
Expand All @@ -127,18 +142,10 @@ private static ClientContext deriveHttpJsonClientContext(
}

private HttpJsonResumableUploadServiceResumableUploadStub(
ClientContext clientContext, ResumableUploadServiceStubSettings settings) {
HttpJsonCallSettings<UploadMediaRequest, UploadMediaResponse> uploadMediaTransportSettings =
HttpJsonCallSettings.<UploadMediaRequest, UploadMediaResponse>newBuilder()
.setMethodDescriptor(uploadMediaMethodDescriptor)
.setTypeRegistry(typeRegistry)
.build();
this.uploadMediaCallable =
HttpJsonCallableFactory.createResumableUploadCallable(
uploadMediaTransportSettings, settings.uploadMediaSettings(), clientContext);

this.backgroundResources =
new BackgroundResourceAggregation(clientContext.getBackgroundResources());
ResumableUploadCallable<UploadMediaRequest, UploadMediaResponse> uploadMediaCallable,
BackgroundResource backgroundResources) {
this.uploadMediaCallable = uploadMediaCallable;
this.backgroundResources = backgroundResources;
}

public ResumableUploadCallable<UploadMediaRequest, UploadMediaResponse> uploadMediaCallable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package com.google.showcase.v1beta1.it;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.httpjson.HttpJsonCallContext;
import com.google.api.gax.rpc.ApiCallContext;
import com.google.api.gax.rpc.DeadlineExceededException;
Expand All @@ -30,6 +32,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.showcase.v1beta1.ResumableUploadServiceClient;
import com.google.showcase.v1beta1.ResumableUploadServiceSettings;
import com.google.showcase.v1beta1.UploadMediaRequest;
import com.google.showcase.v1beta1.UploadMediaResponse;
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
Expand Down Expand Up @@ -140,24 +143,22 @@ void testGrpcClientDelegation(@TempDir Path tempDir) throws Exception {
UploadMediaRequest request =
UploadMediaRequest.newBuilder().setName("it-grpc-delegation.txt").build();

try (ResumableUploadServiceClient grpcClient =
TestClientInitializer.createGrpcResumableUploadClient(SHOWCASE_CHUNK_SIZE)) {
UploadMediaResponse syncResponse =
grpcClient.uploadMedia(request, () -> Files.newInputStream(file), DEFAULT_TEST_OPTIONS);
assertThat(syncResponse.getName()).isEqualTo("it-grpc-delegation.txt");
assertThat(syncResponse.getSize()).isEqualTo(Files.size(file));

ResumableUploadServiceSettings settings =
ResumableUploadServiceSettings.newBuilder()
.setCredentialsProvider(NoCredentialsProvider.create())
.setEndpoint(TestClientInitializer.DEFAULT_GRPC_ENDPOINT)
.build();
try (ResumableUploadServiceClient grpcClient = ResumableUploadServiceClient.create(settings)) {
ResumableUploadFuture<UploadMediaResponse> future =
grpcClient
.uploadMediaCallable()
.futureCall(request, () -> Files.newInputStream(file), null);
UploadMediaResponse asyncResponse = future.get(10, TimeUnit.SECONDS);
assertThat(future.isDone()).isTrue();
assertThat(future.isCancelled()).isFalse();
assertThat(future.getUploadSessionUrl()).isNotNull();
assertThat(future.getUploadSessionUrl()).contains("/resumable/upload");
assertThat(asyncResponse.getName()).isEqualTo("it-grpc-delegation.txt");
assertThat(asyncResponse.getSize()).isEqualTo(Files.size(file));
assertDoesNotThrow(
() ->
grpcClient
.uploadMediaCallable()
.futureCall(request, () -> Files.newInputStream(file), null));
// Only verify that the bridged callable accepts the call: gRPC settings require
// "localhost:7469" (no scheme), which causes the HTTP/JSON bridge to default to "https://"
// against plaintext Showcase.
future.cancel(true);
}
}

Expand Down Expand Up @@ -359,6 +360,33 @@ void testProgressListener(@TempDir Path tempDir) throws Exception {
}
}

@Test
void testGrpcClientCustomTransportRejected(@TempDir Path tempDir) throws Exception {
Path file =
createTempFile(
tempDir,
"it-grpc-rejected.txt",
"Payload that should not be uploaded".getBytes(StandardCharsets.UTF_8));
UploadMediaRequest request =
UploadMediaRequest.newBuilder().setName("it-grpc-rejected.txt").build();

try (ResumableUploadServiceClient grpcClient =
TestClientInitializer.createGrpcResumableUploadClient(SHOWCASE_CHUNK_SIZE)) {
assertThat(grpcClient.uploadMediaCallable()).isNotNull();
assertThrows(
IllegalStateException.class,
() ->
grpcClient.uploadMedia(
request, () -> Files.newInputStream(file), DEFAULT_TEST_OPTIONS));
assertThrows(
IllegalStateException.class,
() ->
grpcClient
.uploadMediaCallable()
.futureCall(request, () -> Files.newInputStream(file), null));
}
}

private static Path createTempFile(Path dir, String fileName, byte[] data) throws IOException {
Path path = dir.resolve(fileName);
Files.write(path, data);
Expand Down
Loading
Loading