use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class ServiceClientMethodSampleComposerTest method valid_composeDefaultSample_pureUnaryReturnVoid.
@Test
public void valid_composeDefaultSample_pureUnaryReturnVoid() {
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("EchoRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("Empty").setPakkage(PROTO_PACKAGE_NAME).build());
Method method = Method.builder().setName("Echo").setInputType(inputType).setOutputType(outputType).setMethodSignatures(Collections.emptyList()).build();
String results = writeStatements(ServiceClientMethodSampleComposer.composeCanonicalSample(method, clientType, resourceNames, messageTypes));
String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", " EchoRequest request =\n", " EchoRequest.newBuilder()\n", " .setName(FoobarName.ofProjectFoobarName(\"[PROJECT]\"," + " \"[FOOBAR]\").toString())\n", " .setParent(FoobarName.ofProjectFoobarName(\"[PROJECT]\"," + " \"[FOOBAR]\").toString())\n", " .setSeverity(Severity.forNumber(0))\n", " .setFoobar(Foobar.newBuilder().build())\n", " .build();\n", " echoClient.echo(request);\n", "}");
Assert.assertEquals(results, expected);
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class SettingsSampleComposerTest method composeSettingsSample_serviceStubClass.
@Test
public void composeSettingsSample_serviceStubClass() {
TypeNode classType = TypeNode.withReference(VaporReference.builder().setName("EchoStubSettings").setPakkage("com.google.showcase.v1beta1").build());
Optional<String> results = writeSample(SettingsSampleComposer.composeSettingsSample(Optional.of("Echo"), "EchoSettings", classType));
String expected = LineFormatter.lines("EchoStubSettings.Builder echoSettingsBuilder = EchoStubSettings.newBuilder();\n", "echoSettingsBuilder\n", " .echoSettings()\n", " .setRetrySettings(\n", " echoSettingsBuilder\n", " .echoSettings()\n", " .getRetrySettings()\n", " .toBuilder()\n", " .setTotalTimeout(Duration.ofSeconds(30))\n", " .build());\n", "EchoStubSettings echoSettings = echoSettingsBuilder.build();");
assertEquals(results.get(), expected);
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class SampleCodeWriterTest method setUp.
@BeforeClass
public static void setUp() {
TypeNode settingType = TypeNode.withReference(ConcreteReference.withClazz(ClientSettings.class));
Variable aVar = Variable.builder().setName("clientSettings").setType(settingType).build();
VariableExpr aVarExpr = VariableExpr.withVariable(aVar);
MethodInvocationExpr aValueExpr = MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName("newBuilder").setStaticReferenceType(settingType).build()).setReturnType(settingType).setMethodName("build").build();
AssignmentExpr assignmentExpr = AssignmentExpr.builder().setVariableExpr(aVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(aValueExpr).build();
TryCatchStatement sampleStatement = TryCatchStatement.builder().setTryResourceExpr(createAssignmentExpr("aBool", "false", TypeNode.BOOLEAN)).setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))).setIsSampleCode(true).build();
testingSampleStatements = Arrays.asList(ExprStatement.withExpr(assignmentExpr), sampleStatement);
regionTag = RegionTag.builder().setApiShortName("testing").setApiVersion("v1").setServiceName("samples").setRpcName("write").build();
testingSample = Sample.builder().setFileHeader(Arrays.asList(CommentStatement.withComment(BlockComment.withComment("Apache License")))).setBody(testingSampleStatements).setRegionTag(regionTag).build();
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class ServiceClientCallableMethodSampleComposerTest method invalid_composePagedCallableMethod_inputTypeNotExistInMessage.
@Test
public void invalid_composePagedCallableMethod_inputTypeNotExistInMessage() {
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("PagedExpandResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
Method method = Method.builder().setName("PagedExpand").setInputType(inputType).setOutputType(outputType).setPageSizeFieldName(PAGINATED_FIELD_NAME).build();
Assert.assertThrows(NullPointerException.class, () -> ServiceClientCallableMethodSampleComposer.composePagedCallableMethod(method, clientType, resourceNames, messageTypes));
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class ServiceClientCallableMethodSampleComposerTest method valid_composePagedCallableMethod.
/*Testing composePagedCallableMethod*/
@Test
public void valid_composePagedCallableMethod() {
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("PagedExpandRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("PagedExpandResponse").setPakkage(SHOWCASE_PACKAGE_NAME).build());
Method method = Method.builder().setName("PagedExpand").setInputType(inputType).setOutputType(outputType).setPageSizeFieldName(PAGINATED_FIELD_NAME).build();
String results = writeStatements(ServiceClientCallableMethodSampleComposer.composePagedCallableMethod(method, clientType, resourceNames, messageTypes));
String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", " PagedExpandRequest request =\n", " PagedExpandRequest.newBuilder()\n", " .setContent(\"content951530617\")\n", " .setPageSize(883849137)\n", " .setPageToken(\"pageToken873572522\")\n", " .build();\n", " ApiFuture<EchoResponse> future =" + " echoClient.pagedExpandPagedCallable().futureCall(request);\n", " // Do something.\n", " for (EchoResponse element : future.get().iterateAll()) {\n", " // doThingsWith(element);\n", " }\n", "}");
Assert.assertEquals(results, expected);
}
Aggregations