use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.
the class RetrySettingsComposerTest method lroBuilderExpr.
@Test
public void lroBuilderExpr() {
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();
// LRO method.
Method waitMethod = findMethod(service, "Wait");
assertThat(waitMethod).isNotNull();
VariableExpr builderVarExpr = createBuilderVarExpr(service);
Expr builderExpr = RetrySettingsComposer.createLroSettingsBuilderExpr(service, serviceConfig, waitMethod, builderVarExpr, RETRY_CODES_DEFINITIONS_VAR_EXPR, RETRY_PARAM_DEFINITIONS_VAR_EXPR, GrpcContext.instance().operationResponseTransformerType(), GrpcContext.instance().operationMetadataTransformerType());
builderExpr.accept(writerVisitor);
String expected = LineFormatter.lines("builder.waitOperationSettings()" + ".setInitialCallSettings(UnaryCallSettings.<WaitRequest," + " OperationSnapshot>newUnaryCallSettingsBuilder()" + ".setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get(\"no_retry_0_codes\"))" + ".setRetrySettings(RETRY_PARAM_DEFINITIONS.get(\"no_retry_0_params\")).build())" + ".setResponseTransformer(ProtoOperationTransformers.ResponseTransformer.create(" + "WaitResponse.class))" + ".setMetadataTransformer(ProtoOperationTransformers.MetadataTransformer.create(" + "WaitMetadata.class)).setPollingAlgorithm(OperationTimedPollAlgorithm.create(" + "RetrySettings.newBuilder().setInitialRetryDelay(Duration.ofMillis(5000L))" + ".setRetryDelayMultiplier(1.5).setMaxRetryDelay(Duration.ofMillis(45000L))" + ".setInitialRpcTimeout(Duration.ZERO).setRpcTimeoutMultiplier(1.0)" + ".setMaxRpcTimeout(Duration.ZERO).setTotalTimeout(Duration.ofMillis(300000L))" + ".build()))");
assertEquals(expected, writerVisitor.write());
}
use of com.google.api.generator.gapic.model.Method 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.Method 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.Method in project gapic-generator-java by googleapis.
the class ServiceClientMethodSampleComposerTest method valid_composeDefaultSample_hasLroMethodWithReturnResponse.
@Test
public void valid_composeDefaultSample_hasLroMethodWithReturnResponse() {
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("WaitRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("Operation").setPakkage(LRO_PACKAGE_NAME).build());
TypeNode responseType = TypeNode.withReference(VaporReference.builder().setName("Empty").setPakkage(PROTO_PACKAGE_NAME).build());
TypeNode metadataType = TypeNode.withReference(VaporReference.builder().setName("WaitMetadata").setPakkage(SHOWCASE_PACKAGE_NAME).build());
LongrunningOperation lro = LongrunningOperation.builder().setResponseType(responseType).setMetadataType(metadataType).build();
Method method = Method.builder().setName("Wait").setInputType(inputType).setOutputType(outputType).setMethodSignatures(Collections.emptyList()).setLro(lro).build();
String results = writeStatements(ServiceClientMethodSampleComposer.composeCanonicalSample(method, clientType, resourceNames, messageTypes));
String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", " WaitRequest request = WaitRequest.newBuilder().build();\n", " echoClient.waitAsync(request).get();\n", "}");
Assert.assertEquals(results, expected);
}
use of com.google.api.generator.gapic.model.Method in project gapic-generator-java by googleapis.
the class ServiceClientMethodSampleComposerTest method valid_composeDefaultSample_hasLroMethodWithReturnVoid.
@Test
public void valid_composeDefaultSample_hasLroMethodWithReturnVoid() {
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("WaitRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("Operation").setPakkage(LRO_PACKAGE_NAME).build());
TypeNode responseType = TypeNode.withReference(VaporReference.builder().setName("WaitResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode metadataType = TypeNode.withReference(VaporReference.builder().setName("WaitMetadata").setPakkage(SHOWCASE_PACKAGE_NAME).build());
LongrunningOperation lro = LongrunningOperation.builder().setResponseType(responseType).setMetadataType(metadataType).build();
Method method = Method.builder().setName("Wait").setInputType(inputType).setOutputType(outputType).setMethodSignatures(Collections.emptyList()).setLro(lro).build();
String results = writeStatements(ServiceClientMethodSampleComposer.composeCanonicalSample(method, clientType, resourceNames, messageTypes));
String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", " WaitRequest request = WaitRequest.newBuilder().build();\n", " WaitResponse response = echoClient.waitAsync(request).get();\n", "}");
Assert.assertEquals(results, expected);
}
Aggregations