Search in sources :

Example 61 with ResourceName

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

the class ServiceClientHeaderSampleComposerTest method valid_composeShowcaseMethodSample_pagedRpcWithMultipleMethodArguments.

/*Testing composeShowcaseMethodSample*/
@Test
public void valid_composeShowcaseMethodSample_pagedRpcWithMultipleMethodArguments() {
    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());
    TypeNode resourceNameType = TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(ConcreteReference.withClazz(String.class)).build());
    List<MethodArgument> arguments = Arrays.asList(MethodArgument.builder().setName("resourceName").setType(resourceNameType).setField(Field.builder().setName("resourceName").setType(resourceNameType).setIsRepeated(true).build()).build(), MethodArgument.builder().setName("filter").setType(TypeNode.STRING).setField(Field.builder().setName("filter").setType(TypeNode.STRING).build()).build());
    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", "  List<String> resourceName = new ArrayList<>();\n", "  String filter = \"filter-1274492040\";\n", "  for (Content element : echoClient.listContent(resourceName, filter).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 62 with ResourceName

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

the class ServiceClientHeaderSampleComposerTest method composeClassHeaderSample_unaryRpc.

/*Testing composeClassHeaderSample*/
@Test
public void composeClassHeaderSample_unaryRpc() {
    Descriptors.FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
    Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
    Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
    Set<ResourceName> outputResourceNames = new HashSet<>();
    List<Service> services = Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
    Service echoProtoService = services.get(0);
    TypeNode clientType = TypeNode.withReference(VaporReference.builder().setName("EchoClient").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    Sample sample = ServiceClientHeaderSampleComposer.composeClassHeaderSample(echoProtoService, clientType, resourceNames, messageTypes);
    String results = writeStatements(sample);
    String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", "  EchoResponse response = echoClient.echo();\n", "}");
    Assert.assertEquals(expected, results);
}
Also used : Message(com.google.api.generator.gapic.model.Message) ResourceName(com.google.api.generator.gapic.model.ResourceName) Sample(com.google.api.generator.gapic.model.Sample) Service(com.google.api.generator.gapic.model.Service) Descriptors(com.google.protobuf.Descriptors) TypeNode(com.google.api.generator.engine.ast.TypeNode) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 63 with ResourceName

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

the class ResourceNameHelperClassComposerTest method generateResourceNameClass_loggingOnePatternMultipleVariables.

@Test
public void generateResourceNameClass_loggingOnePatternMultipleVariables() {
    FileDescriptor serviceFileDescriptor = LoggingConfigProto.getDescriptor();
    ServiceDescriptor serviceDescriptor = serviceFileDescriptor.getServices().get(0);
    assertEquals(serviceDescriptor.getName(), "ConfigServiceV2");
    List<FileDescriptor> protoFiles = Arrays.asList(serviceFileDescriptor, LoggingProto.getDescriptor(), 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));
    }
    // Additional resource names.
    FileDescriptor commonResourcesFileDescriptor = CommonResources.getDescriptor();
    resourceNames.putAll(Parser.parseResourceNames(commonResourcesFileDescriptor));
    Set<ResourceName> outputResourceNames = new HashSet<>();
    Parser.parseService(serviceFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
    ResourceName billingAccountLocationResname = resourceNames.get("logging.googleapis.com/BillingAccountLocation");
    assertThat(outputResourceNames).contains(billingAccountLocationResname);
    GapicClass clazz = ResourceNameHelperClassComposer.instance().generate(billingAccountLocationResname, TestProtoLoader.instance().parseLogging());
    JavaWriterVisitor visitor = new JavaWriterVisitor();
    clazz.classDefinition().accept(visitor);
    Utils.saveCodegenToFile(this.getClass(), "BillingAccountLocationName.golden", visitor.write());
    Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "BillingAccountLocationName.golden");
    Assert.assertCodeEquals(goldenFilePath, visitor.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) JavaWriterVisitor(com.google.api.generator.engine.writer.JavaWriterVisitor) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) ServiceDescriptor(com.google.protobuf.Descriptors.ServiceDescriptor) GapicClass(com.google.api.generator.gapic.model.GapicClass) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 64 with ResourceName

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

the class ResourceNameHelperClassComposerTest method generateResourceNameClass_childSingleton.

@Test
public void generateResourceNameClass_childSingleton() {
    ResourceName agentResname = ResourceName.builder().setVariableName("agent").setPakkage("com.google.cloud.dialogflow.v2beta1").setResourceTypeString("dialogflow.googleapis.com/Agent").setPatterns(Arrays.asList("projects/{project}/locations/{location}/agent", "projects/{project}/agent")).setParentMessageName("Agent").setDescription("This is a description").build();
    GapicContext irrelevantContext = TestProtoLoader.instance().parseShowcaseEcho();
    GapicClass clazz = ResourceNameHelperClassComposer.instance().generate(agentResname, irrelevantContext);
    JavaWriterVisitor visitor = new JavaWriterVisitor();
    clazz.classDefinition().accept(visitor);
    Utils.saveCodegenToFile(this.getClass(), "AgentName.golden", visitor.write());
    Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "AgentName.golden");
    Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
Also used : Path(java.nio.file.Path) ResourceName(com.google.api.generator.gapic.model.ResourceName) GapicClass(com.google.api.generator.gapic.model.GapicClass) JavaWriterVisitor(com.google.api.generator.engine.writer.JavaWriterVisitor) GapicContext(com.google.api.generator.gapic.model.GapicContext) Test(org.junit.Test)

Example 65 with ResourceName

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

the class GrpcRestTestProtoLoader method parseShowcaseEcho.

@Override
public GapicContext parseShowcaseEcho() {
    FileDescriptor echoFileDescriptor = EchoGrpcrest.getDescriptor();
    ServiceDescriptor echoServiceDescriptor = echoFileDescriptor.getServices().get(0);
    assertEquals("Echo", echoServiceDescriptor.getName());
    Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
    Map<String, Message> operationMessageTypes = Parser.parseMessages(OperationsProto.getDescriptor());
    messageTypes.putAll(operationMessageTypes);
    Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
    Set<ResourceName> outputResourceNames = new HashSet<>();
    List<Service> services = Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
    String jsonFilename = "showcase_grpc_service_config.json";
    Path jsonPath = Paths.get(getTestFilesDirectory(), jsonFilename);
    Optional<GapicServiceConfig> configOpt = ServiceConfigParser.parse(jsonPath.toString());
    assertTrue(configOpt.isPresent());
    GapicServiceConfig config = configOpt.get();
    return GapicContext.builder().setMessages(messageTypes).setResourceNames(resourceNames).setServices(services).setServiceConfig(config).setHelperResourceNames(outputResourceNames).setTransport(getTransport()).build();
}
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) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) ServiceDescriptor(com.google.protobuf.Descriptors.ServiceDescriptor) HashSet(java.util.HashSet)

Aggregations

ResourceName (com.google.api.generator.gapic.model.ResourceName)112 Message (com.google.api.generator.gapic.model.Message)82 Test (org.junit.Test)76 TypeNode (com.google.api.generator.engine.ast.TypeNode)58 Method (com.google.api.generator.gapic.model.Method)50 FileDescriptor (com.google.protobuf.Descriptors.FileDescriptor)47 HashSet (java.util.HashSet)35 Descriptors (com.google.protobuf.Descriptors)30 List (java.util.List)29 Expr (com.google.api.generator.engine.ast.Expr)28 Service (com.google.api.generator.gapic.model.Service)28 HashMap (java.util.HashMap)28 ServiceDescriptor (com.google.protobuf.Descriptors.ServiceDescriptor)20 ArrayList (java.util.ArrayList)19 Preconditions (com.google.common.base.Preconditions)18 Path (java.nio.file.Path)18 Map (java.util.Map)18 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)17 Field (com.google.api.generator.gapic.model.Field)17 GapicClass (com.google.api.generator.gapic.model.GapicClass)17