Search in sources :

Example 1 with GaxGrpcProperties

use of com.google.api.gax.grpc.GaxGrpcProperties 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;
}
Also used : LocalChannelProvider(com.google.api.gax.grpc.testing.LocalChannelProvider) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) Variable(com.google.api.generator.engine.ast.Variable) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) GaxGrpcProperties(com.google.api.gax.grpc.GaxGrpcProperties) DefaultValueComposer(com.google.api.generator.gapic.composer.defaultvalue.DefaultValueComposer) Field(com.google.api.generator.gapic.model.Field) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) Method(com.google.api.generator.gapic.model.Method) MethodArgument(com.google.api.generator.gapic.model.MethodArgument) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) Map(java.util.Map) MockServiceHelper(com.google.api.gax.grpc.testing.MockServiceHelper) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) Collectors(java.util.stream.Collectors) ClassNames(com.google.api.generator.gapic.composer.utils.ClassNames) List(java.util.List) GapicContext(com.google.api.generator.gapic.model.GapicContext) MockGrpcService(com.google.api.gax.grpc.testing.MockGrpcService) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) AbstractServiceClientTestClassComposer(com.google.api.generator.gapic.composer.common.AbstractServiceClientTestClassComposer) Function(java.util.function.Function) MockStreamObserver(com.google.api.gax.grpc.testing.MockStreamObserver) ArrayList(java.util.ArrayList) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) AbstractMessage(com.google.protobuf.AbstractMessage) CastExpr(com.google.api.generator.engine.ast.CastExpr) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) ResourceName(com.google.api.generator.gapic.model.ResourceName) StatusRuntimeException(io.grpc.StatusRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) Statement(com.google.api.generator.engine.ast.Statement) Service(com.google.api.generator.gapic.model.Service) TreeMap(java.util.TreeMap) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) Preconditions(com.google.common.base.Preconditions) StatusCode(com.google.api.gax.rpc.StatusCode) Message(com.google.api.generator.gapic.model.Message) Variable(com.google.api.generator.engine.ast.Variable) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) ArrayList(java.util.ArrayList) Field(com.google.api.generator.gapic.model.Field) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

GaxGrpcProperties (com.google.api.gax.grpc.GaxGrpcProperties)1 LocalChannelProvider (com.google.api.gax.grpc.testing.LocalChannelProvider)1 MockGrpcService (com.google.api.gax.grpc.testing.MockGrpcService)1 MockServiceHelper (com.google.api.gax.grpc.testing.MockServiceHelper)1 MockStreamObserver (com.google.api.gax.grpc.testing.MockStreamObserver)1 StatusCode (com.google.api.gax.rpc.StatusCode)1 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)1 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)1 CastExpr (com.google.api.generator.engine.ast.CastExpr)1 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)1 EnumRefExpr (com.google.api.generator.engine.ast.EnumRefExpr)1 Expr (com.google.api.generator.engine.ast.Expr)1 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)1 InstanceofExpr (com.google.api.generator.engine.ast.InstanceofExpr)1 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)1 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)1 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)1 PrimitiveValue (com.google.api.generator.engine.ast.PrimitiveValue)1 ScopeNode (com.google.api.generator.engine.ast.ScopeNode)1 Statement (com.google.api.generator.engine.ast.Statement)1