From 8772c514cdd727762f1bca9b0d455f58d927d061 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Mon, 8 Apr 2024 16:39:02 -0400 Subject: [PATCH 01/11] chore: rename current apiVersion to apiMajorVersion. --- .../AbstractServiceClientClassComposer.java | 2 +- .../AbstractServiceSettingsClassComposer.java | 2 +- ...AbstractServiceStubSettingsClassComposer.java | 2 +- .../api/generator/gapic/model/Service.java | 16 ++++++++-------- .../generator/gapic/composer/ComposerTest.java | 4 ++-- .../api/generator/gapic/model/ServiceTest.java | 4 ++-- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java index 5f8456aa97..bad8cfdd09 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java @@ -173,7 +173,7 @@ public GapicClass generate(GapicContext context, Service service) { updateGapicMetadata(context, service, className, grpcRpcsToJavaMethodNames); return GapicClass.create(kind, classDef, SampleComposerUtil.handleDuplicateSamples(samples)) .withApiShortName(service.apiShortName()) - .withApiVersion(service.apiVersion()); + .withApiVersion(service.apiMajorVersion()); } private static List createClassAnnotations(Service service, TypeStore typeStore) { diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java index c83bd54deb..b0e5e40a25 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java @@ -128,7 +128,7 @@ public GapicClass generate(GapicContext context, Service service) { .build(); return GapicClass.create(kind, classDef, SampleComposerUtil.handleDuplicateSamples(samples)) .withApiShortName(service.apiShortName()) - .withApiVersion(service.apiVersion()); + .withApiVersion(service.apiMajorVersion()); } private static List createClassHeaderComments( diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java index f8c00131be..5865c562f7 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java @@ -204,7 +204,7 @@ public GapicClass generate(GapicContext context, Service service) { return GapicClass.create( GapicClass.Kind.STUB, classDef, SampleComposerUtil.handleDuplicateSamples(samples)) .withApiShortName(service.apiShortName()) - .withApiVersion(service.apiVersion()); + .withApiVersion(service.apiMajorVersion()); } protected MethodDefinition createDefaultCredentialsProviderBuilderMethod() { diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/Service.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/Service.java index b89d42b4d2..cf0e21c597 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/Service.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/Service.java @@ -65,9 +65,9 @@ public String apiShortName() { return ""; } - public String apiVersion() { + public String apiMajorVersion() { if (!Strings.isNullOrEmpty(protoPakkage())) { - return parseApiVersion(protoPakkage()); + return parseApiMajorVersion(protoPakkage()); } return ""; } @@ -177,17 +177,17 @@ public abstract static class Builder { public abstract Service build(); } - private static String parseApiVersion(String protoPackage) { - // parse protoPackage for apiVersion + private static String parseApiMajorVersion(String protoPackage) { + // parse protoPackage for apiMajorVersion String[] pakkage = protoPackage.split("\\."); - String apiVersion; + String apiMajorVersion; // e.g. v1, v2, v1beta1 if (pakkage[pakkage.length - 1].matches("v[0-9].*")) { - apiVersion = pakkage[pakkage.length - 1]; + apiMajorVersion = pakkage[pakkage.length - 1]; } else { - apiVersion = ""; + apiMajorVersion = ""; } - return apiVersion; + return apiMajorVersion; } // Parse the service name from the default host configured in the protos diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java index bc99864bdd..5598e29c9d 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java @@ -45,7 +45,7 @@ public class ComposerTest { GrpcServiceCallableFactoryClassComposer.instance() .generate(context, echoProtoService) .withApiShortName(echoProtoService.apiShortName()) - .withApiVersion(echoProtoService.apiVersion())); + .withApiVersion(echoProtoService.apiMajorVersion())); private final Sample sample = Sample.builder() .setRegionTag( @@ -160,7 +160,7 @@ private List getTestClassListFromService(Service testService) { .generate(context, testService) .withSamples(ListofSamples) .withApiShortName(testService.apiShortName()) - .withApiVersion(testService.apiVersion()); + .withApiVersion(testService.apiMajorVersion()); List testClassList = Arrays.asList(new GapicClass[] {testClass}); return testClassList; } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java index 82a3a7f2b9..dbe3d0fa15 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java @@ -82,14 +82,14 @@ public void apiShortName_shouldReturnHostIfNoPeriods() { public void apiVersion_shouldReturnVersionIfMatch() { String protoPackage = "com.google.showcase.v1"; Service testService = testServiceBuilder.setProtoPakkage(protoPackage).build(); - assertEquals("v1", testService.apiVersion()); + assertEquals("v1", testService.apiMajorVersion()); } @Test public void apiVersion_shouldReturnEmptyIfNoMatch() { String protoPackage = "com.google.showcase"; Service testService = testServiceBuilder.setProtoPakkage(protoPackage).build(); - assertEquals("", testService.apiVersion()); + assertEquals("", testService.apiMajorVersion()); } @Test From 9ec3531ea267c641ed265d685d4d4fecbe2285f3 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Mon, 8 Apr 2024 17:35:49 -0400 Subject: [PATCH 02/11] continue: rename apiVersion used in sample regiontags to apiMajorVersion. --- .../google/api/generator/gapic/composer/Composer.java | 2 +- .../common/AbstractServiceClientClassComposer.java | 2 +- .../common/AbstractServiceSettingsClassComposer.java | 2 +- .../AbstractServiceStubSettingsClassComposer.java | 2 +- .../google/api/generator/gapic/model/GapicClass.java | 10 +++++----- .../api/generator/gapic/composer/ComposerTest.java | 4 ++-- .../composer/grpc/ServiceClientClassComposerTest.java | 2 +- .../grpc/ServiceSettingsClassComposerTest.java | 2 +- .../composer/grpc/ServiceStubClassComposerTest.java | 2 +- .../grpc/ServiceStubSettingsClassComposerTest.java | 2 +- 10 files changed, 15 insertions(+), 15 deletions(-) diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/Composer.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/Composer.java index e92fd9b0c4..c1bd5ab3c7 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/Composer.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/Composer.java @@ -207,7 +207,7 @@ static List prepareExecutableSamples(List clazzes) { sample -> samples.add( addRegionTagAndHeaderToSample( - sample, gapicClass.apiShortName(), gapicClass.apiVersion()))); + sample, gapicClass.apiShortName(), gapicClass.apiMajorVersion()))); clazzesWithSamples.add(gapicClass.withSamples(samples)); }); return clazzesWithSamples; diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java index bad8cfdd09..7a5f5b254d 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java @@ -173,7 +173,7 @@ public GapicClass generate(GapicContext context, Service service) { updateGapicMetadata(context, service, className, grpcRpcsToJavaMethodNames); return GapicClass.create(kind, classDef, SampleComposerUtil.handleDuplicateSamples(samples)) .withApiShortName(service.apiShortName()) - .withApiVersion(service.apiMajorVersion()); + .withApiMajorVersion(service.apiMajorVersion()); } private static List createClassAnnotations(Service service, TypeStore typeStore) { diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java index b0e5e40a25..b612ee305b 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java @@ -128,7 +128,7 @@ public GapicClass generate(GapicContext context, Service service) { .build(); return GapicClass.create(kind, classDef, SampleComposerUtil.handleDuplicateSamples(samples)) .withApiShortName(service.apiShortName()) - .withApiVersion(service.apiMajorVersion()); + .withApiMajorVersion(service.apiMajorVersion()); } private static List createClassHeaderComments( diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java index 5865c562f7..3156c6bd73 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java @@ -204,7 +204,7 @@ public GapicClass generate(GapicContext context, Service service) { return GapicClass.create( GapicClass.Kind.STUB, classDef, SampleComposerUtil.handleDuplicateSamples(samples)) .withApiShortName(service.apiShortName()) - .withApiVersion(service.apiMajorVersion()); + .withApiMajorVersion(service.apiMajorVersion()); } protected MethodDefinition createDefaultCredentialsProviderBuilderMethod() { diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/GapicClass.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/GapicClass.java index 11d6598e7e..0c57c5601a 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/GapicClass.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/GapicClass.java @@ -43,7 +43,7 @@ public enum Kind { public abstract String apiShortName(); // Only used for generating the region tag for samples; therefore only used in select Composers. - public abstract String apiVersion(); + public abstract String apiMajorVersion(); /** * Create a GapicClass with minimal information. This is intended to be used for GapicClasses that @@ -76,7 +76,7 @@ static Builder builder() { return new AutoValue_GapicClass.Builder() .setSamples(Collections.emptyList()) .setApiShortName("") - .setApiVersion(""); + .setApiMajorVersion(""); } abstract Builder toBuilder(); @@ -89,8 +89,8 @@ public final GapicClass withApiShortName(String apiShortName) { return toBuilder().setApiShortName(apiShortName).build(); } - public final GapicClass withApiVersion(String apiVersion) { - return toBuilder().setApiVersion(apiVersion).build(); + public final GapicClass withApiMajorVersion(String apiMajorVersion) { + return toBuilder().setApiMajorVersion(apiMajorVersion).build(); } @AutoValue.Builder @@ -103,7 +103,7 @@ abstract static class Builder { abstract Builder setApiShortName(String apiShortName); - abstract Builder setApiVersion(String apiVersion); + abstract Builder setApiMajorVersion(String apiMajorVersion); abstract GapicClass build(); } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java index 5598e29c9d..640864abfe 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java @@ -45,7 +45,7 @@ public class ComposerTest { GrpcServiceCallableFactoryClassComposer.instance() .generate(context, echoProtoService) .withApiShortName(echoProtoService.apiShortName()) - .withApiVersion(echoProtoService.apiMajorVersion())); + .withApiMajorVersion(echoProtoService.apiMajorVersion())); private final Sample sample = Sample.builder() .setRegionTag( @@ -160,7 +160,7 @@ private List getTestClassListFromService(Service testService) { .generate(context, testService) .withSamples(ListofSamples) .withApiShortName(testService.apiShortName()) - .withApiVersion(testService.apiMajorVersion()); + .withApiMajorVersion(testService.apiMajorVersion()); List testClassList = Arrays.asList(new GapicClass[] {testClass}); return testClassList; } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java index 8da1e5015c..39e9908088 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java @@ -83,6 +83,6 @@ public void generateServiceClientClasses() { Assert.assertGoldenSamples( this.getClass(), name, clazz.classDefinition().packageString(), clazz.samples()); Assert.assertCodeEquals(clazz.apiShortName(), apiShortNameExpected); - Assert.assertCodeEquals(clazz.apiVersion(), apiVersionExpected); + Assert.assertCodeEquals(clazz.apiMajorVersion(), apiVersionExpected); } } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java index 9a9f4fb2e1..d358ebca05 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java @@ -69,6 +69,6 @@ public void generateServiceSettingsClasses() { clazz.classDefinition().packageString(), clazz.samples()); Assert.assertCodeEquals(clazz.apiShortName(), apiShortNameExpected); - Assert.assertCodeEquals(clazz.apiVersion(), apiVersionExpected); + Assert.assertCodeEquals(clazz.apiMajorVersion(), apiVersionExpected); } } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java index 387dba166f..ee711c4bbf 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java @@ -55,6 +55,6 @@ public void generateServiceStubClasses() { Assert.assertGoldenClass(this.getClass(), clazz, name + ".golden"); Assert.assertEmptySamples(clazz.samples()); Assert.assertCodeEquals(clazz.apiShortName(), apiShortNameExpected); - Assert.assertCodeEquals(clazz.apiVersion(), apiVersionExpected); + Assert.assertCodeEquals(clazz.apiMajorVersion(), apiVersionExpected); } } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java index d552342d67..ad71e3aee4 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java @@ -81,6 +81,6 @@ public void generateServiceStubSettingsClasses() { clazz.classDefinition().packageString(), clazz.samples()); Assert.assertCodeEquals(clazz.apiShortName(), apiShortNameExpected); - Assert.assertCodeEquals(clazz.apiVersion(), apiVersionExpected); + Assert.assertCodeEquals(clazz.apiMajorVersion(), apiVersionExpected); } } From 3b35cd0a0de6a01f8dac728a0c5e788f91bb0777 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Tue, 9 Apr 2024 12:34:54 -0400 Subject: [PATCH 03/11] add new apiVersion field to Service model. --- .../api/generator/gapic/model/Service.java | 9 ++++ .../generator/gapic/model/ServiceTest.java | 42 +++++++++++++++---- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/Service.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/Service.java index cf0e21c597..abba77eddf 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/Service.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/Service.java @@ -27,6 +27,9 @@ public abstract class Service { public abstract String name(); + @Nullable + public abstract String apiVersion(); + public abstract String defaultHost(); public abstract ImmutableList oauthScopes(); @@ -52,6 +55,10 @@ public boolean hasDescription() { return !Strings.isNullOrEmpty(description()); } + public boolean hasApiVersion() { + return !Strings.isNullOrEmpty(apiVersion()); + } + public String hostServiceName() { // Host Service Name is guaranteed to exist and be non-null and non-empty // Parser will fail if the default host is not supplied @@ -158,6 +165,8 @@ public abstract static class Builder { public abstract Builder setOverriddenName(String overriddenName); + public abstract Builder setApiVersion(String apiVersion); + public abstract Builder setDefaultHost(String defaultHost); public abstract Builder setOauthScopes(List oauthScopes); diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java index dbe3d0fa15..f3e8e0e998 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java @@ -16,24 +16,20 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import com.google.api.generator.engine.ast.TypeNode; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import java.util.Arrays; +import org.junit.Before; import org.junit.Test; public class ServiceTest { private static final String SHOWCASE_PACKAGE_NAME = "com.google.showcase.v1beta1"; - private static final Service.Builder testServiceBuilder = - Service.builder() - .setName("Echo") - .setDefaultHost("localhost:7469") - .setOauthScopes(Arrays.asList("https://www.googleapis.com/auth/cloud-platform")) - .setPakkage(SHOWCASE_PACKAGE_NAME) - .setProtoPakkage(SHOWCASE_PACKAGE_NAME) - .setOriginalJavaPackage(SHOWCASE_PACKAGE_NAME) - .setOverriddenName("Echo"); + private Service.Builder testServiceBuilder; private static final Method.Builder testMethodBuilder = Method.builder() @@ -50,6 +46,19 @@ public class ServiceTest { .setIsAsteriskBody(false) .setHttpVerb(HttpBindings.HttpVerb.GET); + @Before + public void init() { + testServiceBuilder = + Service.builder() + .setName("Echo") + .setDefaultHost("localhost:7469") + .setOauthScopes(Arrays.asList("https://www.googleapis.com/auth/cloud-platform")) + .setPakkage(SHOWCASE_PACKAGE_NAME) + .setProtoPakkage(SHOWCASE_PACKAGE_NAME) + .setOriginalJavaPackage(SHOWCASE_PACKAGE_NAME) + .setOverriddenName("Echo"); + } + @Test public void apiShortName_shouldReturnApiShortNameIfHostContainsRegionalEndpoint() { String defaultHost = "us-east1-pubsub.googleapis.com"; @@ -85,6 +94,21 @@ public void apiVersion_shouldReturnVersionIfMatch() { assertEquals("v1", testService.apiMajorVersion()); } + @Test + public void apiVersion_shouldReturnApiVersion() { + String apiVersion = "v1_20230601"; + Service testService = testServiceBuilder.setApiVersion(apiVersion).build(); + assertTrue(testService.hasApiVersion()); + assertEquals(apiVersion, testService.apiVersion()); + } + + @Test + public void apiVersion_shouldReturnNullApiVersion() { + Service testService = testServiceBuilder.build(); + assertNull(testService.apiVersion()); + assertFalse(testService.hasApiVersion()); + } + @Test public void apiVersion_shouldReturnEmptyIfNoMatch() { String protoPackage = "com.google.showcase"; From 23f2ce72a52aeb7c7077d292faae4f647a0e5772 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Tue, 9 Apr 2024 16:11:25 -0400 Subject: [PATCH 04/11] add logic so Parser can handle optional api version.: --- .../generator/gapic/protoparser/Parser.java | 47 +++++++++++-------- .../gapic/protoparser/ParserTest.java | 24 ++++++++++ .../src/test/proto/echo.proto | 1 + 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java index e960ad3f6d..e13350f7a1 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java @@ -483,26 +483,33 @@ public static List parseService( overriddenServiceName = languageSettings.getJavaServiceName(fileDescriptor.getPackage(), s.getName()); } - return serviceBuilder - .setName(serviceName) - .setOverriddenName(overriddenServiceName) - .setDefaultHost(defaultHost) - .setOauthScopes(oauthScopes) - .setPakkage(pakkage) - .setOriginalJavaPackage(originalJavaPackage) - .setProtoPakkage(fileDescriptor.getPackage()) - .setIsDeprecated(isDeprecated) - .setMethods( - parseMethods( - s, - pakkage, - messageTypes, - resourceNames, - serviceConfigOpt, - serviceYamlProtoOpt, - outputArgResourceNames, - transport)) - .build(); + Service.Builder service = + serviceBuilder + .setName(serviceName) + .setOverriddenName(overriddenServiceName) + .setDefaultHost(defaultHost) + .setOauthScopes(oauthScopes) + .setPakkage(pakkage) + .setOriginalJavaPackage(originalJavaPackage) + .setProtoPakkage(fileDescriptor.getPackage()) + .setIsDeprecated(isDeprecated) + .setMethods( + parseMethods( + s, + pakkage, + messageTypes, + resourceNames, + serviceConfigOpt, + serviceYamlProtoOpt, + outputArgResourceNames, + transport)); + + String apiVersion = null; + if (serviceOptions.hasExtension(ClientProto.apiVersion)) { + apiVersion = serviceOptions.getExtension(ClientProto.apiVersion); + service.setApiVersion(apiVersion); + } + return service.build(); }) .collect(Collectors.toList()); } 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 b4379fdf66..d941656809 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 @@ -17,6 +17,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; @@ -607,6 +608,29 @@ public void parseNestedProtoTypeName() { "google.ads.googleads.v3.resources.MutateJob.MutateJobMetadata")); } + @Test + public void testServiceApiVersionParsed() { + Map messageTypes = Parser.parseMessages(echoFileDescriptor); + Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + List services = + Parser.parseService( + echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), new HashSet<>()); + com.google.api.generator.gapic.model.Service parsedEchoService = services.get(0); + assertEquals("v1_20230601", parsedEchoService.apiVersion()); + } + + @Test + public void testServiceWithoutApiVersionParsed() { + FileDescriptor bookshopFileDescriptor = BookshopProto.getDescriptor(); + Map messageTypes = Parser.parseMessages(bookshopFileDescriptor); + Map resourceNames = Parser.parseResourceNames(bookshopFileDescriptor); + List services = + Parser.parseService( + bookshopFileDescriptor, messageTypes, resourceNames, Optional.empty(), new HashSet<>()); + com.google.api.generator.gapic.model.Service parsedBookshopService = services.get(0); + assertNull(parsedBookshopService.apiVersion()); + } + private void assertMethodArgumentEquals( String name, TypeNode type, List nestedFields, MethodArgument argument) { assertEquals(name, argument.name()); diff --git a/gapic-generator-java/src/test/proto/echo.proto b/gapic-generator-java/src/test/proto/echo.proto index 361e661459..eba8f20bbe 100644 --- a/gapic-generator-java/src/test/proto/echo.proto +++ b/gapic-generator-java/src/test/proto/echo.proto @@ -44,6 +44,7 @@ service Echo { // This service is meant to only run locally on the port 7469 (keypad digits // for "show"). option (google.api.default_host) = "localhost:7469"; + option (google.api.api_version) = "v1_20230601"; option (google.api.oauth_scopes) = "https://www.googleapis.com/auth/cloud-platform"; From 255c6f3dfdfa5b8908abf9943066214f9721f989 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Fri, 12 Apr 2024 18:01:21 -0400 Subject: [PATCH 05/11] fix: simplify parser logic. --- .../generator/gapic/protoparser/Parser.java | 52 +++++++++---------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java index e13350f7a1..c7d0b95a3d 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/protoparser/Parser.java @@ -470,6 +470,11 @@ public static List parseService( } } + if (serviceOptions.hasExtension(ClientProto.apiVersion)) { + String apiVersion = serviceOptions.getExtension(ClientProto.apiVersion); + serviceBuilder.setApiVersion(apiVersion); + } + String serviceName = s.getName(); String overriddenServiceName = serviceName; String pakkage = TypeParser.getPackage(fileDescriptor); @@ -483,33 +488,26 @@ public static List parseService( overriddenServiceName = languageSettings.getJavaServiceName(fileDescriptor.getPackage(), s.getName()); } - Service.Builder service = - serviceBuilder - .setName(serviceName) - .setOverriddenName(overriddenServiceName) - .setDefaultHost(defaultHost) - .setOauthScopes(oauthScopes) - .setPakkage(pakkage) - .setOriginalJavaPackage(originalJavaPackage) - .setProtoPakkage(fileDescriptor.getPackage()) - .setIsDeprecated(isDeprecated) - .setMethods( - parseMethods( - s, - pakkage, - messageTypes, - resourceNames, - serviceConfigOpt, - serviceYamlProtoOpt, - outputArgResourceNames, - transport)); - - String apiVersion = null; - if (serviceOptions.hasExtension(ClientProto.apiVersion)) { - apiVersion = serviceOptions.getExtension(ClientProto.apiVersion); - service.setApiVersion(apiVersion); - } - return service.build(); + return serviceBuilder + .setName(serviceName) + .setOverriddenName(overriddenServiceName) + .setDefaultHost(defaultHost) + .setOauthScopes(oauthScopes) + .setPakkage(pakkage) + .setOriginalJavaPackage(originalJavaPackage) + .setProtoPakkage(fileDescriptor.getPackage()) + .setIsDeprecated(isDeprecated) + .setMethods( + parseMethods( + s, + pakkage, + messageTypes, + resourceNames, + serviceConfigOpt, + serviceYamlProtoOpt, + outputArgResourceNames, + transport)) + .build(); }) .collect(Collectors.toList()); } From 1ddb2846811cbf1f3198270996a9554b81052ef2 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Wed, 17 Apr 2024 16:30:23 -0400 Subject: [PATCH 06/11] rename old apiVersion to packageVersion to disambiguous. --- .../api/generator/gapic/composer/Composer.java | 2 +- .../AbstractServiceClientClassComposer.java | 2 +- .../AbstractServiceSettingsClassComposer.java | 2 +- ...AbstractServiceStubSettingsClassComposer.java | 2 +- .../api/generator/gapic/model/GapicClass.java | 10 +++++----- .../api/generator/gapic/model/Service.java | 16 ++++++++-------- .../generator/gapic/composer/ComposerTest.java | 4 ++-- .../grpc/ServiceClientClassComposerTest.java | 2 +- .../grpc/ServiceSettingsClassComposerTest.java | 2 +- .../grpc/ServiceStubClassComposerTest.java | 2 +- .../ServiceStubSettingsClassComposerTest.java | 2 +- .../api/generator/gapic/model/ServiceTest.java | 4 ++-- 12 files changed, 25 insertions(+), 25 deletions(-) diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/Composer.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/Composer.java index c1bd5ab3c7..f2ffa56016 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/Composer.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/Composer.java @@ -207,7 +207,7 @@ static List prepareExecutableSamples(List clazzes) { sample -> samples.add( addRegionTagAndHeaderToSample( - sample, gapicClass.apiShortName(), gapicClass.apiMajorVersion()))); + sample, gapicClass.apiShortName(), gapicClass.packageVersion()))); clazzesWithSamples.add(gapicClass.withSamples(samples)); }); return clazzesWithSamples; diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java index 7a5f5b254d..e8bd6fe081 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java @@ -173,7 +173,7 @@ public GapicClass generate(GapicContext context, Service service) { updateGapicMetadata(context, service, className, grpcRpcsToJavaMethodNames); return GapicClass.create(kind, classDef, SampleComposerUtil.handleDuplicateSamples(samples)) .withApiShortName(service.apiShortName()) - .withApiMajorVersion(service.apiMajorVersion()); + .withPackageVersion(service.packageVersion()); } private static List createClassAnnotations(Service service, TypeStore typeStore) { diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java index b612ee305b..60e3fcd38a 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java @@ -128,7 +128,7 @@ public GapicClass generate(GapicContext context, Service service) { .build(); return GapicClass.create(kind, classDef, SampleComposerUtil.handleDuplicateSamples(samples)) .withApiShortName(service.apiShortName()) - .withApiMajorVersion(service.apiMajorVersion()); + .withPackageVersion(service.packageVersion()); } private static List createClassHeaderComments( diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java index 3156c6bd73..88a3cc6ecd 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java @@ -204,7 +204,7 @@ public GapicClass generate(GapicContext context, Service service) { return GapicClass.create( GapicClass.Kind.STUB, classDef, SampleComposerUtil.handleDuplicateSamples(samples)) .withApiShortName(service.apiShortName()) - .withApiMajorVersion(service.apiMajorVersion()); + .withPackageVersion(service.packageVersion()); } protected MethodDefinition createDefaultCredentialsProviderBuilderMethod() { diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/GapicClass.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/GapicClass.java index 0c57c5601a..1fd06a8dfe 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/GapicClass.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/GapicClass.java @@ -43,7 +43,7 @@ public enum Kind { public abstract String apiShortName(); // Only used for generating the region tag for samples; therefore only used in select Composers. - public abstract String apiMajorVersion(); + public abstract String packageVersion(); /** * Create a GapicClass with minimal information. This is intended to be used for GapicClasses that @@ -76,7 +76,7 @@ static Builder builder() { return new AutoValue_GapicClass.Builder() .setSamples(Collections.emptyList()) .setApiShortName("") - .setApiMajorVersion(""); + .setPackageVersion(""); } abstract Builder toBuilder(); @@ -89,8 +89,8 @@ public final GapicClass withApiShortName(String apiShortName) { return toBuilder().setApiShortName(apiShortName).build(); } - public final GapicClass withApiMajorVersion(String apiMajorVersion) { - return toBuilder().setApiMajorVersion(apiMajorVersion).build(); + public final GapicClass withPackageVersion(String packageVersion) { + return toBuilder().setPackageVersion(packageVersion).build(); } @AutoValue.Builder @@ -103,7 +103,7 @@ abstract static class Builder { abstract Builder setApiShortName(String apiShortName); - abstract Builder setApiMajorVersion(String apiMajorVersion); + abstract Builder setPackageVersion(String packageVersion); abstract GapicClass build(); } diff --git a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/Service.java b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/Service.java index abba77eddf..656bf6bbd3 100644 --- a/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/Service.java +++ b/gapic-generator-java/src/main/java/com/google/api/generator/gapic/model/Service.java @@ -72,9 +72,9 @@ public String apiShortName() { return ""; } - public String apiMajorVersion() { + public String packageVersion() { if (!Strings.isNullOrEmpty(protoPakkage())) { - return parseApiMajorVersion(protoPakkage()); + return parsePackageVersion(protoPakkage()); } return ""; } @@ -186,17 +186,17 @@ public abstract static class Builder { public abstract Service build(); } - private static String parseApiMajorVersion(String protoPackage) { - // parse protoPackage for apiMajorVersion + private static String parsePackageVersion(String protoPackage) { + // parse protoPackage for packageVersion String[] pakkage = protoPackage.split("\\."); - String apiMajorVersion; + String packageVersion; // e.g. v1, v2, v1beta1 if (pakkage[pakkage.length - 1].matches("v[0-9].*")) { - apiMajorVersion = pakkage[pakkage.length - 1]; + packageVersion = pakkage[pakkage.length - 1]; } else { - apiMajorVersion = ""; + packageVersion = ""; } - return apiMajorVersion; + return packageVersion; } // Parse the service name from the default host configured in the protos diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java index 640864abfe..66ad0584be 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java @@ -45,7 +45,7 @@ public class ComposerTest { GrpcServiceCallableFactoryClassComposer.instance() .generate(context, echoProtoService) .withApiShortName(echoProtoService.apiShortName()) - .withApiMajorVersion(echoProtoService.apiMajorVersion())); + .withPackageVersion(echoProtoService.packageVersion())); private final Sample sample = Sample.builder() .setRegionTag( @@ -160,7 +160,7 @@ private List getTestClassListFromService(Service testService) { .generate(context, testService) .withSamples(ListofSamples) .withApiShortName(testService.apiShortName()) - .withApiMajorVersion(testService.apiMajorVersion()); + .withPackageVersion(testService.packageVersion()); List testClassList = Arrays.asList(new GapicClass[] {testClass}); return testClassList; } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java index 39e9908088..29b25292f6 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java @@ -83,6 +83,6 @@ public void generateServiceClientClasses() { Assert.assertGoldenSamples( this.getClass(), name, clazz.classDefinition().packageString(), clazz.samples()); Assert.assertCodeEquals(clazz.apiShortName(), apiShortNameExpected); - Assert.assertCodeEquals(clazz.apiMajorVersion(), apiVersionExpected); + Assert.assertCodeEquals(clazz.packageVersion(), apiVersionExpected); } } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java index d358ebca05..f8600f49c8 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java @@ -69,6 +69,6 @@ public void generateServiceSettingsClasses() { clazz.classDefinition().packageString(), clazz.samples()); Assert.assertCodeEquals(clazz.apiShortName(), apiShortNameExpected); - Assert.assertCodeEquals(clazz.apiMajorVersion(), apiVersionExpected); + Assert.assertCodeEquals(clazz.packageVersion(), apiVersionExpected); } } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java index ee711c4bbf..7f399f6ec9 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java @@ -55,6 +55,6 @@ public void generateServiceStubClasses() { Assert.assertGoldenClass(this.getClass(), clazz, name + ".golden"); Assert.assertEmptySamples(clazz.samples()); Assert.assertCodeEquals(clazz.apiShortName(), apiShortNameExpected); - Assert.assertCodeEquals(clazz.apiMajorVersion(), apiVersionExpected); + Assert.assertCodeEquals(clazz.packageVersion(), apiVersionExpected); } } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java index ad71e3aee4..ae8776b1e3 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java @@ -81,6 +81,6 @@ public void generateServiceStubSettingsClasses() { clazz.classDefinition().packageString(), clazz.samples()); Assert.assertCodeEquals(clazz.apiShortName(), apiShortNameExpected); - Assert.assertCodeEquals(clazz.apiMajorVersion(), apiVersionExpected); + Assert.assertCodeEquals(clazz.packageVersion(), apiVersionExpected); } } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java index f3e8e0e998..72caa83d88 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java @@ -91,7 +91,7 @@ public void apiShortName_shouldReturnHostIfNoPeriods() { public void apiVersion_shouldReturnVersionIfMatch() { String protoPackage = "com.google.showcase.v1"; Service testService = testServiceBuilder.setProtoPakkage(protoPackage).build(); - assertEquals("v1", testService.apiMajorVersion()); + assertEquals("v1", testService.packageVersion()); } @Test @@ -113,7 +113,7 @@ public void apiVersion_shouldReturnNullApiVersion() { public void apiVersion_shouldReturnEmptyIfNoMatch() { String protoPackage = "com.google.showcase"; Service testService = testServiceBuilder.setProtoPakkage(protoPackage).build(); - assertEquals("", testService.apiMajorVersion()); + assertEquals("", testService.packageVersion()); } @Test From 0e8705ddd190bd53075ce2152cb92129497cf5bc Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Thu, 18 Apr 2024 17:09:12 -0400 Subject: [PATCH 07/11] test: add additional service in echo.proto to test that api-version is correctly parse per service. --- .../common/RetrySettingsComposerTest.java | 8 +++---- .../ShowcaseWithEchoPackageInfo.golden | 20 ++++++++++++++++ .../gapic/model/GapicServiceConfigTest.java | 3 +-- .../gapic/protoparser/ParserTest.java | 2 ++ .../src/test/proto/echo.proto | 24 +++++++++++++++++++ 5 files changed, 51 insertions(+), 6 deletions(-) diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/common/RetrySettingsComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/common/RetrySettingsComposerTest.java index 0d567f409d..d005a092bc 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/common/RetrySettingsComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/common/RetrySettingsComposerTest.java @@ -148,7 +148,7 @@ public void codesDefinitionsBlock_noConfigsFound() { List services = Parser.parseService( echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); - assertEquals(1, services.size()); + assertEquals(2, services.size()); Service service = services.get(0); @@ -184,7 +184,7 @@ public void codesDefinitionsBlock_basic() { List services = Parser.parseService( echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); - assertEquals(1, services.size()); + assertEquals(2, services.size()); Service service = services.get(0); @@ -223,7 +223,7 @@ public void simpleBuilderExpr_basic() { List services = Parser.parseService( echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); - assertEquals(1, services.size()); + assertEquals(2, services.size()); Service service = services.get(0); @@ -304,7 +304,7 @@ public void lroBuilderExpr() { List services = Parser.parseService( echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); - assertEquals(1, services.size()); + assertEquals(2, services.size()); Service service = services.get(0); diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/goldens/ShowcaseWithEchoPackageInfo.golden b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/goldens/ShowcaseWithEchoPackageInfo.golden index a2c213eb11..8b864f71c0 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/goldens/ShowcaseWithEchoPackageInfo.golden +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/goldens/ShowcaseWithEchoPackageInfo.golden @@ -20,6 +20,26 @@ * EchoResponse response = echoClient.echo(); * } * } + * + *

======================= EchoAgainClient ======================= + * + *

Service Description: This service is used showcase the four main types of rpcs - unary, server + * side streaming, client side streaming, and bidirectional streaming. This service also exposes + * methods that explicitly implement server delay, and paginated calls. Set the 'showcase-trailer' + * metadata key on any method to have the values echoed in the response trailers. + * + *

Sample for EchoAgainClient: + * + *

{@code
+ * // This snippet has been automatically generated and should be regarded as a code template only.
+ * // It will require modifications to work:
+ * // - It may require correct/in-range values for request initialization.
+ * // - It may require specifying regional endpoints when creating the service client as shown in
+ * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
+ * try (EchoAgainClient echoAgainClient = EchoAgainClient.create()) {
+ *   EchoResponse response = echoAgainClient.echo();
+ * }
+ * }
*/ @Generated("by gapic-generator-java") package com.google.showcase.v1beta1; diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/GapicServiceConfigTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/GapicServiceConfigTest.java index ca4cd84418..d47c4b3625 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/GapicServiceConfigTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/GapicServiceConfigTest.java @@ -244,8 +244,7 @@ private static Service parseService(FileDescriptor fileDescriptor) { List services = Parser.parseService( fileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); - assertEquals(1, services.size()); - + assertEquals(2, services.size()); return services.get(0); } 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 d941656809..90c6c93f74 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 @@ -617,6 +617,8 @@ public void testServiceApiVersionParsed() { echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), new HashSet<>()); com.google.api.generator.gapic.model.Service parsedEchoService = services.get(0); assertEquals("v1_20230601", parsedEchoService.apiVersion()); + com.google.api.generator.gapic.model.Service parsedEchoAgainService = services.get(1); + assertEquals("fake-version", parsedEchoAgainService.apiVersion()); } @Test diff --git a/gapic-generator-java/src/test/proto/echo.proto b/gapic-generator-java/src/test/proto/echo.proto index eba8f20bbe..e55ea0d130 100644 --- a/gapic-generator-java/src/test/proto/echo.proto +++ b/gapic-generator-java/src/test/proto/echo.proto @@ -143,6 +143,30 @@ service Echo { } } +// This service is test service level settings. +service EchoAgain { + // This service is meant to only run locally on the port 7469 (keypad digits + // for "show"). + option (google.api.default_host) = "localhost:7469"; + option (google.api.api_version) = "fake-version"; + option (google.api.oauth_scopes) = + "https://www.googleapis.com/auth/cloud-platform"; + + // This method simply echos the request. This method is showcases unary rpcs. + rpc Echo(EchoRequest) returns (EchoResponse) { + option (google.api.http) = { + post: "/v1beta1/echo:echo" + body: "*" + }; + option (google.api.method_signature) = "content"; + option (google.api.method_signature) = "error"; + option (google.api.method_signature) = "content,severity"; + option (google.api.method_signature) = "name"; + option (google.api.method_signature) = "parent"; + option (google.api.method_signature) = ""; + } +} + // A severity enum used to test enum capabilities in GAPIC surfaces enum Severity { UNNECESSARY = 0; From f025a3f2b838dbde89a9872561297229f518a922 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Thu, 18 Apr 2024 17:46:42 -0400 Subject: [PATCH 08/11] fix: checkstyle --- .../api/generator/gapic/model/GapicServiceConfigTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/GapicServiceConfigTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/GapicServiceConfigTest.java index d47c4b3625..686c43542f 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/GapicServiceConfigTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/GapicServiceConfigTest.java @@ -244,7 +244,7 @@ private static Service parseService(FileDescriptor fileDescriptor) { List services = Parser.parseService( fileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); - assertEquals(2, services.size()); + assertEquals(2, services.size()); return services.get(0); } From 1bc4d64dbf1396791a1b8ff33fc2bf187a0df00b Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Tue, 23 Apr 2024 14:57:12 -0400 Subject: [PATCH 09/11] test: revert changes to echo.proto, and create dedicated proto to test this feat. --- .../common/RetrySettingsComposerTest.java | 8 +- .../ShowcaseWithEchoPackageInfo.golden | 20 -- .../gapic/model/GapicServiceConfigTest.java | 3 +- .../gapic/protoparser/ParserTest.java | 60 +++- .../src/test/proto/api_version_testing.proto | 264 ++++++++++++++++++ .../src/test/proto/echo.proto | 25 -- 6 files changed, 323 insertions(+), 57 deletions(-) create mode 100644 gapic-generator-java/src/test/proto/api_version_testing.proto diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/common/RetrySettingsComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/common/RetrySettingsComposerTest.java index d005a092bc..0d567f409d 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/common/RetrySettingsComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/common/RetrySettingsComposerTest.java @@ -148,7 +148,7 @@ public void codesDefinitionsBlock_noConfigsFound() { List services = Parser.parseService( echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); - assertEquals(2, services.size()); + assertEquals(1, services.size()); Service service = services.get(0); @@ -184,7 +184,7 @@ public void codesDefinitionsBlock_basic() { List services = Parser.parseService( echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); - assertEquals(2, services.size()); + assertEquals(1, services.size()); Service service = services.get(0); @@ -223,7 +223,7 @@ public void simpleBuilderExpr_basic() { List services = Parser.parseService( echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); - assertEquals(2, services.size()); + assertEquals(1, services.size()); Service service = services.get(0); @@ -304,7 +304,7 @@ public void lroBuilderExpr() { List services = Parser.parseService( echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); - assertEquals(2, services.size()); + assertEquals(1, services.size()); Service service = services.get(0); diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/goldens/ShowcaseWithEchoPackageInfo.golden b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/goldens/ShowcaseWithEchoPackageInfo.golden index 8b864f71c0..a2c213eb11 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/goldens/ShowcaseWithEchoPackageInfo.golden +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/goldens/ShowcaseWithEchoPackageInfo.golden @@ -20,26 +20,6 @@ * EchoResponse response = echoClient.echo(); * } * } - * - *

======================= EchoAgainClient ======================= - * - *

Service Description: This service is used showcase the four main types of rpcs - unary, server - * side streaming, client side streaming, and bidirectional streaming. This service also exposes - * methods that explicitly implement server delay, and paginated calls. Set the 'showcase-trailer' - * metadata key on any method to have the values echoed in the response trailers. - * - *

Sample for EchoAgainClient: - * - *

{@code
- * // This snippet has been automatically generated and should be regarded as a code template only.
- * // It will require modifications to work:
- * // - It may require correct/in-range values for request initialization.
- * // - It may require specifying regional endpoints when creating the service client as shown in
- * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
- * try (EchoAgainClient echoAgainClient = EchoAgainClient.create()) {
- *   EchoResponse response = echoAgainClient.echo();
- * }
- * }
*/ @Generated("by gapic-generator-java") package com.google.showcase.v1beta1; diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/GapicServiceConfigTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/GapicServiceConfigTest.java index 686c43542f..ca4cd84418 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/GapicServiceConfigTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/GapicServiceConfigTest.java @@ -244,7 +244,8 @@ private static Service parseService(FileDescriptor fileDescriptor) { List services = Parser.parseService( fileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames); - assertEquals(2, services.size()); + assertEquals(1, services.size()); + return services.get(0); } 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 90c6c93f74..42462dd1c8 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 @@ -36,6 +36,7 @@ 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.api.version.test.ApiVersionTestingOuterClass; import com.google.auto.populate.field.AutoPopulateFieldTestingOuterClass; import com.google.bookshop.v1beta1.BookshopProto; import com.google.common.collect.ImmutableList; @@ -609,16 +610,61 @@ public void parseNestedProtoTypeName() { } @Test - public void testServiceApiVersionParsed() { - Map messageTypes = Parser.parseMessages(echoFileDescriptor); - Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + public void parseServiceApiVersionTest() { + FileDescriptor apiVersionFileDescriptor = ApiVersionTestingOuterClass.getDescriptor(); + Map messageTypes = Parser.parseMessages(apiVersionFileDescriptor); + Map resourceNames = Parser.parseResourceNames(apiVersionFileDescriptor); List services = Parser.parseService( - echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), new HashSet<>()); + apiVersionFileDescriptor, + messageTypes, + resourceNames, + Optional.empty(), + new HashSet<>()); com.google.api.generator.gapic.model.Service parsedEchoService = services.get(0); - assertEquals("v1_20230601", parsedEchoService.apiVersion()); - com.google.api.generator.gapic.model.Service parsedEchoAgainService = services.get(1); - assertEquals("fake-version", parsedEchoAgainService.apiVersion()); + + assertEquals("Echo", parsedEchoService.overriddenName()); + assertTrue(parsedEchoService.hasApiVersion()); + assertEquals("fake_version", parsedEchoService.apiVersion()); + } + + @Test + public void parseServiceWithoutApiVersionTest() { + FileDescriptor apiVersionFileDescriptor = ApiVersionTestingOuterClass.getDescriptor(); + Map messageTypes = Parser.parseMessages(apiVersionFileDescriptor); + Map resourceNames = Parser.parseResourceNames(apiVersionFileDescriptor); + List services = + Parser.parseService( + apiVersionFileDescriptor, + messageTypes, + resourceNames, + Optional.empty(), + new HashSet<>()); + com.google.api.generator.gapic.model.Service parsedEchoWithoutVersionService = services.get(1); + + assertNull(parsedEchoWithoutVersionService.apiVersion()); + assertFalse(parsedEchoWithoutVersionService.hasApiVersion()); + assertEquals("EchoWithoutVersion", parsedEchoWithoutVersionService.overriddenName()); + } + + @Test + public void parseServiceWithEmptyApiVersionTest() { + FileDescriptor apiVersionFileDescriptor = ApiVersionTestingOuterClass.getDescriptor(); + Map messageTypes = Parser.parseMessages(apiVersionFileDescriptor); + Map resourceNames = Parser.parseResourceNames(apiVersionFileDescriptor); + List services = + Parser.parseService( + apiVersionFileDescriptor, + messageTypes, + resourceNames, + Optional.empty(), + new HashSet<>()); + com.google.api.generator.gapic.model.Service parsedEchoWithEmptyVersionService = + services.get(2); + + assertEquals("EchoWithEmptyVersion", parsedEchoWithEmptyVersionService.overriddenName()); + assertEquals("", parsedEchoWithEmptyVersionService.apiVersion()); + assertFalse(parsedEchoWithEmptyVersionService.hasApiVersion()); } @Test diff --git a/gapic-generator-java/src/test/proto/api_version_testing.proto b/gapic-generator-java/src/test/proto/api_version_testing.proto new file mode 100644 index 0000000000..52c7b6bdd9 --- /dev/null +++ b/gapic-generator-java/src/test/proto/api_version_testing.proto @@ -0,0 +1,264 @@ +// Copyright 2018 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"; + +package google.api.version.test; + +option java_package = "com.google.api.version.test"; +option java_multiple_files = true; +option java_outer_classname = "ApiVersionTestingOuterClass"; + +option (google.api.resource_definition) = { + type: "showcase.googleapis.com/AnythingGoes" + pattern: "*" +}; +// This proto is used to test scenarios related to api_version feature. +// api_version is set on service level, so this proto includes various services +// to test different scenarios. + +// This service is used to test when api_version string is provided. +service Echo { + // This service is meant to only run locally on the port 7469 (keypad digits + // for "show"). + option (google.api.default_host) = "localhost:7469"; + option (google.api.api_version) = "fake_version"; + option (google.api.oauth_scopes) = + "https://www.googleapis.com/auth/cloud-platform"; + + // This method simply echos the request. This method is showcases unary rpcs. + rpc Echo(EchoRequest) returns (EchoResponse) { + option (google.api.http) = { + post: "/v1beta1/echo:echo" + body: "*" + }; + option (google.api.method_signature) = "content"; + option (google.api.method_signature) = "error"; + option (google.api.method_signature) = "content,severity"; + option (google.api.method_signature) = "name"; + option (google.api.method_signature) = "parent"; + option (google.api.method_signature) = ""; + } +} + +// This service is used to test when api_version is not provided. +service EchoWithoutVersion { + // This service is meant to only run locally on the port 7469 (keypad digits + // for "show"). + option (google.api.default_host) = "localhost:7469"; + option (google.api.oauth_scopes) = + "https://www.googleapis.com/auth/cloud-platform"; + + // This method simply echos the request. This method is showcases unary rpcs. + rpc Echo(EchoRequest) returns (EchoResponse) { + option (google.api.http) = { + post: "/v1beta1/echo:echo" + body: "*" + }; + option (google.api.method_signature) = "content"; + option (google.api.method_signature) = "error"; + option (google.api.method_signature) = "content,severity"; + option (google.api.method_signature) = "name"; + option (google.api.method_signature) = "parent"; + option (google.api.method_signature) = ""; + } +} + +// This service is to test api_version is empty. +service EchoWithEmptyVersion { + // This service is meant to only run locally on the port 7469 (keypad digits + // for "show"). + option (google.api.default_host) = "localhost:7469"; + option (google.api.api_version) = ""; + option (google.api.oauth_scopes) = + "https://www.googleapis.com/auth/cloud-platform"; + + // This method simply echos the request. This method is showcases unary rpcs. + rpc Echo(EchoRequest) returns (EchoResponse) { + option (google.api.http) = { + post: "/v1beta1/echo:echo" + body: "*" + }; + option (google.api.method_signature) = "content"; + option (google.api.method_signature) = "error"; + option (google.api.method_signature) = "content,severity"; + option (google.api.method_signature) = "name"; + option (google.api.method_signature) = "parent"; + option (google.api.method_signature) = ""; + } +} + +// A severity enum used to test enum capabilities in GAPIC surfaces +enum Severity { + UNNECESSARY = 0; + NECESSARY = 1; + URGENT = 2; + CRITICAL = 3; +} + +message Foobar { + option (google.api.resource) = { + type: "showcase.googleapis.com/Foobar" + pattern: "projects/{project}/foobars/{foobar}" + pattern: "projects/{project}/chocolate/variants/{variant}/foobars/{foobar}" + pattern: "foobars/{foobar}" + pattern: "bar_foos/{bar_foo}/foobars/{foobar}" + pattern: "*" + }; + + string name = 1; + string info = 2; +} + +// The request message used for the Echo, Collect and Chat 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 EchoRequest { + string name = 5 [ + (google.api.resource_reference).type = "showcase.googleapis.com/Foobar", + (google.api.field_behavior) = REQUIRED + ]; + + string parent = 6 [ + (google.api.resource_reference).child_type = + "showcase.googleapis.com/AnythingGoes", + (google.api.field_behavior) = REQUIRED + ]; + + 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; + } + + // The severity to be echoed by the server. + Severity severity = 3; + + Foobar foobar = 4; +} + +// The response message for the Echo methods. +message EchoResponse { + // The content specified in the request. + string content = 1; + + // The severity specified in the request. + Severity severity = 2; +} + +// Tests name collisions with java.lang.Object. +message Object { + // The content specified in the request. + string content = 1; +} + +// The request message for the Expand method. +message ExpandRequest { + // 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; + + string info = 3; + +} + +// The request for the PagedExpand method. +message PagedExpandRequest { + // The string to expand. + string content = 1 [(google.api.field_behavior) = REQUIRED]; + + // The amount of words to returned in each page. + int32 page_size = 2; + + // The position of the page to be returned. + string page_token = 3; +} + +// The response for the PagedExpand method. +message PagedExpandResponse { + // The words that were expanded. + repeated EchoResponse responses = 1; + + // The next page token. + string next_page_token = 2; +} + +// The request for Wait method. +message WaitRequest { + oneof end { + // The time that this operation will complete. + google.protobuf.Timestamp end_time = 1; + + // The duration of this operation. + google.protobuf.Duration ttl = 4; + } + + oneof response { + // The error that will be returned by the server. If this code is specified + // to be the OK rpc code, an empty response will be returned. + google.rpc.Status error = 2; + + // The response to be returned on operation completion. + WaitResponse success = 3; + } +} + +// The result of the Wait operation. +message WaitResponse { + // This content of the result. + string content = 1; +} + +// The metadata for Wait operation. +message WaitMetadata { + // The time that this operation will complete. + google.protobuf.Timestamp end_time = 1; +} + +// The request for Block method. +message BlockRequest { + // The amount of time to block before returning a response. + google.protobuf.Duration response_delay = 1; + + oneof response { + // The error that will be returned by the server. If this code is specified + // to be the OK rpc code, an empty response will be returned. + google.rpc.Status error = 2; + + // The response to be returned that will signify successful method call. + BlockResponse success = 3; + } +} + +// The response for Block method. +message BlockResponse { + // This content can contain anything, the server will not depend on a value + // here. + string content = 1; +} diff --git a/gapic-generator-java/src/test/proto/echo.proto b/gapic-generator-java/src/test/proto/echo.proto index e55ea0d130..361e661459 100644 --- a/gapic-generator-java/src/test/proto/echo.proto +++ b/gapic-generator-java/src/test/proto/echo.proto @@ -44,7 +44,6 @@ service Echo { // This service is meant to only run locally on the port 7469 (keypad digits // for "show"). option (google.api.default_host) = "localhost:7469"; - option (google.api.api_version) = "v1_20230601"; option (google.api.oauth_scopes) = "https://www.googleapis.com/auth/cloud-platform"; @@ -143,30 +142,6 @@ service Echo { } } -// This service is test service level settings. -service EchoAgain { - // This service is meant to only run locally on the port 7469 (keypad digits - // for "show"). - option (google.api.default_host) = "localhost:7469"; - option (google.api.api_version) = "fake-version"; - option (google.api.oauth_scopes) = - "https://www.googleapis.com/auth/cloud-platform"; - - // This method simply echos the request. This method is showcases unary rpcs. - rpc Echo(EchoRequest) returns (EchoResponse) { - option (google.api.http) = { - post: "/v1beta1/echo:echo" - body: "*" - }; - option (google.api.method_signature) = "content"; - option (google.api.method_signature) = "error"; - option (google.api.method_signature) = "content,severity"; - option (google.api.method_signature) = "name"; - option (google.api.method_signature) = "parent"; - option (google.api.method_signature) = ""; - } -} - // A severity enum used to test enum capabilities in GAPIC surfaces enum Severity { UNNECESSARY = 0; From 0b57b3ed6f32d343aca60cf77d7420ac48944f4d Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Tue, 23 Apr 2024 15:10:06 -0400 Subject: [PATCH 10/11] chore: rename test name and var references of old apiVersion to packageVersion. --- .../grpc/ServiceClientClassComposerTest.java | 4 ++-- .../grpc/ServiceSettingsClassComposerTest.java | 4 ++-- .../grpc/ServiceStubClassComposerTest.java | 4 ++-- .../ServiceStubSettingsClassComposerTest.java | 4 ++-- .../api/generator/gapic/model/ServiceTest.java | 16 ++++++++-------- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java index 29b25292f6..151ccb578d 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java @@ -72,7 +72,7 @@ public static Collection data() { public String apiShortNameExpected; @Parameterized.Parameter(3) - public String apiVersionExpected; + public String packageVersionExpected; @Test public void generateServiceClientClasses() { @@ -83,6 +83,6 @@ public void generateServiceClientClasses() { Assert.assertGoldenSamples( this.getClass(), name, clazz.classDefinition().packageString(), clazz.samples()); Assert.assertCodeEquals(clazz.apiShortName(), apiShortNameExpected); - Assert.assertCodeEquals(clazz.packageVersion(), apiVersionExpected); + Assert.assertCodeEquals(clazz.packageVersion(), packageVersionExpected); } } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java index f8600f49c8..7a955fecd3 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java @@ -55,7 +55,7 @@ public static Collection data() { public String apiShortNameExpected; @Parameterized.Parameter(3) - public String apiVersionExpected; + public String packageVersionExpected; @Test public void generateServiceSettingsClasses() { @@ -69,6 +69,6 @@ public void generateServiceSettingsClasses() { clazz.classDefinition().packageString(), clazz.samples()); Assert.assertCodeEquals(clazz.apiShortName(), apiShortNameExpected); - Assert.assertCodeEquals(clazz.packageVersion(), apiVersionExpected); + Assert.assertCodeEquals(clazz.packageVersion(), packageVersionExpected); } } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java index 7f399f6ec9..8984c3550c 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java @@ -45,7 +45,7 @@ public static Collection data() { public String apiShortNameExpected; @Parameterized.Parameter(3) - public String apiVersionExpected; + public String packageVersionExpected; @Test public void generateServiceStubClasses() { @@ -55,6 +55,6 @@ public void generateServiceStubClasses() { Assert.assertGoldenClass(this.getClass(), clazz, name + ".golden"); Assert.assertEmptySamples(clazz.samples()); Assert.assertCodeEquals(clazz.apiShortName(), apiShortNameExpected); - Assert.assertCodeEquals(clazz.packageVersion(), apiVersionExpected); + Assert.assertCodeEquals(clazz.packageVersion(), packageVersionExpected); } } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java index ae8776b1e3..ff4f35bff7 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java @@ -67,7 +67,7 @@ public static Collection data() { public String apiShortNameExpected; @Parameterized.Parameter(3) - public String apiVersionExpected; + public String packageVersionExpected; @Test public void generateServiceStubSettingsClasses() { @@ -81,6 +81,6 @@ public void generateServiceStubSettingsClasses() { clazz.classDefinition().packageString(), clazz.samples()); Assert.assertCodeEquals(clazz.apiShortName(), apiShortNameExpected); - Assert.assertCodeEquals(clazz.packageVersion(), apiVersionExpected); + Assert.assertCodeEquals(clazz.packageVersion(), packageVersionExpected); } } diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java index 72caa83d88..4a18ab7021 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java @@ -88,12 +88,19 @@ public void apiShortName_shouldReturnHostIfNoPeriods() { } @Test - public void apiVersion_shouldReturnVersionIfMatch() { + public void packageVersion_shouldReturnVersionIfMatch() { String protoPackage = "com.google.showcase.v1"; Service testService = testServiceBuilder.setProtoPakkage(protoPackage).build(); assertEquals("v1", testService.packageVersion()); } + @Test + public void packageVersion_shouldReturnEmptyIfNoMatch() { + String protoPackage = "com.google.showcase"; + Service testService = testServiceBuilder.setProtoPakkage(protoPackage).build(); + assertEquals("", testService.packageVersion()); + } + @Test public void apiVersion_shouldReturnApiVersion() { String apiVersion = "v1_20230601"; @@ -109,13 +116,6 @@ public void apiVersion_shouldReturnNullApiVersion() { assertFalse(testService.hasApiVersion()); } - @Test - public void apiVersion_shouldReturnEmptyIfNoMatch() { - String protoPackage = "com.google.showcase"; - Service testService = testServiceBuilder.setProtoPakkage(protoPackage).build(); - assertEquals("", testService.packageVersion()); - } - @Test public void hasAnyEnabledMethodsForTransport_shouldReturnFalseForEmptyMethodListForBothTransports() { From a28a8e84aa6e6004eefa02e58033fa57d6b36b65 Mon Sep 17 00:00:00 2001 From: Min Zhu Date: Tue, 23 Apr 2024 15:17:38 -0400 Subject: [PATCH 11/11] test: add test case for service when apiVersion is empty. --- .../google/api/generator/gapic/model/ServiceTest.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java index 4a18ab7021..10b82ec585 100644 --- a/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java +++ b/gapic-generator-java/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java @@ -110,12 +110,20 @@ public void apiVersion_shouldReturnApiVersion() { } @Test - public void apiVersion_shouldReturnNullApiVersion() { + public void apiVersion_shouldReturnNoApiVersionWhenNull() { Service testService = testServiceBuilder.build(); assertNull(testService.apiVersion()); assertFalse(testService.hasApiVersion()); } + @Test + public void apiVersion_shouldReturnNoApiVersionWhenEmpty() { + String apiVersion = ""; + Service testService = testServiceBuilder.setApiVersion(apiVersion).build(); + assertEquals("", testService.apiVersion()); + assertFalse(testService.hasApiVersion()); + } + @Test public void hasAnyEnabledMethodsForTransport_shouldReturnFalseForEmptyMethodListForBothTransports() {