Search in sources :

Example 51 with Message

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

the class BatchingDescriptorComposerTest method batchingDescriptor_noSubresponseField.

@Test
public void batchingDescriptor_noSubresponseField() {
    FileDescriptor serviceFileDescriptor = LoggingProto.getDescriptor();
    ServiceDescriptor serviceDescriptor = serviceFileDescriptor.getServices().get(0);
    assertEquals(serviceDescriptor.getName(), "LoggingServiceV2");
    List<FileDescriptor> protoFiles = Arrays.asList(serviceFileDescriptor, LogEntryProto.getDescriptor(), LoggingConfigProto.getDescriptor(), LoggingMetricsProto.getDescriptor());
    Map<String, ResourceName> resourceNames = new HashMap<>();
    Map<String, Message> messageTypes = new HashMap<>();
    for (FileDescriptor fileDescriptor : protoFiles) {
        resourceNames.putAll(Parser.parseResourceNames(fileDescriptor));
        messageTypes.putAll(Parser.parseMessages(fileDescriptor));
    }
    Set<ResourceName> outputResourceNames = new HashSet<>();
    List<Service> services = Parser.parseService(serviceFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
    String filename = "logging_gapic.yaml";
    Path path = Paths.get(TestProtoLoader.instance().getTestFilesDirectory(), filename);
    Optional<List<GapicBatchingSettings>> batchingSettingsOpt = BatchingSettingsConfigParser.parse(Optional.of(path.toString()));
    assertTrue(batchingSettingsOpt.isPresent());
    String jsonFilename = "logging_grpc_service_config.json";
    Path jsonPath = Paths.get(TestProtoLoader.instance().getTestFilesDirectory(), jsonFilename);
    Optional<GapicServiceConfig> configOpt = ServiceConfigParser.parse(jsonPath.toString());
    assertTrue(configOpt.isPresent());
    GapicServiceConfig config = configOpt.get();
    config.setBatchingSettings(batchingSettingsOpt);
    Service service = services.get(0);
    assertEquals("LoggingServiceV2", service.name());
    Method method = findMethod(service, "WriteLogEntries");
    GapicBatchingSettings batchingSetting = batchingSettingsOpt.get().get(0);
    assertEquals("WriteLogEntries", batchingSetting.methodName());
    Expr batchingDescriptorExpr = BatchingDescriptorComposer.createBatchingDescriptorFieldDeclExpr(method, batchingSetting, messageTypes);
    batchingDescriptorExpr.accept(writerVisitor);
    Utils.saveCodegenToFile(this.getClass(), "BatchingDescriptorComposerTestNoSubresponse.golden", writerVisitor.write());
    Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "BatchingDescriptorComposerTestNoSubresponse.golden");
    Assert.assertCodeEquals(goldenFilePath, writerVisitor.write());
}
Also used : Path(java.nio.file.Path) Message(com.google.api.generator.gapic.model.Message) HashMap(java.util.HashMap) 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) ServiceDescriptor(com.google.protobuf.Descriptors.ServiceDescriptor) List(java.util.List) GapicBatchingSettings(com.google.api.generator.gapic.model.GapicBatchingSettings) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 52 with Message

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

the class RetrySettingsComposerTest method codesDefinitionsBlock_basic.

@Test
public void codesDefinitionsBlock_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();
    BlockStatement paramDefinitionsBlock = RetrySettingsComposer.createRetryCodesDefinitionsBlock(service, serviceConfig, RETRY_CODES_DEFINITIONS_VAR_EXPR);
    paramDefinitionsBlock.accept(writerVisitor);
    String expected = LineFormatter.lines("static {\n", "ImmutableMap.Builder<String, ImmutableSet<StatusCode.Code>> definitions =" + " ImmutableMap.builder();\n", "definitions.put(\"retry_policy_1_codes\"," + " ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList(StatusCode.Code.UNAVAILABLE," + " StatusCode.Code.UNKNOWN)));\n", "definitions.put(\"no_retry_0_codes\"," + " ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList()));\n", "RETRYABLE_CODE_DEFINITIONS = definitions.build();\n", "}\n");
    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) BlockStatement(com.google.api.generator.engine.ast.BlockStatement) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 53 with Message

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

the class RetrySettingsComposerTest method codesDefinitionsBlock_noConfigsFound.

@Test
public void codesDefinitionsBlock_noConfigsFound() {
    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 = "retrying_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();
    BlockStatement paramDefinitionsBlock = RetrySettingsComposer.createRetryCodesDefinitionsBlock(service, serviceConfig, RETRY_CODES_DEFINITIONS_VAR_EXPR);
    paramDefinitionsBlock.accept(writerVisitor);
    String expected = LineFormatter.lines("static {\n", "ImmutableMap.Builder<String, ImmutableSet<StatusCode.Code>> definitions =" + " ImmutableMap.builder();\n", "definitions.put(\"no_retry_codes\"," + " ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList()));\n", "RETRYABLE_CODE_DEFINITIONS = definitions.build();\n", "}\n");
    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) BlockStatement(com.google.api.generator.engine.ast.BlockStatement) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 54 with Message

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

the class ServiceClientCallableMethodSampleComposerTest method invalid_composePagedCallableMethod_inputTypeNotExistInMessage.

@Test
public void invalid_composePagedCallableMethod_inputTypeNotExistInMessage() {
    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("PagedExpandResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    Method method = Method.builder().setName("PagedExpand").setInputType(inputType).setOutputType(outputType).setPageSizeFieldName(PAGINATED_FIELD_NAME).build();
    Assert.assertThrows(NullPointerException.class, () -> ServiceClientCallableMethodSampleComposer.composePagedCallableMethod(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 55 with Message

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

the class ServiceClientCallableMethodSampleComposerTest method valid_composePagedCallableMethod.

/*Testing composePagedCallableMethod*/
@Test
public void valid_composePagedCallableMethod() {
    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).setPageSizeFieldName(PAGINATED_FIELD_NAME).build();
    String results = writeStatements(ServiceClientCallableMethodSampleComposer.composePagedCallableMethod(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", "  ApiFuture<EchoResponse> future =" + " echoClient.pagedExpandPagedCallable().futureCall(request);\n", "  // Do something.\n", "  for (EchoResponse element : future.get().iterateAll()) {\n", "    // doThingsWith(element);\n", "  }\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)

Aggregations

Message (com.google.api.generator.gapic.model.Message)123 ResourceName (com.google.api.generator.gapic.model.ResourceName)85 TypeNode (com.google.api.generator.engine.ast.TypeNode)74 Test (org.junit.Test)68 Method (com.google.api.generator.gapic.model.Method)60 FileDescriptor (com.google.protobuf.Descriptors.FileDescriptor)44 HashSet (java.util.HashSet)39 ArrayList (java.util.ArrayList)36 Service (com.google.api.generator.gapic.model.Service)35 List (java.util.List)34 Expr (com.google.api.generator.engine.ast.Expr)32 Field (com.google.api.generator.gapic.model.Field)31 Descriptors (com.google.protobuf.Descriptors)30 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)29 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)26 ServiceDescriptor (com.google.protobuf.Descriptors.ServiceDescriptor)26 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)25 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)25 Statement (com.google.api.generator.engine.ast.Statement)24 HashMap (java.util.HashMap)23