use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class ServiceClientCallableMethodSampleComposerTest method valid_composeLroCallableMethod_withReturnVoid.
@Test
public void valid_composeLroCallableMethod_withReturnVoid() {
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("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).setLro(lro).build();
String results = writeStatements(ServiceClientCallableMethodSampleComposer.composeLroCallableMethod(method, clientType, resourceNames, messageTypes));
String expected = LineFormatter.lines("try (EchoClient echoClient = EchoClient.create()) {\n", " WaitRequest request = WaitRequest.newBuilder().build();\n", " OperationFuture<Empty, WaitMetadata> future =\n", " echoClient.waitOperationCallable().futureCall(request);\n", " // Do something.\n", " future.get();\n", "}");
Assert.assertEquals(results, expected);
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeThrowExpr_basicWithCause.
@Test
public void writeThrowExpr_basicWithCause() {
TypeNode npeType = TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class));
ThrowExpr throwExpr = ThrowExpr.builder().setType(npeType).setCauseExpr(NewObjectExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class))).build()).build();
throwExpr.accept(writerVisitor);
assertEquals("throw new NullPointerException(new Throwable())", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeLogicalOperationExpr_logicalAnd.
@Test
public void writeLogicalOperationExpr_logicalAnd() {
VariableExpr lhsExpr = VariableExpr.withVariable(createVariable("isEmpty", TypeNode.BOOLEAN));
VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
TypeNode classType = TypeNode.withReference(ref);
MethodInvocationExpr rhsExpr = MethodInvocationExpr.builder().setMethodName("isValid").setExprReferenceExpr(ValueExpr.withValue(ThisObjectValue.withType(classType))).setReturnType(TypeNode.BOOLEAN).build();
LogicalOperationExpr logicalOperationExpr = LogicalOperationExpr.logicalAndWithExprs(lhsExpr, rhsExpr);
logicalOperationExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("isEmpty && this.isValid()");
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAnonymousClassExpr_basic.
@Test
public void writeAnonymousClassExpr_basic() {
ConcreteReference ref = ConcreteReference.withClazz(Runnable.class);
TypeNode type = TypeNode.withReference(ref);
AssignmentExpr assignmentExpr = createAssignmentExpr("foobar", "false", TypeNode.BOOLEAN);
ExprStatement statement = ExprStatement.withExpr(assignmentExpr);
MethodDefinition method = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("run").setIsOverride(true).setBody(Arrays.asList(statement)).build();
AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setMethods(Arrays.asList(method)).build();
anonymousClassExpr.accept(writerVisitor);
assertEquals(LineFormatter.lines("new Runnable() {\n", "@Override\n", "public void run() {\n", "boolean foobar = false;\n}\n\n}"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class ServiceClientCallableMethodSampleComposerTest method invalid_composePagedCallableMethod_noExistMethodResponse.
@Test
public void invalid_composePagedCallableMethod_noExistMethodResponse() {
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("EchoRequest").setPakkage(SHOWCASE_PACKAGE_NAME).build());
TypeNode outputType = TypeNode.withReference(VaporReference.builder().setName("NoExistResponse").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));
}
Aggregations