Skip to content

Commit

Permalink
[ggj][codegen][test] fix: refactor testgen, use paged resource name, …
Browse files Browse the repository at this point in the history
…add pubsub client test (#406)

* fix: refactor requestBuilder into separate method in ServiceClientClassComposer

* feat: add varargs to AnonClass and ref setter methods

* feat: add HTTP annotation parsing/validation

* feat: Generate RequestParamsExtractor in GrpcServiceStub

* feat: add GrpcPublisherStub test to exercise HTTP subfields

* fix: add ByteString to DefaultValueComposer

* fix: Use repeated field name for paged RPC unit tests

* fix:  change getter too

* fix: refactor exception field, use paged repeated field name, add pubsub client test
  • Loading branch information
miraleung authored Oct 24, 2020
1 parent 3cf3ba3 commit a6ef2ef
Show file tree
Hide file tree
Showing 3 changed files with 1,549 additions and 17 deletions.
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

0 comments on commit a6ef2ef

Please sign in to comment.