Search in sources :

Example 76 with Method

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

the class RetrySettingsComposerTest method lroBuilderExpr.

@Test
public void lroBuilderExpr() {
    FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
    Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
    Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
    Set<ResourceName> outputResourceNames = new HashSet<>();
    List<Service> services = Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
    assertEquals(1, services.size());
    Service service = services.get(0);
    String jsonFilename = "showcase_grpc_service_config.json";
    Path jsonPath = Paths.get(TestProtoLoader.instance().getTestFilesDirectory(), jsonFilename);
    Optional<GapicServiceConfig> serviceConfigOpt = ServiceConfigParser.parse(jsonPath.toString());
    assertTrue(serviceConfigOpt.isPresent());
    GapicServiceConfig serviceConfig = serviceConfigOpt.get();
    // LRO method.
    Method waitMethod = findMethod(service, "Wait");
    assertThat(waitMethod).isNotNull();
    VariableExpr builderVarExpr = createBuilderVarExpr(service);
    Expr builderExpr = RetrySettingsComposer.createLroSettingsBuilderExpr(service, serviceConfig, waitMethod, builderVarExpr, RETRY_CODES_DEFINITIONS_VAR_EXPR, RETRY_PARAM_DEFINITIONS_VAR_EXPR, GrpcContext.instance().operationResponseTransformerType(), GrpcContext.instance().operationMetadataTransformerType());
    builderExpr.accept(writerVisitor);
    String expected = LineFormatter.lines("builder.waitOperationSettings()" + ".setInitialCallSettings(UnaryCallSettings.<WaitRequest," + " OperationSnapshot>newUnaryCallSettingsBuilder()" + ".setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get(\"no_retry_0_codes\"))" + ".setRetrySettings(RETRY_PARAM_DEFINITIONS.get(\"no_retry_0_params\")).build())" + ".setResponseTransformer(ProtoOperationTransformers.ResponseTransformer.create(" + "WaitResponse.class))" + ".setMetadataTransformer(ProtoOperationTransformers.MetadataTransformer.create(" + "WaitMetadata.class)).setPollingAlgorithm(OperationTimedPollAlgorithm.create(" + "RetrySettings.newBuilder().setInitialRetryDelay(Duration.ofMillis(5000L))" + ".setRetryDelayMultiplier(1.5).setMaxRetryDelay(Duration.ofMillis(45000L))" + ".setInitialRpcTimeout(Duration.ZERO).setRpcTimeoutMultiplier(1.0)" + ".setMaxRpcTimeout(Duration.ZERO).setTotalTimeout(Duration.ofMillis(300000L))" + ".build()))");
    assertEquals(expected, writerVisitor.write());
}
Also used : Path(java.nio.file.Path) Message(com.google.api.generator.gapic.model.Message) ResourceName(com.google.api.generator.gapic.model.ResourceName) Service(com.google.api.generator.gapic.model.Service) Method(com.google.api.generator.gapic.model.Method) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Expr(com.google.api.generator.engine.ast.Expr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 77 with Method

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

the class RetrySettingsComposerTest method simpleBuilderExpr_basic.

@Test
public void simpleBuilderExpr_basic() {
    FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
    Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
    Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
    Set<ResourceName> outputResourceNames = new HashSet<>();
    List<Service> services = Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
    assertEquals(1, services.size());
    Service service = services.get(0);
    String jsonFilename = "showcase_grpc_service_config.json";
    Path jsonPath = Paths.get(TestProtoLoader.instance().getTestFilesDirectory(), jsonFilename);
    Optional<GapicServiceConfig> serviceConfigOpt = ServiceConfigParser.parse(jsonPath.toString());
    assertTrue(serviceConfigOpt.isPresent());
    GapicServiceConfig serviceConfig = serviceConfigOpt.get();
    // Regular method.
    Method echoMethod = findMethod(service, "Echo");
    assertThat(echoMethod).isNotNull();
    VariableExpr builderVarExpr = createBuilderVarExpr(service);
    Expr builderExpr = RetrySettingsComposer.createSimpleBuilderSettingsExpr(service, serviceConfig, echoMethod, builderVarExpr, RETRY_CODES_DEFINITIONS_VAR_EXPR, RETRY_PARAM_DEFINITIONS_VAR_EXPR);
    builderExpr.accept(writerVisitor);
    String expected = LineFormatter.lines("builder.echoSettings()" + ".setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get(\"retry_policy_1_codes\"))" + ".setRetrySettings(RETRY_PARAM_DEFINITIONS.get(\"retry_policy_1_params\"))");
    assertEquals(expected, writerVisitor.write());
    // Server-streaming method.
    Method expandMethod = findMethod(service, "Expand");
    assertThat(expandMethod).isNotNull();
    builderExpr = RetrySettingsComposer.createSimpleBuilderSettingsExpr(service, serviceConfig, expandMethod, builderVarExpr, RETRY_CODES_DEFINITIONS_VAR_EXPR, RETRY_PARAM_DEFINITIONS_VAR_EXPR);
    writerVisitor.clear();
    builderExpr.accept(writerVisitor);
    expected = LineFormatter.lines("builder.expandSettings()" + ".setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get(\"retry_policy_1_codes\"))" + ".setRetrySettings(RETRY_PARAM_DEFINITIONS.get(\"retry_policy_1_params\"))");
    assertEquals(expected, writerVisitor.write());
    // LRO method.
    Method waitMethod = findMethod(service, "Wait");
    assertThat(waitMethod).isNotNull();
    builderExpr = RetrySettingsComposer.createSimpleBuilderSettingsExpr(service, serviceConfig, waitMethod, builderVarExpr, RETRY_CODES_DEFINITIONS_VAR_EXPR, RETRY_PARAM_DEFINITIONS_VAR_EXPR);
    writerVisitor.clear();
    builderExpr.accept(writerVisitor);
    expected = LineFormatter.lines("builder.waitSettings()" + ".setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get(\"no_retry_0_codes\"))" + ".setRetrySettings(RETRY_PARAM_DEFINITIONS.get(\"no_retry_0_params\"))");
    assertEquals(expected, writerVisitor.write());
}
Also used : Path(java.nio.file.Path) Message(com.google.api.generator.gapic.model.Message) ResourceName(com.google.api.generator.gapic.model.ResourceName) Service(com.google.api.generator.gapic.model.Service) Method(com.google.api.generator.gapic.model.Method) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Expr(com.google.api.generator.engine.ast.Expr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 78 with Method

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

the class ServiceClientCallableMethodSampleComposerTest method invalid_composeStreamCallableMethod_serverStreamNotExistRequest.

@Test
public void invalid_composeStreamCallableMethod_serverStreamNotExistRequest() {
    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("Expand").setInputType(inputType).setOutputType(outputType).setStream(Method.Stream.SERVER).build();
    Assert.assertThrows(NullPointerException.class, () -> ServiceClientCallableMethodSampleComposer.composeStreamCallableMethod(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 79 with Method

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

the class ServiceClientMethodSampleComposerTest method valid_composeDefaultSample_hasLroMethodWithReturnResponse.

@Test
public void valid_composeDefaultSample_hasLroMethodWithReturnResponse() {
    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();
    Method method = Method.builder().setName("Wait").setInputType(inputType).setOutputType(outputType).setMethodSignatures(Collections.emptyList()).setLro(lro).build();
    String results = writeStatements(ServiceClientMethodSampleComposer.composeCanonicalSample(method, clientType, resourceNames, messageTypes));
    String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", "  WaitRequest request = WaitRequest.newBuilder().build();\n", "  echoClient.waitAsync(request).get();\n", "}");
    Assert.assertEquals(results, expected);
}
Also used : LongrunningOperation(com.google.api.generator.gapic.model.LongrunningOperation) Message(com.google.api.generator.gapic.model.Message) ResourceName(com.google.api.generator.gapic.model.ResourceName) TypeNode(com.google.api.generator.engine.ast.TypeNode) Method(com.google.api.generator.gapic.model.Method) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) Test(org.junit.Test)

Example 80 with Method

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

the class ServiceClientMethodSampleComposerTest method valid_composeDefaultSample_hasLroMethodWithReturnVoid.

@Test
public void valid_composeDefaultSample_hasLroMethodWithReturnVoid() {
    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();
    Method method = Method.builder().setName("Wait").setInputType(inputType).setOutputType(outputType).setMethodSignatures(Collections.emptyList()).setLro(lro).build();
    String results = writeStatements(ServiceClientMethodSampleComposer.composeCanonicalSample(method, clientType, resourceNames, messageTypes));
    String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", "  WaitRequest request = WaitRequest.newBuilder().build();\n", "  WaitResponse response = echoClient.waitAsync(request).get();\n", "}");
    Assert.assertEquals(results, expected);
}
Also used : LongrunningOperation(com.google.api.generator.gapic.model.LongrunningOperation) Message(com.google.api.generator.gapic.model.Message) ResourceName(com.google.api.generator.gapic.model.ResourceName) TypeNode(com.google.api.generator.engine.ast.TypeNode) Method(com.google.api.generator.gapic.model.Method) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) 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