Search in sources :

Example 31 with Service

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

the class RetrySettingsComposerTest method codesDefinitionsBlock_basic.

@Test
public void codesDefinitionsBlock_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();
    BlockStatement paramDefinitionsBlock = RetrySettingsComposer.createRetryCodesDefinitionsBlock(service, serviceConfig, RETRY_CODES_DEFINITIONS_VAR_EXPR);
    paramDefinitionsBlock.accept(writerVisitor);
    String expected = LineFormatter.lines("static {\n", "ImmutableMap.Builder<String, ImmutableSet<StatusCode.Code>> definitions =" + " ImmutableMap.builder();\n", "definitions.put(\"retry_policy_1_codes\"," + " ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList(StatusCode.Code.UNAVAILABLE," + " StatusCode.Code.UNKNOWN)));\n", "definitions.put(\"no_retry_0_codes\"," + " ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList()));\n", "RETRYABLE_CODE_DEFINITIONS = definitions.build();\n", "}\n");
    assertEquals(expected, writerVisitor.write());
}
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) BlockStatement(com.google.api.generator.engine.ast.BlockStatement) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 32 with Service

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

the class RetrySettingsComposerTest method paramDefinitionsBlock_noConfigsFound.

@Test
public void paramDefinitionsBlock_noConfigsFound() {
    GapicContext context = TestProtoLoader.instance().parseShowcaseEcho();
    Service service = context.services().get(0);
    String jsonFilename = "retrying_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().setRpcTimeoutMultiplier(1.0).build();\n", "definitions.put(\"no_retry_params\", settings);\n", "RETRY_PARAM_DEFINITIONS = definitions.build();\n", "}\n");
    assertEquals(expected, writerVisitor.write());
}
Also used : Path(java.nio.file.Path) Service(com.google.api.generator.gapic.model.Service) BlockStatement(com.google.api.generator.engine.ast.BlockStatement) GapicContext(com.google.api.generator.gapic.model.GapicContext) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) Test(org.junit.Test)

Example 33 with Service

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

the class RetrySettingsComposerTest method codesDefinitionsBlock_noConfigsFound.

@Test
public void codesDefinitionsBlock_noConfigsFound() {
    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 = "retrying_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.createRetryCodesDefinitionsBlock(service, serviceConfig, RETRY_CODES_DEFINITIONS_VAR_EXPR);
    paramDefinitionsBlock.accept(writerVisitor);
    String expected = LineFormatter.lines("static {\n", "ImmutableMap.Builder<String, ImmutableSet<StatusCode.Code>> definitions =" + " ImmutableMap.builder();\n", "definitions.put(\"no_retry_codes\"," + " ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList()));\n", "RETRYABLE_CODE_DEFINITIONS = definitions.build();\n", "}\n");
    assertEquals(expected, writerVisitor.write());
}
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) BlockStatement(com.google.api.generator.engine.ast.BlockStatement) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 34 with Service

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

the class ServiceStubSettingsClassComposerTest method generateServiceClasses.

@Test
public void generateServiceClasses() {
    GapicContext context = RestTestProtoLoader.instance().parseCompliance();
    Service echoProtoService = context.services().get(0);
    GapicClass clazz = ServiceStubSettingsClassComposer.instance().generate(context, echoProtoService);
    JavaWriterVisitor visitor = new JavaWriterVisitor();
    clazz.classDefinition().accept(visitor);
    Utils.saveCodegenToFile(this.getClass(), "ComplianceStubSettings.golden", visitor.write());
    Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "ComplianceStubSettings.golden");
    Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
Also used : Path(java.nio.file.Path) GapicClass(com.google.api.generator.gapic.model.GapicClass) Service(com.google.api.generator.gapic.model.Service) JavaWriterVisitor(com.google.api.generator.engine.writer.JavaWriterVisitor) GapicContext(com.google.api.generator.gapic.model.GapicContext) Test(org.junit.Test)

Example 35 with Service

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

the class ServiceClientHeaderSampleComposerTest method composeClassHeaderSample_firstMethodIsStream.

@Test
public void composeClassHeaderSample_firstMethodIsStream() {
    Descriptors.FileDescriptor echoFileDescriptor = EchoOuterClass.getDescriptor();
    Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor);
    Map<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor);
    TypeNode inputType = TypeNode.withReference(VaporReference.builder().setName("ExpandRequest").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();
    Service service = Service.builder().setName("Echo").setDefaultHost("localhost:7469").setOauthScopes(Arrays.asList("https://www.googleapis.com/auth/cloud-platform")).setPakkage(SHOWCASE_PACKAGE_NAME).setProtoPakkage(SHOWCASE_PACKAGE_NAME).setOriginalJavaPackage(SHOWCASE_PACKAGE_NAME).setOverriddenName("Echo").setMethods(Arrays.asList(method)).build();
    TypeNode clientType = TypeNode.withReference(VaporReference.builder().setName("EchoClient").setPakkage(SHOWCASE_PACKAGE_NAME).build());
    String results = writeStatements(ServiceClientHeaderSampleComposer.composeClassHeaderSample(service, clientType, resourceNames, messageTypes));
    String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", "  ExpandRequest request =\n", "      ExpandRequest.newBuilder().setContent(\"content951530617\").setInfo(\"info3237038\").build();\n", "  ServerStream<EchoResponse> stream = echoClient.expandCallable().call(request);\n", "  for (EchoResponse response : stream) {\n", "    // Do something when a response is received.\n", "  }\n", "}");
    Assert.assertEquals(results, expected);
}
Also used : Message(com.google.api.generator.gapic.model.Message) ResourceName(com.google.api.generator.gapic.model.ResourceName) Service(com.google.api.generator.gapic.model.Service) Descriptors(com.google.protobuf.Descriptors) TypeNode(com.google.api.generator.engine.ast.TypeNode) Method(com.google.api.generator.gapic.model.Method) Test(org.junit.Test)

Aggregations

Service (com.google.api.generator.gapic.model.Service)87 GapicContext (com.google.api.generator.gapic.model.GapicContext)54 GapicClass (com.google.api.generator.gapic.model.GapicClass)48 Message (com.google.api.generator.gapic.model.Message)45 Test (org.junit.Test)45 Method (com.google.api.generator.gapic.model.Method)39 List (java.util.List)39 TypeNode (com.google.api.generator.engine.ast.TypeNode)37 ResourceName (com.google.api.generator.gapic.model.ResourceName)35 Expr (com.google.api.generator.engine.ast.Expr)34 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)32 GapicServiceConfig (com.google.api.generator.gapic.model.GapicServiceConfig)32 ArrayList (java.util.ArrayList)32 Arrays (java.util.Arrays)32 Collectors (java.util.stream.Collectors)32 Path (java.nio.file.Path)31 HashSet (java.util.HashSet)31 Function (java.util.function.Function)31 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)30 Preconditions (com.google.common.base.Preconditions)30