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);
}
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);
}
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());
}
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());
}
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();
}
Aggregations