use of com.google.api.generator.engine.ast.TryCatchStatement in project gapic-generator-java by googleapis.
the class AbstractServiceClientTestClassComposer method createRpcExceptionTestStatements.
protected List<Statement> createRpcExceptionTestStatements(Method method, List<MethodArgument> methodSignature, Map<String, VariableExpr> classMemberVarExprs, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
List<VariableExpr> argVarExprs = new ArrayList<>();
List<Expr> tryBodyExprs = new ArrayList<>();
if (methodSignature.isEmpty()) {
// Construct the actual request.
VariableExpr varExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("request").build());
argVarExprs.add(varExpr);
Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
Preconditions.checkNotNull(requestMessage);
Map<String, String> valuePatterns = Collections.emptyMap();
if (getTransportContext().useValuePatterns() && method.hasHttpBindings()) {
valuePatterns = method.httpBindings().getPathParametersValuePatterns();
}
Expr valExpr = DefaultValueComposer.createSimpleMessageBuilderValue(requestMessage, resourceNames, messageTypes, valuePatterns);
tryBodyExprs.add(AssignmentExpr.builder().setVariableExpr(varExpr.toBuilder().setIsDecl(true).build()).setValueExpr(valExpr).build());
} else {
Map<String, String> valuePatterns = Collections.emptyMap();
if (getTransportContext().useValuePatterns() && method.hasHttpBindings()) {
valuePatterns = method.httpBindings().getPathParametersValuePatterns();
}
for (MethodArgument methodArg : methodSignature) {
String methodArgName = JavaStyle.toLowerCamelCase(methodArg.name());
VariableExpr varExpr = VariableExpr.withVariable(Variable.builder().setType(methodArg.type()).setName(methodArgName).build());
argVarExprs.add(varExpr);
Expr valExpr = DefaultValueComposer.createMethodArgValue(methodArg, resourceNames, messageTypes, valuePatterns);
tryBodyExprs.add(AssignmentExpr.builder().setVariableExpr(varExpr.toBuilder().setIsDecl(true).build()).setValueExpr(valExpr).build());
}
}
String rpcJavaName = JavaStyle.toLowerCamelCase(method.name());
if (method.hasLro()) {
rpcJavaName += "Async";
}
MethodInvocationExpr rpcJavaMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(classMemberVarExprs.get("client")).setMethodName(rpcJavaName).setArguments(argVarExprs.stream().map(e -> (Expr) e).collect(Collectors.toList())).build();
if (method.hasLro()) {
rpcJavaMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(rpcJavaMethodInvocationExpr).setMethodName("get").build();
}
tryBodyExprs.add(rpcJavaMethodInvocationExpr);
VariableExpr catchExceptionVarExpr = VariableExpr.builder().setVariable(Variable.builder().setType(TypeNode.withExceptionClazz(method.hasLro() ? ExecutionException.class : InvalidArgumentException.class)).setName("e").build()).build();
List<Statement> catchBody = method.hasLro() ? createRpcLroExceptionTestCatchBody(catchExceptionVarExpr, false) : Arrays.asList(CommentStatement.withComment(LineComment.withComment("Expected exception.")));
// Assert a failure if no exception was raised.
tryBodyExprs.add(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Assert")).setMethodName("fail").setArguments(ValueExpr.withValue(StringObjectValue.withValue("No exception raised"))).build());
TryCatchStatement tryCatchBlock = TryCatchStatement.builder().setTryBody(tryBodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).addCatch(catchExceptionVarExpr.toBuilder().setIsDecl(true).build(), catchBody).build();
return Arrays.asList(EMPTY_LINE_STATEMENT, tryCatchBlock);
}
use of com.google.api.generator.engine.ast.TryCatchStatement in project gapic-generator-java by googleapis.
the class ServiceClientTestClassComposer method createStreamingRpcExceptionTestStatements.
@Override
protected List<Statement> createStreamingRpcExceptionTestStatements(Method method, Map<String, VariableExpr> classMemberVarExprs, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes) {
// Build the request variable and assign it.
VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("request").build());
Message requestMessage = messageTypes.get(method.inputType().reference().fullName());
Preconditions.checkNotNull(requestMessage);
Expr valExpr = DefaultValueComposer.createSimpleMessageBuilderValue(requestMessage, resourceNames, messageTypes);
List<Statement> statements = new ArrayList<>();
statements.add(ExprStatement.withExpr(AssignmentExpr.builder().setVariableExpr(requestVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(valExpr).build()));
statements.add(EMPTY_LINE_STATEMENT);
// Build the responseObserver variable.
VariableExpr responseObserverVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(FIXED_GRPC_TYPESTORE.get("MockStreamObserver").reference().copyAndSetGenerics(Arrays.asList(method.outputType().reference())))).setName("responseObserver").build());
statements.add(ExprStatement.withExpr(AssignmentExpr.builder().setVariableExpr(responseObserverVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(NewObjectExpr.builder().setType(FIXED_GRPC_TYPESTORE.get("MockStreamObserver")).setIsGeneric(true).build()).build()));
statements.add(EMPTY_LINE_STATEMENT);
// Build the callable variable and assign it.
VariableExpr callableVarExpr = VariableExpr.withVariable(Variable.builder().setType(getCallableType(method)).setName("callable").build());
Expr streamingCallExpr = MethodInvocationExpr.builder().setExprReferenceExpr(classMemberVarExprs.get("client")).setMethodName(String.format("%sCallable", JavaStyle.toLowerCamelCase(method.name()))).setReturnType(callableVarExpr.type()).build();
List<Expr> exprs = new ArrayList<>();
exprs.add(AssignmentExpr.builder().setVariableExpr(callableVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(streamingCallExpr).build());
if (method.stream().equals(Method.Stream.SERVER)) {
exprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(callableVarExpr).setMethodName("serverStreamingCall").setArguments(requestVarExpr, responseObserverVarExpr).build());
} else {
// Call the streaming-variant callable method.
VariableExpr requestObserverVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(FIXED_TYPESTORE.get("ApiStreamObserver").reference().copyAndSetGenerics(Arrays.asList(method.inputType().reference())))).setName("requestObserver").build());
List<Expr> callableMethodArgs = new ArrayList<>();
if (!method.stream().equals(Method.Stream.BIDI) && !method.stream().equals(Method.Stream.CLIENT)) {
callableMethodArgs.add(requestVarExpr);
}
callableMethodArgs.add(responseObserverVarExpr);
exprs.add(AssignmentExpr.builder().setVariableExpr(requestObserverVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(callableVarExpr).setMethodName(getCallableMethodName(method)).setArguments(callableMethodArgs).setReturnType(requestObserverVarExpr.type()).build()).build());
statements.addAll(exprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()));
exprs.clear();
statements.add(EMPTY_LINE_STATEMENT);
exprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(requestObserverVarExpr).setMethodName("onNext").setArguments(requestVarExpr).build());
}
statements.addAll(exprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()));
exprs.clear();
statements.add(EMPTY_LINE_STATEMENT);
List<Expr> tryBodyExprs = new ArrayList<>();
// TODO(v2): This variable is unused in the generated test, it can be deleted.
VariableExpr actualResponsesVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(method.outputType().reference())).build())).setName("actualResponses").build());
Expr getFutureResponseExpr = MethodInvocationExpr.builder().setExprReferenceExpr(responseObserverVarExpr).setMethodName("future").build();
getFutureResponseExpr = MethodInvocationExpr.builder().setExprReferenceExpr(getFutureResponseExpr).setMethodName("get").setReturnType(actualResponsesVarExpr.type()).build();
tryBodyExprs.add(AssignmentExpr.builder().setVariableExpr(actualResponsesVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(getFutureResponseExpr).build());
// Assert a failure if no exception was raised.
tryBodyExprs.add(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Assert")).setMethodName("fail").setArguments(ValueExpr.withValue(StringObjectValue.withValue("No exception thrown"))).build());
VariableExpr catchExceptionVarExpr = VariableExpr.builder().setVariable(Variable.builder().setType(TypeNode.withExceptionClazz(ExecutionException.class)).setName("e").build()).build();
TryCatchStatement tryCatchBlock = TryCatchStatement.builder().setTryBody(tryBodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).addCatch(catchExceptionVarExpr.toBuilder().setIsDecl(true).build(), createRpcLroExceptionTestCatchBody(catchExceptionVarExpr, true)).build();
statements.add(tryCatchBlock);
return statements;
}
use of com.google.api.generator.engine.ast.TryCatchStatement in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method createSampleCode.
private static String createSampleCode() {
JavaWriterVisitor writerVisitor = new JavaWriterVisitor();
TryCatchStatement tryCatch = TryCatchStatement.builder().setTryResourceExpr(createAssignmentExpr("condition", "false", TypeNode.BOOLEAN)).setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))).setIsSampleCode(true).build();
tryCatch.accept(writerVisitor);
return writerVisitor.write();
}
use of com.google.api.generator.engine.ast.TryCatchStatement in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method createPrintShelfListToFile.
private MethodDefinition createPrintShelfListToFile() {
ConcreteReference stringBuilderRef = ConcreteReference.builder().setClazz(StringBuilder.class).build();
ConcreteReference fileWriterRef = ConcreteReference.builder().setClazz(FileWriter.class).build();
Variable stringBuilderVar = createVarFromConcreteRef(stringBuilderRef, "sb");
Variable fileNameVar = createVarFromType(TypeNode.STRING, "fileName");
Variable shelfObject = createVarFromVaporRef(shelfClassRef, "s");
Variable fileWriterVar = createVarFromConcreteRef(fileWriterRef, "fileWriter");
Variable ioException = createVarFromConcreteRef(ConcreteReference.withClazz(IOException.class), "e");
VariableExpr shelfNameFromShelfObject = fieldFromShelfObjectExpr(shelfObject, shelfNameVar);
VariableExpr seriesNumFromShelfObject = fieldFromShelfObjectExpr(shelfObject, seriesNumVar);
AssignmentExpr createStringBuilderExpr = createAssignmentExpr(createVarDeclExpr(stringBuilderVar), NewObjectExpr.withType(TypeNode.withReference(stringBuilderRef)));
AssignmentExpr createFileWriterExpr = createAssignmentExpr(createVarDeclExpr(fileWriterVar), NewObjectExpr.builder().setType(TypeNode.withReference(fileWriterRef)).setArguments(Arrays.asList(VariableExpr.withVariable(fileNameVar))).build());
MethodInvocationExpr appendShelfName = methodExprWithRefAndArg(stringBuilderVar, "append", Arrays.asList(shelfNameFromShelfObject));
MethodInvocationExpr appendSeriesNum = MethodInvocationExpr.builder().setMethodName("append").setExprReferenceExpr(appendShelfName).setArguments(seriesNumFromShelfObject).build();
MethodInvocationExpr stringBuilderToString = methodExprWithRef(stringBuilderVar, "toString");
MethodInvocationExpr writeToFileWriter = methodExprWithRefAndArg(fileNameVar, "write", Arrays.asList(stringBuilderToString));
MethodInvocationExpr closeFileWriter = methodExprWithRef(fileNameVar, "close");
MethodInvocationExpr printError = methodExprWithRef(ioException, "printStackTrace");
ForStatement loopShelfList = ForStatement.builder().setLocalVariableExpr(createVarDeclExpr(shelfObject)).setCollectionExpr(VariableExpr.withVariable(shelfListVar)).setBody(Arrays.asList(ExprStatement.withExpr(appendSeriesNum))).build();
TryCatchStatement tryCatchStatement = TryCatchStatement.builder().setTryBody(Arrays.asList(ExprStatement.withExpr(createFileWriterExpr), loopShelfList, ExprStatement.withExpr(writeToFileWriter), ExprStatement.withExpr(closeFileWriter))).addCatch(createVarDeclExpr(ioException), Arrays.asList(ExprStatement.withExpr(printError))).build();
return MethodDefinition.builder().setName("printShelfListToFile").setReturnType(TypeNode.VOID).setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(createStringBuilderExpr), tryCatchStatement)).setArguments(Arrays.asList(createVarDeclExpr(fileNameVar))).build();
}
use of com.google.api.generator.engine.ast.TryCatchStatement 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();
}
Aggregations