Search in sources :

Example 36 with Method

use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.

the class ServiceClientCallableMethodSampleComposerTest method valid_composeRegularCallableMethod_unaryRpc.

/*Testing composeRegularCallableMethod*/
@Test
public void valid_composeRegularCallableMethod_unaryRpc() {
    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("Echo").setInputType(inputType).setOutputType(outputType).build();
    String results = writeStatements(ServiceClientCallableMethodSampleComposer.composeRegularCallableMethod(method, clientType, resourceNames, messageTypes));
    String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\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", "  ApiFuture<EchoResponse> future = echoClient.echoCallable().futureCall(request);\n", "  // Do something.\n", "  EchoResponse response = future.get();\n", "}");
    Assert.assertEquals(results, expected);
}
Also used : Message(com.google.api.generator.gapic.model.Message) ResourceName(com.google.api.generator.gapic.model.ResourceName) Descriptors(com.google.protobuf.Descriptors) TypeNode(com.google.api.generator.engine.ast.TypeNode) Method(com.google.api.generator.gapic.model.Method) Test(org.junit.Test)

Example 37 with Method

use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.

the class ServiceClientCallableMethodSampleComposerTest method invalid_composeRegularCallableMethod_noExistMethodRequest.

@Test
public void invalid_composeRegularCallableMethod_noExistMethodRequest() {
    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("NoExistRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("EchoResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    Method method = Method.builder().setName("Echo").setInputType(inputType).setOutputType(outputType).build();
    Assert.assertThrows(NullPointerException.class, () -> ServiceClientCallableMethodSampleComposer.composeRegularCallableMethod(method, clientType, resourceNames, messageTypes));
}
Also used : Message(com.google.api.generator.gapic.model.Message) ResourceName(com.google.api.generator.gapic.model.ResourceName) Descriptors(com.google.protobuf.Descriptors) TypeNode(com.google.api.generator.engine.ast.TypeNode) Method(com.google.api.generator.gapic.model.Method) Test(org.junit.Test)

Example 38 with Method

use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.

the class ServiceClientHeaderSampleComposerTest method composeClassHeaderSample_firstMethodIsStream.

@Test
public void composeClassHeaderSample_firstMethodIsStream() {
    Descriptors.FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
    Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
    Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
    TypeNode inputType = TypeNode.withReference(VaporReference.builder().setName("ExpandRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("EchoResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    Method method = Method.builder().setName("Expand").setInputType(inputType).setOutputType(outputType).setStream(Method.Stream.SERVER).build();
    Service service = Service.builder().setName("Echo").setDefaultHost("localhost:7469").setOauthScopes(Arrays.asList("https://www.googleapis.com/auth/cloud-platform")).setPakkage(SHOWCASE_PACKAGE_NAME).setProtoPakkage(SHOWCASE_PACKAGE_NAME).setOriginalJavaPackage(SHOWCASE_PACKAGE_NAME).setOverriddenName("Echo").setMethods(Arrays.asList(method)).build();
    TypeNode clientType = TypeNode.withReference(VaporReference.builder().setName("EchoClient").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    String results = writeStatements(ServiceClientHeaderSampleComposer.composeClassHeaderSample(service, clientType, resourceNames, messageTypes));
    String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", "  ExpandRequest request =\n", "      ExpandRequest.newBuilder().setContent(\"content951530617\").setInfo(\"info3237038\").build();\n", "  ServerStream<EchoResponse> stream = echoClient.expandCallable().call(request);\n", "  for (EchoResponse response : stream) {\n", "    // Do something when a response is received.\n", "  }\n", "}");
    Assert.assertEquals(results, expected);
}
Also used : Message(com.google.api.generator.gapic.model.Message) ResourceName(com.google.api.generator.gapic.model.ResourceName) Service(com.google.api.generator.gapic.model.Service) Descriptors(com.google.protobuf.Descriptors) TypeNode(com.google.api.generator.engine.ast.TypeNode) Method(com.google.api.generator.gapic.model.Method) Test(org.junit.Test)

Example 39 with Method

use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.

the class ServiceClientHeaderSampleComposerTest method valid_composeShowcaseMethodSample_pagedRpcWithNoMethodArguments.

@Test
public void valid_composeShowcaseMethodSample_pagedRpcWithNoMethodArguments() {
    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("ListContentRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("ListContentResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    List<MethodArgument> arguments = Collections.emptyList();
    Method method = Method.builder().setName("ListContent").setMethodSignatures(Arrays.asList(arguments)).setInputType(inputType).setOutputType(outputType).setPageSizeFieldName(PAGINATED_FIELD_NAME).build();
    Reference repeatedResponseReference = VaporReference.builder().setName("Content").setPakkage(SHOWCASE_PACKAGE_NAME).build();
    Field repeatedField = Field.builder().setName("responses").setType(TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(repeatedResponseReference).build())).setIsMessage(true).setIsRepeated(true).build();
    Field nextPagedTokenField = Field.builder().setName("next_page_token").setType(TypeNode.STRING).build();
    Message listContentResponseMessage = Message.builder().setName("ListContentResponse").setFullProtoName("google.showcase.v1beta1.ListContentResponse").setType(outputType).setFields(Arrays.asList(repeatedField, nextPagedTokenField)).build();
    messageTypes.put("com.google.showcase.v1beta1.ListContentResponse", listContentResponseMessage);
    String results = writeStatements(ServiceClientHeaderSampleComposer.composeShowcaseMethodSample(method, clientType, arguments, resourceNames, messageTypes));
    String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", "  for (Content element : echoClient.listContent().iterateAll()) {\n", "    // doThingsWith(element);\n", "  }\n", "}");
    Assert.assertEquals(results, expected);
}
Also used : Message(com.google.api.generator.gapic.model.Message) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) ResourceName(com.google.api.generator.gapic.model.ResourceName) VaporReference(com.google.api.generator.engine.ast.VaporReference) Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) Method(com.google.api.generator.gapic.model.Method) Field(com.google.api.generator.gapic.model.Field) List(java.util.List) Descriptors(com.google.protobuf.Descriptors) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 40 with Method

use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.

the class ServiceClientHeaderSampleComposerTest method valid_composeShowcaseMethodSample_lroRpcWithReturnResponseType.

@Test
public void valid_composeShowcaseMethodSample_lroRpcWithReturnResponseType() {
    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("WaitResponse").setPakkage(SHOWCASE_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", "  WaitResponse response = echoClient.waitAsync(ttl).get();\n", "}");
    Assert.assertEquals(results, expected);
}
Also used : LongrunningOperation(com.google.api.generator.gapic.model.LongrunningOperation) Message(com.google.api.generator.gapic.model.Message) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) ResourceName(com.google.api.generator.gapic.model.ResourceName) Descriptors(com.google.protobuf.Descriptors) TypeNode(com.google.api.generator.engine.ast.TypeNode) Method(com.google.api.generator.gapic.model.Method) Test(org.junit.Test)

Aggregations

Method (com.google.api.generator.gapic.model.Method)81 TypeNode (com.google.api.generator.engine.ast.TypeNode)75 Message (com.google.api.generator.gapic.model.Message)71 ResourceName (com.google.api.generator.gapic.model.ResourceName)60 Test (org.junit.Test)44 List (java.util.List)39 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)38 Expr (com.google.api.generator.engine.ast.Expr)37 ArrayList (java.util.ArrayList)36 Service (com.google.api.generator.gapic.model.Service)34 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)33 Arrays (java.util.Arrays)33 Field (com.google.api.generator.gapic.model.Field)32 Collectors (java.util.stream.Collectors)32 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)31 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)31 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)31 Preconditions (com.google.common.base.Preconditions)31 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)30 Variable (com.google.api.generator.engine.ast.Variable)30