use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class MockServiceImplClassComposer method createAddResponseMethod.
private static MethodDefinition createAddResponseMethod() {
VariableExpr responseArgExpr = VariableExpr.withVariable(Variable.builder().setName("response").setType(FIXED_TYPESTORE.get("AbstractMessage")).build());
Expr methodInvocationExpr = MethodInvocationExpr.builder().setMethodName("add").setArguments(Arrays.asList(responseArgExpr)).setExprReferenceExpr(responsesVarExpr).build();
return MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("addResponse").setArguments(Arrays.asList(responseArgExpr.toBuilder().setIsDecl(true).build())).setBody(Arrays.asList(ExprStatement.withExpr(methodInvocationExpr))).build();
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class MockServiceImplClassComposer method createAddExceptionMethod.
private static MethodDefinition createAddExceptionMethod() {
VariableExpr exceptionArgExpr = VariableExpr.withVariable(Variable.builder().setName("exception").setType(TypeNode.withReference(ConcreteReference.withClazz(Exception.class))).build());
Expr methodInvocationExpr = MethodInvocationExpr.builder().setMethodName("add").setArguments(Arrays.asList(exceptionArgExpr)).setExprReferenceExpr(responsesVarExpr).build();
return MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("addException").setArguments(Arrays.asList(exceptionArgExpr.toBuilder().setIsDecl(true).build())).setBody(Arrays.asList(ExprStatement.withExpr(methodInvocationExpr))).build();
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class MockServiceImplClassComposer method createGenericClientStreamingProtoMethodOverride.
private static MethodDefinition createGenericClientStreamingProtoMethodOverride(Method protoMethod, VariableExpr responseObserverVarExpr, VariableExpr localResponseVarExpr) {
ConcreteReference streamObserverRef = ConcreteReference.withClazz(StreamObserver.class);
TypeNode returnType = TypeNode.withReference(streamObserverRef.copyAndSetGenerics(Arrays.asList(protoMethod.inputType().reference())));
VariableExpr requestObserverVarExpr = VariableExpr.withVariable(Variable.builder().setName("requestObserver").setType(returnType).build());
return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(returnType).setName(JavaStyle.toLowerCamelCase(protoMethod.name())).setArguments(Arrays.asList(responseObserverVarExpr.toBuilder().setIsFinal(true).setIsDecl(true).build())).setBody(Arrays.asList(ExprStatement.withExpr(AssignmentExpr.builder().setVariableExpr(requestObserverVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(createStreamObserverClassInstance(protoMethod, returnType, responseObserverVarExpr, localResponseVarExpr)).build()))).setReturnExpr(requestObserverVarExpr).build();
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class MockServiceImplClassComposer method createGenericProtoMethodOverride.
private static MethodDefinition createGenericProtoMethodOverride(Method protoMethod) {
ConcreteReference streamObserverRef = ConcreteReference.withClazz(StreamObserver.class);
TypeNode objectType = TypeNode.withReference(javaObjectReference);
VariableExpr localResponseVarExpr = VariableExpr.withVariable(Variable.builder().setName("response").setType(objectType).build());
VariableExpr responseObserverVarExpr = VariableExpr.withVariable(Variable.builder().setName("responseObserver").setType(TypeNode.withReference(streamObserverRef.copyAndSetGenerics(Arrays.asList(protoMethod.outputType().reference())))).build());
if (protoMethod.stream().equals(Stream.CLIENT) || protoMethod.stream().equals(Stream.BIDI)) {
return createGenericClientStreamingProtoMethodOverride(protoMethod, responseObserverVarExpr, localResponseVarExpr);
}
VariableExpr requestArgVarExpr = VariableExpr.withVariable(Variable.builder().setName("request").setType(protoMethod.inputType()).build());
return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName(JavaStyle.toLowerCamelCase(protoMethod.name())).setArguments(Arrays.asList(requestArgVarExpr.toBuilder().setIsDecl(true).build(), responseObserverVarExpr.toBuilder().setIsDecl(true).build())).setBody(Arrays.asList(ExprStatement.withExpr(AssignmentExpr.builder().setVariableExpr(localResponseVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setMethodName("poll").setExprReferenceExpr(responsesVarExpr).setReturnType(objectType).build()).build()), createHandleObjectStatement(protoMethod, requestArgVarExpr, responseObserverVarExpr, localResponseVarExpr))).build();
}
use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.
the class ServiceClientTestClassComposer method constructRpcTestCheckerLogic.
@Override
protected List<Statement> constructRpcTestCheckerLogic(Method method, Service service, boolean isRequestArg, Map<String, VariableExpr> classMemberVarExprs, VariableExpr requestVarExpr, Message requestMessage, List<VariableExpr> argExprs) {
List<Expr> methodExprs = new ArrayList<>();
List<Statement> methodStatements = new ArrayList<>();
// Construct the request checker logic.
VariableExpr actualRequestsVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(AbstractMessage.class))).build())).setName("actualRequests").build());
methodExprs.add(AssignmentExpr.builder().setVariableExpr(actualRequestsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(classMemberVarExprs.get(getMockServiceVarName(service))).setMethodName("getRequests").setReturnType(actualRequestsVarExpr.type()).build()).build());
methodExprs.add(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Assert")).setMethodName("assertEquals").setArguments(ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("1").build()), MethodInvocationExpr.builder().setExprReferenceExpr(actualRequestsVarExpr).setMethodName("size").build()).build());
VariableExpr actualRequestVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("actualRequest").build());
Expr getFirstRequestExpr = MethodInvocationExpr.builder().setExprReferenceExpr(actualRequestsVarExpr).setMethodName("get").setArguments(ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("0").build())).setReturnType(FIXED_TYPESTORE.get("AbstractMessage")).build();
getFirstRequestExpr = CastExpr.builder().setType(method.inputType()).setExpr(getFirstRequestExpr).build();
methodExprs.add(AssignmentExpr.builder().setVariableExpr(actualRequestVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(getFirstRequestExpr).build());
methodStatements.addAll(methodExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()));
methodExprs.clear();
methodStatements.add(EMPTY_LINE_STATEMENT);
// Assert field equality.
if (isRequestArg) {
// TODO(miraleung): Replace these with a simple request object equals?
Preconditions.checkNotNull(requestVarExpr);
Preconditions.checkNotNull(requestMessage);
for (Field field : requestMessage.fields()) {
String fieldGetterMethodNamePatternTemp = "get%s";
if (field.isRepeated()) {
fieldGetterMethodNamePatternTemp = field.isMap() ? "get%sMap" : "get%sList";
}
final String fieldGetterMethodNamePattern = fieldGetterMethodNamePatternTemp;
Function<VariableExpr, Expr> checkExprFn = v -> MethodInvocationExpr.builder().setExprReferenceExpr(v).setMethodName(String.format(fieldGetterMethodNamePattern, JavaStyle.toUpperCamelCase(field.name()))).build();
Expr expectedFieldExpr = checkExprFn.apply(requestVarExpr);
Expr actualFieldExpr = checkExprFn.apply(actualRequestVarExpr);
List<Expr> assertEqualsArguments = new ArrayList<>();
assertEqualsArguments.add(expectedFieldExpr);
assertEqualsArguments.add(actualFieldExpr);
if (TypeNode.isFloatingPointType(field.type())) {
boolean isFloat = field.type().equals(TypeNode.FLOAT);
assertEqualsArguments.add(ValueExpr.withValue(PrimitiveValue.builder().setType(isFloat ? TypeNode.FLOAT : TypeNode.DOUBLE).setValue(String.format("0.0001%s", isFloat ? "f" : "")).build()));
}
methodExprs.add(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Assert")).setMethodName("assertEquals").setArguments(assertEqualsArguments).build());
}
} else {
for (VariableExpr argVarExpr : argExprs) {
Variable variable = argVarExpr.variable();
String fieldGetterMethodNamePattern = "get%s";
if (LIST_TYPE.isSupertypeOrEquals(variable.type())) {
fieldGetterMethodNamePattern = "get%sList";
} else if (MAP_TYPE.isSupertypeOrEquals(variable.type())) {
fieldGetterMethodNamePattern = "get%sMap";
}
Expr actualFieldExpr = MethodInvocationExpr.builder().setExprReferenceExpr(actualRequestVarExpr).setMethodName(String.format(fieldGetterMethodNamePattern, JavaStyle.toUpperCamelCase(variable.identifier().name()))).build();
Expr expectedFieldExpr = argVarExpr;
if (RESOURCE_NAME_TYPE.isSupertypeOrEquals(argVarExpr.type())) {
expectedFieldExpr = MethodInvocationExpr.builder().setExprReferenceExpr(argVarExpr).setMethodName("toString").build();
}
methodExprs.add(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Assert")).setMethodName("assertEquals").setArguments(expectedFieldExpr, actualFieldExpr).build());
}
}
// Assert header equality.
Expr headerKeyExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ApiClientHeaderProvider")).setMethodName("getDefaultApiClientHeaderKey").build();
Expr headerPatternExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_GRPC_TYPESTORE.get("GaxGrpcProperties")).setMethodName("getDefaultApiClientHeaderPattern").build();
Expr headerSentExpr = MethodInvocationExpr.builder().setExprReferenceExpr(classMemberVarExprs.get("channelProvider")).setMethodName("isHeaderSent").setArguments(headerKeyExpr, headerPatternExpr).build();
methodExprs.add(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Assert")).setMethodName("assertTrue").setArguments(headerSentExpr).build());
methodStatements.addAll(methodExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()));
methodExprs.clear();
return methodStatements;
}
Aggregations