use of com.google.api.generator.gapic.model.Service in project gapic-generator-java by googleapis.
the class RetrySettingsComposerTest method paramDefinitionsBlock_basic.
@Test
public void paramDefinitionsBlock_basic() {
GapicContext context = TestProtoLoader.instance().parseShowcaseEcho();
Service service = context.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.createRetryParamDefinitionsBlock(service, serviceConfig, RETRY_PARAM_DEFINITIONS_VAR_EXPR);
paramDefinitionsBlock.accept(writerVisitor);
String expected = LineFormatter.lines("static {\n", "ImmutableMap.Builder<String, RetrySettings> definitions = ImmutableMap.builder();\n", "RetrySettings settings = null;\n", "settings =" + " RetrySettings.newBuilder().setInitialRetryDelay(" + "Duration.ofMillis(100L)).setRetryDelayMultiplier(2.0)" + ".setMaxRetryDelay(Duration.ofMillis(3000L))" + ".setInitialRpcTimeout(Duration.ofMillis(10000L))" + ".setRpcTimeoutMultiplier(1.0)" + ".setMaxRpcTimeout(Duration.ofMillis(10000L))" + ".setTotalTimeout(Duration.ofMillis(10000L)).build();\n", "definitions.put(\"retry_policy_1_params\", settings);\n", "settings =" + " RetrySettings.newBuilder()" + ".setInitialRpcTimeout(Duration.ofMillis(5000L))" + ".setRpcTimeoutMultiplier(1.0)" + ".setMaxRpcTimeout(Duration.ofMillis(5000L))" + ".setTotalTimeout(Duration.ofMillis(5000L)).build();\n", "definitions.put(\"no_retry_0_params\", settings);\n", "RETRY_PARAM_DEFINITIONS = definitions.build();\n", "}\n");
assertEquals(expected, writerVisitor.write());
}
use of com.google.api.generator.gapic.model.Service 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.Service in project gapic-generator-java by googleapis.
the class RetrySettingsComposerTest method batchingSettings_minimalFlowControlSettings.
@Test
public void batchingSettings_minimalFlowControlSettings() {
String filename = "pubsub_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 = "pubsub_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);
GapicContext context = TestProtoLoader.instance().parsePubSubPublisher();
Service service = context.services().get(0);
assertEquals("Publisher", service.name());
VariableExpr builderVarExpr = createBuilderVarExpr(service);
String methodSettingsName = "publishSettings";
GapicBatchingSettings batchingSettings = GapicBatchingSettings.builder().setProtoPakkage("com.google.pubsub.v1").setServiceName("Publishing").setMethodName("Publish").setElementCountThreshold(100).setRequestByteThreshold(1048576).setDelayThresholdMillis(10).setBatchedFieldName("messages").setDiscriminatorFieldNames(Arrays.asList("topic")).setSubresponseFieldName("message_ids").build();
Expr builderExpr = RetrySettingsComposer.createBatchingBuilderSettingsExpr(methodSettingsName, batchingSettings, builderVarExpr);
builderExpr.accept(writerVisitor);
String expected = "builder" + ".publishSettings()" + ".setBatchingSettings(" + "BatchingSettings.newBuilder()" + ".setElementCountThreshold(100L)" + ".setRequestByteThreshold(1048576L)" + ".setDelayThreshold(Duration.ofMillis(10L))" + ".setFlowControlSettings(" + "FlowControlSettings.newBuilder()" + ".setLimitExceededBehavior(FlowController.LimitExceededBehavior.Ignore)" + ".build())" + ".build())";
assertEquals(expected, writerVisitor.write());
}
use of com.google.api.generator.gapic.model.Service in project gapic-generator-java by googleapis.
the class ServiceSettingsClassComposerTest method generateServiceClasses.
@Test
public void generateServiceClasses() {
GapicContext context = RestTestProtoLoader.instance().parseCompliance();
Service echoProtoService = context.services().get(0);
GapicClass clazz = ServiceSettingsClassComposer.instance().generate(context, echoProtoService);
JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "ComplianceSettings.golden", visitor.write());
Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "ComplianceSettings.golden");
Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
use of com.google.api.generator.gapic.model.Service in project gapic-generator-java by googleapis.
the class ServiceClientTestClassComposerTest method generateServiceClasses.
@Test
public void generateServiceClasses() {
GapicContext context = GrpcRestTestProtoLoader.instance().parseShowcaseEcho();
Service echoProtoService = context.services().get(0);
GapicClass clazz = ServiceClientTestClassComposer.instance().generate(context, echoProtoService);
JavaWriterVisitor visitor = new JavaWriterVisitor();
clazz.classDefinition().accept(visitor);
Utils.saveCodegenToFile(this.getClass(), "EchoClientTest.golden", visitor.write());
Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "EchoClientTest.golden");
assertCodeEquals(goldenFilePath, visitor.write());
}
Aggregations