use of com.google.api.generator.gapic.model.ResourceName 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());
}
use of com.google.api.generator.gapic.model.ResourceName 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));
}
use of com.google.api.generator.gapic.model.ResourceName in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposerTest method generateResourceNameClass_testingSessionOnePattern.
@Test
public void generateResourceNameClass_testingSessionOnePattern() {
FileDescriptor testingFileDescriptor = TestingOuterClass.getDescriptor();
ServiceDescriptor testingService = testingFileDescriptor.getServices().get(0);
assertEquals(testingService.getName(), "Testing");
Map<String, Message> messageTypes = Parser.parseMessages(testingFileDescriptor);
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(testingFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
Parser.parseService(testingFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
ResourceName sessionResname = resourceNames.get("showcase.googleapis.com/Session");
assertThat(outputResourceNames).contains(sessionResname);
GapicClass clazz = ResourceNameHelperClassComposer.instance().generate(sessionResname, TestProtoLoader.instance().parseShowcaseTesting());
JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "SessionName.golden", visitor.write());
Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "SessionName.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_echoFoobarMultiplePatterns.
@Test
public void generateResourceNameClass_echoFoobarMultiplePatterns() {
Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
ResourceName foobarResname = resourceNames.get("showcase.googleapis.com/Foobar");
assertThat(outputResourceNames).contains(foobarResname);
GapicClass clazz = ResourceNameHelperClassComposer.instance().generate(foobarResname, TestProtoLoader.instance().parseShowcaseEcho());
JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "FoobarName.golden", visitor.write());
Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "FoobarName.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_testingBlueprintPatternWithNonSlashSeparator.
@Test
public void generateResourceNameClass_testingBlueprintPatternWithNonSlashSeparator() {
FileDescriptor testingFileDescriptor = TestingOuterClass.getDescriptor();
ServiceDescriptor testingService = testingFileDescriptor.getServices().get(0);
assertEquals(testingService.getName(), "Testing");
Map<String, Message> messageTypes = Parser.parseMessages(testingFileDescriptor);
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(testingFileDescriptor);
Set<ResourceName> outputResourceNames = new HashSet<>();
Parser.parseService(testingFileDescriptor, messageTypes, resourceNames, Optional.empty(), outputResourceNames);
ResourceName testResname = resourceNames.get("showcase.googleapis.com/Test");
assertThat(outputResourceNames).contains(testResname);
GapicClass clazz = ResourceNameHelperClassComposer.instance().generate(testResname, TestProtoLoader.instance().parseShowcaseTesting());
JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "TestName.golden", visitor.write());
Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "TestName.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
Aggregations