Skip to content

Commit 6ab1628

Browse files
committed
test: add test proto to use nested type with golden test for writer
1 parent 158e6a9 commit 6ab1628

4 files changed

Lines changed: 291 additions & 0 deletions

File tree

gapic-generator-java/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@
7171
import com.google.api.generator.engine.ast.Variable;
7272
import com.google.api.generator.engine.ast.VariableExpr;
7373
import com.google.api.generator.engine.ast.WhileStatement;
74+
import com.google.api.generator.gapic.composer.grpc.ServiceClientClassComposer;
75+
import com.google.api.generator.gapic.model.GapicClass;
76+
import com.google.api.generator.gapic.model.GapicContext;
77+
import com.google.api.generator.gapic.model.Service;
78+
import com.google.api.generator.test.framework.Assert;
79+
import com.google.api.generator.test.protoloader.TestProtoLoader;
7480
import com.google.api.generator.test.utils.LineFormatter;
7581
import com.google.api.generator.test.utils.TestExprBuilder;
7682
import com.google.common.base.Function;
@@ -2817,6 +2823,17 @@ public void writePackageInfoDefinition() {
28172823
writerVisitor.write());
28182824
}
28192825

2826+
/** =============================== GOLDEN TESTS =============================== */
2827+
@Test
2828+
public void writeSGrpcServiceClientWithNestedClassImport() {
2829+
GapicContext context = TestProtoLoader.instance().parseNestedMessage();
2830+
Service nestedService = context.services().get(0);
2831+
GapicClass clazz = ServiceClientClassComposer.instance().generate(context, nestedService);
2832+
2833+
Assert.assertGoldenClass(
2834+
this.getClass(), clazz, "GrpcServiceClientWithNestedClassImport.golden");
2835+
}
2836+
28202837
/** =============================== HELPERS =============================== */
28212838
private static AssignmentExpr createAssignmentExpr(
28222839
String variableName, String value, TypeNode type) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package com.google.types.testing;
2+
3+
import com.google.api.gax.core.BackgroundResource;
4+
import com.google.api.gax.rpc.UnaryCallable;
5+
import com.google.testgapic.v1beta1.Outer;
6+
import com.google.types.testing.stub.NestedMessageServiceStub;
7+
import com.google.types.testing.stub.NestedMessageServiceStubSettings;
8+
import java.io.IOException;
9+
import java.util.concurrent.TimeUnit;
10+
import javax.annotation.Generated;
11+
12+
// AUTO-GENERATED DOCUMENTATION AND CLASS.
13+
/**
14+
* This class provides the ability to make remote calls to the backing service through method calls
15+
* that map to API methods. Sample code to get started:
16+
*
17+
* <pre>{@code
18+
* // This snippet has been automatically generated and should be regarded as a code template only.
19+
* // It will require modifications to work:
20+
* // - It may require correct/in-range values for request initialization.
21+
* // - It may require specifying regional endpoints when creating the service client as shown in
22+
* // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
23+
* try (NestedMessageServiceClient nestedMessageServiceClient =
24+
* NestedMessageServiceClient.create()) {
25+
* Outer.Middle request = Outer.Middle.newBuilder().setX(120).build();
26+
* Outer.Middle.Inner response = nestedMessageServiceClient.nestedMessageMethod(request);
27+
* }
28+
* }</pre>
29+
*
30+
* <p>Note: close() needs to be called on the NestedMessageServiceClient object to clean up
31+
* resources such as threads. In the example above, try-with-resources is used, which automatically
32+
* calls close().
33+
*
34+
* <p>The surface of this class includes several types of Java methods for each of the API's
35+
* methods:
36+
*
37+
* <ol>
38+
* <li>A "flattened" method. With this type of method, the fields of the request type have been
39+
* converted into function parameters. It may be the case that not all fields are available as
40+
* parameters, and not every API method will have a flattened method entry point.
41+
* <li>A "request object" method. This type of method only takes one parameter, a request object,
42+
* which must be constructed before the call. Not every API method will have a request object
43+
* method.
44+
* <li>A "callable" method. This type of method takes no parameters and returns an immutable API
45+
* callable object, which can be used to initiate calls to the service.
46+
* </ol>
47+
*
48+
* <p>See the individual methods for example code.
49+
*
50+
* <p>Many parameters require resource names to be formatted in a particular way. To assist with
51+
* these names, this class includes a format method for each type of name, and additionally a parse
52+
* method to extract the individual identifiers contained within names that are returned.
53+
*
54+
* <p>This class can be customized by passing in a custom instance of NestedMessageServiceSettings
55+
* to create(). For example:
56+
*
57+
* <p>To customize credentials:
58+
*
59+
* <pre>{@code
60+
* // This snippet has been automatically generated and should be regarded as a code template only.
61+
* // It will require modifications to work:
62+
* // - It may require correct/in-range values for request initialization.
63+
* // - It may require specifying regional endpoints when creating the service client as shown in
64+
* // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
65+
* NestedMessageServiceSettings nestedMessageServiceSettings =
66+
* NestedMessageServiceSettings.newBuilder()
67+
* .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
68+
* .build();
69+
* NestedMessageServiceClient nestedMessageServiceClient =
70+
* NestedMessageServiceClient.create(nestedMessageServiceSettings);
71+
* }</pre>
72+
*
73+
* <p>To customize the endpoint:
74+
*
75+
* <pre>{@code
76+
* // This snippet has been automatically generated and should be regarded as a code template only.
77+
* // It will require modifications to work:
78+
* // - It may require correct/in-range values for request initialization.
79+
* // - It may require specifying regional endpoints when creating the service client as shown in
80+
* // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
81+
* NestedMessageServiceSettings nestedMessageServiceSettings =
82+
* NestedMessageServiceSettings.newBuilder().setEndpoint(myEndpoint).build();
83+
* NestedMessageServiceClient nestedMessageServiceClient =
84+
* NestedMessageServiceClient.create(nestedMessageServiceSettings);
85+
* }</pre>
86+
*
87+
* <p>Please refer to the GitHub repository's samples for more quickstart code snippets.
88+
*/
89+
@Generated("by gapic-generator-java")
90+
public class NestedMessageServiceClient implements BackgroundResource {
91+
private final NestedMessageServiceSettings settings;
92+
private final NestedMessageServiceStub stub;
93+
94+
/** Constructs an instance of NestedMessageServiceClient with default settings. */
95+
public static final NestedMessageServiceClient create() throws IOException {
96+
return create(NestedMessageServiceSettings.newBuilder().build());
97+
}
98+
99+
/**
100+
* Constructs an instance of NestedMessageServiceClient, using the given settings. The channels
101+
* are created based on the settings passed in, or defaults for any settings that are not set.
102+
*/
103+
public static final NestedMessageServiceClient create(NestedMessageServiceSettings settings)
104+
throws IOException {
105+
return new NestedMessageServiceClient(settings);
106+
}
107+
108+
/**
109+
* Constructs an instance of NestedMessageServiceClient, using the given stub for making calls.
110+
* This is for advanced usage - prefer using create(NestedMessageServiceSettings).
111+
*/
112+
public static final NestedMessageServiceClient create(NestedMessageServiceStub stub) {
113+
return new NestedMessageServiceClient(stub);
114+
}
115+
116+
/**
117+
* Constructs an instance of NestedMessageServiceClient, using the given settings. This is
118+
* protected so that it is easy to make a subclass, but otherwise, the static factory methods
119+
* should be preferred.
120+
*/
121+
protected NestedMessageServiceClient(NestedMessageServiceSettings settings) throws IOException {
122+
this.settings = settings;
123+
this.stub = ((NestedMessageServiceStubSettings) settings.getStubSettings()).createStub();
124+
}
125+
126+
protected NestedMessageServiceClient(NestedMessageServiceStub stub) {
127+
this.settings = null;
128+
this.stub = stub;
129+
}
130+
131+
public final NestedMessageServiceSettings getSettings() {
132+
return settings;
133+
}
134+
135+
public NestedMessageServiceStub getStub() {
136+
return stub;
137+
}
138+
139+
// AUTO-GENERATED DOCUMENTATION AND METHOD.
140+
/**
141+
* Sample code:
142+
*
143+
* <pre>{@code
144+
* // This snippet has been automatically generated and should be regarded as a code template only.
145+
* // It will require modifications to work:
146+
* // - It may require correct/in-range values for request initialization.
147+
* // - It may require specifying regional endpoints when creating the service client as shown in
148+
* // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
149+
* try (NestedMessageServiceClient nestedMessageServiceClient =
150+
* NestedMessageServiceClient.create()) {
151+
* Outer.Middle request = Outer.Middle.newBuilder().setX(120).build();
152+
* Outer.Middle.Inner response = nestedMessageServiceClient.nestedMessageMethod(request);
153+
* }
154+
* }</pre>
155+
*
156+
* @param request The request object containing all of the parameters for the API call.
157+
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
158+
*/
159+
public final Outer.Middle.Inner nestedMessageMethod(Outer.Middle request) {
160+
return nestedMessageMethodCallable().call(request);
161+
}
162+
163+
// AUTO-GENERATED DOCUMENTATION AND METHOD.
164+
/**
165+
* Sample code:
166+
*
167+
* <pre>{@code
168+
* // This snippet has been automatically generated and should be regarded as a code template only.
169+
* // It will require modifications to work:
170+
* // - It may require correct/in-range values for request initialization.
171+
* // - It may require specifying regional endpoints when creating the service client as shown in
172+
* // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
173+
* try (NestedMessageServiceClient nestedMessageServiceClient =
174+
* NestedMessageServiceClient.create()) {
175+
* Outer.Middle request = Outer.Middle.newBuilder().setX(120).build();
176+
* ApiFuture<Outer.Middle.Inner> future =
177+
* nestedMessageServiceClient.nestedMessageMethodCallable().futureCall(request);
178+
* // Do something.
179+
* Outer.Middle.Inner response = future.get();
180+
* }
181+
* }</pre>
182+
*/
183+
public final UnaryCallable<Outer.Middle, Outer.Middle.Inner> nestedMessageMethodCallable() {
184+
return stub.nestedMessageMethodCallable();
185+
}
186+
187+
@Override
188+
public final void close() {
189+
stub.close();
190+
}
191+
192+
@Override
193+
public void shutdown() {
194+
stub.shutdown();
195+
}
196+
197+
@Override
198+
public boolean isShutdown() {
199+
return stub.isShutdown();
200+
}
201+
202+
@Override
203+
public boolean isTerminated() {
204+
return stub.isTerminated();
205+
}
206+
207+
@Override
208+
public void shutdownNow() {
209+
stub.shutdownNow();
210+
}
211+
212+
@Override
213+
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
214+
return stub.awaitTermination(duration, unit);
215+
}
216+
}

gapic-generator-java/src/test/java/com/google/api/generator/test/protoloader/TestProtoLoader.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import com.google.showcase.v1beta1.MessagingOuterClass;
4242
import com.google.showcase.v1beta1.TestingOuterClass;
4343
import com.google.testdata.v1.DeprecatedServiceOuterClass;
44+
import com.google.testgapic.v1beta1.NestedMessageProto;
45+
import com.google.types.testing.TypesTestingProto;
4446
import google.cloud.CommonResources;
4547
import java.nio.file.Path;
4648
import java.nio.file.Paths;
@@ -131,6 +133,30 @@ public GapicContext parseBookshopService() {
131133
.build();
132134
}
133135

136+
public GapicContext parseNestedMessage() {
137+
FileDescriptor fileDescriptor = TypesTestingProto.getDescriptor();
138+
ServiceDescriptor serviceDescriptor = fileDescriptor.getServices().get(0);
139+
assertEquals(serviceDescriptor.getName(), "NestedMessageService");
140+
Map<String, Message> messageTypes = Parser.parseMessages(fileDescriptor);
141+
142+
FileDescriptor messageFileDescriptor = NestedMessageProto.getDescriptor();
143+
messageTypes.putAll(Parser.parseMessages(messageFileDescriptor));
144+
145+
Map<String, ResourceName> resourceNames = new HashMap<>();
146+
Set<ResourceName> outputResourceNames = new HashSet<>();
147+
List<Service> services =
148+
Parser.parseService(
149+
fileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
150+
151+
return GapicContext.builder()
152+
.setMessages(messageTypes)
153+
.setResourceNames(resourceNames)
154+
.setServices(services)
155+
.setHelperResourceNames(outputResourceNames)
156+
.setTransport(transport)
157+
.build();
158+
}
159+
134160
public GapicContext parseShowcaseEcho() {
135161
FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
136162
ServiceDescriptor echoServiceDescriptor = echoFileDescriptor.getServices().get(0);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
syntax = "proto3";
16+
17+
import "google/api/client.proto";
18+
import "nested_message.proto";
19+
20+
package google.types.testing;
21+
22+
option java_package = "com.google.types.testing";
23+
option java_multiple_files = true;
24+
option java_outer_classname = "TypesTestingProto";
25+
26+
// A service to test handling of deeply-nested types
27+
service NestedMessageService {
28+
option (google.api.default_host) = "localhost:7469";
29+
// Testing with nested message from a different package to require imports in generated code
30+
rpc NestedMessageMethod(google.testgapic.Outer.Middle) returns (google.testgapic.Outer.Middle.Inner);
31+
}
32+

0 commit comments

Comments
 (0)