use of com.google.api.generator.gapic.model.ResourceName in project gapic-generator-java by googleapis.
the class ServiceClientCallableMethodSampleComposerTest method valid_composeRegularCallableMethod_pageRpc.
@Test
public void valid_composeRegularCallableMethod_pageRpc() {
Descriptors.FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
TypeNode clientType = TypeNode.withReference(VaporReference.builder().setName("EchoClient").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode inputType = TypeNode.withReference(VaporReference.builder().setName("PagedExpandRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("PagedExpandResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
Method method = Method.builder().setName("PagedExpand").setInputType(inputType).setOutputType(outputType).setMethodSignatures(Collections.emptyList()).setPageSizeFieldName(PAGINATED_FIELD_NAME).build();
String results = writeStatements(ServiceClientCallableMethodSampleComposer.composeRegularCallableMethod(method, clientType, resourceNames, messageTypes));
String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", " PagedExpandRequest request =\n", " PagedExpandRequest.newBuilder()\n", " .setContent(\"content951530617\")\n", " .setPageSize(883849137)\n", " .setPageToken(\"pageToken873572522\")\n", " .build();\n", " while (true) {\n", " PagedExpandResponse response = echoClient.pagedExpandCallable().call(request);\n", " for (EchoResponse element : response.getResponsesList()) {\n", " // doThingsWith(element);\n", " }\n", " String nextPageToken = response.getNextPageToken();\n", " if (!Strings.isNullOrEmpty(nextPageToken)) {\n", " request = request.toBuilder().setPageToken(nextPageToken).build();\n", " } else {\n", " break;\n", " }\n", " }\n", "}");
Assert.assertEquals(results, expected);
}
use of com.google.api.generator.gapic.model.ResourceName in project gapic-generator-java by googleapis.
the class ServiceClientCallableMethodSampleComposerTest method valid_composeStreamCallableMethod_bidiStream.
@Test
public void valid_composeStreamCallableMethod_bidiStream() {
Descriptors.FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
TypeNode clientType = TypeNode.withReference(VaporReference.builder().setName("EchoClient").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode inputType = TypeNode.withReference(VaporReference.builder().setName("EchoRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("EchoResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
Method method = Method.builder().setName("chat").setInputType(inputType).setOutputType(outputType).setStream(Method.Stream.BIDI).build();
String results = writeStatements(ServiceClientCallableMethodSampleComposer.composeStreamCallableMethod(method, clientType, resourceNames, messageTypes));
String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", " BidiStream<EchoRequest, EchoResponse> bidiStream =" + " echoClient.chatCallable().call();\n", " EchoRequest request =\n", " EchoRequest.newBuilder()\n", " .setName(FoobarName.ofProjectFoobarName(\"[PROJECT]\"," + " \"[FOOBAR]\").toString())\n", " .setParent(FoobarName.ofProjectFoobarName(\"[PROJECT]\"," + " \"[FOOBAR]\").toString())\n", " .setSeverity(Severity.forNumber(0))\n", " .setFoobar(Foobar.newBuilder().build())\n", " .build();\n", " bidiStream.send(request);\n", " for (EchoResponse response : bidiStream) {\n", " // Do something when a response is received.\n", " }\n", "}");
Assert.assertEquals(results, expected);
}
use of com.google.api.generator.gapic.model.ResourceName in project gapic-generator-java by googleapis.
the class ServiceClientCallableMethodSampleComposerTest method invalid_composeStreamCallableMethod_clientStreamNotExistRequest.
@Test
public void invalid_composeStreamCallableMethod_clientStreamNotExistRequest() {
Descriptors.FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
TypeNode clientType = TypeNode.withReference(VaporReference.builder().setName("EchoClient").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode inputType = TypeNode.withReference(VaporReference.builder().setName("NotExistRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("EchoResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
Method method = Method.builder().setName("Collect").setInputType(inputType).setOutputType(outputType).setStream(Method.Stream.CLIENT).build();
Assert.assertThrows(NullPointerException.class, () -> ServiceClientCallableMethodSampleComposer.composeStreamCallableMethod(method, clientType, resourceNames, messageTypes));
}
use of com.google.api.generator.gapic.model.ResourceName in project gapic-generator-java by googleapis.
the class ServiceClientHeaderSampleComposerTest method valid_composeShowcaseMethodSample_lroRpcWithReturnVoid.
@Test
public void valid_composeShowcaseMethodSample_lroRpcWithReturnVoid() {
Descriptors.FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
TypeNode clientType = TypeNode.withReference(VaporReference.builder().setName("EchoClient").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode inputType = TypeNode.withReference(VaporReference.builder().setName("WaitRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("Operation").setPakkage(LRO_PACKAGE_NAME).build());
TypeNode responseType = TypeNode.withReference(VaporReference.builder().setName("Empty").setPakkage(PROTO_PACKAGE_NAME).build());
TypeNode metadataType = TypeNode.withReference(VaporReference.builder().setName("WaitMetadata").setPakkage(SHOWCASE_PACKAGE_NAME).build());
LongrunningOperation lro = LongrunningOperation.builder().setResponseType(responseType).setMetadataType(metadataType).build();
TypeNode ttlTypeNode = TypeNode.withReference(VaporReference.builder().setName("Duration").setPakkage(PROTO_PACKAGE_NAME).build());
MethodArgument ttl = MethodArgument.builder().setName("ttl").setType(ttlTypeNode).setField(Field.builder().setName("ttl").setType(ttlTypeNode).setIsMessage(true).setIsContainedInOneof(true).build()).build();
List<MethodArgument> arguments = Arrays.asList(ttl);
Method method = Method.builder().setName("Wait").setInputType(inputType).setOutputType(outputType).setLro(lro).setMethodSignatures(Arrays.asList(arguments)).build();
String results = writeStatements(ServiceClientHeaderSampleComposer.composeShowcaseMethodSample(method, clientType, arguments, resourceNames, messageTypes));
String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", " Duration ttl = Duration.newBuilder().build();\n", " echoClient.waitAsync(ttl).get();\n", "}");
Assert.assertEquals(results, expected);
}
use of com.google.api.generator.gapic.model.ResourceName in project gapic-generator-java by googleapis.
the class ServiceClientHeaderSampleComposerTest method invalid_composeShowcaseMethodSample_noRepeatedResponseTypeInPagedMethod.
@Test
public void invalid_composeShowcaseMethodSample_noRepeatedResponseTypeInPagedMethod() {
Descriptors.FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
TypeNode clientType = TypeNode.withReference(VaporReference.builder().setName("EchoClient").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode inputType = TypeNode.withReference(VaporReference.builder().setName("EchoRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("PagedResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
List<MethodArgument> methodArguments = Collections.emptyList();
Method method = Method.builder().setName("simplePagedMethod").setMethodSignatures(Arrays.asList(methodArguments)).setInputType(inputType).setOutputType(outputType).setPageSizeFieldName(PAGINATED_FIELD_NAME).build();
Field responseField = Field.builder().setName("response").setType(TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(ConcreteReference.withClazz(String.class)).build())).setIsMessage(true).setIsRepeated(false).build();
Field nextPageToken = Field.builder().setName("next_page_token").setType(TypeNode.STRING).build();
Message noRepeatedFieldMessage = Message.builder().setName("PagedResponse").setFullProtoName("google.showcase.v1beta1.PagedResponse").setType(outputType).setFields(Arrays.asList(responseField, nextPageToken)).build();
messageTypes.put("PagedResponse", noRepeatedFieldMessage);
Assert.assertThrows(NullPointerException.class, () -> ServiceClientHeaderSampleComposer.composeShowcaseMethodSample(method, clientType, methodArguments, resourceNames, messageTypes));
}
Aggregations