From ade36dc38bee10eb3ce346d27e8a4ac77ee0ef63 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Tue, 24 Nov 2020 16:43:52 -0800 Subject: [PATCH] [ggj][engx] feat: allow example/library to lack the grpc_config file (#566) * fix: handle wildcard-typed resrefs when parsing methods * fix: support older type-only resrefs w/o a service prefix * feat: allow example/library to lack the grpc_config file --- rules_java_gapic/java_gapic.bzl | 5 +- .../gapic/model/GapicServiceConfig.java | 7 +- .../protoparser/ServiceConfigParser.java | 5 +- test/integration/BUILD.bazel | 35 + test/integration/goldens/library/BUILD.bazel | 6 + .../integration/goldens/library/BookName.java | 191 ++++ .../GrpcLibraryServiceCallableFactory.java | 113 ++ .../library/GrpcLibraryServiceStub.java | 457 ++++++++ .../goldens/library/LibraryServiceClient.java | 943 +++++++++++++++ .../library/LibraryServiceClientTest.java | 1014 +++++++++++++++++ .../library/LibraryServiceSettings.java | 309 +++++ .../goldens/library/LibraryServiceStub.java | 105 ++ .../library/LibraryServiceStubSettings.java | 620 ++++++++++ .../goldens/library/MockLibraryService.java | 59 + .../library/MockLibraryServiceImpl.java | 232 ++++ .../goldens/library/ShelfName.java | 168 +++ .../goldens/library/package-info.java | 36 + 17 files changed, 4299 insertions(+), 6 deletions(-) create mode 100644 test/integration/goldens/library/BUILD.bazel create mode 100644 test/integration/goldens/library/BookName.java create mode 100644 test/integration/goldens/library/GrpcLibraryServiceCallableFactory.java create mode 100644 test/integration/goldens/library/GrpcLibraryServiceStub.java create mode 100644 test/integration/goldens/library/LibraryServiceClient.java create mode 100644 test/integration/goldens/library/LibraryServiceClientTest.java create mode 100644 test/integration/goldens/library/LibraryServiceSettings.java create mode 100644 test/integration/goldens/library/LibraryServiceStub.java create mode 100644 test/integration/goldens/library/LibraryServiceStubSettings.java create mode 100644 test/integration/goldens/library/MockLibraryService.java create mode 100644 test/integration/goldens/library/MockLibraryServiceImpl.java create mode 100644 test/integration/goldens/library/ShelfName.java create mode 100644 test/integration/goldens/library/package-info.java diff --git a/rules_java_gapic/java_gapic.bzl b/rules_java_gapic/java_gapic.bzl index 28f76d27b7..e76ecd4682 100644 --- a/rules_java_gapic/java_gapic.bzl +++ b/rules_java_gapic/java_gapic.bzl @@ -15,6 +15,7 @@ load("@com_google_api_codegen//rules_gapic:gapic.bzl", "proto_custom_library", "unzipped_srcjar") SERVICE_YAML_ALLOWLIST = ["googleads"] +NO_GRPC_CONFIG_ALLOWLIST = ["library"] def _java_gapic_postprocess_srcjar_impl(ctx): gapic_srcjar = ctx.file.gapic_srcjar @@ -113,7 +114,9 @@ def java_gapic_library( if grpc_service_config: file_args_dict[grpc_service_config] = "grpc-service-config" else: - fail("Missing a gRPC service config file") + for keyword in NO_GRPC_CONFIG_ALLOWLIST: + if keyword not in name: + fail("Missing a gRPC service config file") if gapic_yaml: file_args_dict[gapic_yaml] = "gapic-config" diff --git a/src/main/java/com/google/api/generator/gapic/model/GapicServiceConfig.java b/src/main/java/com/google/api/generator/gapic/model/GapicServiceConfig.java index 28f9751f1d..69f8398417 100644 --- a/src/main/java/com/google/api/generator/gapic/model/GapicServiceConfig.java +++ b/src/main/java/com/google/api/generator/gapic/model/GapicServiceConfig.java @@ -55,7 +55,12 @@ private GapicServiceConfig( this.methodConfigTable = methodConfigTable; } - public static GapicServiceConfig create(ServiceConfig serviceConfig) { + public static GapicServiceConfig create(Optional serviceConfigOpt) { + if (!serviceConfigOpt.isPresent()) { + return new GapicServiceConfig(Collections.emptyList(), Collections.emptyMap()); + } + + ServiceConfig serviceConfig = serviceConfigOpt.get(); Map methodConfigTable = new HashMap<>(); List methodConfigs = serviceConfig.getMethodConfigList(); for (int i = 0; i < methodConfigs.size(); i++) { diff --git a/src/main/java/com/google/api/generator/gapic/protoparser/ServiceConfigParser.java b/src/main/java/com/google/api/generator/gapic/protoparser/ServiceConfigParser.java index 24bbe84716..ba075dc483 100644 --- a/src/main/java/com/google/api/generator/gapic/protoparser/ServiceConfigParser.java +++ b/src/main/java/com/google/api/generator/gapic/protoparser/ServiceConfigParser.java @@ -27,10 +27,7 @@ public class ServiceConfigParser { public static Optional parse(String serviceConfigFilePath) { Optional rawConfigOpt = parseFile(serviceConfigFilePath); - if (!rawConfigOpt.isPresent()) { - return Optional.empty(); - } - return Optional.of(GapicServiceConfig.create(rawConfigOpt.get())); + return Optional.of(GapicServiceConfig.create(rawConfigOpt)); } @VisibleForTesting diff --git a/test/integration/BUILD.bazel b/test/integration/BUILD.bazel index 957467a317..597e389b17 100644 --- a/test/integration/BUILD.bazel +++ b/test/integration/BUILD.bazel @@ -20,6 +20,7 @@ INTEGRATION_TEST_LIBRARIES = [ "asset", "logging", "redis", + "library", # No gRPC service config. ] [integration_test( @@ -147,3 +148,37 @@ java_gapic_assembly_gradle_pkg( "@com_google_googleapis//google/logging/v2:logging_proto", ], ) + +# example/library API. +# Tests the edge case of a legitimately missing gRPC service config file. +java_gapic_library( + name = "library_java_gapic", + srcs = ["@com_google_googleapis//google/example/library/v1:library_proto_with_info"], + gapic_yaml = "@com_google_googleapis//google/example/library/v1:library_example_gapic.yaml", + package = "google.example.library.v1", + test_deps = [ + "@com_google_googleapis//google/example/library/v1:library_java_grpc", + ], + deps = [ + "@com_google_googleapis//google/example/library/v1:library_java_proto", + ], +) + +java_gapic_test( + name = "library_java_gapic_test_suite", + test_classes = [ + "com.google.cloud.example.library.v1.LibraryServiceClientTest", + ], + runtime_deps = [":library_java_gapic_test"], +) + +# Open Source Packages +java_gapic_assembly_gradle_pkg( + name = "google-cloud-example-library-v1-java", + deps = [ + ":library_java_gapic", + "@com_google_googleapis//google/example/library/v1:library_java_grpc", + "@com_google_googleapis//google/example/library/v1:library_java_proto", + "@com_google_googleapis//google/example/library/v1:library_proto", + ], +) diff --git a/test/integration/goldens/library/BUILD.bazel b/test/integration/goldens/library/BUILD.bazel new file mode 100644 index 0000000000..0b74aed56b --- /dev/null +++ b/test/integration/goldens/library/BUILD.bazel @@ -0,0 +1,6 @@ +package(default_visibility = ["//visibility:public"]) + +filegroup( + name = "goldens_files", + srcs = glob(["*.java"]), +) diff --git a/test/integration/goldens/library/BookName.java b/test/integration/goldens/library/BookName.java new file mode 100644 index 0000000000..eb89e75b61 --- /dev/null +++ b/test/integration/goldens/library/BookName.java @@ -0,0 +1,191 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.example.library.v1; + +import com.google.api.pathtemplate.PathTemplate; +import com.google.api.resourcenames.ResourceName; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableMap; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import javax.annotation.Generated; + +// AUTO-GENERATED DOCUMENTATION AND CLASS. +@Generated("by gapic-generator-java") +public class BookName implements ResourceName { + private static final PathTemplate SHELF_ID_BOOK_ID = + PathTemplate.createWithoutUrlEncoding("shelves/{shelf_id}/books/{book_id}"); + private volatile Map fieldValuesMap; + private final String shelfId; + private final String bookId; + + @Deprecated + protected BookName() { + shelfId = null; + bookId = null; + } + + private BookName(Builder builder) { + shelfId = Preconditions.checkNotNull(builder.getShelfId()); + bookId = Preconditions.checkNotNull(builder.getBookId()); + } + + public String getShelfId() { + return shelfId; + } + + public String getBookId() { + return bookId; + } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder toBuilder() { + return new Builder(this); + } + + public static BookName of(String shelfId, String bookId) { + return newBuilder().setShelfId(shelfId).setBookId(bookId).build(); + } + + public static String format(String shelfId, String bookId) { + return newBuilder().setShelfId(shelfId).setBookId(bookId).build().toString(); + } + + public static BookName parse(String formattedString) { + if (formattedString.isEmpty()) { + return null; + } + Map matchMap = + SHELF_ID_BOOK_ID.validatedMatch( + formattedString, "BookName.parse: formattedString not in valid format"); + return of(matchMap.get("shelf_id"), matchMap.get("book_id")); + } + + public static List parseList(List formattedStrings) { + List list = new ArrayList<>(formattedStrings.size()); + for (String formattedString : formattedStrings) { + list.add(parse(formattedString)); + } + return list; + } + + public static List toStringList(List values) { + List list = new ArrayList<>(values.size()); + for (BookName value : values) { + if (Objects.isNull(value)) { + list.add(""); + } else { + list.add(value.toString()); + } + } + return list; + } + + public static boolean isParsableFrom(String formattedString) { + return SHELF_ID_BOOK_ID.matches(formattedString); + } + + @Override + public Map getFieldValuesMap() { + if (Objects.isNull(fieldValuesMap)) { + synchronized (this) { + if (Objects.isNull(fieldValuesMap)) { + ImmutableMap.Builder fieldMapBuilder = ImmutableMap.builder(); + if (!Objects.isNull(shelfId)) { + fieldMapBuilder.put("shelf_id", shelfId); + } + if (!Objects.isNull(bookId)) { + fieldMapBuilder.put("book_id", bookId); + } + fieldValuesMap = fieldMapBuilder.build(); + } + } + } + return fieldValuesMap; + } + + public String getFieldValue(String fieldName) { + return getFieldValuesMap().get(fieldName); + } + + @Override + public String toString() { + return SHELF_ID_BOOK_ID.instantiate("shelf_id", shelfId, "book_id", bookId); + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } + if (o != null || getClass() == o.getClass()) { + BookName that = ((BookName) o); + return Objects.equals(this.shelfId, that.shelfId) && Objects.equals(this.bookId, that.bookId); + } + return false; + } + + @Override + public int hashCode() { + int h = 1; + h *= 1000003; + h ^= Objects.hashCode(shelfId); + h *= 1000003; + h ^= Objects.hashCode(bookId); + return h; + } + + /** Builder for shelves/{shelf_id}/books/{book_id}. */ + public static class Builder { + private String shelfId; + private String bookId; + + protected Builder() {} + + public String getShelfId() { + return shelfId; + } + + public String getBookId() { + return bookId; + } + + public Builder setShelfId(String shelfId) { + this.shelfId = shelfId; + return this; + } + + public Builder setBookId(String bookId) { + this.bookId = bookId; + return this; + } + + private Builder(BookName bookName) { + shelfId = bookName.shelfId; + bookId = bookName.bookId; + } + + public BookName build() { + return new BookName(this); + } + } +} diff --git a/test/integration/goldens/library/GrpcLibraryServiceCallableFactory.java b/test/integration/goldens/library/GrpcLibraryServiceCallableFactory.java new file mode 100644 index 0000000000..50414f7b6b --- /dev/null +++ b/test/integration/goldens/library/GrpcLibraryServiceCallableFactory.java @@ -0,0 +1,113 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.example.library.v1.stub; + +import com.google.api.gax.grpc.GrpcCallSettings; +import com.google.api.gax.grpc.GrpcCallableFactory; +import com.google.api.gax.grpc.GrpcStubCallableFactory; +import com.google.api.gax.rpc.BatchingCallSettings; +import com.google.api.gax.rpc.BidiStreamingCallable; +import com.google.api.gax.rpc.ClientContext; +import com.google.api.gax.rpc.ClientStreamingCallable; +import com.google.api.gax.rpc.OperationCallSettings; +import com.google.api.gax.rpc.OperationCallable; +import com.google.api.gax.rpc.PagedCallSettings; +import com.google.api.gax.rpc.ServerStreamingCallSettings; +import com.google.api.gax.rpc.ServerStreamingCallable; +import com.google.api.gax.rpc.StreamingCallSettings; +import com.google.api.gax.rpc.UnaryCallSettings; +import com.google.api.gax.rpc.UnaryCallable; +import com.google.longrunning.Operation; +import com.google.longrunning.stub.OperationsStub; +import javax.annotation.Generated; + +// AUTO-GENERATED DOCUMENTATION AND CLASS. +/** + * gRPC callable factory implementation for the LibraryService service API. + * + *

This class is for advanced usage. + */ +@Generated("by gapic-generator") +public class GrpcLibraryServiceCallableFactory implements GrpcStubCallableFactory { + + @Override + public UnaryCallable createUnaryCallable( + GrpcCallSettings grpcCallSettings, + UnaryCallSettings callSettings, + ClientContext clientContext) { + return GrpcCallableFactory.createUnaryCallable(grpcCallSettings, callSettings, clientContext); + } + + @Override + public + UnaryCallable createPagedCallable( + GrpcCallSettings grpcCallSettings, + PagedCallSettings callSettings, + ClientContext clientContext) { + return GrpcCallableFactory.createPagedCallable(grpcCallSettings, callSettings, clientContext); + } + + @Override + public UnaryCallable createBatchingCallable( + GrpcCallSettings grpcCallSettings, + BatchingCallSettings callSettings, + ClientContext clientContext) { + return GrpcCallableFactory.createBatchingCallable( + grpcCallSettings, callSettings, clientContext); + } + + @Override + public + OperationCallable createOperationCallable( + GrpcCallSettings grpcCallSettings, + OperationCallSettings callSettings, + ClientContext clientContext, + OperationsStub operationsStub) { + return GrpcCallableFactory.createOperationCallable( + grpcCallSettings, callSettings, clientContext, operationsStub); + } + + @Override + public + BidiStreamingCallable createBidiStreamingCallable( + GrpcCallSettings grpcCallSettings, + StreamingCallSettings callSettings, + ClientContext clientContext) { + return GrpcCallableFactory.createBidiStreamingCallable( + grpcCallSettings, callSettings, clientContext); + } + + @Override + public + ServerStreamingCallable createServerStreamingCallable( + GrpcCallSettings grpcCallSettings, + ServerStreamingCallSettings callSettings, + ClientContext clientContext) { + return GrpcCallableFactory.createServerStreamingCallable( + grpcCallSettings, callSettings, clientContext); + } + + @Override + public + ClientStreamingCallable createClientStreamingCallable( + GrpcCallSettings grpcCallSettings, + StreamingCallSettings callSettings, + ClientContext clientContext) { + return GrpcCallableFactory.createClientStreamingCallable( + grpcCallSettings, callSettings, clientContext); + } +} diff --git a/test/integration/goldens/library/GrpcLibraryServiceStub.java b/test/integration/goldens/library/GrpcLibraryServiceStub.java new file mode 100644 index 0000000000..17eafdd337 --- /dev/null +++ b/test/integration/goldens/library/GrpcLibraryServiceStub.java @@ -0,0 +1,457 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.example.library.v1.stub; + +import static com.google.cloud.example.library.v1.LibraryServiceClient.ListBooksPagedResponse; +import static com.google.cloud.example.library.v1.LibraryServiceClient.ListShelvesPagedResponse; + +import com.google.api.gax.core.BackgroundResource; +import com.google.api.gax.core.BackgroundResourceAggregation; +import com.google.api.gax.grpc.GrpcCallSettings; +import com.google.api.gax.grpc.GrpcStubCallableFactory; +import com.google.api.gax.rpc.ClientContext; +import com.google.api.gax.rpc.RequestParamsExtractor; +import com.google.api.gax.rpc.UnaryCallable; +import com.google.common.collect.ImmutableMap; +import com.google.example.library.v1.Book; +import com.google.example.library.v1.CreateBookRequest; +import com.google.example.library.v1.CreateShelfRequest; +import com.google.example.library.v1.DeleteBookRequest; +import com.google.example.library.v1.DeleteShelfRequest; +import com.google.example.library.v1.GetBookRequest; +import com.google.example.library.v1.GetShelfRequest; +import com.google.example.library.v1.ListBooksRequest; +import com.google.example.library.v1.ListBooksResponse; +import com.google.example.library.v1.ListShelvesRequest; +import com.google.example.library.v1.ListShelvesResponse; +import com.google.example.library.v1.MergeShelvesRequest; +import com.google.example.library.v1.MoveBookRequest; +import com.google.example.library.v1.Shelf; +import com.google.example.library.v1.UpdateBookRequest; +import com.google.longrunning.stub.GrpcOperationsStub; +import com.google.protobuf.Empty; +import io.grpc.MethodDescriptor; +import io.grpc.protobuf.ProtoUtils; +import java.io.IOException; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import javax.annotation.Generated; + +// AUTO-GENERATED DOCUMENTATION AND CLASS. +/** + * gRPC stub implementation for the LibraryService service API. + * + *

This class is for advanced usage and reflects the underlying API directly. + */ +@Generated("by gapic-generator-java") +public class GrpcLibraryServiceStub extends LibraryServiceStub { + private static final MethodDescriptor createShelfMethodDescriptor = + MethodDescriptor.newBuilder() + .setType(MethodDescriptor.MethodType.UNARY) + .setFullMethodName("google.example.library.v1.LibraryService/CreateShelf") + .setRequestMarshaller(ProtoUtils.marshaller(CreateShelfRequest.getDefaultInstance())) + .setResponseMarshaller(ProtoUtils.marshaller(Shelf.getDefaultInstance())) + .build(); + + private static final MethodDescriptor getShelfMethodDescriptor = + MethodDescriptor.newBuilder() + .setType(MethodDescriptor.MethodType.UNARY) + .setFullMethodName("google.example.library.v1.LibraryService/GetShelf") + .setRequestMarshaller(ProtoUtils.marshaller(GetShelfRequest.getDefaultInstance())) + .setResponseMarshaller(ProtoUtils.marshaller(Shelf.getDefaultInstance())) + .build(); + + private static final MethodDescriptor + listShelvesMethodDescriptor = + MethodDescriptor.newBuilder() + .setType(MethodDescriptor.MethodType.UNARY) + .setFullMethodName("google.example.library.v1.LibraryService/ListShelves") + .setRequestMarshaller(ProtoUtils.marshaller(ListShelvesRequest.getDefaultInstance())) + .setResponseMarshaller( + ProtoUtils.marshaller(ListShelvesResponse.getDefaultInstance())) + .build(); + + private static final MethodDescriptor deleteShelfMethodDescriptor = + MethodDescriptor.newBuilder() + .setType(MethodDescriptor.MethodType.UNARY) + .setFullMethodName("google.example.library.v1.LibraryService/DeleteShelf") + .setRequestMarshaller(ProtoUtils.marshaller(DeleteShelfRequest.getDefaultInstance())) + .setResponseMarshaller(ProtoUtils.marshaller(Empty.getDefaultInstance())) + .build(); + + private static final MethodDescriptor mergeShelvesMethodDescriptor = + MethodDescriptor.newBuilder() + .setType(MethodDescriptor.MethodType.UNARY) + .setFullMethodName("google.example.library.v1.LibraryService/MergeShelves") + .setRequestMarshaller(ProtoUtils.marshaller(MergeShelvesRequest.getDefaultInstance())) + .setResponseMarshaller(ProtoUtils.marshaller(Shelf.getDefaultInstance())) + .build(); + + private static final MethodDescriptor createBookMethodDescriptor = + MethodDescriptor.newBuilder() + .setType(MethodDescriptor.MethodType.UNARY) + .setFullMethodName("google.example.library.v1.LibraryService/CreateBook") + .setRequestMarshaller(ProtoUtils.marshaller(CreateBookRequest.getDefaultInstance())) + .setResponseMarshaller(ProtoUtils.marshaller(Book.getDefaultInstance())) + .build(); + + private static final MethodDescriptor getBookMethodDescriptor = + MethodDescriptor.newBuilder() + .setType(MethodDescriptor.MethodType.UNARY) + .setFullMethodName("google.example.library.v1.LibraryService/GetBook") + .setRequestMarshaller(ProtoUtils.marshaller(GetBookRequest.getDefaultInstance())) + .setResponseMarshaller(ProtoUtils.marshaller(Book.getDefaultInstance())) + .build(); + + private static final MethodDescriptor + listBooksMethodDescriptor = + MethodDescriptor.newBuilder() + .setType(MethodDescriptor.MethodType.UNARY) + .setFullMethodName("google.example.library.v1.LibraryService/ListBooks") + .setRequestMarshaller(ProtoUtils.marshaller(ListBooksRequest.getDefaultInstance())) + .setResponseMarshaller(ProtoUtils.marshaller(ListBooksResponse.getDefaultInstance())) + .build(); + + private static final MethodDescriptor deleteBookMethodDescriptor = + MethodDescriptor.newBuilder() + .setType(MethodDescriptor.MethodType.UNARY) + .setFullMethodName("google.example.library.v1.LibraryService/DeleteBook") + .setRequestMarshaller(ProtoUtils.marshaller(DeleteBookRequest.getDefaultInstance())) + .setResponseMarshaller(ProtoUtils.marshaller(Empty.getDefaultInstance())) + .build(); + + private static final MethodDescriptor updateBookMethodDescriptor = + MethodDescriptor.newBuilder() + .setType(MethodDescriptor.MethodType.UNARY) + .setFullMethodName("google.example.library.v1.LibraryService/UpdateBook") + .setRequestMarshaller(ProtoUtils.marshaller(UpdateBookRequest.getDefaultInstance())) + .setResponseMarshaller(ProtoUtils.marshaller(Book.getDefaultInstance())) + .build(); + + private static final MethodDescriptor moveBookMethodDescriptor = + MethodDescriptor.newBuilder() + .setType(MethodDescriptor.MethodType.UNARY) + .setFullMethodName("google.example.library.v1.LibraryService/MoveBook") + .setRequestMarshaller(ProtoUtils.marshaller(MoveBookRequest.getDefaultInstance())) + .setResponseMarshaller(ProtoUtils.marshaller(Book.getDefaultInstance())) + .build(); + + private final UnaryCallable createShelfCallable; + private final UnaryCallable getShelfCallable; + private final UnaryCallable listShelvesCallable; + private final UnaryCallable + listShelvesPagedCallable; + private final UnaryCallable deleteShelfCallable; + private final UnaryCallable mergeShelvesCallable; + private final UnaryCallable createBookCallable; + private final UnaryCallable getBookCallable; + private final UnaryCallable listBooksCallable; + private final UnaryCallable listBooksPagedCallable; + private final UnaryCallable deleteBookCallable; + private final UnaryCallable updateBookCallable; + private final UnaryCallable moveBookCallable; + + private final BackgroundResource backgroundResources; + private final GrpcOperationsStub operationsStub; + private final GrpcStubCallableFactory callableFactory; + + public static final GrpcLibraryServiceStub create(LibraryServiceStubSettings settings) + throws IOException { + return new GrpcLibraryServiceStub(settings, ClientContext.create(settings)); + } + + public static final GrpcLibraryServiceStub create(ClientContext clientContext) + throws IOException { + return new GrpcLibraryServiceStub( + LibraryServiceStubSettings.newBuilder().build(), clientContext); + } + + public static final GrpcLibraryServiceStub create( + ClientContext clientContext, GrpcStubCallableFactory callableFactory) throws IOException { + return new GrpcLibraryServiceStub( + LibraryServiceStubSettings.newBuilder().build(), clientContext, callableFactory); + } + + protected GrpcLibraryServiceStub(LibraryServiceStubSettings settings, ClientContext clientContext) + throws IOException { + this(settings, clientContext, new GrpcLibraryServiceCallableFactory()); + } + + protected GrpcLibraryServiceStub( + LibraryServiceStubSettings settings, + ClientContext clientContext, + GrpcStubCallableFactory callableFactory) + throws IOException { + this.callableFactory = callableFactory; + this.operationsStub = GrpcOperationsStub.create(clientContext, callableFactory); + + GrpcCallSettings createShelfTransportSettings = + GrpcCallSettings.newBuilder() + .setMethodDescriptor(createShelfMethodDescriptor) + .build(); + GrpcCallSettings getShelfTransportSettings = + GrpcCallSettings.newBuilder() + .setMethodDescriptor(getShelfMethodDescriptor) + .setParamsExtractor( + new RequestParamsExtractor() { + @Override + public Map extract(GetShelfRequest request) { + ImmutableMap.Builder params = ImmutableMap.builder(); + params.put("name", String.valueOf(request.getName())); + return params.build(); + } + }) + .build(); + GrpcCallSettings listShelvesTransportSettings = + GrpcCallSettings.newBuilder() + .setMethodDescriptor(listShelvesMethodDescriptor) + .build(); + GrpcCallSettings deleteShelfTransportSettings = + GrpcCallSettings.newBuilder() + .setMethodDescriptor(deleteShelfMethodDescriptor) + .setParamsExtractor( + new RequestParamsExtractor() { + @Override + public Map extract(DeleteShelfRequest request) { + ImmutableMap.Builder params = ImmutableMap.builder(); + params.put("name", String.valueOf(request.getName())); + return params.build(); + } + }) + .build(); + GrpcCallSettings mergeShelvesTransportSettings = + GrpcCallSettings.newBuilder() + .setMethodDescriptor(mergeShelvesMethodDescriptor) + .setParamsExtractor( + new RequestParamsExtractor() { + @Override + public Map extract(MergeShelvesRequest request) { + ImmutableMap.Builder params = ImmutableMap.builder(); + params.put("name", String.valueOf(request.getName())); + return params.build(); + } + }) + .build(); + GrpcCallSettings createBookTransportSettings = + GrpcCallSettings.newBuilder() + .setMethodDescriptor(createBookMethodDescriptor) + .setParamsExtractor( + new RequestParamsExtractor() { + @Override + public Map extract(CreateBookRequest request) { + ImmutableMap.Builder params = ImmutableMap.builder(); + params.put("name", String.valueOf(request.getName())); + return params.build(); + } + }) + .build(); + GrpcCallSettings getBookTransportSettings = + GrpcCallSettings.newBuilder() + .setMethodDescriptor(getBookMethodDescriptor) + .setParamsExtractor( + new RequestParamsExtractor() { + @Override + public Map extract(GetBookRequest request) { + ImmutableMap.Builder params = ImmutableMap.builder(); + params.put("name", String.valueOf(request.getName())); + return params.build(); + } + }) + .build(); + GrpcCallSettings listBooksTransportSettings = + GrpcCallSettings.newBuilder() + .setMethodDescriptor(listBooksMethodDescriptor) + .setParamsExtractor( + new RequestParamsExtractor() { + @Override + public Map extract(ListBooksRequest request) { + ImmutableMap.Builder params = ImmutableMap.builder(); + params.put("name", String.valueOf(request.getName())); + return params.build(); + } + }) + .build(); + GrpcCallSettings deleteBookTransportSettings = + GrpcCallSettings.newBuilder() + .setMethodDescriptor(deleteBookMethodDescriptor) + .setParamsExtractor( + new RequestParamsExtractor() { + @Override + public Map extract(DeleteBookRequest request) { + ImmutableMap.Builder params = ImmutableMap.builder(); + params.put("name", String.valueOf(request.getName())); + return params.build(); + } + }) + .build(); + GrpcCallSettings updateBookTransportSettings = + GrpcCallSettings.newBuilder() + .setMethodDescriptor(updateBookMethodDescriptor) + .setParamsExtractor( + new RequestParamsExtractor() { + @Override + public Map extract(UpdateBookRequest request) { + ImmutableMap.Builder params = ImmutableMap.builder(); + params.put("book.name", String.valueOf(request.getBook().getName())); + return params.build(); + } + }) + .build(); + GrpcCallSettings moveBookTransportSettings = + GrpcCallSettings.newBuilder() + .setMethodDescriptor(moveBookMethodDescriptor) + .setParamsExtractor( + new RequestParamsExtractor() { + @Override + public Map extract(MoveBookRequest request) { + ImmutableMap.Builder params = ImmutableMap.builder(); + params.put("name", String.valueOf(request.getName())); + return params.build(); + } + }) + .build(); + + this.createShelfCallable = + callableFactory.createUnaryCallable( + createShelfTransportSettings, settings.createShelfSettings(), clientContext); + this.getShelfCallable = + callableFactory.createUnaryCallable( + getShelfTransportSettings, settings.getShelfSettings(), clientContext); + this.listShelvesCallable = + callableFactory.createUnaryCallable( + listShelvesTransportSettings, settings.listShelvesSettings(), clientContext); + this.listShelvesPagedCallable = + callableFactory.createPagedCallable( + listShelvesTransportSettings, settings.listShelvesSettings(), clientContext); + this.deleteShelfCallable = + callableFactory.createUnaryCallable( + deleteShelfTransportSettings, settings.deleteShelfSettings(), clientContext); + this.mergeShelvesCallable = + callableFactory.createUnaryCallable( + mergeShelvesTransportSettings, settings.mergeShelvesSettings(), clientContext); + this.createBookCallable = + callableFactory.createUnaryCallable( + createBookTransportSettings, settings.createBookSettings(), clientContext); + this.getBookCallable = + callableFactory.createUnaryCallable( + getBookTransportSettings, settings.getBookSettings(), clientContext); + this.listBooksCallable = + callableFactory.createUnaryCallable( + listBooksTransportSettings, settings.listBooksSettings(), clientContext); + this.listBooksPagedCallable = + callableFactory.createPagedCallable( + listBooksTransportSettings, settings.listBooksSettings(), clientContext); + this.deleteBookCallable = + callableFactory.createUnaryCallable( + deleteBookTransportSettings, settings.deleteBookSettings(), clientContext); + this.updateBookCallable = + callableFactory.createUnaryCallable( + updateBookTransportSettings, settings.updateBookSettings(), clientContext); + this.moveBookCallable = + callableFactory.createUnaryCallable( + moveBookTransportSettings, settings.moveBookSettings(), clientContext); + + this.backgroundResources = + new BackgroundResourceAggregation(clientContext.getBackgroundResources()); + } + + public GrpcOperationsStub getOperationsStub() { + return operationsStub; + } + + public UnaryCallable createShelfCallable() { + return createShelfCallable; + } + + public UnaryCallable getShelfCallable() { + return getShelfCallable; + } + + public UnaryCallable listShelvesCallable() { + return listShelvesCallable; + } + + public UnaryCallable listShelvesPagedCallable() { + return listShelvesPagedCallable; + } + + public UnaryCallable deleteShelfCallable() { + return deleteShelfCallable; + } + + public UnaryCallable mergeShelvesCallable() { + return mergeShelvesCallable; + } + + public UnaryCallable createBookCallable() { + return createBookCallable; + } + + public UnaryCallable getBookCallable() { + return getBookCallable; + } + + public UnaryCallable listBooksCallable() { + return listBooksCallable; + } + + public UnaryCallable listBooksPagedCallable() { + return listBooksPagedCallable; + } + + public UnaryCallable deleteBookCallable() { + return deleteBookCallable; + } + + public UnaryCallable updateBookCallable() { + return updateBookCallable; + } + + public UnaryCallable moveBookCallable() { + return moveBookCallable; + } + + @Override + public final void close() { + shutdown(); + } + + @Override + public void shutdown() { + backgroundResources.shutdown(); + } + + @Override + public boolean isShutdown() { + return backgroundResources.isShutdown(); + } + + @Override + public boolean isTerminated() { + return backgroundResources.isTerminated(); + } + + @Override + public void shutdownNow() { + backgroundResources.shutdownNow(); + } + + @Override + public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { + return backgroundResources.awaitTermination(duration, unit); + } +} diff --git a/test/integration/goldens/library/LibraryServiceClient.java b/test/integration/goldens/library/LibraryServiceClient.java new file mode 100644 index 0000000000..e9d2780f0b --- /dev/null +++ b/test/integration/goldens/library/LibraryServiceClient.java @@ -0,0 +1,943 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.example.library.v1; + +import com.google.api.core.ApiFunction; +import com.google.api.core.ApiFuture; +import com.google.api.core.ApiFutures; +import com.google.api.core.BetaApi; +import com.google.api.gax.core.BackgroundResource; +import com.google.api.gax.paging.AbstractFixedSizeCollection; +import com.google.api.gax.paging.AbstractPage; +import com.google.api.gax.paging.AbstractPagedListResponse; +import com.google.api.gax.rpc.PageContext; +import com.google.api.gax.rpc.UnaryCallable; +import com.google.cloud.example.library.v1.stub.LibraryServiceStub; +import com.google.cloud.example.library.v1.stub.LibraryServiceStubSettings; +import com.google.common.util.concurrent.MoreExecutors; +import com.google.example.library.v1.Book; +import com.google.example.library.v1.BookName; +import com.google.example.library.v1.CreateBookRequest; +import com.google.example.library.v1.CreateShelfRequest; +import com.google.example.library.v1.DeleteBookRequest; +import com.google.example.library.v1.DeleteShelfRequest; +import com.google.example.library.v1.GetBookRequest; +import com.google.example.library.v1.GetShelfRequest; +import com.google.example.library.v1.ListBooksRequest; +import com.google.example.library.v1.ListBooksResponse; +import com.google.example.library.v1.ListShelvesRequest; +import com.google.example.library.v1.ListShelvesResponse; +import com.google.example.library.v1.MergeShelvesRequest; +import com.google.example.library.v1.MoveBookRequest; +import com.google.example.library.v1.Shelf; +import com.google.example.library.v1.ShelfName; +import com.google.example.library.v1.UpdateBookRequest; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.TimeUnit; +import javax.annotation.Generated; + +// AUTO-GENERATED DOCUMENTATION AND CLASS. +/** + * Service Description: This API represents a simple digital library. It lets you manage Shelf + * resources and Book resources in the library. It defines the following resource model: + * + *

- The API has a collection of [Shelf][google.example.library.v1.Shelf] resources, named + * `shelves/*` + * + *

- Each Shelf has a collection of [Book][google.example.library.v1.Book] resources, named + * `shelves/*/books/*` + * + *

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: + * + *

Note: close() needs to be called on the LibraryServiceClient 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 LibraryServiceSettings to + * create(). For example: + * + *

To customize credentials: + * + *

{@code
+ * LibraryServiceSettings libraryServiceSettings =
+ *     LibraryServiceSettings.newBuilder()
+ *         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
+ *         .build();
+ * LibraryServiceClient libraryServiceClient = LibraryServiceClient.create(libraryServiceSettings);
+ * }
+ * + *

To customize the endpoint: + * + *

{@code
+ * LibraryServiceSettings libraryServiceSettings =
+ *     LibraryServiceSettings.newBuilder().setEndpoint(myEndpoint).build();
+ * LibraryServiceClient libraryServiceClient = LibraryServiceClient.create(libraryServiceSettings);
+ * }
+ * + *

Please refer to the GitHub repository's samples for more quickstart code snippets. + */ +@BetaApi +@Generated("by gapic-generator") +public class LibraryServiceClient implements BackgroundResource { + private final LibraryServiceSettings settings; + private final LibraryServiceStub stub; + + /** Constructs an instance of LibraryServiceClient with default settings. */ + public static final LibraryServiceClient create() throws IOException { + return create(LibraryServiceSettings.newBuilder().build()); + } + + /** + * Constructs an instance of LibraryServiceClient, 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 LibraryServiceClient create(LibraryServiceSettings settings) + throws IOException { + return new LibraryServiceClient(settings); + } + + /** + * Constructs an instance of LibraryServiceClient, using the given stub for making calls. This is + * for advanced usage - prefer using create(LibraryServiceSettings). + */ + @BetaApi("A restructuring of stub classes is planned, so this may break in the future") + public static final LibraryServiceClient create(LibraryServiceStub stub) { + return new LibraryServiceClient(stub); + } + + /** + * Constructs an instance of LibraryServiceClient, 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 LibraryServiceClient(LibraryServiceSettings settings) throws IOException { + this.settings = settings; + this.stub = ((LibraryServiceStubSettings) settings.getStubSettings()).createStub(); + } + + @BetaApi("A restructuring of stub classes is planned, so this may break in the future") + protected LibraryServiceClient(LibraryServiceStub stub) { + this.settings = null; + this.stub = stub; + } + + public final LibraryServiceSettings getSettings() { + return settings; + } + + @BetaApi("A restructuring of stub classes is planned, so this may break in the future") + public LibraryServiceStub getStub() { + return stub; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Creates a shelf, and returns the new Shelf. + * + * @param shelf The shelf to create. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final Shelf createShelf(Shelf shelf) { + CreateShelfRequest request = CreateShelfRequest.newBuilder().setShelf(shelf).build(); + return createShelf(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Creates a shelf, and returns the new Shelf. + * + * @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 Shelf createShelf(CreateShelfRequest request) { + return createShelfCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Creates a shelf, and returns the new Shelf. + * + *

Sample code: + */ + public final UnaryCallable createShelfCallable() { + return stub.createShelfCallable(); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Gets a shelf. Returns NOT_FOUND if the shelf does not exist. + * + * @param name The name of the shelf to retrieve. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final Shelf getShelf(ShelfName name) { + GetShelfRequest request = + GetShelfRequest.newBuilder().setName(Objects.isNull(name) ? null : name.toString()).build(); + return getShelf(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Gets a shelf. Returns NOT_FOUND if the shelf does not exist. + * + * @param name The name of the shelf to retrieve. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final Shelf getShelf(String name) { + GetShelfRequest request = GetShelfRequest.newBuilder().setName(name).build(); + return getShelf(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Gets a shelf. Returns NOT_FOUND if the shelf does not exist. + * + * @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 Shelf getShelf(GetShelfRequest request) { + return getShelfCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Gets a shelf. Returns NOT_FOUND if the shelf does not exist. + * + *

Sample code: + */ + public final UnaryCallable getShelfCallable() { + return stub.getShelfCallable(); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Lists shelves. The order is unspecified but deterministic. Newly created shelves will not + * necessarily be added to the end of this list. + * + * @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 ListShelvesPagedResponse listShelves(ListShelvesRequest request) { + return listShelvesPagedCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Lists shelves. The order is unspecified but deterministic. Newly created shelves will not + * necessarily be added to the end of this list. + * + *

Sample code: + */ + public final UnaryCallable + listShelvesPagedCallable() { + return stub.listShelvesPagedCallable(); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Lists shelves. The order is unspecified but deterministic. Newly created shelves will not + * necessarily be added to the end of this list. + * + *

Sample code: + */ + public final UnaryCallable listShelvesCallable() { + return stub.listShelvesCallable(); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Deletes a shelf. Returns NOT_FOUND if the shelf does not exist. + * + * @param name The name of the shelf to delete. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final void deleteShelf(ShelfName name) { + DeleteShelfRequest request = + DeleteShelfRequest.newBuilder() + .setName(Objects.isNull(name) ? null : name.toString()) + .build(); + deleteShelf(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Deletes a shelf. Returns NOT_FOUND if the shelf does not exist. + * + * @param name The name of the shelf to delete. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final void deleteShelf(String name) { + DeleteShelfRequest request = DeleteShelfRequest.newBuilder().setName(name).build(); + deleteShelf(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Deletes a shelf. Returns NOT_FOUND if the shelf does not exist. + * + * @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 deleteShelf(DeleteShelfRequest request) { + deleteShelfCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Deletes a shelf. Returns NOT_FOUND if the shelf does not exist. + * + *

Sample code: + */ + public final UnaryCallable deleteShelfCallable() { + return stub.deleteShelfCallable(); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Merges two shelves by adding all books from the shelf named `other_shelf_name` to shelf `name`, + * and deletes `other_shelf_name`. Returns the updated shelf. The book ids of the moved books may + * not be the same as the original books. + * + *

Returns NOT_FOUND if either shelf does not exist. This call is a no-op if the specified + * shelves are the same. + * + * @param name The name of the shelf we're adding books to. + * @param other_shelf_name The name of the shelf we're removing books from and deleting. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final Shelf mergeShelves(ShelfName name, ShelfName otherShelfName) { + MergeShelvesRequest request = + MergeShelvesRequest.newBuilder() + .setName(Objects.isNull(name) ? null : name.toString()) + .setOtherShelfName(Objects.isNull(otherShelfName) ? null : otherShelfName.toString()) + .build(); + return mergeShelves(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Merges two shelves by adding all books from the shelf named `other_shelf_name` to shelf `name`, + * and deletes `other_shelf_name`. Returns the updated shelf. The book ids of the moved books may + * not be the same as the original books. + * + *

Returns NOT_FOUND if either shelf does not exist. This call is a no-op if the specified + * shelves are the same. + * + * @param name The name of the shelf we're adding books to. + * @param other_shelf_name The name of the shelf we're removing books from and deleting. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final Shelf mergeShelves(ShelfName name, String otherShelfName) { + MergeShelvesRequest request = + MergeShelvesRequest.newBuilder() + .setName(Objects.isNull(name) ? null : name.toString()) + .setOtherShelfName(otherShelfName) + .build(); + return mergeShelves(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Merges two shelves by adding all books from the shelf named `other_shelf_name` to shelf `name`, + * and deletes `other_shelf_name`. Returns the updated shelf. The book ids of the moved books may + * not be the same as the original books. + * + *

Returns NOT_FOUND if either shelf does not exist. This call is a no-op if the specified + * shelves are the same. + * + * @param name The name of the shelf we're adding books to. + * @param other_shelf_name The name of the shelf we're removing books from and deleting. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final Shelf mergeShelves(String name, ShelfName otherShelfName) { + MergeShelvesRequest request = + MergeShelvesRequest.newBuilder() + .setName(name) + .setOtherShelfName(Objects.isNull(otherShelfName) ? null : otherShelfName.toString()) + .build(); + return mergeShelves(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Merges two shelves by adding all books from the shelf named `other_shelf_name` to shelf `name`, + * and deletes `other_shelf_name`. Returns the updated shelf. The book ids of the moved books may + * not be the same as the original books. + * + *

Returns NOT_FOUND if either shelf does not exist. This call is a no-op if the specified + * shelves are the same. + * + * @param name The name of the shelf we're adding books to. + * @param other_shelf_name The name of the shelf we're removing books from and deleting. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final Shelf mergeShelves(String name, String otherShelfName) { + MergeShelvesRequest request = + MergeShelvesRequest.newBuilder().setName(name).setOtherShelfName(otherShelfName).build(); + return mergeShelves(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Merges two shelves by adding all books from the shelf named `other_shelf_name` to shelf `name`, + * and deletes `other_shelf_name`. Returns the updated shelf. The book ids of the moved books may + * not be the same as the original books. + * + *

Returns NOT_FOUND if either shelf does not exist. This call is a no-op if the specified + * shelves are the same. + * + * @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 Shelf mergeShelves(MergeShelvesRequest request) { + return mergeShelvesCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Merges two shelves by adding all books from the shelf named `other_shelf_name` to shelf `name`, + * and deletes `other_shelf_name`. Returns the updated shelf. The book ids of the moved books may + * not be the same as the original books. + * + *

Returns NOT_FOUND if either shelf does not exist. This call is a no-op if the specified + * shelves are the same. + * + *

Sample code: + */ + public final UnaryCallable mergeShelvesCallable() { + return stub.mergeShelvesCallable(); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Creates a book, and returns the new Book. + * + * @param name The name of the shelf in which the book is created. + * @param book The book to create. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final Book createBook(ShelfName name, Book book) { + CreateBookRequest request = + CreateBookRequest.newBuilder() + .setName(Objects.isNull(name) ? null : name.toString()) + .setBook(book) + .build(); + return createBook(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Creates a book, and returns the new Book. + * + * @param name The name of the shelf in which the book is created. + * @param book The book to create. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final Book createBook(String name, Book book) { + CreateBookRequest request = CreateBookRequest.newBuilder().setName(name).setBook(book).build(); + return createBook(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Creates a book, and returns the new Book. + * + * @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 Book createBook(CreateBookRequest request) { + return createBookCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Creates a book, and returns the new Book. + * + *

Sample code: + */ + public final UnaryCallable createBookCallable() { + return stub.createBookCallable(); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Gets a book. Returns NOT_FOUND if the book does not exist. + * + * @param name The name of the book to retrieve. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final Book getBook(BookName name) { + GetBookRequest request = + GetBookRequest.newBuilder().setName(Objects.isNull(name) ? null : name.toString()).build(); + return getBook(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Gets a book. Returns NOT_FOUND if the book does not exist. + * + * @param name The name of the book to retrieve. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final Book getBook(String name) { + GetBookRequest request = GetBookRequest.newBuilder().setName(name).build(); + return getBook(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Gets a book. Returns NOT_FOUND if the book does not exist. + * + * @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 Book getBook(GetBookRequest request) { + return getBookCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Gets a book. Returns NOT_FOUND if the book does not exist. + * + *

Sample code: + */ + public final UnaryCallable getBookCallable() { + return stub.getBookCallable(); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Lists books in a shelf. The order is unspecified but deterministic. Newly created books will + * not necessarily be added to the end of this list. Returns NOT_FOUND if the shelf does not + * exist. + * + * @param name The name of the shelf whose books we'd like to list. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final ListBooksPagedResponse listBooks(ShelfName name) { + ListBooksRequest request = + ListBooksRequest.newBuilder() + .setName(Objects.isNull(name) ? null : name.toString()) + .build(); + return listBooks(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Lists books in a shelf. The order is unspecified but deterministic. Newly created books will + * not necessarily be added to the end of this list. Returns NOT_FOUND if the shelf does not + * exist. + * + * @param name The name of the shelf whose books we'd like to list. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final ListBooksPagedResponse listBooks(String name) { + ListBooksRequest request = ListBooksRequest.newBuilder().setName(name).build(); + return listBooks(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Lists books in a shelf. The order is unspecified but deterministic. Newly created books will + * not necessarily be added to the end of this list. Returns NOT_FOUND if the shelf does not + * exist. + * + * @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 ListBooksPagedResponse listBooks(ListBooksRequest request) { + return listBooksPagedCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Lists books in a shelf. The order is unspecified but deterministic. Newly created books will + * not necessarily be added to the end of this list. Returns NOT_FOUND if the shelf does not + * exist. + * + *

Sample code: + */ + public final UnaryCallable listBooksPagedCallable() { + return stub.listBooksPagedCallable(); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Lists books in a shelf. The order is unspecified but deterministic. Newly created books will + * not necessarily be added to the end of this list. Returns NOT_FOUND if the shelf does not + * exist. + * + *

Sample code: + */ + public final UnaryCallable listBooksCallable() { + return stub.listBooksCallable(); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Deletes a book. Returns NOT_FOUND if the book does not exist. + * + * @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 deleteBook(DeleteBookRequest request) { + deleteBookCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Deletes a book. Returns NOT_FOUND if the book does not exist. + * + *

Sample code: + */ + public final UnaryCallable deleteBookCallable() { + return stub.deleteBookCallable(); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Updates a book. Returns INVALID_ARGUMENT if the name of the book is non-empty and does not + * equal the existing name. + * + * @param book The book to update with. The name must match or be empty. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final Book updateBook(Book book) { + UpdateBookRequest request = UpdateBookRequest.newBuilder().setBook(book).build(); + return updateBook(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Updates a book. Returns INVALID_ARGUMENT if the name of the book is non-empty and does not + * equal the existing name. + * + * @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 Book updateBook(UpdateBookRequest request) { + return updateBookCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Updates a book. Returns INVALID_ARGUMENT if the name of the book is non-empty and does not + * equal the existing name. + * + *

Sample code: + */ + public final UnaryCallable updateBookCallable() { + return stub.updateBookCallable(); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Moves a book to another shelf, and returns the new book. The book id of the new book may not be + * the same as the original book. + * + * @param name The name of the book to move. + * @param other_shelf_name The name of the destination shelf. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final Book moveBook(BookName name, ShelfName otherShelfName) { + MoveBookRequest request = + MoveBookRequest.newBuilder() + .setName(Objects.isNull(name) ? null : name.toString()) + .setOtherShelfName(Objects.isNull(otherShelfName) ? null : otherShelfName.toString()) + .build(); + return moveBook(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Moves a book to another shelf, and returns the new book. The book id of the new book may not be + * the same as the original book. + * + * @param name The name of the book to move. + * @param other_shelf_name The name of the destination shelf. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final Book moveBook(BookName name, String otherShelfName) { + MoveBookRequest request = + MoveBookRequest.newBuilder() + .setName(Objects.isNull(name) ? null : name.toString()) + .setOtherShelfName(otherShelfName) + .build(); + return moveBook(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Moves a book to another shelf, and returns the new book. The book id of the new book may not be + * the same as the original book. + * + * @param name The name of the book to move. + * @param other_shelf_name The name of the destination shelf. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final Book moveBook(String name, ShelfName otherShelfName) { + MoveBookRequest request = + MoveBookRequest.newBuilder() + .setName(name) + .setOtherShelfName(Objects.isNull(otherShelfName) ? null : otherShelfName.toString()) + .build(); + return moveBook(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Moves a book to another shelf, and returns the new book. The book id of the new book may not be + * the same as the original book. + * + * @param name The name of the book to move. + * @param other_shelf_name The name of the destination shelf. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final Book moveBook(String name, String otherShelfName) { + MoveBookRequest request = + MoveBookRequest.newBuilder().setName(name).setOtherShelfName(otherShelfName).build(); + return moveBook(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Moves a book to another shelf, and returns the new book. The book id of the new book may not be + * the same as the original book. + * + * @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 Book moveBook(MoveBookRequest request) { + return moveBookCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD. + /** + * Moves a book to another shelf, and returns the new book. The book id of the new book may not be + * the same as the original book. + * + *

Sample code: + */ + public final UnaryCallable moveBookCallable() { + return stub.moveBookCallable(); + } + + @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(); + } + + @Override + public void shutdownNow() { + stub.shutdownNow(); + } + + @Override + public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { + return stub.awaitTermination(duration, unit); + } + + public static class ListShelvesPagedResponse + extends AbstractPagedListResponse< + ListShelvesRequest, + ListShelvesResponse, + Shelf, + ListShelvesPage, + ListShelvesFixedSizeCollection> { + + public static ApiFuture createAsync( + PageContext context, + ApiFuture futureResponse) { + ApiFuture futurePage = + ListShelvesPage.createEmptyPage().createPageAsync(context, futureResponse); + return ApiFutures.transform( + futurePage, + new ApiFunction() { + @Override + public ListShelvesPagedResponse apply(ListShelvesPage input) { + return new ListShelvesPagedResponse(input); + } + }, + MoreExecutors.directExecutor()); + } + + private ListShelvesPagedResponse(ListShelvesPage page) { + super(page, ListShelvesFixedSizeCollection.createEmptyCollection()); + } + } + + public static class ListShelvesPage + extends AbstractPage { + + private ListShelvesPage( + PageContext context, + ListShelvesResponse response) { + super(context, response); + } + + private static ListShelvesPage createEmptyPage() { + return new ListShelvesPage(null, null); + } + + @Override + protected ListShelvesPage createPage( + PageContext context, + ListShelvesResponse response) { + return new ListShelvesPage(context, response); + } + + @Override + public ApiFuture createPageAsync( + PageContext context, + ApiFuture futureResponse) { + return super.createPageAsync(context, futureResponse); + } + } + + public static class ListShelvesFixedSizeCollection + extends AbstractFixedSizeCollection< + ListShelvesRequest, + ListShelvesResponse, + Shelf, + ListShelvesPage, + ListShelvesFixedSizeCollection> { + + private ListShelvesFixedSizeCollection(List pages, int collectionSize) { + super(pages, collectionSize); + } + + private static ListShelvesFixedSizeCollection createEmptyCollection() { + return new ListShelvesFixedSizeCollection(null, 0); + } + + @Override + protected ListShelvesFixedSizeCollection createCollection( + List pages, int collectionSize) { + return new ListShelvesFixedSizeCollection(pages, collectionSize); + } + } + + public static class ListBooksPagedResponse + extends AbstractPagedListResponse< + ListBooksRequest, ListBooksResponse, Book, ListBooksPage, ListBooksFixedSizeCollection> { + + public static ApiFuture createAsync( + PageContext context, + ApiFuture futureResponse) { + ApiFuture futurePage = + ListBooksPage.createEmptyPage().createPageAsync(context, futureResponse); + return ApiFutures.transform( + futurePage, + new ApiFunction() { + @Override + public ListBooksPagedResponse apply(ListBooksPage input) { + return new ListBooksPagedResponse(input); + } + }, + MoreExecutors.directExecutor()); + } + + private ListBooksPagedResponse(ListBooksPage page) { + super(page, ListBooksFixedSizeCollection.createEmptyCollection()); + } + } + + public static class ListBooksPage + extends AbstractPage { + + private ListBooksPage( + PageContext context, + ListBooksResponse response) { + super(context, response); + } + + private static ListBooksPage createEmptyPage() { + return new ListBooksPage(null, null); + } + + @Override + protected ListBooksPage createPage( + PageContext context, + ListBooksResponse response) { + return new ListBooksPage(context, response); + } + + @Override + public ApiFuture createPageAsync( + PageContext context, + ApiFuture futureResponse) { + return super.createPageAsync(context, futureResponse); + } + } + + public static class ListBooksFixedSizeCollection + extends AbstractFixedSizeCollection< + ListBooksRequest, ListBooksResponse, Book, ListBooksPage, ListBooksFixedSizeCollection> { + + private ListBooksFixedSizeCollection(List pages, int collectionSize) { + super(pages, collectionSize); + } + + private static ListBooksFixedSizeCollection createEmptyCollection() { + return new ListBooksFixedSizeCollection(null, 0); + } + + @Override + protected ListBooksFixedSizeCollection createCollection( + List pages, int collectionSize) { + return new ListBooksFixedSizeCollection(pages, collectionSize); + } + } +} diff --git a/test/integration/goldens/library/LibraryServiceClientTest.java b/test/integration/goldens/library/LibraryServiceClientTest.java new file mode 100644 index 0000000000..90a1de03c6 --- /dev/null +++ b/test/integration/goldens/library/LibraryServiceClientTest.java @@ -0,0 +1,1014 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.example.library.v1; + +import static com.google.cloud.example.library.v1.LibraryServiceClient.ListBooksPagedResponse; +import static com.google.cloud.example.library.v1.LibraryServiceClient.ListShelvesPagedResponse; + +import com.google.api.gax.core.NoCredentialsProvider; +import com.google.api.gax.grpc.GaxGrpcProperties; +import com.google.api.gax.grpc.testing.LocalChannelProvider; +import com.google.api.gax.grpc.testing.MockGrpcService; +import com.google.api.gax.grpc.testing.MockServiceHelper; +import com.google.api.gax.rpc.ApiClientHeaderProvider; +import com.google.api.gax.rpc.InvalidArgumentException; +import com.google.common.collect.Lists; +import com.google.example.library.v1.Book; +import com.google.example.library.v1.BookName; +import com.google.example.library.v1.CreateBookRequest; +import com.google.example.library.v1.CreateShelfRequest; +import com.google.example.library.v1.DeleteBookRequest; +import com.google.example.library.v1.DeleteShelfRequest; +import com.google.example.library.v1.GetBookRequest; +import com.google.example.library.v1.GetShelfRequest; +import com.google.example.library.v1.ListBooksRequest; +import com.google.example.library.v1.ListBooksResponse; +import com.google.example.library.v1.ListShelvesRequest; +import com.google.example.library.v1.ListShelvesResponse; +import com.google.example.library.v1.MergeShelvesRequest; +import com.google.example.library.v1.MoveBookRequest; +import com.google.example.library.v1.Shelf; +import com.google.example.library.v1.ShelfName; +import com.google.example.library.v1.UpdateBookRequest; +import com.google.protobuf.AbstractMessage; +import com.google.protobuf.Empty; +import io.grpc.StatusRuntimeException; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; +import javax.annotation.Generated; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +@Generated("by gapic-generator-java") +public class LibraryServiceClientTest { + private static MockServiceHelper mockServiceHelper; + private LibraryServiceClient client; + private static MockLibraryService mockLibraryService; + private LocalChannelProvider channelProvider; + + @BeforeClass + public static void startStaticServer() { + mockLibraryService = new MockLibraryService(); + mockServiceHelper = + new MockServiceHelper( + UUID.randomUUID().toString(), Arrays.asList(mockLibraryService)); + mockServiceHelper.start(); + } + + @AfterClass + public static void stopServer() { + mockServiceHelper.stop(); + } + + @Before + public void setUp() throws IOException { + mockServiceHelper.reset(); + channelProvider = mockServiceHelper.createChannelProvider(); + LibraryServiceSettings settings = + LibraryServiceSettings.newBuilder() + .setTransportChannelProvider(channelProvider) + .setCredentialsProvider(NoCredentialsProvider.create()) + .build(); + client = LibraryServiceClient.create(settings); + } + + @After + public void tearDown() throws Exception { + client.close(); + } + + @Test + public void createShelfTest() throws Exception { + Shelf expectedResponse = + Shelf.newBuilder() + .setName(ShelfName.of("[SHELF_ID]").toString()) + .setTheme("theme110327241") + .build(); + mockLibraryService.addResponse(expectedResponse); + + Shelf shelf = Shelf.newBuilder().build(); + + Shelf actualResponse = client.createShelf(shelf); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + CreateShelfRequest actualRequest = ((CreateShelfRequest) actualRequests.get(0)); + + Assert.assertEquals(shelf, actualRequest.getShelf()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void createShelfExceptionTest() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + Shelf shelf = Shelf.newBuilder().build(); + client.createShelf(shelf); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void getShelfTest() throws Exception { + Shelf expectedResponse = + Shelf.newBuilder() + .setName(ShelfName.of("[SHELF_ID]").toString()) + .setTheme("theme110327241") + .build(); + mockLibraryService.addResponse(expectedResponse); + + ShelfName name = ShelfName.of("[SHELF_ID]"); + + Shelf actualResponse = client.getShelf(name); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetShelfRequest actualRequest = ((GetShelfRequest) actualRequests.get(0)); + + Assert.assertEquals(name.toString(), actualRequest.getName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void getShelfExceptionTest() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + ShelfName name = ShelfName.of("[SHELF_ID]"); + client.getShelf(name); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void getShelfTest2() throws Exception { + Shelf expectedResponse = + Shelf.newBuilder() + .setName(ShelfName.of("[SHELF_ID]").toString()) + .setTheme("theme110327241") + .build(); + mockLibraryService.addResponse(expectedResponse); + + String name = "name3373707"; + + Shelf actualResponse = client.getShelf(name); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetShelfRequest actualRequest = ((GetShelfRequest) actualRequests.get(0)); + + Assert.assertEquals(name, actualRequest.getName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void getShelfExceptionTest2() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + String name = "name3373707"; + client.getShelf(name); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void listShelvesTest() throws Exception { + Shelf responsesElement = Shelf.newBuilder().build(); + ListShelvesResponse expectedResponse = + ListShelvesResponse.newBuilder() + .setNextPageToken("") + .addAllShelves(Arrays.asList(responsesElement)) + .build(); + mockLibraryService.addResponse(expectedResponse); + + ListShelvesRequest request = + ListShelvesRequest.newBuilder() + .setPageSize(883849137) + .setPageToken("page_token1630607433") + .build(); + + ListShelvesPagedResponse pagedListResponse = client.listShelves(request); + + List resources = Lists.newArrayList(pagedListResponse.iterateAll()); + + Assert.assertEquals(1, resources.size()); + Assert.assertEquals(expectedResponse.getShelvesList().get(0), resources.get(0)); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + ListShelvesRequest actualRequest = ((ListShelvesRequest) actualRequests.get(0)); + + Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize()); + Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void listShelvesExceptionTest() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + ListShelvesRequest request = + ListShelvesRequest.newBuilder() + .setPageSize(883849137) + .setPageToken("page_token1630607433") + .build(); + client.listShelves(request); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void deleteShelfTest() throws Exception { + Empty expectedResponse = Empty.newBuilder().build(); + mockLibraryService.addResponse(expectedResponse); + + ShelfName name = ShelfName.of("[SHELF_ID]"); + + client.deleteShelf(name); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + DeleteShelfRequest actualRequest = ((DeleteShelfRequest) actualRequests.get(0)); + + Assert.assertEquals(name.toString(), actualRequest.getName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void deleteShelfExceptionTest() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + ShelfName name = ShelfName.of("[SHELF_ID]"); + client.deleteShelf(name); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void deleteShelfTest2() throws Exception { + Empty expectedResponse = Empty.newBuilder().build(); + mockLibraryService.addResponse(expectedResponse); + + String name = "name3373707"; + + client.deleteShelf(name); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + DeleteShelfRequest actualRequest = ((DeleteShelfRequest) actualRequests.get(0)); + + Assert.assertEquals(name, actualRequest.getName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void deleteShelfExceptionTest2() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + String name = "name3373707"; + client.deleteShelf(name); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void mergeShelvesTest() throws Exception { + Shelf expectedResponse = + Shelf.newBuilder() + .setName(ShelfName.of("[SHELF_ID]").toString()) + .setTheme("theme110327241") + .build(); + mockLibraryService.addResponse(expectedResponse); + + ShelfName name = ShelfName.of("[SHELF_ID]"); + ShelfName otherShelfName = ShelfName.of("[SHELF_ID]"); + + Shelf actualResponse = client.mergeShelves(name, otherShelfName); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + MergeShelvesRequest actualRequest = ((MergeShelvesRequest) actualRequests.get(0)); + + Assert.assertEquals(name.toString(), actualRequest.getName()); + Assert.assertEquals(otherShelfName.toString(), actualRequest.getOtherShelfName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void mergeShelvesExceptionTest() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + ShelfName name = ShelfName.of("[SHELF_ID]"); + ShelfName otherShelfName = ShelfName.of("[SHELF_ID]"); + client.mergeShelves(name, otherShelfName); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void mergeShelvesTest2() throws Exception { + Shelf expectedResponse = + Shelf.newBuilder() + .setName(ShelfName.of("[SHELF_ID]").toString()) + .setTheme("theme110327241") + .build(); + mockLibraryService.addResponse(expectedResponse); + + ShelfName name = ShelfName.of("[SHELF_ID]"); + String otherShelfName = "other_shelf_name145746959"; + + Shelf actualResponse = client.mergeShelves(name, otherShelfName); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + MergeShelvesRequest actualRequest = ((MergeShelvesRequest) actualRequests.get(0)); + + Assert.assertEquals(name.toString(), actualRequest.getName()); + Assert.assertEquals(otherShelfName, actualRequest.getOtherShelfName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void mergeShelvesExceptionTest2() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + ShelfName name = ShelfName.of("[SHELF_ID]"); + String otherShelfName = "other_shelf_name145746959"; + client.mergeShelves(name, otherShelfName); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void mergeShelvesTest3() throws Exception { + Shelf expectedResponse = + Shelf.newBuilder() + .setName(ShelfName.of("[SHELF_ID]").toString()) + .setTheme("theme110327241") + .build(); + mockLibraryService.addResponse(expectedResponse); + + String name = "name3373707"; + ShelfName otherShelfName = ShelfName.of("[SHELF_ID]"); + + Shelf actualResponse = client.mergeShelves(name, otherShelfName); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + MergeShelvesRequest actualRequest = ((MergeShelvesRequest) actualRequests.get(0)); + + Assert.assertEquals(name, actualRequest.getName()); + Assert.assertEquals(otherShelfName.toString(), actualRequest.getOtherShelfName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void mergeShelvesExceptionTest3() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + String name = "name3373707"; + ShelfName otherShelfName = ShelfName.of("[SHELF_ID]"); + client.mergeShelves(name, otherShelfName); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void mergeShelvesTest4() throws Exception { + Shelf expectedResponse = + Shelf.newBuilder() + .setName(ShelfName.of("[SHELF_ID]").toString()) + .setTheme("theme110327241") + .build(); + mockLibraryService.addResponse(expectedResponse); + + String name = "name3373707"; + String otherShelfName = "other_shelf_name145746959"; + + Shelf actualResponse = client.mergeShelves(name, otherShelfName); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + MergeShelvesRequest actualRequest = ((MergeShelvesRequest) actualRequests.get(0)); + + Assert.assertEquals(name, actualRequest.getName()); + Assert.assertEquals(otherShelfName, actualRequest.getOtherShelfName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void mergeShelvesExceptionTest4() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + String name = "name3373707"; + String otherShelfName = "other_shelf_name145746959"; + client.mergeShelves(name, otherShelfName); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void createBookTest() throws Exception { + Book expectedResponse = + Book.newBuilder() + .setName(BookName.of("[SHELF_ID]", "[BOOK_ID]").toString()) + .setAuthor("author-1406328437") + .setTitle("title110371416") + .setRead(true) + .build(); + mockLibraryService.addResponse(expectedResponse); + + ShelfName name = ShelfName.of("[SHELF_ID]"); + Book book = Book.newBuilder().build(); + + Book actualResponse = client.createBook(name, book); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + CreateBookRequest actualRequest = ((CreateBookRequest) actualRequests.get(0)); + + Assert.assertEquals(name.toString(), actualRequest.getName()); + Assert.assertEquals(book, actualRequest.getBook()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void createBookExceptionTest() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + ShelfName name = ShelfName.of("[SHELF_ID]"); + Book book = Book.newBuilder().build(); + client.createBook(name, book); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void createBookTest2() throws Exception { + Book expectedResponse = + Book.newBuilder() + .setName(BookName.of("[SHELF_ID]", "[BOOK_ID]").toString()) + .setAuthor("author-1406328437") + .setTitle("title110371416") + .setRead(true) + .build(); + mockLibraryService.addResponse(expectedResponse); + + String name = "name3373707"; + Book book = Book.newBuilder().build(); + + Book actualResponse = client.createBook(name, book); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + CreateBookRequest actualRequest = ((CreateBookRequest) actualRequests.get(0)); + + Assert.assertEquals(name, actualRequest.getName()); + Assert.assertEquals(book, actualRequest.getBook()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void createBookExceptionTest2() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + String name = "name3373707"; + Book book = Book.newBuilder().build(); + client.createBook(name, book); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void getBookTest() throws Exception { + Book expectedResponse = + Book.newBuilder() + .setName(BookName.of("[SHELF_ID]", "[BOOK_ID]").toString()) + .setAuthor("author-1406328437") + .setTitle("title110371416") + .setRead(true) + .build(); + mockLibraryService.addResponse(expectedResponse); + + BookName name = BookName.of("[SHELF_ID]", "[BOOK_ID]"); + + Book actualResponse = client.getBook(name); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetBookRequest actualRequest = ((GetBookRequest) actualRequests.get(0)); + + Assert.assertEquals(name.toString(), actualRequest.getName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void getBookExceptionTest() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + BookName name = BookName.of("[SHELF_ID]", "[BOOK_ID]"); + client.getBook(name); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void getBookTest2() throws Exception { + Book expectedResponse = + Book.newBuilder() + .setName(BookName.of("[SHELF_ID]", "[BOOK_ID]").toString()) + .setAuthor("author-1406328437") + .setTitle("title110371416") + .setRead(true) + .build(); + mockLibraryService.addResponse(expectedResponse); + + String name = "name3373707"; + + Book actualResponse = client.getBook(name); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetBookRequest actualRequest = ((GetBookRequest) actualRequests.get(0)); + + Assert.assertEquals(name, actualRequest.getName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void getBookExceptionTest2() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + String name = "name3373707"; + client.getBook(name); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void listBooksTest() throws Exception { + Book responsesElement = Book.newBuilder().build(); + ListBooksResponse expectedResponse = + ListBooksResponse.newBuilder() + .setNextPageToken("") + .addAllBooks(Arrays.asList(responsesElement)) + .build(); + mockLibraryService.addResponse(expectedResponse); + + ShelfName name = ShelfName.of("[SHELF_ID]"); + + ListBooksPagedResponse pagedListResponse = client.listBooks(name); + + List resources = Lists.newArrayList(pagedListResponse.iterateAll()); + + Assert.assertEquals(1, resources.size()); + Assert.assertEquals(expectedResponse.getBooksList().get(0), resources.get(0)); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + ListBooksRequest actualRequest = ((ListBooksRequest) actualRequests.get(0)); + + Assert.assertEquals(name.toString(), actualRequest.getName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void listBooksExceptionTest() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + ShelfName name = ShelfName.of("[SHELF_ID]"); + client.listBooks(name); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void listBooksTest2() throws Exception { + Book responsesElement = Book.newBuilder().build(); + ListBooksResponse expectedResponse = + ListBooksResponse.newBuilder() + .setNextPageToken("") + .addAllBooks(Arrays.asList(responsesElement)) + .build(); + mockLibraryService.addResponse(expectedResponse); + + String name = "name3373707"; + + ListBooksPagedResponse pagedListResponse = client.listBooks(name); + + List resources = Lists.newArrayList(pagedListResponse.iterateAll()); + + Assert.assertEquals(1, resources.size()); + Assert.assertEquals(expectedResponse.getBooksList().get(0), resources.get(0)); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + ListBooksRequest actualRequest = ((ListBooksRequest) actualRequests.get(0)); + + Assert.assertEquals(name, actualRequest.getName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void listBooksExceptionTest2() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + String name = "name3373707"; + client.listBooks(name); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void deleteBookTest() throws Exception { + Empty expectedResponse = Empty.newBuilder().build(); + mockLibraryService.addResponse(expectedResponse); + + DeleteBookRequest request = DeleteBookRequest.newBuilder().setName("name3373707").build(); + + client.deleteBook(request); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + DeleteBookRequest actualRequest = ((DeleteBookRequest) actualRequests.get(0)); + + Assert.assertEquals(request.getName(), actualRequest.getName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void deleteBookExceptionTest() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + DeleteBookRequest request = DeleteBookRequest.newBuilder().setName("name3373707").build(); + client.deleteBook(request); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void updateBookTest() throws Exception { + Book expectedResponse = + Book.newBuilder() + .setName(BookName.of("[SHELF_ID]", "[BOOK_ID]").toString()) + .setAuthor("author-1406328437") + .setTitle("title110371416") + .setRead(true) + .build(); + mockLibraryService.addResponse(expectedResponse); + + Book book = Book.newBuilder().build(); + + Book actualResponse = client.updateBook(book); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + UpdateBookRequest actualRequest = ((UpdateBookRequest) actualRequests.get(0)); + + Assert.assertEquals(book, actualRequest.getBook()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void updateBookExceptionTest() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + Book book = Book.newBuilder().build(); + client.updateBook(book); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void moveBookTest() throws Exception { + Book expectedResponse = + Book.newBuilder() + .setName(BookName.of("[SHELF_ID]", "[BOOK_ID]").toString()) + .setAuthor("author-1406328437") + .setTitle("title110371416") + .setRead(true) + .build(); + mockLibraryService.addResponse(expectedResponse); + + BookName name = BookName.of("[SHELF_ID]", "[BOOK_ID]"); + ShelfName otherShelfName = ShelfName.of("[SHELF_ID]"); + + Book actualResponse = client.moveBook(name, otherShelfName); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + MoveBookRequest actualRequest = ((MoveBookRequest) actualRequests.get(0)); + + Assert.assertEquals(name.toString(), actualRequest.getName()); + Assert.assertEquals(otherShelfName.toString(), actualRequest.getOtherShelfName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void moveBookExceptionTest() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + BookName name = BookName.of("[SHELF_ID]", "[BOOK_ID]"); + ShelfName otherShelfName = ShelfName.of("[SHELF_ID]"); + client.moveBook(name, otherShelfName); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void moveBookTest2() throws Exception { + Book expectedResponse = + Book.newBuilder() + .setName(BookName.of("[SHELF_ID]", "[BOOK_ID]").toString()) + .setAuthor("author-1406328437") + .setTitle("title110371416") + .setRead(true) + .build(); + mockLibraryService.addResponse(expectedResponse); + + BookName name = BookName.of("[SHELF_ID]", "[BOOK_ID]"); + String otherShelfName = "other_shelf_name145746959"; + + Book actualResponse = client.moveBook(name, otherShelfName); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + MoveBookRequest actualRequest = ((MoveBookRequest) actualRequests.get(0)); + + Assert.assertEquals(name.toString(), actualRequest.getName()); + Assert.assertEquals(otherShelfName, actualRequest.getOtherShelfName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void moveBookExceptionTest2() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + BookName name = BookName.of("[SHELF_ID]", "[BOOK_ID]"); + String otherShelfName = "other_shelf_name145746959"; + client.moveBook(name, otherShelfName); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void moveBookTest3() throws Exception { + Book expectedResponse = + Book.newBuilder() + .setName(BookName.of("[SHELF_ID]", "[BOOK_ID]").toString()) + .setAuthor("author-1406328437") + .setTitle("title110371416") + .setRead(true) + .build(); + mockLibraryService.addResponse(expectedResponse); + + String name = "name3373707"; + ShelfName otherShelfName = ShelfName.of("[SHELF_ID]"); + + Book actualResponse = client.moveBook(name, otherShelfName); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + MoveBookRequest actualRequest = ((MoveBookRequest) actualRequests.get(0)); + + Assert.assertEquals(name, actualRequest.getName()); + Assert.assertEquals(otherShelfName.toString(), actualRequest.getOtherShelfName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void moveBookExceptionTest3() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + String name = "name3373707"; + ShelfName otherShelfName = ShelfName.of("[SHELF_ID]"); + client.moveBook(name, otherShelfName); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void moveBookTest4() throws Exception { + Book expectedResponse = + Book.newBuilder() + .setName(BookName.of("[SHELF_ID]", "[BOOK_ID]").toString()) + .setAuthor("author-1406328437") + .setTitle("title110371416") + .setRead(true) + .build(); + mockLibraryService.addResponse(expectedResponse); + + String name = "name3373707"; + String otherShelfName = "other_shelf_name145746959"; + + Book actualResponse = client.moveBook(name, otherShelfName); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockLibraryService.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + MoveBookRequest actualRequest = ((MoveBookRequest) actualRequests.get(0)); + + Assert.assertEquals(name, actualRequest.getName()); + Assert.assertEquals(otherShelfName, actualRequest.getOtherShelfName()); + Assert.assertTrue( + channelProvider.isHeaderSent( + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); + } + + @Test + public void moveBookExceptionTest4() throws Exception { + StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); + mockLibraryService.addException(exception); + + try { + String name = "name3373707"; + String otherShelfName = "other_shelf_name145746959"; + client.moveBook(name, otherShelfName); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } +} diff --git a/test/integration/goldens/library/LibraryServiceSettings.java b/test/integration/goldens/library/LibraryServiceSettings.java new file mode 100644 index 0000000000..42a50bd525 --- /dev/null +++ b/test/integration/goldens/library/LibraryServiceSettings.java @@ -0,0 +1,309 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.example.library.v1; + +import static com.google.cloud.example.library.v1.LibraryServiceClient.ListBooksPagedResponse; +import static com.google.cloud.example.library.v1.LibraryServiceClient.ListShelvesPagedResponse; + +import com.google.api.core.ApiFunction; +import com.google.api.core.BetaApi; +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.PagedCallSettings; +import com.google.api.gax.rpc.StubSettings; +import com.google.api.gax.rpc.TransportChannelProvider; +import com.google.api.gax.rpc.UnaryCallSettings; +import com.google.cloud.example.library.v1.stub.LibraryServiceStubSettings; +import com.google.example.library.v1.Book; +import com.google.example.library.v1.CreateBookRequest; +import com.google.example.library.v1.CreateShelfRequest; +import com.google.example.library.v1.DeleteBookRequest; +import com.google.example.library.v1.DeleteShelfRequest; +import com.google.example.library.v1.GetBookRequest; +import com.google.example.library.v1.GetShelfRequest; +import com.google.example.library.v1.ListBooksRequest; +import com.google.example.library.v1.ListBooksResponse; +import com.google.example.library.v1.ListShelvesRequest; +import com.google.example.library.v1.ListShelvesResponse; +import com.google.example.library.v1.MergeShelvesRequest; +import com.google.example.library.v1.MoveBookRequest; +import com.google.example.library.v1.Shelf; +import com.google.example.library.v1.UpdateBookRequest; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.List; +import javax.annotation.Generated; + +// AUTO-GENERATED DOCUMENTATION AND CLASS. +/** + * Settings class to configure an instance of {@link LibraryServiceClient}. + * + *

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

    + *
  • The default service address (library-example.googleapis.com) and default port (443) are + * used. + *
  • Credentials are acquired automatically through Application Default Credentials. + *
  • Retries are configured for idempotent methods but not for non-idempotent methods. + *
+ * + *

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. + * + *

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

{@code
+ * LibraryServiceSettings.Builder libraryServiceSettingsBuilder =
+ *     LibraryServiceSettings.newBuilder();
+ * libraryServiceSettingsBuilder
+ *     .createShelfSettings()
+ *     .setRetrySettings(
+ *         libraryServiceSettingsBuilder
+ *             .createShelfSettings()
+ *             .getRetrySettings()
+ *             .toBuilder()
+ *             .setTotalTimeout(Duration.ofSeconds(30))
+ *             .build());
+ * LibraryServiceSettings libraryServiceSettings = libraryServiceSettingsBuilder.build();
+ * }
+ */ +@Generated("by gapic-generator-java") +public class LibraryServiceSettings extends ClientSettings { + + /** Returns the object with the settings used for calls to createShelf. */ + public UnaryCallSettings createShelfSettings() { + return ((LibraryServiceStubSettings) getStubSettings()).createShelfSettings(); + } + + /** Returns the object with the settings used for calls to getShelf. */ + public UnaryCallSettings getShelfSettings() { + return ((LibraryServiceStubSettings) getStubSettings()).getShelfSettings(); + } + + /** Returns the object with the settings used for calls to listShelves. */ + public PagedCallSettings + listShelvesSettings() { + return ((LibraryServiceStubSettings) getStubSettings()).listShelvesSettings(); + } + + /** Returns the object with the settings used for calls to deleteShelf. */ + public UnaryCallSettings deleteShelfSettings() { + return ((LibraryServiceStubSettings) getStubSettings()).deleteShelfSettings(); + } + + /** Returns the object with the settings used for calls to mergeShelves. */ + public UnaryCallSettings mergeShelvesSettings() { + return ((LibraryServiceStubSettings) getStubSettings()).mergeShelvesSettings(); + } + + /** Returns the object with the settings used for calls to createBook. */ + public UnaryCallSettings createBookSettings() { + return ((LibraryServiceStubSettings) getStubSettings()).createBookSettings(); + } + + /** Returns the object with the settings used for calls to getBook. */ + public UnaryCallSettings getBookSettings() { + return ((LibraryServiceStubSettings) getStubSettings()).getBookSettings(); + } + + /** Returns the object with the settings used for calls to listBooks. */ + public PagedCallSettings + listBooksSettings() { + return ((LibraryServiceStubSettings) getStubSettings()).listBooksSettings(); + } + + /** Returns the object with the settings used for calls to deleteBook. */ + public UnaryCallSettings deleteBookSettings() { + return ((LibraryServiceStubSettings) getStubSettings()).deleteBookSettings(); + } + + /** Returns the object with the settings used for calls to updateBook. */ + public UnaryCallSettings updateBookSettings() { + return ((LibraryServiceStubSettings) getStubSettings()).updateBookSettings(); + } + + /** Returns the object with the settings used for calls to moveBook. */ + public UnaryCallSettings moveBookSettings() { + return ((LibraryServiceStubSettings) getStubSettings()).moveBookSettings(); + } + + public static final LibraryServiceSettings create(LibraryServiceStubSettings stub) + throws IOException { + return new LibraryServiceSettings.Builder(stub.toBuilder()).build(); + } + + /** Returns a builder for the default ExecutorProvider for this service. */ + public static InstantiatingExecutorProvider.Builder defaultExecutorProviderBuilder() { + return LibraryServiceStubSettings.defaultExecutorProviderBuilder(); + } + + /** Returns the default service endpoint. */ + public static String getDefaultEndpoint() { + return LibraryServiceStubSettings.getDefaultEndpoint(); + } + + /** Returns the default service scopes. */ + public static List getDefaultServiceScopes() { + return LibraryServiceStubSettings.getDefaultServiceScopes(); + } + + /** Returns a builder for the default credentials for this service. */ + public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilder() { + return LibraryServiceStubSettings.defaultCredentialsProviderBuilder(); + } + + /** Returns a builder for the default ChannelProvider for this service. */ + public static InstantiatingGrpcChannelProvider.Builder defaultGrpcTransportProviderBuilder() { + return LibraryServiceStubSettings.defaultGrpcTransportProviderBuilder(); + } + + public static TransportChannelProvider defaultTransportChannelProvider() { + return LibraryServiceStubSettings.defaultTransportChannelProvider(); + } + + @BetaApi("The surface for customizing headers is not stable yet and may change in the future.") + public static ApiClientHeaderProvider.Builder defaultApiClientHeaderProviderBuilder() { + return LibraryServiceStubSettings.defaultApiClientHeaderProviderBuilder(); + } + + /** Returns a new builder for this class. */ + public static Builder newBuilder() { + return Builder.createDefault(); + } + + /** Returns a new builder for this class. */ + public static Builder newBuilder(ClientContext clientContext) { + return new Builder(clientContext); + } + + /** Returns a builder containing all the values of this settings class. */ + public Builder toBuilder() { + return new Builder(this); + } + + protected LibraryServiceSettings(Builder settingsBuilder) throws IOException { + super(settingsBuilder); + } + + /** Builder for LibraryServiceSettings. */ + public static class Builder extends ClientSettings.Builder { + + protected Builder() throws IOException { + this(((ClientContext) null)); + } + + protected Builder(ClientContext clientContext) { + super(LibraryServiceStubSettings.newBuilder(clientContext)); + } + + protected Builder(LibraryServiceSettings settings) { + super(settings.getStubSettings().toBuilder()); + } + + protected Builder(LibraryServiceStubSettings.Builder stubSettings) { + super(stubSettings); + } + + private static Builder createDefault() { + return new Builder(LibraryServiceStubSettings.newBuilder()); + } + + public LibraryServiceStubSettings.Builder getStubSettingsBuilder() { + return ((LibraryServiceStubSettings.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 createShelf. */ + public UnaryCallSettings.Builder createShelfSettings() { + return getStubSettingsBuilder().createShelfSettings(); + } + + /** Returns the builder for the settings used for calls to getShelf. */ + public UnaryCallSettings.Builder getShelfSettings() { + return getStubSettingsBuilder().getShelfSettings(); + } + + /** Returns the builder for the settings used for calls to listShelves. */ + public PagedCallSettings.Builder< + ListShelvesRequest, ListShelvesResponse, ListShelvesPagedResponse> + listShelvesSettings() { + return getStubSettingsBuilder().listShelvesSettings(); + } + + /** Returns the builder for the settings used for calls to deleteShelf. */ + public UnaryCallSettings.Builder deleteShelfSettings() { + return getStubSettingsBuilder().deleteShelfSettings(); + } + + /** Returns the builder for the settings used for calls to mergeShelves. */ + public UnaryCallSettings.Builder mergeShelvesSettings() { + return getStubSettingsBuilder().mergeShelvesSettings(); + } + + /** Returns the builder for the settings used for calls to createBook. */ + public UnaryCallSettings.Builder createBookSettings() { + return getStubSettingsBuilder().createBookSettings(); + } + + /** Returns the builder for the settings used for calls to getBook. */ + public UnaryCallSettings.Builder getBookSettings() { + return getStubSettingsBuilder().getBookSettings(); + } + + /** Returns the builder for the settings used for calls to listBooks. */ + public PagedCallSettings.Builder + listBooksSettings() { + return getStubSettingsBuilder().listBooksSettings(); + } + + /** Returns the builder for the settings used for calls to deleteBook. */ + public UnaryCallSettings.Builder deleteBookSettings() { + return getStubSettingsBuilder().deleteBookSettings(); + } + + /** Returns the builder for the settings used for calls to updateBook. */ + public UnaryCallSettings.Builder updateBookSettings() { + return getStubSettingsBuilder().updateBookSettings(); + } + + /** Returns the builder for the settings used for calls to moveBook. */ + public UnaryCallSettings.Builder moveBookSettings() { + return getStubSettingsBuilder().moveBookSettings(); + } + + @Override + public LibraryServiceSettings build() throws IOException { + return new LibraryServiceSettings(this); + } + } +} diff --git a/test/integration/goldens/library/LibraryServiceStub.java b/test/integration/goldens/library/LibraryServiceStub.java new file mode 100644 index 0000000000..c256e68362 --- /dev/null +++ b/test/integration/goldens/library/LibraryServiceStub.java @@ -0,0 +1,105 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.example.library.v1.stub; + +import static com.google.cloud.example.library.v1.LibraryServiceClient.ListBooksPagedResponse; +import static com.google.cloud.example.library.v1.LibraryServiceClient.ListShelvesPagedResponse; + +import com.google.api.gax.core.BackgroundResource; +import com.google.api.gax.rpc.UnaryCallable; +import com.google.example.library.v1.Book; +import com.google.example.library.v1.CreateBookRequest; +import com.google.example.library.v1.CreateShelfRequest; +import com.google.example.library.v1.DeleteBookRequest; +import com.google.example.library.v1.DeleteShelfRequest; +import com.google.example.library.v1.GetBookRequest; +import com.google.example.library.v1.GetShelfRequest; +import com.google.example.library.v1.ListBooksRequest; +import com.google.example.library.v1.ListBooksResponse; +import com.google.example.library.v1.ListShelvesRequest; +import com.google.example.library.v1.ListShelvesResponse; +import com.google.example.library.v1.MergeShelvesRequest; +import com.google.example.library.v1.MoveBookRequest; +import com.google.example.library.v1.Shelf; +import com.google.example.library.v1.UpdateBookRequest; +import com.google.protobuf.Empty; +import javax.annotation.Generated; + +// AUTO-GENERATED DOCUMENTATION AND CLASS. +/** + * Base stub class for the LibraryService service API. + * + *

This class is for advanced usage and reflects the underlying API directly. + */ +@Generated("by gapic-generator") +public abstract class LibraryServiceStub implements BackgroundResource { + + public UnaryCallable createShelfCallable() { + throw new UnsupportedOperationException("Not implemented: createShelfCallable()"); + } + + public UnaryCallable getShelfCallable() { + throw new UnsupportedOperationException("Not implemented: getShelfCallable()"); + } + + public UnaryCallable listShelvesPagedCallable() { + throw new UnsupportedOperationException("Not implemented: listShelvesPagedCallable()"); + } + + public UnaryCallable listShelvesCallable() { + throw new UnsupportedOperationException("Not implemented: listShelvesCallable()"); + } + + public UnaryCallable deleteShelfCallable() { + throw new UnsupportedOperationException("Not implemented: deleteShelfCallable()"); + } + + public UnaryCallable mergeShelvesCallable() { + throw new UnsupportedOperationException("Not implemented: mergeShelvesCallable()"); + } + + public UnaryCallable createBookCallable() { + throw new UnsupportedOperationException("Not implemented: createBookCallable()"); + } + + public UnaryCallable getBookCallable() { + throw new UnsupportedOperationException("Not implemented: getBookCallable()"); + } + + public UnaryCallable listBooksPagedCallable() { + throw new UnsupportedOperationException("Not implemented: listBooksPagedCallable()"); + } + + public UnaryCallable listBooksCallable() { + throw new UnsupportedOperationException("Not implemented: listBooksCallable()"); + } + + public UnaryCallable deleteBookCallable() { + throw new UnsupportedOperationException("Not implemented: deleteBookCallable()"); + } + + public UnaryCallable updateBookCallable() { + throw new UnsupportedOperationException("Not implemented: updateBookCallable()"); + } + + public UnaryCallable moveBookCallable() { + throw new UnsupportedOperationException("Not implemented: moveBookCallable()"); + } + + @Override + public abstract void close(); +} diff --git a/test/integration/goldens/library/LibraryServiceStubSettings.java b/test/integration/goldens/library/LibraryServiceStubSettings.java new file mode 100644 index 0000000000..59c8e8aa5f --- /dev/null +++ b/test/integration/goldens/library/LibraryServiceStubSettings.java @@ -0,0 +1,620 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.example.library.v1.stub; + +import static com.google.cloud.example.library.v1.LibraryServiceClient.ListBooksPagedResponse; +import static com.google.cloud.example.library.v1.LibraryServiceClient.ListShelvesPagedResponse; + +import com.google.api.core.ApiFunction; +import com.google.api.core.ApiFuture; +import com.google.api.core.BetaApi; +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.ApiCallContext; +import com.google.api.gax.rpc.ApiClientHeaderProvider; +import com.google.api.gax.rpc.ClientContext; +import com.google.api.gax.rpc.PageContext; +import com.google.api.gax.rpc.PagedCallSettings; +import com.google.api.gax.rpc.PagedListDescriptor; +import com.google.api.gax.rpc.PagedListResponseFactory; +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.api.gax.rpc.UnaryCallable; +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.example.library.v1.Book; +import com.google.example.library.v1.CreateBookRequest; +import com.google.example.library.v1.CreateShelfRequest; +import com.google.example.library.v1.DeleteBookRequest; +import com.google.example.library.v1.DeleteShelfRequest; +import com.google.example.library.v1.GetBookRequest; +import com.google.example.library.v1.GetShelfRequest; +import com.google.example.library.v1.ListBooksRequest; +import com.google.example.library.v1.ListBooksResponse; +import com.google.example.library.v1.ListShelvesRequest; +import com.google.example.library.v1.ListShelvesResponse; +import com.google.example.library.v1.MergeShelvesRequest; +import com.google.example.library.v1.MoveBookRequest; +import com.google.example.library.v1.Shelf; +import com.google.example.library.v1.UpdateBookRequest; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.List; +import java.util.Objects; +import javax.annotation.Generated; + +// AUTO-GENERATED DOCUMENTATION AND CLASS. +/** + * Settings class to configure an instance of {@link LibraryServiceStub}. + * + *

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

    + *
  • The default service address (library-example.googleapis.com) and default port (443) are + * used. + *
  • Credentials are acquired automatically through Application Default Credentials. + *
  • Retries are configured for idempotent methods but not for non-idempotent methods. + *
+ * + *

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. + * + *

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

{@code
+ * LibraryServiceStubSettings.Builder libraryServiceSettingsBuilder =
+ *     LibraryServiceStubSettings.newBuilder();
+ * libraryServiceSettingsBuilder
+ *     .createShelfSettings()
+ *     .setRetrySettings(
+ *         libraryServiceSettingsBuilder
+ *             .createShelfSettings()
+ *             .getRetrySettings()
+ *             .toBuilder()
+ *             .setTotalTimeout(Duration.ofSeconds(30))
+ *             .build());
+ * LibraryServiceStubSettings libraryServiceSettings = libraryServiceSettingsBuilder.build();
+ * }
+ */ +@BetaApi +@Generated("by gapic-generator-java") +public class LibraryServiceStubSettings extends StubSettings { + /** The default scopes of the service. */ + private static final ImmutableList DEFAULT_SERVICE_SCOPES = + ImmutableList.builder().build(); + + private final UnaryCallSettings createShelfSettings; + private final UnaryCallSettings getShelfSettings; + private final PagedCallSettings + listShelvesSettings; + private final UnaryCallSettings deleteShelfSettings; + private final UnaryCallSettings mergeShelvesSettings; + private final UnaryCallSettings createBookSettings; + private final UnaryCallSettings getBookSettings; + private final PagedCallSettings + listBooksSettings; + private final UnaryCallSettings deleteBookSettings; + private final UnaryCallSettings updateBookSettings; + private final UnaryCallSettings moveBookSettings; + + private static final PagedListDescriptor + LIST_SHELVES_PAGE_STR_DESC = + new PagedListDescriptor() { + @Override + public String emptyToken() { + return ""; + } + + @Override + public ListShelvesRequest injectToken(ListShelvesRequest payload, String token) { + return ListShelvesRequest.newBuilder(payload).setPageToken(token).build(); + } + + @Override + public ListShelvesRequest injectPageSize(ListShelvesRequest payload, int pageSize) { + return ListShelvesRequest.newBuilder(payload).setPageSize(pageSize).build(); + } + + @Override + public Integer extractPageSize(ListShelvesRequest payload) { + return payload.getPageSize(); + } + + @Override + public String extractNextToken(ListShelvesResponse payload) { + return payload.getNextPageToken(); + } + + @Override + public Iterable extractResources(ListShelvesResponse payload) { + return Objects.isNull(payload.getShelvesList()) + ? ImmutableList.of() + : payload.getShelvesList(); + } + }; + + private static final PagedListDescriptor + LIST_BOOKS_PAGE_STR_DESC = + new PagedListDescriptor() { + @Override + public String emptyToken() { + return ""; + } + + @Override + public ListBooksRequest injectToken(ListBooksRequest payload, String token) { + return ListBooksRequest.newBuilder(payload).setPageToken(token).build(); + } + + @Override + public ListBooksRequest injectPageSize(ListBooksRequest payload, int pageSize) { + return ListBooksRequest.newBuilder(payload).setPageSize(pageSize).build(); + } + + @Override + public Integer extractPageSize(ListBooksRequest payload) { + return payload.getPageSize(); + } + + @Override + public String extractNextToken(ListBooksResponse payload) { + return payload.getNextPageToken(); + } + + @Override + public Iterable extractResources(ListBooksResponse payload) { + return Objects.isNull(payload.getBooksList()) + ? ImmutableList.of() + : payload.getBooksList(); + } + }; + + private static final PagedListResponseFactory< + ListShelvesRequest, ListShelvesResponse, ListShelvesPagedResponse> + LIST_SHELVES_PAGE_STR_FACT = + new PagedListResponseFactory< + ListShelvesRequest, ListShelvesResponse, ListShelvesPagedResponse>() { + @Override + public ApiFuture getFuturePagedResponse( + UnaryCallable callable, + ListShelvesRequest request, + ApiCallContext context, + ApiFuture futureResponse) { + PageContext pageContext = + PageContext.create(callable, LIST_SHELVES_PAGE_STR_DESC, request, context); + return ListShelvesPagedResponse.createAsync(pageContext, futureResponse); + } + }; + + private static final PagedListResponseFactory< + ListBooksRequest, ListBooksResponse, ListBooksPagedResponse> + LIST_BOOKS_PAGE_STR_FACT = + new PagedListResponseFactory< + ListBooksRequest, ListBooksResponse, ListBooksPagedResponse>() { + @Override + public ApiFuture getFuturePagedResponse( + UnaryCallable callable, + ListBooksRequest request, + ApiCallContext context, + ApiFuture futureResponse) { + PageContext pageContext = + PageContext.create(callable, LIST_BOOKS_PAGE_STR_DESC, request, context); + return ListBooksPagedResponse.createAsync(pageContext, futureResponse); + } + }; + + /** Returns the object with the settings used for calls to createShelf. */ + public UnaryCallSettings createShelfSettings() { + return createShelfSettings; + } + + /** Returns the object with the settings used for calls to getShelf. */ + public UnaryCallSettings getShelfSettings() { + return getShelfSettings; + } + + /** Returns the object with the settings used for calls to listShelves. */ + public PagedCallSettings + listShelvesSettings() { + return listShelvesSettings; + } + + /** Returns the object with the settings used for calls to deleteShelf. */ + public UnaryCallSettings deleteShelfSettings() { + return deleteShelfSettings; + } + + /** Returns the object with the settings used for calls to mergeShelves. */ + public UnaryCallSettings mergeShelvesSettings() { + return mergeShelvesSettings; + } + + /** Returns the object with the settings used for calls to createBook. */ + public UnaryCallSettings createBookSettings() { + return createBookSettings; + } + + /** Returns the object with the settings used for calls to getBook. */ + public UnaryCallSettings getBookSettings() { + return getBookSettings; + } + + /** Returns the object with the settings used for calls to listBooks. */ + public PagedCallSettings + listBooksSettings() { + return listBooksSettings; + } + + /** Returns the object with the settings used for calls to deleteBook. */ + public UnaryCallSettings deleteBookSettings() { + return deleteBookSettings; + } + + /** Returns the object with the settings used for calls to updateBook. */ + public UnaryCallSettings updateBookSettings() { + return updateBookSettings; + } + + /** Returns the object with the settings used for calls to moveBook. */ + public UnaryCallSettings moveBookSettings() { + return moveBookSettings; + } + + @BetaApi("A restructuring of stub classes is planned, so this may break in the future") + public LibraryServiceStub createStub() throws IOException { + if (getTransportChannelProvider() + .getTransportName() + .equals(GrpcTransportChannel.getGrpcTransportName())) { + return GrpcLibraryServiceStub.create(this); + } + throw new UnsupportedOperationException( + String.format( + "Transport not supported: %s", getTransportChannelProvider().getTransportName())); + } + + /** Returns a builder for the default ExecutorProvider for this service. */ + public static InstantiatingExecutorProvider.Builder defaultExecutorProviderBuilder() { + return InstantiatingExecutorProvider.newBuilder(); + } + + /** Returns the default service endpoint. */ + public static String getDefaultEndpoint() { + return "library-example.googleapis.com:443"; + } + + /** Returns the default service scopes. */ + public static List getDefaultServiceScopes() { + return DEFAULT_SERVICE_SCOPES; + } + + /** Returns a builder for the default credentials for this service. */ + public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilder() { + return GoogleCredentialsProvider.newBuilder().setScopesToApply(DEFAULT_SERVICE_SCOPES); + } + + /** Returns a builder for the default ChannelProvider for this service. */ + public static InstantiatingGrpcChannelProvider.Builder defaultGrpcTransportProviderBuilder() { + return InstantiatingGrpcChannelProvider.newBuilder() + .setMaxInboundMessageSize(Integer.MAX_VALUE); + } + + public static TransportChannelProvider defaultTransportChannelProvider() { + return defaultGrpcTransportProviderBuilder().build(); + } + + @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(LibraryServiceStubSettings.class)) + .setTransportToken( + GaxGrpcProperties.getGrpcTokenName(), GaxGrpcProperties.getGrpcVersion()); + } + + /** Returns a new builder for this class. */ + public static Builder newBuilder() { + return Builder.createDefault(); + } + + /** Returns a new builder for this class. */ + public static Builder newBuilder(ClientContext clientContext) { + return new Builder(clientContext); + } + + /** Returns a builder containing all the values of this settings class. */ + public Builder toBuilder() { + return new Builder(this); + } + + protected LibraryServiceStubSettings(Builder settingsBuilder) throws IOException { + super(settingsBuilder); + + createShelfSettings = settingsBuilder.createShelfSettings().build(); + getShelfSettings = settingsBuilder.getShelfSettings().build(); + listShelvesSettings = settingsBuilder.listShelvesSettings().build(); + deleteShelfSettings = settingsBuilder.deleteShelfSettings().build(); + mergeShelvesSettings = settingsBuilder.mergeShelvesSettings().build(); + createBookSettings = settingsBuilder.createBookSettings().build(); + getBookSettings = settingsBuilder.getBookSettings().build(); + listBooksSettings = settingsBuilder.listBooksSettings().build(); + deleteBookSettings = settingsBuilder.deleteBookSettings().build(); + updateBookSettings = settingsBuilder.updateBookSettings().build(); + moveBookSettings = settingsBuilder.moveBookSettings().build(); + } + + /** Builder for LibraryServiceStubSettings. */ + public static class Builder extends StubSettings.Builder { + private final ImmutableList> unaryMethodSettingsBuilders; + private final UnaryCallSettings.Builder createShelfSettings; + private final UnaryCallSettings.Builder getShelfSettings; + private final PagedCallSettings.Builder< + ListShelvesRequest, ListShelvesResponse, ListShelvesPagedResponse> + listShelvesSettings; + private final UnaryCallSettings.Builder deleteShelfSettings; + private final UnaryCallSettings.Builder mergeShelvesSettings; + private final UnaryCallSettings.Builder createBookSettings; + private final UnaryCallSettings.Builder getBookSettings; + private final PagedCallSettings.Builder< + ListBooksRequest, ListBooksResponse, ListBooksPagedResponse> + listBooksSettings; + private final UnaryCallSettings.Builder deleteBookSettings; + private final UnaryCallSettings.Builder updateBookSettings; + private final UnaryCallSettings.Builder moveBookSettings; + private static final ImmutableMap> + RETRYABLE_CODE_DEFINITIONS; + + static { + ImmutableMap.Builder> definitions = + ImmutableMap.builder(); + definitions.put("no_retry_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().setRpcTimeoutMultiplier(1.0).build(); + definitions.put("no_retry_params", settings); + RETRY_PARAM_DEFINITIONS = definitions.build(); + } + + protected Builder() { + this(((ClientContext) null)); + } + + protected Builder(ClientContext clientContext) { + super(clientContext); + + createShelfSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); + getShelfSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); + listShelvesSettings = PagedCallSettings.newBuilder(LIST_SHELVES_PAGE_STR_FACT); + deleteShelfSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); + mergeShelvesSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); + createBookSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); + getBookSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); + listBooksSettings = PagedCallSettings.newBuilder(LIST_BOOKS_PAGE_STR_FACT); + deleteBookSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); + updateBookSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); + moveBookSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); + + unaryMethodSettingsBuilders = + ImmutableList.>of( + createShelfSettings, + getShelfSettings, + listShelvesSettings, + deleteShelfSettings, + mergeShelvesSettings, + createBookSettings, + getBookSettings, + listBooksSettings, + deleteBookSettings, + updateBookSettings, + moveBookSettings); + initDefaults(this); + } + + protected Builder(LibraryServiceStubSettings settings) { + super(settings); + + createShelfSettings = settings.createShelfSettings.toBuilder(); + getShelfSettings = settings.getShelfSettings.toBuilder(); + listShelvesSettings = settings.listShelvesSettings.toBuilder(); + deleteShelfSettings = settings.deleteShelfSettings.toBuilder(); + mergeShelvesSettings = settings.mergeShelvesSettings.toBuilder(); + createBookSettings = settings.createBookSettings.toBuilder(); + getBookSettings = settings.getBookSettings.toBuilder(); + listBooksSettings = settings.listBooksSettings.toBuilder(); + deleteBookSettings = settings.deleteBookSettings.toBuilder(); + updateBookSettings = settings.updateBookSettings.toBuilder(); + moveBookSettings = settings.moveBookSettings.toBuilder(); + + unaryMethodSettingsBuilders = + ImmutableList.>of( + createShelfSettings, + getShelfSettings, + listShelvesSettings, + deleteShelfSettings, + mergeShelvesSettings, + createBookSettings, + getBookSettings, + listBooksSettings, + deleteBookSettings, + updateBookSettings, + moveBookSettings); + } + + 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 + .createShelfSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("no_retry_codes")) + .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("no_retry_params")); + + builder + .getShelfSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("no_retry_codes")) + .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("no_retry_params")); + + builder + .listShelvesSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("no_retry_codes")) + .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("no_retry_params")); + + builder + .deleteShelfSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("no_retry_codes")) + .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("no_retry_params")); + + builder + .mergeShelvesSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("no_retry_codes")) + .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("no_retry_params")); + + builder + .createBookSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("no_retry_codes")) + .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("no_retry_params")); + + builder + .getBookSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("no_retry_codes")) + .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("no_retry_params")); + + builder + .listBooksSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("no_retry_codes")) + .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("no_retry_params")); + + builder + .deleteBookSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("no_retry_codes")) + .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("no_retry_params")); + + builder + .updateBookSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("no_retry_codes")) + .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("no_retry_params")); + + builder + .moveBookSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("no_retry_codes")) + .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("no_retry_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 createShelf. */ + public UnaryCallSettings.Builder createShelfSettings() { + return createShelfSettings; + } + + /** Returns the builder for the settings used for calls to getShelf. */ + public UnaryCallSettings.Builder getShelfSettings() { + return getShelfSettings; + } + + /** Returns the builder for the settings used for calls to listShelves. */ + public PagedCallSettings.Builder< + ListShelvesRequest, ListShelvesResponse, ListShelvesPagedResponse> + listShelvesSettings() { + return listShelvesSettings; + } + + /** Returns the builder for the settings used for calls to deleteShelf. */ + public UnaryCallSettings.Builder deleteShelfSettings() { + return deleteShelfSettings; + } + + /** Returns the builder for the settings used for calls to mergeShelves. */ + public UnaryCallSettings.Builder mergeShelvesSettings() { + return mergeShelvesSettings; + } + + /** Returns the builder for the settings used for calls to createBook. */ + public UnaryCallSettings.Builder createBookSettings() { + return createBookSettings; + } + + /** Returns the builder for the settings used for calls to getBook. */ + public UnaryCallSettings.Builder getBookSettings() { + return getBookSettings; + } + + /** Returns the builder for the settings used for calls to listBooks. */ + public PagedCallSettings.Builder + listBooksSettings() { + return listBooksSettings; + } + + /** Returns the builder for the settings used for calls to deleteBook. */ + public UnaryCallSettings.Builder deleteBookSettings() { + return deleteBookSettings; + } + + /** Returns the builder for the settings used for calls to updateBook. */ + public UnaryCallSettings.Builder updateBookSettings() { + return updateBookSettings; + } + + /** Returns the builder for the settings used for calls to moveBook. */ + public UnaryCallSettings.Builder moveBookSettings() { + return moveBookSettings; + } + + @Override + public LibraryServiceStubSettings build() throws IOException { + return new LibraryServiceStubSettings(this); + } + } +} diff --git a/test/integration/goldens/library/MockLibraryService.java b/test/integration/goldens/library/MockLibraryService.java new file mode 100644 index 0000000000..64c111250b --- /dev/null +++ b/test/integration/goldens/library/MockLibraryService.java @@ -0,0 +1,59 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.example.library.v1; + +import com.google.api.core.BetaApi; +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; + +@BetaApi +@Generated("by gapic-generator-java") +public class MockLibraryService implements MockGrpcService { + private final MockLibraryServiceImpl serviceImpl; + + public MockLibraryService() { + serviceImpl = new MockLibraryServiceImpl(); + } + + @Override + public List getRequests() { + return serviceImpl.getRequests(); + } + + @Override + public void addResponse(AbstractMessage response) { + serviceImpl.addResponse(response); + } + + @Override + public void addException(Exception exception) { + serviceImpl.addException(exception); + } + + @Override + public ServerServiceDefinition getServiceDefinition() { + return serviceImpl.bindService(); + } + + @Override + public void reset() { + serviceImpl.reset(); + } +} diff --git a/test/integration/goldens/library/MockLibraryServiceImpl.java b/test/integration/goldens/library/MockLibraryServiceImpl.java new file mode 100644 index 0000000000..f9c59c9467 --- /dev/null +++ b/test/integration/goldens/library/MockLibraryServiceImpl.java @@ -0,0 +1,232 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.example.library.v1; + +import com.google.api.core.BetaApi; +import com.google.example.library.v1.Book; +import com.google.example.library.v1.CreateBookRequest; +import com.google.example.library.v1.CreateShelfRequest; +import com.google.example.library.v1.DeleteBookRequest; +import com.google.example.library.v1.DeleteShelfRequest; +import com.google.example.library.v1.GetBookRequest; +import com.google.example.library.v1.GetShelfRequest; +import com.google.example.library.v1.LibraryServiceGrpc.LibraryServiceImplBase; +import com.google.example.library.v1.ListBooksRequest; +import com.google.example.library.v1.ListBooksResponse; +import com.google.example.library.v1.ListShelvesRequest; +import com.google.example.library.v1.ListShelvesResponse; +import com.google.example.library.v1.MergeShelvesRequest; +import com.google.example.library.v1.MoveBookRequest; +import com.google.example.library.v1.Shelf; +import com.google.example.library.v1.UpdateBookRequest; +import com.google.protobuf.AbstractMessage; +import com.google.protobuf.Empty; +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; + +@BetaApi +@Generated("by gapic-generator-java") +public class MockLibraryServiceImpl extends LibraryServiceImplBase { + private List requests; + private Queue responses; + + public MockLibraryServiceImpl() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + public List getRequests() { + return requests; + } + + public void addResponse(AbstractMessage response) { + responses.add(response); + } + + public void setResponses(List responses) { + this.responses = new LinkedList(responses); + } + + public void addException(Exception exception) { + responses.add(exception); + } + + public void reset() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + @Override + public void createShelf(CreateShelfRequest request, StreamObserver responseObserver) { + Object response = responses.remove(); + if (response instanceof Shelf) { + requests.add(request); + responseObserver.onNext(((Shelf) response)); + responseObserver.onCompleted(); + } else if (response instanceof Exception) { + responseObserver.onError(((Exception) response)); + } else { + responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); + } + } + + @Override + public void getShelf(GetShelfRequest request, StreamObserver responseObserver) { + Object response = responses.remove(); + if (response instanceof Shelf) { + requests.add(request); + responseObserver.onNext(((Shelf) response)); + responseObserver.onCompleted(); + } else if (response instanceof Exception) { + responseObserver.onError(((Exception) response)); + } else { + responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); + } + } + + @Override + public void listShelves( + ListShelvesRequest request, StreamObserver responseObserver) { + Object response = responses.remove(); + if (response instanceof ListShelvesResponse) { + requests.add(request); + responseObserver.onNext(((ListShelvesResponse) response)); + responseObserver.onCompleted(); + } else if (response instanceof Exception) { + responseObserver.onError(((Exception) response)); + } else { + responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); + } + } + + @Override + public void deleteShelf(DeleteShelfRequest request, StreamObserver responseObserver) { + Object response = responses.remove(); + 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("Unrecognized response type")); + } + } + + @Override + public void mergeShelves(MergeShelvesRequest request, StreamObserver responseObserver) { + Object response = responses.remove(); + if (response instanceof Shelf) { + requests.add(request); + responseObserver.onNext(((Shelf) response)); + responseObserver.onCompleted(); + } else if (response instanceof Exception) { + responseObserver.onError(((Exception) response)); + } else { + responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); + } + } + + @Override + public void createBook(CreateBookRequest request, StreamObserver responseObserver) { + Object response = responses.remove(); + if (response instanceof Book) { + requests.add(request); + responseObserver.onNext(((Book) response)); + responseObserver.onCompleted(); + } else if (response instanceof Exception) { + responseObserver.onError(((Exception) response)); + } else { + responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); + } + } + + @Override + public void getBook(GetBookRequest request, StreamObserver responseObserver) { + Object response = responses.remove(); + if (response instanceof Book) { + requests.add(request); + responseObserver.onNext(((Book) response)); + responseObserver.onCompleted(); + } else if (response instanceof Exception) { + responseObserver.onError(((Exception) response)); + } else { + responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); + } + } + + @Override + public void listBooks( + ListBooksRequest request, StreamObserver responseObserver) { + Object response = responses.remove(); + if (response instanceof ListBooksResponse) { + requests.add(request); + responseObserver.onNext(((ListBooksResponse) response)); + responseObserver.onCompleted(); + } else if (response instanceof Exception) { + responseObserver.onError(((Exception) response)); + } else { + responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); + } + } + + @Override + public void deleteBook(DeleteBookRequest request, StreamObserver responseObserver) { + Object response = responses.remove(); + 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("Unrecognized response type")); + } + } + + @Override + public void updateBook(UpdateBookRequest request, StreamObserver responseObserver) { + Object response = responses.remove(); + if (response instanceof Book) { + requests.add(request); + responseObserver.onNext(((Book) response)); + responseObserver.onCompleted(); + } else if (response instanceof Exception) { + responseObserver.onError(((Exception) response)); + } else { + responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); + } + } + + @Override + public void moveBook(MoveBookRequest request, StreamObserver responseObserver) { + Object response = responses.remove(); + if (response instanceof Book) { + requests.add(request); + responseObserver.onNext(((Book) response)); + responseObserver.onCompleted(); + } else if (response instanceof Exception) { + responseObserver.onError(((Exception) response)); + } else { + responseObserver.onError(new IllegalArgumentException("Unrecognized response type")); + } + } +} diff --git a/test/integration/goldens/library/ShelfName.java b/test/integration/goldens/library/ShelfName.java new file mode 100644 index 0000000000..cc50ef5f07 --- /dev/null +++ b/test/integration/goldens/library/ShelfName.java @@ -0,0 +1,168 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.example.library.v1; + +import com.google.api.pathtemplate.PathTemplate; +import com.google.api.resourcenames.ResourceName; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableMap; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import javax.annotation.Generated; + +// AUTO-GENERATED DOCUMENTATION AND CLASS. +@Generated("by gapic-generator-java") +public class ShelfName implements ResourceName { + private static final PathTemplate SHELF_ID = + PathTemplate.createWithoutUrlEncoding("shelves/{shelf_id}"); + private volatile Map fieldValuesMap; + private final String shelfId; + + @Deprecated + protected ShelfName() { + shelfId = null; + } + + private ShelfName(Builder builder) { + shelfId = Preconditions.checkNotNull(builder.getShelfId()); + } + + public String getShelfId() { + return shelfId; + } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder toBuilder() { + return new Builder(this); + } + + public static ShelfName of(String shelfId) { + return newBuilder().setShelfId(shelfId).build(); + } + + public static String format(String shelfId) { + return newBuilder().setShelfId(shelfId).build().toString(); + } + + public static ShelfName parse(String formattedString) { + if (formattedString.isEmpty()) { + return null; + } + Map matchMap = + SHELF_ID.validatedMatch( + formattedString, "ShelfName.parse: formattedString not in valid format"); + return of(matchMap.get("shelf_id")); + } + + public static List parseList(List formattedStrings) { + List list = new ArrayList<>(formattedStrings.size()); + for (String formattedString : formattedStrings) { + list.add(parse(formattedString)); + } + return list; + } + + public static List toStringList(List values) { + List list = new ArrayList<>(values.size()); + for (ShelfName value : values) { + if (Objects.isNull(value)) { + list.add(""); + } else { + list.add(value.toString()); + } + } + return list; + } + + public static boolean isParsableFrom(String formattedString) { + return SHELF_ID.matches(formattedString); + } + + @Override + public Map getFieldValuesMap() { + if (Objects.isNull(fieldValuesMap)) { + synchronized (this) { + if (Objects.isNull(fieldValuesMap)) { + ImmutableMap.Builder fieldMapBuilder = ImmutableMap.builder(); + if (!Objects.isNull(shelfId)) { + fieldMapBuilder.put("shelf_id", shelfId); + } + fieldValuesMap = fieldMapBuilder.build(); + } + } + } + return fieldValuesMap; + } + + public String getFieldValue(String fieldName) { + return getFieldValuesMap().get(fieldName); + } + + @Override + public String toString() { + return SHELF_ID.instantiate("shelf_id", shelfId); + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } + if (o != null || getClass() == o.getClass()) { + ShelfName that = ((ShelfName) o); + return Objects.equals(this.shelfId, that.shelfId); + } + return false; + } + + @Override + public int hashCode() { + int h = 1; + h *= 1000003; + h ^= Objects.hashCode(shelfId); + return h; + } + + /** Builder for shelves/{shelf_id}. */ + public static class Builder { + private String shelfId; + + protected Builder() {} + + public String getShelfId() { + return shelfId; + } + + public Builder setShelfId(String shelfId) { + this.shelfId = shelfId; + return this; + } + + private Builder(ShelfName shelfName) { + shelfId = shelfName.shelfId; + } + + public ShelfName build() { + return new ShelfName(this); + } + } +} diff --git a/test/integration/goldens/library/package-info.java b/test/integration/goldens/library/package-info.java new file mode 100644 index 0000000000..17a7499dac --- /dev/null +++ b/test/integration/goldens/library/package-info.java @@ -0,0 +1,36 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * The interfaces provided are listed below, along with usage samples. + * + *

======================= LibraryServiceClient ======================= + * + *

Service Description: This API represents a simple digital library. It lets you manage Shelf + * resources and Book resources in the library. It defines the following resource model: + * + *

- The API has a collection of [Shelf][google.example.library.v1.Shelf] resources, named + * `shelves/*` + * + *

- Each Shelf has a collection of [Book][google.example.library.v1.Book] resources, named + * `shelves/*/books/*` + * + *

Sample for LibraryServiceClient: + */ +@Generated("by gapic-generator-java") +package com.google.cloud.example.library.v1; + +import javax.annotation.Generated;