From 0313efdfdafc22c415cfe448fcd15b84e1ec2433 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Wed, 21 Apr 2021 14:17:15 -0700 Subject: [PATCH] fix: Use the right composers in deprecation unit tests --- .../MockServiceClassComposerTest.java | 2 +- .../MockServiceImplClassComposerTest.java | 2 +- .../ServiceSettingsClassComposerTest.java | 2 +- .../ServiceStubClassComposerTest.java | 2 +- .../ServiceStubSettingsClassComposerTest.java | 2 +- .../goldens/DeprecatedServiceSettings.golden | 311 ++++++-------- .../goldens/DeprecatedServiceStub.golden | 222 +--------- .../DeprecatedServiceStubSettings.golden | 404 ++++++++++-------- .../goldens/MockDeprecatedService.golden | 229 +--------- .../goldens/MockDeprecatedServiceImpl.golden | 267 +++--------- 10 files changed, 453 insertions(+), 990 deletions(-) diff --git a/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java index 073593a8f9..5126817fdb 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java @@ -43,7 +43,7 @@ public void generateServiceClasses() { public void generateServiceClasses_deprecated() { GapicContext context = TestProtoLoaderUtil.parseDeprecatedService(); Service protoService = context.services().get(0); - GapicClass clazz = ServiceClientClassComposer.instance().generate(context, protoService); + GapicClass clazz = MockServiceClassComposer.instance().generate(context, protoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); diff --git a/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java index 7cd84311aa..a4464ad1c5 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java @@ -43,7 +43,7 @@ public void generateServiceClasses() { public void generateServiceClasses_deprecated() { GapicContext context = TestProtoLoaderUtil.parseDeprecatedService(); Service protoService = context.services().get(0); - GapicClass clazz = ServiceClientClassComposer.instance().generate(context, protoService); + GapicClass clazz = MockServiceImplClassComposer.instance().generate(context, protoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java index e1f363addf..9075e0dd95 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java @@ -43,7 +43,7 @@ public void generateServiceClasses() { public void generateServiceClasses_deprecated() { GapicContext context = TestProtoLoaderUtil.parseDeprecatedService(); Service protoService = context.services().get(0); - GapicClass clazz = ServiceClientClassComposer.instance().generate(context, protoService); + GapicClass clazz = ServiceSettingsClassComposer.instance().generate(context, protoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java index 667b09583e..739f806d9c 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java @@ -43,7 +43,7 @@ public void generateServiceClasses() { public void generateServiceClasses_deprecated() { GapicContext context = TestProtoLoaderUtil.parseDeprecatedService(); Service protoService = context.services().get(0); - GapicClass clazz = ServiceClientClassComposer.instance().generate(context, protoService); + GapicClass clazz = ServiceStubClassComposer.instance().generate(context, protoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposerTest.java index 028993bb1f..33a23fac52 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceStubSettingsClassComposerTest.java @@ -79,7 +79,7 @@ public void generateServiceStubSettingsClasses_basic() throws IOException { public void generateServiceStubSettingsClasses_deprecated() throws IOException { GapicContext context = TestProtoLoaderUtil.parseDeprecatedService(); Service protoService = context.services().get(0); - GapicClass clazz = ServiceClientClassComposer.instance().generate(context, protoService); + GapicClass clazz = ServiceStubSettingsClassComposer.instance().generate(context, protoService); JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); diff --git a/src/test/java/com/google/api/generator/gapic/composer/goldens/DeprecatedServiceSettings.golden b/src/test/java/com/google/api/generator/gapic/composer/goldens/DeprecatedServiceSettings.golden index 9af83eb743..34d39be82a 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/goldens/DeprecatedServiceSettings.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/goldens/DeprecatedServiceSettings.golden @@ -1,234 +1,191 @@ package com.google.testdata.v1; +import com.google.api.core.ApiFunction; import com.google.api.core.BetaApi; -import com.google.api.gax.core.BackgroundResource; -import com.google.api.gax.rpc.UnaryCallable; +import com.google.api.gax.core.GoogleCredentialsProvider; +import com.google.api.gax.core.InstantiatingExecutorProvider; +import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; +import com.google.api.gax.rpc.ApiClientHeaderProvider; +import com.google.api.gax.rpc.ClientContext; +import com.google.api.gax.rpc.ClientSettings; +import com.google.api.gax.rpc.StubSettings; +import com.google.api.gax.rpc.TransportChannelProvider; +import com.google.api.gax.rpc.UnaryCallSettings; import com.google.protobuf.Empty; -import com.google.testdata.v1.stub.DeprecatedServiceStub; import com.google.testdata.v1.stub.DeprecatedServiceStubSettings; import java.io.IOException; -import java.util.concurrent.TimeUnit; +import java.util.List; import javax.annotation.Generated; // AUTO-GENERATED DOCUMENTATION AND CLASS. /** - * This class provides the ability to make remote calls to the backing service through method calls - * that map to API methods. Sample code to get started: + * Settings class to configure an instance of {@link DeprecatedServiceClient}. * - *
{@code
- * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
- *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
- *   deprecatedServiceClient.fastFibonacci(request);
- * }
- * }
- * - *

Note: close() needs to be called on the DeprecatedServiceClient object to clean up resources - * such as threads. In the example above, try-with-resources is used, which automatically calls - * close(). - * - *

The surface of this class includes several types of Java methods for each of the API's - * methods: + *

The default instance has everything set to sensible defaults: * - *

    - *
  1. A "flattened" method. With this type of method, the fields of the request type have been - * converted into function parameters. It may be the case that not all fields are available as - * parameters, and not every API method will have a flattened method entry point. - *
  2. A "request object" method. This type of method only takes one parameter, a request object, - * which must be constructed before the call. Not every API method will have a request object - * method. - *
  3. A "callable" method. This type of method takes no parameters and returns an immutable API - * callable object, which can be used to initiate calls to the service. - *
+ * * - *

See the individual methods for example code. + *

The builder of this class is recursive, so contained classes are themselves builders. When + * build() is called, the tree of builders is called to create the complete settings object. * - *

Many parameters require resource names to be formatted in a particular way. To assist with - * these names, this class includes a format method for each type of name, and additionally a parse - * method to extract the individual identifiers contained within names that are returned. - * - *

This class can be customized by passing in a custom instance of DeprecatedServiceSettings to - * create(). For example: - * - *

To customize credentials: + *

For example, to set the total timeout of fastFibonacci to 30 seconds: * *

{@code
- * DeprecatedServiceSettings deprecatedServiceSettings =
- *     DeprecatedServiceSettings.newBuilder()
- *         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
- *         .build();
- * DeprecatedServiceClient deprecatedServiceClient =
- *     DeprecatedServiceClient.create(deprecatedServiceSettings);
+ * DeprecatedServiceSettings.Builder deprecatedServiceSettingsBuilder =
+ *     DeprecatedServiceSettings.newBuilder();
+ * deprecatedServiceSettingsBuilder
+ *     .fastFibonacciSettings()
+ *     .setRetrySettings(
+ *         deprecatedServiceSettingsBuilder
+ *             .fastFibonacciSettings()
+ *             .getRetrySettings()
+ *             .toBuilder()
+ *             .setTotalTimeout(Duration.ofSeconds(30))
+ *             .build());
+ * DeprecatedServiceSettings deprecatedServiceSettings = deprecatedServiceSettingsBuilder.build();
  * }
* - *

To customize the endpoint: - * - *

{@code
- * DeprecatedServiceSettings deprecatedServiceSettings =
- *     DeprecatedServiceSettings.newBuilder().setEndpoint(myEndpoint).build();
- * DeprecatedServiceClient deprecatedServiceClient =
- *     DeprecatedServiceClient.create(deprecatedServiceSettings);
- * }
- * - *

Please refer to the GitHub repository's samples for more quickstart code snippets. - * * @deprecated This class is deprecated and will be removed in the next major version update. */ @Deprecated @Generated("by gapic-generator-java") -public class DeprecatedServiceClient implements BackgroundResource { - private final DeprecatedServiceSettings settings; - private final DeprecatedServiceStub stub; +public class DeprecatedServiceSettings extends ClientSettings { - /** Constructs an instance of DeprecatedServiceClient with default settings. */ - public static final DeprecatedServiceClient create() throws IOException { - return create(DeprecatedServiceSettings.newBuilder().build()); + /** Returns the object with the settings used for calls to fastFibonacci. */ + public UnaryCallSettings fastFibonacciSettings() { + return ((DeprecatedServiceStubSettings) getStubSettings()).fastFibonacciSettings(); } /** - * Constructs an instance of DeprecatedServiceClient, using the given settings. The channels are - * created based on the settings passed in, or defaults for any settings that are not set. + * Returns the object with the settings used for calls to slowFibonacci. + * + * @deprecated This method is deprecated and will be removed in the next major version update. */ - public static final DeprecatedServiceClient create(DeprecatedServiceSettings settings) - throws IOException { - return new DeprecatedServiceClient(settings); + @Deprecated + public UnaryCallSettings slowFibonacciSettings() { + return ((DeprecatedServiceStubSettings) getStubSettings()).slowFibonacciSettings(); } - /** - * Constructs an instance of DeprecatedServiceClient, using the given stub for making calls. This - * is for advanced usage - prefer using create(DeprecatedServiceSettings). - */ - @BetaApi("A restructuring of stub classes is planned, so this may break in the future") - public static final DeprecatedServiceClient create(DeprecatedServiceStub stub) { - return new DeprecatedServiceClient(stub); + public static final DeprecatedServiceSettings create(DeprecatedServiceStubSettings stub) + throws IOException { + return new DeprecatedServiceSettings.Builder(stub.toBuilder()).build(); } - /** - * Constructs an instance of DeprecatedServiceClient, using the given settings. This is protected - * so that it is easy to make a subclass, but otherwise, the static factory methods should be - * preferred. - */ - protected DeprecatedServiceClient(DeprecatedServiceSettings settings) throws IOException { - this.settings = settings; - this.stub = ((DeprecatedServiceStubSettings) settings.getStubSettings()).createStub(); + /** Returns a builder for the default ExecutorProvider for this service. */ + public static InstantiatingExecutorProvider.Builder defaultExecutorProviderBuilder() { + return DeprecatedServiceStubSettings.defaultExecutorProviderBuilder(); } - @BetaApi("A restructuring of stub classes is planned, so this may break in the future") - protected DeprecatedServiceClient(DeprecatedServiceStub stub) { - this.settings = null; - this.stub = stub; + /** Returns the default service endpoint. */ + public static String getDefaultEndpoint() { + return DeprecatedServiceStubSettings.getDefaultEndpoint(); } - public final DeprecatedServiceSettings getSettings() { - return settings; + /** Returns the default service scopes. */ + public static List getDefaultServiceScopes() { + return DeprecatedServiceStubSettings.getDefaultServiceScopes(); } - @BetaApi("A restructuring of stub classes is planned, so this may break in the future") - public DeprecatedServiceStub getStub() { - return stub; + /** Returns a builder for the default credentials for this service. */ + public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilder() { + return DeprecatedServiceStubSettings.defaultCredentialsProviderBuilder(); } - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *

{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   deprecatedServiceClient.fastFibonacci(request);
-   * }
-   * }
- * - * @param request The request object containing all of the parameters for the API call. - * @throws com.google.api.gax.rpc.ApiException if the remote call fails - */ - public final void fastFibonacci(FibonacciRequest request) { - fastFibonacciCallable().call(request); + /** Returns a builder for the default ChannelProvider for this service. */ + public static InstantiatingGrpcChannelProvider.Builder defaultGrpcTransportProviderBuilder() { + return DeprecatedServiceStubSettings.defaultGrpcTransportProviderBuilder(); } - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *
{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   ApiFuture future = deprecatedServiceClient.fastFibonacciCallable().futureCall(request);
-   *   // Do something.
-   *   future.get();
-   * }
-   * }
- */ - public final UnaryCallable fastFibonacciCallable() { - return stub.fastFibonacciCallable(); + public static TransportChannelProvider defaultTransportChannelProvider() { + return DeprecatedServiceStubSettings.defaultTransportChannelProvider(); } - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *
{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   deprecatedServiceClient.slowFibonacci(request);
-   * }
-   * }
- * - * @param request The request object containing all of the parameters for the API call. - * @throws com.google.api.gax.rpc.ApiException if the remote call fails - * @deprecated This method is deprecated and will be removed in the next major version update. - */ - @Deprecated - public final void slowFibonacci(FibonacciRequest request) { - slowFibonacciCallable().call(request); + @BetaApi("The surface for customizing headers is not stable yet and may change in the future.") + public static ApiClientHeaderProvider.Builder defaultApiClientHeaderProviderBuilder() { + return DeprecatedServiceStubSettings.defaultApiClientHeaderProviderBuilder(); } - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *
{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   ApiFuture future = deprecatedServiceClient.slowFibonacciCallable().futureCall(request);
-   *   // Do something.
-   *   future.get();
-   * }
-   * }
- * - * @deprecated This method is deprecated and will be removed in the next major version update. - */ - @Deprecated - public final UnaryCallable slowFibonacciCallable() { - return stub.slowFibonacciCallable(); + /** Returns a new builder for this class. */ + public static Builder newBuilder() { + return Builder.createDefault(); } - @Override - public final void close() { - stub.close(); + /** Returns a new builder for this class. */ + public static Builder newBuilder(ClientContext clientContext) { + return new Builder(clientContext); } - @Override - public void shutdown() { - stub.shutdown(); + /** Returns a builder containing all the values of this settings class. */ + public Builder toBuilder() { + return new Builder(this); } - @Override - public boolean isShutdown() { - return stub.isShutdown(); + protected DeprecatedServiceSettings(Builder settingsBuilder) throws IOException { + super(settingsBuilder); } - @Override - public boolean isTerminated() { - return stub.isTerminated(); - } + /** Builder for DeprecatedServiceSettings. */ + public static class Builder extends ClientSettings.Builder { - @Override - public void shutdownNow() { - stub.shutdownNow(); - } + protected Builder() throws IOException { + this(((ClientContext) null)); + } + + protected Builder(ClientContext clientContext) { + super(DeprecatedServiceStubSettings.newBuilder(clientContext)); + } + + protected Builder(DeprecatedServiceSettings settings) { + super(settings.getStubSettings().toBuilder()); + } + + protected Builder(DeprecatedServiceStubSettings.Builder stubSettings) { + super(stubSettings); + } + + private static Builder createDefault() { + return new Builder(DeprecatedServiceStubSettings.newBuilder()); + } + + public DeprecatedServiceStubSettings.Builder getStubSettingsBuilder() { + return ((DeprecatedServiceStubSettings.Builder) getStubSettings()); + } + + // NEXT_MAJOR_VER: remove 'throws Exception'. + /** + * Applies the given settings updater function to all of the unary API methods in this service. + * + *

Note: This method does not support applying settings to streaming methods. + */ + public Builder applyToAllUnaryMethods( + ApiFunction, Void> settingsUpdater) throws Exception { + super.applyToAllUnaryMethods( + getStubSettingsBuilder().unaryMethodSettingsBuilders(), settingsUpdater); + return this; + } + + /** Returns the builder for the settings used for calls to fastFibonacci. */ + public UnaryCallSettings.Builder fastFibonacciSettings() { + return getStubSettingsBuilder().fastFibonacciSettings(); + } + + /** + * Returns the builder for the settings used for calls to slowFibonacci. + * + * @deprecated This method is deprecated and will be removed in the next major version update. + */ + @Deprecated + public UnaryCallSettings.Builder slowFibonacciSettings() { + return getStubSettingsBuilder().slowFibonacciSettings(); + } - @Override - public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { - return stub.awaitTermination(duration, unit); + @Override + public DeprecatedServiceSettings build() throws IOException { + return new DeprecatedServiceSettings(this); + } } } diff --git a/src/test/java/com/google/api/generator/gapic/composer/goldens/DeprecatedServiceStub.golden b/src/test/java/com/google/api/generator/gapic/composer/goldens/DeprecatedServiceStub.golden index 9af83eb743..6a40197ba4 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/goldens/DeprecatedServiceStub.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/goldens/DeprecatedServiceStub.golden @@ -1,234 +1,32 @@ -package com.google.testdata.v1; +package com.google.testdata.v1.stub; -import com.google.api.core.BetaApi; import com.google.api.gax.core.BackgroundResource; import com.google.api.gax.rpc.UnaryCallable; import com.google.protobuf.Empty; -import com.google.testdata.v1.stub.DeprecatedServiceStub; -import com.google.testdata.v1.stub.DeprecatedServiceStubSettings; -import java.io.IOException; -import java.util.concurrent.TimeUnit; +import com.google.testdata.v1.FibonacciRequest; import javax.annotation.Generated; // AUTO-GENERATED DOCUMENTATION AND CLASS. /** - * This class provides the ability to make remote calls to the backing service through method calls - * that map to API methods. Sample code to get started: + * Base stub class for the DeprecatedService service API. * - *

{@code
- * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
- *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
- *   deprecatedServiceClient.fastFibonacci(request);
- * }
- * }
- * - *

Note: close() needs to be called on the DeprecatedServiceClient object to clean up resources - * such as threads. In the example above, try-with-resources is used, which automatically calls - * close(). - * - *

The surface of this class includes several types of Java methods for each of the API's - * methods: - * - *

    - *
  1. A "flattened" method. With this type of method, the fields of the request type have been - * converted into function parameters. It may be the case that not all fields are available as - * parameters, and not every API method will have a flattened method entry point. - *
  2. A "request object" method. This type of method only takes one parameter, a request object, - * which must be constructed before the call. Not every API method will have a request object - * method. - *
  3. A "callable" method. This type of method takes no parameters and returns an immutable API - * callable object, which can be used to initiate calls to the service. - *
- * - *

See the individual methods for example code. - * - *

Many parameters require resource names to be formatted in a particular way. To assist with - * these names, this class includes a format method for each type of name, and additionally a parse - * method to extract the individual identifiers contained within names that are returned. - * - *

This class can be customized by passing in a custom instance of DeprecatedServiceSettings to - * create(). For example: - * - *

To customize credentials: - * - *

{@code
- * DeprecatedServiceSettings deprecatedServiceSettings =
- *     DeprecatedServiceSettings.newBuilder()
- *         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
- *         .build();
- * DeprecatedServiceClient deprecatedServiceClient =
- *     DeprecatedServiceClient.create(deprecatedServiceSettings);
- * }
- * - *

To customize the endpoint: - * - *

{@code
- * DeprecatedServiceSettings deprecatedServiceSettings =
- *     DeprecatedServiceSettings.newBuilder().setEndpoint(myEndpoint).build();
- * DeprecatedServiceClient deprecatedServiceClient =
- *     DeprecatedServiceClient.create(deprecatedServiceSettings);
- * }
- * - *

Please refer to the GitHub repository's samples for more quickstart code snippets. + *

This class is for advanced usage and reflects the underlying API directly. * * @deprecated This class is deprecated and will be removed in the next major version update. */ @Deprecated @Generated("by gapic-generator-java") -public class DeprecatedServiceClient implements BackgroundResource { - private final DeprecatedServiceSettings settings; - private final DeprecatedServiceStub stub; - - /** Constructs an instance of DeprecatedServiceClient with default settings. */ - public static final DeprecatedServiceClient create() throws IOException { - return create(DeprecatedServiceSettings.newBuilder().build()); - } - - /** - * Constructs an instance of DeprecatedServiceClient, using the given settings. The channels are - * created based on the settings passed in, or defaults for any settings that are not set. - */ - public static final DeprecatedServiceClient create(DeprecatedServiceSettings settings) - throws IOException { - return new DeprecatedServiceClient(settings); - } - - /** - * Constructs an instance of DeprecatedServiceClient, using the given stub for making calls. This - * is for advanced usage - prefer using create(DeprecatedServiceSettings). - */ - @BetaApi("A restructuring of stub classes is planned, so this may break in the future") - public static final DeprecatedServiceClient create(DeprecatedServiceStub stub) { - return new DeprecatedServiceClient(stub); - } - - /** - * Constructs an instance of DeprecatedServiceClient, using the given settings. This is protected - * so that it is easy to make a subclass, but otherwise, the static factory methods should be - * preferred. - */ - protected DeprecatedServiceClient(DeprecatedServiceSettings settings) throws IOException { - this.settings = settings; - this.stub = ((DeprecatedServiceStubSettings) settings.getStubSettings()).createStub(); - } - - @BetaApi("A restructuring of stub classes is planned, so this may break in the future") - protected DeprecatedServiceClient(DeprecatedServiceStub stub) { - this.settings = null; - this.stub = stub; - } - - public final DeprecatedServiceSettings getSettings() { - return settings; - } +public abstract class DeprecatedServiceStub implements BackgroundResource { - @BetaApi("A restructuring of stub classes is planned, so this may break in the future") - public DeprecatedServiceStub getStub() { - return stub; + public UnaryCallable fastFibonacciCallable() { + throw new UnsupportedOperationException("Not implemented: fastFibonacciCallable()"); } - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *

{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   deprecatedServiceClient.fastFibonacci(request);
-   * }
-   * }
- * - * @param request The request object containing all of the parameters for the API call. - * @throws com.google.api.gax.rpc.ApiException if the remote call fails - */ - public final void fastFibonacci(FibonacciRequest request) { - fastFibonacciCallable().call(request); - } - - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *
{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   ApiFuture future = deprecatedServiceClient.fastFibonacciCallable().futureCall(request);
-   *   // Do something.
-   *   future.get();
-   * }
-   * }
- */ - public final UnaryCallable fastFibonacciCallable() { - return stub.fastFibonacciCallable(); - } - - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *
{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   deprecatedServiceClient.slowFibonacci(request);
-   * }
-   * }
- * - * @param request The request object containing all of the parameters for the API call. - * @throws com.google.api.gax.rpc.ApiException if the remote call fails - * @deprecated This method is deprecated and will be removed in the next major version update. - */ - @Deprecated - public final void slowFibonacci(FibonacciRequest request) { - slowFibonacciCallable().call(request); - } - - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *
{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   ApiFuture future = deprecatedServiceClient.slowFibonacciCallable().futureCall(request);
-   *   // Do something.
-   *   future.get();
-   * }
-   * }
- * - * @deprecated This method is deprecated and will be removed in the next major version update. - */ @Deprecated - public final UnaryCallable slowFibonacciCallable() { - return stub.slowFibonacciCallable(); - } - - @Override - public final void close() { - stub.close(); + public UnaryCallable slowFibonacciCallable() { + throw new UnsupportedOperationException("Not implemented: slowFibonacciCallable()"); } @Override - public void shutdown() { - stub.shutdown(); - } - - @Override - public boolean isShutdown() { - return stub.isShutdown(); - } - - @Override - public boolean isTerminated() { - return stub.isTerminated(); - } - - @Override - public void shutdownNow() { - stub.shutdownNow(); - } - - @Override - public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { - return stub.awaitTermination(duration, unit); - } + public abstract void close(); } diff --git a/src/test/java/com/google/api/generator/gapic/composer/goldens/DeprecatedServiceStubSettings.golden b/src/test/java/com/google/api/generator/gapic/composer/goldens/DeprecatedServiceStubSettings.golden index 9af83eb743..6e44ff4bd2 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/goldens/DeprecatedServiceStubSettings.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/goldens/DeprecatedServiceStubSettings.golden @@ -1,234 +1,282 @@ -package com.google.testdata.v1; +package com.google.testdata.v1.stub; +import com.google.api.core.ApiFunction; import com.google.api.core.BetaApi; -import com.google.api.gax.core.BackgroundResource; -import com.google.api.gax.rpc.UnaryCallable; +import com.google.api.gax.core.GaxProperties; +import com.google.api.gax.core.GoogleCredentialsProvider; +import com.google.api.gax.core.InstantiatingExecutorProvider; +import com.google.api.gax.grpc.GaxGrpcProperties; +import com.google.api.gax.grpc.GrpcTransportChannel; +import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; +import com.google.api.gax.retrying.RetrySettings; +import com.google.api.gax.rpc.ApiClientHeaderProvider; +import com.google.api.gax.rpc.ClientContext; +import com.google.api.gax.rpc.StatusCode; +import com.google.api.gax.rpc.StubSettings; +import com.google.api.gax.rpc.TransportChannelProvider; +import com.google.api.gax.rpc.UnaryCallSettings; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; import com.google.protobuf.Empty; -import com.google.testdata.v1.stub.DeprecatedServiceStub; -import com.google.testdata.v1.stub.DeprecatedServiceStubSettings; +import com.google.testdata.v1.FibonacciRequest; import java.io.IOException; -import java.util.concurrent.TimeUnit; +import java.util.List; import javax.annotation.Generated; +import org.threeten.bp.Duration; // AUTO-GENERATED DOCUMENTATION AND CLASS. /** - * This class provides the ability to make remote calls to the backing service through method calls - * that map to API methods. Sample code to get started: + * Settings class to configure an instance of {@link DeprecatedServiceStub}. * - *
{@code
- * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
- *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
- *   deprecatedServiceClient.fastFibonacci(request);
- * }
- * }
- * - *

Note: close() needs to be called on the DeprecatedServiceClient object to clean up resources - * such as threads. In the example above, try-with-resources is used, which automatically calls - * close(). - * - *

The surface of this class includes several types of Java methods for each of the API's - * methods: - * - *

    - *
  1. A "flattened" method. With this type of method, the fields of the request type have been - * converted into function parameters. It may be the case that not all fields are available as - * parameters, and not every API method will have a flattened method entry point. - *
  2. A "request object" method. This type of method only takes one parameter, a request object, - * which must be constructed before the call. Not every API method will have a request object - * method. - *
  3. A "callable" method. This type of method takes no parameters and returns an immutable API - * callable object, which can be used to initiate calls to the service. - *
- * - *

See the individual methods for example code. - * - *

Many parameters require resource names to be formatted in a particular way. To assist with - * these names, this class includes a format method for each type of name, and additionally a parse - * method to extract the individual identifiers contained within names that are returned. - * - *

This class can be customized by passing in a custom instance of DeprecatedServiceSettings to - * create(). For example: + *

The default instance has everything set to sensible defaults: * - *

To customize credentials: + *

    + *
  • The default service address (localhost) and default port (7469) are used. + *
  • Credentials are acquired automatically through Application Default Credentials. + *
  • Retries are configured for idempotent methods but not for non-idempotent methods. + *
* - *
{@code
- * DeprecatedServiceSettings deprecatedServiceSettings =
- *     DeprecatedServiceSettings.newBuilder()
- *         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
- *         .build();
- * DeprecatedServiceClient deprecatedServiceClient =
- *     DeprecatedServiceClient.create(deprecatedServiceSettings);
- * }
+ *

The builder of this class is recursive, so contained classes are themselves builders. When + * build() is called, the tree of builders is called to create the complete settings object. * - *

To customize the endpoint: + *

For example, to set the total timeout of fastFibonacci to 30 seconds: * *

{@code
- * DeprecatedServiceSettings deprecatedServiceSettings =
- *     DeprecatedServiceSettings.newBuilder().setEndpoint(myEndpoint).build();
- * DeprecatedServiceClient deprecatedServiceClient =
- *     DeprecatedServiceClient.create(deprecatedServiceSettings);
+ * DeprecatedServiceStubSettings.Builder deprecatedServiceSettingsBuilder =
+ *     DeprecatedServiceStubSettings.newBuilder();
+ * deprecatedServiceSettingsBuilder
+ *     .fastFibonacciSettings()
+ *     .setRetrySettings(
+ *         deprecatedServiceSettingsBuilder
+ *             .fastFibonacciSettings()
+ *             .getRetrySettings()
+ *             .toBuilder()
+ *             .setTotalTimeout(Duration.ofSeconds(30))
+ *             .build());
+ * DeprecatedServiceStubSettings deprecatedServiceSettings =
+ *     deprecatedServiceSettingsBuilder.build();
  * }
* - *

Please refer to the GitHub repository's samples for more quickstart code snippets. - * * @deprecated This class is deprecated and will be removed in the next major version update. */ @Deprecated @Generated("by gapic-generator-java") -public class DeprecatedServiceClient implements BackgroundResource { - private final DeprecatedServiceSettings settings; - private final DeprecatedServiceStub stub; +public class DeprecatedServiceStubSettings extends StubSettings { + /** The default scopes of the service. */ + private static final ImmutableList DEFAULT_SERVICE_SCOPES = + ImmutableList.builder().add("https://www.googleapis.com/auth/cloud-platform").build(); + + private final UnaryCallSettings fastFibonacciSettings; + private final UnaryCallSettings slowFibonacciSettings; - /** Constructs an instance of DeprecatedServiceClient with default settings. */ - public static final DeprecatedServiceClient create() throws IOException { - return create(DeprecatedServiceSettings.newBuilder().build()); + /** Returns the object with the settings used for calls to fastFibonacci. */ + public UnaryCallSettings fastFibonacciSettings() { + return fastFibonacciSettings; } /** - * Constructs an instance of DeprecatedServiceClient, using the given settings. The channels are - * created based on the settings passed in, or defaults for any settings that are not set. + * Returns the object with the settings used for calls to slowFibonacci. + * + * @deprecated This method is deprecated and will be removed in the next major version update. */ - public static final DeprecatedServiceClient create(DeprecatedServiceSettings settings) - throws IOException { - return new DeprecatedServiceClient(settings); + @Deprecated + public UnaryCallSettings slowFibonacciSettings() { + return slowFibonacciSettings; } - /** - * Constructs an instance of DeprecatedServiceClient, using the given stub for making calls. This - * is for advanced usage - prefer using create(DeprecatedServiceSettings). - */ @BetaApi("A restructuring of stub classes is planned, so this may break in the future") - public static final DeprecatedServiceClient create(DeprecatedServiceStub stub) { - return new DeprecatedServiceClient(stub); + public DeprecatedServiceStub createStub() throws IOException { + if (getTransportChannelProvider() + .getTransportName() + .equals(GrpcTransportChannel.getGrpcTransportName())) { + return GrpcDeprecatedServiceStub.create(this); + } + throw new UnsupportedOperationException( + String.format( + "Transport not supported: %s", getTransportChannelProvider().getTransportName())); } - /** - * Constructs an instance of DeprecatedServiceClient, using the given settings. This is protected - * so that it is easy to make a subclass, but otherwise, the static factory methods should be - * preferred. - */ - protected DeprecatedServiceClient(DeprecatedServiceSettings settings) throws IOException { - this.settings = settings; - this.stub = ((DeprecatedServiceStubSettings) settings.getStubSettings()).createStub(); + /** Returns a builder for the default ExecutorProvider for this service. */ + public static InstantiatingExecutorProvider.Builder defaultExecutorProviderBuilder() { + return InstantiatingExecutorProvider.newBuilder(); } - @BetaApi("A restructuring of stub classes is planned, so this may break in the future") - protected DeprecatedServiceClient(DeprecatedServiceStub stub) { - this.settings = null; - this.stub = stub; + /** Returns the default service endpoint. */ + public static String getDefaultEndpoint() { + return "localhost:7469"; } - public final DeprecatedServiceSettings getSettings() { - return settings; + /** Returns the default service scopes. */ + public static List getDefaultServiceScopes() { + return DEFAULT_SERVICE_SCOPES; } - @BetaApi("A restructuring of stub classes is planned, so this may break in the future") - public DeprecatedServiceStub getStub() { - return stub; + /** Returns a builder for the default credentials for this service. */ + public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilder() { + return GoogleCredentialsProvider.newBuilder().setScopesToApply(DEFAULT_SERVICE_SCOPES); } - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *

{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   deprecatedServiceClient.fastFibonacci(request);
-   * }
-   * }
- * - * @param request The request object containing all of the parameters for the API call. - * @throws com.google.api.gax.rpc.ApiException if the remote call fails - */ - public final void fastFibonacci(FibonacciRequest request) { - fastFibonacciCallable().call(request); + /** Returns a builder for the default ChannelProvider for this service. */ + public static InstantiatingGrpcChannelProvider.Builder defaultGrpcTransportProviderBuilder() { + return InstantiatingGrpcChannelProvider.newBuilder() + .setMaxInboundMessageSize(Integer.MAX_VALUE); } - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *
{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   ApiFuture future = deprecatedServiceClient.fastFibonacciCallable().futureCall(request);
-   *   // Do something.
-   *   future.get();
-   * }
-   * }
- */ - public final UnaryCallable fastFibonacciCallable() { - return stub.fastFibonacciCallable(); + public static TransportChannelProvider defaultTransportChannelProvider() { + return defaultGrpcTransportProviderBuilder().build(); } - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *
{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   deprecatedServiceClient.slowFibonacci(request);
-   * }
-   * }
- * - * @param request The request object containing all of the parameters for the API call. - * @throws com.google.api.gax.rpc.ApiException if the remote call fails - * @deprecated This method is deprecated and will be removed in the next major version update. - */ - @Deprecated - public final void slowFibonacci(FibonacciRequest request) { - slowFibonacciCallable().call(request); + @BetaApi("The surface for customizing headers is not stable yet and may change in the future.") + public static ApiClientHeaderProvider.Builder defaultApiClientHeaderProviderBuilder() { + return ApiClientHeaderProvider.newBuilder() + .setGeneratedLibToken( + "gapic", GaxProperties.getLibraryVersion(DeprecatedServiceStubSettings.class)) + .setTransportToken( + GaxGrpcProperties.getGrpcTokenName(), GaxGrpcProperties.getGrpcVersion()); } - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *
{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   ApiFuture future = deprecatedServiceClient.slowFibonacciCallable().futureCall(request);
-   *   // Do something.
-   *   future.get();
-   * }
-   * }
- * - * @deprecated This method is deprecated and will be removed in the next major version update. - */ - @Deprecated - public final UnaryCallable slowFibonacciCallable() { - return stub.slowFibonacciCallable(); + /** Returns a new builder for this class. */ + public static Builder newBuilder() { + return Builder.createDefault(); } - @Override - public final void close() { - stub.close(); + /** Returns a new builder for this class. */ + public static Builder newBuilder(ClientContext clientContext) { + return new Builder(clientContext); } - @Override - public void shutdown() { - stub.shutdown(); + /** Returns a builder containing all the values of this settings class. */ + public Builder toBuilder() { + return new Builder(this); } - @Override - public boolean isShutdown() { - return stub.isShutdown(); - } + protected DeprecatedServiceStubSettings(Builder settingsBuilder) throws IOException { + super(settingsBuilder); - @Override - public boolean isTerminated() { - return stub.isTerminated(); + fastFibonacciSettings = settingsBuilder.fastFibonacciSettings().build(); + slowFibonacciSettings = settingsBuilder.slowFibonacciSettings().build(); } - @Override - public void shutdownNow() { - stub.shutdownNow(); - } + /** Builder for DeprecatedServiceStubSettings. */ + public static class Builder extends StubSettings.Builder { + private final ImmutableList> unaryMethodSettingsBuilders; + private final UnaryCallSettings.Builder fastFibonacciSettings; + private final UnaryCallSettings.Builder slowFibonacciSettings; + private static final ImmutableMap> + RETRYABLE_CODE_DEFINITIONS; + + static { + ImmutableMap.Builder> definitions = + ImmutableMap.builder(); + definitions.put( + "no_retry_0_codes", ImmutableSet.copyOf(Lists.newArrayList())); + RETRYABLE_CODE_DEFINITIONS = definitions.build(); + } + + private static final ImmutableMap RETRY_PARAM_DEFINITIONS; + + static { + ImmutableMap.Builder definitions = ImmutableMap.builder(); + RetrySettings settings = null; + settings = + RetrySettings.newBuilder() + .setInitialRpcTimeout(Duration.ofMillis(60000L)) + .setRpcTimeoutMultiplier(1.0) + .setMaxRpcTimeout(Duration.ofMillis(60000L)) + .setTotalTimeout(Duration.ofMillis(60000L)) + .build(); + definitions.put("no_retry_0_params", settings); + RETRY_PARAM_DEFINITIONS = definitions.build(); + } + + protected Builder() { + this(((ClientContext) null)); + } + + protected Builder(ClientContext clientContext) { + super(clientContext); + + fastFibonacciSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); + slowFibonacciSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); + + unaryMethodSettingsBuilders = + ImmutableList.>of( + fastFibonacciSettings, slowFibonacciSettings); + initDefaults(this); + } + + protected Builder(DeprecatedServiceStubSettings settings) { + super(settings); + + fastFibonacciSettings = settings.fastFibonacciSettings.toBuilder(); + slowFibonacciSettings = settings.slowFibonacciSettings.toBuilder(); + + unaryMethodSettingsBuilders = + ImmutableList.>of( + fastFibonacciSettings, slowFibonacciSettings); + } + + private static Builder createDefault() { + Builder builder = new Builder(((ClientContext) null)); + + builder.setTransportChannelProvider(defaultTransportChannelProvider()); + builder.setCredentialsProvider(defaultCredentialsProviderBuilder().build()); + builder.setInternalHeaderProvider(defaultApiClientHeaderProviderBuilder().build()); + builder.setEndpoint(getDefaultEndpoint()); + + return initDefaults(builder); + } + + private static Builder initDefaults(Builder builder) { + builder + .fastFibonacciSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("no_retry_0_codes")) + .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("no_retry_0_params")); + + builder + .slowFibonacciSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("no_retry_0_codes")) + .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("no_retry_0_params")); + + return builder; + } + + // NEXT_MAJOR_VER: remove 'throws Exception'. + /** + * Applies the given settings updater function to all of the unary API methods in this service. + * + *

Note: This method does not support applying settings to streaming methods. + */ + public Builder applyToAllUnaryMethods( + ApiFunction, Void> settingsUpdater) throws Exception { + super.applyToAllUnaryMethods(unaryMethodSettingsBuilders, settingsUpdater); + return this; + } + + public ImmutableList> unaryMethodSettingsBuilders() { + return unaryMethodSettingsBuilders; + } + + /** Returns the builder for the settings used for calls to fastFibonacci. */ + public UnaryCallSettings.Builder fastFibonacciSettings() { + return fastFibonacciSettings; + } + + /** + * Returns the builder for the settings used for calls to slowFibonacci. + * + * @deprecated This method is deprecated and will be removed in the next major version update. + */ + @Deprecated + public UnaryCallSettings.Builder slowFibonacciSettings() { + return slowFibonacciSettings; + } - @Override - public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { - return stub.awaitTermination(duration, unit); + @Override + public DeprecatedServiceStubSettings build() throws IOException { + return new DeprecatedServiceStubSettings(this); + } } } diff --git a/src/test/java/com/google/api/generator/gapic/composer/goldens/MockDeprecatedService.golden b/src/test/java/com/google/api/generator/gapic/composer/goldens/MockDeprecatedService.golden index 9af83eb743..f189549919 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/goldens/MockDeprecatedService.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/goldens/MockDeprecatedService.golden @@ -1,234 +1,43 @@ package com.google.testdata.v1; import com.google.api.core.BetaApi; -import com.google.api.gax.core.BackgroundResource; -import com.google.api.gax.rpc.UnaryCallable; -import com.google.protobuf.Empty; -import com.google.testdata.v1.stub.DeprecatedServiceStub; -import com.google.testdata.v1.stub.DeprecatedServiceStubSettings; -import java.io.IOException; -import java.util.concurrent.TimeUnit; +import com.google.api.gax.grpc.testing.MockGrpcService; +import com.google.protobuf.AbstractMessage; +import io.grpc.ServerServiceDefinition; +import java.util.List; import javax.annotation.Generated; -// AUTO-GENERATED DOCUMENTATION AND CLASS. -/** - * This class provides the ability to make remote calls to the backing service through method calls - * that map to API methods. Sample code to get started: - * - *

{@code
- * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
- *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
- *   deprecatedServiceClient.fastFibonacci(request);
- * }
- * }
- * - *

Note: close() needs to be called on the DeprecatedServiceClient object to clean up resources - * such as threads. In the example above, try-with-resources is used, which automatically calls - * close(). - * - *

The surface of this class includes several types of Java methods for each of the API's - * methods: - * - *

    - *
  1. A "flattened" method. With this type of method, the fields of the request type have been - * converted into function parameters. It may be the case that not all fields are available as - * parameters, and not every API method will have a flattened method entry point. - *
  2. A "request object" method. This type of method only takes one parameter, a request object, - * which must be constructed before the call. Not every API method will have a request object - * method. - *
  3. A "callable" method. This type of method takes no parameters and returns an immutable API - * callable object, which can be used to initiate calls to the service. - *
- * - *

See the individual methods for example code. - * - *

Many parameters require resource names to be formatted in a particular way. To assist with - * these names, this class includes a format method for each type of name, and additionally a parse - * method to extract the individual identifiers contained within names that are returned. - * - *

This class can be customized by passing in a custom instance of DeprecatedServiceSettings to - * create(). For example: - * - *

To customize credentials: - * - *

{@code
- * DeprecatedServiceSettings deprecatedServiceSettings =
- *     DeprecatedServiceSettings.newBuilder()
- *         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
- *         .build();
- * DeprecatedServiceClient deprecatedServiceClient =
- *     DeprecatedServiceClient.create(deprecatedServiceSettings);
- * }
- * - *

To customize the endpoint: - * - *

{@code
- * DeprecatedServiceSettings deprecatedServiceSettings =
- *     DeprecatedServiceSettings.newBuilder().setEndpoint(myEndpoint).build();
- * DeprecatedServiceClient deprecatedServiceClient =
- *     DeprecatedServiceClient.create(deprecatedServiceSettings);
- * }
- * - *

Please refer to the GitHub repository's samples for more quickstart code snippets. - * - * @deprecated This class is deprecated and will be removed in the next major version update. - */ -@Deprecated +@BetaApi @Generated("by gapic-generator-java") -public class DeprecatedServiceClient implements BackgroundResource { - private final DeprecatedServiceSettings settings; - private final DeprecatedServiceStub stub; +public class MockDeprecatedService implements MockGrpcService { + private final MockDeprecatedServiceImpl serviceImpl; - /** Constructs an instance of DeprecatedServiceClient with default settings. */ - public static final DeprecatedServiceClient create() throws IOException { - return create(DeprecatedServiceSettings.newBuilder().build()); - } - - /** - * Constructs an instance of DeprecatedServiceClient, using the given settings. The channels are - * created based on the settings passed in, or defaults for any settings that are not set. - */ - public static final DeprecatedServiceClient create(DeprecatedServiceSettings settings) - throws IOException { - return new DeprecatedServiceClient(settings); - } - - /** - * Constructs an instance of DeprecatedServiceClient, using the given stub for making calls. This - * is for advanced usage - prefer using create(DeprecatedServiceSettings). - */ - @BetaApi("A restructuring of stub classes is planned, so this may break in the future") - public static final DeprecatedServiceClient create(DeprecatedServiceStub stub) { - return new DeprecatedServiceClient(stub); - } - - /** - * Constructs an instance of DeprecatedServiceClient, using the given settings. This is protected - * so that it is easy to make a subclass, but otherwise, the static factory methods should be - * preferred. - */ - protected DeprecatedServiceClient(DeprecatedServiceSettings settings) throws IOException { - this.settings = settings; - this.stub = ((DeprecatedServiceStubSettings) settings.getStubSettings()).createStub(); - } - - @BetaApi("A restructuring of stub classes is planned, so this may break in the future") - protected DeprecatedServiceClient(DeprecatedServiceStub stub) { - this.settings = null; - this.stub = stub; - } - - public final DeprecatedServiceSettings getSettings() { - return settings; - } - - @BetaApi("A restructuring of stub classes is planned, so this may break in the future") - public DeprecatedServiceStub getStub() { - return stub; - } - - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *

{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   deprecatedServiceClient.fastFibonacci(request);
-   * }
-   * }
- * - * @param request The request object containing all of the parameters for the API call. - * @throws com.google.api.gax.rpc.ApiException if the remote call fails - */ - public final void fastFibonacci(FibonacciRequest request) { - fastFibonacciCallable().call(request); - } - - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *
{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   ApiFuture future = deprecatedServiceClient.fastFibonacciCallable().futureCall(request);
-   *   // Do something.
-   *   future.get();
-   * }
-   * }
- */ - public final UnaryCallable fastFibonacciCallable() { - return stub.fastFibonacciCallable(); - } - - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *
{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   deprecatedServiceClient.slowFibonacci(request);
-   * }
-   * }
- * - * @param request The request object containing all of the parameters for the API call. - * @throws com.google.api.gax.rpc.ApiException if the remote call fails - * @deprecated This method is deprecated and will be removed in the next major version update. - */ - @Deprecated - public final void slowFibonacci(FibonacciRequest request) { - slowFibonacciCallable().call(request); - } - - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *
{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   ApiFuture future = deprecatedServiceClient.slowFibonacciCallable().futureCall(request);
-   *   // Do something.
-   *   future.get();
-   * }
-   * }
- * - * @deprecated This method is deprecated and will be removed in the next major version update. - */ - @Deprecated - public final UnaryCallable slowFibonacciCallable() { - return stub.slowFibonacciCallable(); - } - - @Override - public final void close() { - stub.close(); + public MockDeprecatedService() { + serviceImpl = new MockDeprecatedServiceImpl(); } @Override - public void shutdown() { - stub.shutdown(); + public List getRequests() { + return serviceImpl.getRequests(); } @Override - public boolean isShutdown() { - return stub.isShutdown(); + public void addResponse(AbstractMessage response) { + serviceImpl.addResponse(response); } @Override - public boolean isTerminated() { - return stub.isTerminated(); + public void addException(Exception exception) { + serviceImpl.addException(exception); } @Override - public void shutdownNow() { - stub.shutdownNow(); + public ServerServiceDefinition getServiceDefinition() { + return serviceImpl.bindService(); } @Override - public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { - return stub.awaitTermination(duration, unit); + public void reset() { + serviceImpl.reset(); } } diff --git a/src/test/java/com/google/api/generator/gapic/composer/goldens/MockDeprecatedServiceImpl.golden b/src/test/java/com/google/api/generator/gapic/composer/goldens/MockDeprecatedServiceImpl.golden index 9af83eb743..f400f9aa20 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/goldens/MockDeprecatedServiceImpl.golden +++ b/src/test/java/com/google/api/generator/gapic/composer/goldens/MockDeprecatedServiceImpl.golden @@ -1,234 +1,85 @@ package com.google.testdata.v1; import com.google.api.core.BetaApi; -import com.google.api.gax.core.BackgroundResource; -import com.google.api.gax.rpc.UnaryCallable; +import com.google.protobuf.AbstractMessage; import com.google.protobuf.Empty; -import com.google.testdata.v1.stub.DeprecatedServiceStub; -import com.google.testdata.v1.stub.DeprecatedServiceStubSettings; -import java.io.IOException; -import java.util.concurrent.TimeUnit; +import com.google.testdata.v1.DeprecatedServiceGrpc.DeprecatedServiceImplBase; +import io.grpc.stub.StreamObserver; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; import javax.annotation.Generated; -// AUTO-GENERATED DOCUMENTATION AND CLASS. -/** - * This class provides the ability to make remote calls to the backing service through method calls - * that map to API methods. Sample code to get started: - * - *
{@code
- * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
- *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
- *   deprecatedServiceClient.fastFibonacci(request);
- * }
- * }
- * - *

Note: close() needs to be called on the DeprecatedServiceClient object to clean up resources - * such as threads. In the example above, try-with-resources is used, which automatically calls - * close(). - * - *

The surface of this class includes several types of Java methods for each of the API's - * methods: - * - *

    - *
  1. A "flattened" method. With this type of method, the fields of the request type have been - * converted into function parameters. It may be the case that not all fields are available as - * parameters, and not every API method will have a flattened method entry point. - *
  2. A "request object" method. This type of method only takes one parameter, a request object, - * which must be constructed before the call. Not every API method will have a request object - * method. - *
  3. A "callable" method. This type of method takes no parameters and returns an immutable API - * callable object, which can be used to initiate calls to the service. - *
- * - *

See the individual methods for example code. - * - *

Many parameters require resource names to be formatted in a particular way. To assist with - * these names, this class includes a format method for each type of name, and additionally a parse - * method to extract the individual identifiers contained within names that are returned. - * - *

This class can be customized by passing in a custom instance of DeprecatedServiceSettings to - * create(). For example: - * - *

To customize credentials: - * - *

{@code
- * DeprecatedServiceSettings deprecatedServiceSettings =
- *     DeprecatedServiceSettings.newBuilder()
- *         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
- *         .build();
- * DeprecatedServiceClient deprecatedServiceClient =
- *     DeprecatedServiceClient.create(deprecatedServiceSettings);
- * }
- * - *

To customize the endpoint: - * - *

{@code
- * DeprecatedServiceSettings deprecatedServiceSettings =
- *     DeprecatedServiceSettings.newBuilder().setEndpoint(myEndpoint).build();
- * DeprecatedServiceClient deprecatedServiceClient =
- *     DeprecatedServiceClient.create(deprecatedServiceSettings);
- * }
- * - *

Please refer to the GitHub repository's samples for more quickstart code snippets. - * - * @deprecated This class is deprecated and will be removed in the next major version update. - */ -@Deprecated +@BetaApi @Generated("by gapic-generator-java") -public class DeprecatedServiceClient implements BackgroundResource { - private final DeprecatedServiceSettings settings; - private final DeprecatedServiceStub stub; +public class MockDeprecatedServiceImpl extends DeprecatedServiceImplBase { + private List requests; + private Queue responses; - /** Constructs an instance of DeprecatedServiceClient with default settings. */ - public static final DeprecatedServiceClient create() throws IOException { - return create(DeprecatedServiceSettings.newBuilder().build()); + public MockDeprecatedServiceImpl() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); } - /** - * Constructs an instance of DeprecatedServiceClient, using the given settings. The channels are - * created based on the settings passed in, or defaults for any settings that are not set. - */ - public static final DeprecatedServiceClient create(DeprecatedServiceSettings settings) - throws IOException { - return new DeprecatedServiceClient(settings); + public List getRequests() { + return requests; } - /** - * Constructs an instance of DeprecatedServiceClient, using the given stub for making calls. This - * is for advanced usage - prefer using create(DeprecatedServiceSettings). - */ - @BetaApi("A restructuring of stub classes is planned, so this may break in the future") - public static final DeprecatedServiceClient create(DeprecatedServiceStub stub) { - return new DeprecatedServiceClient(stub); + public void addResponse(AbstractMessage response) { + responses.add(response); } - /** - * Constructs an instance of DeprecatedServiceClient, using the given settings. This is protected - * so that it is easy to make a subclass, but otherwise, the static factory methods should be - * preferred. - */ - protected DeprecatedServiceClient(DeprecatedServiceSettings settings) throws IOException { - this.settings = settings; - this.stub = ((DeprecatedServiceStubSettings) settings.getStubSettings()).createStub(); + public void setResponses(List responses) { + this.responses = new LinkedList(responses); } - @BetaApi("A restructuring of stub classes is planned, so this may break in the future") - protected DeprecatedServiceClient(DeprecatedServiceStub stub) { - this.settings = null; - this.stub = stub; + public void addException(Exception exception) { + responses.add(exception); } - public final DeprecatedServiceSettings getSettings() { - return settings; - } - - @BetaApi("A restructuring of stub classes is planned, so this may break in the future") - public DeprecatedServiceStub getStub() { - return stub; - } - - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *
{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   deprecatedServiceClient.fastFibonacci(request);
-   * }
-   * }
- * - * @param request The request object containing all of the parameters for the API call. - * @throws com.google.api.gax.rpc.ApiException if the remote call fails - */ - public final void fastFibonacci(FibonacciRequest request) { - fastFibonacciCallable().call(request); - } - - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *
{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   ApiFuture future = deprecatedServiceClient.fastFibonacciCallable().futureCall(request);
-   *   // Do something.
-   *   future.get();
-   * }
-   * }
- */ - public final UnaryCallable fastFibonacciCallable() { - return stub.fastFibonacciCallable(); - } - - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *
{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   deprecatedServiceClient.slowFibonacci(request);
-   * }
-   * }
- * - * @param request The request object containing all of the parameters for the API call. - * @throws com.google.api.gax.rpc.ApiException if the remote call fails - * @deprecated This method is deprecated and will be removed in the next major version update. - */ - @Deprecated - public final void slowFibonacci(FibonacciRequest request) { - slowFibonacciCallable().call(request); - } - - // AUTO-GENERATED DOCUMENTATION AND METHOD. - /** - * Sample code: - * - *
{@code
-   * try (DeprecatedServiceClient deprecatedServiceClient = DeprecatedServiceClient.create()) {
-   *   FibonacciRequest request = FibonacciRequest.newBuilder().setValue(111972721).build();
-   *   ApiFuture future = deprecatedServiceClient.slowFibonacciCallable().futureCall(request);
-   *   // Do something.
-   *   future.get();
-   * }
-   * }
- * - * @deprecated This method is deprecated and will be removed in the next major version update. - */ - @Deprecated - public final UnaryCallable slowFibonacciCallable() { - return stub.slowFibonacciCallable(); - } - - @Override - public final void close() { - stub.close(); - } - - @Override - public void shutdown() { - stub.shutdown(); - } - - @Override - public boolean isShutdown() { - return stub.isShutdown(); - } - - @Override - public boolean isTerminated() { - return stub.isTerminated(); + public void reset() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); } @Override - public void shutdownNow() { - stub.shutdownNow(); + public void fastFibonacci(FibonacciRequest request, StreamObserver responseObserver) { + Object response = responses.poll(); + if (response instanceof Empty) { + requests.add(request); + responseObserver.onNext(((Empty) response)); + responseObserver.onCompleted(); + } else if (response instanceof Exception) { + responseObserver.onError(((Exception) response)); + } else { + responseObserver.onError( + new IllegalArgumentException( + String.format( + "Unrecognized response type %s for method FastFibonacci, expected %s or %s", + response == null ? "null" : response.getClass().getName(), + Empty.class.getName(), + Exception.class.getName()))); + } } @Override - public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { - return stub.awaitTermination(duration, unit); + public void slowFibonacci(FibonacciRequest request, StreamObserver responseObserver) { + Object response = responses.poll(); + if (response instanceof Empty) { + requests.add(request); + responseObserver.onNext(((Empty) response)); + responseObserver.onCompleted(); + } else if (response instanceof Exception) { + responseObserver.onError(((Exception) response)); + } else { + responseObserver.onError( + new IllegalArgumentException( + String.format( + "Unrecognized response type %s for method SlowFibonacci, expected %s or %s", + response == null ? "null" : response.getClass().getName(), + Empty.class.getName(), + Exception.class.getName()))); + } } }