use of com.google.api.generator.engine.ast.VariableExpr 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.VariableExpr in project gapic-generator-java by googleapis.
the class ServiceClientTestClassComposer method createSetUpMethod.
@Override
protected MethodDefinition createSetUpMethod(Service service, Map<String, VariableExpr> classMemberVarExprs, TypeStore typeStore) {
VariableExpr clientVarExpr = classMemberVarExprs.get(CLIENT_VAR_NAME);
VariableExpr serviceHelperVarExpr = classMemberVarExprs.get(SERVICE_HELPER_VAR_NAME);
VariableExpr channelProviderVarExpr = classMemberVarExprs.get(CHANNEL_PROVIDER_VAR_NAME);
Expr resetServiceHelperExpr = MethodInvocationExpr.builder().setExprReferenceExpr(serviceHelperVarExpr).setMethodName("reset").build();
Expr channelProviderInitExpr = AssignmentExpr.builder().setVariableExpr(channelProviderVarExpr).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(serviceHelperVarExpr).setMethodName("createChannelProvider").setReturnType(channelProviderVarExpr.type()).build()).build();
TypeNode settingsType = typeStore.get(ClassNames.getServiceSettingsClassName(service));
VariableExpr localSettingsVarExpr = VariableExpr.withVariable(Variable.builder().setName("settings").setType(settingsType).build());
Expr settingsBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName("newBuilder").build();
Function<Expr, BiFunction<String, Expr, MethodInvocationExpr>> methodBuilderFn = methodExpr -> (mName, argExpr) -> MethodInvocationExpr.builder().setExprReferenceExpr(methodExpr).setMethodName(mName).setArguments(Arrays.asList(argExpr)).build();
settingsBuilderExpr = methodBuilderFn.apply(settingsBuilderExpr).apply("setTransportChannelProvider", classMemberVarExprs.get(CHANNEL_PROVIDER_VAR_NAME));
settingsBuilderExpr = methodBuilderFn.apply(settingsBuilderExpr).apply("setCredentialsProvider", MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("NoCredentialsProvider")).setMethodName("create").build());
settingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderExpr).setMethodName("build").setReturnType(localSettingsVarExpr.type()).build();
Expr initLocalSettingsExpr = AssignmentExpr.builder().setVariableExpr(localSettingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(settingsBuilderExpr).build();
Expr initClientExpr = AssignmentExpr.builder().setVariableExpr(clientVarExpr).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get(ClassNames.getServiceClientClassName(service))).setMethodName("create").setArguments(Arrays.asList(localSettingsVarExpr)).setReturnType(clientVarExpr.type()).build()).build();
return MethodDefinition.builder().setAnnotations(Arrays.asList(AnnotationNode.withType(FIXED_TYPESTORE.get("Before")))).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("setUp").setThrowsExceptions(Arrays.asList(FIXED_TYPESTORE.get("IOException"))).setBody(Arrays.asList(resetServiceHelperExpr, channelProviderInitExpr, initLocalSettingsExpr, initClientExpr).stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build();
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createToStringListMethod.
private static MethodDefinition createToStringListMethod(TypeNode thisClassType) {
TypeNode listClassType = TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(thisClassType.reference())).build());
VariableExpr valuesVarExpr = VariableExpr.withVariable(Variable.builder().setName("values").setType(listClassType).build());
TypeNode listStringType = TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class))).build());
VariableExpr listVarExpr = VariableExpr.withVariable(Variable.builder().setName("list").setType(listStringType).build());
AssignmentExpr listAssignExpr = AssignmentExpr.builder().setVariableExpr(listVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(NewObjectExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(ArrayList.class))).setIsGeneric(true).setArguments(Arrays.asList(MethodInvocationExpr.builder().setExprReferenceExpr(valuesVarExpr).setMethodName("size").build())).build()).build();
VariableExpr valueVarExpr = VariableExpr.withVariable(Variable.builder().setName("value").setType(thisClassType).build());
// We use an equality check instead of Objects.isNull() for Java 7 compatibility.
Expr isNullCheck = RelationalOperationExpr.equalToWithExprs(valueVarExpr, ValueExpr.createNullExpr());
Statement listAddEmptyStringStatement = ExprStatement.withExpr(MethodInvocationExpr.builder().setExprReferenceExpr(listVarExpr).setMethodName("add").setArguments(Arrays.asList(ValueExpr.withValue(StringObjectValue.withValue("")))).build());
Statement listAddValueStatement = ExprStatement.withExpr(MethodInvocationExpr.builder().setExprReferenceExpr(listVarExpr).setMethodName("add").setArguments(Arrays.asList(MethodInvocationExpr.builder().setExprReferenceExpr(valueVarExpr).setMethodName("toString").build())).build());
IfStatement ifStatement = IfStatement.builder().setConditionExpr(isNullCheck).setBody(Arrays.asList(listAddEmptyStringStatement)).setElseBody(Arrays.asList(listAddValueStatement)).build();
ForStatement forStatement = ForStatement.builder().setLocalVariableExpr(valueVarExpr.toBuilder().setIsDecl(true).build()).setCollectionExpr(valuesVarExpr).setBody(Arrays.asList(ifStatement)).build();
return MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(listStringType).setName("toStringList").setArguments(Arrays.asList(valuesVarExpr.toBuilder().setIsDecl(true).build())).setBody(Arrays.asList(ExprStatement.withExpr(listAssignExpr), forStatement)).setReturnExpr(listVarExpr).build();
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createFixedClassMemberVariables.
private static Map<String, VariableExpr> createFixedClassMemberVariables() {
Map<String, TypeNode> memberVars = new HashMap<>();
Reference stringRef = ConcreteReference.withClazz(String.class);
memberVars.put("fieldValuesMap", TypeNode.withReference(ConcreteReference.builder().setClazz(Map.class).setGenerics(Arrays.asList(stringRef, stringRef)).build()));
memberVars.put("pathTemplate", TypeNode.withReference(ConcreteReference.withClazz(PathTemplate.class)));
memberVars.put("fixedValue", TypeNode.STRING);
return memberVars.entrySet().stream().map(e -> Variable.builder().setName(e.getKey()).setType(e.getValue()).build()).collect(Collectors.toMap(v -> v.identifier().name(), v -> VariableExpr.withVariable(v)));
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createClassStatements.
private static List<Statement> createClassStatements(List<VariableExpr> templateFinalVarExprs, Map<String, VariableExpr> patternTokenVarExprs, List<String> patterns, List<List<String>> tokenHierarchies) {
List<Expr> memberVars = new ArrayList<>();
// "projects/{project}/locations/{location}/autoscalingPolicies/{autoscaling_policy}");
for (int i = 0; i < patterns.size(); i++) {
VariableExpr varExpr = templateFinalVarExprs.get(i).toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setIsFinal(true).build();
String pattern = patterns.get(i);
Expr valueExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("PathTemplate")).setMethodName("createWithoutUrlEncoding").setArguments(Arrays.asList(ValueExpr.withValue(StringObjectValue.withValue(pattern)))).setReturnType(FIXED_TYPESTORE.get("PathTemplate")).build();
// PubSub special-case handling for _deleted-topic_.
if (pattern.equals(ResourceNameConstants.DELETED_TOPIC_LITERAL)) {
valueExpr = ValueExpr.withValue(StringObjectValue.withValue(pattern));
}
memberVars.add(AssignmentExpr.builder().setVariableExpr(varExpr).setValueExpr(valueExpr).build());
}
memberVars.add(FIXED_CLASS_VARS.get("fieldValuesMap").toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsVolatile(true).build());
boolean hasVariants = tokenHierarchies.size() > 1;
if (hasVariants) {
Function<VariableExpr, VariableExpr> toDeclFn = v -> v.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).build();
memberVars.add(toDeclFn.apply(FIXED_CLASS_VARS.get("pathTemplate")));
memberVars.add(toDeclFn.apply(FIXED_CLASS_VARS.get("fixedValue")));
}
// Private per-token string variables.
// Use the token set as a key to maintain ordering (for consistency).
Function<VariableExpr, VariableExpr> toFinalDeclFn = v -> v.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsFinal(true).build();
// Special-cased PubSub handling.
List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(tokens -> !tokens.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
memberVars.addAll(getTokenSet(processedTokenHierarchies).stream().map(t -> toFinalDeclFn.apply(patternTokenVarExprs.get(t))).collect(Collectors.toList()));
return memberVars.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList());
}
Aggregations