From 6d97ce438bf04cc34db8554f6dbff38ce4291bcc Mon Sep 17 00:00:00 2001 From: Burke Davison Date: Thu, 10 Nov 2022 14:52:11 -0500 Subject: [PATCH 1/5] fix: Support testing nested argument method signatures and 'double' field assertions --- ...bstractServiceClientTestClassComposer.java | 52 +++++++------ .../grpc/ServiceClientTestClassComposer.java | 77 +++++++++++++------ .../rest/ServiceClientTestClassComposer.java | 2 + 3 files changed, 82 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java index 896607982c..875f5bdd2c 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java @@ -82,6 +82,7 @@ import org.junit.Test; public abstract class AbstractServiceClientTestClassComposer implements ClassComposer { + protected static final Statement EMPTY_LINE_STATEMENT = EmptyLineStatement.create(); protected static final String CLIENT_VAR_NAME = "client"; @@ -303,19 +304,23 @@ private List createTestMethods( /** * Creates a test method for a given RPC, e.g. createAssetTest. * - * @param method the RPC for which this test method is created. - * @param apiService the host service under test. - * @param rpcService the service that {@code method} belongs to. This is not equal to {@code - * apiService} only when {@code method} is a mixin, in which case {@code rpcService} is the - * mixed-in service. If {@code apiService} and {@code rpcService} are different, they will be - * used only for pagination. Otherwise, {@code rpcService} subsumes {@code apiService}. - * @param methodSignature the method signature of the RPC under test. - * @param variantIndex the nth variant of the RPC under test. This applies when we have - * polymorphism due to the presence of several method signature annotations in the proto. - * @param isRequestArg whether the RPC variant under test take only the request proto message. + * @param method the RPC for which this test method is created. + * @param apiService the host service under test. + * @param rpcService the service that {@code method} belongs to. This is not equal to + * {@code apiService} only when {@code method} is a mixin, in which + * case {@code rpcService} is the mixed-in service. If + * {@code apiService} and {@code rpcService} are different, they will + * be used only for pagination. Otherwise, {@code rpcService} subsumes + * {@code apiService}. + * @param methodSignature the method signature of the RPC under test. + * @param variantIndex the nth variant of the RPC under test. This applies when we have + * polymorphism due to the presence of several method signature + * annotations in the proto. + * @param isRequestArg whether the RPC variant under test take only the request proto + * message. * @param classMemberVarExprs the class members in the generated test class. - * @param resourceNames the resource names available for use. - * @param messageTypes the proto message types available for use. + * @param resourceNames the resource names available for use. + * @param messageTypes the proto message types available for use. */ private MethodDefinition createRpcTestMethod( Method method, @@ -419,7 +424,7 @@ private MethodDefinition createRpcTestMethod( if (method.hasLro() && (method.lro().operationServiceStubType() == null - || !method.lro().responseType().equals(method.outputType()))) { + || !method.lro().responseType().equals(method.outputType()))) { VariableExpr resultOperationVarExpr = VariableExpr.withVariable( @@ -689,13 +694,10 @@ private MethodDefinition createRpcTestMethod( methodExprs.clear(); methodStatements.add(EMPTY_LINE_STATEMENT); - methodStatements.addAll( - methodExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())); - methodExprs.clear(); - methodStatements.addAll( constructRpcTestCheckerLogic( method, + methodSignature, rpcService, isRequestArg, classMemberVarExprs, @@ -720,6 +722,7 @@ private MethodDefinition createRpcTestMethod( protected abstract List constructRpcTestCheckerLogic( Method method, + List methodSignature, Service service, boolean isRequestArg, Map classMemberVarExprs, @@ -737,14 +740,15 @@ protected abstract MethodDefinition createStreamingRpcTestMethod( /** * Creates a test method to exercise exceptions for a given RPC, e.g. createAssetTest. * - * @param method the RPC for which this test method is created. - * @param service the service that {@code method} belongs to. - * @param methodSignature the method signature of the RPC under test. - * @param variantIndex the nth variant of the RPC under test. This applies when we have - * polymorphism due to the presence of several method signature annotations in the proto. + * @param method the RPC for which this test method is created. + * @param service the service that {@code method} belongs to. + * @param methodSignature the method signature of the RPC under test. + * @param variantIndex the nth variant of the RPC under test. This applies when we have + * polymorphism due to the presence of several method signature + * annotations in the proto. * @param classMemberVarExprs the class members in the generated test class. - * @param resourceNames the resource names available for use. - * @param messageTypes the proto message types available for use. + * @param resourceNames the resource names available for use. + * @param messageTypes the proto message types available for use. */ protected abstract MethodDefinition createRpcExceptionTestMethod( Method method, diff --git a/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java index 36f45326af..c8d55785ef 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java @@ -30,6 +30,7 @@ import com.google.api.generator.engine.ast.InstanceofExpr; import com.google.api.generator.engine.ast.MethodDefinition; import com.google.api.generator.engine.ast.MethodInvocationExpr; +import com.google.api.generator.engine.ast.MethodInvocationExpr.Builder; import com.google.api.generator.engine.ast.NewObjectExpr; import com.google.api.generator.engine.ast.PrimitiveValue; import com.google.api.generator.engine.ast.ScopeNode; @@ -66,6 +67,7 @@ import java.util.stream.Collectors; public class ServiceClientTestClassComposer extends AbstractServiceClientTestClassComposer { + private static final String SERVICE_HELPER_VAR_NAME = "mockServiceHelper"; private static final String CHANNEL_PROVIDER_VAR_NAME = "channelProvider"; @@ -359,10 +361,11 @@ protected MethodDefinition createTearDownMethod( @Override protected List constructRpcTestCheckerLogic( Method method, + List methodSignature, Service service, boolean isRequestArg, Map classMemberVarExprs, - VariableExpr requestVarExpr, + VariableExpr requestVarExpr, // Nullable Message requestMessage, List argExprs) { List methodExprs = new ArrayList<>(); @@ -470,36 +473,50 @@ protected List constructRpcTestCheckerLogic( .build()); } } else { - for (VariableExpr argVarExpr : argExprs) { - Variable variable = argVarExpr.variable(); - String fieldGetterMethodNamePattern = "get%s"; - if (LIST_TYPE.isSupertypeOrEquals(variable.type())) { - fieldGetterMethodNamePattern = "get%sList"; - } else if (MAP_TYPE.isSupertypeOrEquals(variable.type())) { - fieldGetterMethodNamePattern = "get%sMap"; - } - Expr actualFieldExpr = - MethodInvocationExpr.builder() - .setExprReferenceExpr(actualRequestVarExpr) - .setMethodName( - String.format( - fieldGetterMethodNamePattern, - JavaStyle.toUpperCamelCase(variable.identifier().name()))) + for (MethodArgument arg : methodSignature) { + Expr root = actualRequestVarExpr; + if (!arg.nestedFields().isEmpty()) { + for (Field field : arg.nestedFields()) { + root = MethodInvocationExpr.builder() + .setMethodName("get" + JavaStyle.toUpperCamelCase(field.name())) + .setExprReferenceExpr(root) .build(); - Expr expectedFieldExpr = argVarExpr; - if (RESOURCE_NAME_TYPE.isSupertypeOrEquals(argVarExpr.type())) { + } + } + MethodInvocationExpr actual = MethodInvocationExpr.builder() + .setExprReferenceExpr(root) + .setMethodName( + String.format( + createGetterNamePattern(arg.field().type()), + JavaStyle.toUpperCamelCase(arg.field().name()))) + .build(); + + Variable var = Variable.builder() + .setName(JavaStyle.toLowerCamelCase(arg.name())) + .setType(arg.type()) + .build(); + Expr expectedFieldExpr = VariableExpr.withVariable(var); + if (RESOURCE_NAME_TYPE.isSupertypeOrEquals(var.type())) { expectedFieldExpr = MethodInvocationExpr.builder() - .setExprReferenceExpr(argVarExpr) + .setExprReferenceExpr(expectedFieldExpr) .setMethodName("toString") .build(); } - methodExprs.add( - MethodInvocationExpr.builder() - .setStaticReferenceType(FIXED_TYPESTORE.get("Assert")) - .setMethodName("assertEquals") - .setArguments(expectedFieldExpr, actualFieldExpr) - .build()); + + Builder assertionExpr = MethodInvocationExpr.builder() + .setStaticReferenceType(FIXED_TYPESTORE.get("Assert")) + .setMethodName("assertEquals"); + + ArrayList assertionArgs = new ArrayList<>(); + assertionArgs.add(expectedFieldExpr); + assertionArgs.add(actual); + if (arg.type() == TypeNode.DOUBLE) { + assertionArgs.add(ValueExpr.withValue( + PrimitiveValue.builder().setValue("0.01").setType(TypeNode.DOUBLE) + .build())); + } + methodExprs.add(assertionExpr.setArguments(assertionArgs).build()); } } @@ -533,6 +550,16 @@ protected List constructRpcTestCheckerLogic( return methodStatements; } + private static String createGetterNamePattern(TypeNode type) { + String fieldGetterMethodNamePattern = "get%s"; + if (LIST_TYPE.isSupertypeOrEquals(type)) { + fieldGetterMethodNamePattern = "get%sList"; + } else if (MAP_TYPE.isSupertypeOrEquals(type)) { + fieldGetterMethodNamePattern = "get%sMap"; + } + return fieldGetterMethodNamePattern; + } + @Override protected MethodDefinition createStreamingRpcTestMethod( Service service, diff --git a/src/main/java/com/google/api/generator/gapic/composer/rest/ServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/rest/ServiceClientTestClassComposer.java index bfc69954de..a628a4572c 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/rest/ServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/rest/ServiceClientTestClassComposer.java @@ -60,6 +60,7 @@ import java.util.stream.Collectors; public class ServiceClientTestClassComposer extends AbstractServiceClientTestClassComposer { + private static final String MOCK_SERVICE_VAR_NAME = "mockService"; private static final ServiceClientTestClassComposer INSTANCE = @@ -297,6 +298,7 @@ protected MethodDefinition createTearDownMethod( @Override protected List constructRpcTestCheckerLogic( Method method, + List methodSignature, Service service, boolean isRequestArg, Map classMemberVarExprs, From e1904ffb2c376950bda7e96abe11473fd632d9a0 Mon Sep 17 00:00:00 2001 From: Burke Davison Date: Mon, 14 Nov 2022 10:37:03 -0500 Subject: [PATCH 2/5] fix: DRY refactor for consistent double+float assertions --- ...bstractServiceClientTestClassComposer.java | 45 ++++----- .../grpc/ServiceClientTestClassComposer.java | 98 +++++++++---------- 2 files changed, 67 insertions(+), 76 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java index 875f5bdd2c..a58b9e752d 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java @@ -304,23 +304,19 @@ private List createTestMethods( /** * Creates a test method for a given RPC, e.g. createAssetTest. * - * @param method the RPC for which this test method is created. - * @param apiService the host service under test. - * @param rpcService the service that {@code method} belongs to. This is not equal to - * {@code apiService} only when {@code method} is a mixin, in which - * case {@code rpcService} is the mixed-in service. If - * {@code apiService} and {@code rpcService} are different, they will - * be used only for pagination. Otherwise, {@code rpcService} subsumes - * {@code apiService}. - * @param methodSignature the method signature of the RPC under test. - * @param variantIndex the nth variant of the RPC under test. This applies when we have - * polymorphism due to the presence of several method signature - * annotations in the proto. - * @param isRequestArg whether the RPC variant under test take only the request proto - * message. + * @param method the RPC for which this test method is created. + * @param apiService the host service under test. + * @param rpcService the service that {@code method} belongs to. This is not equal to {@code + * apiService} only when {@code method} is a mixin, in which case {@code rpcService} is the + * mixed-in service. If {@code apiService} and {@code rpcService} are different, they will be + * used only for pagination. Otherwise, {@code rpcService} subsumes {@code apiService}. + * @param methodSignature the method signature of the RPC under test. + * @param variantIndex the nth variant of the RPC under test. This applies when we have + * polymorphism due to the presence of several method signature annotations in the proto. + * @param isRequestArg whether the RPC variant under test take only the request proto message. * @param classMemberVarExprs the class members in the generated test class. - * @param resourceNames the resource names available for use. - * @param messageTypes the proto message types available for use. + * @param resourceNames the resource names available for use. + * @param messageTypes the proto message types available for use. */ private MethodDefinition createRpcTestMethod( Method method, @@ -424,7 +420,7 @@ private MethodDefinition createRpcTestMethod( if (method.hasLro() && (method.lro().operationServiceStubType() == null - || !method.lro().responseType().equals(method.outputType()))) { + || !method.lro().responseType().equals(method.outputType()))) { VariableExpr resultOperationVarExpr = VariableExpr.withVariable( @@ -740,15 +736,14 @@ protected abstract MethodDefinition createStreamingRpcTestMethod( /** * Creates a test method to exercise exceptions for a given RPC, e.g. createAssetTest. * - * @param method the RPC for which this test method is created. - * @param service the service that {@code method} belongs to. - * @param methodSignature the method signature of the RPC under test. - * @param variantIndex the nth variant of the RPC under test. This applies when we have - * polymorphism due to the presence of several method signature - * annotations in the proto. + * @param method the RPC for which this test method is created. + * @param service the service that {@code method} belongs to. + * @param methodSignature the method signature of the RPC under test. + * @param variantIndex the nth variant of the RPC under test. This applies when we have + * polymorphism due to the presence of several method signature annotations in the proto. * @param classMemberVarExprs the class members in the generated test class. - * @param resourceNames the resource names available for use. - * @param messageTypes the proto message types available for use. + * @param resourceNames the resource names available for use. + * @param messageTypes the proto message types available for use. */ protected abstract MethodDefinition createRpcExceptionTestMethod( Method method, diff --git a/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java index c8d55785ef..843fdb5fe1 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java @@ -30,7 +30,6 @@ import com.google.api.generator.engine.ast.InstanceofExpr; import com.google.api.generator.engine.ast.MethodDefinition; import com.google.api.generator.engine.ast.MethodInvocationExpr; -import com.google.api.generator.engine.ast.MethodInvocationExpr.Builder; import com.google.api.generator.engine.ast.NewObjectExpr; import com.google.api.generator.engine.ast.PrimitiveValue; import com.google.api.generator.engine.ast.ScopeNode; @@ -451,52 +450,37 @@ protected List constructRpcTestCheckerLogic( String.format( fieldGetterMethodNamePattern, JavaStyle.toUpperCamelCase(field.name()))) .build(); + Expr expectedFieldExpr = checkExprFn.apply(requestVarExpr); Expr actualFieldExpr = checkExprFn.apply(actualRequestVarExpr); - List assertEqualsArguments = new ArrayList<>(); - assertEqualsArguments.add(expectedFieldExpr); - assertEqualsArguments.add(actualFieldExpr); - if (TypeNode.isFloatingPointType(field.type())) { - boolean isFloat = field.type().equals(TypeNode.FLOAT); - assertEqualsArguments.add( - ValueExpr.withValue( - PrimitiveValue.builder() - .setType(isFloat ? TypeNode.FLOAT : TypeNode.DOUBLE) - .setValue(String.format("0.0001%s", isFloat ? "f" : "")) - .build())); - } - methodExprs.add( - MethodInvocationExpr.builder() - .setStaticReferenceType(FIXED_TYPESTORE.get("Assert")) - .setMethodName("assertEquals") - .setArguments(assertEqualsArguments) - .build()); + methodExprs.add(createAssertEquals(expectedFieldExpr, actualFieldExpr, field.type())); } } else { for (MethodArgument arg : methodSignature) { Expr root = actualRequestVarExpr; - if (!arg.nestedFields().isEmpty()) { - for (Field field : arg.nestedFields()) { - root = MethodInvocationExpr.builder() - .setMethodName("get" + JavaStyle.toUpperCamelCase(field.name())) + for (Field field : arg.nestedFields()) { + root = + MethodInvocationExpr.builder() + .setMethodName("get" + JavaStyle.toUpperCamelCase(field.name())) + .setExprReferenceExpr(root) + .build(); + } + MethodInvocationExpr actual = + MethodInvocationExpr.builder() .setExprReferenceExpr(root) + .setMethodName( + String.format( + createGetterNamePattern(arg.field().type()), + JavaStyle.toUpperCamelCase(arg.field().name()))) .build(); - } - } - MethodInvocationExpr actual = MethodInvocationExpr.builder() - .setExprReferenceExpr(root) - .setMethodName( - String.format( - createGetterNamePattern(arg.field().type()), - JavaStyle.toUpperCamelCase(arg.field().name()))) - .build(); - Variable var = Variable.builder() - .setName(JavaStyle.toLowerCamelCase(arg.name())) - .setType(arg.type()) - .build(); - Expr expectedFieldExpr = VariableExpr.withVariable(var); - if (RESOURCE_NAME_TYPE.isSupertypeOrEquals(var.type())) { + Expr expectedFieldExpr = + VariableExpr.withVariable( + Variable.builder() + .setName(JavaStyle.toLowerCamelCase(arg.name())) + .setType(arg.type()) + .build()); + if (RESOURCE_NAME_TYPE.isSupertypeOrEquals(arg.type())) { expectedFieldExpr = MethodInvocationExpr.builder() .setExprReferenceExpr(expectedFieldExpr) @@ -504,19 +488,7 @@ protected List constructRpcTestCheckerLogic( .build(); } - Builder assertionExpr = MethodInvocationExpr.builder() - .setStaticReferenceType(FIXED_TYPESTORE.get("Assert")) - .setMethodName("assertEquals"); - - ArrayList assertionArgs = new ArrayList<>(); - assertionArgs.add(expectedFieldExpr); - assertionArgs.add(actual); - if (arg.type() == TypeNode.DOUBLE) { - assertionArgs.add(ValueExpr.withValue( - PrimitiveValue.builder().setValue("0.01").setType(TypeNode.DOUBLE) - .build())); - } - methodExprs.add(assertionExpr.setArguments(assertionArgs).build()); + methodExprs.add(createAssertEquals(expectedFieldExpr, actual, arg.type())); } } @@ -550,6 +522,30 @@ protected List constructRpcTestCheckerLogic( return methodStatements; } + private static MethodInvocationExpr createAssertEquals( + Expr expected, Expr actual, TypeNode type) { + + ArrayList assertionArgs = new ArrayList<>(); + assertionArgs.add(expected); + assertionArgs.add(actual); + + if (TypeNode.isFloatingPointType(type)) { + boolean isFloat = type.equals(TypeNode.FLOAT); + assertionArgs.add( + ValueExpr.withValue( + PrimitiveValue.builder() + .setType(isFloat ? TypeNode.FLOAT : TypeNode.DOUBLE) + .setValue(String.format("0.0001%s", isFloat ? "f" : "")) + .build())); + } + + return MethodInvocationExpr.builder() + .setStaticReferenceType(FIXED_TYPESTORE.get("Assert")) + .setMethodName("assertEquals") + .setArguments(assertionArgs) + .build(); + } + private static String createGetterNamePattern(TypeNode type) { String fieldGetterMethodNamePattern = "get%s"; if (LIST_TYPE.isSupertypeOrEquals(type)) { From 8321a6829916af171416fe89c3d0bab82ac22121 Mon Sep 17 00:00:00 2001 From: Burke Davison Date: Thu, 10 Nov 2022 15:15:41 -0500 Subject: [PATCH 3/5] fix: Remove unused parameter. --- .../common/AbstractServiceClientTestClassComposer.java | 6 ++---- .../gapic/composer/grpc/ServiceClientTestClassComposer.java | 3 +-- .../gapic/composer/rest/ServiceClientTestClassComposer.java | 3 +-- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java index a58b9e752d..fd6686212f 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/common/AbstractServiceClientTestClassComposer.java @@ -698,8 +698,7 @@ private MethodDefinition createRpcTestMethod( isRequestArg, classMemberVarExprs, requestVarExpr, - requestMessage, - argExprs)); + requestMessage)); String testMethodName = String.format( @@ -723,8 +722,7 @@ protected abstract List constructRpcTestCheckerLogic( boolean isRequestArg, Map classMemberVarExprs, VariableExpr requestVarExpr, - Message requestMessage, - List argExprs); + Message requestMessage); protected abstract MethodDefinition createStreamingRpcTestMethod( Service service, diff --git a/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java index 843fdb5fe1..c9387b94cd 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java @@ -365,8 +365,7 @@ protected List constructRpcTestCheckerLogic( boolean isRequestArg, Map classMemberVarExprs, VariableExpr requestVarExpr, // Nullable - Message requestMessage, - List argExprs) { + Message requestMessage) { List methodExprs = new ArrayList<>(); List methodStatements = new ArrayList<>(); diff --git a/src/main/java/com/google/api/generator/gapic/composer/rest/ServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/rest/ServiceClientTestClassComposer.java index a628a4572c..d9fc2843cd 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/rest/ServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/rest/ServiceClientTestClassComposer.java @@ -303,8 +303,7 @@ protected List constructRpcTestCheckerLogic( boolean isRequestArg, Map classMemberVarExprs, VariableExpr requestVarExpr, - Message requestMessage, - List argExprs) { + Message requestMessage) { VariableExpr actualRequestsVarExpr = VariableExpr.withVariable( From dd8ad114178c2a723ce5ecebb66ebb7602ae1b3a Mon Sep 17 00:00:00 2001 From: Burke Davison Date: Mon, 14 Nov 2022 11:14:22 -0500 Subject: [PATCH 4/5] fix: Simplify and DRY getter creation --- .../grpc/ServiceClientTestClassComposer.java | 43 ++++++------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java index c9387b94cd..4af282269e 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java @@ -436,42 +436,17 @@ protected List constructRpcTestCheckerLogic( Preconditions.checkNotNull(requestVarExpr); Preconditions.checkNotNull(requestMessage); for (Field field : requestMessage.fields()) { - String fieldGetterMethodNamePatternTemp = "get%s"; - if (field.isRepeated()) { - fieldGetterMethodNamePatternTemp = field.isMap() ? "get%sMap" : "get%sList"; - } - final String fieldGetterMethodNamePattern = fieldGetterMethodNamePatternTemp; - Function checkExprFn = - v -> - MethodInvocationExpr.builder() - .setExprReferenceExpr(v) - .setMethodName( - String.format( - fieldGetterMethodNamePattern, JavaStyle.toUpperCamelCase(field.name()))) - .build(); - - Expr expectedFieldExpr = checkExprFn.apply(requestVarExpr); - Expr actualFieldExpr = checkExprFn.apply(actualRequestVarExpr); + Expr expectedFieldExpr = createGetter(requestVarExpr, field); + Expr actualFieldExpr = createGetter(actualRequestVarExpr, field); methodExprs.add(createAssertEquals(expectedFieldExpr, actualFieldExpr, field.type())); } } else { for (MethodArgument arg : methodSignature) { Expr root = actualRequestVarExpr; for (Field field : arg.nestedFields()) { - root = - MethodInvocationExpr.builder() - .setMethodName("get" + JavaStyle.toUpperCamelCase(field.name())) - .setExprReferenceExpr(root) - .build(); + root = createGetter(root, field); } - MethodInvocationExpr actual = - MethodInvocationExpr.builder() - .setExprReferenceExpr(root) - .setMethodName( - String.format( - createGetterNamePattern(arg.field().type()), - JavaStyle.toUpperCamelCase(arg.field().name()))) - .build(); + MethodInvocationExpr actual = createGetter(root, arg.field()); Expr expectedFieldExpr = VariableExpr.withVariable( @@ -545,6 +520,16 @@ private static MethodInvocationExpr createAssertEquals( .build(); } + private static MethodInvocationExpr createGetter(Expr exprReference, Field field) { + return MethodInvocationExpr.builder() + .setExprReferenceExpr(exprReference) + .setMethodName( + String.format( + createGetterNamePattern(field.type()), + JavaStyle.toUpperCamelCase(field.name()))) + .build(); + } + private static String createGetterNamePattern(TypeNode type) { String fieldGetterMethodNamePattern = "get%s"; if (LIST_TYPE.isSupertypeOrEquals(type)) { From 982222edf7a1a8d6ee7a9dcef31f6d887dc61acf Mon Sep 17 00:00:00 2001 From: Burke Davison Date: Mon, 14 Nov 2022 11:17:18 -0500 Subject: [PATCH 5/5] fix: format --- .../gapic/composer/grpc/ServiceClientTestClassComposer.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java index 4af282269e..b43d4a4fc2 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/grpc/ServiceClientTestClassComposer.java @@ -525,8 +525,7 @@ private static MethodInvocationExpr createGetter(Expr exprReference, Field field .setExprReferenceExpr(exprReference) .setMethodName( String.format( - createGetterNamePattern(field.type()), - JavaStyle.toUpperCamelCase(field.name()))) + createGetterNamePattern(field.type()), JavaStyle.toUpperCamelCase(field.name()))) .build(); }