From 2ebe94874699fa99520ddbb3231c8c72821944df Mon Sep 17 00:00:00 2001 From: Emily Wang Date: Mon, 31 Oct 2022 17:04:21 -0400 Subject: [PATCH] refactor: expose parsed api short name and version as fields in Service (#1075) * Exposes parsed apiShortName and apiVersion as fields in Service.java, since the source fields (defaultHost and protoPakkage) are both defined per-service. * Adds these two fields correspondingly to GapicClass so that it can be composed from Service given these two upstream fields, then replaces withDefaultHost (for building GapicClass) withApiShortName and withApiVersion. (Composers and tests where this is currently used for sample generation are updated) * This change will enable Spring Codegen (when eventually split out from this repo) to reuse this parsing logic in descriptive comments and metadata. It also moves the parsing logic to earlier in the parse-compose process. --- .../generator/gapic/composer/Composer.java | 37 +-------- .../AbstractServiceClientClassComposer.java | 3 +- .../AbstractServiceSettingsClassComposer.java | 3 +- ...tractServiceStubSettingsClassComposer.java | 3 +- .../api/generator/gapic/model/GapicClass.java | 23 ++++-- .../api/generator/gapic/model/Service.java | 47 ++++++++++++ .../gapic/composer/ComposerTest.java | 74 ++++++++---------- .../grpc/ServiceClientClassComposerTest.java | 27 +++++-- .../ServiceSettingsClassComposerTest.java | 18 ++++- .../grpc/ServiceStubClassComposerTest.java | 12 ++- .../ServiceStubSettingsClassComposerTest.java | 22 ++++-- .../generator/gapic/model/ServiceTest.java | 75 +++++++++++++++++++ 12 files changed, 238 insertions(+), 106 deletions(-) create mode 100644 src/test/java/com/google/api/generator/gapic/model/ServiceTest.java diff --git a/src/main/java/com/google/api/generator/gapic/composer/Composer.java b/src/main/java/com/google/api/generator/gapic/composer/Composer.java index 60d4f5dde2..acd914a163 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/Composer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/Composer.java @@ -37,8 +37,6 @@ import com.google.api.generator.gapic.model.Service; import com.google.api.generator.gapic.model.Transport; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Splitter; -import com.google.common.collect.Iterables; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -50,8 +48,7 @@ public static List composeServiceClasses(GapicContext context) { clazzes.addAll(generateServiceClasses(context)); clazzes.addAll(generateMockClasses(context, context.mixinServices())); clazzes.addAll(generateResourceNameHelperClasses(context)); - return addApacheLicense( - prepareExecutableSamples(clazzes, context.gapicMetadata().getProtoPackage())); + return addApacheLicense(prepareExecutableSamples(clazzes)); } public static GapicPackageInfo composePackageInfo(GapicContext context) { @@ -193,16 +190,7 @@ public static List generateTestClasses(GapicContext context) { } @VisibleForTesting - static List prepareExecutableSamples(List clazzes, String protoPackage) { - // parse protoPackage for apiVersion - String[] pakkage = protoPackage.split("\\."); - String apiVersion; - // e.g. v1, v2, v1beta1 - if (pakkage[pakkage.length - 1].matches("v[0-9].*")) { - apiVersion = pakkage[pakkage.length - 1]; - } else { - apiVersion = ""; - } + static List prepareExecutableSamples(List clazzes) { // Include license header, apiShortName, and apiVersion List clazzesWithSamples = new ArrayList<>(); clazzes.forEach( @@ -214,31 +202,12 @@ static List prepareExecutableSamples(List clazzes, Strin sample -> samples.add( addRegionTagAndHeaderToSample( - sample, parseDefaultHost(gapicClass.defaultHost()), apiVersion))); + sample, gapicClass.apiShortName(), gapicClass.apiVersion()))); clazzesWithSamples.add(gapicClass.withSamples(samples)); }); return clazzesWithSamples; } - // Parse defaultHost for apiShortName for the RegionTag. Need to account for regional default - // endpoints like - // "us-east1-pubsub.googleapis.com". - @VisibleForTesting - protected static String parseDefaultHost(String defaultHost) { - // If the defaultHost is of the format "**.googleapis.com", take the name before the first - // period. - String apiShortName = Iterables.getFirst(Splitter.on(".").split(defaultHost), defaultHost); - // If the defaultHost is of the format "**-**-**.googleapis.com", take the section before the - // first period and after the last dash to follow CSharp's implementation here: - // https://github.com/googleapis/gapic-generator-csharp/blob/main/Google.Api.Generator/Generation/ServiceDetails.cs#L70 - apiShortName = Iterables.getLast(Splitter.on("-").split(apiShortName), defaultHost); - // `iam-meta-api` service is an exceptional case and is handled as a one-off - if (defaultHost.contains("iam-meta-api")) { - apiShortName = "iam"; - } - return apiShortName; - } - @VisibleForTesting protected static Sample addRegionTagAndHeaderToSample( Sample sample, String apiShortName, String apiVersion) { diff --git a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java index c76f25c9f6..073023cfad 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientClassComposer.java @@ -164,7 +164,8 @@ public GapicClass generate(GapicContext context, Service service) { updateGapicMetadata(context, service, className, grpcRpcsToJavaMethodNames); return GapicClass.create(kind, classDef, SampleComposerUtil.handleDuplicateSamples(samples)) - .withDefaultHost(service.defaultHost()); + .withApiShortName(service.apiShortName()) + .withApiVersion(service.apiVersion()); } private static List createClassAnnotations(Service service, TypeStore typeStore) { diff --git a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java index 08186862ae..fdfa33f471 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceSettingsClassComposer.java @@ -127,7 +127,8 @@ public GapicClass generate(GapicContext context, Service service) { .setNestedClasses(Arrays.asList(createNestedBuilderClass(service, typeStore))) .build(); return GapicClass.create(kind, classDef, SampleComposerUtil.handleDuplicateSamples(samples)) - .withDefaultHost(service.defaultHost()); + .withApiShortName(service.apiShortName()) + .withApiVersion(service.apiVersion()); } private static List createClassHeaderComments( diff --git a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java index cea124e96d..fe66a11e77 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceStubSettingsClassComposer.java @@ -202,7 +202,8 @@ public GapicClass generate(GapicContext context, Service service) { .build(); return GapicClass.create( GapicClass.Kind.STUB, classDef, SampleComposerUtil.handleDuplicateSamples(samples)) - .withDefaultHost(service.defaultHost()); + .withApiShortName(service.apiShortName()) + .withApiVersion(service.apiVersion()); } protected MethodDefinition createDefaultCredentialsProviderBuilderMethod() { diff --git a/src/main/java/com/google/api/generator/gapic/model/GapicClass.java b/src/main/java/com/google/api/generator/gapic/model/GapicClass.java index 165cb4d94b..f05e308290 100644 --- a/src/main/java/com/google/api/generator/gapic/model/GapicClass.java +++ b/src/main/java/com/google/api/generator/gapic/model/GapicClass.java @@ -35,9 +35,11 @@ public enum Kind { public abstract List samples(); - // Represents the host URL for the service. May or may not contain a regional endpoint. Only used - // for generating the region tag for samples; therefore only used in select Composers. - public abstract String defaultHost(); + // Only used for generating the region tag for samples; therefore only used in select Composers. + public abstract String apiShortName(); + + // Only used for generating the region tag for samples; therefore only used in select Composers. + public abstract String apiVersion(); public static GapicClass create(Kind kind, ClassDefinition classDefinition) { return builder().setKind(kind).setClassDefinition(classDefinition).build(); @@ -51,7 +53,8 @@ public static GapicClass create( static Builder builder() { return new AutoValue_GapicClass.Builder() .setSamples(Collections.emptyList()) - .setDefaultHost(""); + .setApiShortName("") + .setApiVersion(""); } abstract Builder toBuilder(); @@ -60,8 +63,12 @@ public final GapicClass withSamples(List samples) { return toBuilder().setSamples(samples).build(); } - public final GapicClass withDefaultHost(String defaultHost) { - return toBuilder().setDefaultHost(defaultHost).build(); + public final GapicClass withApiShortName(String apiShortName) { + return toBuilder().setApiShortName(apiShortName).build(); + } + + public final GapicClass withApiVersion(String apiVersion) { + return toBuilder().setApiVersion(apiVersion).build(); } @AutoValue.Builder @@ -72,7 +79,9 @@ abstract static class Builder { abstract Builder setSamples(List samples); - abstract Builder setDefaultHost(String defaultHost); + abstract Builder setApiShortName(String apiShortName); + + abstract Builder setApiVersion(String apiVersion); abstract GapicClass build(); } diff --git a/src/main/java/com/google/api/generator/gapic/model/Service.java b/src/main/java/com/google/api/generator/gapic/model/Service.java index 34d3f97b25..2e3a6be7df 100644 --- a/src/main/java/com/google/api/generator/gapic/model/Service.java +++ b/src/main/java/com/google/api/generator/gapic/model/Service.java @@ -16,8 +16,10 @@ import com.google.api.generator.engine.ast.TypeNode; import com.google.auto.value.AutoValue; +import com.google.common.base.Splitter; import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; import java.util.List; import javax.annotation.Nullable; @@ -50,6 +52,20 @@ public boolean hasDescription() { return !Strings.isNullOrEmpty(description()); } + public String apiShortName() { + if (!Strings.isNullOrEmpty(defaultHost())) { + return parseApiShortName(defaultHost()); + } + return ""; + } + + public String apiVersion() { + if (!Strings.isNullOrEmpty(protoPakkage())) { + return parseApiVersion(protoPakkage()); + } + return ""; + } + public Method operationPollingMethod() { for (Method method : methods()) { if (method.isOperationPollingMethod()) { @@ -127,4 +143,35 @@ public abstract static class Builder { public abstract Service build(); } + + private static String parseApiVersion(String protoPackage) { + // parse protoPackage for apiVersion + String[] pakkage = protoPackage.split("\\."); + String apiVersion; + // e.g. v1, v2, v1beta1 + if (pakkage[pakkage.length - 1].matches("v[0-9].*")) { + apiVersion = pakkage[pakkage.length - 1]; + } else { + apiVersion = ""; + } + return apiVersion; + } + + // Parse defaultHost for apiShortName for the RegionTag. Need to account for regional default + // endpoints like + // "us-east1-pubsub.googleapis.com". + private static String parseApiShortName(String defaultHost) { + // If the defaultHost is of the format "**.googleapis.com", take the name before the first + // period. + String apiShortName = Iterables.getFirst(Splitter.on(".").split(defaultHost), defaultHost); + // If the defaultHost is of the format "**-**-**.googleapis.com", take the section before the + // first period and after the last dash to follow CSharp's implementation here: + // https://github.com/googleapis/gapic-generator-csharp/blob/main/Google.Api.Generator/Generation/ServiceDetails.cs#L70 + apiShortName = Iterables.getLast(Splitter.on("-").split(apiShortName), defaultHost); + // `iam-meta-api` service is an exceptional case and is handled as a one-off + if (defaultHost.contains("iam-meta-api")) { + apiShortName = "iam"; + } + return apiShortName; + } } diff --git a/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java index caf070682c..d99325fbe8 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java @@ -42,14 +42,16 @@ public class ComposerTest { private final Service echoProtoService = context.services().get(0); private final List clazzes = Arrays.asList( - GrpcServiceCallableFactoryClassComposer.instance().generate(context, echoProtoService)); + GrpcServiceCallableFactoryClassComposer.instance() + .generate(context, echoProtoService) + .withApiShortName(echoProtoService.apiShortName()) + .withApiVersion(echoProtoService.apiVersion())); private final Sample sample = Sample.builder() .setRegionTag( RegionTag.builder().setServiceName("serviceName").setRpcName("rpcName").build()) .build(); private List ListofSamples = Arrays.asList(new Sample[] {sample}); - private final String protoPackage = echoProtoService.protoPakkage(); @Test public void gapicClass_addApacheLicense() { @@ -75,7 +77,7 @@ public void composeSamples_showcase() { List testClassList = Arrays.asList(new GapicClass[] {testClass}); List composedSamples = - Composer.prepareExecutableSamples(testClassList, protoPackage).get(0).samples(); + Composer.prepareExecutableSamples(testClassList).get(0).samples(); assertFalse(composedSamples.isEmpty()); for (Sample sample : composedSamples) { @@ -83,39 +85,14 @@ public void composeSamples_showcase() { "File header should be APACHE", Arrays.asList(CommentComposer.APACHE_LICENSE_COMMENT), sample.fileHeader()); - assertEquals("ApiShortName should be empty", "", sample.regionTag().apiShortName()); - assertEquals("ApiVersion should be V1beta1", "V1Beta1", sample.regionTag().apiVersion()); + assertEquals( + "ApiShortName should be Localhost7469", + "Localhost7469", + sample.regionTag().apiShortName()); + assertEquals("ApiVersion should be V1Beta1", "V1Beta1", sample.regionTag().apiVersion()); } } - @Test - public void parseDefaultHost_shouldReturnApiShortNameIfHostContainsRegionalEndpoint() { - String defaultHost = "us-east1-pubsub.googleapis.com"; - String apiShortName = Composer.parseDefaultHost(defaultHost); - assertEquals("pubsub", apiShortName); - } - - @Test - public void parseDefaultHost_shouldReturnApiShortName() { - String defaultHost = "logging.googleapis.com"; - String apiShortName = Composer.parseDefaultHost(defaultHost); - assertEquals("logging", apiShortName); - } - - @Test - public void parseDefaultHost_shouldReturnApiShortNameForIam() { - String defaultHost = "iam-meta-api.googleapis.com"; - String apiShortName = Composer.parseDefaultHost(defaultHost); - assertEquals("iam", apiShortName); - } - - @Test - public void parseDefaultHost_shouldReturnHostIfNoPeriods() { - String defaultHost = "logging:7469"; - String apiShortName = Composer.parseDefaultHost(defaultHost); - assertEquals("logging:7469", apiShortName); - } - @Test public void gapicClass_addRegionTagAndHeaderToSample() { Sample testSample; @@ -129,12 +106,12 @@ public void gapicClass_addRegionTagAndHeaderToSample() { public void composeSamples_parseProtoPackage() { String defaultHost = "accessapproval.googleapis.com:443"; - GapicClass testClass = clazzes.get(0).withSamples(ListofSamples).withDefaultHost(defaultHost); - List testClassList = Arrays.asList(new GapicClass[] {testClass}); String protoPack = "google.cloud.accessapproval.v1"; - + Service testService = + echoProtoService.toBuilder().setDefaultHost(defaultHost).setProtoPakkage(protoPack).build(); + List testClassList = getTestClassListFromService(testService); List composedSamples = - Composer.prepareExecutableSamples(testClassList, protoPack).get(0).samples(); + Composer.prepareExecutableSamples(testClassList).get(0).samples(); // If samples is empty, the test automatically passes without checking. assertFalse(composedSamples.isEmpty()); @@ -149,9 +126,10 @@ public void composeSamples_parseProtoPackage() { protoPack = "google.cloud.vision.v1p1beta1"; defaultHost = "vision.googleapis.com"; - testClass = clazzes.get(0).withSamples(ListofSamples).withDefaultHost(defaultHost); - testClassList = Arrays.asList(new GapicClass[] {testClass}); - composedSamples = Composer.prepareExecutableSamples(testClassList, protoPack).get(0).samples(); + testService = + testService.toBuilder().setDefaultHost(defaultHost).setProtoPakkage(protoPack).build(); + testClassList = getTestClassListFromService(testService); + composedSamples = Composer.prepareExecutableSamples(testClassList).get(0).samples(); // If samples is empty, the test automatically passes without checking. assertFalse(composedSamples.isEmpty()); @@ -161,7 +139,10 @@ public void composeSamples_parseProtoPackage() { } protoPack = "google.cloud.vision"; - composedSamples = Composer.prepareExecutableSamples(testClassList, protoPack).get(0).samples(); + testService = + testService.toBuilder().setDefaultHost(defaultHost).setProtoPakkage(protoPack).build(); + testClassList = getTestClassListFromService(testService); + composedSamples = Composer.prepareExecutableSamples(testClassList).get(0).samples(); // If samples is empty, the test automatically passes without checking. assertFalse(composedSamples.isEmpty()); @@ -170,4 +151,15 @@ public void composeSamples_parseProtoPackage() { assertEquals("ApiVersion should be empty", sample.regionTag().apiVersion(), ""); } } + + private List getTestClassListFromService(Service testService) { + GapicClass testClass = + GrpcServiceCallableFactoryClassComposer.instance() + .generate(context, testService) + .withSamples(ListofSamples) + .withApiShortName(testService.apiShortName()) + .withApiVersion(testService.apiVersion()); + List testClassList = Arrays.asList(new GapicClass[] {testClass}); + return testClassList; + } } diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java index 18450c1ca8..c169c88eb3 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceClientClassComposerTest.java @@ -29,26 +29,35 @@ public class ServiceClientClassComposerTest { public static Collection data() { return Arrays.asList( new Object[][] { - {"EchoClient", GrpcTestProtoLoader.instance().parseShowcaseEcho(), "localhost:7469"}, + { + "EchoClient", + GrpcTestProtoLoader.instance().parseShowcaseEcho(), + "localhost:7469", + "v1beta1" + }, { "DeprecatedServiceClient", GrpcTestProtoLoader.instance().parseDeprecatedService(), - "localhost:7469" + "localhost:7469", + "v1" }, { "IdentityClient", GrpcTestProtoLoader.instance().parseShowcaseIdentity(), - "localhost:7469" + "localhost:7469", + "v1beta1" }, { "BookshopClient", GrpcTestProtoLoader.instance().parseBookshopService(), - "localhost:2665" + "localhost:2665", + "v1beta1" }, { "MessagingClient", GrpcTestProtoLoader.instance().parseShowcaseMessaging(), - "localhost:7469" + "localhost:7469", + "v1beta1" }, }); } @@ -59,7 +68,10 @@ public static Collection data() { public GapicContext context; @Parameterized.Parameter(2) - public String defaultHostExpected; + public String apiShortNameExpected; + + @Parameterized.Parameter(3) + public String apiVersionExpected; @Test public void generateServiceClientClasses() { @@ -69,6 +81,7 @@ public void generateServiceClientClasses() { Assert.assertGoldenClass(this.getClass(), clazz, name + ".golden"); Assert.assertGoldenSamples( this.getClass(), name, clazz.classDefinition().packageString(), clazz.samples()); - Assert.assertCodeEquals(clazz.defaultHost(), defaultHostExpected); + Assert.assertCodeEquals(clazz.apiShortName(), apiShortNameExpected); + Assert.assertCodeEquals(clazz.apiVersion(), apiVersionExpected); } } diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java index 7f59ac58d0..1d245a7490 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceSettingsClassComposerTest.java @@ -31,11 +31,17 @@ public class ServiceSettingsClassComposerTest { public static Collection data() { return Arrays.asList( new Object[][] { - {"EchoSettings", TestProtoLoader.instance().parseShowcaseEcho(), "localhost:7469"}, + { + "EchoSettings", + TestProtoLoader.instance().parseShowcaseEcho(), + "localhost:7469", + "v1beta1" + }, { "DeprecatedServiceSettings", TestProtoLoader.instance().parseDeprecatedService(), - "localhost:7469" + "localhost:7469", + "v1" } }); } @@ -46,7 +52,10 @@ public static Collection data() { public GapicContext context; @Parameterized.Parameter(2) - public String defaultHostExpected; + public String apiShortNameExpected; + + @Parameterized.Parameter(3) + public String apiVersionExpected; @Test public void generateServiceSettingsClasses() { @@ -59,6 +68,7 @@ public void generateServiceSettingsClasses() { "servicesettings", clazz.classDefinition().packageString(), clazz.samples()); - Assert.assertCodeEquals(clazz.defaultHost(), defaultHostExpected); + Assert.assertCodeEquals(clazz.apiShortName(), apiShortNameExpected); + Assert.assertCodeEquals(clazz.apiVersion(), apiVersionExpected); } } diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java index 0da8ac4548..730136c083 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubClassComposerTest.java @@ -31,8 +31,8 @@ public class ServiceStubClassComposerTest { public static Collection data() { return Arrays.asList( new Object[][] { - {"EchoStub", TestProtoLoader.instance().parseShowcaseEcho(), ""}, - {"DeprecatedServiceStub", TestProtoLoader.instance().parseDeprecatedService(), ""} + {"EchoStub", TestProtoLoader.instance().parseShowcaseEcho(), "", ""}, + {"DeprecatedServiceStub", TestProtoLoader.instance().parseDeprecatedService(), "", ""} }); } @@ -42,7 +42,10 @@ public static Collection data() { public GapicContext context; @Parameterized.Parameter(2) - public String defaultHostExpected; + public String apiShortNameExpected; + + @Parameterized.Parameter(3) + public String apiVersionExpected; @Test public void generateServiceStubClasses() { @@ -51,6 +54,7 @@ public void generateServiceStubClasses() { Assert.assertGoldenClass(this.getClass(), clazz, name + ".golden"); Assert.assertEmptySamples(clazz.samples()); - Assert.assertCodeEquals(clazz.defaultHost(), defaultHostExpected); + Assert.assertCodeEquals(clazz.apiShortName(), apiShortNameExpected); + Assert.assertCodeEquals(clazz.apiVersion(), apiVersionExpected); } } diff --git a/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java index 019252dce3..747b19b8e0 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/grpc/ServiceStubSettingsClassComposerTest.java @@ -33,20 +33,26 @@ public static Collection data() { { "LoggingServiceV2StubSettings", GrpcTestProtoLoader.instance().parseLogging(), - "logging.googleapis.com:443" + "logging", + "v2" }, { "PublisherStubSettings", GrpcTestProtoLoader.instance().parsePubSubPublisher(), - "pubsub.googleapis.com:443" + "pubsub", + "v1" }, { - "EchoStubSettings", GrpcTestProtoLoader.instance().parseShowcaseEcho(), "localhost:7469" + "EchoStubSettings", + GrpcTestProtoLoader.instance().parseShowcaseEcho(), + "localhost:7469", + "v1beta1" }, { "DeprecatedServiceStubSettings", GrpcTestProtoLoader.instance().parseDeprecatedService(), - "localhost:7469" + "localhost:7469", + "v1" } }); } @@ -57,7 +63,10 @@ public static Collection data() { public GapicContext context; @Parameterized.Parameter(2) - public String defaultHostExpected; + public String apiShortNameExpected; + + @Parameterized.Parameter(3) + public String apiVersionExpected; @Test public void generateServiceStubSettingsClasses() { @@ -70,6 +79,7 @@ public void generateServiceStubSettingsClasses() { "servicesettings/stub", clazz.classDefinition().packageString(), clazz.samples()); - Assert.assertCodeEquals(clazz.defaultHost(), defaultHostExpected); + Assert.assertCodeEquals(clazz.apiShortName(), apiShortNameExpected); + Assert.assertCodeEquals(clazz.apiVersion(), apiVersionExpected); } } diff --git a/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java b/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java new file mode 100644 index 0000000000..c6f60b831f --- /dev/null +++ b/src/test/java/com/google/api/generator/gapic/model/ServiceTest.java @@ -0,0 +1,75 @@ +// Copyright 2022 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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.api.generator.gapic.model; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +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"); + + @Test + public void apiShortName_shouldReturnApiShortNameIfHostContainsRegionalEndpoint() { + String defaultHost = "us-east1-pubsub.googleapis.com"; + Service testService = testServiceBuilder.setDefaultHost(defaultHost).build(); + assertEquals("pubsub", testService.apiShortName()); + } + + @Test + public void apiShortName_shouldReturnApiShortName() { + String defaultHost = "logging.googleapis.com"; + Service testService = testServiceBuilder.setDefaultHost(defaultHost).build(); + assertEquals("logging", testService.apiShortName()); + } + + @Test + public void apiShortName_shouldReturnApiShortNameForIam() { + String defaultHost = "iam-meta-api.googleapis.com"; + Service testService = testServiceBuilder.setDefaultHost(defaultHost).build(); + assertEquals("iam", testService.apiShortName()); + } + + @Test + public void apiShortName_shouldReturnHostIfNoPeriods() { + String defaultHost = "logging:7469"; + Service testService = testServiceBuilder.setDefaultHost(defaultHost).build(); + assertEquals("logging:7469", testService.apiShortName()); + } + + @Test + public void apiVersion_shouldReturnVersionIfMatch() { + String protoPackage = "com.google.showcase.v1"; + Service testService = testServiceBuilder.setProtoPakkage(protoPackage).build(); + assertEquals("v1", testService.apiVersion()); + } + + @Test + public void apiVersion_shouldReturnEmptyIfNoMatch() { + String protoPackage = "com.google.showcase"; + Service testService = testServiceBuilder.setProtoPakkage(protoPackage).build(); + assertEquals("", testService.apiVersion()); + } +}