From 33ba4238f6e61b0d5629ddc4fc09c327d95c8c6e Mon Sep 17 00:00:00 2001 From: Alice <65933803+alicejli@users.noreply.github.com> Date: Thu, 8 Feb 2024 16:59:51 -0500 Subject: [PATCH] chore: refactor golden tests for autopopulation feature (#2446) As a clean-up follow up to https://github.com/googleapis/sdk-platform-java/pull/2353, this PR refactors the unit golden tests for the autopopulation feature to a separate, new proto called `auto_populate_field_testing.proto`. --- .../DefaultValueComposerTest.java | 6 - .../GrpcServiceStubClassComposerTest.java | 10 + .../composer/grpc/goldens/EchoClient.golden | 48 +-- .../grpc/goldens/EchoClientTest.golden | 72 +---- .../goldens/GrpcAutoPopulateFieldStub.golden | 282 +++++++++++++++++ .../composer/grpc/goldens/GrpcEchoStub.golden | 13 - .../samples/echoclient/AsyncChat.golden | 6 - .../samples/echoclient/AsyncChatAgain.golden | 6 - .../samples/echoclient/AsyncCollect.golden | 6 - .../echoclient/AsyncCollideName.golden | 6 - .../samples/echoclient/AsyncEcho.golden | 6 - .../samples/echoclient/AsyncExpand.golden | 6 +- .../samples/echoclient/SyncCollideName.golden | 6 - .../samples/echoclient/SyncEcho.golden | 6 - .../HttpJsonServiceStubClassComposerTest.java | 10 + ...ttpJsonAutoPopulateFieldTestingStub.golden | 288 ++++++++++++++++++ .../rest/goldens/HttpJsonEchoStub.golden | 13 - ...lientCallableMethodSampleComposerTest.java | 24 +- ...ServiceClientHeaderSampleComposerTest.java | 12 +- ...ServiceClientMethodSampleComposerTest.java | 12 - .../gapic/protoparser/ParserTest.java | 61 ++-- .../protoparser/ServiceYamlParserTest.java | 24 +- .../test/protoloader/TestProtoLoader.java | 35 +++ .../proto/auto_populate_field_testing.proto | 144 +++++++++ .../src/test/proto/echo.proto | 37 --- .../auto_populate_field_testing.yaml | 24 ++ .../src/test/resources/echo_v1beta1.yaml | 11 +- 27 files changed, 847 insertions(+), 327 deletions(-) create mode 100644 gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcAutoPopulateFieldStub.golden create mode 100644 gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonAutoPopulateFieldTestingStub.golden create mode 100644 gapic-generator-java/src/test/proto/auto_populate_field_testing.proto create mode 100644 gapic-generator-java/src/test/resources/auto_populate_field_testing.yaml diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/defaultvalue/DefaultValueComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/defaultvalue/DefaultValueComposerTest.java index 3345645c8c..264060f474 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/defaultvalue/DefaultValueComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/defaultvalue/DefaultValueComposerTest.java @@ -568,12 +568,6 @@ public void createSimpleMessage_containsMessagesEnumsAndResourceName() { "EchoRequest.newBuilder().setName(" + "FoobarName.ofProjectFoobarName(\"[PROJECT]\", \"[FOOBAR]\").toString())" + ".setParent(FoobarName.ofProjectFoobarName(\"[PROJECT]\", \"[FOOBAR]\").toString())" - + ".setRequestId(\"requestId693933066\")" - + ".setSecondRequestId(\"secondRequestId344404470\")" - + ".setThirdRequestId(true)" - + ".setFourthRequestId(\"fourthRequestId-2116417776\")" - + ".setFifthRequestId(\"fifthRequestId959024147\")" - + ".setSixthRequestId(\"sixthRequestId1005218260\")" + ".setSeverity(Severity.forNumber(0))" + ".setFoobar(Foobar.newBuilder().build()).build()", writerVisitor.write()); diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/GrpcServiceStubClassComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/GrpcServiceStubClassComposerTest.java index efe0ce8222..1c00534433 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/GrpcServiceStubClassComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/GrpcServiceStubClassComposerTest.java @@ -82,4 +82,14 @@ public void generateGrpcServiceStubClass_createBatchingCallable() { Assert.assertGoldenClass(this.getClass(), clazz, "GrpcLoggingStub.golden"); Assert.assertEmptySamples(clazz.samples()); } + + @Test + public void generateGrpcServiceStubClass_autopopulateField() { + GapicContext context = GrpcTestProtoLoader.instance().parseAutoPopulateFieldTesting(); + Service service = context.services().get(0); + GapicClass clazz = GrpcServiceStubClassComposer.instance().generate(context, service); + + Assert.assertGoldenClass(this.getClass(), clazz, "GrpcAutoPopulateFieldStub.golden"); + Assert.assertEmptySamples(clazz.samples()); + } } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/EchoClient.golden b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/EchoClient.golden index f1bf7437b4..431700191c 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/EchoClient.golden +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/EchoClient.golden @@ -517,12 +517,6 @@ public class EchoClient implements BackgroundResource { * EchoRequest.newBuilder() * .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) * .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - * .setRequestId("requestId693933066") - * .setSecondRequestId("secondRequestId344404470") - * .setThirdRequestId(true) - * .setFourthRequestId("fourthRequestId-2116417776") - * .setFifthRequestId("fifthRequestId959024147") - * .setSixthRequestId("sixthRequestId1005218260") * .setSeverity(Severity.forNumber(0)) * .setFoobar(Foobar.newBuilder().build()) * .build(); @@ -552,12 +546,6 @@ public class EchoClient implements BackgroundResource { * EchoRequest.newBuilder() * .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) * .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - * .setRequestId("requestId693933066") - * .setSecondRequestId("secondRequestId344404470") - * .setThirdRequestId(true) - * .setFourthRequestId("fourthRequestId-2116417776") - * .setFifthRequestId("fifthRequestId959024147") - * .setSixthRequestId("sixthRequestId1005218260") * .setSeverity(Severity.forNumber(0)) * .setFoobar(Foobar.newBuilder().build()) * .build(); @@ -583,11 +571,7 @@ public class EchoClient implements BackgroundResource { * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library * try (EchoClient echoClient = EchoClient.create()) { * ExpandRequest request = - * ExpandRequest.newBuilder() - * .setContent("content951530617") - * .setInfo("info3237038") - * .setRequestId("requestId693933066") - * .build(); + * ExpandRequest.newBuilder().setContent("content951530617").setInfo("info3237038").build(); * ServerStream stream = echoClient.expandCallable().call(request); * for (EchoResponse response : stream) { * // Do something when a response is received. @@ -633,12 +617,6 @@ public class EchoClient implements BackgroundResource { * EchoRequest.newBuilder() * .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) * .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - * .setRequestId("requestId693933066") - * .setSecondRequestId("secondRequestId344404470") - * .setThirdRequestId(true) - * .setFourthRequestId("fourthRequestId-2116417776") - * .setFifthRequestId("fifthRequestId959024147") - * .setSixthRequestId("sixthRequestId1005218260") * .setSeverity(Severity.forNumber(0)) * .setFoobar(Foobar.newBuilder().build()) * .build(); @@ -666,12 +644,6 @@ public class EchoClient implements BackgroundResource { * EchoRequest.newBuilder() * .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) * .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - * .setRequestId("requestId693933066") - * .setSecondRequestId("secondRequestId344404470") - * .setThirdRequestId(true) - * .setFourthRequestId("fourthRequestId-2116417776") - * .setFifthRequestId("fifthRequestId959024147") - * .setSixthRequestId("sixthRequestId1005218260") * .setSeverity(Severity.forNumber(0)) * .setFoobar(Foobar.newBuilder().build()) * .build(); @@ -702,12 +674,6 @@ public class EchoClient implements BackgroundResource { * EchoRequest.newBuilder() * .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) * .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - * .setRequestId("requestId693933066") - * .setSecondRequestId("secondRequestId344404470") - * .setThirdRequestId(true) - * .setFourthRequestId("fourthRequestId-2116417776") - * .setFifthRequestId("fifthRequestId959024147") - * .setSixthRequestId("sixthRequestId1005218260") * .setSeverity(Severity.forNumber(0)) * .setFoobar(Foobar.newBuilder().build()) * .build(); @@ -1116,12 +1082,6 @@ public class EchoClient implements BackgroundResource { * EchoRequest.newBuilder() * .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) * .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - * .setRequestId("requestId693933066") - * .setSecondRequestId("secondRequestId344404470") - * .setThirdRequestId(true) - * .setFourthRequestId("fourthRequestId-2116417776") - * .setFifthRequestId("fifthRequestId959024147") - * .setSixthRequestId("sixthRequestId1005218260") * .setSeverity(Severity.forNumber(0)) * .setFoobar(Foobar.newBuilder().build()) * .build(); @@ -1151,12 +1111,6 @@ public class EchoClient implements BackgroundResource { * EchoRequest.newBuilder() * .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) * .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - * .setRequestId("requestId693933066") - * .setSecondRequestId("secondRequestId344404470") - * .setThirdRequestId(true) - * .setFourthRequestId("fourthRequestId-2116417776") - * .setFifthRequestId("fifthRequestId959024147") - * .setSixthRequestId("sixthRequestId1005218260") * .setSeverity(Severity.forNumber(0)) * .setFoobar(Foobar.newBuilder().build()) * .build(); diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/EchoClientTest.golden b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/EchoClientTest.golden index a2a88d6bf3..1372cdd94e 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/EchoClientTest.golden +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/EchoClientTest.golden @@ -108,12 +108,6 @@ public class EchoClientTest { EchoRequest.newBuilder() .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - .setRequestId("requestId693933066") - .setSecondRequestId("secondRequestId344404470") - .setThirdRequestId(true) - .setFourthRequestId("fourthRequestId-2116417776") - .setFifthRequestId("fifthRequestId959024147") - .setSixthRequestId("sixthRequestId1005218260") .setSeverity(Severity.forNumber(0)) .setFoobar(Foobar.newBuilder().build()) .build(); @@ -409,11 +403,7 @@ public class EchoClientTest { .build(); mockEcho.addResponse(expectedResponse); ExpandRequest request = - ExpandRequest.newBuilder() - .setContent("content951530617") - .setInfo("info3237038") - .setRequestId("requestId693933066") - .build(); + ExpandRequest.newBuilder().setContent("content951530617").setInfo("info3237038").build(); MockStreamObserver responseObserver = new MockStreamObserver<>(); @@ -430,11 +420,7 @@ public class EchoClientTest { StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); mockEcho.addException(exception); ExpandRequest request = - ExpandRequest.newBuilder() - .setContent("content951530617") - .setInfo("info3237038") - .setRequestId("requestId693933066") - .build(); + ExpandRequest.newBuilder().setContent("content951530617").setInfo("info3237038").build(); MockStreamObserver responseObserver = new MockStreamObserver<>(); @@ -463,12 +449,6 @@ public class EchoClientTest { EchoRequest.newBuilder() .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - .setRequestId("requestId693933066") - .setSecondRequestId("secondRequestId344404470") - .setThirdRequestId(true) - .setFourthRequestId("fourthRequestId-2116417776") - .setFifthRequestId("fifthRequestId959024147") - .setSixthRequestId("sixthRequestId1005218260") .setSeverity(Severity.forNumber(0)) .setFoobar(Foobar.newBuilder().build()) .build(); @@ -494,12 +474,6 @@ public class EchoClientTest { EchoRequest.newBuilder() .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - .setRequestId("requestId693933066") - .setSecondRequestId("secondRequestId344404470") - .setThirdRequestId(true) - .setFourthRequestId("fourthRequestId-2116417776") - .setFifthRequestId("fifthRequestId959024147") - .setSixthRequestId("sixthRequestId1005218260") .setSeverity(Severity.forNumber(0)) .setFoobar(Foobar.newBuilder().build()) .build(); @@ -533,12 +507,6 @@ public class EchoClientTest { EchoRequest.newBuilder() .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - .setRequestId("requestId693933066") - .setSecondRequestId("secondRequestId344404470") - .setThirdRequestId(true) - .setFourthRequestId("fourthRequestId-2116417776") - .setFifthRequestId("fifthRequestId959024147") - .setSixthRequestId("sixthRequestId1005218260") .setSeverity(Severity.forNumber(0)) .setFoobar(Foobar.newBuilder().build()) .build(); @@ -564,12 +532,6 @@ public class EchoClientTest { EchoRequest.newBuilder() .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - .setRequestId("requestId693933066") - .setSecondRequestId("secondRequestId344404470") - .setThirdRequestId(true) - .setFourthRequestId("fourthRequestId-2116417776") - .setFifthRequestId("fifthRequestId959024147") - .setSixthRequestId("sixthRequestId1005218260") .setSeverity(Severity.forNumber(0)) .setFoobar(Foobar.newBuilder().build()) .build(); @@ -603,12 +565,6 @@ public class EchoClientTest { EchoRequest.newBuilder() .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - .setRequestId("requestId693933066") - .setSecondRequestId("secondRequestId344404470") - .setThirdRequestId(true) - .setFourthRequestId("fourthRequestId-2116417776") - .setFifthRequestId("fifthRequestId959024147") - .setSixthRequestId("sixthRequestId1005218260") .setSeverity(Severity.forNumber(0)) .setFoobar(Foobar.newBuilder().build()) .build(); @@ -634,12 +590,6 @@ public class EchoClientTest { EchoRequest.newBuilder() .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - .setRequestId("requestId693933066") - .setSecondRequestId("secondRequestId344404470") - .setThirdRequestId(true) - .setFourthRequestId("fourthRequestId-2116417776") - .setFifthRequestId("fifthRequestId959024147") - .setSixthRequestId("sixthRequestId1005218260") .setSeverity(Severity.forNumber(0)) .setFoobar(Foobar.newBuilder().build()) .build(); @@ -898,12 +848,6 @@ public class EchoClientTest { EchoRequest.newBuilder() .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - .setRequestId("requestId693933066") - .setSecondRequestId("secondRequestId344404470") - .setThirdRequestId(true) - .setFourthRequestId("fourthRequestId-2116417776") - .setFifthRequestId("fifthRequestId959024147") - .setSixthRequestId("sixthRequestId1005218260") .setSeverity(Severity.forNumber(0)) .setFoobar(Foobar.newBuilder().build()) .build(); @@ -917,12 +861,6 @@ public class EchoClientTest { Assert.assertEquals(request.getName(), actualRequest.getName()); Assert.assertEquals(request.getParent(), actualRequest.getParent()); - Assert.assertEquals(request.getRequestId(), actualRequest.getRequestId()); - Assert.assertEquals(request.getSecondRequestId(), actualRequest.getSecondRequestId()); - Assert.assertEquals(request.getThirdRequestId(), actualRequest.getThirdRequestId()); - Assert.assertEquals(request.getFourthRequestId(), actualRequest.getFourthRequestId()); - Assert.assertEquals(request.getFifthRequestId(), actualRequest.getFifthRequestId()); - Assert.assertEquals(request.getSixthRequestId(), actualRequest.getSixthRequestId()); Assert.assertEquals(request.getContent(), actualRequest.getContent()); Assert.assertEquals(request.getError(), actualRequest.getError()); Assert.assertEquals(request.getSeverity(), actualRequest.getSeverity()); @@ -943,12 +881,6 @@ public class EchoClientTest { EchoRequest.newBuilder() .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - .setRequestId("requestId693933066") - .setSecondRequestId("secondRequestId344404470") - .setThirdRequestId(true) - .setFourthRequestId("fourthRequestId-2116417776") - .setFifthRequestId("fifthRequestId959024147") - .setSixthRequestId("sixthRequestId1005218260") .setSeverity(Severity.forNumber(0)) .setFoobar(Foobar.newBuilder().build()) .build(); diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcAutoPopulateFieldStub.golden b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcAutoPopulateFieldStub.golden new file mode 100644 index 0000000000..cfef350643 --- /dev/null +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcAutoPopulateFieldStub.golden @@ -0,0 +1,282 @@ +package com.google.auto.populate.field.stub; + +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.BidiStreamingCallable; +import com.google.api.gax.rpc.ClientContext; +import com.google.api.gax.rpc.ClientStreamingCallable; +import com.google.api.gax.rpc.ServerStreamingCallable; +import com.google.api.gax.rpc.UnaryCallable; +import com.google.auto.populate.field.AutoPopulateFieldTestingEchoRequest; +import com.google.auto.populate.field.AutoPopulateFieldTestingEchoResponse; +import com.google.auto.populate.field.AutoPopulateFieldTestingExpandRequest; +import com.google.common.base.Strings; +import com.google.longrunning.stub.GrpcOperationsStub; +import io.grpc.MethodDescriptor; +import io.grpc.protobuf.ProtoUtils; +import java.io.IOException; +import java.util.UUID; +import java.util.concurrent.TimeUnit; +import javax.annotation.Generated; + +// AUTO-GENERATED DOCUMENTATION AND CLASS. +/** + * gRPC stub implementation for the AutoPopulateFieldTesting service API. + * + *

This class is for advanced usage and reflects the underlying API directly. + */ +@Generated("by gapic-generator-java") +public class GrpcAutoPopulateFieldTestingStub extends AutoPopulateFieldTestingStub { + private static final MethodDescriptor< + AutoPopulateFieldTestingEchoRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingEchoMethodDescriptor = + MethodDescriptor + . + newBuilder() + .setType(MethodDescriptor.MethodType.UNARY) + .setFullMethodName( + "google.auto.populate.field.AutoPopulateFieldTesting/AutoPopulateFieldTestingEcho") + .setRequestMarshaller( + ProtoUtils.marshaller(AutoPopulateFieldTestingEchoRequest.getDefaultInstance())) + .setResponseMarshaller( + ProtoUtils.marshaller(AutoPopulateFieldTestingEchoResponse.getDefaultInstance())) + .build(); + + private static final MethodDescriptor< + AutoPopulateFieldTestingExpandRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingExpandMethodDescriptor = + MethodDescriptor + . + newBuilder() + .setType(MethodDescriptor.MethodType.SERVER_STREAMING) + .setFullMethodName( + "google.auto.populate.field.AutoPopulateFieldTesting/AutoPopulateFieldTestingExpand") + .setRequestMarshaller( + ProtoUtils.marshaller(AutoPopulateFieldTestingExpandRequest.getDefaultInstance())) + .setResponseMarshaller( + ProtoUtils.marshaller(AutoPopulateFieldTestingEchoResponse.getDefaultInstance())) + .build(); + + private static final MethodDescriptor< + AutoPopulateFieldTestingEchoRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingCollectMethodDescriptor = + MethodDescriptor + . + newBuilder() + .setType(MethodDescriptor.MethodType.CLIENT_STREAMING) + .setFullMethodName( + "google.auto.populate.field.AutoPopulateFieldTesting/AutoPopulateFieldTestingCollect") + .setRequestMarshaller( + ProtoUtils.marshaller(AutoPopulateFieldTestingEchoRequest.getDefaultInstance())) + .setResponseMarshaller( + ProtoUtils.marshaller(AutoPopulateFieldTestingEchoResponse.getDefaultInstance())) + .build(); + + private static final MethodDescriptor< + AutoPopulateFieldTestingEchoRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingChatMethodDescriptor = + MethodDescriptor + . + newBuilder() + .setType(MethodDescriptor.MethodType.BIDI_STREAMING) + .setFullMethodName( + "google.auto.populate.field.AutoPopulateFieldTesting/AutoPopulateFieldTestingChat") + .setRequestMarshaller( + ProtoUtils.marshaller(AutoPopulateFieldTestingEchoRequest.getDefaultInstance())) + .setResponseMarshaller( + ProtoUtils.marshaller(AutoPopulateFieldTestingEchoResponse.getDefaultInstance())) + .build(); + + private final UnaryCallable< + AutoPopulateFieldTestingEchoRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingEchoCallable; + private final ServerStreamingCallable< + AutoPopulateFieldTestingExpandRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingExpandCallable; + private final ClientStreamingCallable< + AutoPopulateFieldTestingEchoRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingCollectCallable; + private final BidiStreamingCallable< + AutoPopulateFieldTestingEchoRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingChatCallable; + + private final BackgroundResource backgroundResources; + private final GrpcOperationsStub operationsStub; + private final GrpcStubCallableFactory callableFactory; + + public static final GrpcAutoPopulateFieldTestingStub create( + AutoPopulateFieldTestingStubSettings settings) throws IOException { + return new GrpcAutoPopulateFieldTestingStub(settings, ClientContext.create(settings)); + } + + public static final GrpcAutoPopulateFieldTestingStub create(ClientContext clientContext) + throws IOException { + return new GrpcAutoPopulateFieldTestingStub( + AutoPopulateFieldTestingStubSettings.newBuilder().build(), clientContext); + } + + public static final GrpcAutoPopulateFieldTestingStub create( + ClientContext clientContext, GrpcStubCallableFactory callableFactory) throws IOException { + return new GrpcAutoPopulateFieldTestingStub( + AutoPopulateFieldTestingStubSettings.newBuilder().build(), clientContext, callableFactory); + } + + /** + * Constructs an instance of GrpcAutoPopulateFieldTestingStub, 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 GrpcAutoPopulateFieldTestingStub( + AutoPopulateFieldTestingStubSettings settings, ClientContext clientContext) + throws IOException { + this(settings, clientContext, new GrpcAutoPopulateFieldTestingCallableFactory()); + } + + /** + * Constructs an instance of GrpcAutoPopulateFieldTestingStub, 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 GrpcAutoPopulateFieldTestingStub( + AutoPopulateFieldTestingStubSettings settings, + ClientContext clientContext, + GrpcStubCallableFactory callableFactory) + throws IOException { + this.callableFactory = callableFactory; + this.operationsStub = GrpcOperationsStub.create(clientContext, callableFactory); + + GrpcCallSettings + autoPopulateFieldTestingEchoTransportSettings = + GrpcCallSettings + . + newBuilder() + .setMethodDescriptor(autoPopulateFieldTestingEchoMethodDescriptor) + .setRequestMutator( + request -> { + AutoPopulateFieldTestingEchoRequest.Builder requestBuilder = + request.toBuilder(); + if (Strings.isNullOrEmpty(request.getRequestId())) { + requestBuilder.setRequestId(UUID.randomUUID().toString()); + } + if (Strings.isNullOrEmpty(request.getSecondRequestId())) { + requestBuilder.setSecondRequestId(UUID.randomUUID().toString()); + } + return requestBuilder.build(); + }) + .build(); + GrpcCallSettings + autoPopulateFieldTestingExpandTransportSettings = + GrpcCallSettings + . + newBuilder() + .setMethodDescriptor(autoPopulateFieldTestingExpandMethodDescriptor) + .build(); + GrpcCallSettings + autoPopulateFieldTestingCollectTransportSettings = + GrpcCallSettings + . + newBuilder() + .setMethodDescriptor(autoPopulateFieldTestingCollectMethodDescriptor) + .build(); + GrpcCallSettings + autoPopulateFieldTestingChatTransportSettings = + GrpcCallSettings + . + newBuilder() + .setMethodDescriptor(autoPopulateFieldTestingChatMethodDescriptor) + .build(); + + this.autoPopulateFieldTestingEchoCallable = + callableFactory.createUnaryCallable( + autoPopulateFieldTestingEchoTransportSettings, + settings.autoPopulateFieldTestingEchoSettings(), + clientContext); + this.autoPopulateFieldTestingExpandCallable = + callableFactory.createServerStreamingCallable( + autoPopulateFieldTestingExpandTransportSettings, + settings.autoPopulateFieldTestingExpandSettings(), + clientContext); + this.autoPopulateFieldTestingCollectCallable = + callableFactory.createClientStreamingCallable( + autoPopulateFieldTestingCollectTransportSettings, + settings.autoPopulateFieldTestingCollectSettings(), + clientContext); + this.autoPopulateFieldTestingChatCallable = + callableFactory.createBidiStreamingCallable( + autoPopulateFieldTestingChatTransportSettings, + settings.autoPopulateFieldTestingChatSettings(), + clientContext); + + this.backgroundResources = + new BackgroundResourceAggregation(clientContext.getBackgroundResources()); + } + + public GrpcOperationsStub getOperationsStub() { + return operationsStub; + } + + @Override + public UnaryCallable + autoPopulateFieldTestingEchoCallable() { + return autoPopulateFieldTestingEchoCallable; + } + + @Override + public ServerStreamingCallable< + AutoPopulateFieldTestingExpandRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingExpandCallable() { + return autoPopulateFieldTestingExpandCallable; + } + + @Override + public ClientStreamingCallable< + AutoPopulateFieldTestingEchoRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingCollectCallable() { + return autoPopulateFieldTestingCollectCallable; + } + + @Override + public BidiStreamingCallable< + AutoPopulateFieldTestingEchoRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingChatCallable() { + return autoPopulateFieldTestingChatCallable; + } + + @Override + public final void close() { + try { + backgroundResources.close(); + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } + } + + @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/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden index c74e415fef..940ab7d4c4 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/GrpcEchoStub.golden @@ -14,7 +14,6 @@ import com.google.api.gax.rpc.ClientStreamingCallable; import com.google.api.gax.rpc.OperationCallable; import com.google.api.gax.rpc.ServerStreamingCallable; import com.google.api.gax.rpc.UnaryCallable; -import com.google.common.base.Strings; import com.google.longrunning.Operation; import com.google.longrunning.stub.GrpcOperationsStub; import com.google.showcase.v1beta1.BlockRequest; @@ -31,7 +30,6 @@ import com.google.showcase.v1beta1.WaitResponse; import io.grpc.MethodDescriptor; import io.grpc.protobuf.ProtoUtils; import java.io.IOException; -import java.util.UUID; import java.util.concurrent.TimeUnit; import javax.annotation.Generated; @@ -185,17 +183,6 @@ public class GrpcEchoStub extends EchoStub { GrpcCallSettings echoTransportSettings = GrpcCallSettings.newBuilder() .setMethodDescriptor(echoMethodDescriptor) - .setRequestMutator( - request -> { - EchoRequest.Builder requestBuilder = request.toBuilder(); - if (Strings.isNullOrEmpty(request.getRequestId())) { - requestBuilder.setRequestId(UUID.randomUUID().toString()); - } - if (Strings.isNullOrEmpty(request.getSecondRequestId())) { - requestBuilder.setSecondRequestId(UUID.randomUUID().toString()); - } - return requestBuilder.build(); - }) .build(); GrpcCallSettings expandTransportSettings = GrpcCallSettings.newBuilder() diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncChat.golden b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncChat.golden index 42bbd4a5a7..020c13e86f 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncChat.golden +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncChat.golden @@ -43,12 +43,6 @@ public class AsyncChat { EchoRequest.newBuilder() .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - .setRequestId("requestId693933066") - .setSecondRequestId("secondRequestId344404470") - .setThirdRequestId(true) - .setFourthRequestId("fourthRequestId-2116417776") - .setFifthRequestId("fifthRequestId959024147") - .setSixthRequestId("sixthRequestId1005218260") .setSeverity(Severity.forNumber(0)) .setFoobar(Foobar.newBuilder().build()) .build(); diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncChatAgain.golden b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncChatAgain.golden index 6897c6f67a..249166996c 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncChatAgain.golden +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncChatAgain.golden @@ -43,12 +43,6 @@ public class AsyncChatAgain { EchoRequest.newBuilder() .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - .setRequestId("requestId693933066") - .setSecondRequestId("secondRequestId344404470") - .setThirdRequestId(true) - .setFourthRequestId("fourthRequestId-2116417776") - .setFifthRequestId("fifthRequestId959024147") - .setSixthRequestId("sixthRequestId1005218260") .setSeverity(Severity.forNumber(0)) .setFoobar(Foobar.newBuilder().build()) .build(); diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncCollect.golden b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncCollect.golden index 4d9438ca9e..72be3b0a98 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncCollect.golden +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncCollect.golden @@ -61,12 +61,6 @@ public class AsyncCollect { EchoRequest.newBuilder() .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - .setRequestId("requestId693933066") - .setSecondRequestId("secondRequestId344404470") - .setThirdRequestId(true) - .setFourthRequestId("fourthRequestId-2116417776") - .setFifthRequestId("fifthRequestId959024147") - .setSixthRequestId("sixthRequestId1005218260") .setSeverity(Severity.forNumber(0)) .setFoobar(Foobar.newBuilder().build()) .build(); diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncCollideName.golden b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncCollideName.golden index 00083b62ad..8e276d6adf 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncCollideName.golden +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncCollideName.golden @@ -42,12 +42,6 @@ public class AsyncCollideName { EchoRequest.newBuilder() .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - .setRequestId("requestId693933066") - .setSecondRequestId("secondRequestId344404470") - .setThirdRequestId(true) - .setFourthRequestId("fourthRequestId-2116417776") - .setFifthRequestId("fifthRequestId959024147") - .setSixthRequestId("sixthRequestId1005218260") .setSeverity(Severity.forNumber(0)) .setFoobar(Foobar.newBuilder().build()) .build(); diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncEcho.golden b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncEcho.golden index e75246e24a..e9e14e9459 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncEcho.golden +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncEcho.golden @@ -42,12 +42,6 @@ public class AsyncEcho { EchoRequest.newBuilder() .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - .setRequestId("requestId693933066") - .setSecondRequestId("secondRequestId344404470") - .setThirdRequestId(true) - .setFourthRequestId("fourthRequestId-2116417776") - .setFifthRequestId("fifthRequestId959024147") - .setSixthRequestId("sixthRequestId1005218260") .setSeverity(Severity.forNumber(0)) .setFoobar(Foobar.newBuilder().build()) .build(); diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncExpand.golden b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncExpand.golden index fe82f5cddf..8c095ac983 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncExpand.golden +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/AsyncExpand.golden @@ -36,11 +36,7 @@ public class AsyncExpand { // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library try (EchoClient echoClient = EchoClient.create()) { ExpandRequest request = - ExpandRequest.newBuilder() - .setContent("content951530617") - .setInfo("info3237038") - .setRequestId("requestId693933066") - .build(); + ExpandRequest.newBuilder().setContent("content951530617").setInfo("info3237038").build(); ServerStream stream = echoClient.expandCallable().call(request); for (EchoResponse response : stream) { // Do something when a response is received. diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/SyncCollideName.golden b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/SyncCollideName.golden index 06b87bc186..db8008e189 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/SyncCollideName.golden +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/SyncCollideName.golden @@ -41,12 +41,6 @@ public class SyncCollideName { EchoRequest.newBuilder() .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - .setRequestId("requestId693933066") - .setSecondRequestId("secondRequestId344404470") - .setThirdRequestId(true) - .setFourthRequestId("fourthRequestId-2116417776") - .setFifthRequestId("fifthRequestId959024147") - .setSixthRequestId("sixthRequestId1005218260") .setSeverity(Severity.forNumber(0)) .setFoobar(Foobar.newBuilder().build()) .build(); diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/SyncEcho.golden b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/SyncEcho.golden index 662f69c7c3..534773f3bb 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/SyncEcho.golden +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/goldens/samples/echoclient/SyncEcho.golden @@ -41,12 +41,6 @@ public class SyncEcho { EchoRequest.newBuilder() .setName(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) .setParent(FoobarName.ofProjectFoobarName("[PROJECT]", "[FOOBAR]").toString()) - .setRequestId("requestId693933066") - .setSecondRequestId("secondRequestId344404470") - .setThirdRequestId(true) - .setFourthRequestId("fourthRequestId-2116417776") - .setFifthRequestId("fifthRequestId959024147") - .setSixthRequestId("sixthRequestId1005218260") .setSeverity(Severity.forNumber(0)) .setFoobar(Foobar.newBuilder().build()) .build(); diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/rest/HttpJsonServiceStubClassComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/rest/HttpJsonServiceStubClassComposerTest.java index f96e67217f..d1231ebd4a 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/rest/HttpJsonServiceStubClassComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/rest/HttpJsonServiceStubClassComposerTest.java @@ -207,4 +207,14 @@ public void generateGrpcServiceStubClass_routingHeaders() { Assert.assertGoldenClass(this.getClass(), clazz, "HttpJsonRoutingHeadersStub.golden"); Assert.assertEmptySamples(clazz.samples()); } + + @Test + public void generateHttpJsonServiceStubClass_autopopulateField() { + GapicContext context = RestTestProtoLoader.instance().parseAutoPopulateFieldTesting(); + Service service = context.services().get(0); + GapicClass clazz = HttpJsonServiceStubClassComposer.instance().generate(context, service); + + Assert.assertGoldenClass(this.getClass(), clazz, "HttpJsonAutoPopulateFieldTestingStub.golden"); + Assert.assertEmptySamples(clazz.samples()); + } } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonAutoPopulateFieldTestingStub.golden b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonAutoPopulateFieldTestingStub.golden new file mode 100644 index 0000000000..9b5d7c3b8f --- /dev/null +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonAutoPopulateFieldTestingStub.golden @@ -0,0 +1,288 @@ +package com.google.auto.populate.field.stub; + +import com.google.api.core.BetaApi; +import com.google.api.core.InternalApi; +import com.google.api.gax.core.BackgroundResource; +import com.google.api.gax.core.BackgroundResourceAggregation; +import com.google.api.gax.httpjson.ApiMethodDescriptor; +import com.google.api.gax.httpjson.HttpJsonCallSettings; +import com.google.api.gax.httpjson.HttpJsonStubCallableFactory; +import com.google.api.gax.httpjson.ProtoMessageRequestFormatter; +import com.google.api.gax.httpjson.ProtoMessageResponseParser; +import com.google.api.gax.httpjson.ProtoRestSerializer; +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.ServerStreamingCallable; +import com.google.api.gax.rpc.UnaryCallable; +import com.google.auto.populate.field.AutoPopulateFieldTestingEchoRequest; +import com.google.auto.populate.field.AutoPopulateFieldTestingEchoResponse; +import com.google.auto.populate.field.AutoPopulateFieldTestingExpandRequest; +import com.google.common.base.Strings; +import com.google.protobuf.TypeRegistry; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.TimeUnit; +import javax.annotation.Generated; + +// AUTO-GENERATED DOCUMENTATION AND CLASS. +/** + * REST stub implementation for the AutoPopulateFieldTesting service API. + * + *

This class is for advanced usage and reflects the underlying API directly. + */ +@Generated("by gapic-generator-java") +@BetaApi +public class HttpJsonAutoPopulateFieldTestingStub extends AutoPopulateFieldTestingStub { + private static final TypeRegistry typeRegistry = TypeRegistry.newBuilder().build(); + + private static final ApiMethodDescriptor< + AutoPopulateFieldTestingEchoRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingEchoMethodDescriptor = + ApiMethodDescriptor + . + newBuilder() + .setFullMethodName( + "google.auto.populate.field.AutoPopulateFieldTesting/AutoPopulateFieldTestingEcho") + .setHttpMethod("POST") + .setType(ApiMethodDescriptor.MethodType.UNARY) + .setRequestFormatter( + ProtoMessageRequestFormatter.newBuilder() + .setPath( + "/v1beta1/AutoPopulateFieldTesting:echo", + request -> { + Map fields = new HashMap<>(); + ProtoRestSerializer serializer = + ProtoRestSerializer.create(); + return fields; + }) + .setQueryParamsExtractor( + request -> { + Map> fields = new HashMap<>(); + ProtoRestSerializer serializer = + ProtoRestSerializer.create(); + return fields; + }) + .setRequestBodyExtractor( + request -> + ProtoRestSerializer.create() + .toBody("*", request.toBuilder().build(), false)) + .build()) + .setResponseParser( + ProtoMessageResponseParser.newBuilder() + .setDefaultInstance(AutoPopulateFieldTestingEchoResponse.getDefaultInstance()) + .setDefaultTypeRegistry(typeRegistry) + .build()) + .build(); + + private static final ApiMethodDescriptor< + AutoPopulateFieldTestingExpandRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingExpandMethodDescriptor = + ApiMethodDescriptor + . + newBuilder() + .setFullMethodName( + "google.auto.populate.field.AutoPopulateFieldTesting/AutoPopulateFieldTestingExpand") + .setHttpMethod("POST") + .setType(ApiMethodDescriptor.MethodType.SERVER_STREAMING) + .setRequestFormatter( + ProtoMessageRequestFormatter.newBuilder() + .setPath( + "/v1beta1/AutoPopulateFieldTesting:expand", + request -> { + Map fields = new HashMap<>(); + ProtoRestSerializer serializer = + ProtoRestSerializer.create(); + return fields; + }) + .setQueryParamsExtractor( + request -> { + Map> fields = new HashMap<>(); + ProtoRestSerializer serializer = + ProtoRestSerializer.create(); + return fields; + }) + .setRequestBodyExtractor( + request -> + ProtoRestSerializer.create() + .toBody("*", request.toBuilder().build(), false)) + .build()) + .setResponseParser( + ProtoMessageResponseParser.newBuilder() + .setDefaultInstance(AutoPopulateFieldTestingEchoResponse.getDefaultInstance()) + .setDefaultTypeRegistry(typeRegistry) + .build()) + .build(); + + private final UnaryCallable< + AutoPopulateFieldTestingEchoRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingEchoCallable; + private final ServerStreamingCallable< + AutoPopulateFieldTestingExpandRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingExpandCallable; + + private final BackgroundResource backgroundResources; + private final HttpJsonStubCallableFactory callableFactory; + + public static final HttpJsonAutoPopulateFieldTestingStub create( + AutoPopulateFieldTestingStubSettings settings) throws IOException { + return new HttpJsonAutoPopulateFieldTestingStub(settings, ClientContext.create(settings)); + } + + public static final HttpJsonAutoPopulateFieldTestingStub create(ClientContext clientContext) + throws IOException { + return new HttpJsonAutoPopulateFieldTestingStub( + AutoPopulateFieldTestingStubSettings.newBuilder().build(), clientContext); + } + + public static final HttpJsonAutoPopulateFieldTestingStub create( + ClientContext clientContext, HttpJsonStubCallableFactory callableFactory) throws IOException { + return new HttpJsonAutoPopulateFieldTestingStub( + AutoPopulateFieldTestingStubSettings.newBuilder().build(), clientContext, callableFactory); + } + + /** + * Constructs an instance of HttpJsonAutoPopulateFieldTestingStub, 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 HttpJsonAutoPopulateFieldTestingStub( + AutoPopulateFieldTestingStubSettings settings, ClientContext clientContext) + throws IOException { + this(settings, clientContext, new HttpJsonAutoPopulateFieldTestingCallableFactory()); + } + + /** + * Constructs an instance of HttpJsonAutoPopulateFieldTestingStub, 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 HttpJsonAutoPopulateFieldTestingStub( + AutoPopulateFieldTestingStubSettings settings, + ClientContext clientContext, + HttpJsonStubCallableFactory callableFactory) + throws IOException { + this.callableFactory = callableFactory; + + HttpJsonCallSettings + autoPopulateFieldTestingEchoTransportSettings = + HttpJsonCallSettings + . + newBuilder() + .setMethodDescriptor(autoPopulateFieldTestingEchoMethodDescriptor) + .setTypeRegistry(typeRegistry) + .setRequestMutator( + request -> { + AutoPopulateFieldTestingEchoRequest.Builder requestBuilder = + request.toBuilder(); + if (Strings.isNullOrEmpty(request.getRequestId())) { + requestBuilder.setRequestId(UUID.randomUUID().toString()); + } + if (Strings.isNullOrEmpty(request.getSecondRequestId())) { + requestBuilder.setSecondRequestId(UUID.randomUUID().toString()); + } + return requestBuilder.build(); + }) + .build(); + HttpJsonCallSettings< + AutoPopulateFieldTestingExpandRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingExpandTransportSettings = + HttpJsonCallSettings + . + newBuilder() + .setMethodDescriptor(autoPopulateFieldTestingExpandMethodDescriptor) + .setTypeRegistry(typeRegistry) + .build(); + + this.autoPopulateFieldTestingEchoCallable = + callableFactory.createUnaryCallable( + autoPopulateFieldTestingEchoTransportSettings, + settings.autoPopulateFieldTestingEchoSettings(), + clientContext); + this.autoPopulateFieldTestingExpandCallable = + callableFactory.createServerStreamingCallable( + autoPopulateFieldTestingExpandTransportSettings, + settings.autoPopulateFieldTestingExpandSettings(), + clientContext); + + this.backgroundResources = + new BackgroundResourceAggregation(clientContext.getBackgroundResources()); + } + + @InternalApi + public static List getMethodDescriptors() { + List methodDescriptors = new ArrayList<>(); + methodDescriptors.add(autoPopulateFieldTestingEchoMethodDescriptor); + methodDescriptors.add(autoPopulateFieldTestingExpandMethodDescriptor); + return methodDescriptors; + } + + @Override + public UnaryCallable + autoPopulateFieldTestingEchoCallable() { + return autoPopulateFieldTestingEchoCallable; + } + + @Override + public ServerStreamingCallable< + AutoPopulateFieldTestingExpandRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingExpandCallable() { + return autoPopulateFieldTestingExpandCallable; + } + + @Override + public ClientStreamingCallable< + AutoPopulateFieldTestingEchoRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingCollectCallable() { + throw new UnsupportedOperationException( + "Not implemented: autoPopulateFieldTestingCollectCallable(). REST transport is not implemented for this method yet."); + } + + @Override + public BidiStreamingCallable< + AutoPopulateFieldTestingEchoRequest, AutoPopulateFieldTestingEchoResponse> + autoPopulateFieldTestingChatCallable() { + throw new UnsupportedOperationException( + "Not implemented: autoPopulateFieldTestingChatCallable(). REST transport is not implemented for this method yet."); + } + + @Override + public final void close() { + try { + backgroundResources.close(); + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } + } + + @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/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonEchoStub.golden b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonEchoStub.golden index d58ce093b9..18904bdfbe 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonEchoStub.golden +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/rest/goldens/HttpJsonEchoStub.golden @@ -23,7 +23,6 @@ import com.google.api.gax.rpc.ClientStreamingCallable; import com.google.api.gax.rpc.OperationCallable; import com.google.api.gax.rpc.ServerStreamingCallable; import com.google.api.gax.rpc.UnaryCallable; -import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; import com.google.longrunning.Operation; import com.google.protobuf.TypeRegistry; @@ -43,7 +42,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.UUID; import java.util.concurrent.TimeUnit; import javax.annotation.Generated; @@ -410,17 +408,6 @@ public class HttpJsonEchoStub extends EchoStub { HttpJsonCallSettings.newBuilder() .setMethodDescriptor(echoMethodDescriptor) .setTypeRegistry(typeRegistry) - .setRequestMutator( - request -> { - EchoRequest.Builder requestBuilder = request.toBuilder(); - if (Strings.isNullOrEmpty(request.getRequestId())) { - requestBuilder.setRequestId(UUID.randomUUID().toString()); - } - if (Strings.isNullOrEmpty(request.getSecondRequestId())) { - requestBuilder.setSecondRequestId(UUID.randomUUID().toString()); - } - return requestBuilder.build(); - }) .build(); HttpJsonCallSettings expandTransportSettings = HttpJsonCallSettings.newBuilder() diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCallableMethodSampleComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCallableMethodSampleComposerTest.java index 47be643ffa..f063c50903 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCallableMethodSampleComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientCallableMethodSampleComposerTest.java @@ -462,11 +462,7 @@ public void valid_composeStreamCallableMethod_serverStream() { LineFormatter.lines( "try (EchoClient echoClient = EchoClient.create()) {\n", " ExpandRequest request =\n", - " ExpandRequest.newBuilder()\n", - " .setContent(\"content951530617\")\n", - " .setInfo(\"info3237038\")\n", - " .setRequestId(\"requestId693933066\")\n", - " .build();\n", + " ExpandRequest.newBuilder().setContent(\"content951530617\").setInfo(\"info3237038\").build();\n", " ServerStream stream = echoClient.expandCallable().call(request);\n", " for (EchoResponse response : stream) {\n", " // Do something when a response is received.\n", @@ -579,12 +575,6 @@ public void valid_composeStreamCallableMethod_bidiStream() { + " \"[FOOBAR]\").toString())\n", " .setParent(FoobarName.ofProjectFoobarName(\"[PROJECT]\"," + " \"[FOOBAR]\").toString())\n", - " .setRequestId(\"requestId693933066\")\n", - " .setSecondRequestId(\"secondRequestId344404470\")\n", - " .setThirdRequestId(true)\n", - " .setFourthRequestId(\"fourthRequestId-2116417776\")\n", - " .setFifthRequestId(\"fifthRequestId959024147\")\n", - " .setSixthRequestId(\"sixthRequestId1005218260\")\n", " .setSeverity(Severity.forNumber(0))\n", " .setFoobar(Foobar.newBuilder().build())\n", " .build();\n", @@ -717,12 +707,6 @@ public void valid_composeStreamCallableMethod_clientStream() { + " \"[FOOBAR]\").toString())\n", " .setParent(FoobarName.ofProjectFoobarName(\"[PROJECT]\"," + " \"[FOOBAR]\").toString())\n", - " .setRequestId(\"requestId693933066\")\n", - " .setSecondRequestId(\"secondRequestId344404470\")\n", - " .setThirdRequestId(true)\n", - " .setFourthRequestId(\"fourthRequestId-2116417776\")\n", - " .setFifthRequestId(\"fifthRequestId959024147\")\n", - " .setSixthRequestId(\"sixthRequestId1005218260\")\n", " .setSeverity(Severity.forNumber(0))\n", " .setFoobar(Foobar.newBuilder().build())\n", " .build();\n", @@ -829,12 +813,6 @@ public void valid_composeRegularCallableMethod_unaryRpc() { + " \"[FOOBAR]\").toString())\n", " .setParent(FoobarName.ofProjectFoobarName(\"[PROJECT]\"," + " \"[FOOBAR]\").toString())\n", - " .setRequestId(\"requestId693933066\")\n", - " .setSecondRequestId(\"secondRequestId344404470\")\n", - " .setThirdRequestId(true)\n", - " .setFourthRequestId(\"fourthRequestId-2116417776\")\n", - " .setFifthRequestId(\"fifthRequestId959024147\")\n", - " .setSixthRequestId(\"sixthRequestId1005218260\")\n", " .setSeverity(Severity.forNumber(0))\n", " .setFoobar(Foobar.newBuilder().build())\n", " .build();\n", diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientHeaderSampleComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientHeaderSampleComposerTest.java index affffb9c09..bf33f30e26 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientHeaderSampleComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientHeaderSampleComposerTest.java @@ -223,12 +223,6 @@ public void composeClassHeaderSample_firstMethodHasNoSignatures() { + " \"[FOOBAR]\").toString())\n", " .setParent(FoobarName.ofProjectFoobarName(\"[PROJECT]\"," + " \"[FOOBAR]\").toString())\n", - " .setRequestId(\"requestId693933066\")\n", - " .setSecondRequestId(\"secondRequestId344404470\")\n", - " .setThirdRequestId(true)\n", - " .setFourthRequestId(\"fourthRequestId-2116417776\")\n", - " .setFifthRequestId(\"fifthRequestId959024147\")\n", - " .setSixthRequestId(\"sixthRequestId1005218260\")\n", " .setSeverity(Severity.forNumber(0))\n", " .setFoobar(Foobar.newBuilder().build())\n", " .build();\n", @@ -286,11 +280,7 @@ public void composeClassHeaderSample_firstMethodIsStream() { LineFormatter.lines( "try (EchoClient echoClient = EchoClient.create()) {\n", " ExpandRequest request =\n", - " ExpandRequest.newBuilder()\n", - " .setContent(\"content951530617\")\n", - " .setInfo(\"info3237038\")\n", - " .setRequestId(\"requestId693933066\")\n", - " .build();\n", + " ExpandRequest.newBuilder().setContent(\"content951530617\").setInfo(\"info3237038\").build();\n", " ServerStream stream = echoClient.expandCallable().call(request);\n", " for (EchoResponse response : stream) {\n", " // Do something when a response is received.\n", diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientMethodSampleComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientMethodSampleComposerTest.java index 6d473be885..6ba7985b5d 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientMethodSampleComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/samplecode/ServiceClientMethodSampleComposerTest.java @@ -335,12 +335,6 @@ public void valid_composeDefaultSample_pureUnaryReturnVoid() { + " \"[FOOBAR]\").toString())\n", " .setParent(FoobarName.ofProjectFoobarName(\"[PROJECT]\"," + " \"[FOOBAR]\").toString())\n", - " .setRequestId(\"requestId693933066\")\n", - " .setSecondRequestId(\"secondRequestId344404470\")\n", - " .setThirdRequestId(true)\n", - " .setFourthRequestId(\"fourthRequestId-2116417776\")\n", - " .setFifthRequestId(\"fifthRequestId959024147\")\n", - " .setSixthRequestId(\"sixthRequestId1005218260\")\n", " .setSeverity(Severity.forNumber(0))\n", " .setFoobar(Foobar.newBuilder().build())\n", " .build();\n", @@ -403,12 +397,6 @@ public void valid_composeDefaultSample_pureUnaryReturnResponse() { + " \"[FOOBAR]\").toString())\n", " .setParent(FoobarName.ofProjectFoobarName(\"[PROJECT]\"," + " \"[FOOBAR]\").toString())\n", - " .setRequestId(\"requestId693933066\")\n", - " .setSecondRequestId(\"secondRequestId344404470\")\n", - " .setThirdRequestId(true)\n", - " .setFourthRequestId(\"fourthRequestId-2116417776\")\n", - " .setFifthRequestId(\"fifthRequestId959024147\")\n", - " .setSixthRequestId(\"sixthRequestId1005218260\")\n", " .setSeverity(Severity.forNumber(0))\n", " .setFoobar(Foobar.newBuilder().build())\n", " .build();\n", diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/protoparser/ParserTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/protoparser/ParserTest.java index 8fdf2576c9..b4379fdf66 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/protoparser/ParserTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/protoparser/ParserTest.java @@ -35,8 +35,10 @@ import com.google.api.generator.gapic.model.ResourceName; import com.google.api.generator.gapic.model.ResourceReference; import com.google.api.generator.gapic.model.Transport; +import com.google.auto.populate.field.AutoPopulateFieldTestingOuterClass; import com.google.bookshop.v1beta1.BookshopProto; import com.google.common.collect.ImmutableList; +import com.google.common.truth.Truth; import com.google.protobuf.Descriptors.FileDescriptor; import com.google.protobuf.Descriptors.MethodDescriptor; import com.google.protobuf.Descriptors.ServiceDescriptor; @@ -144,15 +146,7 @@ public void parseMethods_basic() { Method echoMethod = methods.get(0); assertEquals(echoMethod.name(), "Echo"); assertEquals(echoMethod.stream(), Method.Stream.NONE); - assertEquals(true, echoMethod.hasAutoPopulatedFields()); - assertEquals( - Arrays.asList( - "request_id", - "second_request_id", - "third_request_id", - "fourth_request_id", - "non_existent_field"), - echoMethod.autoPopulatedFields()); + assertEquals(false, echoMethod.hasAutoPopulatedFields()); // Detailed method signature parsing tests are in a separate unit test. List> methodSignatures = echoMethod.methodSignatures(); @@ -447,17 +441,13 @@ public void parseFields_mapType() { @Test public void parseFields_autoPopulated() { - Map messageTypes = Parser.parseMessages(echoFileDescriptor); - Message message = messageTypes.get("com.google.showcase.v1beta1.EchoRequest"); + Map messageTypes = + Parser.parseMessages(AutoPopulateFieldTestingOuterClass.getDescriptor()); + Message message = + messageTypes.get("com.google.auto.populate.field.AutoPopulateFieldTestingEchoRequest"); Field field = message.fieldMap().get("request_id"); assertEquals(false, field.isRequired()); assertEquals(Format.UUID4, field.fieldInfoFormat()); - field = message.fieldMap().get("name"); - assertEquals(true, field.isRequired()); - assertEquals(null, field.fieldInfoFormat()); - field = message.fieldMap().get("severity"); - assertEquals(false, field.isRequired()); - assertEquals(null, field.fieldInfoFormat()); field = message.fieldMap().get("second_request_id"); assertEquals(false, field.isRequired()); assertEquals(Format.UUID4, field.fieldInfoFormat()); @@ -474,18 +464,24 @@ public void parseFields_autoPopulated() { assertEquals(true, field.isRequired()); assertEquals(Format.UUID4, field.fieldInfoFormat()); - message = messageTypes.get("com.google.showcase.v1beta1.ExpandRequest"); + message = + messageTypes.get("com.google.auto.populate.field.AutoPopulateFieldTestingExpandRequest"); field = message.fieldMap().get("request_id"); assertEquals(false, field.isRequired()); - assertEquals(Format.IPV6, field.fieldInfoFormat()); + assertEquals(Format.UUID4, field.fieldInfoFormat()); } @Test public void parseAutoPopulatedMethodsAndFields_exists() { + String yamlFilename = "auto_populate_field_testing.yaml"; + Path yamlPath = Paths.get(YAML_DIRECTORY, yamlFilename); Map> autoPopulatedMethodsWithFields = - Parser.parseAutoPopulatedMethodsAndFields(serviceYamlProtoOpt); + Parser.parseAutoPopulatedMethodsAndFields(ServiceYamlParser.parse(yamlPath.toString())); + assertEquals( - true, autoPopulatedMethodsWithFields.containsKey("google.showcase.v1beta1.Echo.Echo")); + true, + autoPopulatedMethodsWithFields.containsKey( + "google.auto.populate.field.AutoPopulateFieldTesting.AutoPopulateFieldTestingEcho")); assertEquals( Arrays.asList( "request_id", @@ -493,7 +489,8 @@ public void parseAutoPopulatedMethodsAndFields_exists() { "third_request_id", "fourth_request_id", "non_existent_field"), - autoPopulatedMethodsWithFields.get("google.showcase.v1beta1.Echo.Echo")); + autoPopulatedMethodsWithFields.get( + "google.auto.populate.field.AutoPopulateFieldTesting.AutoPopulateFieldTestingEcho")); } @Test @@ -535,17 +532,15 @@ public void parseAutoPopulatedMethodsAndFields_returnsMapOfMethodsAndAutoPopulat .build(); Optional testService = Optional.of(Service.newBuilder().setPublishing(testPublishing).build()); - assertEquals( - Arrays.asList("test_field", "test_field_2"), - Parser.parseAutoPopulatedMethodsAndFields(testService).get("test_method")); - assertEquals( - Arrays.asList("test_field_3"), - Parser.parseAutoPopulatedMethodsAndFields(testService).get("test_method_2")); - assertEquals( - Arrays.asList(), - Parser.parseAutoPopulatedMethodsAndFields(testService).get("test_method_3")); - assertEquals( - false, Parser.parseAutoPopulatedMethodsAndFields(testService).containsKey("test_method_4")); + Truth.assertThat(Parser.parseAutoPopulatedMethodsAndFields(testService).get("test_method")) + .containsExactly("test_field", "test_field_2"); + Truth.assertThat(Parser.parseAutoPopulatedMethodsAndFields(testService).get("test_method_2")) + .containsExactly("test_field_3"); + Truth.assertThat(Parser.parseAutoPopulatedMethodsAndFields(testService).get("test_method_3")) + .isEmpty(); + Truth.assertThat( + Parser.parseAutoPopulatedMethodsAndFields(testService).containsKey("test_method_4")) + .isEqualTo(false); } @Test diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/protoparser/ServiceYamlParserTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/protoparser/ServiceYamlParserTest.java index f50fc572ff..213b0abab8 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/protoparser/ServiceYamlParserTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/protoparser/ServiceYamlParserTest.java @@ -19,6 +19,7 @@ import com.google.api.MethodSettings; import com.google.api.Publishing; +import com.google.common.truth.Truth; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; @@ -45,19 +46,32 @@ public void parseServiceYaml_basic() { // FieldNames, etc.) @Test public void parseServiceYaml_autoPopulatedFields() { - String yamlFilename = "echo_v1beta1.yaml"; + String yamlFilename = "auto_populate_field_testing.yaml"; Path yamlPath = Paths.get(YAML_DIRECTORY, yamlFilename); Optional serviceYamlProtoOpt = ServiceYamlParser.parse(yamlPath.toString()); assertTrue(serviceYamlProtoOpt.isPresent()); com.google.api.Service serviceYamlProto = serviceYamlProtoOpt.get(); - assertEquals("showcase.googleapis.com", serviceYamlProto.getName()); + assertEquals("autopopulatefieldtesting.googleapis.com", serviceYamlProto.getName()); Publishing publishingSettings = serviceYamlProto.getPublishing(); List methodSettings = publishingSettings.getMethodSettingsList(); - MethodSettings methodSetting = methodSettings.get(0); - assertEquals("google.showcase.v1beta1.Echo.Echo", methodSetting.getSelector()); - assertEquals("request_id", methodSetting.getAutoPopulatedFieldsList().get(0)); + Truth.assertThat(methodSettings.size() == 2); + Truth.assertThat(methodSettings.get(0).getSelector()) + .isEqualTo( + "google.auto.populate.field.AutoPopulateFieldTesting.AutoPopulateFieldTestingEcho"); + Truth.assertThat(methodSettings.get(0).getAutoPopulatedFieldsList()) + .containsExactly( + "request_id", + "second_request_id", + "third_request_id", + "fourth_request_id", + "non_existent_field"); + Truth.assertThat(methodSettings.get(1).getSelector()) + .isEqualTo( + "google.auto.populate.field.AutoPopulateFieldTesting.AutoPopulateFieldTestingExpand"); + Truth.assertThat(methodSettings.get(1).getAutoPopulatedFieldsList()) + .containsExactly("request_id"); } } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/test/protoloader/TestProtoLoader.java b/gapic-generator-java/src/test/java/com/google/api/generator/test/protoloader/TestProtoLoader.java index 8a812ea437..d5e62ddf70 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/test/protoloader/TestProtoLoader.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/test/protoloader/TestProtoLoader.java @@ -28,6 +28,7 @@ import com.google.api.generator.gapic.protoparser.Parser; import com.google.api.generator.gapic.protoparser.ServiceConfigParser; import com.google.api.generator.gapic.protoparser.ServiceYamlParser; +import com.google.auto.populate.field.AutoPopulateFieldTestingOuterClass; import com.google.bookshop.v1beta1.BookshopProto; import com.google.explicit.dynamic.routing.header.ExplicitDynamicRoutingHeaderTestingOuterClass; import com.google.logging.v2.LogEntryProto; @@ -292,6 +293,40 @@ public GapicContext parseExplicitDynamicRoutingHeaderTesting() { .build(); } + public GapicContext parseAutoPopulateFieldTesting() { + FileDescriptor autopopulationFileDescriptor = + AutoPopulateFieldTestingOuterClass.getDescriptor(); + ServiceDescriptor autopopulationServiceDescriptor = + autopopulationFileDescriptor.getServices().get(0); + assertEquals(autopopulationServiceDescriptor.getName(), "AutoPopulateFieldTesting"); + + String serviceYamlFilename = "auto_populate_field_testing.yaml"; + Path serviceYamlPath = Paths.get(testFilesDirectory, serviceYamlFilename); + Optional serviceYamlOpt = + ServiceYamlParser.parse(serviceYamlPath.toString()); + assertTrue(serviceYamlOpt.isPresent()); + + Map messageTypes = Parser.parseMessages(autopopulationFileDescriptor); + Map resourceNames = + Parser.parseResourceNames(autopopulationFileDescriptor); + Set outputResourceNames = new HashSet<>(); + List services = + Parser.parseService( + autopopulationFileDescriptor, + messageTypes, + resourceNames, + serviceYamlOpt, + outputResourceNames); + + return GapicContext.builder() + .setMessages(messageTypes) + .setResourceNames(resourceNames) + .setServices(services) + .setHelperResourceNames(outputResourceNames) + .setTransport(transport) + .build(); + } + public GapicContext parsePubSubPublisher() { FileDescriptor serviceFileDescriptor = PubsubProto.getDescriptor(); FileDescriptor commonResourcesFileDescriptor = CommonResources.getDescriptor(); diff --git a/gapic-generator-java/src/test/proto/auto_populate_field_testing.proto b/gapic-generator-java/src/test/proto/auto_populate_field_testing.proto new file mode 100644 index 0000000000..aa5b291180 --- /dev/null +++ b/gapic-generator-java/src/test/proto/auto_populate_field_testing.proto @@ -0,0 +1,144 @@ +// Copyright 2024 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. + +syntax = "proto3"; + +import "google/api/annotations.proto"; +import "google/api/client.proto"; +import "google/api/field_behavior.proto"; +import "google/api/field_info.proto"; +import "google/api/resource.proto"; +import "google/longrunning/operations.proto"; +import "google/protobuf/duration.proto"; +import "google/protobuf/timestamp.proto"; +import "google/rpc/status.proto"; +import "google/protobuf/empty.proto"; + +package google.auto.populate.field; + +option java_package = "com.google.auto.populate.field"; +option java_multiple_files = true; + +// This service is meant for testing all scenarios related to +// the field AutoPopulateFieldTesting feature, including but not limited to the example in gapic-showcase's echo.proto +service AutoPopulateFieldTesting { + option (google.api.default_host) = "localhost:7469"; + + // This unary method simply echoes the request. request_id and second_request_id *should* be autopopulated on the request if not set. + rpc AutoPopulateFieldTestingEcho(AutoPopulateFieldTestingEchoRequest) returns (AutoPopulateFieldTestingEchoResponse) { + option (google.api.http) = { + post: "/v1beta1/AutoPopulateFieldTesting:echo" + body: "*" + }; + } + + // This server-side streaming method splits the given content into words and will pass each word back + // through the stream. This method should not have any autopopulated fields. + rpc AutoPopulateFieldTestingExpand(AutoPopulateFieldTestingExpandRequest) returns (stream AutoPopulateFieldTestingEchoResponse) { + option (google.api.http) = { + post: "/v1beta1/AutoPopulateFieldTesting:expand" + body: "*" + }; + } + + // This client-side streaming method will collect the words given to it. When the stream is closed + // by the client, this method will return the a concatenation of the strings + // passed to it. This method should not have any autopopulated fields. + rpc AutoPopulateFieldTestingCollect(stream AutoPopulateFieldTestingEchoRequest) returns (AutoPopulateFieldTestingEchoResponse) { + option (google.api.http) = { + post: "/v1beta1/AutoPopulateFieldTesting:collect" + body: "*" + }; + } + + // This bidirectional streaming method, upon receiving a request on the stream, will pass the same + // content back on the stream. This method should not have any autopopulated fields. + rpc AutoPopulateFieldTestingChat(stream AutoPopulateFieldTestingEchoRequest) returns (stream AutoPopulateFieldTestingEchoResponse); +} + +// The request message used for the AutoPopulateFieldTestingEcho, AutoPopulateFieldTestingCollect and AutoPopulateFieldTestingChat methods. +// If content or opt are set in this message then the request will succeed. +// If status is set in this message then the status will be returned as an +// error. +message AutoPopulateFieldTestingEchoRequest { + oneof response { + // The content to be echoed by the server. + string content = 1; + + // The error to be thrown by the server. + google.rpc.Status error = 2; + } + + // Based on go/client-populate-request-id-design; subject to change + string request_id = 3 [ + (google.api.field_info).format = UUID4 + ]; + + // This field is added to test that AutoPopulateFieldTesting works for multiple fields + string second_request_id = 8 [ + (google.api.field_behavior) = OPTIONAL, + (google.api.field_info).format = UUID4 + ]; + + // This field is added to test that AutoPopulateFieldTesting should not populate this field since it is not of type String + bool third_request_id = 9 [ + (google.api.field_info).format = UUID4 + ]; + + // This field is added to test that AutoPopulateFieldTesting should not populate this field since it is not annotated with UUID4 format + string fourth_request_id = 10 [ + (google.api.field_info).format = IPV4_OR_IPV6 + ]; + + // This field is added to test that AutoPopulateFieldTesting should not populate this field since it is not designated in the service_yaml + string fifth_request_id = 11 [ + (google.api.field_info).format = UUID4 + ]; + + // This field is added to test that AutoPopulateFieldTesting should not populate this field since it marked as Required + string sixth_request_id = 12 [ + (google.api.field_info).format = UUID4, + (google.api.field_behavior) = REQUIRED + ]; +} + +// The response message for the AutoPopulateFieldTesting service methods. +message AutoPopulateFieldTestingEchoResponse { + // The content specified in the request. + string content = 1; + + // The request ID specified or autopopulated in the request. + string request_id = 2; + + // The second request ID specified or autopopulated in the request. + string second_request_id = 3; +} + +// The request message for the AutoPopulateFieldTestingExpand method. +message AutoPopulateFieldTestingExpandRequest { + // The content that will be split into words and returned on the stream. + string content = 1; + + // The error that is thrown after all words are sent on the stream. + google.rpc.Status error = 2; + + //The wait time between each server streaming messages + google.protobuf.Duration stream_wait_time = 3; + + // This field is added to test that AutoPopulateFieldTesting should not populate this field since it is not used in a unary method, even though it is otherwise configured correctly. + string request_id = 4 [ + (google.api.field_behavior) = OPTIONAL, + (google.api.field_info).format = UUID4 + ]; +} diff --git a/gapic-generator-java/src/test/proto/echo.proto b/gapic-generator-java/src/test/proto/echo.proto index effa0325cd..361e661459 100644 --- a/gapic-generator-java/src/test/proto/echo.proto +++ b/gapic-generator-java/src/test/proto/echo.proto @@ -180,39 +180,6 @@ message EchoRequest { (google.api.field_behavior) = REQUIRED ]; - // This field is added based on go/client-populate-request-id-design; subject to change - string request_id = 7 [ - (google.api.field_info).format = UUID4 - ]; - - // This field is added to test that autopopulation works for multiple fields - string second_request_id = 8 [ - (google.api.field_behavior) = OPTIONAL, - (google.api.field_info).format = UUID4 - ]; - - // This field is added to test that autopopulation should not populate this field since it is not of type String - bool third_request_id = 9 [ - (google.api.field_info).format = UUID4 - ]; - - // This field is added to test that autopopulation should not populate this field since it is not annotated with UUID4 format - string fourth_request_id = 10 [ - (google.api.field_info).format = IPV4_OR_IPV6 - ]; - - // This field is added to test that autopopulation should not populate this field since it is not designated in the service_yaml - string fifth_request_id = 11 [ - (google.api.field_info).format = UUID4 - ]; - - // This field is added to test that autopopulation should not populate this field since it marked as Required - string sixth_request_id = 12 [ - (google.api.field_info).format = UUID4, - (google.api.field_behavior) = REQUIRED - ]; - - oneof response { // The content to be echoed by the server. string content = 1; @@ -252,10 +219,6 @@ message ExpandRequest { string info = 3; - // This field is added based on go/client-populate-request-id-design; subject to change - string request_id = 4 [ - (google.api.field_info).format = IPV6 - ]; } // The request for the PagedExpand method. diff --git a/gapic-generator-java/src/test/resources/auto_populate_field_testing.yaml b/gapic-generator-java/src/test/resources/auto_populate_field_testing.yaml new file mode 100644 index 0000000000..dfb9c24dbb --- /dev/null +++ b/gapic-generator-java/src/test/resources/auto_populate_field_testing.yaml @@ -0,0 +1,24 @@ +type: google.api.Service +config_version: 3 +name: autopopulatefieldtesting.googleapis.com +title: Autopopulate Field Testing API + +apis: +- name: google.auto.populate.field.AutoPopulateFieldTesting + +documentation: + summary: |- + YAML file for auto_populate_field_testing.proto to test request ID autopopulation feature. + +publishing: + method_settings: + - selector: google.auto.populate.field.AutoPopulateFieldTesting.AutoPopulateFieldTestingEcho + auto_populated_fields: + - request_id + - second_request_id + - third_request_id + - fourth_request_id + - non_existent_field + - selector: google.auto.populate.field.AutoPopulateFieldTesting.AutoPopulateFieldTestingExpand + auto_populated_fields: + - request_id \ No newline at end of file diff --git a/gapic-generator-java/src/test/resources/echo_v1beta1.yaml b/gapic-generator-java/src/test/resources/echo_v1beta1.yaml index a6aea48e87..321758a4ea 100644 --- a/gapic-generator-java/src/test/resources/echo_v1beta1.yaml +++ b/gapic-generator-java/src/test/resources/echo_v1beta1.yaml @@ -94,13 +94,4 @@ http: post: '/v1beta1/{name=operations/**}:cancel' additional_bindings: - post: '/v1beta2/{name=operations/**}:cancel' - - post: '/v1beta3/{name=operations/**}:cancel' -publishing: - method_settings: - - selector: google.showcase.v1beta1.Echo.Echo - auto_populated_fields: - - request_id - - second_request_id - - third_request_id - - fourth_request_id - - non_existent_field \ No newline at end of file + - post: '/v1beta3/{name=operations/**}:cancel' \ No newline at end of file