Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ggj][codegen][test] fix: refactor testgen, use paged resource name, add pubsub client test #406

Merged
merged 15 commits into from
Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: change getter too
  • Loading branch information
miraleung committed Oct 22, 2020
commit c304cc10369a8fbbd4da06ea394de4eb08ea73a1
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ public class ServiceClientTestClassComposer {
private static final String GRPC_TESTING_PACKAGE = "com.google.api.gax.grpc.testing";
private static final String MOCK_SERVICE_CLASS_NAME_PATTERN = "Mock%s";
private static final String MOCK_SERVICE_VAR_NAME_PATTERN = "mock%s";
private static final String PAGED_RESPONSE_TYPE_NAME_PATTERN = "%sPagedResponse";
private static final String SERVICE_CLIENT_CLASS_NAME_PATTERN = "%sClient";
private static final String SERVICE_HELPER_VAR_NAME = "mockServiceHelper";
private static final String SERVICE_SETTINGS_CLASS_NAME_PATTERN = "%sSettings";
private static final String STUB_SETTINGS_PATTERN = "%sSettings";
private static final String PAGED_RESPONSE_TYPE_NAME_PATTERN = "%sPagedResponse";

private static final ServiceClientTestClassComposer INSTANCE =
new ServiceClientTestClassComposer();
Expand Down Expand Up @@ -424,18 +424,18 @@ private static List<MethodDefinition> createTestMethods(
javaMethods.add(
createRpcTestMethod(
method,
service,
Collections.emptyList(),
0,
service.name(),
classMemberVarExprs,
resourceNames,
messageTypes));
javaMethods.add(
createRpcExceptionTestMethod(
method,
service,
Collections.emptyList(),
0,
service.name(),
classMemberVarExprs,
resourceNames,
messageTypes));
Expand All @@ -444,18 +444,18 @@ private static List<MethodDefinition> createTestMethods(
javaMethods.add(
createRpcTestMethod(
method,
service,
method.methodSignatures().get(i),
i,
service.name(),
classMemberVarExprs,
resourceNames,
messageTypes));
javaMethods.add(
createRpcExceptionTestMethod(
method,
service,
method.methodSignatures().get(i),
i,
service.name(),
classMemberVarExprs,
resourceNames,
messageTypes));
Expand All @@ -467,12 +467,14 @@ private static List<MethodDefinition> createTestMethods(

private static MethodDefinition createRpcTestMethod(
Method method,
Service service,
List<MethodArgument> methodSignature,
int variantIndex,
String serviceName,
Map<String, VariableExpr> classMemberVarExprs,
Map<String, ResourceName> resourceNames,
Map<String, Message> messageTypes) {
String serviceName = service.name();

if (!method.stream().equals(Method.Stream.NONE)) {
return createStreamingRpcTestMethod(
method, serviceName, classMemberVarExprs, resourceNames, messageTypes);
Expand Down Expand Up @@ -627,7 +629,8 @@ private static MethodDefinition createRpcTestMethod(
VariableExpr actualResponseVarExpr =
VariableExpr.withVariable(
Variable.builder()
.setType(methodOutputType)
.setType(
method.isPaged() ? getPagedResponseType(service, method) : methodOutputType)
.setName(method.isPaged() ? "pagedListResponse" : "actualResponse")
.build());
Expr rpcJavaMethodInvocationExpr =
Expand Down Expand Up @@ -707,12 +710,21 @@ private static MethodDefinition createRpcTestMethod(
.build());

// Assert the responses are equivalent.
Message methodOutputMessage = messageTypes.get(method.outputType().reference().name());
Field firstRepeatedField = methodOutputMessage.findAndUnwrapFirstRepeatedField();
Preconditions.checkNotNull(
firstRepeatedField,
String.format(
"Expected paged RPC %s to have a repeated field in the response %s but found none",
method.name(), methodOutputMessage.name()));

Expr zeroExpr =
ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("0").build());
Expr expectedPagedResponseExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(expectedResponseVarExpr)
.setMethodName("getResponsesList")
.setMethodName(
String.format("get%sList", JavaStyle.toUpperCamelCase(firstRepeatedField.name())))
.build();
expectedPagedResponseExpr =
MethodInvocationExpr.builder()
Expand Down Expand Up @@ -1168,12 +1180,14 @@ private static MethodDefinition createStreamingRpcTestMethod(

private static MethodDefinition createRpcExceptionTestMethod(
Method method,
Service service,
List<MethodArgument> methodSignature,
int variantIndex,
String serviceName,
Map<String, VariableExpr> classMemberVarExprs,
Map<String, ResourceName> resourceNames,
Map<String, Message> messageTypes) {
String serviceName = service.name();

VariableExpr exceptionVarExpr =
VariableExpr.withVariable(
Variable.builder()
Expand Down Expand Up @@ -1841,6 +1855,16 @@ private static TypeNode getCallableType(Method protoMethod) {
ConcreteReference.builder().setClazz(callableClazz).setGenerics(generics).build());
}

private static TypeNode getPagedResponseType(Service service, Method method) {
return TypeNode.withReference(
VaporReference.builder()
.setName(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, method.name()))
.setPakkage(service.pakkage())
.setEnclosingClassName(getClientClassName(service.name()))
.setIsStaticImport(true)
.build());
}

private static String getCallableMethodName(Method protoMethod) {
Preconditions.checkState(
!protoMethod.stream().equals(Method.Stream.NONE),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.google.showcase.v1beta1;

import static com.google.showcase.v1beta1.EchoClient.PagedExpandPagedResponse;

import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.GaxGrpcProperties;
import com.google.api.gax.grpc.testing.LocalChannelProvider;
Expand Down Expand Up @@ -550,7 +552,7 @@ public class EchoClientTest {
.setPageToken("page_token1630607433")
.build();

PagedExpandResponse pagedListResponse = client.pagedExpand(request);
PagedExpandPagedResponse pagedListResponse = client.pagedExpand(request);

List<EchoResponse> resources = Lists.newArrayList(pagedListResponse.iterateAll());

Expand Down