Skip to content

Commit

Permalink
[ggj][codegen] feat: add ServiceClientTest.methodExceptionTests (#346)
Browse files Browse the repository at this point in the history
* fix!: refactor field into MethodArgument, add enum/msg flags

* feat: partial isAssignableFrom VaporRef support, enable full-name type usage

* feat: support negative numeric literals

* fix: separate resname tokenizing from class composer

* feat: add per-type default value composer

* feat: add ServiceClientTest.methodExceptionTests codegen

* fix: CI merge conflict
  • Loading branch information
miraleung authored Sep 26, 2020
1 parent eebd4d3 commit d221f8b
Show file tree
Hide file tree
Showing 8 changed files with 351 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public class Composer {
public static List<GapicClass> composeServiceClasses(GapicContext context) {
List<GapicClass> clazzes = new ArrayList<>();
for (Service service : context.services()) {
clazzes.addAll(generateServiceClasses(service, context.serviceConfig(), context.messages()));
clazzes.addAll(
generateServiceClasses(
service, context.serviceConfig(), context.resourceNames(), context.messages()));
}
clazzes.addAll(generateResourceNameHelperClasses(context.helperResourceNames()));
return addApacheLicense(clazzes);
Expand All @@ -45,11 +47,12 @@ public static List<GapicClass> composeServiceClasses(GapicContext context) {
public static List<GapicClass> generateServiceClasses(
@Nonnull Service service,
@Nullable GapicServiceConfig serviceConfig,
@Nonnull Map<String, ResourceName> resourceNames,
@Nonnull Map<String, Message> messageTypes) {
List<GapicClass> clazzes = new ArrayList<>();
clazzes.addAll(generateStubClasses(service, serviceConfig, messageTypes));
clazzes.addAll(generateClientSettingsClasses(service, messageTypes));
clazzes.addAll(generateMocksAndTestClasses(service, messageTypes));
clazzes.addAll(generateMocksAndTestClasses(service, resourceNames, messageTypes));
// TODO(miraleung): Generate test classes.
return clazzes;
}
Expand Down Expand Up @@ -82,11 +85,12 @@ public static List<GapicClass> generateClientSettingsClasses(
}

public static List<GapicClass> generateMocksAndTestClasses(
Service service, Map<String, Message> messageTypes) {
Service service, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
List<GapicClass> clazzes = new ArrayList<>();
clazzes.add(MockServiceClassComposer.instance().generate(service, messageTypes));
clazzes.add(MockServiceImplClassComposer.instance().generate(service, messageTypes));
clazzes.add(ServiceClientTestClassComposer.instance().generate(service, messageTypes));
clazzes.add(
ServiceClientTestClassComposer.instance().generate(service, resourceNames, messageTypes));
return clazzes;
}

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ java_library(
"//src/main/java/com/google/api/generator:autovalue",
"//src/main/java/com/google/api/generator/engine/ast",
"//src/main/java/com/google/api/generator/gapic/utils",
"@com_google_api_api_common//jar",
"@com_google_auto_value_auto_value//jar",
"@com_google_auto_value_auto_value_annotations//jar",
"@com_google_code_findbugs_jsr305//jar",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.google.api.generator.gapic.model;

import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.Reference;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.VaporReference;
import com.google.api.generator.gapic.utils.JavaStyle;
Expand All @@ -27,6 +29,8 @@
@AutoValue
public abstract class ResourceName {
static final String SLASH = "/";
static final Reference RESOURCE_NAME_REF =
ConcreteReference.withClazz(com.google.api.resourcenames.ResourceName.class);

// The original binding variable name.
// This should be in lower_snake_case in the proto, and expected to be surrounded by braces.
Expand All @@ -42,6 +46,8 @@ public abstract class ResourceName {
public abstract String resourceTypeString();

// A list of patterns such as projects/{project}/locations/{location}/resources/{this_resource}.
// Order is copuled to the method variants and ordering in the reosurce name helper, as well as
// the relevant client tests.
public abstract ImmutableList<String> patterns();

// The Java TypeNode of the resource name helper class to generate.
Expand Down Expand Up @@ -83,12 +89,7 @@ public static ResourceName createWildcard(String resourceTypeString, String pakk
.setResourceTypeString(resourceTypeString)
.setPatterns(ImmutableList.of(ResourceNameConstants.WILDCARD_PATTERN))
.setIsOnlyWildcard(true)
.setType(
TypeNode.withReference(
VaporReference.builder()
.setName("ResourceName")
.setPakkage("com.google.api.resourcenames")
.build()))
.setType(TypeNode.withReference(RESOURCE_NAME_REF))
.build();
}

Expand Down Expand Up @@ -159,6 +160,7 @@ public ResourceName build() {
VaporReference.builder()
.setName(String.format("%sName", typeName))
.setPakkage(pakkage())
.setSupertypeReference(RESOURCE_NAME_REF)
.build()));
}
return autoBuild();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package(default_visibility = ["//visibility:public"])
TESTS = [
"BatchingDescriptorComposerTest",
"ComposerTest",
"DefaultValueComposerTest",
"GrpcServiceCallableFactoryClassComposerTest",
"GrpcServiceStubClassComposerTest",
"MockServiceClassComposerTest",
"MockServiceImplClassComposerTest",
"ResourceNameHelperClassComposerTest",
"ResourceNameTokenizerTest",
"RetrySettingsComposerTest",
"ServiceClientClassComposerTest",
"ServiceClientTestClassComposerTest",
Expand Down Expand Up @@ -52,6 +54,7 @@ java_proto_library(
"//src/main/java/com/google/api/generator/gapic/model",
"//src/main/java/com/google/api/generator/gapic/protoparser",
"//src/test/java/com/google/api/generator/gapic/testdata:showcase_java_proto",
"//src/test/java/com/google/api/generator/gapic/testdata:testgapic_java_proto",
"@com_google_api_gax_java//gax",
"@com_google_googleapis//google/logging/v2:logging_java_proto",
"@com_google_googleapis//google/pubsub/v1:pubsub_java_proto",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public void generateServiceClasses() {

Service echoProtoService = services.get(0);
GapicClass clazz =
ServiceClientTestClassComposer.instance().generate(echoProtoService, messageTypes);
ServiceClientTestClassComposer.instance()
.generate(echoProtoService, resourceNames, messageTypes);

JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Expand All @@ -68,14 +69,20 @@ public void generateServiceClasses() {
+ "import com.google.api.gax.grpc.testing.LocalChannelProvider;\n"
+ "import com.google.api.gax.grpc.testing.MockGrpcService;\n"
+ "import com.google.api.gax.grpc.testing.MockServiceHelper;\n"
+ "import com.google.api.gax.rpc.InvalidArgumentException;\n"
+ "import com.google.api.resourcenames.ResourceName;\n"
+ "import com.google.rpc.Status;\n"
+ "import io.grpc.StatusRuntimeException;\n"
+ "import java.io.IOException;\n"
+ "import java.util.Arrays;\n"
+ "import java.util.UUID;\n"
+ "import javax.annotation.Generated;\n"
+ "import org.junit.After;\n"
+ "import org.junit.AfterClass;\n"
+ "import org.junit.Assert;\n"
+ "import org.junit.Before;\n"
+ "import org.junit.BeforeClass;\n"
+ "import org.junit.Test;\n"
+ "\n"
+ "@Generated(\"by gapic-generator-java\")\n"
+ "public class EchoClientTest {\n"
Expand All @@ -89,7 +96,8 @@ public void generateServiceClasses() {
+ " mockEcho = new MockEcho();\n"
+ " mockServiceHelper =\n"
+ " new MockServiceHelper(\n"
+ " UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockEcho));\n"
+ " UUID.randomUUID().toString(),"
+ " Arrays.<MockGrpcService>asList(mockEcho));\n"
+ " mockServiceHelper.start();\n"
+ " }\n"
+ "\n"
Expand All @@ -114,5 +122,149 @@ public void generateServiceClasses() {
+ " public void tearDown() throws Exception {\n"
+ " client.close();\n"
+ " }\n"
+ "\n"
+ " @Test\n"
+ " public void echoExceptionTest() throws Exception {\n"
+ " StatusRuntimeException exception = new"
+ " StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);\n"
+ " mockEcho.addException(exception);\n"
+ " try {\n"
+ " String content = \"content951530617\";\n"
+ " client.echo(content);\n"
+ " Assert.fail(\"No exception raised\");\n"
+ " } catch (InvalidArgumentException e) {\n"
+ " // Expected exception.\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " @Test\n"
+ " public void echoExceptionTest2() throws Exception {\n"
+ " StatusRuntimeException exception = new"
+ " StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);\n"
+ " mockEcho.addException(exception);\n"
+ " try {\n"
+ " Status error = Status.newBuilder().build();\n"
+ " client.echo(error);\n"
+ " Assert.fail(\"No exception raised\");\n"
+ " } catch (InvalidArgumentException e) {\n"
+ " // Expected exception.\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " @Test\n"
+ " public void echoExceptionTest3() throws Exception {\n"
+ " StatusRuntimeException exception = new"
+ " StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);\n"
+ " mockEcho.addException(exception);\n"
+ " try {\n"
+ " String content = \"content951530617\";\n"
+ " Severity severity = Severity.forNumber(0);\n"
+ " client.echo(content, severity);\n"
+ " Assert.fail(\"No exception raised\");\n"
+ " } catch (InvalidArgumentException e) {\n"
+ " // Expected exception.\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " @Test\n"
+ " public void echoExceptionTest4() throws Exception {\n"
+ " StatusRuntimeException exception = new"
+ " StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);\n"
+ " mockEcho.addException(exception);\n"
+ " try {\n"
+ " String name = \"name3373707\";\n"
+ " client.echo(name);\n"
+ " Assert.fail(\"No exception raised\");\n"
+ " } catch (InvalidArgumentException e) {\n"
+ " // Expected exception.\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " @Test\n"
+ " public void echoExceptionTest5() throws Exception {\n"
+ " StatusRuntimeException exception = new"
+ " StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);\n"
+ " mockEcho.addException(exception);\n"
+ " try {\n"
+ " FoobarName name = FoobarName.ofProjectFoobarName(\"[PROJECT]\","
+ " \"[FOOBAR]\");\n"
+ " client.echo(name);\n"
+ " Assert.fail(\"No exception raised\");\n"
+ " } catch (InvalidArgumentException e) {\n"
+ " // Expected exception.\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " @Test\n"
+ " public void echoExceptionTest6() throws Exception {\n"
+ " StatusRuntimeException exception = new"
+ " StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);\n"
+ " mockEcho.addException(exception);\n"
+ " try {\n"
+ " String parent = \"parent-995424086\";\n"
+ " client.echo(parent);\n"
+ " Assert.fail(\"No exception raised\");\n"
+ " } catch (InvalidArgumentException e) {\n"
+ " // Expected exception.\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " @Test\n"
+ " public void echoExceptionTest7() throws Exception {\n"
+ " StatusRuntimeException exception = new"
+ " StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);\n"
+ " mockEcho.addException(exception);\n"
+ " try {\n"
+ " ResourceName parent = FoobarName.ofProjectFoobarName(\"[PROJECT]\","
+ " \"[FOOBAR]\");\n"
+ " client.echo(parent);\n"
+ " Assert.fail(\"No exception raised\");\n"
+ " } catch (InvalidArgumentException e) {\n"
+ " // Expected exception.\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " @Test\n"
+ " public void expandExceptionTest() throws Exception {\n"
+ " StatusRuntimeException exception = new"
+ " StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);\n"
+ " addException(exception);\n"
+ " try {\n"
+ " String content = \"content951530617\";\n"
+ " Status error = Status.newBuilder().build();\n"
+ " client.expand(content, error);\n"
+ " Assert.fail(\"No exception raised\");\n"
+ " } catch (InvalidArgumentException e) {\n"
+ " // Expected exception.\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " @Test\n"
+ " public void collectExceptionTest() throws Exception {\n"
+ " StatusRuntimeException exception = new"
+ " StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);\n"
+ " addException(exception);\n"
+ " try {\n"
+ " String content = \"content951530617\";\n"
+ " client.collect(content);\n"
+ " Assert.fail(\"No exception raised\");\n"
+ " } catch (InvalidArgumentException e) {\n"
+ " // Expected exception.\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " @Test\n"
+ " public void chatAgainExceptionTest() throws Exception {\n"
+ " StatusRuntimeException exception = new"
+ " StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);\n"
+ " addException(exception);\n"
+ " try {\n"
+ " String content = \"content951530617\";\n"
+ " client.chatAgain(content);\n"
+ " Assert.fail(\"No exception raised\");\n"
+ " } catch (InvalidArgumentException e) {\n"
+ " // Expected exception.\n"
+ " }\n"
+ " }\n"
+ "}\n";
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ filegroup(
"//src/main/java/com/google/api/generator/gapic/utils",
"//src/test/java/com/google/api/generator/gapic/testdata:showcase_java_proto",
"//src/test/java/com/google/api/generator/gapic/testdata:testgapic_java_proto",
"@com_google_api_api_common//jar",
"@com_google_googleapis//google/rpc:rpc_java_proto",
"@com_google_protobuf//:protobuf_java",
"@com_google_protobuf//:protobuf_java_util",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertThrows;

import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.VaporReference;
import com.google.api.generator.gapic.model.ResourceName;
import com.google.api.generator.gapic.utils.ResourceNameConstants;
import com.google.protobuf.Descriptors.Descriptor;
Expand Down Expand Up @@ -99,10 +99,7 @@ public void parseResourceNames_wildcard() {
assertTrue(resourceName.isOnlyWildcard());
assertEquals(
TypeNode.withReference(
VaporReference.builder()
.setName("ResourceName")
.setPakkage("com.google.api.resourcenames")
.build()),
ConcreteReference.withClazz(com.google.api.resourcenames.ResourceName.class)),
resourceName.type());
}

Expand Down

0 comments on commit d221f8b

Please sign in to comment.