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 all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ private static MethodDefinition createRpcTestMethod(
VariableExpr.withVariable(
Variable.builder()
.setType(
method.isPaged() ? getPagedResponseType(service, method) : methodOutputType)
method.isPaged() ? getPagedResponseType(method, service) : methodOutputType)
.setName(method.isPaged() ? "pagedListResponse" : "actualResponse")
.build());
Expr rpcJavaMethodInvocationExpr =
Expand Down Expand Up @@ -711,11 +711,11 @@ private static MethodDefinition createRpcTestMethod(

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

Expr zeroExpr =
Expand All @@ -724,7 +724,8 @@ private static MethodDefinition createRpcTestMethod(
MethodInvocationExpr.builder()
.setExprReferenceExpr(expectedResponseVarExpr)
.setMethodName(
String.format("get%sList", JavaStyle.toUpperCamelCase(firstRepeatedField.name())))
String.format(
"get%sList", JavaStyle.toUpperCamelCase(repeatedPagedResultsField.name())))
.build();
expectedPagedResponseExpr =
MethodInvocationExpr.builder()
Expand Down Expand Up @@ -1855,7 +1856,7 @@ private static TypeNode getCallableType(Method protoMethod) {
ConcreteReference.builder().setClazz(callableClazz).setGenerics(generics).build());
}

private static TypeNode getPagedResponseType(Service service, Method method) {
private static TypeNode getPagedResponseType(Method method, Service service) {
return TypeNode.withReference(
VaporReference.builder()
.setName(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, method.name()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,25 @@
import com.google.api.generator.test.framework.Utils;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Descriptors.ServiceDescriptor;
import com.google.pubsub.v1.PubsubProto;
import com.google.showcase.v1beta1.EchoOuterClass;
import google.cloud.CommonResources;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;

public class ServiceClientTestClassComposerTest {
private ServiceDescriptor echoService;
private FileDescriptor echoFileDescriptor;

@Before
public void setUp() {
echoFileDescriptor = EchoOuterClass.getDescriptor();
echoService = echoFileDescriptor.getServices().get(0);
@Test
public void generateClientTest_echoClient() {
FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
ServiceDescriptor echoService = echoFileDescriptor.getServices().get(0);
assertEquals(echoService.getName(), "Echo");
}

@Test
public void generateServiceClasses() {
Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
Expand All @@ -67,4 +63,36 @@ public void generateServiceClasses() {
Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "EchoClientTest.golden");
assertCodeEquals(goldenFilePath, visitor.write());
}

@Test
public void generateClientTest_pubSubPublisherClient() {
FileDescriptor serviceFileDescriptor = PubsubProto.getDescriptor();
FileDescriptor commonResourcesFileDescriptor = CommonResources.getDescriptor();
ServiceDescriptor serviceDescriptor = serviceFileDescriptor.getServices().get(0);
assertEquals("Publisher", serviceDescriptor.getName());

Map<String, ResourceName> resourceNames = new HashMap<>();
resourceNames.putAll(Parser.parseResourceNames(serviceFileDescriptor));
resourceNames.putAll(Parser.parseResourceNames(commonResourcesFileDescriptor));

Map<String, Message> messageTypes = Parser.parseMessages(serviceFileDescriptor);

Set<ResourceName> outputResourceNames = new HashSet<>();
List<Service> services =
Parser.parseService(
serviceFileDescriptor, messageTypes, resourceNames, outputResourceNames);

Service subscriptionService = services.get(1);
assertEquals("Subscriber", subscriptionService.name());
GapicClass clazz =
ServiceClientTestClassComposer.instance()
.generate(subscriptionService, resourceNames, messageTypes);

JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "SubscriberClientTest.golden", visitor.write());
Path goldenFilePath =
Paths.get(ComposerConstants.GOLDENFILES_DIRECTORY, "SubscriberClientTest.golden");
assertCodeEquals(goldenFilePath, visitor.write());
}
}
Loading