Skip to content

Commit fe8dd39

Browse files
committed
test(generator): add unit goldens for resumable upload stubs, client, and settings
1 parent 3682105 commit fe8dd39

20 files changed

Lines changed: 1496 additions & 3 deletions

‎sdk-platform-java/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/GrpcServiceStubClassComposerTest.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,14 @@ void generateGrpcServiceStubClass_clientResourceNameExtractor() {
122122
Assert.assertGoldenClass(this.getClass(), clazz, "GrpcResourceNameExtractorStub.golden");
123123
Assert.assertEmptySamples(clazz.samples());
124124
}
125+
126+
@Test
127+
void generateGrpcServiceStubClass_resumableUpload() {
128+
GapicContext context = GrpcTestProtoLoader.instance().parseShowcaseResumableUpload();
129+
Service service = context.services().get(0);
130+
GapicClass clazz = GrpcServiceStubClassComposer.instance().generate(context, service);
131+
132+
Assert.assertGoldenClass(this.getClass(), clazz, "GrpcResumableUploadServiceStub.golden");
133+
Assert.assertEmptySamples(clazz.samples());
134+
}
125135
}

‎sdk-platform-java/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ private static Stream<Arguments> data() {
6464
GrpcTestProtoLoader.instance().parseSelectiveGenerationTesting(),
6565
"localhost:7469",
6666
"v1beta1",
67-
1));
67+
1),
68+
Arguments.of(
69+
"ResumableUploadServiceClient",
70+
GrpcTestProtoLoader.instance().parseShowcaseResumableUpload(),
71+
"localhost:7469",
72+
"v1beta1",
73+
0));
6874
}
6975

7076
@ParameterizedTest

‎sdk-platform-java/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ static Stream<Arguments> data() {
4545
TestProtoLoader.instance().parseSelectiveGenerationTesting(),
4646
"localhost:7469",
4747
"v1beta1",
48-
1));
48+
1),
49+
Arguments.of(
50+
"ResumableUploadServiceSettings",
51+
TestProtoLoader.instance().parseShowcaseResumableUpload(),
52+
"localhost:7469",
53+
"v1beta1",
54+
0));
4955
}
5056

5157
@ParameterizedTest

‎sdk-platform-java/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ static Stream<Arguments> data() {
3939
TestProtoLoader.instance().parseSelectiveGenerationTesting(),
4040
"",
4141
"",
42-
1));
42+
1),
43+
Arguments.of(
44+
"ResumableUploadServiceStub",
45+
TestProtoLoader.instance().parseShowcaseResumableUpload(),
46+
"",
47+
"",
48+
0));
4349
}
4450

4551
@ParameterizedTest

‎sdk-platform-java/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ static Stream<Arguments> data() {
6868
GrpcTestProtoLoader.instance().parseBigqueryService(),
6969
"bigquery",
7070
"v2",
71+
0),
72+
Arguments.of(
73+
"ResumableUploadServiceStubSettings",
74+
GrpcTestProtoLoader.instance().parseShowcaseResumableUpload(),
75+
"localhost:7469",
76+
"v1beta1",
7177
0));
7278
}
7379

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package com.google.showcase.v1beta1.stub;
2+
3+
import com.google.api.core.BetaApi;
4+
import com.google.api.gax.core.BackgroundResource;
5+
import com.google.api.gax.core.BackgroundResourceAggregation;
6+
import com.google.api.gax.grpc.GrpcStatusCode;
7+
import com.google.api.gax.grpc.GrpcStubCallableFactory;
8+
import com.google.api.gax.httpjson.HttpJsonCallContext;
9+
import com.google.api.gax.rpc.ClientContext;
10+
import com.google.api.gax.rpc.FailedPreconditionException;
11+
import com.google.api.gax.rpc.ResumableUploadCallable;
12+
import com.google.longrunning.stub.GrpcOperationsStub;
13+
import com.google.showcase.v1beta1.UploadMediaRequest;
14+
import com.google.showcase.v1beta1.UploadMediaResponse;
15+
import io.grpc.Status;
16+
import java.io.IOException;
17+
import java.util.concurrent.TimeUnit;
18+
import javax.annotation.Generated;
19+
import org.jspecify.annotations.NullMarked;
20+
import org.jspecify.annotations.Nullable;
21+
22+
// AUTO-GENERATED DOCUMENTATION AND CLASS.
23+
/**
24+
* gRPC stub implementation for the ResumableUploadService service API.
25+
*
26+
* <p>This class is for advanced usage and reflects the underlying API directly.
27+
*/
28+
@NullMarked
29+
@BetaApi
30+
@Generated("by gapic-generator-java")
31+
public class GrpcResumableUploadServiceStub extends ResumableUploadServiceStub {
32+
33+
private final BackgroundResource backgroundResources;
34+
private final GrpcOperationsStub operationsStub;
35+
private final @Nullable HttpJsonResumableUploadServiceResumableUploadStub resumableUploadStub;
36+
private final GrpcStubCallableFactory callableFactory;
37+
38+
public static final GrpcResumableUploadServiceStub create(
39+
ResumableUploadServiceStubSettings settings) throws IOException {
40+
return new GrpcResumableUploadServiceStub(settings, ClientContext.create(settings));
41+
}
42+
43+
public static final GrpcResumableUploadServiceStub create(ClientContext clientContext)
44+
throws IOException {
45+
return new GrpcResumableUploadServiceStub(
46+
ResumableUploadServiceStubSettings.newBuilder().build(), clientContext);
47+
}
48+
49+
public static final GrpcResumableUploadServiceStub create(
50+
ClientContext clientContext, GrpcStubCallableFactory callableFactory) throws IOException {
51+
return new GrpcResumableUploadServiceStub(
52+
ResumableUploadServiceStubSettings.newBuilder().build(), clientContext, callableFactory);
53+
}
54+
55+
/**
56+
* Constructs an instance of GrpcResumableUploadServiceStub, using the given settings. This is
57+
* protected so that it is easy to make a subclass, but otherwise, the static factory methods
58+
* should be preferred.
59+
*/
60+
protected GrpcResumableUploadServiceStub(
61+
ResumableUploadServiceStubSettings settings, ClientContext clientContext) throws IOException {
62+
this(settings, clientContext, new GrpcResumableUploadServiceCallableFactory());
63+
}
64+
65+
/**
66+
* Constructs an instance of GrpcResumableUploadServiceStub, using the given settings. This is
67+
* protected so that it is easy to make a subclass, but otherwise, the static factory methods
68+
* should be preferred.
69+
*/
70+
protected GrpcResumableUploadServiceStub(
71+
ResumableUploadServiceStubSettings settings,
72+
ClientContext clientContext,
73+
GrpcStubCallableFactory callableFactory)
74+
throws IOException {
75+
this.callableFactory = callableFactory;
76+
this.operationsStub = GrpcOperationsStub.create(clientContext, callableFactory);
77+
78+
if (clientContext.getCredentials() != null) {
79+
ClientContext httpJsonClientContext =
80+
ClientContext.newBuilder()
81+
.setCredentials(clientContext.getCredentials())
82+
.setEndpoint(settings.getEndpoint())
83+
.setExecutor(clientContext.getExecutor())
84+
.setDefaultCallContext(HttpJsonCallContext.createDefault())
85+
.build();
86+
this.resumableUploadStub =
87+
HttpJsonResumableUploadServiceResumableUploadStub.create(httpJsonClientContext, settings);
88+
} else {
89+
this.resumableUploadStub = null;
90+
}
91+
92+
this.backgroundResources =
93+
new BackgroundResourceAggregation(clientContext.getBackgroundResources());
94+
}
95+
96+
public GrpcOperationsStub getOperationsStub() {
97+
return operationsStub;
98+
}
99+
100+
@Override
101+
public ResumableUploadCallable<UploadMediaRequest, UploadMediaResponse> uploadMediaCallable() {
102+
if (resumableUploadStub == null) {
103+
throw new FailedPreconditionException(
104+
"Resumable uploads execute over HTTP/REST and require credentials. The client was initialized with a pre-constructed gRPC Channel, from which credentials cannot be extracted. Please configure a CredentialsProvider instead.",
105+
null,
106+
GrpcStatusCode.of(Status.Code.FAILED_PRECONDITION),
107+
false);
108+
}
109+
return resumableUploadStub.uploadMediaCallable();
110+
}
111+
112+
@Override
113+
public final void close() {
114+
try {
115+
backgroundResources.close();
116+
if (resumableUploadStub != null) {
117+
resumableUploadStub.close();
118+
}
119+
} catch (RuntimeException e) {
120+
throw e;
121+
} catch (Exception e) {
122+
throw new IllegalStateException("Failed to close resource", e);
123+
}
124+
}
125+
126+
@Override
127+
public void shutdown() {
128+
backgroundResources.shutdown();
129+
}
130+
131+
@Override
132+
public boolean isShutdown() {
133+
return backgroundResources.isShutdown();
134+
}
135+
136+
@Override
137+
public boolean isTerminated() {
138+
return backgroundResources.isTerminated();
139+
}
140+
141+
@Override
142+
public void shutdownNow() {
143+
backgroundResources.shutdownNow();
144+
}
145+
146+
@Override
147+
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
148+
return backgroundResources.awaitTermination(duration, unit);
149+
}
150+
}

0 commit comments

Comments
 (0)